blob: a12434c0835e2a7c42d768d37a6cfeffac52ef92 [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
Romain Guy02ccac62011-06-24 13:20:23 -070019import android.graphics.Bitmap;
Romain Guy302a9df2011-08-16 13:55:02 -070020import android.graphics.Matrix;
Chet Haased15ebf22012-09-05 11:40:29 -070021import android.graphics.Paint;
John Reck04fc5832014-02-05 16:38:25 -080022import android.graphics.SurfaceTexture;
Romain Guy6c319ca2011-01-11 14:29:25 -080023
John Reckd72e0a32014-05-29 18:56:11 -070024import com.android.internal.util.VirtualRefBasePtr;
25
Romain Guy6c319ca2011-01-11 14:29:25 -080026/**
27 * A hardware layer can be used to render graphics operations into a hardware
Romain Guy52036b12013-02-14 18:03:37 -080028 * friendly buffer. For instance, with an OpenGL backend a hardware layer
Romain Guy6c319ca2011-01-11 14:29:25 -080029 * 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.
John Reck04fc5832014-02-05 16:38:25 -080032 *
33 * @hide
Romain Guy6c319ca2011-01-11 14:29:25 -080034 */
John Reck04fc5832014-02-05 16:38:25 -080035final class HardwareLayer {
John Reck51aaf902015-12-02 15:08:07 -080036 private ThreadedRenderer mRenderer;
John Reckd72e0a32014-05-29 18:56:11 -070037 private VirtualRefBasePtr mFinalizer;
Romain Guy6c319ca2011-01-11 14:29:25 -080038
John Reck51aaf902015-12-02 15:08:07 -080039 private HardwareLayer(ThreadedRenderer renderer, long deferredUpdater) {
John Reck04fc5832014-02-05 16:38:25 -080040 if (renderer == null || deferredUpdater == 0) {
41 throw new IllegalArgumentException("Either hardware renderer: " + renderer
42 + " or deferredUpdater: " + deferredUpdater + " is invalid");
43 }
44 mRenderer = renderer;
John Reckd72e0a32014-05-29 18:56:11 -070045 mFinalizer = new VirtualRefBasePtr(deferredUpdater);
Romain Guyaa6c24c2011-04-28 18:40:04 -070046 }
47
Romain Guy6c319ca2011-01-11 14:29:25 -080048 /**
Chet Haased15ebf22012-09-05 11:40:29 -070049 * 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 */
John Reck04fc5832014-02-05 16:38:25 -080054 public void setLayerPaint(Paint paint) {
Derek Sollenbergerdfba4d32014-09-02 15:42:54 -040055 nSetLayerPaint(mFinalizer.get(), paint.getNativeInstance());
John Reckd72e0a32014-05-29 18:56:11 -070056 mRenderer.pushLayerUpdate(this);
Romain Guy6c319ca2011-01-11 14:29:25 -080057 }
58
59 /**
Romain Guy6c319ca2011-01-11 14:29:25 -080060 * Indicates whether this layer can be rendered.
John Reck04fc5832014-02-05 16:38:25 -080061 *
Romain Guy6c319ca2011-01-11 14:29:25 -080062 * @return True if the layer can be rendered into, false otherwise
63 */
John Reck04fc5832014-02-05 16:38:25 -080064 public boolean isValid() {
John Reckd72e0a32014-05-29 18:56:11 -070065 return mFinalizer != null && mFinalizer.get() != 0;
John Reck04fc5832014-02-05 16:38:25 -080066 }
Romain Guy6c319ca2011-01-11 14:29:25 -080067
68 /**
John Reck04fc5832014-02-05 16:38:25 -080069 * Destroys resources without waiting for a GC.
Romain Guy6c319ca2011-01-11 14:29:25 -080070 */
John Reck04fc5832014-02-05 16:38:25 -080071 public void destroy() {
72 if (!isValid()) {
73 // Already destroyed
74 return;
75 }
John Reckd72e0a32014-05-29 18:56:11 -070076 mRenderer.onLayerDestroyed(this);
77 mRenderer = null;
78 mFinalizer.release();
79 mFinalizer = null;
John Reck04fc5832014-02-05 16:38:25 -080080 }
Romain Guy6c319ca2011-01-11 14:29:25 -080081
John Reck19b6bcf2014-02-14 20:03:38 -080082 public long getDeferredLayerUpdater() {
John Reckd72e0a32014-05-29 18:56:11 -070083 return mFinalizer.get();
John Reck04fc5832014-02-05 16:38:25 -080084 }
Romain Guy6c319ca2011-01-11 14:29:25 -080085
Romain Guy02ccac62011-06-24 13:20:23 -070086 /**
87 * Copies this layer into the specified bitmap.
John Reck04fc5832014-02-05 16:38:25 -080088 *
Romain Guy02ccac62011-06-24 13:20:23 -070089 * @param bitmap The bitmap to copy they layer into
John Reck04fc5832014-02-05 16:38:25 -080090 *
Romain Guy02ccac62011-06-24 13:20:23 -070091 * @return True if the copy was successful, false otherwise
92 */
John Reck04fc5832014-02-05 16:38:25 -080093 public boolean copyInto(Bitmap bitmap) {
94 return mRenderer.copyLayerInto(this, bitmap);
95 }
Romain Guy02ccac62011-06-24 13:20:23 -070096
97 /**
John Reck04fc5832014-02-05 16:38:25 -080098 * Update the layer's properties. Note that after calling this isValid() may
99 * return false if the requested width/height cannot be satisfied
100 *
Romain Guy02ccac62011-06-24 13:20:23 -0700101 * @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
John Reck04fc5832014-02-05 16:38:25 -0800104 *
105 * @return true if the layer's properties will change, false if they already
106 * match the desired values.
Romain Guy02ccac62011-06-24 13:20:23 -0700107 */
John Reck04fc5832014-02-05 16:38:25 -0800108 public boolean prepare(int width, int height, boolean isOpaque) {
John Reckd72e0a32014-05-29 18:56:11 -0700109 return nPrepare(mFinalizer.get(), width, height, isOpaque);
Romain Guy02ccac62011-06-24 13:20:23 -0700110 }
Romain Guy302a9df2011-08-16 13:55:02 -0700111
112 /**
113 * Sets an optional transform on this layer.
John Reck04fc5832014-02-05 16:38:25 -0800114 *
Romain Guy302a9df2011-08-16 13:55:02 -0700115 * @param matrix The transform to apply to the layer.
116 */
John Reck04fc5832014-02-05 16:38:25 -0800117 public void setTransform(Matrix matrix) {
John Reckd72e0a32014-05-29 18:56:11 -0700118 nSetTransform(mFinalizer.get(), matrix.native_instance);
119 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800120 }
Romain Guy2bf68f02012-03-02 13:37:47 -0800121
122 /**
John Reck04fc5832014-02-05 16:38:25 -0800123 * Indicates that this layer has lost its texture.
Romain Guy2bf68f02012-03-02 13:37:47 -0800124 */
John Reck918ad522014-06-27 14:45:25 -0700125 public void detachSurfaceTexture() {
126 mRenderer.detachSurfaceTexture(mFinalizer.get());
John Reck04fc5832014-02-05 16:38:25 -0800127 }
Romain Guyef09a212012-09-25 12:17:14 -0700128
John Reck12f5e342014-11-07 07:53:43 -0800129 public long getLayerHandle() {
130 return mFinalizer.get();
John Reck04fc5832014-02-05 16:38:25 -0800131 }
132
133 public void setSurfaceTexture(SurfaceTexture surface) {
John Reckd72e0a32014-05-29 18:56:11 -0700134 nSetSurfaceTexture(mFinalizer.get(), surface, false);
135 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800136 }
137
138 public void updateSurfaceTexture() {
John Reckd72e0a32014-05-29 18:56:11 -0700139 nUpdateSurfaceTexture(mFinalizer.get());
140 mRenderer.pushLayerUpdate(this);
John Reck04fc5832014-02-05 16:38:25 -0800141 }
142
John Reck51aaf902015-12-02 15:08:07 -0800143 static HardwareLayer adoptTextureLayer(ThreadedRenderer renderer, long layer) {
John Reck25fbb3f2014-06-12 13:46:45 -0700144 return new HardwareLayer(renderer, layer);
John Reck04fc5832014-02-05 16:38:25 -0800145 }
146
John Reck04fc5832014-02-05 16:38:25 -0800147 private static native boolean nPrepare(long layerUpdater, int width, int height, 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);
150 private static native void nSetSurfaceTexture(long layerUpdater,
151 SurfaceTexture surface, boolean isAlreadyAttached);
152 private static native void nUpdateSurfaceTexture(long layerUpdater);
Romain Guy6c319ca2011-01-11 14:29:25 -0800153}