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.HardwareRenderer; 22 import android.graphics.Matrix; 23 import android.graphics.Paint; 24 import android.graphics.SurfaceTexture; 25 26 import com.android.internal.util.VirtualRefBasePtr; 27 28 /** 29 * TextureLayer represents a SurfaceTexture that will be composited by RenderThread into the 30 * frame when drawn in a HW accelerated Canvas. This is backed by a DeferredLayerUpdater on 31 * the native side. 32 * 33 * @hide 34 */ 35 public final class TextureLayer { 36 private HardwareRenderer mRenderer; 37 private VirtualRefBasePtr mFinalizer; 38 39 private TextureLayer(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(@Nullable Paint paint) { 55 nSetLayerPaint(mFinalizer.get(), paint != null ? paint.getNativeInstance() : 0); 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 getLayerHandle() { 130 return mFinalizer.get(); 131 } 132 133 public void setSurfaceTexture(SurfaceTexture surface) { 134 nSetSurfaceTexture(mFinalizer.get(), surface); 135 mRenderer.pushLayerUpdate(this); 136 } 137 138 public void updateSurfaceTexture() { 139 nUpdateSurfaceTexture(mFinalizer.get()); 140 mRenderer.pushLayerUpdate(this); 141 } 142 143 /** @hide */ 144 public static TextureLayer adoptTextureLayer(HardwareRenderer renderer, long layer) { 145 return new TextureLayer(renderer, layer); 146 } 147 148 private static native boolean nPrepare(long layerUpdater, int width, int height, 149 boolean isOpaque); 150 private static native void nSetLayerPaint(long layerUpdater, long paint); 151 private static native void nSetTransform(long layerUpdater, long matrix); 152 private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface); 153 private static native void nUpdateSurfaceTexture(long layerUpdater); 154 } 155