Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
John Reck | 4afbed1 | 2016-04-18 09:29:36 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 22 | import android.graphics.BaseRecordingCanvas; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 24 | import android.graphics.CanvasProperty; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 25 | import android.graphics.Paint; |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 26 | import android.util.Pools.SynchronizedPool; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 27 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 28 | import dalvik.annotation.optimization.CriticalNative; |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 29 | import dalvik.annotation.optimization.FastNative; |
| 30 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 31 | /** |
Derek Sollenberger | f64c34e | 2016-06-28 16:39:13 -0400 | [diff] [blame] | 32 | * A Canvas implementation that records view system drawing operations for deferred rendering. |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 33 | * This is intended for use with a DisplayList. This class keeps a list of all the Paint and |
| 34 | * Bitmap objects that it draws, preventing the backing memory of Bitmaps from being freed while |
| 35 | * the DisplayList is still holding a native reference to the memory. |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 36 | * |
| 37 | * @hide |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 38 | */ |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 39 | public final class DisplayListCanvas extends BaseRecordingCanvas { |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 40 | // The recording canvas pool should be large enough to handle a deeply nested |
| 41 | // view hierarchy because display lists are generated recursively. |
| 42 | private static final int POOL_LIMIT = 25; |
| 43 | |
Lucas Dupin | d8e1244 | 2018-08-21 09:20:48 -0700 | [diff] [blame] | 44 | public static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024; // 100 MB |
sergeyv | 3d8298e | 2016-04-21 13:05:24 -0700 | [diff] [blame] | 45 | |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 46 | private static final SynchronizedPool<DisplayListCanvas> sPool = |
John Reck | caa08ff | 2016-10-07 13:21:36 -0700 | [diff] [blame] | 47 | new SynchronizedPool<>(POOL_LIMIT); |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 48 | |
| 49 | RenderNode mNode; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 50 | private int mWidth; |
| 51 | private int mHeight; |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 52 | |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 53 | static DisplayListCanvas obtain(@NonNull RenderNode node, int width, int height) { |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 54 | if (node == null) throw new IllegalArgumentException("node cannot be null"); |
| 55 | DisplayListCanvas canvas = sPool.acquire(); |
| 56 | if (canvas == null) { |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 57 | canvas = new DisplayListCanvas(node, width, height); |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 58 | } else { |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 59 | nResetDisplayListCanvas(canvas.mNativeCanvasWrapper, node.mNativeRenderNode, |
| 60 | width, height); |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 61 | } |
| 62 | canvas.mNode = node; |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 63 | canvas.mWidth = width; |
| 64 | canvas.mHeight = height; |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 65 | return canvas; |
| 66 | } |
| 67 | |
| 68 | void recycle() { |
| 69 | mNode = null; |
| 70 | sPool.release(this); |
| 71 | } |
| 72 | |
| 73 | long finishRecording() { |
| 74 | return nFinishRecording(mNativeCanvasWrapper); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean isRecordingFor(Object o) { |
| 79 | return o == mNode; |
| 80 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 81 | |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 83 | // Constructors |
| 84 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 85 | |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 86 | private DisplayListCanvas(@NonNull RenderNode node, int width, int height) { |
| 87 | super(nCreateDisplayListCanvas(node.mNativeRenderNode, width, height)); |
Chris Craik | 9c08ee0 | 2015-05-21 16:46:53 -0700 | [diff] [blame] | 88 | mDensity = 0; // disable bitmap density scaling |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 89 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 90 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 91 | /////////////////////////////////////////////////////////////////////////// |
| 92 | // Canvas management |
| 93 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 94 | |
Chris Craik | 9c08ee0 | 2015-05-21 16:46:53 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
| 97 | public void setDensity(int density) { |
| 98 | // drop silently, since DisplayListCanvas doesn't perform density scaling |
| 99 | } |
| 100 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 101 | @Override |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 102 | public boolean isHardwareAccelerated() { |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void setBitmap(Bitmap bitmap) { |
| 108 | throw new UnsupportedOperationException(); |
| 109 | } |
| 110 | |
| 111 | @Override |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 112 | public boolean isOpaque() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 113 | return false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int getWidth() { |
| 118 | return mWidth; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int getHeight() { |
| 123 | return mHeight; |
| 124 | } |
| 125 | |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public int getMaximumBitmapWidth() { |
| 128 | return nGetMaximumTextureWidth(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int getMaximumBitmapHeight() { |
| 133 | return nGetMaximumTextureHeight(); |
| 134 | } |
| 135 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 136 | /////////////////////////////////////////////////////////////////////////// |
| 137 | // Setup |
| 138 | /////////////////////////////////////////////////////////////////////////// |
| 139 | |
| 140 | @Override |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 141 | public void insertReorderBarrier() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 142 | nInsertReorderBarrier(mNativeCanvasWrapper, true); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void insertInorderBarrier() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 147 | nInsertReorderBarrier(mNativeCanvasWrapper, false); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 150 | /////////////////////////////////////////////////////////////////////////// |
| 151 | // Functor |
| 152 | /////////////////////////////////////////////////////////////////////////// |
| 153 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 154 | /** |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 155 | * Records the functor specified with the drawGLFunction function pointer. This is |
| 156 | * functionality used by webview for calling into their renderer from our display lists. |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 157 | * |
| 158 | * @param drawGLFunction A native function pointer |
| 159 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 160 | @UnsupportedAppUsage |
Narayan Kamath | 57616d3e | 2014-11-21 11:57:07 +0000 | [diff] [blame] | 161 | public void callDrawGLFunction2(long drawGLFunction) { |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 162 | nCallDrawGLFunction(mNativeCanvasWrapper, drawGLFunction, null); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 163 | } |
| 164 | |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Records the functor specified with the drawGLFunction function pointer. This is |
| 167 | * functionality used by webview for calling into their renderer from our display lists. |
| 168 | * |
| 169 | * @param drawGLFunction A native function pointer |
| 170 | * @param releasedCallback Called when the display list is destroyed, and thus |
| 171 | * the functor is no longer referenced by this canvas's display list. |
| 172 | * |
| 173 | * NOTE: The callback does *not* necessarily mean that there are no longer |
| 174 | * any references to the functor, just that the reference from this specific |
| 175 | * canvas's display list has been released. |
| 176 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 177 | @UnsupportedAppUsage |
John Reck | 4afbed1 | 2016-04-18 09:29:36 -0700 | [diff] [blame] | 178 | public void drawGLFunctor2(long drawGLFunctor, @Nullable Runnable releasedCallback) { |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 179 | nCallDrawGLFunction(mNativeCanvasWrapper, drawGLFunctor, releasedCallback); |
| 180 | } |
| 181 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 182 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 183 | // Display list |
| 184 | /////////////////////////////////////////////////////////////////////////// |
| 185 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 186 | /** |
John Reck | 568b2a6 | 2018-06-04 17:02:02 -0700 | [diff] [blame] | 187 | * Draws the specified display list onto this canvas. |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 188 | * |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 189 | * @param renderNode The RenderNode to draw. |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 190 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 191 | @UnsupportedAppUsage |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 192 | public void drawRenderNode(RenderNode renderNode) { |
John Reck | 568b2a6 | 2018-06-04 17:02:02 -0700 | [diff] [blame] | 193 | nDrawRenderNode(mNativeCanvasWrapper, renderNode.mNativeRenderNode); |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 196 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 197 | // Hardware layer |
| 198 | /////////////////////////////////////////////////////////////////////////// |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 199 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Draws the specified layer onto this canvas. |
| 202 | * |
| 203 | * @param layer The layer to composite on this canvas |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 204 | */ |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 205 | void drawTextureLayer(TextureLayer layer) { |
| 206 | nDrawTextureLayer(mNativeCanvasWrapper, layer.getLayerHandle()); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 209 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 210 | // Drawing |
| 211 | /////////////////////////////////////////////////////////////////////////// |
| 212 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 213 | @UnsupportedAppUsage |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 214 | public void drawCircle(CanvasProperty<Float> cx, CanvasProperty<Float> cy, |
| 215 | CanvasProperty<Float> radius, CanvasProperty<Paint> paint) { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 216 | nDrawCircle(mNativeCanvasWrapper, cx.getNativeContainer(), cy.getNativeContainer(), |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 217 | radius.getNativeContainer(), paint.getNativeContainer()); |
| 218 | } |
| 219 | |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 220 | public void drawRoundRect(CanvasProperty<Float> left, CanvasProperty<Float> top, |
| 221 | CanvasProperty<Float> right, CanvasProperty<Float> bottom, CanvasProperty<Float> rx, |
| 222 | CanvasProperty<Float> ry, CanvasProperty<Paint> paint) { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 223 | nDrawRoundRect(mNativeCanvasWrapper, left.getNativeContainer(), top.getNativeContainer(), |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 224 | right.getNativeContainer(), bottom.getNativeContainer(), |
| 225 | rx.getNativeContainer(), ry.getNativeContainer(), |
| 226 | paint.getNativeContainer()); |
| 227 | } |
| 228 | |
sergeyv | 3d8298e | 2016-04-21 13:05:24 -0700 | [diff] [blame] | 229 | @Override |
| 230 | protected void throwIfCannotDraw(Bitmap bitmap) { |
| 231 | super.throwIfCannotDraw(bitmap); |
| 232 | int bitmapSize = bitmap.getByteCount(); |
| 233 | if (bitmapSize > MAX_BITMAP_SIZE) { |
| 234 | throw new RuntimeException( |
| 235 | "Canvas: trying to draw too large(" + bitmapSize + "bytes) bitmap."); |
| 236 | } |
| 237 | } |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 238 | |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 239 | |
| 240 | // ------------------ Fast JNI ------------------------ |
| 241 | |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 242 | @FastNative |
| 243 | private static native void nCallDrawGLFunction(long renderer, |
| 244 | long drawGLFunction, Runnable releasedCallback); |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 245 | |
| 246 | |
| 247 | // ------------------ Critical JNI ------------------------ |
| 248 | |
| 249 | @CriticalNative |
| 250 | private static native long nCreateDisplayListCanvas(long node, int width, int height); |
| 251 | @CriticalNative |
| 252 | private static native void nResetDisplayListCanvas(long canvas, long node, |
| 253 | int width, int height); |
| 254 | @CriticalNative |
| 255 | private static native int nGetMaximumTextureWidth(); |
| 256 | @CriticalNative |
| 257 | private static native int nGetMaximumTextureHeight(); |
| 258 | @CriticalNative |
| 259 | private static native void nInsertReorderBarrier(long renderer, boolean enableReorder); |
| 260 | @CriticalNative |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 261 | private static native long nFinishRecording(long renderer); |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 262 | @CriticalNative |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 263 | private static native void nDrawRenderNode(long renderer, long renderNode); |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 264 | @CriticalNative |
John Reck | 9d8d99d | 2018-02-21 12:55:41 -0800 | [diff] [blame] | 265 | private static native void nDrawTextureLayer(long renderer, long layer); |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 266 | @CriticalNative |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 267 | private static native void nDrawCircle(long renderer, long propCx, |
| 268 | long propCy, long propRadius, long propPaint); |
Chris Craik | fc29424 | 2016-12-13 18:10:46 -0800 | [diff] [blame] | 269 | @CriticalNative |
John Reck | 5cb74bc | 2016-10-07 11:24:44 -0700 | [diff] [blame] | 270 | private static native void nDrawRoundRect(long renderer, long propLeft, long propTop, |
| 271 | long propRight, long propBottom, long propRx, long propRy, long propPaint); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 272 | } |