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