blob: ad1bb5429a993f7c52fe16d0f6e4909da81eb32d [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;
28import android.util.Log;
29import android.view.Surface;
30import android.view.SurfaceHolder;
31import android.view.SurfaceView;
32
Jason Samse29d4712009-07-23 15:19:03 -070033/**
34 * @hide
35 *
36 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070037public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
38 private SurfaceHolder mSurfaceHolder;
Jason Sams65e7aa52009-09-24 17:38:20 -070039 private RenderScript mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070040
41 /**
42 * Standard View constructor. In order to render something, you
43 * must call {@link #setRenderer} to register a renderer.
44 */
45 public RSSurfaceView(Context context) {
46 super(context);
47 init();
Jason Sams66b27712009-09-25 15:25:00 -070048 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070049 }
50
51 /**
52 * Standard View constructor. In order to render something, you
53 * must call {@link #setRenderer} to register a renderer.
54 */
55 public RSSurfaceView(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 init();
Jason Sams66b27712009-09-25 15:25:00 -070058 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070059 }
60
61 private void init() {
62 // Install a SurfaceHolder.Callback so we get notified when the
63 // underlying surface is created and destroyed
64 SurfaceHolder holder = getHolder();
65 holder.addCallback(this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070066 }
67
68 /**
69 * This method is part of the SurfaceHolder.Callback interface, and is
70 * not normally called or subclassed by clients of RSSurfaceView.
71 */
72 public void surfaceCreated(SurfaceHolder holder) {
Jason Samsf29ca502009-06-23 12:22:47 -070073 Log.v(RenderScript.LOG_TAG, "surfaceCreated");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070074 mSurfaceHolder = holder;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070075 }
76
77 /**
78 * This method is part of the SurfaceHolder.Callback interface, and is
79 * not normally called or subclassed by clients of RSSurfaceView.
80 */
81 public void surfaceDestroyed(SurfaceHolder holder) {
82 // Surface will be destroyed when we return
Jason Sams3bc47d42009-11-12 15:10:25 -080083 Log.v(RenderScript.LOG_TAG, "surfaceDestroyed");
Jason Samsefd9b6fb2009-11-03 13:58:36 -080084 if (mRS != null) {
Jason Sams3bc47d42009-11-12 15:10:25 -080085 mRS.contextSetSurface(0, 0, null);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080086 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070087 }
88
89 /**
90 * This method is part of the SurfaceHolder.Callback interface, and is
91 * not normally called or subclassed by clients of RSSurfaceView.
92 */
93 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Jason Sams3bc47d42009-11-12 15:10:25 -080094 Log.v(RenderScript.LOG_TAG, "surfaceChanged");
Jason Samsefd9b6fb2009-11-03 13:58:36 -080095 if (mRS != null) {
Jason Sams3bc47d42009-11-12 15:10:25 -080096 mRS.contextSetSurface(w, h, holder.getSurface());
Jason Samsefd9b6fb2009-11-03 13:58:36 -080097 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070098 }
99
100 /**
101 * Inform the view that the activity is paused. The owner of this view must
102 * call this method when the activity is paused. Calling this method will
103 * pause the rendering thread.
104 * Must not be called before a renderer has been set.
105 */
106 public void onPause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700107 if(mRS != null) {
108 mRS.pause();
109 }
Jason Sams66b27712009-09-25 15:25:00 -0700110 //Log.v(RenderScript.LOG_TAG, "onPause");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700111 }
112
113 /**
114 * Inform the view that the activity is resumed. The owner of this view must
115 * call this method when the activity is resumed. Calling this method will
116 * recreate the OpenGL display and resume the rendering
117 * thread.
118 * Must not be called before a renderer has been set.
119 */
120 public void onResume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700121 if(mRS != null) {
122 mRS.resume();
123 }
Jason Sams66b27712009-09-25 15:25:00 -0700124 //Log.v(RenderScript.LOG_TAG, "onResume");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700125 }
126
127 /**
128 * Queue a runnable to be run on the GL rendering thread. This can be used
129 * to communicate with the Renderer on the rendering thread.
130 * Must not be called before a renderer has been set.
131 * @param r the runnable to be run on the GL rendering thread.
132 */
133 public void queueEvent(Runnable r) {
Jason Sams66b27712009-09-25 15:25:00 -0700134 //Log.v(RenderScript.LOG_TAG, "queueEvent");
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700135 }
136
137 /**
138 * This method is used as part of the View class and is not normally
139 * called or subclassed by clients of RSSurfaceView.
140 * Must not be called before a renderer has been set.
141 */
142 @Override
143 protected void onDetachedFromWindow() {
144 super.onDetachedFromWindow();
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700145 }
146
147 // ----------------------------------------------------------------------
148
Jason Samsebfb4362009-09-23 13:57:02 -0700149 public RenderScript createRenderScript(boolean useDepth, boolean forceSW) {
Jason Sams3bc47d42009-11-12 15:10:25 -0800150 Log.v(RenderScript.LOG_TAG, "createRenderScript");
151 mRS = new RenderScript(useDepth, forceSW);
Jason Sams65e7aa52009-09-24 17:38:20 -0700152 return mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700153 }
154
Jason Samsebfb4362009-09-23 13:57:02 -0700155 public RenderScript createRenderScript(boolean useDepth) {
156 return createRenderScript(useDepth, false);
157 }
158
Joe Onorato5fda65f12009-09-25 09:12:16 -0700159 public void destroyRenderScript() {
Jason Sams3bc47d42009-11-12 15:10:25 -0800160 Log.v(RenderScript.LOG_TAG, "destroyRenderScript");
Joe Onorato5fda65f12009-09-25 09:12:16 -0700161 mRS.destroy();
162 mRS = null;
163 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700164}
165