blob: a8e3107606c9299ee80e0512b29f36e2be58b3ff [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 Sakhartchouk38da5082011-11-15 14:21:58 -080080 public void surfaceDestroyed(SurfaceHolder holder) {
81 synchronized (this) {
82 // Surface will be destroyed when we return
83 if (mRS != null) {
84 mRS.setSurface(null, 0, 0);
85 }
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 */
Alex Sakhartchouk38da5082011-11-15 14:21:58 -080093 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
94 synchronized (this) {
95 if (mRS != null) {
96 mRS.setSurface(holder, w, h);
97 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -080098 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070099 }
100
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800101 /**
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700102 * Inform the view that the activity is paused. The owner of this view must
103 * call this method when the activity is paused. Calling this method will
104 * pause the rendering thread.
105 * Must not be called before a renderer has been set.
106 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800107 public void pause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700108 if(mRS != null) {
109 mRS.pause();
110 }
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 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800120 public void resume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700121 if(mRS != null) {
122 mRS.resume();
123 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700124 }
125
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800126 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800127 RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800128 setRenderScriptGL(rs);
129 return rs;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700130 }
131
Alex Sakhartchouk38da5082011-11-15 14:21:58 -0800132 public void destroyRenderScriptGL() {
133 synchronized (this) {
134 mRS.destroy();
135 mRS = null;
136 }
Joe Onorato5fda65f2009-09-25 09:12:16 -0700137 }
Jason Sams2222aa92010-10-10 17:58:25 -0700138
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800139 public void setRenderScriptGL(RenderScriptGL rs) {
Romain Guya8551b12010-03-10 22:11:50 -0800140 mRS = rs;
141 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800142
143 public RenderScriptGL getRenderScriptGL() {
144 return mRS;
145 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700146}