Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 1 | /* |
| 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 Sams | 94d8e90a | 2009-06-10 16:09:05 -0700 | [diff] [blame] | 17 | package android.renderscript; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 18 | |
| 19 | import java.io.Writer; |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.concurrent.Semaphore; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.os.Handler; |
| 25 | import android.os.Message; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 28 | import android.view.Surface; |
| 29 | import android.view.SurfaceHolder; |
| 30 | import android.view.SurfaceView; |
| 31 | |
Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 32 | /** |
Alex Sakhartchouk | 93c47f1 | 2011-11-11 11:49:45 -0800 | [diff] [blame] | 33 | * The Surface View for a graphics renderscript (RenderScriptGL) to draw on. |
Stephen Hines | b11e3d2 | 2011-01-11 19:30:58 -0800 | [diff] [blame] | 34 | */ |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 35 | public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
| 36 | private SurfaceHolder mSurfaceHolder; |
Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 37 | private RenderScriptGL mRS; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Standard View constructor. In order to render something, you |
Stephen Hines | b11e3d2 | 2011-01-11 19:30:58 -0800 | [diff] [blame] | 41 | * must call {@link android.opengl.GLSurfaceView#setRenderer} to |
| 42 | * register a renderer. |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 43 | */ |
| 44 | public RSSurfaceView(Context context) { |
| 45 | super(context); |
| 46 | init(); |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 47 | //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Standard View constructor. In order to render something, you |
Stephen Hines | b11e3d2 | 2011-01-11 19:30:58 -0800 | [diff] [blame] | 52 | * must call {@link android.opengl.GLSurfaceView#setRenderer} to |
| 53 | * register a renderer. |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 54 | */ |
| 55 | public RSSurfaceView(Context context, AttributeSet attrs) { |
| 56 | super(context, attrs); |
| 57 | init(); |
Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 58 | //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 59 | } |
| 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 Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 66 | } |
| 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 Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 73 | mSurfaceHolder = holder; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 74 | } |
| 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 Sakhartchouk | 38da508 | 2011-11-15 14:21:58 -0800 | [diff] [blame] | 80 | 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 Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 86 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 87 | } |
| 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 Sakhartchouk | 38da508 | 2011-11-15 14:21:58 -0800 | [diff] [blame] | 93 | 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 Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 98 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 101 | /** |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 102 | * 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 Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 107 | public void pause() { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 108 | if(mRS != null) { |
| 109 | mRS.pause(); |
| 110 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 111 | } |
| 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 Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 120 | public void resume() { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 121 | if(mRS != null) { |
| 122 | mRS.resume(); |
| 123 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 126 | public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { |
Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 127 | RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc); |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 128 | setRenderScriptGL(rs); |
| 129 | return rs; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Alex Sakhartchouk | 38da508 | 2011-11-15 14:21:58 -0800 | [diff] [blame] | 132 | public void destroyRenderScriptGL() { |
| 133 | synchronized (this) { |
| 134 | mRS.destroy(); |
| 135 | mRS = null; |
| 136 | } |
Joe Onorato | 5fda65f | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 137 | } |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 138 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 139 | public void setRenderScriptGL(RenderScriptGL rs) { |
Romain Guy | a8551b1 | 2010-03-10 22:11:50 -0800 | [diff] [blame] | 140 | mRS = rs; |
| 141 | } |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 142 | |
| 143 | public RenderScriptGL getRenderScriptGL() { |
| 144 | return mRS; |
| 145 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 146 | } |