blob: e1df1e7725b5b4f0364659d7161d28a3cac52bfa [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"
Yuqian Liafc221492016-07-18 13:07:42 -040023#include "hwui/MinikinUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070024
25namespace android {
26namespace uirenderer {
27
28RecordingCanvas::RecordingCanvas(size_t width, size_t height)
John Reck1bcacfd2017-11-03 10:12:19 -070029 : mState(*this), 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() {
John Reck1bcacfd2017-11-03 10:12:19 -070034 LOG_ALWAYS_FATAL_IF(mDisplayList, "Destroyed a RecordingCanvas during a record!");
Chris Craikb565df12015-10-05 13:00:52 -070035}
36
Stan Ilievc0e7a902016-10-13 17:07:09 -040037void RecordingCanvas::resetRecording(int width, int height, RenderNode* node) {
John Reck1bcacfd2017-11-03 10:12:19 -070038 LOG_ALWAYS_FATAL_IF(mDisplayList, "prepareDirty called a second time during a recording!");
Chris Craik003cc3d2015-10-16 10:24:55 -070039 mDisplayList = new DisplayList();
Chris Craikb565df12015-10-05 13:00:52 -070040
Chris Craike4db79d2015-12-22 16:32:23 -080041 mState.initializeRecordingSaveStack(width, height);
Chris Craikb565df12015-10-05 13:00:52 -070042
Chris Craik161f54b2015-11-05 11:08:52 -080043 mDeferredBarrierType = DeferredBarrierType::InOrder;
Chris Craikb565df12015-10-05 13:00:52 -070044}
45
Chris Craik003cc3d2015-10-16 10:24:55 -070046DisplayList* RecordingCanvas::finishRecording() {
Chris Craikb87eadd2016-01-06 09:16:05 -080047 restoreToCount(1);
Chris Craikb565df12015-10-05 13:00:52 -070048 mPaintMap.clear();
49 mRegionMap.clear();
50 mPathMap.clear();
Chris Craik003cc3d2015-10-16 10:24:55 -070051 DisplayList* displayList = mDisplayList;
52 mDisplayList = nullptr;
Chris Craikb565df12015-10-05 13:00:52 -070053 mSkiaCanvasProxy.reset(nullptr);
Chris Craik003cc3d2015-10-16 10:24:55 -070054 return displayList;
Chris Craikb565df12015-10-05 13:00:52 -070055}
56
Chris Craikd6456402016-04-11 12:24:23 -070057void RecordingCanvas::insertReorderBarrier(bool enableReorder) {
58 if (enableReorder) {
59 mDeferredBarrierType = DeferredBarrierType::OutOfOrder;
60 mDeferredBarrierClip = getRecordedClip();
61 } else {
62 mDeferredBarrierType = DeferredBarrierType::InOrder;
63 mDeferredBarrierClip = nullptr;
64 }
65}
66
Chris Craikb565df12015-10-05 13:00:52 -070067SkCanvas* RecordingCanvas::asSkCanvas() {
John Reck1bcacfd2017-11-03 10:12:19 -070068 LOG_ALWAYS_FATAL_IF(!mDisplayList, "attempting to get an SkCanvas when we are not recording!");
Chris Craikb565df12015-10-05 13:00:52 -070069 if (!mSkiaCanvasProxy) {
70 mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this));
71 }
72
73 // SkCanvas instances default to identity transform, but should inherit
74 // the state of this Canvas; if this code was in the SkiaCanvasProxy
75 // constructor, we couldn't cache mSkiaCanvasProxy.
76 SkMatrix parentTransform;
77 getMatrix(&parentTransform);
78 mSkiaCanvasProxy.get()->setMatrix(parentTransform);
79
80 return mSkiaCanvasProxy.get();
81}
82
83// ----------------------------------------------------------------------------
Chris Craik6fe991e52015-10-20 09:39:42 -070084// CanvasStateClient implementation
85// ----------------------------------------------------------------------------
86
John Reck1bcacfd2017-11-03 10:12:19 -070087void RecordingCanvas::onViewportInitialized() {}
Chris Craik6fe991e52015-10-20 09:39:42 -070088
89void RecordingCanvas::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
90 if (removed.flags & Snapshot::kFlagIsFboLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080091 addOp(alloc().create_trivial<EndLayerOp>());
Chris Craikb87eadd2016-01-06 09:16:05 -080092 } else if (removed.flags & Snapshot::kFlagIsLayer) {
John Reck7df9ff22016-02-10 16:08:08 -080093 addOp(alloc().create_trivial<EndUnclippedLayerOp>());
Chris Craik6fe991e52015-10-20 09:39:42 -070094 }
95}
96
97// ----------------------------------------------------------------------------
Chris Craikb565df12015-10-05 13:00:52 -070098// android/graphics/Canvas state operations
99// ----------------------------------------------------------------------------
100// Save (layer)
Florin Malitaeecff562015-12-21 10:43:01 -0500101int RecordingCanvas::save(SaveFlags::Flags flags) {
John Reck1bcacfd2017-11-03 10:12:19 -0700102 return mState.save((int)flags);
Chris Craikb565df12015-10-05 13:00:52 -0700103}
104
105void RecordingCanvas::RecordingCanvas::restore() {
Chris Craikb565df12015-10-05 13:00:52 -0700106 mState.restore();
107}
108
109void RecordingCanvas::restoreToCount(int saveCount) {
Chris Craikb565df12015-10-05 13:00:52 -0700110 mState.restoreToCount(saveCount);
111}
112
Chris Craikb87eadd2016-01-06 09:16:05 -0800113int RecordingCanvas::saveLayer(float left, float top, float right, float bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700114 const SkPaint* paint, SaveFlags::Flags flags) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700115 // force matrix/clip isolation for layer
Florin Malitaeecff562015-12-21 10:43:01 -0500116 flags |= SaveFlags::MatrixClip;
117 bool clippedLayer = flags & SaveFlags::ClipToLayer;
Chris Craik6fe991e52015-10-20 09:39:42 -0700118
119 const Snapshot& previous = *mState.currentSnapshot();
120
121 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
122 // operations will be able to store and restore the current clip and transform info, and
123 // quick rejection will be correct (for display lists)
124
Chris Craike4f6d962016-06-27 16:30:27 -0700125 Rect unmappedBounds(left, top, right, bottom);
126 unmappedBounds.roundOut();
Chris Craik6fe991e52015-10-20 09:39:42 -0700127
128 // determine clipped bounds relative to previous viewport.
Chris Craikb87eadd2016-01-06 09:16:05 -0800129 Rect visibleBounds = unmappedBounds;
Chris Craik6fe991e52015-10-20 09:39:42 -0700130 previous.transform->mapRect(visibleBounds);
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132 if (CC_UNLIKELY(!clippedLayer && previous.transform->rectToRect() &&
133 visibleBounds.contains(previous.getRenderTargetClip()))) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800134 // unlikely case where an unclipped savelayer is recorded with a clip it can use,
135 // as none of its unaffected/unclipped area is visible
136 clippedLayer = true;
Florin Malitaeecff562015-12-21 10:43:01 -0500137 flags |= SaveFlags::ClipToLayer;
Chris Craikb87eadd2016-01-06 09:16:05 -0800138 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700139
140 visibleBounds.doIntersect(previous.getRenderTargetClip());
141 visibleBounds.snapToPixelBoundaries();
Chris Craikb87eadd2016-01-06 09:16:05 -0800142 visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight()));
Chris Craik6fe991e52015-10-20 09:39:42 -0700143
144 // Map visible bounds back to layer space, and intersect with parameter bounds
145 Rect layerBounds = visibleBounds;
Chris Craik3c53ec52016-08-08 15:15:57 -0700146 if (CC_LIKELY(!layerBounds.isEmpty())) {
147 // if non-empty, can safely map by the inverse transform
148 Matrix4 inverse;
149 inverse.loadInverse(*previous.transform);
150 inverse.mapRect(layerBounds);
151 layerBounds.doIntersect(unmappedBounds);
152 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700153
John Reck1bcacfd2017-11-03 10:12:19 -0700154 int saveValue = mState.save((int)flags);
Chris Craik6fe991e52015-10-20 09:39:42 -0700155 Snapshot& snapshot = *mState.writableSnapshot();
156
Chris Craikb87eadd2016-01-06 09:16:05 -0800157 // layerBounds is in original bounds space, but clipped by current recording clip
Chris Craik3c53ec52016-08-08 15:15:57 -0700158 if (!layerBounds.isEmpty() && !unmappedBounds.isEmpty()) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800159 if (CC_LIKELY(clippedLayer)) {
John Reck1bcacfd2017-11-03 10:12:19 -0700160 auto previousClip = getRecordedClip(); // capture before new snapshot clip has changed
Chris Craik3c53ec52016-08-08 15:15:57 -0700161 if (addOp(alloc().create_trivial<BeginLayerOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700162 unmappedBounds,
163 *previous.transform, // transform to *draw* with
164 previousClip, // clip to *draw* with
165 refPaint(paint))) >= 0) {
Chris Craik3c53ec52016-08-08 15:15:57 -0700166 snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
167 snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
168 snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
169
170 Rect clip = layerBounds;
171 clip.translate(-unmappedBounds.left, -unmappedBounds.top);
172 snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
173 snapshot.roundRectClipState = nullptr;
174 return saveValue;
175 }
176 } else {
177 if (addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700178 unmappedBounds, *mState.currentSnapshot()->transform, getRecordedClip(),
179 refPaint(paint))) >= 0) {
Chris Craik3c53ec52016-08-08 15:15:57 -0700180 snapshot.flags |= Snapshot::kFlagIsLayer;
181 return saveValue;
182 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800183 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700184 }
185
Chris Craik3c53ec52016-08-08 15:15:57 -0700186 // Layer not needed, so skip recording it...
Chris Craikb87eadd2016-01-06 09:16:05 -0800187 if (CC_LIKELY(clippedLayer)) {
Chris Craik3c53ec52016-08-08 15:15:57 -0700188 // ... and set empty clip to reject inner content, if possible
189 snapshot.resetClip(0, 0, 0, 0);
Chris Craikb87eadd2016-01-06 09:16:05 -0800190 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700191 return saveValue;
Chris Craikb565df12015-10-05 13:00:52 -0700192}
193
194// Matrix
195void RecordingCanvas::rotate(float degrees) {
196 if (degrees == 0) return;
197
198 mState.rotate(degrees);
199}
200
201void RecordingCanvas::scale(float sx, float sy) {
202 if (sx == 1 && sy == 1) return;
203
204 mState.scale(sx, sy);
205}
206
207void RecordingCanvas::skew(float sx, float sy) {
208 mState.skew(sx, sy);
209}
210
211void RecordingCanvas::translate(float dx, float dy) {
212 if (dx == 0 && dy == 0) return;
213
214 mState.translate(dx, dy, 0);
215}
216
217// Clip
218bool RecordingCanvas::getClipBounds(SkRect* outRect) const {
Chris Craike29ce6f2015-12-10 16:25:13 -0800219 *outRect = mState.getLocalClipBounds().toSkRect();
Chris Craikb565df12015-10-05 13:00:52 -0700220 return !(outRect->isEmpty());
221}
222bool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
223 return mState.quickRejectConservative(left, top, right, bottom);
224}
225bool RecordingCanvas::quickRejectPath(const SkPath& path) const {
226 SkRect bounds = path.getBounds();
227 return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
228}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500229bool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Chris Craikb565df12015-10-05 13:00:52 -0700230 return mState.clipRect(left, top, right, bottom, op);
231}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500232bool RecordingCanvas::clipPath(const SkPath* path, SkClipOp op) {
Chris Craikb565df12015-10-05 13:00:52 -0700233 return mState.clipPath(path, op);
234}
Chris Craikb565df12015-10-05 13:00:52 -0700235
236// ----------------------------------------------------------------------------
237// android/graphics/Canvas draw operations
238// ----------------------------------------------------------------------------
Mike Reed260ab722016-10-07 15:59:20 -0400239void RecordingCanvas::drawColor(int color, SkBlendMode mode) {
John Reck1bcacfd2017-11-03 10:12:19 -0700240 addOp(alloc().create_trivial<ColorOp>(getRecordedClip(), color, mode));
Chris Craikb565df12015-10-05 13:00:52 -0700241}
242
243void RecordingCanvas::drawPaint(const SkPaint& paint) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700244 SkRect bounds;
245 if (getClipBounds(&bounds)) {
246 drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint);
247 }
Chris Craikb565df12015-10-05 13:00:52 -0700248}
249
Chris Craik386aa032015-12-07 17:08:25 -0800250static Rect calcBoundsOfPoints(const float* points, int floatCount) {
251 Rect unmappedBounds(points[0], points[1], points[0], points[1]);
252 for (int i = 2; i < floatCount; i += 2) {
253 unmappedBounds.expandToCover(points[i], points[i + 1]);
254 }
255 return unmappedBounds;
256}
257
Chris Craikb565df12015-10-05 13:00:52 -0700258// Geometry
Chris Craik386aa032015-12-07 17:08:25 -0800259void RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500260 if (CC_UNLIKELY(floatCount < 2 || paint.nothingToDraw())) return;
John Reck1bcacfd2017-11-03 10:12:19 -0700261 floatCount &= ~0x1; // round down to nearest two
Chris Craik386aa032015-12-07 17:08:25 -0800262
John Reck7df9ff22016-02-10 16:08:08 -0800263 addOp(alloc().create_trivial<PointsOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700264 calcBoundsOfPoints(points, floatCount), *mState.currentSnapshot()->transform,
265 getRecordedClip(), refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700266}
Chris Craika1717272015-11-19 13:02:43 -0800267
268void RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500269 if (CC_UNLIKELY(floatCount < 4 || paint.nothingToDraw())) return;
John Reck1bcacfd2017-11-03 10:12:19 -0700270 floatCount &= ~0x3; // round down to nearest four
Chris Craika1717272015-11-19 13:02:43 -0800271
John Reck7df9ff22016-02-10 16:08:08 -0800272 addOp(alloc().create_trivial<LinesOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700273 calcBoundsOfPoints(points, floatCount), *mState.currentSnapshot()->transform,
274 getRecordedClip(), refPaint(&paint), refBuffer<float>(points, floatCount), floatCount));
Chris Craikb565df12015-10-05 13:00:52 -0700275}
Chris Craika1717272015-11-19 13:02:43 -0800276
John Reck1bcacfd2017-11-03 10:12:19 -0700277void RecordingCanvas::drawRect(float left, float top, float right, float bottom,
278 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500279 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700280
John Reck1bcacfd2017-11-03 10:12:19 -0700281 addOp(alloc().create_trivial<RectOp>(Rect(left, top, right, bottom),
282 *(mState.currentSnapshot()->transform), getRecordedClip(),
283 refPaint(&paint)));
Chris Craikb565df12015-10-05 13:00:52 -0700284}
285
286void RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) {
287 if (rects == nullptr) return;
288
John Reck1bcacfd2017-11-03 10:12:19 -0700289 Vertex* rectData = (Vertex*)mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount);
Chris Craikb565df12015-10-05 13:00:52 -0700290 Vertex* vertex = rectData;
291
292 float left = FLT_MAX;
293 float top = FLT_MAX;
294 float right = FLT_MIN;
295 float bottom = FLT_MIN;
296 for (int index = 0; index < vertexCount; index += 4) {
297 float l = rects[index + 0];
298 float t = rects[index + 1];
299 float r = rects[index + 2];
300 float b = rects[index + 3];
301
302 Vertex::set(vertex++, l, t);
303 Vertex::set(vertex++, r, t);
304 Vertex::set(vertex++, l, b);
305 Vertex::set(vertex++, r, b);
306
307 left = std::min(left, l);
308 top = std::min(top, t);
309 right = std::max(right, r);
310 bottom = std::max(bottom, b);
311 }
John Reck7df9ff22016-02-10 16:08:08 -0800312 addOp(alloc().create_trivial<SimpleRectsOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700313 Rect(left, top, right, bottom), *(mState.currentSnapshot()->transform),
314 getRecordedClip(), refPaint(paint), rectData, vertexCount));
Chris Craikb565df12015-10-05 13:00:52 -0700315}
316
317void RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500318 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700319
John Reck1bcacfd2017-11-03 10:12:19 -0700320 if (paint.getStyle() == SkPaint::kFill_Style &&
321 (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
Chris Craikb565df12015-10-05 13:00:52 -0700322 int count = 0;
323 Vector<float> rects;
324 SkRegion::Iterator it(region);
325 while (!it.done()) {
326 const SkIRect& r = it.rect();
327 rects.push(r.fLeft);
328 rects.push(r.fTop);
329 rects.push(r.fRight);
330 rects.push(r.fBottom);
331 count += 4;
332 it.next();
333 }
334 drawSimpleRects(rects.array(), count, &paint);
335 } else {
336 SkRegion::Iterator it(region);
337 while (!it.done()) {
338 const SkIRect& r = it.rect();
339 drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
340 it.next();
341 }
342 }
343}
Chris Craik814ee6a92016-07-26 16:22:50 -0700344
John Reck1bcacfd2017-11-03 10:12:19 -0700345void RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx,
346 float ry, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500347 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700348
Chris Craik2dbb4c42016-03-11 18:58:37 -0800349 if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) {
John Reck1bcacfd2017-11-03 10:12:19 -0700350 addOp(alloc().create_trivial<RoundRectOp>(Rect(left, top, right, bottom),
351 *(mState.currentSnapshot()->transform),
352 getRecordedClip(), refPaint(&paint), rx, ry));
Chris Craik2dbb4c42016-03-11 18:58:37 -0800353 } else {
354 drawRect(left, top, right, bottom, paint);
355 }
Chris Craikb565df12015-10-05 13:00:52 -0700356}
Chris Craik386aa032015-12-07 17:08:25 -0800357
John Reck1bcacfd2017-11-03 10:12:19 -0700358void RecordingCanvas::drawRoundRect(CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
359 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
360 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
361 CanvasPropertyPaint* paint) {
Chris Craik268a9c02015-12-09 18:05:12 -0800362 mDisplayList->ref(left);
363 mDisplayList->ref(top);
364 mDisplayList->ref(right);
365 mDisplayList->ref(bottom);
366 mDisplayList->ref(rx);
367 mDisplayList->ref(ry);
368 mDisplayList->ref(paint);
369 refBitmapsInShader(paint->value.getShader());
John Reck7df9ff22016-02-10 16:08:08 -0800370 addOp(alloc().create_trivial<RoundRectPropsOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700371 *(mState.currentSnapshot()->transform), getRecordedClip(), &paint->value, &left->value,
372 &top->value, &right->value, &bottom->value, &rx->value, &ry->value));
Chris Craik268a9c02015-12-09 18:05:12 -0800373}
374
Chris Craikb565df12015-10-05 13:00:52 -0700375void RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Chris Craik268a9c02015-12-09 18:05:12 -0800376 // TODO: move to Canvas.h
Derek Sollenberger792fb322017-03-03 14:02:09 -0500377 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700378
Chris Craik386aa032015-12-07 17:08:25 -0800379 drawOval(x - radius, y - radius, x + radius, y + radius, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700380}
Chris Craik386aa032015-12-07 17:08:25 -0800381
John Reck1bcacfd2017-11-03 10:12:19 -0700382void RecordingCanvas::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
383 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
Chris Craik268a9c02015-12-09 18:05:12 -0800384 mDisplayList->ref(x);
385 mDisplayList->ref(y);
386 mDisplayList->ref(radius);
387 mDisplayList->ref(paint);
388 refBitmapsInShader(paint->value.getShader());
John Reck1bcacfd2017-11-03 10:12:19 -0700389 addOp(alloc().create_trivial<CirclePropsOp>(*(mState.currentSnapshot()->transform),
390 getRecordedClip(), &paint->value, &x->value,
391 &y->value, &radius->value));
Chris Craik268a9c02015-12-09 18:05:12 -0800392}
393
John Reck1bcacfd2017-11-03 10:12:19 -0700394void RecordingCanvas::drawOval(float left, float top, float right, float bottom,
395 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500396 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700397
John Reck1bcacfd2017-11-03 10:12:19 -0700398 addOp(alloc().create_trivial<OvalOp>(Rect(left, top, right, bottom),
399 *(mState.currentSnapshot()->transform), getRecordedClip(),
400 refPaint(&paint)));
Chris Craikb565df12015-10-05 13:00:52 -0700401}
Chris Craik386aa032015-12-07 17:08:25 -0800402
John Reck1bcacfd2017-11-03 10:12:19 -0700403void RecordingCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
404 float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500405 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700406
Chris Craikcaa24182016-02-19 15:20:35 -0800407 if (fabs(sweepAngle) >= 360.0f) {
408 drawOval(left, top, right, bottom, paint);
409 } else {
410 addOp(alloc().create_trivial<ArcOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700411 Rect(left, top, right, bottom), *(mState.currentSnapshot()->transform),
412 getRecordedClip(), refPaint(&paint), startAngle, sweepAngle, useCenter));
Chris Craikcaa24182016-02-19 15:20:35 -0800413 }
Chris Craikb565df12015-10-05 13:00:52 -0700414}
Chris Craik386aa032015-12-07 17:08:25 -0800415
Chris Craikb565df12015-10-05 13:00:52 -0700416void RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500417 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Chris Craik814ee6a92016-07-26 16:22:50 -0700418
John Reck1bcacfd2017-11-03 10:12:19 -0700419 addOp(alloc().create_trivial<PathOp>(Rect(path.getBounds()),
420 *(mState.currentSnapshot()->transform), getRecordedClip(),
421 refPaint(&paint), refPath(&path)));
Chris Craikb565df12015-10-05 13:00:52 -0700422}
423
Doris Liu766431a2016-02-04 22:17:11 +0000424void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
425 mDisplayList->ref(tree);
Doris Liu718cd3e2016-05-17 16:50:31 -0700426 mDisplayList->vectorDrawables.push_back(tree);
John Reck7df9ff22016-02-10 16:08:08 -0800427 addOp(alloc().create_trivial<VectorDrawableOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700428 tree, Rect(tree->stagingProperties()->getBounds()),
429 *(mState.currentSnapshot()->transform), getRecordedClip()));
Doris Liu766431a2016-02-04 22:17:11 +0000430}
431
Chris Craikb565df12015-10-05 13:00:52 -0700432// Bitmap-based
sergeyvaed7f582016-10-14 16:30:21 -0700433void RecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500434 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700435 translate(left, top);
sergeyvec4a4b12016-10-20 18:39:04 -0700436 drawBitmap(bitmap, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700437 restore();
438}
439
John Reck1bcacfd2017-11-03 10:12:19 -0700440void RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Chris Craikb565df12015-10-05 13:00:52 -0700441 if (matrix.isIdentity()) {
sergeyvec4a4b12016-10-20 18:39:04 -0700442 drawBitmap(bitmap, paint);
John Reck1bcacfd2017-11-03 10:12:19 -0700443 } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) &&
444 MathUtils::isPositive(matrix.getScaleX()) &&
445 MathUtils::isPositive(matrix.getScaleY())) {
Chris Craikb565df12015-10-05 13:00:52 -0700446 // SkMatrix::isScaleTranslate() not available in L
447 SkRect src;
448 SkRect dst;
449 bitmap.getBounds(&src);
450 matrix.mapRect(&dst, src);
John Reck1bcacfd2017-11-03 10:12:19 -0700451 drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, dst.fTop,
452 dst.fRight, dst.fBottom, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700453 } else {
Florin Malitaeecff562015-12-21 10:43:01 -0500454 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700455 concat(matrix);
sergeyvec4a4b12016-10-20 18:39:04 -0700456 drawBitmap(bitmap, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700457 restore();
458 }
459}
Chris Craik386aa032015-12-07 17:08:25 -0800460
John Reck1bcacfd2017-11-03 10:12:19 -0700461void RecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
462 float srcBottom, float dstLeft, float dstTop, float dstRight,
463 float dstBottom, const SkPaint* paint) {
464 if (srcLeft == 0 && srcTop == 0 && srcRight == bitmap.width() && srcBottom == bitmap.height() &&
465 (srcBottom - srcTop == dstBottom - dstTop) && (srcRight - srcLeft == dstRight - dstLeft)) {
Chris Craikb565df12015-10-05 13:00:52 -0700466 // transform simple rect to rect drawing case into position bitmap ops, since they merge
Florin Malitaeecff562015-12-21 10:43:01 -0500467 save(SaveFlags::Matrix);
Chris Craikb565df12015-10-05 13:00:52 -0700468 translate(dstLeft, dstTop);
sergeyvec4a4b12016-10-20 18:39:04 -0700469 drawBitmap(bitmap, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700470 restore();
471 } else {
John Reck7df9ff22016-02-10 16:08:08 -0800472 addOp(alloc().create_trivial<BitmapRectOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700473 Rect(dstLeft, dstTop, dstRight, dstBottom), *(mState.currentSnapshot()->transform),
474 getRecordedClip(), refPaint(paint), refBitmap(bitmap),
Chris Craikf09ff5a2015-12-08 17:21:58 -0800475 Rect(srcLeft, srcTop, srcRight, srcBottom)));
Chris Craikb565df12015-10-05 13:00:52 -0700476 }
477}
Chris Craik386aa032015-12-07 17:08:25 -0800478
sergeyvec4a4b12016-10-20 18:39:04 -0700479void RecordingCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700480 const float* vertices, const int* colors,
481 const SkPaint* paint) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800482 int vertexCount = (meshWidth + 1) * (meshHeight + 1);
John Reck7df9ff22016-02-10 16:08:08 -0800483 addOp(alloc().create_trivial<BitmapMeshOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700484 calcBoundsOfPoints(vertices, vertexCount * 2), *(mState.currentSnapshot()->transform),
485 getRecordedClip(), refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight,
486 refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex
487 refBuffer<int>(colors, vertexCount))); // 1 color per vertex
Chris Craikb565df12015-10-05 13:00:52 -0700488}
Chris Craik386aa032015-12-07 17:08:25 -0800489
sergeyvec4a4b12016-10-20 18:39:04 -0700490void RecordingCanvas::drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& patch,
John Reck1bcacfd2017-11-03 10:12:19 -0700491 float dstLeft, float dstTop, float dstRight, float dstBottom,
492 const SkPaint* paint) {
493 addOp(alloc().create_trivial<PatchOp>(Rect(dstLeft, dstTop, dstRight, dstBottom),
494 *(mState.currentSnapshot()->transform), getRecordedClip(),
495 refPaint(paint), refBitmap(bitmap), refPatch(&patch)));
Chris Craikb565df12015-10-05 13:00:52 -0700496}
497
Derek Sollenberger2d142132018-01-22 10:25:26 -0500498double RecordingCanvas::drawAnimatedImage(AnimatedImageDrawable*) {
Leon Scroggins III671cce22018-01-14 16:52:17 -0500499 // Unimplemented
Derek Sollenberger2d142132018-01-22 10:25:26 -0500500 return 0;
Leon Scroggins III671cce22018-01-14 16:52:17 -0500501}
502
Chris Craikb565df12015-10-05 13:00:52 -0700503// Text
Stan Iliev0b58d992017-03-30 18:22:27 -0400504void RecordingCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int glyphCount, const SkPaint& paint,
John Reck1bcacfd2017-11-03 10:12:19 -0700505 float x, float y, float boundsLeft, float boundsTop,
506 float boundsRight, float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400507 if (glyphCount <= 0 || paint.nothingToDraw()) return;
508 uint16_t* glyphs = (glyph_t*)alloc().alloc<glyph_t>(glyphCount * sizeof(glyph_t));
509 float* positions = (float*)alloc().alloc<float>(2 * glyphCount * sizeof(float));
510 glyphFunc(glyphs, positions);
Chris Craika1717272015-11-19 13:02:43 -0800511
Chris Craik15c3f192015-12-03 12:16:56 -0800512 // TODO: either must account for text shadow in bounds, or record separate ops for text shadows
John Reck1bcacfd2017-11-03 10:12:19 -0700513 addOp(alloc().create_trivial<TextOp>(Rect(boundsLeft, boundsTop, boundsRight, boundsBottom),
514 *(mState.currentSnapshot()->transform), getRecordedClip(),
515 refPaint(&paint), glyphs, positions, glyphCount, x, y));
Chris Craika1717272015-11-19 13:02:43 -0800516 drawTextDecorations(x, y, totalAdvance, paint);
Chris Craikb565df12015-10-05 13:00:52 -0700517}
Chris Craika1717272015-11-19 13:02:43 -0800518
Yuqian Liafc221492016-07-18 13:07:42 -0400519void RecordingCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700520 const SkPaint& paint, const SkPath& path, size_t start,
521 size_t end) {
Yuqian Liafc221492016-07-18 13:07:42 -0400522 uint16_t glyphs[1];
523 for (size_t i = start; i < end; i++) {
524 glyphs[0] = layout.getGlyphId(i);
525 float x = hOffset + layout.getX(i);
526 float y = vOffset + layout.getY(i);
Derek Sollenberger792fb322017-03-03 14:02:09 -0500527 if (paint.nothingToDraw()) return;
Yuqian Liafc221492016-07-18 13:07:42 -0400528 const uint16_t* tempGlyphs = refBuffer<glyph_t>(glyphs, 1);
John Reck1bcacfd2017-11-03 10:12:19 -0700529 addOp(alloc().create_trivial<TextOnPathOp>(*(mState.currentSnapshot()->transform),
530 getRecordedClip(), refPaint(&paint), tempGlyphs,
531 1, refPath(&path), x, y));
Yuqian Liafc221492016-07-18 13:07:42 -0400532 }
Chris Craikb565df12015-10-05 13:00:52 -0700533}
534
sergeyvec4a4b12016-10-20 18:39:04 -0700535void RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkPaint* paint) {
John Reck1bcacfd2017-11-03 10:12:19 -0700536 addOp(alloc().create_trivial<BitmapOp>(Rect(bitmap.width(), bitmap.height()),
537 *(mState.currentSnapshot()->transform),
538 getRecordedClip(), refPaint(paint), refBitmap(bitmap)));
Chris Craikb565df12015-10-05 13:00:52 -0700539}
Chris Craike29ce6f2015-12-10 16:25:13 -0800540
Chris Craikb565df12015-10-05 13:00:52 -0700541void RecordingCanvas::drawRenderNode(RenderNode* renderNode) {
Chris Craik54fa17f2015-11-25 14:14:53 -0800542 auto&& stagingProps = renderNode->stagingProperties();
John Reck7df9ff22016-02-10 16:08:08 -0800543 RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>(
Chris Craik54fa17f2015-11-25 14:14:53 -0800544 Rect(stagingProps.getWidth(), stagingProps.getHeight()),
John Reck1bcacfd2017-11-03 10:12:19 -0700545 *(mState.currentSnapshot()->transform), getRecordedClip(), renderNode);
Chris Craikb565df12015-10-05 13:00:52 -0700546 int opIndex = addOp(op);
Chris Craik1367d252016-03-10 15:43:13 -0800547 if (CC_LIKELY(opIndex >= 0)) {
548 int childIndex = mDisplayList->addChild(op);
Chris Craikb565df12015-10-05 13:00:52 -0700549
Chris Craik1367d252016-03-10 15:43:13 -0800550 // update the chunk's child indices
551 DisplayList::Chunk& chunk = mDisplayList->chunks.back();
552 chunk.endChildIndex = childIndex + 1;
Chris Craikb565df12015-10-05 13:00:52 -0700553
Chris Craik1367d252016-03-10 15:43:13 -0800554 if (renderNode->stagingProperties().isProjectionReceiver()) {
555 // use staging property, since recording on UI thread
556 mDisplayList->projectionReceiveIndex = opIndex;
557 }
Chris Craikb565df12015-10-05 13:00:52 -0700558 }
559}
560
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800561void RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) {
562 // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics.
563 mDisplayList->ref(layerHandle);
564
sergeyv3e9999b2017-01-19 15:37:02 -0800565 LOG_ALWAYS_FATAL_IF(layerHandle->getBackingLayerApi() != Layer::Api::OpenGL);
John Reck417ed6d2016-03-22 16:01:08 -0700566 // Note that the backing layer has *not* yet been updated, so don't trust
567 // its width, height, transform, etc...!
John Reck7df9ff22016-02-10 16:08:08 -0800568 addOp(alloc().create_trivial<TextureLayerOp>(
John Reck417ed6d2016-03-22 16:01:08 -0700569 Rect(layerHandle->getWidth(), layerHandle->getHeight()),
John Reck1bcacfd2017-11-03 10:12:19 -0700570 *(mState.currentSnapshot()->transform), getRecordedClip(), layerHandle));
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800571}
572
John Reck1bcacfd2017-11-03 10:12:19 -0700573void RecordingCanvas::callDrawGLFunction(Functor* functor, GlFunctorLifecycleListener* listener) {
John Reckcd1c3eb2016-04-14 10:38:54 -0700574 mDisplayList->functors.push_back({functor, listener});
575 mDisplayList->ref(listener);
John Reck1bcacfd2017-11-03 10:12:19 -0700576 addOp(alloc().create_trivial<FunctorOp>(*(mState.currentSnapshot()->transform),
577 getRecordedClip(), functor));
Chris Craike29ce6f2015-12-10 16:25:13 -0800578}
579
Chris Craik3c53ec52016-08-08 15:15:57 -0700580int RecordingCanvas::addOp(RecordedOp* op) {
Chris Craik261725f2016-02-29 12:52:33 -0800581 // skip op with empty clip
582 if (op->localClip && op->localClip->rect.isEmpty()) {
583 // NOTE: this rejection happens after op construction/content ref-ing, so content ref'd
584 // and held by renderthread isn't affected by clip rejection.
585 // Could rewind alloc here if desired, but callers would have to not touch op afterwards.
586 return -1;
587 }
588
Chris Craik003cc3d2015-10-16 10:24:55 -0700589 int insertIndex = mDisplayList->ops.size();
590 mDisplayList->ops.push_back(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800591 if (mDeferredBarrierType != DeferredBarrierType::None) {
Chris Craikb565df12015-10-05 13:00:52 -0700592 // op is first in new chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700593 mDisplayList->chunks.emplace_back();
594 DisplayList::Chunk& newChunk = mDisplayList->chunks.back();
Chris Craikb565df12015-10-05 13:00:52 -0700595 newChunk.beginOpIndex = insertIndex;
596 newChunk.endOpIndex = insertIndex + 1;
Chris Craik161f54b2015-11-05 11:08:52 -0800597 newChunk.reorderChildren = (mDeferredBarrierType == DeferredBarrierType::OutOfOrder);
Chris Craikd6456402016-04-11 12:24:23 -0700598 newChunk.reorderClip = mDeferredBarrierClip;
Chris Craikb565df12015-10-05 13:00:52 -0700599
Chris Craikb36af872015-10-16 14:23:12 -0700600 int nextChildIndex = mDisplayList->children.size();
Chris Craikb565df12015-10-05 13:00:52 -0700601 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex;
Chris Craik161f54b2015-11-05 11:08:52 -0800602 mDeferredBarrierType = DeferredBarrierType::None;
Chris Craikb565df12015-10-05 13:00:52 -0700603 } else {
604 // standard case - append to existing chunk
Chris Craik003cc3d2015-10-16 10:24:55 -0700605 mDisplayList->chunks.back().endOpIndex = insertIndex + 1;
Chris Craikb565df12015-10-05 13:00:52 -0700606 }
607 return insertIndex;
608}
609
610void RecordingCanvas::refBitmapsInShader(const SkShader* shader) {
611 if (!shader) return;
612
613 // If this paint has an SkShader that has an SkBitmap add
614 // it to the bitmap pile
615 SkBitmap bitmap;
616 SkShader::TileMode xy[2];
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400617 if (shader->isABitmap(&bitmap, nullptr, xy)) {
sergeyvec4a4b12016-10-20 18:39:04 -0700618 Bitmap* hwuiBitmap = static_cast<Bitmap*>(bitmap.pixelRef());
619 refBitmap(*hwuiBitmap);
Chris Craikb565df12015-10-05 13:00:52 -0700620 return;
621 }
622 SkShader::ComposeRec rec;
623 if (shader->asACompose(&rec)) {
624 refBitmapsInShader(rec.fShaderA);
625 refBitmapsInShader(rec.fShaderB);
626 return;
627 }
628}
629
John Reck1bcacfd2017-11-03 10:12:19 -0700630}; // namespace uirenderer
631}; // namespace android