RSTextureView.java revision 684b23513c9137ddc04b06ac05a4b15be06ad2f4
1/* 2 * Copyright (C) 2011 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 android.renderscript; 18 19import java.io.Writer; 20import java.util.ArrayList; 21import java.util.concurrent.Semaphore; 22 23import android.content.Context; 24import android.graphics.SurfaceTexture; 25import android.os.Handler; 26import android.os.Message; 27import android.util.AttributeSet; 28import android.util.Log; 29import android.view.TextureView; 30 31/** 32 * The Texture View for a graphics renderscript (RenderScriptGL) 33 * to draw on. 34 * 35 */ 36public class RSTextureView extends TextureView implements TextureView.SurfaceTextureListener { 37 private RenderScriptGL mRS; 38 private SurfaceTexture mSurfaceTexture; 39 40 /** 41 * Standard View constructor. In order to render something, you 42 * must call {@link android.opengl.GLSurfaceView#setRenderer} to 43 * register a renderer. 44 */ 45 public RSTextureView(Context context) { 46 super(context); 47 init(); 48 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); 49 } 50 51 /** 52 * Standard View constructor. In order to render something, you 53 * must call {@link android.opengl.GLSurfaceView#setRenderer} to 54 * register a renderer. 55 */ 56 public RSTextureView(Context context, AttributeSet attrs) { 57 super(context, attrs); 58 init(); 59 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); 60 } 61 62 private void init() { 63 setSurfaceTextureListener(this); 64 //android.util.Log.e("rs", "getSurfaceTextureListerner " + getSurfaceTextureListener()); 65 } 66 67 @Override 68 public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 69 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureAvailable"); 70 mSurfaceTexture = surface; 71 72 if (mRS != null) { 73 mRS.setSurfaceTexture(mSurfaceTexture, width, height); 74 } 75 } 76 77 @Override 78 public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 79 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureSizeChanged"); 80 mSurfaceTexture = surface; 81 82 if (mRS != null) { 83 mRS.setSurfaceTexture(mSurfaceTexture, width, height); 84 } 85 } 86 87 @Override 88 public void onSurfaceTextureDestroyed(SurfaceTexture surface) { 89 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureDestroyed"); 90 mSurfaceTexture = surface; 91 92 if (mRS != null) { 93 mRS.setSurfaceTexture(null, 0, 0); 94 } 95 } 96 97 @Override 98 public void onSurfaceTextureUpdated(SurfaceTexture surface) { 99 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureUpdated"); 100 mSurfaceTexture = surface; 101 } 102 103 /** 104 * Inform the view that the activity is paused. The owner of this view must 105 * call this method when the activity is paused. Calling this method will 106 * pause the rendering thread. 107 * Must not be called before a renderer has been set. 108 */ 109 public void pause() { 110 if(mRS != null) { 111 mRS.pause(); 112 } 113 } 114 115 /** 116 * Inform the view that the activity is resumed. The owner of this view must 117 * call this method when the activity is resumed. Calling this method will 118 * recreate the OpenGL display and resume the rendering 119 * thread. 120 * Must not be called before a renderer has been set. 121 */ 122 public void resume() { 123 if(mRS != null) { 124 mRS.resume(); 125 } 126 } 127 128 /** 129 * Create a new RenderScriptGL object and attach it to the 130 * TextureView if present. 131 * 132 * 133 * @param sc The RS surface config to create. 134 * 135 * @return RenderScriptGL The new object created. 136 */ 137 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { 138 RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc); 139 setRenderScriptGL(rs); 140 if (mSurfaceTexture != null) { 141 mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight()); 142 } 143 return rs; 144 } 145 146 /** 147 * Destroy the RenderScriptGL object associated with this 148 * TextureView. 149 */ 150 public void destroyRenderScriptGL() { 151 mRS.destroy(); 152 mRS = null; 153 } 154 155 /** 156 * Set a new RenderScriptGL object. This also will attach the 157 * new object to the TextureView if present. 158 * 159 * @param rs The new RS object. 160 */ 161 public void setRenderScriptGL(RenderScriptGL rs) { 162 mRS = rs; 163 if (mSurfaceTexture != null) { 164 mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight()); 165 } 166 } 167 168 /** 169 * Returns the previously set RenderScriptGL object. 170 * 171 * @return RenderScriptGL 172 */ 173 public RenderScriptGL getRenderScriptGL() { 174 return mRS; 175 } 176} 177 178