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; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 22 | import android.graphics.CanvasProperty; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 23 | import android.graphics.NinePatch; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 24 | import android.graphics.Paint; |
| 25 | import android.graphics.Path; |
| 26 | import android.graphics.Picture; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 27 | import android.graphics.Rect; |
| 28 | import android.graphics.RectF; |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 29 | import android.util.Pools.SynchronizedPool; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 30 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 31 | /** |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 32 | * An implementation of a GL canvas that records drawing operations. |
| 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 | */ |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 39 | public class DisplayListCanvas extends Canvas { |
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 | |
| 44 | private static final SynchronizedPool<DisplayListCanvas> sPool = |
| 45 | new SynchronizedPool<DisplayListCanvas>(POOL_LIMIT); |
| 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) { |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 55 | canvas = new DisplayListCanvas(width, height); |
| 56 | } else { |
| 57 | nResetDisplayListCanvas(canvas.mNativeCanvasWrapper, width, height); |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 58 | } |
| 59 | canvas.mNode = node; |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 60 | canvas.mWidth = width; |
| 61 | canvas.mHeight = height; |
Chris Craik | c9070eb | 2015-03-09 18:50:14 -0700 | [diff] [blame] | 62 | return canvas; |
| 63 | } |
| 64 | |
| 65 | void recycle() { |
| 66 | mNode = null; |
| 67 | sPool.release(this); |
| 68 | } |
| 69 | |
| 70 | long finishRecording() { |
| 71 | return nFinishRecording(mNativeCanvasWrapper); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public boolean isRecordingFor(Object o) { |
| 76 | return o == mNode; |
| 77 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 78 | |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 79 | /////////////////////////////////////////////////////////////////////////// |
| 80 | // JNI |
| 81 | /////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | private static native boolean nIsAvailable(); |
| 84 | private static boolean sIsAvailable = nIsAvailable(); |
| 85 | |
| 86 | static boolean isAvailable() { |
| 87 | return sIsAvailable; |
| 88 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////// |
| 91 | // Constructors |
| 92 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 93 | |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 94 | private DisplayListCanvas(int width, int height) { |
| 95 | super(nCreateDisplayListCanvas(width, height)); |
Chris Craik | 9c08ee0 | 2015-05-21 16:46:53 -0700 | [diff] [blame] | 96 | mDensity = 0; // disable bitmap density scaling |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 97 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 98 | |
Derek Sollenberger | cc882b6 | 2015-07-09 15:51:20 -0400 | [diff] [blame] | 99 | private static native long nCreateDisplayListCanvas(int width, int height); |
| 100 | private static native void nResetDisplayListCanvas(long canvas, int width, int height); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 101 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 102 | /////////////////////////////////////////////////////////////////////////// |
| 103 | // Canvas management |
| 104 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 105 | |
Chris Craik | 9c08ee0 | 2015-05-21 16:46:53 -0700 | [diff] [blame] | 106 | |
| 107 | @Override |
| 108 | public void setDensity(int density) { |
| 109 | // drop silently, since DisplayListCanvas doesn't perform density scaling |
| 110 | } |
| 111 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 112 | @Override |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 113 | public boolean isHardwareAccelerated() { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void setBitmap(Bitmap bitmap) { |
| 119 | throw new UnsupportedOperationException(); |
| 120 | } |
| 121 | |
| 122 | @Override |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 123 | public boolean isOpaque() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 124 | return false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int getWidth() { |
| 129 | return mWidth; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public int getHeight() { |
| 134 | return mHeight; |
| 135 | } |
| 136 | |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public int getMaximumBitmapWidth() { |
| 139 | return nGetMaximumTextureWidth(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int getMaximumBitmapHeight() { |
| 144 | return nGetMaximumTextureHeight(); |
| 145 | } |
| 146 | |
| 147 | private static native int nGetMaximumTextureWidth(); |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 148 | private static native int nGetMaximumTextureHeight(); |
Romain Guy | f61970fc | 2011-07-07 14:10:06 -0700 | [diff] [blame] | 149 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 150 | /////////////////////////////////////////////////////////////////////////// |
| 151 | // Setup |
| 152 | /////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | @Override |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 155 | public void insertReorderBarrier() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 156 | nInsertReorderBarrier(mNativeCanvasWrapper, true); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void insertInorderBarrier() { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 161 | nInsertReorderBarrier(mNativeCanvasWrapper, false); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | private static native void nInsertReorderBarrier(long renderer, boolean enableReorder); |
| 165 | |
Romain Guy | 530041d | 2012-01-25 18:56:29 -0800 | [diff] [blame] | 166 | /////////////////////////////////////////////////////////////////////////// |
| 167 | // Functor |
| 168 | /////////////////////////////////////////////////////////////////////////// |
| 169 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Calls the function specified with the drawGLFunction function pointer. This is |
| 172 | * functionality used by webkit for calling into their renderer from our display lists. |
| 173 | * This function may return true if an invalidation is needed after the call. |
| 174 | * |
| 175 | * @param drawGLFunction A native function pointer |
| 176 | */ |
Narayan Kamath | 57616d3e | 2014-11-21 11:57:07 +0000 | [diff] [blame] | 177 | public void callDrawGLFunction2(long drawGLFunction) { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 178 | nCallDrawGLFunction(mNativeCanvasWrapper, drawGLFunction); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Tom Hudson | 107843d | 2014-09-08 11:26:26 -0400 | [diff] [blame] | 181 | private static native void nCallDrawGLFunction(long renderer, long drawGLFunction); |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 182 | |
Romain Guy | bdf7609 | 2011-07-18 15:00:43 -0700 | [diff] [blame] | 183 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 184 | // Display list |
| 185 | /////////////////////////////////////////////////////////////////////////// |
| 186 | |
John Reck | 44fd8d2 | 2014-02-26 11:00:11 -0800 | [diff] [blame] | 187 | protected static native long nFinishRecording(long renderer); |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 188 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Draws the specified display list onto this canvas. The display list can only |
| 191 | * be drawn if {@link android.view.RenderNode#isValid()} returns true. |
| 192 | * |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 193 | * @param renderNode The RenderNode to draw. |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 194 | */ |
| 195 | public void drawRenderNode(RenderNode renderNode) { |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 196 | nDrawRenderNode(mNativeCanvasWrapper, renderNode.getNativeDisplayList()); |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Chris Craik | 956f340 | 2015-04-27 16:41:00 -0700 | [diff] [blame] | 199 | private static native void nDrawRenderNode(long renderer, long renderNode); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 200 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 201 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 202 | // Hardware layer |
| 203 | /////////////////////////////////////////////////////////////////////////// |
Raph Levien | 051910b | 2014-06-15 18:25:29 -0700 | [diff] [blame] | 204 | |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Draws the specified layer onto this canvas. |
| 207 | * |
| 208 | * @param layer The layer to composite on this canvas |
| 209 | * @param x The left coordinate of the layer |
| 210 | * @param y The top coordinate of the layer |
| 211 | * @param paint The paint used to draw the layer |
| 212 | */ |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 213 | void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) { |
Chris Craik | a4e16c5 | 2013-03-22 10:00:48 -0700 | [diff] [blame] | 214 | layer.setLayerPaint(paint); |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 215 | nDrawLayer(mNativeCanvasWrapper, layer.getLayerHandle(), x, y); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 218 | private static native void nDrawLayer(long renderer, long layer, float x, float y); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 219 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 220 | /////////////////////////////////////////////////////////////////////////// |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 221 | // Drawing |
| 222 | /////////////////////////////////////////////////////////////////////////// |
| 223 | |
John Reck | 773bbe0 | 2015-08-17 15:18:29 -0700 | [diff] [blame^] | 224 | // TODO: move to Canvas.java |
| 225 | @Override |
| 226 | public void drawPatch(NinePatch patch, Rect dst, Paint paint) { |
| 227 | Bitmap bitmap = patch.getBitmap(); |
| 228 | throwIfCannotDraw(bitmap); |
| 229 | final long nativePaint = paint == null ? 0 : paint.getNativeInstance(); |
| 230 | nDrawPatch(mNativeCanvasWrapper, bitmap, patch.mNativeChunk, |
| 231 | dst.left, dst.top, dst.right, dst.bottom, nativePaint); |
| 232 | } |
| 233 | |
| 234 | // TODO: move to Canvas.java |
| 235 | @Override |
| 236 | public void drawPatch(NinePatch patch, RectF dst, Paint paint) { |
| 237 | Bitmap bitmap = patch.getBitmap(); |
| 238 | throwIfCannotDraw(bitmap); |
| 239 | final long nativePaint = paint == null ? 0 : paint.getNativeInstance(); |
| 240 | nDrawPatch(mNativeCanvasWrapper, bitmap, patch.mNativeChunk, |
| 241 | dst.left, dst.top, dst.right, dst.bottom, nativePaint); |
| 242 | } |
| 243 | |
| 244 | private static native void nDrawPatch(long renderer, Bitmap bitmap, long chunk, |
| 245 | float left, float top, float right, float bottom, long paint); |
| 246 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 247 | public void drawCircle(CanvasProperty<Float> cx, CanvasProperty<Float> cy, |
| 248 | CanvasProperty<Float> radius, CanvasProperty<Paint> paint) { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 249 | nDrawCircle(mNativeCanvasWrapper, cx.getNativeContainer(), cy.getNativeContainer(), |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 250 | radius.getNativeContainer(), paint.getNativeContainer()); |
| 251 | } |
| 252 | |
| 253 | private static native void nDrawCircle(long renderer, long propCx, |
| 254 | long propCy, long propRadius, long propPaint); |
| 255 | |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 256 | public void drawRoundRect(CanvasProperty<Float> left, CanvasProperty<Float> top, |
| 257 | CanvasProperty<Float> right, CanvasProperty<Float> bottom, CanvasProperty<Float> rx, |
| 258 | CanvasProperty<Float> ry, CanvasProperty<Paint> paint) { |
Tom Hudson | 8dfaa49 | 2014-12-09 15:03:44 -0500 | [diff] [blame] | 259 | nDrawRoundRect(mNativeCanvasWrapper, left.getNativeContainer(), top.getNativeContainer(), |
Jorim Jaggi | 072707d | 2014-09-15 17:20:08 +0200 | [diff] [blame] | 260 | right.getNativeContainer(), bottom.getNativeContainer(), |
| 261 | rx.getNativeContainer(), ry.getNativeContainer(), |
| 262 | paint.getNativeContainer()); |
| 263 | } |
| 264 | |
| 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 | } |