blob: 2540d0173a2be750f384ca3c780cf3c89981a500 [file] [log] [blame]
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001/*
2 * Copyright (C) 2008 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
Jason Sams94d8e90a2009-06-10 16:09:05 -070017package android.renderscript;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070018
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22
23import android.content.Context;
24import android.os.Handler;
25import android.os.Message;
26import android.util.AttributeSet;
27import android.util.Log;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070028import android.view.Surface;
29import android.view.SurfaceHolder;
30import android.view.SurfaceView;
31
Jason Samse29d4712009-07-23 15:19:03 -070032/**
33 * @hide
34 *
35 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070036public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
37 private SurfaceHolder mSurfaceHolder;
Jason Sams704ff642010-02-09 16:05:07 -080038 private RenderScriptGL mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070039
40 /**
41 * Standard View constructor. In order to render something, you
42 * must call {@link #setRenderer} to register a renderer.
43 */
44 public RSSurfaceView(Context context) {
45 super(context);
46 init();
Jason Sams66b27712009-09-25 15:25:00 -070047 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070048 }
49
50 /**
51 * Standard View constructor. In order to render something, you
52 * must call {@link #setRenderer} to register a renderer.
53 */
54 public RSSurfaceView(Context context, AttributeSet attrs) {
55 super(context, attrs);
56 init();
Jason Sams66b27712009-09-25 15:25:00 -070057 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070058 }
59
60 private void init() {
61 // Install a SurfaceHolder.Callback so we get notified when the
62 // underlying surface is created and destroyed
63 SurfaceHolder holder = getHolder();
64 holder.addCallback(this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065 }
66
67 /**
68 * This method is part of the SurfaceHolder.Callback interface, and is
69 * not normally called or subclassed by clients of RSSurfaceView.
70 */
71 public void surfaceCreated(SurfaceHolder holder) {
Jason Samsf29ca502009-06-23 12:22:47 -070072 Log.v(RenderScript.LOG_TAG, "surfaceCreated");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070073 mSurfaceHolder = holder;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070074 }
75
76 /**
77 * This method is part of the SurfaceHolder.Callback interface, and is
78 * not normally called or subclassed by clients of RSSurfaceView.
79 */
80 public void surfaceDestroyed(SurfaceHolder holder) {
81 // Surface will be destroyed when we return
Jason Sams3bc47d42009-11-12 15:10:25 -080082 Log.v(RenderScript.LOG_TAG, "surfaceDestroyed");
Jason Samsefd9b6fb2009-11-03 13:58:36 -080083 if (mRS != null) {
Jason Sams3bc47d42009-11-12 15:10:25 -080084 mRS.contextSetSurface(0, 0, null);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080085 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070086 }
87
88 /**
89 * This method is part of the SurfaceHolder.Callback interface, and is
90 * not normally called or subclassed by clients of RSSurfaceView.
91 */
92 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Jason Sams3bc47d42009-11-12 15:10:25 -080093 Log.v(RenderScript.LOG_TAG, "surfaceChanged");
Jason Samsefd9b6fb2009-11-03 13:58:36 -080094 if (mRS != null) {
Jason Sams3bc47d42009-11-12 15:10:25 -080095 mRS.contextSetSurface(w, h, holder.getSurface());
Jason Samsefd9b6fb2009-11-03 13:58:36 -080096 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070097 }
98
99 /**
100 * Inform the view that the activity is paused. The owner of this view must
101 * call this method when the activity is paused. Calling this method will
102 * pause the rendering thread.
103 * Must not be called before a renderer has been set.
104 */
105 public void onPause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700106 if(mRS != null) {
107 mRS.pause();
108 }
Jason Sams66b27712009-09-25 15:25:00 -0700109 //Log.v(RenderScript.LOG_TAG, "onPause");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700110 }
111
112 /**
113 * Inform the view that the activity is resumed. The owner of this view must
114 * call this method when the activity is resumed. Calling this method will
115 * recreate the OpenGL display and resume the rendering
116 * thread.
117 * Must not be called before a renderer has been set.
118 */
119 public void onResume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700120 if(mRS != null) {
121 mRS.resume();
122 }
Jason Sams66b27712009-09-25 15:25:00 -0700123 //Log.v(RenderScript.LOG_TAG, "onResume");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700124 }
125
126 /**
127 * Queue a runnable to be run on the GL rendering thread. This can be used
128 * to communicate with the Renderer on the rendering thread.
129 * Must not be called before a renderer has been set.
130 * @param r the runnable to be run on the GL rendering thread.
131 */
132 public void queueEvent(Runnable r) {
Jason Sams66b27712009-09-25 15:25:00 -0700133 //Log.v(RenderScript.LOG_TAG, "queueEvent");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700134 }
135
136 /**
137 * This method is used as part of the View class and is not normally
138 * called or subclassed by clients of RSSurfaceView.
139 * Must not be called before a renderer has been set.
140 */
141 @Override
142 protected void onDetachedFromWindow() {
143 super.onDetachedFromWindow();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700144 }
145
146 // ----------------------------------------------------------------------
147
Jason Sams2222aa92010-10-10 17:58:25 -0700148 public RenderScriptGL createRenderScript(RenderScriptGL.SurfaceConfig sc) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800149 Log.v(RenderScript.LOG_TAG, "createRenderScript");
Jason Sams2222aa92010-10-10 17:58:25 -0700150 mRS = new RenderScriptGL(sc);
Jason Sams65e7aa52009-09-24 17:38:20 -0700151 return mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700152 }
153
Joe Onorato5fda65f12009-09-25 09:12:16 -0700154 public void destroyRenderScript() {
Jason Sams3bc47d42009-11-12 15:10:25 -0800155 Log.v(RenderScript.LOG_TAG, "destroyRenderScript");
Joe Onorato5fda65f12009-09-25 09:12:16 -0700156 mRS.destroy();
157 mRS = null;
158 }
Jason Sams2222aa92010-10-10 17:58:25 -0700159
Romain Guya8551b12010-03-10 22:11:50 -0800160 public void createRenderScript(RenderScriptGL rs) {
161 mRS = rs;
162 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700163}
164