blob: b35c92612a42895d495dd3d8bb0bb36561da8b90 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
2 * Copyright (C) 2015 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#include "RecordingCanvas.h"
18
Chris Craikd2dfd8f2015-12-16 14:27:20 -080019#include "DeferredLayerUpdater.h"
Chris Craikb565df12015-10-05 13:00:52 -070020#include "RecordedOp.h"
21#include "RenderNode.h"
Doris Liu766431a2016-02-04 22:17:11 +000022#include "VectorDrawable.h"
Chris Craikb565df12015-10-05 13:00:52 -070023
24namespace android {
25namespace uirenderer {
26
27RecordingCanvas::RecordingCanvas(size_t width, size_t height)
28 : mState(*this)
29 , mResourceCache(ResourceCache::getInstance()) {
Derek Sollenberger6f485562015-07-30 10:00:39 -040030 resetRecording(width, height);
Chris Craikb565df12015-10-05 13:00:52 -070031}
32
33RecordingCanvas::~RecordingCanvas() {
Chris Craik003cc3d2015-10-16 10:24:55 -070034 LOG_ALWAYS_FATAL_IF(mDisplayList,
Chris Craikb565df12015-10-05 13:00:52 -070035 "Destroyed a RecordingCanvas during a record!");
36}
37
Derek Sollenberger6f485562015-07-30 10:00:39 -040038void RecordingCanvas::resetRecording(int width, int height) {
Chris Craik003cc3d2015-10-16 10:24:55 -070039 LOG_ALWAYS_FATAL_IF(mDisplayList,
Chris Craikb565df12015-10-05 13:00:52 -070040 "prepareDirty called a second time during a recording!");
Chris Craik003cc3d2015-10-16 10:24:55 -070041 mDisplayList = new DisplayList();
Chris Craikb565df12015-10-05 13:00:52 -070042
Chris Craike4db79d2015-12-22 16:32:23 -080043 mState.initializeRecordingSaveStack(width, height);
Chris Craikb565df12015-10-05 13:00:52 -070044
Chris Craik161f54b2015-11-05 11:08:52 -080045 mDeferredBarrierType = DeferredBarrierType::InOrder;
Chris Craikb565df12015-10-05 13:00:52 -070046 mState.setDirtyClip(false);
Chris Craikb565df12015-10-05 13:00:52 -070047}
48
Chris Craik003cc3d2015-10-16 10:24:55 -070049DisplayList* RecordingCanvas::finishRecording() {
Chris Craikb87eadd2016-01-06 09:16:05 -080050 restoreToCount(1);
Chris Craikb565df12015-10-05 13:00:52 -070051 mPaintMap.clear();
52 mRegionMap.clear();
53 mPathMap.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -070054 DisplayList* displayList = mDisplayList;
55 mDisplayList = nullptr;
Chris Craikb565df12015-10-05 13:00:52 -070056 mSkiaCanvasProxy.reset(nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070057 return displayList;
Chris Craikb565df12015-10-05 13:00:52 -070058}
59
Chris Craikd6456402016-04-11 12:24:23 -070060void RecordingCanvas::insertReorderBarrier(bool enableReorder) {
61 if (enableReorder) {
62 mDeferredBarrierType = DeferredBarrierType::OutOfOrder;
63 mDeferredBarrierClip = getRecordedClip();
64 } else {
65 mDeferredBarrierType = DeferredBarrierType::InOrder;
66 mDeferredBarrierClip = nullptr;
67 }
68}
69
Chris Craikb565df12015-10-05 13:00:52 -070070SkCanvas* RecordingCanvas::asSkCanvas() {
Chris Craik003cc3d2015-10-16 10:24:55 -070071 LOG_ALWAYS_FATAL_IF(!mDisplayList,
Chris Craikb565df12015-10-05 13:00:52 -070072 "attempting to get an SkCanvas when we are not recording!");
73 if (!mSkiaCanvasProxy) {
74 mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this));
75 }
76
77 // SkCanvas instances default to identity transform, but should inherit
78 // the state of this Canvas; if this code was in the SkiaCanvasProxy
79 // constructor, we couldn't cache mSkiaCanvasProxy.
80 SkMatrix parentTransform;
81 getMatrix(&parentTransform);
82 mSkiaCanvasProxy.get()->setMatrix(parentTransform);
83
84 return mSkiaCanvasProxy.get();
85}
86
87// ----------------------------------------------------------------------------
Chris Craik6fe991e52015-10-20 09:39:42 -070088// CanvasStateClient implementation
89// ----------------------------------------------------------------------------
90
91void RecordingCanvas::onViewportInitialized() {
Chris Craik6fe991e52015-10-20 09:39:42 -070092}
93
94void RecordingCanvas::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
95 if (removed.flags & Snapshot::kFlagIsFboLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080096 addOp(alloc().create_trivial<EndLayerOp>());
Chris Craikb87eadd2016-01-06 09:16:05 -080097 } else if (removed.flags & Snapshot::kFlagIsLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080098 addOp(alloc().create_trivial<EndUnclippedLayerOp>());
Chris Craik6fe991e52015-10-20 09:39:42 -070099 }
100}
101
102// ----------------------------------------------------------------------------
Chris Craikb565df12015-10-05 13:00:52 -0700103// android/graphics/Canvas state operations
104// ----------------------------------------------------------------------------
105// Save (layer)
Florin Malitaeecff562015-12-21 10:43:01 -0500106int RecordingCanvas::save(SaveFlags::Flags flags) {
Chris Craikb565df12015-10-05 13:00:52 -0700107 return mState.save((int) flags);
108}
109
110void RecordingCanvas::RecordingCanvas::restore() {
Chris Craikb565df12015-10-05 13:00:52 -0700111 mState.restore();
112}
113
114void RecordingCanvas::restoreToCount(int saveCount) {
Chris Craikb565df12015-10-05 13:00:52 -0700115 mState.restoreToCount(saveCount);
116}
117
Chris Craikb87eadd2016-01-06 09:16:05 -0800118int RecordingCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500119 const SkPaint* paint, SaveFlags::Flags flags) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700120 // force matrix/clip isolation for layer
Florin Malitaeecff562015-12-21 10:43:01 -0500121 flags |= SaveFlags::MatrixClip;
122 bool clippedLayer = flags & SaveFlags::ClipToLayer;
Chris Craik6fe991e52015-10-20 09:39:42 -0700123
124 const Snapshot& previous = *mState.currentSnapshot();
125
126 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
127 // operations will be able to store and restore the current clip and transform info, and
128 // quick rejection will be correct (for display lists)
129
Chris Craikb87eadd2016-01-06 09:16:05 -0800130 const Rect unmappedBounds(left, top, right, bottom);
Chris Craik6fe991e52015-10-20 09:39:42 -0700131
132 // determine clipped bounds relative to previous viewport.
Chris Craikb87eadd2016-01-06 09:16:05 -0800133 Rect visibleBounds = unmappedBounds;
Chris Craik6fe991e52015-10-20 09:39:42 -0700134 previous.transform->mapRect(visibleBounds);
135
Chris Craikb87eadd2016-01-06 09:16:05 -0800136 if (CC_UNLIKELY(!clippedLayer
137 && previous.transform->rectToRect()
138 && visibleBounds.contains(previous.getRenderTargetClip()))) {
139 // unlikely case where an unclipped savelayer is recorded with a clip it can use,
140 // as none of its unaffected/unclipped area is visible
141 clippedLayer = true;
Florin Malitaeecff562015-12-21 10:43:01 -0500142 flags |= SaveFlags::ClipToLayer;
Chris Craikb87eadd2016-01-06 09:16:05 -0800143 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700144
145 visibleBounds.doIntersect(previous.getRenderTargetClip());
146 visibleBounds.snapToPixelBoundaries();
Chris Craikb87eadd2016-01-06 09:16:05 -0800147 visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight()));
Chris Craik6fe991e52015-10-20 09:39:42 -0700148
149 // Map visible bounds back to layer space, and intersect with parameter bounds
150 Rect layerBounds = visibleBounds;
151 Matrix4 inverse;
152 inverse.loadInverse(*previous.transform);
153 inverse.mapRect(layerBounds);
Chris Craikb87eadd2016-01-06 09:16:05 -0800154 layerBounds.doIntersect(unmappedBounds);
Chris Craik6fe991e52015-10-20 09:39:42 -0700155
156 int saveValue = mState.save((int) flags);
157 Snapshot& snapshot = *mState.writableSnapshot();
158
Chris Craikb87eadd2016-01-06 09:16:05 -0800159 // layerBounds is in original bounds space, but clipped by current recording clip
160 if (layerBounds.isEmpty() || unmappedBounds.isEmpty()) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700161 // Don't bother recording layer, since it's been rejected
Chris Craikb87eadd2016-01-06 09:16:05 -0800162 if (CC_LIKELY(clippedLayer)) {
163 snapshot.resetClip(0, 0, 0, 0);
164 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700165 return saveValue;
166 }
167
Chris Craikb87eadd2016-01-06 09:16:05 -0800168 if (CC_LIKELY(clippedLayer)) {
169 auto previousClip = getRecordedClip(); // note: done before new snapshot's clip has changed
Chris Craike4db79d2015-12-22 16:32:23 -0800170
Chris Craikb87eadd2016-01-06 09:16:05 -0800171 snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
172 snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
173 snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
Chris Craik6fe991e52015-10-20 09:39:42 -0700174
Chris Craikb87eadd2016-01-06 09:16:05 -0800175 Rect clip = layerBounds;
176 clip.translate(-unmappedBounds.left, -unmappedBounds.top);
177 snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
178 snapshot.roundRectClipState = nullptr;
Chris Craik6fe991e52015-10-20 09:39:42 -0700179
John Reck7df9ff22016-02-10 16:08:08 -0800180 addOp(alloc().create_trivial<BeginLayerOp>(
Chris Craikb87eadd2016-01-06 09:16:05 -0800181 unmappedBounds,
182 *previous.transform, // transform to *draw* with
183 previousClip, // clip to *draw* with
184 refPaint(paint)));
185 } else {
186 snapshot.flags |= Snapshot::kFlagIsLayer;
187
John Reck7df9ff22016-02-10 16:08:08 -0800188 addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
Chris Craikb87eadd2016-01-06 09:16:05 -0800189 unmappedBounds,
190 *mState.currentSnapshot()->transform,
191 getRecordedClip(),
192 refPaint(paint)));
193 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700194
195 return saveValue;
Chris Craikb565df12015-10-05 13:00:52 -0700196}
197
198// Matrix
199void RecordingCanvas::rotate(float degrees) {
200 if (degrees == 0) return;
201
202 mState.rotate(degrees);
203}
204
205void RecordingCanvas::scale(float sx, float sy) {
206 if (sx == 1 && sy == 1) return;
207
208 mState.scale(sx, sy);
209}
210
211void RecordingCanvas::skew(float sx, float sy) {
212 mState.skew(sx, sy);
213}
214
215void RecordingCanvas::translate(float dx, float dy) {
216 if (dx == 0 && dy == 0) return;
217
218 mState.translate(dx, dy, 0);
219}
220
221// Clip
222bool RecordingCanvas::getClipBounds(SkRect* outRect) const {
Chris Craike29ce6f2015-12-10 16:25:13 -0800223 *outRect = mState.getLocalClipBounds().toSkRect();
Chris Craikb565df12015-10-05 13:00:52 -0700224 return !(outRect->isEmpty());
225}
226bool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
227 return mState.quickRejectConservative(left, top, right, bottom);
228}
229bool RecordingCanvas::quickRejectPath(const SkPath& path) const {
230 SkRect bounds = path.getBounds();
231 return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
232}
233bool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
234 return mState.clipRect(left, top, right, bottom, op);
235}
236bool RecordingCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
237 return mState.clipPath(path, op);
238}
239bool RecordingCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
240 return mState.clipRegion(region, op);
241}
242
243// ----------------------------------------------------------------------------
244// android/graphics/Canvas draw operations
245// ----------------------------------------------------------------------------
246void RecordingCanvas::drawColor(int color, SkXfermode::Mode mode) {
Chris Craika2048482016-03-25 14:17:49 -0700247 addOp(alloc().create_trivial<ColorOp>(
248 getRecordedClip(),
249 color,
250 mode));
Chris Craikb565df12015-10-05 13:00:52 -0700251}
252
253void RecordingCanvas::drawPaint(const SkPaint& paint) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700254 SkRect bounds;
255 if (getClipBounds(&bounds)) {
256 drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint);
257 }
Chris Craikb565df12015-10-05 13:00:52 -0700258}
259
Chris Craik386aa032015-12-07 17:08:25 -0800260static Rect calcBoundsOfPoints(const float* points, int floatCount) {
261 Rect unmappedBounds(points[0], points[1], points[0], points[1]);
262 for (int i = 2; i < floatCount; i += 2) {
263 unmappedBounds.expandToCover(points[i], points[i + 1]);
264 }
265 return unmappedBounds;
266}
267
Chris Craikb565df12015-10-05 13:00:52 -0700268// Geometry
Chris Craik386aa032015-12-07 17:08:25 -0800269void RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
270 if (floatCount < 2) return;
271 floatCount &= ~0x1; // round down to nearest two
272
John Reck7df9ff22016-02-10 16:08:08 -0800273 addOp(alloc().create_trivial<PointsOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800274 calcBoundsOfPoints(points, floatCount),
275 *mState.currentSnapshot()->transform,
Chris Craike4db79d2015-12-22 16:32:23 -0800276 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800277 refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700278}
Chris Craika1717272015-11-19 13:02:43 -0800279
280void RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
281 if (floatCount < 4) return;
282 floatCount &= ~0x3; // round down to nearest four
283
John Reck7df9ff22016-02-10 16:08:08 -0800284 addOp(alloc().create_trivial<LinesOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800285 calcBoundsOfPoints(points, floatCount),
Chris Craika1717272015-11-19 13:02:43 -0800286 *mState.currentSnapshot()->transform,
Chris Craike4db79d2015-12-22 16:32:23 -0800287 getRecordedClip(),
Chris Craika1717272015-11-19 13:02:43 -0800288 refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700289}
Chris Craika1717272015-11-19 13:02:43 -0800290
Chris Craikb565df12015-10-05 13:00:52 -0700291void RecordingCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800292 addOp(alloc().create_trivial<RectOp>(
Chris Craikb565df12015-10-05 13:00:52 -0700293 Rect(left, top, right, bottom),
294 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800295 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700296 refPaint(&paint)));
297}
298
299void RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) {
300 if (rects == nullptr) return;
301
Chris Craik7a896002016-02-19 15:51:02 -0800302 Vertex* rectData = (Vertex*) mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount);
Chris Craikb565df12015-10-05 13:00:52 -0700303 Vertex* vertex = rectData;
304
305 float left = FLT_MAX;
306 float top = FLT_MAX;
307 float right = FLT_MIN;
308 float bottom = FLT_MIN;
309 for (int index = 0; index < vertexCount; index += 4) {
310 float l = rects[index + 0];
311 float t = rects[index + 1];
312 float r = rects[index + 2];
313 float b = rects[index + 3];
314
315 Vertex::set(vertex++, l, t);
316 Vertex::set(vertex++, r, t);
317 Vertex::set(vertex++, l, b);
318 Vertex::set(vertex++, r, b);
319
320 left = std::min(left, l);
321 top = std::min(top, t);
322 right = std::max(right, r);
323 bottom = std::max(bottom, b);
324 }
John Reck7df9ff22016-02-10 16:08:08 -0800325 addOp(alloc().create_trivial<SimpleRectsOp>(
Chris Craikb565df12015-10-05 13:00:52 -0700326 Rect(left, top, right, bottom),
327 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800328 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700329 refPaint(paint), rectData, vertexCount));
330}
331
332void RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
333 if (paint.getStyle() == SkPaint::kFill_Style
334 && (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
335 int count = 0;
336 Vector<float> rects;
337 SkRegion::Iterator it(region);
338 while (!it.done()) {
339 const SkIRect& r = it.rect();
340 rects.push(r.fLeft);
341 rects.push(r.fTop);
342 rects.push(r.fRight);
343 rects.push(r.fBottom);
344 count += 4;
345 it.next();
346 }
347 drawSimpleRects(rects.array(), count, &paint);
348 } else {
349 SkRegion::Iterator it(region);
350 while (!it.done()) {
351 const SkIRect& r = it.rect();
352 drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
353 it.next();
354 }
355 }
356}
357void RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom,
358 float rx, float ry, const SkPaint& paint) {
Chris Craik2dbb4c42016-03-11 18:58:37 -0800359 if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) {
360 addOp(alloc().create_trivial<RoundRectOp>(
361 Rect(left, top, right, bottom),
362 *(mState.currentSnapshot()->transform),
363 getRecordedClip(),
364 refPaint(&paint), rx, ry));
365 } else {
366 drawRect(left, top, right, bottom, paint);
367 }
Chris Craikb565df12015-10-05 13:00:52 -0700368}
Chris Craik386aa032015-12-07 17:08:25 -0800369
Chris Craik268a9c02015-12-09 18:05:12 -0800370void RecordingCanvas::drawRoundRect(
371 CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
372 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
373 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
374 CanvasPropertyPaint* paint) {
375 mDisplayList->ref(left);
376 mDisplayList->ref(top);
377 mDisplayList->ref(right);
378 mDisplayList->ref(bottom);
379 mDisplayList->ref(rx);
380 mDisplayList->ref(ry);
381 mDisplayList->ref(paint);
382 refBitmapsInShader(paint->value.getShader());
John Reck7df9ff22016-02-10 16:08:08 -0800383 addOp(alloc().create_trivial<RoundRectPropsOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800384 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800385 getRecordedClip(),
Chris Craik268a9c02015-12-09 18:05:12 -0800386 &paint->value,
387 &left->value, &top->value, &right->value, &bottom->value,
388 &rx->value, &ry->value));
389}
390
Chris Craikb565df12015-10-05 13:00:52 -0700391void RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Chris Craik268a9c02015-12-09 18:05:12 -0800392 // TODO: move to Canvas.h
Chris Craik386aa032015-12-07 17:08:25 -0800393 if (radius <= 0) return;
394 drawOval(x - radius, y - radius, x + radius, y + radius, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700395}
Chris Craik386aa032015-12-07 17:08:25 -0800396
Chris Craik268a9c02015-12-09 18:05:12 -0800397void RecordingCanvas::drawCircle(
398 CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
399 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
400 mDisplayList->ref(x);
401 mDisplayList->ref(y);
402 mDisplayList->ref(radius);
403 mDisplayList->ref(paint);
404 refBitmapsInShader(paint->value.getShader());
John Reck7df9ff22016-02-10 16:08:08 -0800405 addOp(alloc().create_trivial<CirclePropsOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800406 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800407 getRecordedClip(),
Chris Craik268a9c02015-12-09 18:05:12 -0800408 &paint->value,
409 &x->value, &y->value, &radius->value));
410}
411
Chris Craikb565df12015-10-05 13:00:52 -0700412void RecordingCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800413 addOp(alloc().create_trivial<OvalOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800414 Rect(left, top, right, bottom),
415 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800416 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800417 refPaint(&paint)));
Chris Craikb565df12015-10-05 13:00:52 -0700418}
Chris Craik386aa032015-12-07 17:08:25 -0800419
Chris Craikb565df12015-10-05 13:00:52 -0700420void RecordingCanvas::drawArc(float left, float top, float right, float bottom,
Chris Craik386aa032015-12-07 17:08:25 -0800421 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Chris Craikcaa24182016-02-19 15:20:35 -0800422 if (fabs(sweepAngle) >= 360.0f) {
423 drawOval(left, top, right, bottom, paint);
424 } else {
425 addOp(alloc().create_trivial<ArcOp>(
426 Rect(left, top, right, bottom),
427 *(mState.currentSnapshot()->transform),
428 getRecordedClip(),
429 refPaint(&paint),
430 startAngle, sweepAngle, useCenter));
431 }
Chris Craikb565df12015-10-05 13:00:52 -0700432}
Chris Craik386aa032015-12-07 17:08:25 -0800433
Chris Craikb565df12015-10-05 13:00:52 -0700434void RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800435 addOp(alloc().create_trivial<PathOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800436 Rect(path.getBounds()),
437 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800438 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800439 refPaint(&paint), refPath(&path)));
Chris Craikb565df12015-10-05 13:00:52 -0700440}
441
Doris Liu766431a2016-02-04 22:17:11 +0000442void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
443 mDisplayList->ref(tree);
Doris Liu718cd3e2016-05-17 16:50:31 -0700444 mDisplayList->vectorDrawables.push_back(tree);
John Reck7df9ff22016-02-10 16:08:08 -0800445 addOp(alloc().create_trivial<VectorDrawableOp>(
Doris Liu766431a2016-02-04 22:17:11 +0000446 tree,
Doris Liu1d8e1942016-03-02 15:16:28 -0800447 Rect(tree->stagingProperties()->getBounds()),
Doris Liu766431a2016-02-04 22:17:11 +0000448 *(mState.currentSnapshot()->transform),
449 getRecordedClip()));
450}
451
Chris Craikb565df12015-10-05 13:00:52 -0700452// Bitmap-based
453void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500454 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700455 translate(left, top);
456 drawBitmap(&bitmap, paint);
457 restore();
458}
459
460void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
461 const SkPaint* paint) {
462 if (matrix.isIdentity()) {
463 drawBitmap(&bitmap, paint);
464 } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask))
465 && MathUtils::isPositive(matrix.getScaleX())
466 && MathUtils::isPositive(matrix.getScaleY())) {
467 // SkMatrix::isScaleTranslate() not available in L
468 SkRect src;
469 SkRect dst;
470 bitmap.getBounds(&src);
471 matrix.mapRect(&dst, src);
472 drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
473 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
474 } else {
Florin Malitaeecff562015-12-21 10:43:01 -0500475 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700476 concat(matrix);
477 drawBitmap(&bitmap, paint);
478 restore();
479 }
480}
Chris Craik386aa032015-12-07 17:08:25 -0800481
Chris Craikb565df12015-10-05 13:00:52 -0700482void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
483 float srcRight, float srcBottom, float dstLeft, float dstTop,
484 float dstRight, float dstBottom, const SkPaint* paint) {
485 if (srcLeft == 0 && srcTop == 0
486 && srcRight == bitmap.width()
487 && srcBottom == bitmap.height()
488 && (srcBottom - srcTop == dstBottom - dstTop)
489 && (srcRight - srcLeft == dstRight - dstLeft)) {
490 // transform simple rect to rect drawing case into position bitmap ops, since they merge
Florin Malitaeecff562015-12-21 10:43:01 -0500491 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700492 translate(dstLeft, dstTop);
493 drawBitmap(&bitmap, paint);
494 restore();
495 } else {
John Reck7df9ff22016-02-10 16:08:08 -0800496 addOp(alloc().create_trivial<BitmapRectOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800497 Rect(dstLeft, dstTop, dstRight, dstBottom),
498 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800499 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800500 refPaint(paint), refBitmap(bitmap),
501 Rect(srcLeft, srcTop, srcRight, srcBottom)));
Chris Craikb565df12015-10-05 13:00:52 -0700502 }
503}
Chris Craik386aa032015-12-07 17:08:25 -0800504
Chris Craikb565df12015-10-05 13:00:52 -0700505void RecordingCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
506 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800507 int vertexCount = (meshWidth + 1) * (meshHeight + 1);
John Reck7df9ff22016-02-10 16:08:08 -0800508 addOp(alloc().create_trivial<BitmapMeshOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800509 calcBoundsOfPoints(vertices, vertexCount * 2),
510 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800511 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800512 refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight,
513 refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex
514 refBuffer<int>(colors, vertexCount))); // 1 color per vertex
Chris Craikb565df12015-10-05 13:00:52 -0700515}
Chris Craik386aa032015-12-07 17:08:25 -0800516
Chris Craikf09ff5a2015-12-08 17:21:58 -0800517void RecordingCanvas::drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& patch,
Chris Craikb565df12015-10-05 13:00:52 -0700518 float dstLeft, float dstTop, float dstRight, float dstBottom,
519 const SkPaint* paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800520 addOp(alloc().create_trivial<PatchOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800521 Rect(dstLeft, dstTop, dstRight, dstBottom),
522 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800523 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800524 refPaint(paint), refBitmap(bitmap), refPatch(&patch)));
Chris Craikb565df12015-10-05 13:00:52 -0700525}
526
527// Text
sergeyvdccca442016-03-21 15:38:21 -0700528void RecordingCanvas::drawGlyphs(const uint16_t* glyphs, const float* positions, int glyphCount,
Chris Craikb565df12015-10-05 13:00:52 -0700529 const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
530 float boundsRight, float boundsBottom, float totalAdvance) {
Chris Craika1717272015-11-19 13:02:43 -0800531 if (!glyphs || !positions || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
532 glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
533 positions = refBuffer<float>(positions, glyphCount * 2);
534
Chris Craik15c3f192015-12-03 12:16:56 -0800535 // TODO: either must account for text shadow in bounds, or record separate ops for text shadows
John Reck7df9ff22016-02-10 16:08:08 -0800536 addOp(alloc().create_trivial<TextOp>(
Chris Craika1717272015-11-19 13:02:43 -0800537 Rect(boundsLeft, boundsTop, boundsRight, boundsBottom),
538 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800539 getRecordedClip(),
Chris Craika1717272015-11-19 13:02:43 -0800540 refPaint(&paint), glyphs, positions, glyphCount, x, y));
541 drawTextDecorations(x, y, totalAdvance, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700542}
Chris Craika1717272015-11-19 13:02:43 -0800543
sergeyvdccca442016-03-21 15:38:21 -0700544void RecordingCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int glyphCount, const SkPath& path,
Chris Craikb565df12015-10-05 13:00:52 -0700545 float hOffset, float vOffset, const SkPaint& paint) {
Chris Craikd7448e62015-12-15 10:34:36 -0800546 if (!glyphs || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
547 glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
John Reck7df9ff22016-02-10 16:08:08 -0800548 addOp(alloc().create_trivial<TextOnPathOp>(
Chris Craikd7448e62015-12-15 10:34:36 -0800549 *(mState.currentSnapshot()->transform),
Chris Craik4c3980b2016-03-15 14:20:18 -0700550 getRecordedClip(),
Chris Craikd7448e62015-12-15 10:34:36 -0800551 refPaint(&paint), glyphs, glyphCount, refPath(&path), hOffset, vOffset));
Chris Craikb565df12015-10-05 13:00:52 -0700552}
553
554void RecordingCanvas::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800555 addOp(alloc().create_trivial<BitmapOp>(
Chris Craik5430ab22015-12-10 16:28:16 -0800556 Rect(bitmap->width(), bitmap->height()),
Chris Craikb565df12015-10-05 13:00:52 -0700557 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800558 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700559 refPaint(paint), refBitmap(*bitmap)));
560}
Chris Craike29ce6f2015-12-10 16:25:13 -0800561
Chris Craikb565df12015-10-05 13:00:52 -0700562void RecordingCanvas::drawRenderNode(RenderNode* renderNode) {
Chris Craik54fa17f2015-11-25 14:14:53 -0800563 auto&& stagingProps = renderNode->stagingProperties();
John Reck7df9ff22016-02-10 16:08:08 -0800564 RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>(
Chris Craik54fa17f2015-11-25 14:14:53 -0800565 Rect(stagingProps.getWidth(), stagingProps.getHeight()),
Chris Craikb565df12015-10-05 13:00:52 -0700566 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800567 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700568 renderNode);
569 int opIndex = addOp(op);
Chris Craik1367d252016-03-10 15:43:13 -0800570 if (CC_LIKELY(opIndex >= 0)) {
571 int childIndex = mDisplayList->addChild(op);
Chris Craikb565df12015-10-05 13:00:52 -0700572
Chris Craik1367d252016-03-10 15:43:13 -0800573 // update the chunk's child indices
574 DisplayList::Chunk& chunk = mDisplayList->chunks.back();
575 chunk.endChildIndex = childIndex + 1;
Chris Craikb565df12015-10-05 13:00:52 -0700576
Chris Craik1367d252016-03-10 15:43:13 -0800577 if (renderNode->stagingProperties().isProjectionReceiver()) {
578 // use staging property, since recording on UI thread
579 mDisplayList->projectionReceiveIndex = opIndex;
580 }
Chris Craikb565df12015-10-05 13:00:52 -0700581 }
582}
583
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800584void RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) {
585 // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics.
586 mDisplayList->ref(layerHandle);
587
John Reck417ed6d2016-03-22 16:01:08 -0700588 // Note that the backing layer has *not* yet been updated, so don't trust
589 // its width, height, transform, etc...!
John Reck7df9ff22016-02-10 16:08:08 -0800590 addOp(alloc().create_trivial<TextureLayerOp>(
John Reck417ed6d2016-03-22 16:01:08 -0700591 Rect(layerHandle->getWidth(), layerHandle->getHeight()),
Chris Craikaafb01d2016-03-25 18:34:11 -0700592 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800593 getRecordedClip(),
John Reck417ed6d2016-03-22 16:01:08 -0700594 layerHandle->backingLayer()));
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800595}
596
John Reckcd1c3eb2016-04-14 10:38:54 -0700597void RecordingCanvas::callDrawGLFunction(Functor* functor,
598 GlFunctorLifecycleListener* listener) {
599 mDisplayList->functors.push_back({functor, listener});
600 mDisplayList->ref(listener);
John Reck7df9ff22016-02-10 16:08:08 -0800601 addOp(alloc().create_trivial<FunctorOp>(
Chris Craike29ce6f2015-12-10 16:25:13 -0800602 *(mState.currentSnapshot()->transform),
Chris Craik4c3980b2016-03-15 14:20:18 -0700603 getRecordedClip(),
Chris Craike29ce6f2015-12-10 16:25:13 -0800604 functor));
605}
606
Chris Craikb565df12015-10-05 13:00:52 -0700607size_t RecordingCanvas::addOp(RecordedOp* op) {
Chris Craik261725f2016-02-29 12:52:33 -0800608 // skip op with empty clip
609 if (op->localClip && op->localClip->rect.isEmpty()) {
610 // NOTE: this rejection happens after op construction/content ref-ing, so content ref'd
611 // and held by renderthread isn't affected by clip rejection.
612 // Could rewind alloc here if desired, but callers would have to not touch op afterwards.
613 return -1;
614 }
615
Chris Craik003cc3d2015-10-16 10:24:55 -0700616 int insertIndex = mDisplayList->ops.size();
617 mDisplayList->ops.push_back(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800618 if (mDeferredBarrierType != DeferredBarrierType::None) {
Chris Craikb565df12015-10-05 13:00:52 -0700619 // op is first in new chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700620 mDisplayList->chunks.emplace_back();
621 DisplayList::Chunk& newChunk = mDisplayList->chunks.back();
Chris Craikb565df12015-10-05 13:00:52 -0700622 newChunk.beginOpIndex = insertIndex;
623 newChunk.endOpIndex = insertIndex + 1;
Chris Craik161f54b2015-11-05 11:08:52 -0800624 newChunk.reorderChildren = (mDeferredBarrierType == DeferredBarrierType::OutOfOrder);
Chris Craikd6456402016-04-11 12:24:23 -0700625 newChunk.reorderClip = mDeferredBarrierClip;
Chris Craikb565df12015-10-05 13:00:52 -0700626
Chris Craikb36af872015-10-16 14:23:12 -0700627 int nextChildIndex = mDisplayList->children.size();
Chris Craikb565df12015-10-05 13:00:52 -0700628 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex;
Chris Craik161f54b2015-11-05 11:08:52 -0800629 mDeferredBarrierType = DeferredBarrierType::None;
Chris Craikb565df12015-10-05 13:00:52 -0700630 } else {
631 // standard case - append to existing chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700632 mDisplayList->chunks.back().endOpIndex = insertIndex + 1;
Chris Craikb565df12015-10-05 13:00:52 -0700633 }
634 return insertIndex;
635}
636
637void RecordingCanvas::refBitmapsInShader(const SkShader* shader) {
638 if (!shader) return;
639
640 // If this paint has an SkShader that has an SkBitmap add
641 // it to the bitmap pile
642 SkBitmap bitmap;
643 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400644 if (shader->isABitmap(&bitmap, nullptr, xy)) {
Chris Craikb565df12015-10-05 13:00:52 -0700645 refBitmap(bitmap);
646 return;
647 }
648 SkShader::ComposeRec rec;
649 if (shader->asACompose(&rec)) {
650 refBitmapsInShader(rec.fShaderA);
651 refBitmapsInShader(rec.fShaderB);
652 return;
653 }
654}
655
656}; // namespace uirenderer
657}; // namespace android