blob: 30b2f994d39c9adbf96ef362a5ca01082e6bd4dc [file] [log] [blame]
Jason Samsfaa32b32011-06-20 16:58:04 -07001/*
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070031/**
Jason Sams684b2352011-07-26 14:07:19 -070032 * The Texture View for a graphics renderscript (RenderScriptGL)
33 * to draw on.
Jason Samsfaa32b32011-06-20 16:58:04 -070034 *
Jason Samsfaa32b32011-06-20 16:58:04 -070035 */
36public class RSTextureView extends TextureView implements TextureView.SurfaceTextureListener {
37 private RenderScriptGL mRS;
38 private SurfaceTexture mSurfaceTexture;
39
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070040 /**
Jason Samsfaa32b32011-06-20 16:58:04 -070041 * 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 Hines9c9ad3f8c22012-05-07 15:34:29 -070051 /**
Jason Samsfaa32b32011-06-20 16:58:04 -070052 * 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 Kloba402f0552011-08-09 18:47:17 -070088 public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
Jason Samsfaa32b32011-06-20 16:58:04 -070089 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureDestroyed");
90 mSurfaceTexture = surface;
91
92 if (mRS != null) {
93 mRS.setSurfaceTexture(null, 0, 0);
94 }
Grace Kloba402f0552011-08-09 18:47:17 -070095
96 return true;
Jason Samsfaa32b32011-06-20 16:58:04 -070097 }
98
Grace Klobacf559372011-06-22 23:05:40 -070099 @Override
100 public void onSurfaceTextureUpdated(SurfaceTexture surface) {
101 //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureUpdated");
102 mSurfaceTexture = surface;
103 }
104
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700105 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700106 * 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700118 * 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700130 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700131 * 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700148 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700149 * Destroy the RenderScriptGL object associated with this
150 * TextureView.
151 */
152 public void destroyRenderScriptGL() {
153 mRS.destroy();
154 mRS = null;
155 }
156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700158 * 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 Hines9c9ad3f8c22012-05-07 15:34:29 -0700170 /**
Jason Samsfaa32b32011-06-20 16:58:04 -0700171 * Returns the previously set RenderScriptGL object.
172 *
173 * @return RenderScriptGL
174 */
175 public RenderScriptGL getRenderScriptGL() {
176 return mRS;
177 }
178}
179