blob: 0211a4a262896a5ad1fbf5288280bd9a2fd62c10 [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) {
Jack Palevich60aa3ea2009-05-26 13:45:08 -070072 mSurfaceHolder = holder;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070073 }
74
75 /**
76 * This method is part of the SurfaceHolder.Callback interface, and is
77 * not normally called or subclassed by clients of RSSurfaceView.
78 */
79 public void surfaceDestroyed(SurfaceHolder holder) {
80 // Surface will be destroyed when we return
Jason Samsefd9b6fb2009-11-03 13:58:36 -080081 if (mRS != null) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080082 mRS.setSurface(null, 0, 0);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080083 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070084 }
85
86 /**
87 * This method is part of the SurfaceHolder.Callback interface, and is
88 * not normally called or subclassed by clients of RSSurfaceView.
89 */
90 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -080091 if (mRS != null) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080092 mRS.setSurface(holder, w, h);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080093 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070094 }
95
Jason Samsbf6ef8d72010-12-06 15:59:59 -080096 /**
Jack Palevich60aa3ea2009-05-26 13:45:08 -070097 * Inform the view that the activity is paused. The owner of this view must
98 * call this method when the activity is paused. Calling this method will
99 * pause the rendering thread.
100 * Must not be called before a renderer has been set.
101 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800102 public void pause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700103 if(mRS != null) {
104 mRS.pause();
105 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700106 }
107
108 /**
109 * Inform the view that the activity is resumed. The owner of this view must
110 * call this method when the activity is resumed. Calling this method will
111 * recreate the OpenGL display and resume the rendering
112 * thread.
113 * Must not be called before a renderer has been set.
114 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800115 public void resume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700116 if(mRS != null) {
117 mRS.resume();
118 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700119 }
120
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800121 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
122 RenderScriptGL rs = new RenderScriptGL(sc);
123 setRenderScriptGL(rs);
124 return rs;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700125 }
126
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800127 public void destroyRenderScriptGL() {
Joe Onorato5fda65f12009-09-25 09:12:16 -0700128 mRS.destroy();
129 mRS = null;
130 }
Jason Sams2222aa92010-10-10 17:58:25 -0700131
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800132 public void setRenderScriptGL(RenderScriptGL rs) {
Romain Guya8551b12010-03-10 22:11:50 -0800133 mRS = rs;
134 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800135
136 public RenderScriptGL getRenderScriptGL() {
137 return mRS;
138 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700139}
140