CameraScreenNail.java revision 78b7bc5fe8e47bc422daf3b4e83cc7b241b210fd
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.camera; 18 19import android.graphics.Matrix; 20import android.graphics.RectF; 21import android.graphics.SurfaceTexture; 22 23import com.android.gallery3d.app.GalleryActivity; 24import com.android.gallery3d.ui.GLCanvas; 25import com.android.gallery3d.ui.Raw2DTexture; 26import com.android.gallery3d.ui.ScreenNail; 27import com.android.gallery3d.ui.SurfaceTextureScreenNail; 28 29/* 30 * This is a ScreenNail which can displays camera preview. 31 */ 32public class CameraScreenNail extends SurfaceTextureScreenNail { 33 private static final String TAG = "CameraScreenNail"; 34 private static final int ANIM_NONE = 0; 35 private static final int ANIM_TO_START = 1; 36 private static final int ANIM_RUNNING = 2; 37 38 private boolean mVisible; 39 private RenderListener mRenderListener; 40 private final float[] mTextureTransformMatrix = new float[16]; 41 42 // Animation. 43 private CaptureAnimManager mAnimManager = new CaptureAnimManager(); 44 private int mAnimState = ANIM_NONE; 45 private Raw2DTexture mAnimTexture; 46 47 public interface RenderListener { 48 void requestRender(); 49 } 50 51 public CameraScreenNail(RenderListener listener) { 52 mRenderListener = listener; 53 } 54 55 @Override 56 public void acquireSurfaceTexture() { 57 super.acquireSurfaceTexture(); 58 mAnimTexture = new Raw2DTexture(getWidth(), getHeight()); 59 } 60 61 public void animate(int animOrientation) { 62 switch (mAnimState) { 63 case ANIM_TO_START: 64 break; 65 case ANIM_NONE: 66 mAnimManager.setOrientation(animOrientation); 67 // No break here. Continue to set the state and request for rendering. 68 case ANIM_RUNNING: 69 // Don't change the animation orientation during animation. 70 mRenderListener.requestRender(); 71 mAnimState = ANIM_TO_START; 72 break; 73 } 74 } 75 76 public void directDraw(GLCanvas canvas, int x, int y, int width, int height) { 77 super.draw(canvas, x, y, width, height); 78 } 79 80 @Override 81 public void draw(GLCanvas canvas, int x, int y, int width, int height) { 82 if (getSurfaceTexture() == null) return; 83 if (!mVisible) setVisibility(true); 84 85 switch (mAnimState) { 86 case ANIM_TO_START: 87 getSurfaceTexture().getTransformMatrix(mTextureTransformMatrix); 88 Raw2DTexture.copy(canvas, mExtTexture, mAnimTexture); 89 mAnimManager.startAnimation(x, y, width, height, 90 mTextureTransformMatrix); 91 mAnimState = ANIM_RUNNING; 92 // Continue to draw the animation. No break is needed here. 93 case ANIM_RUNNING: 94 if (mAnimManager.drawAnimation(canvas, this, mAnimTexture)) { 95 mRenderListener.requestRender(); 96 break; 97 } 98 // No break here because we continue to the normal draw 99 // procedure if the animation is not drawn. 100 mAnimState = ANIM_NONE; 101 case ANIM_NONE: 102 super.draw(canvas, x, y, width, height); 103 break; 104 } 105 } 106 107 @Override 108 public synchronized void noDraw() { 109 setVisibility(false); 110 } 111 112 @Override 113 public synchronized void recycle() { 114 setVisibility(false); 115 } 116 117 @Override 118 public synchronized void onFrameAvailable(SurfaceTexture surfaceTexture) { 119 if (mVisible) { 120 // We need to ask for re-render if the SurfaceTexture receives a new 121 // frame. 122 mRenderListener.requestRender(); 123 } 124 } 125 126 private void setVisibility(boolean visible) { 127 if (mVisible != visible) { 128 mVisible = visible; 129 } 130 } 131} 132