Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2011 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 
     17 package android.renderscript;
     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.graphics.SurfaceTexture;
     25 import android.os.Handler;
     26 import android.os.Message;
     27 import android.util.AttributeSet;
     28 import android.util.Log;
     29 import android.view.TextureView;
     30 
     31 /**
     32  * The Texture View for a graphics renderscript (RenderScriptGL)
     33  * to draw on.
     34  *
     35  */
     36 public class RSTextureView extends TextureView implements TextureView.SurfaceTextureListener {
     37     private RenderScriptGL mRS;
     38     private SurfaceTexture mSurfaceTexture;
     39 
     40     /**
     41      * Standard View constructor. In order to render something, you
     42      * must call {@link android.opengl.GLSurfaceView#setRenderer} to
     43      * register a renderer.
     44      */
     45     public RSTextureView(Context context) {
     46         super(context);
     47         init();
     48         //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
     49     }
     50 
     51     /**
     52      * Standard View constructor. In order to render something, you
     53      * must call {@link android.opengl.GLSurfaceView#setRenderer} to
     54      * register a renderer.
     55      */
     56     public RSTextureView(Context context, AttributeSet attrs) {
     57         super(context, attrs);
     58         init();
     59         //Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
     60     }
     61 
     62     private void init() {
     63         setSurfaceTextureListener(this);
     64         //android.util.Log.e("rs", "getSurfaceTextureListerner " + getSurfaceTextureListener());
     65     }
     66 
     67     @Override
     68     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
     69         //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureAvailable");
     70         mSurfaceTexture = surface;
     71 
     72         if (mRS != null) {
     73             mRS.setSurfaceTexture(mSurfaceTexture, width, height);
     74         }
     75     }
     76 
     77     @Override
     78     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
     79         //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureSizeChanged");
     80         mSurfaceTexture = surface;
     81 
     82         if (mRS != null) {
     83             mRS.setSurfaceTexture(mSurfaceTexture, width, height);
     84         }
     85     }
     86 
     87     @Override
     88     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
     89         //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureDestroyed");
     90         mSurfaceTexture = surface;
     91 
     92         if (mRS != null) {
     93             mRS.setSurfaceTexture(null, 0, 0);
     94         }
     95 
     96         return true;
     97     }
     98 
     99     @Override
    100     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    101         //Log.e(RenderScript.LOG_TAG, "onSurfaceTextureUpdated");
    102         mSurfaceTexture = surface;
    103     }
    104 
    105    /**
    106      * Inform the view that the activity is paused. The owner of this view must
    107      * call this method when the activity is paused. Calling this method will
    108      * pause the rendering thread.
    109      * Must not be called before a renderer has been set.
    110      */
    111     public void pause() {
    112         if(mRS != null) {
    113             mRS.pause();
    114         }
    115     }
    116 
    117     /**
    118      * Inform the view that the activity is resumed. The owner of this view must
    119      * call this method when the activity is resumed. Calling this method will
    120      * recreate the OpenGL display and resume the rendering
    121      * thread.
    122      * Must not be called before a renderer has been set.
    123      */
    124     public void resume() {
    125         if(mRS != null) {
    126             mRS.resume();
    127         }
    128     }
    129 
    130     /**
    131      * Create a new RenderScriptGL object and attach it to the
    132      * TextureView if present.
    133      *
    134      *
    135      * @param sc The RS surface config to create.
    136      *
    137      * @return RenderScriptGL The new object created.
    138      */
    139     public RenderScriptGL createRenderScriptGL(RenderScriptGL.SurfaceConfig sc) {
    140         RenderScriptGL rs = new RenderScriptGL(this.getContext(), sc);
    141         setRenderScriptGL(rs);
    142         if (mSurfaceTexture != null) {
    143             mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight());
    144         }
    145         return rs;
    146     }
    147 
    148     /**
    149      * Destroy the RenderScriptGL object associated with this
    150      * TextureView.
    151      */
    152     public void destroyRenderScriptGL() {
    153         mRS.destroy();
    154         mRS = null;
    155     }
    156 
    157     /**
    158      * Set a new RenderScriptGL object.  This also will attach the
    159      * new object to the TextureView if present.
    160      *
    161      * @param rs The new RS object.
    162      */
    163     public void setRenderScriptGL(RenderScriptGL rs) {
    164         mRS = rs;
    165         if (mSurfaceTexture != null) {
    166             mRS.setSurfaceTexture(mSurfaceTexture, getWidth(), getHeight());
    167         }
    168     }
    169 
    170     /**
    171      * Returns the previously set RenderScriptGL object.
    172      *
    173      * @return RenderScriptGL
    174      */
    175     public RenderScriptGL getRenderScriptGL() {
    176         return mRS;
    177     }
    178 }
    179 
    180