| 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; |
| 28 | import android.util.Log; |
| 29 | import android.view.Surface; |
| 30 | import android.view.SurfaceHolder; |
| 31 | import android.view.SurfaceView; |
| 32 | |
| Jason Sams | e29d471 | 2009-07-23 15:19:03 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @hide |
| 35 | * |
| 36 | **/ |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 37 | public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback { |
| 38 | private SurfaceHolder mSurfaceHolder; |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 39 | private RenderScript mRS; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Standard View constructor. In order to render something, you |
| 43 | * must call {@link #setRenderer} to register a renderer. |
| 44 | */ |
| 45 | public RSSurfaceView(Context context) { |
| 46 | super(context); |
| 47 | init(); |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 48 | //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Standard View constructor. In order to render something, you |
| 53 | * must call {@link #setRenderer} to register a renderer. |
| 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) { |
| Jason Sams | f29ca50 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 73 | Log.v(RenderScript.LOG_TAG, "surfaceCreated"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 74 | mSurfaceHolder = holder; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * This method is part of the SurfaceHolder.Callback interface, and is |
| 79 | * not normally called or subclassed by clients of RSSurfaceView. |
| 80 | */ |
| 81 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 82 | // Surface will be destroyed when we return |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 83 | //Log.v(RenderScript.LOG_TAG, "surfaceDestroyed"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * This method is part of the SurfaceHolder.Callback interface, and is |
| 88 | * not normally called or subclassed by clients of RSSurfaceView. |
| 89 | */ |
| 90 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 91 | //Log.v(RenderScript.LOG_TAG, "surfaceChanged"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Inform the view that the activity is paused. The owner of this view must |
| 96 | * call this method when the activity is paused. Calling this method will |
| 97 | * pause the rendering thread. |
| 98 | * Must not be called before a renderer has been set. |
| 99 | */ |
| 100 | public void onPause() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 101 | if(mRS != null) { |
| 102 | mRS.pause(); |
| 103 | } |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 104 | //Log.v(RenderScript.LOG_TAG, "onPause"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Inform the view that the activity is resumed. The owner of this view must |
| 109 | * call this method when the activity is resumed. Calling this method will |
| 110 | * recreate the OpenGL display and resume the rendering |
| 111 | * thread. |
| 112 | * Must not be called before a renderer has been set. |
| 113 | */ |
| 114 | public void onResume() { |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 115 | if(mRS != null) { |
| 116 | mRS.resume(); |
| 117 | } |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 118 | //Log.v(RenderScript.LOG_TAG, "onResume"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Queue a runnable to be run on the GL rendering thread. This can be used |
| 123 | * to communicate with the Renderer on the rendering thread. |
| 124 | * Must not be called before a renderer has been set. |
| 125 | * @param r the runnable to be run on the GL rendering thread. |
| 126 | */ |
| 127 | public void queueEvent(Runnable r) { |
| Jason Sams | 66b2771 | 2009-09-25 15:25:00 -0700 | [diff] [blame^] | 128 | //Log.v(RenderScript.LOG_TAG, "queueEvent"); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * This method is used as part of the View class and is not normally |
| 133 | * called or subclassed by clients of RSSurfaceView. |
| 134 | * Must not be called before a renderer has been set. |
| 135 | */ |
| 136 | @Override |
| 137 | protected void onDetachedFromWindow() { |
| 138 | super.onDetachedFromWindow(); |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // ---------------------------------------------------------------------- |
| 142 | |
| Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 143 | public RenderScript createRenderScript(boolean useDepth, boolean forceSW) { |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 144 | Surface sur = null; |
| 145 | while ((sur == null) || (mSurfaceHolder == null)) { |
| 146 | sur = getHolder().getSurface(); |
| 147 | } |
| Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 148 | mRS = new RenderScript(sur, useDepth, forceSW); |
| 149 | return mRS; |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| Jason Sams | ebfb436 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 152 | public RenderScript createRenderScript(boolean useDepth) { |
| 153 | return createRenderScript(useDepth, false); |
| 154 | } |
| 155 | |
| Joe Onorato | 5fda65f1 | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 156 | public void destroyRenderScript() { |
| 157 | mRS.destroy(); |
| 158 | mRS = null; |
| 159 | } |
| Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 160 | } |
| 161 | |