SurfaceTextureScreenNail.java revision ffc2a4a77f7164dd435d07c11a3df290c4b64079
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
17package com.android.gallery3d.ui;
18
19import android.graphics.RectF;
20import android.graphics.SurfaceTexture;
21import android.opengl.GLES11Ext;
22
23public 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
32    public SurfaceTextureScreenNail() {
33    }
34
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.setOnFrameAvailableListener(this);
40        synchronized (this) {
41            mHasTexture = true;
42        }
43    }
44
45    public SurfaceTexture getSurfaceTexture() {
46        return mSurfaceTexture;
47    }
48
49    public void releaseSurfaceTexture() {
50        synchronized (this) {
51            mHasTexture = false;
52        }
53        mExtTexture.recycle();
54        mExtTexture = null;
55        mSurfaceTexture.release();
56        mSurfaceTexture = null;
57    }
58
59    public void setSize(int width, int height) {
60        mWidth = width;
61        mHeight = height;
62    }
63
64    @Override
65    public int getWidth() {
66        return mWidth;
67    }
68
69    @Override
70    public int getHeight() {
71        return mHeight;
72    }
73
74    @Override
75    public void draw(GLCanvas canvas, int x, int y, int width, int height) {
76        synchronized (this) {
77            if (!mHasTexture) return;
78            mSurfaceTexture.updateTexImage();
79            mSurfaceTexture.getTransformMatrix(mTransform);
80
81            // Flip vertically.
82            canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
83            int cx = x + width / 2;
84            int cy = y + height / 2;
85            canvas.translate(cx, cy);
86            canvas.scale(1, -1, 1);
87            canvas.translate(-cx, -cy);
88            canvas.drawTexture(mExtTexture, mTransform, x, y, width, height);
89            canvas.restore();
90        }
91    }
92
93    @Override
94    public void draw(GLCanvas canvas, RectF source, RectF dest) {
95        throw new UnsupportedOperationException();
96    }
97
98    @Override
99    abstract public void noDraw();
100
101    @Override
102    abstract public void recycle();
103
104    @Override
105    abstract public void onFrameAvailable(SurfaceTexture surfaceTexture);
106}
107