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