| 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 | 93c47f1 | 2011-11-11 11:49:45 -0800 | [diff] [blame] | 80 | public synchronized void surfaceDestroyed(SurfaceHolder holder) { |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 81 | // Surface will be destroyed when we return |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 82 | if (mRS != null) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 83 | mRS.setSurface(null, 0, 0); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 84 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 85 | } |
| 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 Sakhartchouk | 93c47f1 | 2011-11-11 11:49:45 -0800 | [diff] [blame] | 91 | public synchronized void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 92 | if (mRS != null) { |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 93 | mRS.setSurface(holder, w, h); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 94 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 97 | /** |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 98 | * 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 Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 103 | public void pause() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 104 | if(mRS != null) { |
| 105 | mRS.pause(); |
| 106 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 107 | } |
| 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 Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 116 | public void resume() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 117 | if(mRS != null) { |
| 118 | mRS.resume(); |
| 119 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 122 | public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { |
| Shih-wei Liao | 6b32fab | 2010-12-10 01:03:59 -0800 | [diff] [blame] | 123 | RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc); |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 124 | setRenderScriptGL(rs); |
| 125 | return rs; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| Alex Sakhartchouk | 93c47f1 | 2011-11-11 11:49:45 -0800 | [diff] [blame] | 128 | public synchronized void destroyRenderScriptGL() { |
| Joe Onorato | 5fda65f1 | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 129 | mRS.destroy(); |
| 130 | mRS = null; |
| 131 | } |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 132 | |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 133 | public void setRenderScriptGL(RenderScriptGL rs) { |
| Romain Guy | a8551b1 | 2010-03-10 22:11:50 -0800 | [diff] [blame] | 134 | mRS = rs; |
| 135 | } |
| Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 136 | |
| 137 | public RenderScriptGL getRenderScriptGL() { |
| 138 | return mRS; |
| 139 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 140 | } |