blob: 20eb93fc1d56675d12685cf8f146331a68f047f2 [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/**
Alex Sakhartchouk93c47f12011-11-11 11:49:45 -080033 * The Surface View for a graphics renderscript (RenderScriptGL) to draw on.
Stephen Hinesb11e3d22011-01-11 19:30:58 -080034 */
Jack Palevich60aa3ea2009-05-26 13:45:08 -070035public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
36 private SurfaceHolder mSurfaceHolder;
Jason Sams704ff642010-02-09 16:05:07 -080037 private RenderScriptGL mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070038
39 /**
40 * Standard View constructor. In order to render something, you
Stephen Hinesb11e3d22011-01-11 19:30:58 -080041 * must call {@link android.opengl.GLSurfaceView#setRenderer} to
42 * register a renderer.
Jack Palevich60aa3ea2009-05-26 13:45:08 -070043 */
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
Stephen Hinesb11e3d22011-01-11 19:30:58 -080052 * must call {@link android.opengl.GLSurfaceView#setRenderer} to
53 * register a renderer.
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054 */
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) {
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 */
Alex Sakhartchouk93c47f12011-11-11 11:49:45 -080080 public synchronized void surfaceDestroyed(SurfaceHolder holder) {
Jack Palevich60aa3ea2009-05-26 13:45:08 -070081 // Surface will be destroyed when we return
Jason Samsefd9b6fb2009-11-03 13:58:36 -080082 if (mRS != null) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080083 mRS.setSurface(null, 0, 0);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080084 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070085 }
86
87 /**
88 * This method is part of the SurfaceHolder.Callback interface, and is
89 * not normally called or subclassed by clients of RSSurfaceView.
90 */
Alex Sakhartchouk93c47f12011-11-11 11:49:45 -080091 public synchronized void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Jason Samsefd9b6fb2009-11-03 13:58:36 -080092 if (mRS != null) {
Jason Samsbf6ef8d72010-12-06 15:59:59 -080093 mRS.setSurface(holder, w, h);
Jason Samsefd9b6fb2009-11-03 13:58:36 -080094 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070095 }
96
Jason Samsbf6ef8d72010-12-06 15:59:59 -080097 /**
Jack Palevich60aa3ea2009-05-26 13:45:08 -070098 * Inform the view that the activity is paused. The owner of this view must
99 * call this method when the activity is paused. Calling this method will
100 * pause the rendering thread.
101 * Must not be called before a renderer has been set.
102 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800103 public void pause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700104 if(mRS != null) {
105 mRS.pause();
106 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700107 }
108
109 /**
110 * Inform the view that the activity is resumed. The owner of this view must
111 * call this method when the activity is resumed. Calling this method will
112 * recreate the OpenGL display and resume the rendering
113 * thread.
114 * Must not be called before a renderer has been set.
115 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800116 public void resume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700117 if(mRS != null) {
118 mRS.resume();
119 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700120 }
121
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800122 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800123 RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800124 setRenderScriptGL(rs);
125 return rs;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700126 }
127
Alex Sakhartchouk93c47f12011-11-11 11:49:45 -0800128 public synchronized void destroyRenderScriptGL() {
Joe Onorato5fda65f12009-09-25 09:12:16 -0700129 mRS.destroy();
130 mRS = null;
131 }
Jason Sams2222aa92010-10-10 17:58:25 -0700132
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800133 public void setRenderScriptGL(RenderScriptGL rs) {
Romain Guya8551b12010-03-10 22:11:50 -0800134 mRS = rs;
135 }
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800136
137 public RenderScriptGL getRenderScriptGL() {
138 return mRS;
139 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700140}