1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
package com.android.camera.support.ui;
import android.annotation.TargetApi;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import com.android.camera.support.common.ApiHelper;
import com.android.camera.support.glrenderer.ExtTexture;
import com.android.camera.support.glrenderer.GLCanvas;
@TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
public abstract class SurfaceTextureScreenNail implements ScreenNail,
SurfaceTexture.OnFrameAvailableListener {
@SuppressWarnings("unused")
private static final String TAG = "SurfaceTextureScreenNail";
// This constant is not available in API level before 15, but it was just an
// oversight.
private static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
protected ExtTexture mExtTexture;
private SurfaceTexture mSurfaceTexture;
private int mWidth, mHeight;
private float[] mTransform = new float[16];
private boolean mHasTexture = false;
public SurfaceTextureScreenNail() {
}
public void acquireSurfaceTexture(GLCanvas canvas) {
mExtTexture = new ExtTexture(canvas, GL_TEXTURE_EXTERNAL_OES);
mExtTexture.setSize(mWidth, mHeight);
mSurfaceTexture = new SurfaceTexture(mExtTexture.getId());
setDefaultBufferSize(mSurfaceTexture, mWidth, mHeight);
mSurfaceTexture.setOnFrameAvailableListener(this);
synchronized (this) {
mHasTexture = true;
}
}
@TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private static void setDefaultBufferSize(SurfaceTexture st, int width, int height) {
if (ApiHelper.HAS_SET_DEFALT_BUFFER_SIZE) {
st.setDefaultBufferSize(width, height);
}
}
@TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
private static void releaseSurfaceTexture(SurfaceTexture st) {
st.setOnFrameAvailableListener(null);
if (ApiHelper.HAS_RELEASE_SURFACE_TEXTURE) {
st.release();
}
}
public SurfaceTexture getSurfaceTexture() {
return mSurfaceTexture;
}
public void releaseSurfaceTexture() {
synchronized (this) {
mHasTexture = false;
}
mExtTexture.recycle();
mExtTexture = null;
releaseSurfaceTexture(mSurfaceTexture);
mSurfaceTexture = null;
}
public void setSize(int width, int height) {
mWidth = width;
mHeight = height;
}
public void resizeTexture() {
if (mExtTexture != null) {
mExtTexture.setSize(mWidth, mHeight);
setDefaultBufferSize(mSurfaceTexture, mWidth, mHeight);
}
}
@Override
public int getWidth() {
return mWidth;
}
@Override
public int getHeight() {
return mHeight;
}
@Override
public void draw(GLCanvas canvas, int x, int y, int width, int height) {
synchronized (this) {
if (!mHasTexture) return;
mSurfaceTexture.updateTexImage();
mSurfaceTexture.getTransformMatrix(mTransform);
// Flip vertically.
canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
int cx = x + width / 2;
int cy = y + height / 2;
canvas.translate(cx, cy);
canvas.scale(1, -1, 1);
canvas.translate(-cx, -cy);
updateTransformMatrix(mTransform);
canvas.drawTexture(mExtTexture, mTransform, x, y, width, height);
canvas.restore();
}
}
@Override
public void draw(GLCanvas canvas, RectF source, RectF dest) {
throw new UnsupportedOperationException();
}
protected void updateTransformMatrix(float[] matrix) {}
@Override
abstract public void noDraw();
@Override
abstract public void recycle();
@Override
abstract public void onFrameAvailable(SurfaceTexture surfaceTexture);
}
|