blob: 31de305d52467bc44919c4f05b0d8b76f23ba82e [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
60SkCanvas* RecordingCanvas::asSkCanvas() {
Chris Craik003cc3d2015-10-16 10:24:55 -070061 LOG_ALWAYS_FATAL_IF(!mDisplayList,
Chris Craikb565df12015-10-05 13:00:52 -070062 "attempting to get an SkCanvas when we are not recording!");
63 if (!mSkiaCanvasProxy) {
64 mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this));
65 }
66
67 // SkCanvas instances default to identity transform, but should inherit
68 // the state of this Canvas; if this code was in the SkiaCanvasProxy
69 // constructor, we couldn't cache mSkiaCanvasProxy.
70 SkMatrix parentTransform;
71 getMatrix(&parentTransform);
72 mSkiaCanvasProxy.get()->setMatrix(parentTransform);
73
74 return mSkiaCanvasProxy.get();
75}
76
77// ----------------------------------------------------------------------------
Chris Craik6fe991e52015-10-20 09:39:42 -070078// CanvasStateClient implementation
79// ----------------------------------------------------------------------------
80
81void RecordingCanvas::onViewportInitialized() {
Chris Craik6fe991e52015-10-20 09:39:42 -070082}
83
84void RecordingCanvas::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
85 if (removed.flags & Snapshot::kFlagIsFboLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080086 addOp(alloc().create_trivial<EndLayerOp>());
Chris Craikb87eadd2016-01-06 09:16:05 -080087 } else if (removed.flags & Snapshot::kFlagIsLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080088 addOp(alloc().create_trivial<EndUnclippedLayerOp>());
Chris Craik6fe991e52015-10-20 09:39:42 -070089 }
90}
91
92// ----------------------------------------------------------------------------
Chris Craikb565df12015-10-05 13:00:52 -070093// android/graphics/Canvas state operations
94// ----------------------------------------------------------------------------
95// Save (layer)
Florin Malitaeecff562015-12-21 10:43:01 -050096int RecordingCanvas::save(SaveFlags::Flags flags) {
Chris Craikb565df12015-10-05 13:00:52 -070097 return mState.save((int) flags);
98}
99
100void RecordingCanvas::RecordingCanvas::restore() {
Chris Craikb565df12015-10-05 13:00:52 -0700101 mState.restore();
102}
103
104void RecordingCanvas::restoreToCount(int saveCount) {
Chris Craikb565df12015-10-05 13:00:52 -0700105 mState.restoreToCount(saveCount);
106}
107
Chris Craikb87eadd2016-01-06 09:16:05 -0800108int RecordingCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500109 const SkPaint* paint, SaveFlags::Flags flags) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700110 // force matrix/clip isolation for layer
Florin Malitaeecff562015-12-21 10:43:01 -0500111 flags |= SaveFlags::MatrixClip;
112 bool clippedLayer = flags & SaveFlags::ClipToLayer;
Chris Craik6fe991e52015-10-20 09:39:42 -0700113
114 const Snapshot& previous = *mState.currentSnapshot();
115
116 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
117 // operations will be able to store and restore the current clip and transform info, and
118 // quick rejection will be correct (for display lists)
119
Chris Craikb87eadd2016-01-06 09:16:05 -0800120 const Rect unmappedBounds(left, top, right, bottom);
Chris Craik6fe991e52015-10-20 09:39:42 -0700121
122 // determine clipped bounds relative to previous viewport.
Chris Craikb87eadd2016-01-06 09:16:05 -0800123 Rect visibleBounds = unmappedBounds;
Chris Craik6fe991e52015-10-20 09:39:42 -0700124 previous.transform->mapRect(visibleBounds);
125
Chris Craikb87eadd2016-01-06 09:16:05 -0800126 if (CC_UNLIKELY(!clippedLayer
127 && previous.transform->rectToRect()
128 && visibleBounds.contains(previous.getRenderTargetClip()))) {
129 // unlikely case where an unclipped savelayer is recorded with a clip it can use,
130 // as none of its unaffected/unclipped area is visible
131 clippedLayer = true;
Florin Malitaeecff562015-12-21 10:43:01 -0500132 flags |= SaveFlags::ClipToLayer;
Chris Craikb87eadd2016-01-06 09:16:05 -0800133 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700134
135 visibleBounds.doIntersect(previous.getRenderTargetClip());
136 visibleBounds.snapToPixelBoundaries();
Chris Craikb87eadd2016-01-06 09:16:05 -0800137 visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight()));
Chris Craik6fe991e52015-10-20 09:39:42 -0700138
139 // Map visible bounds back to layer space, and intersect with parameter bounds
140 Rect layerBounds = visibleBounds;
141 Matrix4 inverse;
142 inverse.loadInverse(*previous.transform);
143 inverse.mapRect(layerBounds);
Chris Craikb87eadd2016-01-06 09:16:05 -0800144 layerBounds.doIntersect(unmappedBounds);
Chris Craik6fe991e52015-10-20 09:39:42 -0700145
146 int saveValue = mState.save((int) flags);
147 Snapshot& snapshot = *mState.writableSnapshot();
148
Chris Craikb87eadd2016-01-06 09:16:05 -0800149 // layerBounds is in original bounds space, but clipped by current recording clip
150 if (layerBounds.isEmpty() || unmappedBounds.isEmpty()) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700151 // Don't bother recording layer, since it's been rejected
Chris Craikb87eadd2016-01-06 09:16:05 -0800152 if (CC_LIKELY(clippedLayer)) {
153 snapshot.resetClip(0, 0, 0, 0);
154 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700155 return saveValue;
156 }
157
Chris Craikb87eadd2016-01-06 09:16:05 -0800158 if (CC_LIKELY(clippedLayer)) {
159 auto previousClip = getRecordedClip(); // note: done before new snapshot's clip has changed
Chris Craike4db79d2015-12-22 16:32:23 -0800160
Chris Craikb87eadd2016-01-06 09:16:05 -0800161 snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
162 snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
163 snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
Chris Craik6fe991e52015-10-20 09:39:42 -0700164
Chris Craikb87eadd2016-01-06 09:16:05 -0800165 Rect clip = layerBounds;
166 clip.translate(-unmappedBounds.left, -unmappedBounds.top);
167 snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
168 snapshot.roundRectClipState = nullptr;
Chris Craik6fe991e52015-10-20 09:39:42 -0700169
John Reck7df9ff22016-02-10 16:08:08 -0800170 addOp(alloc().create_trivial<BeginLayerOp>(
Chris Craikb87eadd2016-01-06 09:16:05 -0800171 unmappedBounds,
172 *previous.transform, // transform to *draw* with
173 previousClip, // clip to *draw* with
174 refPaint(paint)));
175 } else {
176 snapshot.flags |= Snapshot::kFlagIsLayer;
177
John Reck7df9ff22016-02-10 16:08:08 -0800178 addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
Chris Craikb87eadd2016-01-06 09:16:05 -0800179 unmappedBounds,
180 *mState.currentSnapshot()->transform,
181 getRecordedClip(),
182 refPaint(paint)));
183 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700184
185 return saveValue;
Chris Craikb565df12015-10-05 13:00:52 -0700186}
187
188// Matrix
189void RecordingCanvas::rotate(float degrees) {
190 if (degrees == 0) return;
191
192 mState.rotate(degrees);
193}
194
195void RecordingCanvas::scale(float sx, float sy) {
196 if (sx == 1 && sy == 1) return;
197
198 mState.scale(sx, sy);
199}
200
201void RecordingCanvas::skew(float sx, float sy) {
202 mState.skew(sx, sy);
203}
204
205void RecordingCanvas::translate(float dx, float dy) {
206 if (dx == 0 && dy == 0) return;
207
208 mState.translate(dx, dy, 0);
209}
210
211// Clip
212bool RecordingCanvas::getClipBounds(SkRect* outRect) const {
Chris Craike29ce6f2015-12-10 16:25:13 -0800213 *outRect = mState.getLocalClipBounds().toSkRect();
Chris Craikb565df12015-10-05 13:00:52 -0700214 return !(outRect->isEmpty());
215}
216bool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
217 return mState.quickRejectConservative(left, top, right, bottom);
218}
219bool RecordingCanvas::quickRejectPath(const SkPath& path) const {
220 SkRect bounds = path.getBounds();
221 return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
222}
223bool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
224 return mState.clipRect(left, top, right, bottom, op);
225}
226bool RecordingCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
227 return mState.clipPath(path, op);
228}
229bool RecordingCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
230 return mState.clipRegion(region, op);
231}
232
233// ----------------------------------------------------------------------------
234// android/graphics/Canvas draw operations
235// ----------------------------------------------------------------------------
236void RecordingCanvas::drawColor(int color, SkXfermode::Mode mode) {
237 SkPaint paint;
238 paint.setColor(color);
239 paint.setXfermodeMode(mode);
240 drawPaint(paint);
241}
242
243void RecordingCanvas::drawPaint(const SkPaint& paint) {
Chris Craik1713c772016-02-18 17:49:44 -0800244 const ClipBase* clip = getRecordedClip();
245 // if there's no current clip, draw a big rect and hope we cover the eventual clip bounds
246 Rect bounds = clip ? clip->rect : Rect(-10000, -10000, 10000, 10000);
John Reck7df9ff22016-02-10 16:08:08 -0800247 addOp(alloc().create_trivial<RectOp>(
Chris Craik1713c772016-02-18 17:49:44 -0800248 bounds,
Chris Craika1717272015-11-19 13:02:43 -0800249 Matrix4::identity(),
Chris Craik1713c772016-02-18 17:49:44 -0800250 clip,
Chris Craikb565df12015-10-05 13:00:52 -0700251 refPaint(&paint)));
252}
253
Chris Craik386aa032015-12-07 17:08:25 -0800254static Rect calcBoundsOfPoints(const float* points, int floatCount) {
255 Rect unmappedBounds(points[0], points[1], points[0], points[1]);
256 for (int i = 2; i < floatCount; i += 2) {
257 unmappedBounds.expandToCover(points[i], points[i + 1]);
258 }
259 return unmappedBounds;
260}
261
Chris Craikb565df12015-10-05 13:00:52 -0700262// Geometry
Chris Craik386aa032015-12-07 17:08:25 -0800263void RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
264 if (floatCount < 2) return;
265 floatCount &= ~0x1; // round down to nearest two
266
John Reck7df9ff22016-02-10 16:08:08 -0800267 addOp(alloc().create_trivial<PointsOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800268 calcBoundsOfPoints(points, floatCount),
269 *mState.currentSnapshot()->transform,
Chris Craike4db79d2015-12-22 16:32:23 -0800270 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800271 refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700272}
Chris Craika1717272015-11-19 13:02:43 -0800273
274void RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
275 if (floatCount < 4) return;
276 floatCount &= ~0x3; // round down to nearest four
277
John Reck7df9ff22016-02-10 16:08:08 -0800278 addOp(alloc().create_trivial<LinesOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800279 calcBoundsOfPoints(points, floatCount),
Chris Craika1717272015-11-19 13:02:43 -0800280 *mState.currentSnapshot()->transform,
Chris Craike4db79d2015-12-22 16:32:23 -0800281 getRecordedClip(),
Chris Craika1717272015-11-19 13:02:43 -0800282 refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700283}
Chris Craika1717272015-11-19 13:02:43 -0800284
Chris Craikb565df12015-10-05 13:00:52 -0700285void RecordingCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800286 addOp(alloc().create_trivial<RectOp>(
Chris Craikb565df12015-10-05 13:00:52 -0700287 Rect(left, top, right, bottom),
288 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800289 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700290 refPaint(&paint)));
291}
292
293void RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) {
294 if (rects == nullptr) return;
295
Chris Craik7a896002016-02-19 15:51:02 -0800296 Vertex* rectData = (Vertex*) mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount);
Chris Craikb565df12015-10-05 13:00:52 -0700297 Vertex* vertex = rectData;
298
299 float left = FLT_MAX;
300 float top = FLT_MAX;
301 float right = FLT_MIN;
302 float bottom = FLT_MIN;
303 for (int index = 0; index < vertexCount; index += 4) {
304 float l = rects[index + 0];
305 float t = rects[index + 1];
306 float r = rects[index + 2];
307 float b = rects[index + 3];
308
309 Vertex::set(vertex++, l, t);
310 Vertex::set(vertex++, r, t);
311 Vertex::set(vertex++, l, b);
312 Vertex::set(vertex++, r, b);
313
314 left = std::min(left, l);
315 top = std::min(top, t);
316 right = std::max(right, r);
317 bottom = std::max(bottom, b);
318 }
John Reck7df9ff22016-02-10 16:08:08 -0800319 addOp(alloc().create_trivial<SimpleRectsOp>(
Chris Craikb565df12015-10-05 13:00:52 -0700320 Rect(left, top, right, bottom),
321 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800322 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700323 refPaint(paint), rectData, vertexCount));
324}
325
326void RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
327 if (paint.getStyle() == SkPaint::kFill_Style
328 && (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
329 int count = 0;
330 Vector<float> rects;
331 SkRegion::Iterator it(region);
332 while (!it.done()) {
333 const SkIRect& r = it.rect();
334 rects.push(r.fLeft);
335 rects.push(r.fTop);
336 rects.push(r.fRight);
337 rects.push(r.fBottom);
338 count += 4;
339 it.next();
340 }
341 drawSimpleRects(rects.array(), count, &paint);
342 } else {
343 SkRegion::Iterator it(region);
344 while (!it.done()) {
345 const SkIRect& r = it.rect();
346 drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
347 it.next();
348 }
349 }
350}
351void RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom,
352 float rx, float ry, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800353 addOp(alloc().create_trivial<RoundRectOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800354 Rect(left, top, right, bottom),
355 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800356 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800357 refPaint(&paint), rx, ry));
Chris Craikb565df12015-10-05 13:00:52 -0700358}
Chris Craik386aa032015-12-07 17:08:25 -0800359
Chris Craik268a9c02015-12-09 18:05:12 -0800360void RecordingCanvas::drawRoundRect(
361 CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
362 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
363 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
364 CanvasPropertyPaint* paint) {
365 mDisplayList->ref(left);
366 mDisplayList->ref(top);
367 mDisplayList->ref(right);
368 mDisplayList->ref(bottom);
369 mDisplayList->ref(rx);
370 mDisplayList->ref(ry);
371 mDisplayList->ref(paint);
372 refBitmapsInShader(paint->value.getShader());
John Reck7df9ff22016-02-10 16:08:08 -0800373 addOp(alloc().create_trivial<RoundRectPropsOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800374 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800375 getRecordedClip(),
Chris Craik268a9c02015-12-09 18:05:12 -0800376 &paint->value,
377 &left->value, &top->value, &right->value, &bottom->value,
378 &rx->value, &ry->value));
379}
380
Chris Craikb565df12015-10-05 13:00:52 -0700381void RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Chris Craik268a9c02015-12-09 18:05:12 -0800382 // TODO: move to Canvas.h
Chris Craik386aa032015-12-07 17:08:25 -0800383 if (radius <= 0) return;
384 drawOval(x - radius, y - radius, x + radius, y + radius, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700385}
Chris Craik386aa032015-12-07 17:08:25 -0800386
Chris Craik268a9c02015-12-09 18:05:12 -0800387void RecordingCanvas::drawCircle(
388 CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
389 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
390 mDisplayList->ref(x);
391 mDisplayList->ref(y);
392 mDisplayList->ref(radius);
393 mDisplayList->ref(paint);
394 refBitmapsInShader(paint->value.getShader());
John Reck7df9ff22016-02-10 16:08:08 -0800395 addOp(alloc().create_trivial<CirclePropsOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800396 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800397 getRecordedClip(),
Chris Craik268a9c02015-12-09 18:05:12 -0800398 &paint->value,
399 &x->value, &y->value, &radius->value));
400}
401
Chris Craikb565df12015-10-05 13:00:52 -0700402void RecordingCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800403 addOp(alloc().create_trivial<OvalOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800404 Rect(left, top, right, bottom),
405 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800406 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800407 refPaint(&paint)));
Chris Craikb565df12015-10-05 13:00:52 -0700408}
Chris Craik386aa032015-12-07 17:08:25 -0800409
Chris Craikb565df12015-10-05 13:00:52 -0700410void RecordingCanvas::drawArc(float left, float top, float right, float bottom,
Chris Craik386aa032015-12-07 17:08:25 -0800411 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Chris Craikcaa24182016-02-19 15:20:35 -0800412 if (fabs(sweepAngle) >= 360.0f) {
413 drawOval(left, top, right, bottom, paint);
414 } else {
415 addOp(alloc().create_trivial<ArcOp>(
416 Rect(left, top, right, bottom),
417 *(mState.currentSnapshot()->transform),
418 getRecordedClip(),
419 refPaint(&paint),
420 startAngle, sweepAngle, useCenter));
421 }
Chris Craikb565df12015-10-05 13:00:52 -0700422}
Chris Craik386aa032015-12-07 17:08:25 -0800423
Chris Craikb565df12015-10-05 13:00:52 -0700424void RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800425 addOp(alloc().create_trivial<PathOp>(
Chris Craik386aa032015-12-07 17:08:25 -0800426 Rect(path.getBounds()),
427 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800428 getRecordedClip(),
Chris Craik386aa032015-12-07 17:08:25 -0800429 refPaint(&paint), refPath(&path)));
Chris Craikb565df12015-10-05 13:00:52 -0700430}
431
Doris Liu766431a2016-02-04 22:17:11 +0000432void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
433 mDisplayList->ref(tree);
John Reck7df9ff22016-02-10 16:08:08 -0800434 addOp(alloc().create_trivial<VectorDrawableOp>(
Doris Liu766431a2016-02-04 22:17:11 +0000435 tree,
436 Rect(tree->getBounds()),
437 *(mState.currentSnapshot()->transform),
438 getRecordedClip()));
439}
440
Chris Craikb565df12015-10-05 13:00:52 -0700441// Bitmap-based
442void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500443 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700444 translate(left, top);
445 drawBitmap(&bitmap, paint);
446 restore();
447}
448
449void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
450 const SkPaint* paint) {
451 if (matrix.isIdentity()) {
452 drawBitmap(&bitmap, paint);
453 } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask))
454 && MathUtils::isPositive(matrix.getScaleX())
455 && MathUtils::isPositive(matrix.getScaleY())) {
456 // SkMatrix::isScaleTranslate() not available in L
457 SkRect src;
458 SkRect dst;
459 bitmap.getBounds(&src);
460 matrix.mapRect(&dst, src);
461 drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
462 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
463 } else {
Florin Malitaeecff562015-12-21 10:43:01 -0500464 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700465 concat(matrix);
466 drawBitmap(&bitmap, paint);
467 restore();
468 }
469}
Chris Craik386aa032015-12-07 17:08:25 -0800470
Chris Craikb565df12015-10-05 13:00:52 -0700471void RecordingCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
472 float srcRight, float srcBottom, float dstLeft, float dstTop,
473 float dstRight, float dstBottom, const SkPaint* paint) {
474 if (srcLeft == 0 && srcTop == 0
475 && srcRight == bitmap.width()
476 && srcBottom == bitmap.height()
477 && (srcBottom - srcTop == dstBottom - dstTop)
478 && (srcRight - srcLeft == dstRight - dstLeft)) {
479 // transform simple rect to rect drawing case into position bitmap ops, since they merge
Florin Malitaeecff562015-12-21 10:43:01 -0500480 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700481 translate(dstLeft, dstTop);
482 drawBitmap(&bitmap, paint);
483 restore();
484 } else {
John Reck7df9ff22016-02-10 16:08:08 -0800485 addOp(alloc().create_trivial<BitmapRectOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800486 Rect(dstLeft, dstTop, dstRight, dstBottom),
487 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800488 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800489 refPaint(paint), refBitmap(bitmap),
490 Rect(srcLeft, srcTop, srcRight, srcBottom)));
Chris Craikb565df12015-10-05 13:00:52 -0700491 }
492}
Chris Craik386aa032015-12-07 17:08:25 -0800493
Chris Craikb565df12015-10-05 13:00:52 -0700494void RecordingCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
495 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800496 int vertexCount = (meshWidth + 1) * (meshHeight + 1);
John Reck7df9ff22016-02-10 16:08:08 -0800497 addOp(alloc().create_trivial<BitmapMeshOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800498 calcBoundsOfPoints(vertices, vertexCount * 2),
499 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800500 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800501 refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight,
502 refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex
503 refBuffer<int>(colors, vertexCount))); // 1 color per vertex
Chris Craikb565df12015-10-05 13:00:52 -0700504}
Chris Craik386aa032015-12-07 17:08:25 -0800505
Chris Craikf09ff5a2015-12-08 17:21:58 -0800506void RecordingCanvas::drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& patch,
Chris Craikb565df12015-10-05 13:00:52 -0700507 float dstLeft, float dstTop, float dstRight, float dstBottom,
508 const SkPaint* paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800509 addOp(alloc().create_trivial<PatchOp>(
Chris Craikf09ff5a2015-12-08 17:21:58 -0800510 Rect(dstLeft, dstTop, dstRight, dstBottom),
511 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800512 getRecordedClip(),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800513 refPaint(paint), refBitmap(bitmap), refPatch(&patch)));
Chris Craikb565df12015-10-05 13:00:52 -0700514}
515
516// Text
Chris Craika1717272015-11-19 13:02:43 -0800517void RecordingCanvas::drawText(const uint16_t* glyphs, const float* positions, int glyphCount,
Chris Craikb565df12015-10-05 13:00:52 -0700518 const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
519 float boundsRight, float boundsBottom, float totalAdvance) {
Chris Craika1717272015-11-19 13:02:43 -0800520 if (!glyphs || !positions || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
521 glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
522 positions = refBuffer<float>(positions, glyphCount * 2);
523
Chris Craik15c3f192015-12-03 12:16:56 -0800524 // TODO: either must account for text shadow in bounds, or record separate ops for text shadows
John Reck7df9ff22016-02-10 16:08:08 -0800525 addOp(alloc().create_trivial<TextOp>(
Chris Craika1717272015-11-19 13:02:43 -0800526 Rect(boundsLeft, boundsTop, boundsRight, boundsBottom),
527 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800528 getRecordedClip(),
Chris Craika1717272015-11-19 13:02:43 -0800529 refPaint(&paint), glyphs, positions, glyphCount, x, y));
530 drawTextDecorations(x, y, totalAdvance, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700531}
Chris Craika1717272015-11-19 13:02:43 -0800532
Chris Craikd7448e62015-12-15 10:34:36 -0800533void RecordingCanvas::drawTextOnPath(const uint16_t* glyphs, int glyphCount, const SkPath& path,
Chris Craikb565df12015-10-05 13:00:52 -0700534 float hOffset, float vOffset, const SkPaint& paint) {
Chris Craikd7448e62015-12-15 10:34:36 -0800535 if (!glyphs || glyphCount <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
536 glyphs = refBuffer<glyph_t>(glyphs, glyphCount);
John Reck7df9ff22016-02-10 16:08:08 -0800537 addOp(alloc().create_trivial<TextOnPathOp>(
Chris Craike4db79d2015-12-22 16:32:23 -0800538 mState.getLocalClipBounds(), // TODO: explicitly define bounds
Chris Craikd7448e62015-12-15 10:34:36 -0800539 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800540 getRecordedClip(),
Chris Craikd7448e62015-12-15 10:34:36 -0800541 refPaint(&paint), glyphs, glyphCount, refPath(&path), hOffset, vOffset));
Chris Craikb565df12015-10-05 13:00:52 -0700542}
543
544void RecordingCanvas::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
John Reck7df9ff22016-02-10 16:08:08 -0800545 addOp(alloc().create_trivial<BitmapOp>(
Chris Craik5430ab22015-12-10 16:28:16 -0800546 Rect(bitmap->width(), bitmap->height()),
Chris Craikb565df12015-10-05 13:00:52 -0700547 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800548 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700549 refPaint(paint), refBitmap(*bitmap)));
550}
Chris Craike29ce6f2015-12-10 16:25:13 -0800551
Chris Craikb565df12015-10-05 13:00:52 -0700552void RecordingCanvas::drawRenderNode(RenderNode* renderNode) {
Chris Craik54fa17f2015-11-25 14:14:53 -0800553 auto&& stagingProps = renderNode->stagingProperties();
John Reck7df9ff22016-02-10 16:08:08 -0800554 RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>(
Chris Craik54fa17f2015-11-25 14:14:53 -0800555 Rect(stagingProps.getWidth(), stagingProps.getHeight()),
Chris Craikb565df12015-10-05 13:00:52 -0700556 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800557 getRecordedClip(),
Chris Craikb565df12015-10-05 13:00:52 -0700558 renderNode);
559 int opIndex = addOp(op);
Chris Craik003cc3d2015-10-16 10:24:55 -0700560 int childIndex = mDisplayList->addChild(op);
Chris Craikb565df12015-10-05 13:00:52 -0700561
562 // update the chunk's child indices
Chris Craik003cc3d2015-10-16 10:24:55 -0700563 DisplayList::Chunk& chunk = mDisplayList->chunks.back();
Chris Craikb565df12015-10-05 13:00:52 -0700564 chunk.endChildIndex = childIndex + 1;
565
566 if (renderNode->stagingProperties().isProjectionReceiver()) {
567 // use staging property, since recording on UI thread
Chris Craik003cc3d2015-10-16 10:24:55 -0700568 mDisplayList->projectionReceiveIndex = opIndex;
Chris Craikb565df12015-10-05 13:00:52 -0700569 }
570}
571
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800572void RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) {
573 // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics.
574 mDisplayList->ref(layerHandle);
575
576 Layer* layer = layerHandle->backingLayer();
577 Matrix4 totalTransform(*(mState.currentSnapshot()->transform));
578 totalTransform.multiply(layer->getTransform());
579
John Reck7df9ff22016-02-10 16:08:08 -0800580 addOp(alloc().create_trivial<TextureLayerOp>(
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800581 Rect(layer->getWidth(), layer->getHeight()),
582 totalTransform,
Chris Craike4db79d2015-12-22 16:32:23 -0800583 getRecordedClip(),
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800584 layer));
585}
586
Chris Craike29ce6f2015-12-10 16:25:13 -0800587void RecordingCanvas::callDrawGLFunction(Functor* functor) {
588 mDisplayList->functors.push_back(functor);
John Reck7df9ff22016-02-10 16:08:08 -0800589 addOp(alloc().create_trivial<FunctorOp>(
Chris Craike4db79d2015-12-22 16:32:23 -0800590 mState.getLocalClipBounds(), // TODO: explicitly define bounds
Chris Craike29ce6f2015-12-10 16:25:13 -0800591 *(mState.currentSnapshot()->transform),
Chris Craike4db79d2015-12-22 16:32:23 -0800592 getRecordedClip(),
Chris Craike29ce6f2015-12-10 16:25:13 -0800593 functor));
594}
595
Chris Craikb565df12015-10-05 13:00:52 -0700596size_t RecordingCanvas::addOp(RecordedOp* op) {
597 // TODO: validate if "addDrawOp" quickrejection logic is useful before adding
Chris Craik003cc3d2015-10-16 10:24:55 -0700598 int insertIndex = mDisplayList->ops.size();
599 mDisplayList->ops.push_back(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800600 if (mDeferredBarrierType != DeferredBarrierType::None) {
Chris Craikb565df12015-10-05 13:00:52 -0700601 // op is first in new chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700602 mDisplayList->chunks.emplace_back();
603 DisplayList::Chunk& newChunk = mDisplayList->chunks.back();
Chris Craikb565df12015-10-05 13:00:52 -0700604 newChunk.beginOpIndex = insertIndex;
605 newChunk.endOpIndex = insertIndex + 1;
Chris Craik161f54b2015-11-05 11:08:52 -0800606 newChunk.reorderChildren = (mDeferredBarrierType == DeferredBarrierType::OutOfOrder);
Chris Craikb565df12015-10-05 13:00:52 -0700607
Chris Craikb36af872015-10-16 14:23:12 -0700608 int nextChildIndex = mDisplayList->children.size();
Chris Craikb565df12015-10-05 13:00:52 -0700609 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex;
Chris Craik161f54b2015-11-05 11:08:52 -0800610 mDeferredBarrierType = DeferredBarrierType::None;
Chris Craikb565df12015-10-05 13:00:52 -0700611 } else {
612 // standard case - append to existing chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700613 mDisplayList->chunks.back().endOpIndex = insertIndex + 1;
Chris Craikb565df12015-10-05 13:00:52 -0700614 }
615 return insertIndex;
616}
617
618void RecordingCanvas::refBitmapsInShader(const SkShader* shader) {
619 if (!shader) return;
620
621 // If this paint has an SkShader that has an SkBitmap add
622 // it to the bitmap pile
623 SkBitmap bitmap;
624 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400625 if (shader->isABitmap(&bitmap, nullptr, xy)) {
Chris Craikb565df12015-10-05 13:00:52 -0700626 refBitmap(bitmap);
627 return;
628 }
629 SkShader::ComposeRec rec;
630 if (shader->asACompose(&rec)) {
631 refBitmapsInShader(rec.fShaderA);
632 refBitmapsInShader(rec.fShaderB);
633 return;
634 }
635}
636
637}; // namespace uirenderer
638}; // namespace android