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 TextureLayer(ThreadedRenderer renderer, long deferredUpdater)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 */ setLayerPaint(@ullable Paint paint)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 */ isValid()63 public boolean isValid() { 64 return mFinalizer != null && mFinalizer.get() != 0; 65 } 66 67 /** 68 * Destroys resources without waiting for a GC. 69 */ destroy()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 getDeferredLayerUpdater()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 */ copyInto(Bitmap bitmap)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 */ prepare(int width, int height, boolean isOpaque)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 */ setTransform(Matrix matrix)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 */ detachSurfaceTexture()124 public void detachSurfaceTexture() { 125 mRenderer.detachSurfaceTexture(mFinalizer.get()); 126 } 127 getLayerHandle()128 public long getLayerHandle() { 129 return mFinalizer.get(); 130 } 131 setSurfaceTexture(SurfaceTexture surface)132 public void setSurfaceTexture(SurfaceTexture surface) { 133 nSetSurfaceTexture(mFinalizer.get(), surface); 134 mRenderer.pushLayerUpdate(this); 135 } 136 updateSurfaceTexture()137 public void updateSurfaceTexture() { 138 nUpdateSurfaceTexture(mFinalizer.get()); 139 mRenderer.pushLayerUpdate(this); 140 } 141 adoptTextureLayer(ThreadedRenderer renderer, long layer)142 static TextureLayer adoptTextureLayer(ThreadedRenderer renderer, long layer) { 143 return new TextureLayer(renderer, layer); 144 } 145 nPrepare(long layerUpdater, int width, int height, boolean isOpaque)146 private static native boolean nPrepare(long layerUpdater, int width, int height, 147 boolean isOpaque); nSetLayerPaint(long layerUpdater, long paint)148 private static native void nSetLayerPaint(long layerUpdater, long paint); nSetTransform(long layerUpdater, long matrix)149 private static native void nSetTransform(long layerUpdater, long matrix); nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface)150 private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface); nUpdateSurfaceTexture(long layerUpdater)151 private static native void nUpdateSurfaceTexture(long layerUpdater); 152 } 153