blob: 6756fd0d9225249f33d4e0fc4f5cedb7e24b8b2e [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.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080034 *
35 * <div class="special reference">
36 * <h3>Developer Guides</h3>
37 * <p>For more information about creating an application that uses Renderscript, read the
38 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
39 * </div>
Stephen Hinesb11e3d22011-01-11 19:30:58 -080040 */
Jack Palevich60aa3ea2009-05-26 13:45:08 -070041public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
42 private SurfaceHolder mSurfaceHolder;
Jason Sams704ff642010-02-09 16:05:07 -080043 private RenderScriptGL mRS;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070044
45 /**
46 * Standard View constructor. In order to render something, you
Stephen Hinesb11e3d22011-01-11 19:30:58 -080047 * must call {@link android.opengl.GLSurfaceView#setRenderer} to
48 * register a renderer.
Jack Palevich60aa3ea2009-05-26 13:45:08 -070049 */
50 public RSSurfaceView(Context context) {
51 super(context);
52 init();
Jason Sams66b27712009-09-25 15:25:00 -070053 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054 }
55
56 /**
57 * Standard View constructor. In order to render something, you
Stephen Hinesb11e3d22011-01-11 19:30:58 -080058 * must call {@link android.opengl.GLSurfaceView#setRenderer} to
59 * register a renderer.
Jack Palevich60aa3ea2009-05-26 13:45:08 -070060 */
61 public RSSurfaceView(Context context, AttributeSet attrs) {
62 super(context, attrs);
63 init();
Jason Sams66b27712009-09-25 15:25:00 -070064 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065 }
66
67 private void init() {
68 // Install a SurfaceHolder.Callback so we get notified when the
69 // underlying surface is created and destroyed
70 SurfaceHolder holder = getHolder();
71 holder.addCallback(this);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070072 }
73
74 /**
75 * This method is part of the SurfaceHolder.Callback interface, and is
76 * not normally called or subclassed by clients of RSSurfaceView.
77 */
78 public void surfaceCreated(SurfaceHolder holder) {
Jack Palevich60aa3ea2009-05-26 13:45:08 -070079 mSurfaceHolder = holder;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070080 }
81
82 /**
83 * This method is part of the SurfaceHolder.Callback interface, and is
84 * not normally called or subclassed by clients of RSSurfaceView.
85 */
Alex Sakhartchouk38da5082011-11-15 14:21:58 -080086 public void surfaceDestroyed(SurfaceHolder holder) {
87 synchronized (this) {
88 // Surface will be destroyed when we return
89 if (mRS != null) {
90 mRS.setSurface(null, 0, 0);
91 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -080092 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070093 }
94
95 /**
96 * This method is part of the SurfaceHolder.Callback interface, and is
97 * not normally called or subclassed by clients of RSSurfaceView.
98 */
Alex Sakhartchouk38da5082011-11-15 14:21:58 -080099 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
100 synchronized (this) {
101 if (mRS != null) {
102 mRS.setSurface(holder, w, h);
103 }
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800104 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700105 }
106
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800107 /**
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700108 * Inform the view that the activity is paused. The owner of this view must
109 * call this method when the activity is paused. Calling this method will
110 * pause the rendering thread.
111 * Must not be called before a renderer has been set.
112 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800113 public void pause() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700114 if(mRS != null) {
115 mRS.pause();
116 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700117 }
118
119 /**
120 * Inform the view that the activity is resumed. The owner of this view must
121 * call this method when the activity is resumed. Calling this method will
122 * recreate the OpenGL display and resume the rendering
123 * thread.
124 * Must not be called before a renderer has been set.
125 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800126 public void resume() {
Jason Sams65e7aa52009-09-24 17:38:20 -0700127 if(mRS != null) {
128 mRS.resume();
129 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700130 }
131
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800132 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800133 RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800134 setRenderScriptGL(rs);
135 return rs;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700136 }
137
Alex Sakhartchouk38da5082011-11-15 14:21:58 -0800138 public void destroyRenderScriptGL() {
139 synchronized (this) {
140 mRS.destroy();
141 mRS = null;
142 }
Joe Onorato5fda65f2009-09-25 09:12:16 -0700143 }
Jason Sams2222aa92010-10-10 17:58:25 -0700144
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800145 public void setRenderScriptGL(RenderScriptGL rs) {
Romain Guya8551b12010-03-10 22:11:50 -0800146 mRS = rs;
147 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800148
149 public RenderScriptGL getRenderScriptGL() {
150 return mRS;
151 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700152}