| 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 | /** |
| 33 | * @hide |
| 34 | * |
| 35 | **/ |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 36 | public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
| 37 | private SurfaceHolder mSurfaceHolder; |
| Jason Sams | 704ff64 | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 38 | private RenderScriptGL mRS; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Standard View constructor. In order to render something, you |
| 42 | * must call {@link #setRenderer} to register a renderer. |
| 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 |
| 52 | * must call {@link #setRenderer} to register a renderer. |
| 53 | */ |
| 54 | public RSSurfaceView(Context context, AttributeSet attrs) { |
| 55 | super(context, attrs); |
| 56 | init(); |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 57 | //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private void init() { |
| 61 | // Install a SurfaceHolder.Callback so we get notified when the |
| 62 | // underlying surface is created and destroyed |
| 63 | SurfaceHolder holder = getHolder(); |
| 64 | holder.addCallback(this); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * This method is part of the SurfaceHolder.Callback interface, and is |
| 69 | * not normally called or subclassed by clients of RSSurfaceView. |
| 70 | */ |
| 71 | public void surfaceCreated(SurfaceHolder holder) { |
| Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 72 | Log.v(RenderScript.LOG_TAG, "surfaceCreated"); |
| 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 | */ |
| 80 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 81 | // Surface will be destroyed when we return |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 82 | Log.v(RenderScript.LOG_TAG, "surfaceDestroyed"); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 83 | if (mRS != null) { |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 84 | mRS.contextSetSurface(0, 0, null); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 85 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * This method is part of the SurfaceHolder.Callback interface, and is |
| 90 | * not normally called or subclassed by clients of RSSurfaceView. |
| 91 | */ |
| 92 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 93 | Log.v(RenderScript.LOG_TAG, "surfaceChanged"); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 94 | if (mRS != null) { |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 95 | mRS.contextSetSurface(w, h, holder.getSurface()); |
| Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 96 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Inform the view that the activity is paused. The owner of this view must |
| 101 | * call this method when the activity is paused. Calling this method will |
| 102 | * pause the rendering thread. |
| 103 | * Must not be called before a renderer has been set. |
| 104 | */ |
| 105 | public void onPause() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 106 | if(mRS != null) { |
| 107 | mRS.pause(); |
| 108 | } |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 109 | //Log.v(RenderScript.LOG_TAG, "onPause"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Inform the view that the activity is resumed. The owner of this view must |
| 114 | * call this method when the activity is resumed. Calling this method will |
| 115 | * recreate the OpenGL display and resume the rendering |
| 116 | * thread. |
| 117 | * Must not be called before a renderer has been set. |
| 118 | */ |
| 119 | public void onResume() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 120 | if(mRS != null) { |
| 121 | mRS.resume(); |
| 122 | } |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 123 | //Log.v(RenderScript.LOG_TAG, "onResume"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Queue a runnable to be run on the GL rendering thread. This can be used |
| 128 | * to communicate with the Renderer on the rendering thread. |
| 129 | * Must not be called before a renderer has been set. |
| 130 | * @param r the runnable to be run on the GL rendering thread. |
| 131 | */ |
| 132 | public void queueEvent(Runnable r) { |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 133 | //Log.v(RenderScript.LOG_TAG, "queueEvent"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * This method is used as part of the View class and is not normally |
| 138 | * called or subclassed by clients of RSSurfaceView. |
| 139 | * Must not be called before a renderer has been set. |
| 140 | */ |
| 141 | @Override |
| 142 | protected void onDetachedFromWindow() { |
| 143 | super.onDetachedFromWindow(); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // ---------------------------------------------------------------------- |
| 147 | |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 148 | public RenderScriptGL createRenderScript(RenderScriptGL.SurfaceConfig sc) { |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 149 | Log.v(RenderScript.LOG_TAG, "createRenderScript"); |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 150 | mRS = new RenderScriptGL(sc); |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 151 | return mRS; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| Joe Onorato | 5fda65f1 | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 154 | public void destroyRenderScript() { |
| Jason Sams | 3bc47d4 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 155 | Log.v(RenderScript.LOG_TAG, "destroyRenderScript"); |
| Joe Onorato | 5fda65f1 | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 156 | mRS.destroy(); |
| 157 | mRS = null; |
| 158 | } |
| Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 159 | |
| Romain Guy | a8551b1 | 2010-03-10 22:11:50 -0800 | [diff] [blame] | 160 | public void createRenderScript(RenderScriptGL rs) { |
| 161 | mRS = rs; |
| 162 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 163 | } |
| 164 | |