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