RSSurfaceView.java revision b11e3d2b0edb03a5e3ea535d58b0cbe2d920ed16
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 17package android.renderscript; 18 19import java.io.Writer; 20import java.util.ArrayList; 21import java.util.concurrent.Semaphore; 22 23import android.content.Context; 24import android.os.Handler; 25import android.os.Message; 26import android.util.AttributeSet; 27import android.util.Log; 28import android.view.Surface; 29import android.view.SurfaceHolder; 30import android.view.SurfaceView; 31 32/** 33 * 34 */ 35public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback { 36 private SurfaceHolder mSurfaceHolder; 37 private RenderScriptGL mRS; 38 39 /** 40 * Standard View constructor. In order to render something, you 41 * must call {@link android.opengl.GLSurfaceView#setRenderer} to 42 * register a renderer. 43 */ 44 public RSSurfaceView(Context context) { 45 super(context); 46 init(); 47 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); 48 } 49 50 /** 51 * Standard View constructor. In order to render something, you 52 * must call {@link android.opengl.GLSurfaceView#setRenderer} to 53 * register a renderer. 54 */ 55 public RSSurfaceView(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 init(); 58 //Log.v(RenderScript.LOG_TAG, "RSSurfaceView"); 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); 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) { 73 mSurfaceHolder = holder; 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 82 if (mRS != null) { 83 mRS.setSurface(null, 0, 0); 84 } 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 */ 91 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 92 if (mRS != null) { 93 mRS.setSurface(holder, w, h); 94 } 95 } 96 97 /** 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 */ 103 public void pause() { 104 if(mRS != null) { 105 mRS.pause(); 106 } 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 */ 116 public void resume() { 117 if(mRS != null) { 118 mRS.resume(); 119 } 120 } 121 122 public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) { 123 RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc); 124 setRenderScriptGL(rs); 125 return rs; 126 } 127 128 public void destroyRenderScriptGL() { 129 mRS.destroy(); 130 mRS = null; 131 } 132 133 public void setRenderScriptGL(RenderScriptGL rs) { 134 mRS = rs; 135 } 136 137 public RenderScriptGL getRenderScriptGL() { 138 return mRS; 139 } 140} 141