• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.ui;
18 
19 import android.annotation.TargetApi;
20 import android.graphics.RectF;
21 import android.graphics.SurfaceTexture;
22 
23 import com.android.gallery3d.common.ApiHelper;
24 
25 @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
26 public abstract class SurfaceTextureScreenNail implements ScreenNail,
27         SurfaceTexture.OnFrameAvailableListener {
28     @SuppressWarnings("unused")
29     private static final String TAG = "SurfaceTextureScreenNail";
30     // This constant is not available in API level before 15, but it was just an
31     // oversight.
32     private static final int GL_TEXTURE_EXTERNAL_OES = 0x8D65;
33 
34     protected ExtTexture mExtTexture;
35     private SurfaceTexture mSurfaceTexture;
36     private int mWidth, mHeight;
37     private float[] mTransform = new float[16];
38     private boolean mHasTexture = false;
39 
SurfaceTextureScreenNail()40     public SurfaceTextureScreenNail() {
41     }
42 
acquireSurfaceTexture()43     public void acquireSurfaceTexture() {
44         mExtTexture = new ExtTexture(GL_TEXTURE_EXTERNAL_OES);
45         mExtTexture.setSize(mWidth, mHeight);
46         mSurfaceTexture = new SurfaceTexture(mExtTexture.getId());
47         setDefaultBufferSize(mSurfaceTexture, mWidth, mHeight);
48         mSurfaceTexture.setOnFrameAvailableListener(this);
49         synchronized (this) {
50             mHasTexture = true;
51         }
52     }
53 
54     @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
setDefaultBufferSize(SurfaceTexture st, int width, int height)55     private static void setDefaultBufferSize(SurfaceTexture st, int width, int height) {
56         if (ApiHelper.HAS_SET_DEFALT_BUFFER_SIZE) {
57             st.setDefaultBufferSize(width, height);
58         }
59     }
60 
61     @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
releaseSurfaceTexture(SurfaceTexture st)62     private static void releaseSurfaceTexture(SurfaceTexture st) {
63         st.setOnFrameAvailableListener(null);
64         if (ApiHelper.HAS_RELEASE_SURFACE_TEXTURE) {
65             st.release();
66         }
67     }
68 
getSurfaceTexture()69     public SurfaceTexture getSurfaceTexture() {
70         return mSurfaceTexture;
71     }
72 
releaseSurfaceTexture()73     public void releaseSurfaceTexture() {
74         synchronized (this) {
75             mHasTexture = false;
76         }
77         mExtTexture.recycle();
78         mExtTexture = null;
79         releaseSurfaceTexture(mSurfaceTexture);
80         mSurfaceTexture = null;
81     }
82 
setSize(int width, int height)83     public void setSize(int width, int height) {
84         mWidth = width;
85         mHeight = height;
86     }
87 
resizeTexture()88     public void resizeTexture() {
89         if (mExtTexture != null) {
90             mExtTexture.setSize(mWidth, mHeight);
91             setDefaultBufferSize(mSurfaceTexture, mWidth, mHeight);
92         }
93     }
94 
95     @Override
getWidth()96     public int getWidth() {
97         return mWidth;
98     }
99 
100     @Override
getHeight()101     public int getHeight() {
102         return mHeight;
103     }
104 
105     @Override
draw(GLCanvas canvas, int x, int y, int width, int height)106     public void draw(GLCanvas canvas, int x, int y, int width, int height) {
107         synchronized (this) {
108             if (!mHasTexture) return;
109             mSurfaceTexture.updateTexImage();
110             mSurfaceTexture.getTransformMatrix(mTransform);
111 
112             // Flip vertically.
113             canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
114             int cx = x + width / 2;
115             int cy = y + height / 2;
116             canvas.translate(cx, cy);
117             canvas.scale(1, -1, 1);
118             canvas.translate(-cx, -cy);
119             updateTransformMatrix(mTransform);
120             canvas.drawTexture(mExtTexture, mTransform, x, y, width, height);
121             canvas.restore();
122         }
123     }
124 
125     @Override
draw(GLCanvas canvas, RectF source, RectF dest)126     public void draw(GLCanvas canvas, RectF source, RectF dest) {
127         throw new UnsupportedOperationException();
128     }
129 
updateTransformMatrix(float[] matrix)130     protected void updateTransformMatrix(float[] matrix) {}
131 
132     @Override
noDraw()133     abstract public void noDraw();
134 
135     @Override
recycle()136     abstract public void recycle();
137 
138     @Override
onFrameAvailable(SurfaceTexture surfaceTexture)139     abstract public void onFrameAvailable(SurfaceTexture surfaceTexture);
140 }
141