Home | History | Annotate | Download | only in view
      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.view;
     18 
     19 import android.annotation.Nullable;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Matrix;
     22 import android.graphics.Paint;
     23 import android.graphics.SurfaceTexture;
     24 
     25 import com.android.internal.util.VirtualRefBasePtr;
     26 
     27 /**
     28  * TextureLayer represents a SurfaceTexture that will be composited by RenderThread into the
     29  * frame when drawn in a HW accelerated Canvas. This is backed by a DeferredLayerUpdater on
     30  * the native side.
     31  *
     32  * @hide
     33  */
     34 final class TextureLayer {
     35     private ThreadedRenderer mRenderer;
     36     private VirtualRefBasePtr mFinalizer;
     37 
     38     private TextureLayer(ThreadedRenderer renderer, long deferredUpdater) {
     39         if (renderer == null || deferredUpdater == 0) {
     40             throw new IllegalArgumentException("Either hardware renderer: " + renderer
     41                     + " or deferredUpdater: " + deferredUpdater + " is invalid");
     42         }
     43         mRenderer = renderer;
     44         mFinalizer = new VirtualRefBasePtr(deferredUpdater);
     45     }
     46 
     47     /**
     48      * Update the paint used when drawing this layer.
     49      *
     50      * @param paint The paint used when the layer is drawn into the destination canvas.
     51      * @see View#setLayerPaint(android.graphics.Paint)
     52      */
     53     public void setLayerPaint(@Nullable Paint paint) {
     54         nSetLayerPaint(mFinalizer.get(), paint != null ? paint.getNativeInstance() : 0);
     55         mRenderer.pushLayerUpdate(this);
     56     }
     57 
     58     /**
     59      * Indicates whether this layer can be rendered.
     60      *
     61      * @return True if the layer can be rendered into, false otherwise
     62      */
     63     public boolean isValid() {
     64         return mFinalizer != null && mFinalizer.get() != 0;
     65     }
     66 
     67     /**
     68      * Destroys resources without waiting for a GC.
     69      */
     70     public void destroy() {
     71         if (!isValid()) {
     72             // Already destroyed
     73             return;
     74         }
     75         mRenderer.onLayerDestroyed(this);
     76         mRenderer = null;
     77         mFinalizer.release();
     78         mFinalizer = null;
     79     }
     80 
     81     public long getDeferredLayerUpdater() {
     82         return mFinalizer.get();
     83     }
     84 
     85     /**
     86      * Copies this layer into the specified bitmap.
     87      *
     88      * @param bitmap The bitmap to copy they layer into
     89      *
     90      * @return True if the copy was successful, false otherwise
     91      */
     92     public boolean copyInto(Bitmap bitmap) {
     93         return mRenderer.copyLayerInto(this, bitmap);
     94     }
     95 
     96     /**
     97      * Update the layer's properties. Note that after calling this isValid() may
     98      * return false if the requested width/height cannot be satisfied
     99      *
    100      * @param width The new width of this layer
    101      * @param height The new height of this layer
    102      * @param isOpaque Whether this layer is opaque
    103      *
    104      * @return true if the layer's properties will change, false if they already
    105      *         match the desired values.
    106      */
    107     public boolean prepare(int width, int height, boolean isOpaque) {
    108         return nPrepare(mFinalizer.get(), width, height, isOpaque);
    109     }
    110 
    111     /**
    112      * Sets an optional transform on this layer.
    113      *
    114      * @param matrix The transform to apply to the layer.
    115      */
    116     public void setTransform(Matrix matrix) {
    117         nSetTransform(mFinalizer.get(), matrix.native_instance);
    118         mRenderer.pushLayerUpdate(this);
    119     }
    120 
    121     /**
    122      * Indicates that this layer has lost its texture.
    123      */
    124     public void detachSurfaceTexture() {
    125         mRenderer.detachSurfaceTexture(mFinalizer.get());
    126     }
    127 
    128     public long getLayerHandle() {
    129         return mFinalizer.get();
    130     }
    131 
    132     public void setSurfaceTexture(SurfaceTexture surface) {
    133         nSetSurfaceTexture(mFinalizer.get(), surface);
    134         mRenderer.pushLayerUpdate(this);
    135     }
    136 
    137     public void updateSurfaceTexture() {
    138         nUpdateSurfaceTexture(mFinalizer.get());
    139         mRenderer.pushLayerUpdate(this);
    140     }
    141 
    142     static TextureLayer adoptTextureLayer(ThreadedRenderer renderer, long layer) {
    143         return new TextureLayer(renderer, layer);
    144     }
    145 
    146     private static native boolean nPrepare(long layerUpdater, int width, int height,
    147             boolean isOpaque);
    148     private static native void nSetLayerPaint(long layerUpdater, long paint);
    149     private static native void nSetTransform(long layerUpdater, long matrix);
    150     private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface);
    151     private static native void nUpdateSurfaceTexture(long layerUpdater);
    152 }
    153