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.graphics.RectF; 20 import android.graphics.SurfaceTexture; 21 import android.opengl.GLES11Ext; 22 23 public abstract class SurfaceTextureScreenNail implements ScreenNail, 24 SurfaceTexture.OnFrameAvailableListener { 25 private static final String TAG = "SurfaceTextureScreenNail"; 26 protected ExtTexture mExtTexture; 27 private SurfaceTexture mSurfaceTexture; 28 private int mWidth, mHeight; 29 private float[] mTransform = new float[16]; 30 private boolean mHasTexture = false; 31 SurfaceTextureScreenNail()32 public SurfaceTextureScreenNail() { 33 } 34 acquireSurfaceTexture()35 public void acquireSurfaceTexture() { 36 mExtTexture = new ExtTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES); 37 mExtTexture.setSize(mWidth, mHeight); 38 mSurfaceTexture = new SurfaceTexture(mExtTexture.getId()); 39 mSurfaceTexture.setDefaultBufferSize(mWidth, mHeight); 40 mSurfaceTexture.setOnFrameAvailableListener(this); 41 synchronized (this) { 42 mHasTexture = true; 43 } 44 } 45 getSurfaceTexture()46 public SurfaceTexture getSurfaceTexture() { 47 return mSurfaceTexture; 48 } 49 releaseSurfaceTexture()50 public void releaseSurfaceTexture() { 51 synchronized (this) { 52 mHasTexture = false; 53 } 54 mExtTexture.recycle(); 55 mExtTexture = null; 56 mSurfaceTexture.release(); 57 mSurfaceTexture = null; 58 } 59 setSize(int width, int height)60 public void setSize(int width, int height) { 61 mWidth = width; 62 mHeight = height; 63 } 64 65 @Override getWidth()66 public int getWidth() { 67 return mWidth; 68 } 69 70 @Override getHeight()71 public int getHeight() { 72 return mHeight; 73 } 74 75 @Override draw(GLCanvas canvas, int x, int y, int width, int height)76 public void draw(GLCanvas canvas, int x, int y, int width, int height) { 77 synchronized (this) { 78 if (!mHasTexture) return; 79 mSurfaceTexture.updateTexImage(); 80 mSurfaceTexture.getTransformMatrix(mTransform); 81 82 // Flip vertically. 83 canvas.save(GLCanvas.SAVE_FLAG_MATRIX); 84 int cx = x + width / 2; 85 int cy = y + height / 2; 86 canvas.translate(cx, cy); 87 canvas.scale(1, -1, 1); 88 canvas.translate(-cx, -cy); 89 canvas.drawTexture(mExtTexture, mTransform, x, y, width, height); 90 canvas.restore(); 91 } 92 } 93 94 @Override draw(GLCanvas canvas, RectF source, RectF dest)95 public void draw(GLCanvas canvas, RectF source, RectF dest) { 96 throw new UnsupportedOperationException(); 97 } 98 99 @Override noDraw()100 abstract public void noDraw(); 101 102 @Override recycle()103 abstract public void recycle(); 104 105 @Override onFrameAvailable(SurfaceTexture surfaceTexture)106 abstract public void onFrameAvailable(SurfaceTexture surfaceTexture); 107 } 108