blob: 35a886fa27a37d1045c3424a15aee0a98c21982b [file] [log] [blame]
Romain Guy6c319ca2011-01-11 14:29:25 -08001/*
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
17package android.view;
18
sergeyv342a7e62016-03-24 16:06:46 -070019import android.annotation.Nullable;
Romain Guy02ccac62011-06-24 13:20:23 -070020import android.graphics.Bitmap;
Romain Guy302a9df2011-08-16 13:55:02 -070021import android.graphics.Matrix;
Chet Haased15ebf22012-09-05 11:40:29 -070022import android.graphics.Paint;
John Reck04fc5832014-02-05 16:38:25 -080023import android.graphics.SurfaceTexture;
Romain Guy6c319ca2011-01-11 14:29:25 -080024
John Reckd72e0a32014-05-29 18:56:11 -070025import com.android.internal.util.VirtualRefBasePtr;
26
Romain Guy6c319ca2011-01-11 14:29:25 -080027/**
John Reck9d8d99d2018-02-21 12:55:41 -080028 * 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.
John Reck04fc5832014-02-05 16:38:25 -080031 *
32 * @hide
Romain Guy6c319ca2011-01-11 14:29:25 -080033 */
John Reck9d8d99d2018-02-21 12:55:41 -080034final class TextureLayer {
John Reck51aaf902015-12-02 15:08:07 -080035 private ThreadedRenderer mRenderer;
John Reckd72e0a32014-05-29 18:56:11 -070036 private VirtualRefBasePtr mFinalizer;
Romain Guy6c319ca2011-01-11 14:29:25 -080037
John Reck9d8d99d2018-02-21 12:55:41 -080038 private TextureLayer(ThreadedRenderer renderer, long deferredUpdater) {
John Reck04fc5832014-02-05 16:38:25 -080039 if (renderer == null || deferredUpdater == 0) {
40 throw new IllegalArgumentException("Either hardware renderer: " + renderer
41 + " or deferredUpdater: " + deferredUpdater + " is invalid");
42 }
43 mRenderer = renderer;
John Reckd72e0a32014-05-29 18:56:11 -070044 mFinalizer = new VirtualRefBasePtr(deferredUpdater);
Romain Guyaa6c24c2011-04-28 18:40:04 -070045 }
46
Romain Guy6c319ca2011-01-11 14:29:25 -080047 /**
Chet Haased15ebf22012-09-05 11:40:29 -070048 * 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 */
sergeyv342a7e62016-03-24 16:06:46 -070053 public void setLayerPaint(@Nullable Paint paint) {
54 nSetLayerPaint(mFinalizer.get(), paint != null ? paint.getNativeInstance() : 0);
John Reckd72e0a32014-05-29 18:56:11 -070055 mRenderer.pushLayerUpdate(this);
Romain Guy6c319ca2011-01-11 14:29:25 -080056 }
57
58 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080059 * Indicates whether this layer can be rendered.
John Reck04fc5832014-02-05 16:38:25 -080060 *
Romain Guy6c319ca2011-01-11 14:29:25 -080061 * @return True if the layer can be rendered into, false otherwise
62 */
John Reck04fc5832014-02-05 16:38:25 -080063 public boolean isValid() {
John Reckd72e0a32014-05-29 18:56:11 -070064 return mFinalizer != null && mFinalizer.get() != 0;
John Reck04fc5832014-02-05 16:38:25 -080065 }
Romain Guy6c319ca2011-01-11 14:29:25 -080066
67 /**
John Reck04fc5832014-02-05 16:38:25 -080068 * Destroys resources without waiting for a GC.
Romain Guy6c319ca2011-01-11 14:29:25 -080069 */
John Reck04fc5832014-02-05 16:38:25 -080070 public void destroy() {
71 if (!isValid()) {
72 // Already destroyed
73 return;
74 }
John Reckd72e0a32014-05-29 18:56:11 -070075 mRenderer.onLayerDestroyed(this);
76 mRenderer = null;
77 mFinalizer.release();
78 mFinalizer = null;
John Reck04fc5832014-02-05 16:38:25 -080079 }
Romain Guy6c319ca2011-01-11 14:29:25 -080080
John Reck19b6bcf2014-02-14 20:03:38 -080081 public long getDeferredLayerUpdater() {
John Reckd72e0a32014-05-29 18:56:11 -070082 return mFinalizer.get();
John Reck04fc5832014-02-05 16:38:25 -080083 }
Romain Guy6c319ca2011-01-11 14:29:25 -080084
Romain Guy02ccac62011-06-24 13:20:23 -070085 /**
86 * Copies this layer into the specified bitmap.
John Reck04fc5832014-02-05 16:38:25 -080087 *
Romain Guy02ccac62011-06-24 13:20:23 -070088 * @param bitmap The bitmap to copy they layer into
John Reck04fc5832014-02-05 16:38:25 -080089 *
Romain Guy02ccac62011-06-24 13:20:23 -070090 * @return True if the copy was successful, false otherwise
91 */
John Reck04fc5832014-02-05 16:38:25 -080092 public boolean copyInto(Bitmap bitmap) {
93 return mRenderer.copyLayerInto(this, bitmap);
94 }
Romain Guy02ccac62011-06-24 13:20:23 -070095
96 /**
John Reck04fc5832014-02-05 16:38:25 -080097 * Update the layer's properties. Note that after calling this isValid() may
98 * return false if the requested width/height cannot be satisfied
99 *
Romain Guy02ccac62011-06-24 13:20:23 -0700100 * @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
John Reck04fc5832014-02-05 16:38:25 -0800103 *
104 * @return true if the layer's properties will change, false if they already
105 * match the desired values.
Romain Guy02ccac62011-06-24 13:20:23 -0700106 */
John Reck04fc5832014-02-05 16:38:25 -0800107 public boolean prepare(int width, int height, boolean isOpaque) {
John Reckd72e0a32014-05-29 18:56:11 -0700108 return nPrepare(mFinalizer.get(), width, height, isOpaque);
Romain Guy02ccac62011-06-24 13:20:23 -0700109 }
Romain Guy302a9df2011-08-16 13:55:02 -0700110
111 /**
112 * Sets an optional transform on this layer.
John Reck04fc5832014-02-05 16:38:25 -0800113 *
Romain Guy302a9df2011-08-16 13:55:02 -0700114 * @param matrix The transform to apply to the layer.
115 */
John Reck04fc5832014-02-05 16:38:25 -0800116 public void setTransform(Matrix matrix) {
John Reckd72e0a32014-05-29 18:56:11 -0700117 nSetTransform(mFinalizer.get(), matrix.native_instance);
118 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800119 }
Romain Guy2bf68f02012-03-02 13:37:47 -0800120
121 /**
John Reck04fc5832014-02-05 16:38:25 -0800122 * Indicates that this layer has lost its texture.
Romain Guy2bf68f02012-03-02 13:37:47 -0800123 */
John Reck918ad522014-06-27 14:45:25 -0700124 public void detachSurfaceTexture() {
125 mRenderer.detachSurfaceTexture(mFinalizer.get());
John Reck04fc5832014-02-05 16:38:25 -0800126 }
Romain Guyef09a212012-09-25 12:17:14 -0700127
John Reck12f5e342014-11-07 07:53:43 -0800128 public long getLayerHandle() {
129 return mFinalizer.get();
John Reck04fc5832014-02-05 16:38:25 -0800130 }
131
132 public void setSurfaceTexture(SurfaceTexture surface) {
sergeyv00eb43d2017-02-13 14:34:15 -0800133 nSetSurfaceTexture(mFinalizer.get(), surface);
John Reckd72e0a32014-05-29 18:56:11 -0700134 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800135 }
136
137 public void updateSurfaceTexture() {
John Reckd72e0a32014-05-29 18:56:11 -0700138 nUpdateSurfaceTexture(mFinalizer.get());
139 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800140 }
141
John Reck9d8d99d2018-02-21 12:55:41 -0800142 static TextureLayer adoptTextureLayer(ThreadedRenderer renderer, long layer) {
143 return new TextureLayer(renderer, layer);
John Reck04fc5832014-02-05 16:38:25 -0800144 }
145
John Reck9d8d99d2018-02-21 12:55:41 -0800146 private static native boolean nPrepare(long layerUpdater, int width, int height,
147 boolean isOpaque);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000148 private static native void nSetLayerPaint(long layerUpdater, long paint);
John Reck04fc5832014-02-05 16:38:25 -0800149 private static native void nSetTransform(long layerUpdater, long matrix);
sergeyv00eb43d2017-02-13 14:34:15 -0800150 private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface);
John Reck04fc5832014-02-05 16:38:25 -0800151 private static native void nUpdateSurfaceTexture(long layerUpdater);
Romain Guy6c319ca2011-01-11 14:29:25 -0800152}