Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 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 | |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 21 | import android.graphics.Matrix; |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 22 | import android.graphics.Paint; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 23 | import android.graphics.SurfaceTexture; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 24 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 25 | import com.android.internal.util.VirtualRefBasePtr; |
| 26 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 27 | /** |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 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. |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 31 | * |
| 32 | * @hide |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 33 | */ |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 34 | final class TextureLayer { |
John Reck | 51aaf90 | 2015-12-02 15:08:07 -0800 | [diff] [blame] | 35 | private ThreadedRenderer mRenderer; |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 36 | private VirtualRefBasePtr mFinalizer; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 37 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 38 | private TextureLayer(ThreadedRenderer renderer, long deferredUpdater) { |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 39 | if (renderer == null || deferredUpdater == 0) { |
| 40 | throw new IllegalArgumentException("Either hardware renderer: " + renderer |
| 41 | + " or deferredUpdater: " + deferredUpdater + " is invalid"); |
| 42 | } |
| 43 | mRenderer = renderer; |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 44 | mFinalizer = new VirtualRefBasePtr(deferredUpdater); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 47 | /** |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 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 | */ |
sergeyv | 342a7e6 | 2016-03-24 16:06:46 -0700 | [diff] [blame] | 53 | public void setLayerPaint(@Nullable Paint paint) { |
| 54 | nSetLayerPaint(mFinalizer.get(), paint != null ? paint.getNativeInstance() : 0); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 55 | mRenderer.pushLayerUpdate(this); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 59 | * Indicates whether this layer can be rendered. |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 60 | * |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 61 | * @return True if the layer can be rendered into, false otherwise |
| 62 | */ |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 63 | public boolean isValid() { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 64 | return mFinalizer != null && mFinalizer.get() != 0; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 65 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 68 | * Destroys resources without waiting for a GC. |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 69 | */ |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 70 | public void destroy() { |
| 71 | if (!isValid()) { |
| 72 | // Already destroyed |
| 73 | return; |
| 74 | } |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 75 | mRenderer.onLayerDestroyed(this); |
| 76 | mRenderer = null; |
| 77 | mFinalizer.release(); |
| 78 | mFinalizer = null; |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 79 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 80 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 81 | public long getDeferredLayerUpdater() { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 82 | return mFinalizer.get(); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 83 | } |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 84 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Copies this layer into the specified bitmap. |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 87 | * |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 88 | * @param bitmap The bitmap to copy they layer into |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 89 | * |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 90 | * @return True if the copy was successful, false otherwise |
| 91 | */ |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 92 | public boolean copyInto(Bitmap bitmap) { |
| 93 | return mRenderer.copyLayerInto(this, bitmap); |
| 94 | } |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 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 | * |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 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 |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 103 | * |
| 104 | * @return true if the layer's properties will change, false if they already |
| 105 | * match the desired values. |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 106 | */ |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 107 | public boolean prepare(int width, int height, boolean isOpaque) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 108 | return nPrepare(mFinalizer.get(), width, height, isOpaque); |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 109 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Sets an optional transform on this layer. |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 113 | * |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 114 | * @param matrix The transform to apply to the layer. |
| 115 | */ |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 116 | public void setTransform(Matrix matrix) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 117 | nSetTransform(mFinalizer.get(), matrix.native_instance); |
| 118 | mRenderer.pushLayerUpdate(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 119 | } |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 122 | * Indicates that this layer has lost its texture. |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 123 | */ |
John Reck | 918ad52 | 2014-06-27 14:45:25 -0700 | [diff] [blame] | 124 | public void detachSurfaceTexture() { |
| 125 | mRenderer.detachSurfaceTexture(mFinalizer.get()); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 126 | } |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 127 | |
John Reck | 12f5e34 | 2014-11-07 07:53:43 -0800 | [diff] [blame] | 128 | public long getLayerHandle() { |
| 129 | return mFinalizer.get(); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | public void setSurfaceTexture(SurfaceTexture surface) { |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 133 | nSetSurfaceTexture(mFinalizer.get(), surface); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 134 | mRenderer.pushLayerUpdate(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public void updateSurfaceTexture() { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 138 | nUpdateSurfaceTexture(mFinalizer.get()); |
| 139 | mRenderer.pushLayerUpdate(this); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 140 | } |
| 141 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 142 | static TextureLayer adoptTextureLayer(ThreadedRenderer renderer, long layer) { |
| 143 | return new TextureLayer(renderer, layer); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 144 | } |
| 145 | |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 146 | private static native boolean nPrepare(long layerUpdater, int width, int height, |
| 147 | boolean isOpaque); |
Derek Sollenberger | 674554f | 2014-02-19 16:47:32 +0000 | [diff] [blame] | 148 | private static native void nSetLayerPaint(long layerUpdater, long paint); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 149 | private static native void nSetTransform(long layerUpdater, long matrix); |
sergeyv | 00eb43d | 2017-02-13 14:34:15 -0800 | [diff] [blame] | 150 | private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface); |
John Reck | 04fc583 | 2014-02-05 16:38:25 -0800 | [diff] [blame] | 151 | private static native void nUpdateSurfaceTexture(long layerUpdater); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 152 | } |