Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 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 | |
| 17 | package android.renderscript; |
| 18 | |
| 19 | import java.io.Writer; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.concurrent.Semaphore; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.graphics.SurfaceTexture; |
| 25 | import android.os.Handler; |
| 26 | import android.os.Message; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.util.Log; |
| 29 | import android.view.TextureView; |
| 30 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 31 | /** |
Jason Sams | 684b235 | 2011-07-26 14:07:19 -0700 | [diff] [blame] | 32 | * The Texture View for a graphics renderscript (RenderScriptGL) |
| 33 | * to draw on. |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 34 | * |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 35 | */ |
| 36 | public class RSTextureView extends TextureView implements TextureView.SurfaceTextureListener { |
| 37 | private RenderScriptGL mRS; |
| 38 | private SurfaceTexture mSurfaceTexture; |
| 39 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 40 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 51 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 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 |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 88 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 89 | //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureDestroyed"); |
| 90 | mSurfaceTexture = surface; |
| 91 | |
| 92 | if (mRS != null) { |
| 93 | mRS.setSurfaceTexture(null, 0, 0); |
| 94 | } |
Grace Kloba | 402f055 | 2011-08-09 18:47:17 -0700 | [diff] [blame] | 95 | |
| 96 | return true; |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Grace Kloba | cf55937 | 2011-06-22 23:05:40 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void onSurfaceTextureUpdated(SurfaceTexture surface) { |
| 101 | //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureUpdated"); |
| 102 | mSurfaceTexture = surface; |
| 103 | } |
| 104 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 105 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 106 | * Inform the view that the activity is paused. The owner of this view must |
| 107 | * call this method when the activity is paused. Calling this method will |
| 108 | * pause the rendering thread. |
| 109 | * Must not be called before a renderer has been set. |
| 110 | */ |
| 111 | public void pause() { |
| 112 | if(mRS != null) { |
| 113 | mRS.pause(); |
| 114 | } |
| 115 | } |
| 116 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 117 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 118 | * Inform the view that the activity is resumed. The owner of this view must |
| 119 | * call this method when the activity is resumed. Calling this method will |
| 120 | * recreate the OpenGL display and resume the rendering |
| 121 | * thread. |
| 122 | * Must not be called before a renderer has been set. |
| 123 | */ |
| 124 | public void resume() { |
| 125 | if(mRS != null) { |
| 126 | mRS.resume(); |
| 127 | } |
| 128 | } |
| 129 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 130 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 131 | * Create a new RenderScriptGL object and attach it to the |
| 132 | * TextureView if present. |
| 133 | * |
| 134 | * |
| 135 | * @param sc The RS surface config to create. |
| 136 | * |
| 137 | * @return RenderScriptGL The new object created. |
| 138 | */ |
| 139 | public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { |
| 140 | RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc); |
| 141 | setRenderScriptGL(rs); |
| 142 | if (mSurfaceTexture != null) { |
| 143 | mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight()); |
| 144 | } |
| 145 | return rs; |
| 146 | } |
| 147 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 148 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 149 | * Destroy the RenderScriptGL object associated with this |
| 150 | * TextureView. |
| 151 | */ |
| 152 | public void destroyRenderScriptGL() { |
| 153 | mRS.destroy(); |
| 154 | mRS = null; |
| 155 | } |
| 156 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 157 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 158 | * Set a new RenderScriptGL object. This also will attach the |
| 159 | * new object to the TextureView if present. |
| 160 | * |
| 161 | * @param rs The new RS object. |
| 162 | */ |
| 163 | public void setRenderScriptGL(RenderScriptGL rs) { |
| 164 | mRS = rs; |
| 165 | if (mSurfaceTexture != null) { |
| 166 | mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight()); |
| 167 | } |
| 168 | } |
| 169 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame^] | 170 | /** |
Jason Sams | faa32b3 | 2011-06-20 16:58:04 -0700 | [diff] [blame] | 171 | * Returns the previously set RenderScriptGL object. |
| 172 | * |
| 173 | * @return RenderScriptGL |
| 174 | */ |
| 175 | public RenderScriptGL getRenderScriptGL() { |
| 176 | return mRS; |
| 177 | } |
| 178 | } |
| 179 | |