2022-10-02 23:06:56 +02:00
|
|
|
package android.view;
|
|
|
|
|
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
2022-11-04 19:28:46 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
public class SurfaceView extends View {
|
2022-10-26 18:39:04 +02:00
|
|
|
|
2022-11-04 19:28:46 +01:00
|
|
|
final ArrayList<SurfaceHolder.Callback> mCallbacks = new ArrayList<SurfaceHolder.Callback>();
|
|
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
public SurfaceView(Context context) {
|
|
|
|
|
super(context);
|
|
|
|
|
|
2022-10-26 18:39:04 +02:00
|
|
|
native_constructor(context);
|
2022-11-02 14:37:34 +01:00
|
|
|
|
|
|
|
|
mSurface.widget = this.widget;
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-04 19:28:46 +01:00
|
|
|
private void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
|
|
|
|
for (SurfaceHolder.Callback c : mCallbacks) {
|
|
|
|
|
c.surfaceChanged(mSurfaceHolder, format, width, height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 18:39:04 +02:00
|
|
|
private native void native_constructor(Context context);
|
|
|
|
|
|
2022-11-04 19:28:46 +01:00
|
|
|
public SurfaceHolder getHolder() {
|
2022-10-26 18:39:04 +02:00
|
|
|
return mSurfaceHolder;
|
2022-11-04 19:28:46 +01:00
|
|
|
}
|
2022-10-26 18:39:04 +02:00
|
|
|
|
2022-10-02 23:06:56 +02:00
|
|
|
final Surface mSurface = new Surface();
|
|
|
|
|
|
|
|
|
|
private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
|
2022-10-26 18:39:04 +02:00
|
|
|
private static final String LOG_TAG = "SurfaceHolder";
|
2022-10-02 23:06:56 +02:00
|
|
|
|
2022-10-26 18:39:04 +02:00
|
|
|
@Override
|
|
|
|
|
public boolean isCreating() {
|
2022-11-04 19:28:46 +01:00
|
|
|
// return mIsCreating;
|
2022-10-02 23:06:56 +02:00
|
|
|
return false;
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addCallback(Callback callback) {
|
2022-11-04 19:28:46 +01:00
|
|
|
synchronized (mCallbacks) {
|
|
|
|
|
if (mCallbacks.contains(callback) == false) {
|
|
|
|
|
mCallbacks.add(callback);
|
|
|
|
|
}
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeCallback(Callback callback) {
|
2022-11-04 19:28:46 +01:00
|
|
|
/* synchronized (mCallbacks) {
|
2022-10-26 18:39:04 +02:00
|
|
|
mCallbacks.remove(callback);
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setFixedSize(int width, int height) {
|
2022-11-04 19:28:46 +01:00
|
|
|
/* if (mRequestedWidth != width || mRequestedHeight != height) {
|
2022-10-26 18:39:04 +02:00
|
|
|
mRequestedWidth = width;
|
|
|
|
|
mRequestedHeight = height;
|
|
|
|
|
requestLayout();
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setSizeFromLayout() {
|
2022-11-04 19:28:46 +01:00
|
|
|
/* if (mRequestedWidth != -1 || mRequestedHeight != -1) {
|
2022-10-26 18:39:04 +02:00
|
|
|
mRequestedWidth = mRequestedHeight = -1;
|
|
|
|
|
requestLayout();
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setFormat(int format) {
|
|
|
|
|
/*
|
|
|
|
|
// for backward compatibility reason, OPAQUE always
|
|
|
|
|
// means 565 for SurfaceView
|
|
|
|
|
if (format == PixelFormat.OPAQUE)
|
|
|
|
|
format = PixelFormat.RGB_565;
|
|
|
|
|
|
|
|
|
|
mRequestedFormat = format;
|
|
|
|
|
if (mWindow != null) {
|
|
|
|
|
updateWindow(false, false);
|
|
|
|
|
}*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated setType is now ignored.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
@Deprecated
|
|
|
|
|
public void setType(int type) { }
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setKeepScreenOn(boolean screenOn) {
|
2022-11-04 19:28:46 +01:00
|
|
|
// Message msg = mHandler.obtainMessage(KEEP_SCREEN_ON_MSG);
|
|
|
|
|
// msg.arg1 = screenOn ? 1 : 0;
|
|
|
|
|
// mHandler.sendMessage(msg);
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a {@link Canvas} for drawing into the SurfaceView's Surface
|
|
|
|
|
*
|
|
|
|
|
* After drawing into the provided {@link Canvas}, the caller must
|
|
|
|
|
* invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
|
|
|
|
|
*
|
|
|
|
|
* The caller must redraw the entire surface.
|
|
|
|
|
* @return A canvas for drawing into the surface.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Canvas lockCanvas() {
|
2022-11-04 19:28:46 +01:00
|
|
|
// return internalLockCanvas(null);
|
2022-10-26 18:39:04 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a {@link Canvas} for drawing into the SurfaceView's Surface
|
|
|
|
|
*
|
|
|
|
|
* After drawing into the provided {@link Canvas}, the caller must
|
|
|
|
|
* invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
|
|
|
|
|
*
|
|
|
|
|
* @param inOutDirty A rectangle that represents the dirty region that the caller wants
|
|
|
|
|
* to redraw. This function may choose to expand the dirty rectangle if for example
|
|
|
|
|
* the surface has been resized or if the previous contents of the surface were
|
|
|
|
|
* not available. The caller must redraw the entire dirty region as represented
|
|
|
|
|
* by the contents of the inOutDirty rectangle upon return from this function.
|
|
|
|
|
* The caller may also pass <code>null</code> instead, in the case where the
|
|
|
|
|
* entire surface should be redrawn.
|
|
|
|
|
* @return A canvas for drawing into the surface.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Canvas lockCanvas(Rect inOutDirty) {
|
2022-11-04 19:28:46 +01:00
|
|
|
// return internalLockCanvas(inOutDirty);
|
2022-10-02 23:06:56 +02:00
|
|
|
return null;
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final Canvas internalLockCanvas(Rect dirty) {
|
2022-11-04 19:28:46 +01:00
|
|
|
/* mSurfaceLock.lock();
|
2022-10-26 18:39:04 +02:00
|
|
|
|
|
|
|
|
if (DEBUG) Log.i(TAG, "Locking canvas... stopped="
|
|
|
|
|
+ mDrawingStopped + ", win=" + mWindow);
|
|
|
|
|
|
|
|
|
|
Canvas c = null;
|
|
|
|
|
if (!mDrawingStopped && mWindow != null) {
|
|
|
|
|
try {
|
|
|
|
|
c = mSurface.lockCanvas(dirty);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(LOG_TAG, "Exception locking surface", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DEBUG) Log.i(TAG, "Returned canvas: " + c);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
mLastLockTime = SystemClock.uptimeMillis();
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the Surface is not ready to be drawn, then return null,
|
|
|
|
|
// but throttle calls to this function so it isn't called more
|
|
|
|
|
// than every 100ms.
|
|
|
|
|
long now = SystemClock.uptimeMillis();
|
|
|
|
|
long nextTime = mLastLockTime + 100;
|
|
|
|
|
if (nextTime > now) {
|
|
|
|
|
try {
|
|
|
|
|
Thread.sleep(nextTime-now);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
}
|
|
|
|
|
now = SystemClock.uptimeMillis();
|
|
|
|
|
}
|
|
|
|
|
mLastLockTime = now;
|
|
|
|
|
mSurfaceLock.unlock();
|
|
|
|
|
*/
|
2022-10-02 23:06:56 +02:00
|
|
|
return null;
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Posts the new contents of the {@link Canvas} to the surface and
|
|
|
|
|
* releases the {@link Canvas}.
|
|
|
|
|
*
|
|
|
|
|
* @param canvas The canvas previously obtained from {@link #lockCanvas}.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void unlockCanvasAndPost(Canvas canvas) {
|
2022-11-04 19:28:46 +01:00
|
|
|
// mSurface.unlockCanvasAndPost(canvas);
|
|
|
|
|
// mSurfaceLock.unlock();
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Surface getSurface() {
|
|
|
|
|
return mSurface;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Rect getSurfaceFrame() {
|
2022-11-04 19:28:46 +01:00
|
|
|
// return mSurfaceFrame;
|
2022-10-02 23:06:56 +02:00
|
|
|
return null;
|
2022-10-26 18:39:04 +02:00
|
|
|
}
|
|
|
|
|
};
|
2022-10-02 23:06:56 +02:00
|
|
|
}
|