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) { |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 72 | mSurfaceHolder = holder; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * This method is part of the SurfaceHolder.Callback interface, and is |
| 77 | * not normally called or subclassed by clients of RSSurfaceView. |
| 78 | */ |
| 79 | public void surfaceDestroyed(SurfaceHolder holder) { |
| 80 | // Surface will be destroyed when we return |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 81 | if (mRS != null) { |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 82 | mRS.setSurface(null, 0, 0); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 83 | } |
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 | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 91 | if (mRS != null) { |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 92 | mRS.setSurface(holder, w, h); |
Jason Sams | efd9b6fb | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 93 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 96 | /** |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 97 | * Inform the view that the activity is paused. The owner of this view must |
| 98 | * call this method when the activity is paused. Calling this method will |
| 99 | * pause the rendering thread. |
| 100 | * Must not be called before a renderer has been set. |
| 101 | */ |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 102 | public void pause() { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 103 | if(mRS != null) { |
| 104 | mRS.pause(); |
| 105 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Inform the view that the activity is resumed. The owner of this view must |
| 110 | * call this method when the activity is resumed. Calling this method will |
| 111 | * recreate the OpenGL display and resume the rendering |
| 112 | * thread. |
| 113 | * Must not be called before a renderer has been set. |
| 114 | */ |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 115 | public void resume() { |
Jason Sams | 65e7aa5 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 116 | if(mRS != null) { |
| 117 | mRS.resume(); |
| 118 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 121 | public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { |
| 122 | RenderScriptGL rs = new RenderScriptGL(sc); |
| 123 | setRenderScriptGL(rs); |
| 124 | return rs; |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 127 | public void destroyRenderScriptGL() { |
Joe Onorato | 5fda65f1 | 2009-09-25 09:12:16 -0700 | [diff] [blame] | 128 | mRS.destroy(); |
| 129 | mRS = null; |
| 130 | } |
Jason Sams | 2222aa9 | 2010-10-10 17:58:25 -0700 | [diff] [blame] | 131 | |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 132 | public void setRenderScriptGL(RenderScriptGL rs) { |
Romain Guy | a8551b1 | 2010-03-10 22:11:50 -0800 | [diff] [blame] | 133 | mRS = rs; |
| 134 | } |
Jason Sams | bf6ef8d7 | 2010-12-06 15:59:59 -0800 | [diff] [blame^] | 135 | |
| 136 | public RenderScriptGL getRenderScriptGL() { |
| 137 | return mRS; |
| 138 | } |
Jack Palevich | 60aa3ea | 2009-05-26 13:45:08 -0700 | [diff] [blame] | 139 | } |
| 140 | |