1 /*
2  * Copyright (C) 2018 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 
19 #include <GrRecordingContext.h>
20 #include <SkMesh.h>
21 #include <hwui/Paint.h>
22 #include <log/log.h>
23 
24 #include <experimental/type_traits>
25 #include <utility>
26 
27 #include "Mesh.h"
28 #include "SkAndroidFrameworkUtils.h"
29 #include "SkBlendMode.h"
30 #include "SkCanvas.h"
31 #include "SkCanvasPriv.h"
32 #include "SkColor.h"
33 #include "SkData.h"
34 #include "SkDrawShadowInfo.h"
35 #include "SkImage.h"
36 #include "SkImageFilter.h"
37 #include "SkImageInfo.h"
38 #include "SkLatticeIter.h"
39 #include "SkPaint.h"
40 #include "SkPicture.h"
41 #include "SkRRect.h"
42 #include "SkRSXform.h"
43 #include "SkRect.h"
44 #include "SkRegion.h"
45 #include "SkTextBlob.h"
46 #include "SkVertices.h"
47 #include "Tonemapper.h"
48 #include "VectorDrawable.h"
49 #include "effects/GainmapRenderer.h"
50 #include "include/gpu/GpuTypes.h"  // from Skia
51 #include "include/gpu/GrDirectContext.h"
52 #include "pipeline/skia/AnimatedDrawables.h"
53 #include "pipeline/skia/FunctorDrawable.h"
54 #ifdef __ANDROID__
55 #include "renderthread/CanvasContext.h"
56 #endif
57 
58 namespace android {
59 namespace uirenderer {
60 
61 #ifndef SKLITEDL_PAGE
62 #define SKLITEDL_PAGE 4096
63 #endif
64 
65 // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
66 static const SkRect kUnset = {SK_ScalarInfinity, 0, 0, 0};
maybe_unset(const SkRect & r)67 static const SkRect* maybe_unset(const SkRect& r) {
68     return r.left() == SK_ScalarInfinity ? nullptr : &r;
69 }
70 
71 // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.
copy_v(void * dst)72 static void copy_v(void* dst) {}
73 
74 template <typename S, typename... Rest>
copy_v(void * dst,const S * src,int n,Rest &&...rest)75 static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
76     LOG_FATAL_IF(((uintptr_t)dst & (alignof(S) - 1)) != 0,
77                  "Expected %p to be aligned for at least %zu bytes.",
78                  dst, alignof(S));
79     // If n is 0, there is nothing to copy into dst from src.
80     if (n > 0) {
81         memcpy(dst, src, n * sizeof(S));
82         dst = reinterpret_cast<void*>(
83                 reinterpret_cast<uint8_t*>(dst) + n * sizeof(S));
84     }
85     // Repeat for the next items, if any
86     copy_v(dst, std::forward<Rest>(rest)...);
87 }
88 
89 // Helper for getting back at arrays which have been copy_v'd together after an Op.
90 template <typename D, typename T>
pod(const T * op,size_t offset=0)91 static const D* pod(const T* op, size_t offset = 0) {
92     return reinterpret_cast<const D*>(
93                 reinterpret_cast<const uint8_t*>(op + 1) + offset);
94 }
95 
96 namespace {
97 
98 #define X(T) T,
99 enum class Type : uint8_t {
100 #include "DisplayListOps.in"
101 };
102 #undef X
103 
104 struct Op {
105     uint32_t type : 8;
106     uint32_t skip : 24;
107 };
108 static_assert(sizeof(Op) == 4, "");
109 
110 struct Flush final : Op {
111     static const auto kType = Type::Flush;
drawandroid::uirenderer::__anoncf00d6560110::Flush112     void draw(SkCanvas* c, const SkMatrix&) const { c->flush(); }
113 };
114 
115 struct Save final : Op {
116     static const auto kType = Type::Save;
drawandroid::uirenderer::__anoncf00d6560110::Save117     void draw(SkCanvas* c, const SkMatrix&) const { c->save(); }
118 };
119 struct Restore final : Op {
120     static const auto kType = Type::Restore;
drawandroid::uirenderer::__anoncf00d6560110::Restore121     void draw(SkCanvas* c, const SkMatrix&) const { c->restore(); }
122 };
123 struct SaveLayer final : Op {
124     static const auto kType = Type::SaveLayer;
SaveLayerandroid::uirenderer::__anoncf00d6560110::SaveLayer125     SaveLayer(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
126               SkCanvas::SaveLayerFlags flags) {
127         if (bounds) {
128             this->bounds = *bounds;
129         }
130         if (paint) {
131             this->paint = *paint;
132         }
133         this->backdrop = sk_ref_sp(backdrop);
134         this->flags = flags;
135     }
136     SkRect bounds = kUnset;
137     SkPaint paint;
138     sk_sp<const SkImageFilter> backdrop;
139     SkCanvas::SaveLayerFlags flags;
drawandroid::uirenderer::__anoncf00d6560110::SaveLayer140     void draw(SkCanvas* c, const SkMatrix&) const {
141         c->saveLayer({maybe_unset(bounds), &paint, backdrop.get(), flags});
142     }
143 };
144 struct SaveBehind final : Op {
145     static const auto kType = Type::SaveBehind;
SaveBehindandroid::uirenderer::__anoncf00d6560110::SaveBehind146     SaveBehind(const SkRect* subset) {
147         if (subset) { this->subset = *subset; }
148     }
149     SkRect  subset = kUnset;
drawandroid::uirenderer::__anoncf00d6560110::SaveBehind150     void draw(SkCanvas* c, const SkMatrix&) const {
151         SkAndroidFrameworkUtils::SaveBehind(c, &subset);
152     }
153 };
154 
155 struct Concat final : Op {
156     static const auto kType = Type::Concat;
Concatandroid::uirenderer::__anoncf00d6560110::Concat157     Concat(const SkM44& matrix) : matrix(matrix) {}
158     SkM44 matrix;
drawandroid::uirenderer::__anoncf00d6560110::Concat159     void draw(SkCanvas* c, const SkMatrix&) const { c->concat(matrix); }
160 };
161 struct SetMatrix final : Op {
162     static const auto kType = Type::SetMatrix;
SetMatrixandroid::uirenderer::__anoncf00d6560110::SetMatrix163     SetMatrix(const SkM44& matrix) : matrix(matrix) {}
164     SkM44 matrix;
drawandroid::uirenderer::__anoncf00d6560110::SetMatrix165     void draw(SkCanvas* c, const SkMatrix& original) const {
166         c->setMatrix(SkM44(original) * matrix);
167     }
168 };
169 struct Scale final : Op {
170     static const auto kType = Type::Scale;
Scaleandroid::uirenderer::__anoncf00d6560110::Scale171     Scale(SkScalar sx, SkScalar sy) : sx(sx), sy(sy) {}
172     SkScalar sx, sy;
drawandroid::uirenderer::__anoncf00d6560110::Scale173     void draw(SkCanvas* c, const SkMatrix&) const { c->scale(sx, sy); }
174 };
175 struct Translate final : Op {
176     static const auto kType = Type::Translate;
Translateandroid::uirenderer::__anoncf00d6560110::Translate177     Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
178     SkScalar dx, dy;
drawandroid::uirenderer::__anoncf00d6560110::Translate179     void draw(SkCanvas* c, const SkMatrix&) const { c->translate(dx, dy); }
180 };
181 
182 struct ClipPath final : Op {
183     static const auto kType = Type::ClipPath;
ClipPathandroid::uirenderer::__anoncf00d6560110::ClipPath184     ClipPath(const SkPath& path, SkClipOp op, bool aa) : path(path), op(op), aa(aa) {}
185     SkPath path;
186     SkClipOp op;
187     bool aa;
drawandroid::uirenderer::__anoncf00d6560110::ClipPath188     void draw(SkCanvas* c, const SkMatrix&) const { c->clipPath(path, op, aa); }
189 };
190 struct ClipRect final : Op {
191     static const auto kType = Type::ClipRect;
ClipRectandroid::uirenderer::__anoncf00d6560110::ClipRect192     ClipRect(const SkRect& rect, SkClipOp op, bool aa) : rect(rect), op(op), aa(aa) {}
193     SkRect rect;
194     SkClipOp op;
195     bool aa;
drawandroid::uirenderer::__anoncf00d6560110::ClipRect196     void draw(SkCanvas* c, const SkMatrix&) const { c->clipRect(rect, op, aa); }
197 };
198 struct ClipRRect final : Op {
199     static const auto kType = Type::ClipRRect;
ClipRRectandroid::uirenderer::__anoncf00d6560110::ClipRRect200     ClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) : rrect(rrect), op(op), aa(aa) {}
201     SkRRect rrect;
202     SkClipOp op;
203     bool aa;
drawandroid::uirenderer::__anoncf00d6560110::ClipRRect204     void draw(SkCanvas* c, const SkMatrix&) const { c->clipRRect(rrect, op, aa); }
205 };
206 struct ClipRegion final : Op {
207     static const auto kType = Type::ClipRegion;
ClipRegionandroid::uirenderer::__anoncf00d6560110::ClipRegion208     ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
209     SkRegion region;
210     SkClipOp op;
drawandroid::uirenderer::__anoncf00d6560110::ClipRegion211     void draw(SkCanvas* c, const SkMatrix&) const { c->clipRegion(region, op); }
212 };
213 struct ResetClip final : Op {
214     static const auto kType = Type::ResetClip;
ResetClipandroid::uirenderer::__anoncf00d6560110::ResetClip215     ResetClip() {}
drawandroid::uirenderer::__anoncf00d6560110::ResetClip216     void draw(SkCanvas* c, const SkMatrix&) const { SkAndroidFrameworkUtils::ResetClip(c); }
217 };
218 
219 struct DrawPaint final : Op {
220     static const auto kType = Type::DrawPaint;
DrawPaintandroid::uirenderer::__anoncf00d6560110::DrawPaint221     DrawPaint(const SkPaint& paint) : paint(paint) {}
222     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawPaint223     void draw(SkCanvas* c, const SkMatrix&) const { c->drawPaint(paint); }
224 };
225 struct DrawBehind final : Op {
226     static const auto kType = Type::DrawBehind;
DrawBehindandroid::uirenderer::__anoncf00d6560110::DrawBehind227     DrawBehind(const SkPaint& paint) : paint(paint) {}
228     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawBehind229     void draw(SkCanvas* c, const SkMatrix&) const { SkCanvasPriv::DrawBehind(c, paint); }
230 };
231 struct DrawPath final : Op {
232     static const auto kType = Type::DrawPath;
DrawPathandroid::uirenderer::__anoncf00d6560110::DrawPath233     DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
234     SkPath path;
235     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawPath236     void draw(SkCanvas* c, const SkMatrix&) const { c->drawPath(path, paint); }
237 };
238 struct DrawRect final : Op {
239     static const auto kType = Type::DrawRect;
DrawRectandroid::uirenderer::__anoncf00d6560110::DrawRect240     DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
241     SkRect rect;
242     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawRect243     void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
244 };
245 struct DrawRegion final : Op {
246     static const auto kType = Type::DrawRegion;
DrawRegionandroid::uirenderer::__anoncf00d6560110::DrawRegion247     DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
248     SkRegion region;
249     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawRegion250     void draw(SkCanvas* c, const SkMatrix&) const { c->drawRegion(region, paint); }
251 };
252 struct DrawOval final : Op {
253     static const auto kType = Type::DrawOval;
DrawOvalandroid::uirenderer::__anoncf00d6560110::DrawOval254     DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
255     SkRect oval;
256     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawOval257     void draw(SkCanvas* c, const SkMatrix&) const { c->drawOval(oval, paint); }
258 };
259 struct DrawArc final : Op {
260     static const auto kType = Type::DrawArc;
DrawArcandroid::uirenderer::__anoncf00d6560110::DrawArc261     DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
262             const SkPaint& paint)
263             : oval(oval)
264             , startAngle(startAngle)
265             , sweepAngle(sweepAngle)
266             , useCenter(useCenter)
267             , paint(paint) {}
268     SkRect oval;
269     SkScalar startAngle;
270     SkScalar sweepAngle;
271     bool useCenter;
272     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawArc273     void draw(SkCanvas* c, const SkMatrix&) const {
274         c->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
275     }
276 };
277 struct DrawRRect final : Op {
278     static const auto kType = Type::DrawRRect;
DrawRRectandroid::uirenderer::__anoncf00d6560110::DrawRRect279     DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
280     SkRRect rrect;
281     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawRRect282     void draw(SkCanvas* c, const SkMatrix&) const { c->drawRRect(rrect, paint); }
283 };
284 struct DrawDRRect final : Op {
285     static const auto kType = Type::DrawDRRect;
DrawDRRectandroid::uirenderer::__anoncf00d6560110::DrawDRRect286     DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
287             : outer(outer), inner(inner), paint(paint) {}
288     SkRRect outer, inner;
289     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawDRRect290     void draw(SkCanvas* c, const SkMatrix&) const { c->drawDRRect(outer, inner, paint); }
291 };
292 struct DrawAnnotation final : Op {
293     static const auto kType = Type::DrawAnnotation;
DrawAnnotationandroid::uirenderer::__anoncf00d6560110::DrawAnnotation294     DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
295     SkRect rect;
296     sk_sp<SkData> value;
drawandroid::uirenderer::__anoncf00d6560110::DrawAnnotation297     void draw(SkCanvas* c, const SkMatrix&) const {
298         c->drawAnnotation(rect, pod<char>(this), value.get());
299     }
300 };
301 struct DrawDrawable final : Op {
302     static const auto kType = Type::DrawDrawable;
DrawDrawableandroid::uirenderer::__anoncf00d6560110::DrawDrawable303     DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
304         if (matrix) {
305             this->matrix = *matrix;
306         }
307     }
308     sk_sp<SkDrawable> drawable;
309     SkMatrix matrix = SkMatrix::I();
310     // It is important that we call drawable->draw(c) here instead of c->drawDrawable(drawable).
311     // Drawables are mutable and in cases, like RenderNodeDrawable, are not expected to produce the
312     // same content if retained outside the duration of the frame. Therefore we resolve
313     // them now and do not allow the canvas to take a reference to the drawable and potentially
314     // keep it alive for longer than the frames duration (e.g. SKP serialization).
drawandroid::uirenderer::__anoncf00d6560110::DrawDrawable315     void draw(SkCanvas* c, const SkMatrix&) const { drawable->draw(c, &matrix); }
316 };
317 struct DrawPicture final : Op {
318     static const auto kType = Type::DrawPicture;
DrawPictureandroid::uirenderer::__anoncf00d6560110::DrawPicture319     DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
320             : picture(sk_ref_sp(picture)) {
321         if (matrix) {
322             this->matrix = *matrix;
323         }
324         if (paint) {
325             this->paint = *paint;
326             has_paint = true;
327         }
328     }
329     sk_sp<const SkPicture> picture;
330     SkMatrix matrix = SkMatrix::I();
331     SkPaint paint;
332     bool has_paint = false;  // TODO: why is a default paint not the same?
drawandroid::uirenderer::__anoncf00d6560110::DrawPicture333     void draw(SkCanvas* c, const SkMatrix&) const {
334         c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
335     }
336 };
337 
338 struct DrawImage final : Op {
339     static const auto kType = Type::DrawImage;
DrawImageandroid::uirenderer::__anoncf00d6560110::DrawImage340     DrawImage(DrawImagePayload&& payload, SkScalar x, SkScalar y, const SkSamplingOptions& sampling,
341               const SkPaint* paint)
342             : image(std::move(payload.image))
343             , x(x)
344             , y(y)
345             , sampling(sampling)
346             , palette(payload.palette)
347             , gainmap(std::move(payload.gainmapImage))
348             , gainmapInfo(payload.gainmapInfo) {
349         if (paint) {
350             this->paint = *paint;
351         }
352     }
353     sk_sp<const SkImage> image;
354     SkScalar x, y;
355     SkSamplingOptions sampling;
356     SkPaint paint;
357     BitmapPalette palette;
358     sk_sp<const SkImage> gainmap;
359     SkGainmapInfo gainmapInfo;
360 
drawandroid::uirenderer::__anoncf00d6560110::DrawImage361     void draw(SkCanvas* c, const SkMatrix&) const {
362         if (gainmap) {
363             SkRect src = SkRect::MakeWH(image->width(), image->height());
364             SkRect dst = SkRect::MakeXYWH(x, y, src.width(), src.height());
365             DrawGainmapBitmap(c, image, src, dst, sampling, &paint,
366                               SkCanvas::kFast_SrcRectConstraint, gainmap, gainmapInfo);
367         } else {
368             SkPaint newPaint = paint;
369             tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
370             c->drawImage(image.get(), x, y, sampling, &newPaint);
371         }
372     }
373 };
374 struct DrawImageRect final : Op {
375     static const auto kType = Type::DrawImageRect;
DrawImageRectandroid::uirenderer::__anoncf00d6560110::DrawImageRect376     DrawImageRect(DrawImagePayload&& payload, const SkRect* src, const SkRect& dst,
377                   const SkSamplingOptions& sampling, const SkPaint* paint,
378                   SkCanvas::SrcRectConstraint constraint)
379             : image(std::move(payload.image))
380             , dst(dst)
381             , sampling(sampling)
382             , constraint(constraint)
383             , palette(payload.palette)
384             , gainmap(std::move(payload.gainmapImage))
385             , gainmapInfo(payload.gainmapInfo) {
386         this->src = src ? *src : SkRect::MakeIWH(this->image->width(), this->image->height());
387         if (paint) {
388             this->paint = *paint;
389         }
390     }
391     sk_sp<const SkImage> image;
392     SkRect src, dst;
393     SkSamplingOptions sampling;
394     SkPaint paint;
395     SkCanvas::SrcRectConstraint constraint;
396     BitmapPalette palette;
397     sk_sp<const SkImage> gainmap;
398     SkGainmapInfo gainmapInfo;
399 
drawandroid::uirenderer::__anoncf00d6560110::DrawImageRect400     void draw(SkCanvas* c, const SkMatrix&) const {
401         if (gainmap) {
402             DrawGainmapBitmap(c, image, src, dst, sampling, &paint, constraint, gainmap,
403                               gainmapInfo);
404         } else {
405             SkPaint newPaint = paint;
406             tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
407             c->drawImageRect(image.get(), src, dst, sampling, &newPaint, constraint);
408         }
409     }
410 };
411 struct DrawImageLattice final : Op {
412     static const auto kType = Type::DrawImageLattice;
DrawImageLatticeandroid::uirenderer::__anoncf00d6560110::DrawImageLattice413     DrawImageLattice(DrawImagePayload&& payload, int xs, int ys, int fs, const SkIRect& src,
414                      const SkRect& dst, SkFilterMode filter, const SkPaint* paint)
415             : image(std::move(payload.image))
416             , xs(xs)
417             , ys(ys)
418             , fs(fs)
419             , src(src)
420             , dst(dst)
421             , filter(filter)
422             , palette(payload.palette) {
423         if (paint) {
424             this->paint = *paint;
425         }
426     }
427     sk_sp<const SkImage> image;
428     int xs, ys, fs;
429     SkIRect src;
430     SkRect dst;
431     SkFilterMode filter;
432     SkPaint paint;
433     BitmapPalette palette;
drawandroid::uirenderer::__anoncf00d6560110::DrawImageLattice434     void draw(SkCanvas* c, const SkMatrix&) const {
435         // TODO: Support drawing a gainmap 9-patch?
436 
437         auto xdivs = pod<int>(this, 0), ydivs = pod<int>(this, xs * sizeof(int));
438         auto colors = (0 == fs) ? nullptr : pod<SkColor>(this, (xs + ys) * sizeof(int));
439         auto flags =
440                 (0 == fs) ? nullptr : pod<SkCanvas::Lattice::RectType>(
441                                               this, (xs + ys) * sizeof(int) + fs * sizeof(SkColor));
442         SkPaint newPaint = paint;
443         tonemapPaint(image->imageInfo(), c->imageInfo(), -1, newPaint);
444         c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst, filter,
445                             &newPaint);
446     }
447 };
448 
449 struct DrawTextBlob final : Op {
450     static const auto kType = Type::DrawTextBlob;
DrawTextBlobandroid::uirenderer::__anoncf00d6560110::DrawTextBlob451     DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
452         : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint), drawTextBlobMode(gDrawTextBlobMode) {}
453     sk_sp<const SkTextBlob> blob;
454     SkScalar x, y;
455     SkPaint paint;
456     DrawTextBlobMode drawTextBlobMode;
drawandroid::uirenderer::__anoncf00d6560110::DrawTextBlob457     void draw(SkCanvas* c, const SkMatrix&) const { c->drawTextBlob(blob.get(), x, y, paint); }
458 };
459 
460 struct DrawPatch final : Op {
461     static const auto kType = Type::DrawPatch;
DrawPatchandroid::uirenderer::__anoncf00d6560110::DrawPatch462     DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
463               SkBlendMode bmode, const SkPaint& paint)
464             : xfermode(bmode), paint(paint) {
465         copy_v(this->cubics, cubics, 12);
466         if (colors) {
467             copy_v(this->colors, colors, 4);
468             has_colors = true;
469         }
470         if (texs) {
471             copy_v(this->texs, texs, 4);
472             has_texs = true;
473         }
474     }
475     SkPoint cubics[12];
476     SkColor colors[4];
477     SkPoint texs[4];
478     SkBlendMode xfermode;
479     SkPaint paint;
480     bool has_colors = false;
481     bool has_texs = false;
drawandroid::uirenderer::__anoncf00d6560110::DrawPatch482     void draw(SkCanvas* c, const SkMatrix&) const {
483         c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr, xfermode,
484                      paint);
485     }
486 };
487 struct DrawPoints final : Op {
488     static const auto kType = Type::DrawPoints;
DrawPointsandroid::uirenderer::__anoncf00d6560110::DrawPoints489     DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
490             : mode(mode), count(count), paint(paint) {}
491     SkCanvas::PointMode mode;
492     size_t count;
493     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawPoints494     void draw(SkCanvas* c, const SkMatrix&) const {
495         if (paint.isAntiAlias()) {
496             c->drawPoints(mode, count, pod<SkPoint>(this), paint);
497         } else {
498             c->save();
499 #ifdef __ANDROID__
500             auto pixelSnap = renderthread::CanvasContext::getActiveContext()->getPixelSnapMatrix();
501             auto transform = c->getLocalToDevice();
502             transform.postConcat(pixelSnap);
503             c->setMatrix(transform);
504 #endif
505             c->drawPoints(mode, count, pod<SkPoint>(this), paint);
506             c->restore();
507         }
508     }
509 };
510 struct DrawVertices final : Op {
511     static const auto kType = Type::DrawVertices;
DrawVerticesandroid::uirenderer::__anoncf00d6560110::DrawVertices512     DrawVertices(const SkVertices* v, SkBlendMode m, const SkPaint& p)
513             : vertices(sk_ref_sp(const_cast<SkVertices*>(v))), mode(m), paint(p) {}
514     sk_sp<SkVertices> vertices;
515     SkBlendMode mode;
516     SkPaint paint;
drawandroid::uirenderer::__anoncf00d6560110::DrawVertices517     void draw(SkCanvas* c, const SkMatrix&) const {
518         c->drawVertices(vertices, mode, paint);
519     }
520 };
521 struct DrawSkMesh final : Op {
522     static const auto kType = Type::DrawSkMesh;
DrawSkMeshandroid::uirenderer::__anoncf00d6560110::DrawSkMesh523     DrawSkMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint)
524             : cpuMesh(mesh), blender(std::move(blender)), paint(paint) {
525         isGpuBased = false;
526     }
527 
528     const SkMesh& cpuMesh;
529     mutable SkMesh gpuMesh;
530     sk_sp<SkBlender> blender;
531     SkPaint paint;
532     mutable bool isGpuBased;
533     mutable GrDirectContext::DirectContextID contextId;
drawandroid::uirenderer::__anoncf00d6560110::DrawSkMesh534     void draw(SkCanvas* c, const SkMatrix&) const {
535         GrDirectContext* directContext = c->recordingContext()->asDirectContext();
536 
537         GrDirectContext::DirectContextID id = directContext->directContextID();
538         if (!isGpuBased || contextId != id) {
539             sk_sp<SkMesh::VertexBuffer> vb =
540                     SkMesh::CopyVertexBuffer(directContext, cpuMesh.refVertexBuffer());
541             if (!cpuMesh.indexBuffer()) {
542                 gpuMesh = SkMesh::Make(cpuMesh.refSpec(), cpuMesh.mode(), vb, cpuMesh.vertexCount(),
543                                        cpuMesh.vertexOffset(), cpuMesh.refUniforms(),
544                                        cpuMesh.bounds())
545                                   .mesh;
546             } else {
547                 sk_sp<SkMesh::IndexBuffer> ib =
548                         SkMesh::CopyIndexBuffer(directContext, cpuMesh.refIndexBuffer());
549                 gpuMesh = SkMesh::MakeIndexed(cpuMesh.refSpec(), cpuMesh.mode(), vb,
550                                               cpuMesh.vertexCount(), cpuMesh.vertexOffset(), ib,
551                                               cpuMesh.indexCount(), cpuMesh.indexOffset(),
552                                               cpuMesh.refUniforms(), cpuMesh.bounds())
553                                   .mesh;
554             }
555 
556             isGpuBased = true;
557             contextId = id;
558         }
559 
560         c->drawMesh(gpuMesh, blender, paint);
561     }
562 };
563 
564 struct DrawMesh final : Op {
565     static const auto kType = Type::DrawMesh;
DrawMeshandroid::uirenderer::__anoncf00d6560110::DrawMesh566     DrawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint)
567             : mesh(mesh), blender(std::move(blender)), paint(paint) {}
568 
569     const Mesh& mesh;
570     sk_sp<SkBlender> blender;
571     SkPaint paint;
572 
drawandroid::uirenderer::__anoncf00d6560110::DrawMesh573     void draw(SkCanvas* c, const SkMatrix&) const { c->drawMesh(mesh.getSkMesh(), blender, paint); }
574 };
575 struct DrawAtlas final : Op {
576     static const auto kType = Type::DrawAtlas;
DrawAtlasandroid::uirenderer::__anoncf00d6560110::DrawAtlas577     DrawAtlas(const SkImage* atlas, int count, SkBlendMode mode, const SkSamplingOptions& sampling,
578               const SkRect* cull, const SkPaint* paint, bool has_colors)
579             : atlas(sk_ref_sp(atlas)), count(count), mode(mode), sampling(sampling)
580             , has_colors(has_colors) {
581         if (cull) {
582             this->cull = *cull;
583         }
584         if (paint) {
585             this->paint = *paint;
586         }
587     }
588     sk_sp<const SkImage> atlas;
589     int count;
590     SkBlendMode mode;
591     SkSamplingOptions sampling;
592     SkRect cull = kUnset;
593     SkPaint paint;
594     bool has_colors;
drawandroid::uirenderer::__anoncf00d6560110::DrawAtlas595     void draw(SkCanvas* c, const SkMatrix&) const {
596         auto xforms = pod<SkRSXform>(this, 0);
597         auto texs = pod<SkRect>(this, count * sizeof(SkRSXform));
598         auto colors = has_colors ? pod<SkColor>(this, count * (sizeof(SkRSXform) + sizeof(SkRect)))
599                                  : nullptr;
600         c->drawAtlas(atlas.get(), xforms, texs, colors, count, mode, sampling, maybe_unset(cull),
601                      &paint);
602     }
603 };
604 struct DrawShadowRec final : Op {
605     static const auto kType = Type::DrawShadowRec;
DrawShadowRecandroid::uirenderer::__anoncf00d6560110::DrawShadowRec606     DrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) : fPath(path), fRec(rec) {}
607     SkPath fPath;
608     SkDrawShadowRec fRec;
drawandroid::uirenderer::__anoncf00d6560110::DrawShadowRec609     void draw(SkCanvas* c, const SkMatrix&) const { c->private_draw_shadow_rec(fPath, fRec); }
610 };
611 
612 struct DrawVectorDrawable final : Op {
613     static const auto kType = Type::DrawVectorDrawable;
DrawVectorDrawableandroid::uirenderer::__anoncf00d6560110::DrawVectorDrawable614     DrawVectorDrawable(VectorDrawableRoot* tree)
615             : mRoot(tree)
616             , mBounds(tree->stagingProperties().getBounds())
617             , palette(tree->computePalette()) {
618         // Recording, so use staging properties
619         tree->getPaintFor(&paint, tree->stagingProperties());
620     }
621 
drawandroid::uirenderer::__anoncf00d6560110::DrawVectorDrawable622     void draw(SkCanvas* canvas, const SkMatrix&) const {
623         mRoot->draw(canvas, mBounds, paint);
624     }
625 
626     sp<VectorDrawableRoot> mRoot;
627     SkRect mBounds;
628     Paint paint;
629     BitmapPalette palette;
630 };
631 
632 struct DrawRippleDrawable final : Op {
633     static const auto kType = Type::DrawRippleDrawable;
DrawRippleDrawableandroid::uirenderer::__anoncf00d6560110::DrawRippleDrawable634     DrawRippleDrawable(const skiapipeline::RippleDrawableParams& params) : mParams(params) {}
635 
drawandroid::uirenderer::__anoncf00d6560110::DrawRippleDrawable636     void draw(SkCanvas* canvas, const SkMatrix&) const {
637         skiapipeline::AnimatedRippleDrawable::draw(canvas, mParams);
638     }
639 
640     skiapipeline::RippleDrawableParams mParams;
641 };
642 
643 struct DrawWebView final : Op {
644     static const auto kType = Type::DrawWebView;
DrawWebViewandroid::uirenderer::__anoncf00d6560110::DrawWebView645     DrawWebView(skiapipeline::FunctorDrawable* drawable) : drawable(sk_ref_sp(drawable)) {}
646     sk_sp<skiapipeline::FunctorDrawable> drawable;
647     // We can't invoke SkDrawable::draw directly, because VkFunctorDrawable expects
648     // SkDrawable::onSnapGpuDrawHandler callback instead of SkDrawable::onDraw.
649     // SkCanvas::drawDrawable/SkGpuDevice::drawDrawable has the logic to invoke
650     // onSnapGpuDrawHandler.
651 private:
652     // Unfortunately WebView does not have complex clip information serialized, and we only perform
653     // best-effort stencil fill for GLES. So for Vulkan we create an intermediate layer if the
654     // canvas clip is complex.
needsCompositedLayerandroid::uirenderer::__anoncf00d6560110::DrawWebView655     static bool needsCompositedLayer(SkCanvas* c) {
656         if (Properties::getRenderPipelineType() != RenderPipelineType::SkiaVulkan) {
657             return false;
658         }
659         SkRegion clipRegion;
660         // WebView's rasterizer has access to simple clips, so for Vulkan we only need to check if
661         // the clip is more complex than a rectangle.
662         c->temporary_internal_getRgnClip(&clipRegion);
663         return clipRegion.isComplex();
664     }
665 
666     mutable SkImageInfo mLayerImageInfo;
667     mutable sk_sp<SkSurface> mLayerSurface = nullptr;
668 
669 public:
drawandroid::uirenderer::__anoncf00d6560110::DrawWebView670     void draw(SkCanvas* c, const SkMatrix&) const {
671         if (needsCompositedLayer(c)) {
672             // What we do now is create an offscreen surface, sized by the clip bounds.
673             // We won't apply a clip while drawing - clipping will be performed when compositing the
674             // surface back onto the original canvas. Note also that we're not using saveLayer
675             // because the webview functor still doesn't respect the canvas clip stack.
676             const SkIRect deviceBounds = c->getDeviceClipBounds();
677             if (mLayerSurface == nullptr || c->imageInfo() != mLayerImageInfo) {
678                 GrRecordingContext* directContext = c->recordingContext();
679                 mLayerImageInfo =
680                         c->imageInfo().makeWH(deviceBounds.width(), deviceBounds.height());
681                 mLayerSurface = SkSurface::MakeRenderTarget(directContext, skgpu::Budgeted::kYes,
682                                                             mLayerImageInfo, 0,
683                                                             kTopLeft_GrSurfaceOrigin, nullptr);
684             }
685 
686             SkCanvas* layerCanvas = mLayerSurface->getCanvas();
687 
688             SkAutoCanvasRestore(layerCanvas, true);
689             layerCanvas->clear(SK_ColorTRANSPARENT);
690 
691             // Preserve the transform from the original canvas, but now the clip rectangle is
692             // anchored at the origin so we need to transform the clipped content to the origin.
693             SkM44 mat4(c->getLocalToDevice());
694             mat4.postTranslate(-deviceBounds.fLeft, -deviceBounds.fTop);
695             layerCanvas->concat(mat4);
696             layerCanvas->drawDrawable(drawable.get());
697 
698             SkAutoCanvasRestore acr(c, true);
699 
700             // Temporarily use an identity transform, because this is just blitting to the parent
701             // canvas with an offset.
702             SkMatrix invertedMatrix;
703             if (!c->getTotalMatrix().invert(&invertedMatrix)) {
704                 ALOGW("Unable to extract invert canvas matrix; aborting VkFunctor draw");
705                 return;
706             }
707             c->concat(invertedMatrix);
708             mLayerSurface->draw(c, deviceBounds.fLeft, deviceBounds.fTop);
709         } else {
710             c->drawDrawable(drawable.get());
711         }
712     }
713 };
714 }
715 
is_power_of_two(int value)716 static constexpr inline bool is_power_of_two(int value) {
717     return (value & (value - 1)) == 0;
718 }
719 
720 template <typename T, typename... Args>
push(size_t pod,Args &&...args)721 void* DisplayListData::push(size_t pod, Args&&... args) {
722     size_t skip = SkAlignPtr(sizeof(T) + pod);
723     LOG_FATAL_IF(skip >= (1 << 24));
724     if (fUsed + skip > fReserved) {
725         static_assert(is_power_of_two(SKLITEDL_PAGE),
726                       "This math needs updating for non-pow2.");
727         // Next greater multiple of SKLITEDL_PAGE.
728         fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE - 1);
729         fBytes.realloc(fReserved);
730         LOG_ALWAYS_FATAL_IF(fBytes.get() == nullptr, "realloc(%zd) failed", fReserved);
731     }
732     LOG_FATAL_IF((fUsed + skip) > fReserved);
733     auto op = (T*)(fBytes.get() + fUsed);
734     fUsed += skip;
735     new (op) T{std::forward<Args>(args)...};
736     op->type = (uint32_t)T::kType;
737     op->skip = skip;
738     return op + 1;
739 }
740 
741 template <typename Fn, typename... Args>
map(const Fn fns[],Args...args) const742 inline void DisplayListData::map(const Fn fns[], Args... args) const {
743     auto end = fBytes.get() + fUsed;
744     for (const uint8_t* ptr = fBytes.get(); ptr < end;) {
745         auto op = (const Op*)ptr;
746         auto type = op->type;
747         auto skip = op->skip;
748         if (auto fn = fns[type]) {  // We replace no-op functions with nullptrs
749             fn(op, args...);        // to avoid the overhead of a pointless call.
750         }
751         ptr += skip;
752     }
753 }
754 
flush()755 void DisplayListData::flush() {
756     this->push<Flush>(0);
757 }
758 
save()759 void DisplayListData::save() {
760     this->push<Save>(0);
761 }
restore()762 void DisplayListData::restore() {
763     this->push<Restore>(0);
764 }
saveLayer(const SkRect * bounds,const SkPaint * paint,const SkImageFilter * backdrop,SkCanvas::SaveLayerFlags flags)765 void DisplayListData::saveLayer(const SkRect* bounds, const SkPaint* paint,
766                                 const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) {
767     this->push<SaveLayer>(0, bounds, paint, backdrop, flags);
768 }
769 
saveBehind(const SkRect * subset)770 void DisplayListData::saveBehind(const SkRect* subset) {
771     this->push<SaveBehind>(0, subset);
772 }
773 
concat(const SkM44 & m)774 void DisplayListData::concat(const SkM44& m) {
775     this->push<Concat>(0, m);
776 }
setMatrix(const SkM44 & matrix)777 void DisplayListData::setMatrix(const SkM44& matrix) {
778     this->push<SetMatrix>(0, matrix);
779 }
scale(SkScalar sx,SkScalar sy)780 void DisplayListData::scale(SkScalar sx, SkScalar sy) {
781     this->push<Scale>(0, sx, sy);
782 }
translate(SkScalar dx,SkScalar dy)783 void DisplayListData::translate(SkScalar dx, SkScalar dy) {
784     this->push<Translate>(0, dx, dy);
785 }
786 
clipPath(const SkPath & path,SkClipOp op,bool aa)787 void DisplayListData::clipPath(const SkPath& path, SkClipOp op, bool aa) {
788     this->push<ClipPath>(0, path, op, aa);
789 }
clipRect(const SkRect & rect,SkClipOp op,bool aa)790 void DisplayListData::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
791     this->push<ClipRect>(0, rect, op, aa);
792 }
clipRRect(const SkRRect & rrect,SkClipOp op,bool aa)793 void DisplayListData::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
794     this->push<ClipRRect>(0, rrect, op, aa);
795 }
clipRegion(const SkRegion & region,SkClipOp op)796 void DisplayListData::clipRegion(const SkRegion& region, SkClipOp op) {
797     this->push<ClipRegion>(0, region, op);
798 }
resetClip()799 void DisplayListData::resetClip() {
800     this->push<ResetClip>(0);
801 }
802 
drawPaint(const SkPaint & paint)803 void DisplayListData::drawPaint(const SkPaint& paint) {
804     this->push<DrawPaint>(0, paint);
805 }
drawBehind(const SkPaint & paint)806 void DisplayListData::drawBehind(const SkPaint& paint) {
807     this->push<DrawBehind>(0, paint);
808 }
drawPath(const SkPath & path,const SkPaint & paint)809 void DisplayListData::drawPath(const SkPath& path, const SkPaint& paint) {
810     this->push<DrawPath>(0, path, paint);
811 }
drawRect(const SkRect & rect,const SkPaint & paint)812 void DisplayListData::drawRect(const SkRect& rect, const SkPaint& paint) {
813     this->push<DrawRect>(0, rect, paint);
814 }
drawRegion(const SkRegion & region,const SkPaint & paint)815 void DisplayListData::drawRegion(const SkRegion& region, const SkPaint& paint) {
816     this->push<DrawRegion>(0, region, paint);
817 }
drawOval(const SkRect & oval,const SkPaint & paint)818 void DisplayListData::drawOval(const SkRect& oval, const SkPaint& paint) {
819     this->push<DrawOval>(0, oval, paint);
820 }
drawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)821 void DisplayListData::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
822                               bool useCenter, const SkPaint& paint) {
823     this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
824 }
drawRRect(const SkRRect & rrect,const SkPaint & paint)825 void DisplayListData::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
826     this->push<DrawRRect>(0, rrect, paint);
827 }
drawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)828 void DisplayListData::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
829     this->push<DrawDRRect>(0, outer, inner, paint);
830 }
831 
drawAnnotation(const SkRect & rect,const char * key,SkData * value)832 void DisplayListData::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
833     size_t bytes = strlen(key) + 1;
834     void* pod = this->push<DrawAnnotation>(bytes, rect, value);
835     copy_v(pod, key, bytes);
836 }
drawDrawable(SkDrawable * drawable,const SkMatrix * matrix)837 void DisplayListData::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
838     this->push<DrawDrawable>(0, drawable, matrix);
839 }
drawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)840 void DisplayListData::drawPicture(const SkPicture* picture, const SkMatrix* matrix,
841                                   const SkPaint* paint) {
842     this->push<DrawPicture>(0, picture, matrix, paint);
843 }
drawImage(DrawImagePayload && payload,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)844 void DisplayListData::drawImage(DrawImagePayload&& payload, SkScalar x, SkScalar y,
845                                 const SkSamplingOptions& sampling, const SkPaint* paint) {
846     this->push<DrawImage>(0, std::move(payload), x, y, sampling, paint);
847 }
drawImageRect(DrawImagePayload && payload,const SkRect * src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SkCanvas::SrcRectConstraint constraint)848 void DisplayListData::drawImageRect(DrawImagePayload&& payload, const SkRect* src,
849                                     const SkRect& dst, const SkSamplingOptions& sampling,
850                                     const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) {
851     this->push<DrawImageRect>(0, std::move(payload), src, dst, sampling, paint, constraint);
852 }
drawImageLattice(DrawImagePayload && payload,const SkCanvas::Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)853 void DisplayListData::drawImageLattice(DrawImagePayload&& payload, const SkCanvas::Lattice& lattice,
854                                        const SkRect& dst, SkFilterMode filter,
855                                        const SkPaint* paint) {
856     int xs = lattice.fXCount, ys = lattice.fYCount;
857     int fs = lattice.fRectTypes ? (xs + 1) * (ys + 1) : 0;
858     size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType) +
859                    fs * sizeof(SkColor);
860     LOG_FATAL_IF(!lattice.fBounds);
861     void* pod = this->push<DrawImageLattice>(bytes, std::move(payload), xs, ys, fs,
862                                              *lattice.fBounds, dst, filter, paint);
863     copy_v(pod, lattice.fXDivs, xs, lattice.fYDivs, ys, lattice.fColors, fs, lattice.fRectTypes,
864            fs);
865 }
866 
drawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)867 void DisplayListData::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
868                                    const SkPaint& paint) {
869     this->push<DrawTextBlob>(0, blob, x, y, paint);
870     mHasText = true;
871 }
872 
drawRippleDrawable(const skiapipeline::RippleDrawableParams & params)873 void DisplayListData::drawRippleDrawable(const skiapipeline::RippleDrawableParams& params) {
874     this->push<DrawRippleDrawable>(0, params);
875 }
876 
drawPatch(const SkPoint points[12],const SkColor colors[4],const SkPoint texs[4],SkBlendMode bmode,const SkPaint & paint)877 void DisplayListData::drawPatch(const SkPoint points[12], const SkColor colors[4],
878                                 const SkPoint texs[4], SkBlendMode bmode, const SkPaint& paint) {
879     this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
880 }
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint points[],const SkPaint & paint)881 void DisplayListData::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
882                                  const SkPaint& paint) {
883     void* pod = this->push<DrawPoints>(count * sizeof(SkPoint), mode, count, paint);
884     copy_v(pod, points, count);
885 }
drawVertices(const SkVertices * vert,SkBlendMode mode,const SkPaint & paint)886 void DisplayListData::drawVertices(const SkVertices* vert, SkBlendMode mode, const SkPaint& paint) {
887     this->push<DrawVertices>(0, vert, mode, paint);
888 }
drawMesh(const SkMesh & mesh,const sk_sp<SkBlender> & blender,const SkPaint & paint)889 void DisplayListData::drawMesh(const SkMesh& mesh, const sk_sp<SkBlender>& blender,
890                                const SkPaint& paint) {
891     this->push<DrawSkMesh>(0, mesh, blender, paint);
892 }
drawMesh(const Mesh & mesh,const sk_sp<SkBlender> & blender,const SkPaint & paint)893 void DisplayListData::drawMesh(const Mesh& mesh, const sk_sp<SkBlender>& blender,
894                                const SkPaint& paint) {
895     this->push<DrawMesh>(0, mesh, blender, paint);
896 }
drawAtlas(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode xfermode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)897 void DisplayListData::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
898                                 const SkColor colors[], int count, SkBlendMode xfermode,
899                                 const SkSamplingOptions& sampling, const SkRect* cull,
900                                 const SkPaint* paint) {
901     size_t bytes = count * (sizeof(SkRSXform) + sizeof(SkRect));
902     if (colors) {
903         bytes += count * sizeof(SkColor);
904     }
905     void* pod = this->push<DrawAtlas>(bytes, atlas, count, xfermode, sampling, cull, paint,
906                                       colors != nullptr);
907     copy_v(pod, xforms, count, texs, count, colors, colors ? count : 0);
908 }
drawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)909 void DisplayListData::drawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
910     this->push<DrawShadowRec>(0, path, rec);
911 }
drawVectorDrawable(VectorDrawableRoot * tree)912 void DisplayListData::drawVectorDrawable(VectorDrawableRoot* tree) {
913     this->push<DrawVectorDrawable>(0, tree);
914 }
drawWebView(skiapipeline::FunctorDrawable * drawable)915 void DisplayListData::drawWebView(skiapipeline::FunctorDrawable* drawable) {
916     this->push<DrawWebView>(0, drawable);
917 }
918 
919 typedef void (*draw_fn)(const void*, SkCanvas*, const SkMatrix&);
920 typedef void (*void_fn)(const void*);
921 typedef void (*color_transform_fn)(const void*, ColorTransform);
922 
923 // All ops implement draw().
924 #define X(T)                                                    \
925     [](const void* op, SkCanvas* c, const SkMatrix& original) { \
926         ((const T*)op)->draw(c, original);                      \
927     },
928 static const draw_fn draw_fns[] = {
929 #include "DisplayListOps.in"
930 };
931 #undef X
932 
933 // Most state ops (matrix, clip, save, restore) have a trivial destructor.
934 #define X(T)                                                                                 \
935     !std::is_trivially_destructible<T>::value ? [](const void* op) { ((const T*)op)->~T(); } \
936                                               : (void_fn) nullptr,
937 
938 static const void_fn dtor_fns[] = {
939 #include "DisplayListOps.in"
940 };
941 #undef X
942 
draw(SkCanvas * canvas) const943 void DisplayListData::draw(SkCanvas* canvas) const {
944     SkAutoCanvasRestore acr(canvas, false);
945     this->map(draw_fns, canvas, canvas->getTotalMatrix());
946 }
947 
~DisplayListData()948 DisplayListData::~DisplayListData() {
949     this->reset();
950 }
951 
reset()952 void DisplayListData::reset() {
953     this->map(dtor_fns);
954 
955     // Leave fBytes and fReserved alone.
956     fUsed = 0;
957 }
958 
959 template <class T>
960 using has_paint_helper = decltype(std::declval<T>().paint);
961 
962 template <class T>
963 constexpr bool has_paint = std::experimental::is_detected_v<has_paint_helper, T>;
964 
965 template <class T>
966 using has_palette_helper = decltype(std::declval<T>().palette);
967 
968 template <class T>
969 constexpr bool has_palette = std::experimental::is_detected_v<has_palette_helper, T>;
970 
971 template <class T>
colorTransformForOp()972 constexpr color_transform_fn colorTransformForOp() {
973     if
974         constexpr(has_paint<T> && has_palette<T>) {
975             // It's a bitmap
976             return [](const void* opRaw, ColorTransform transform) {
977                 // TODO: We should be const. Or not. Or just use a different map
978                 // Unclear, but this is the quick fix
979                 const T* op = reinterpret_cast<const T*>(opRaw);
980                 const SkPaint* paint = &op->paint;
981                 transformPaint(transform, const_cast<SkPaint*>(paint), op->palette);
982             };
983         }
984     else if
985         constexpr(has_paint<T>) {
986             return [](const void* opRaw, ColorTransform transform) {
987                 // TODO: We should be const. Or not. Or just use a different map
988                 // Unclear, but this is the quick fix
989                 const T* op = reinterpret_cast<const T*>(opRaw);
990                 const SkPaint* paint = &op->paint;
991                 transformPaint(transform, const_cast<SkPaint*>(paint));
992             };
993         }
994     else {
995         return nullptr;
996     }
997 }
998 
999 template<>
colorTransformForOp()1000 constexpr color_transform_fn colorTransformForOp<DrawTextBlob>() {
1001     return [](const void *opRaw, ColorTransform transform) {
1002         const DrawTextBlob *op = reinterpret_cast<const DrawTextBlob*>(opRaw);
1003         switch (op->drawTextBlobMode) {
1004         case DrawTextBlobMode::HctOutline:
1005             const_cast<SkPaint&>(op->paint).setColor(SK_ColorBLACK);
1006             break;
1007         case DrawTextBlobMode::HctInner:
1008             const_cast<SkPaint&>(op->paint).setColor(SK_ColorWHITE);
1009             break;
1010         default:
1011             transformPaint(transform, const_cast<SkPaint*>(&(op->paint)));
1012             break;
1013         }
1014     };
1015 }
1016 
1017 template <>
colorTransformForOp()1018 constexpr color_transform_fn colorTransformForOp<DrawRippleDrawable>() {
1019     return [](const void* opRaw, ColorTransform transform) {
1020         const DrawRippleDrawable* op = reinterpret_cast<const DrawRippleDrawable*>(opRaw);
1021         // Ripple drawable needs to contrast against the background, so we need the inverse color.
1022         SkColor color = transformColorInverse(transform, op->mParams.color);
1023         const_cast<DrawRippleDrawable*>(op)->mParams.color = color;
1024     };
1025 }
1026 
1027 #define X(T) colorTransformForOp<T>(),
1028 static const color_transform_fn color_transform_fns[] = {
1029 #include "DisplayListOps.in"
1030 };
1031 #undef X
1032 
applyColorTransform(ColorTransform transform)1033 void DisplayListData::applyColorTransform(ColorTransform transform) {
1034     this->map(color_transform_fns, transform);
1035 }
1036 
RecordingCanvas()1037 RecordingCanvas::RecordingCanvas() : INHERITED(1, 1), fDL(nullptr) {}
1038 
reset(DisplayListData * dl,const SkIRect & bounds)1039 void RecordingCanvas::reset(DisplayListData* dl, const SkIRect& bounds) {
1040     this->resetCanvas(bounds.right(), bounds.bottom());
1041     fDL = dl;
1042     mClipMayBeComplex = false;
1043     mSaveCount = mComplexSaveCount = 0;
1044 }
1045 
onNewSurface(const SkImageInfo &,const SkSurfaceProps &)1046 sk_sp<SkSurface> RecordingCanvas::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
1047     return nullptr;
1048 }
1049 
onFlush()1050 void RecordingCanvas::onFlush() {
1051     fDL->flush();
1052 }
1053 
willSave()1054 void RecordingCanvas::willSave() {
1055     mSaveCount++;
1056     fDL->save();
1057 }
getSaveLayerStrategy(const SaveLayerRec & rec)1058 SkCanvas::SaveLayerStrategy RecordingCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
1059     fDL->saveLayer(rec.fBounds, rec.fPaint, rec.fBackdrop, rec.fSaveLayerFlags);
1060     return SkCanvas::kNoLayer_SaveLayerStrategy;
1061 }
willRestore()1062 void RecordingCanvas::willRestore() {
1063     mSaveCount--;
1064     if (mSaveCount < mComplexSaveCount) {
1065         mClipMayBeComplex = false;
1066         mComplexSaveCount = 0;
1067     }
1068     fDL->restore();
1069 }
1070 
onDoSaveBehind(const SkRect * subset)1071 bool RecordingCanvas::onDoSaveBehind(const SkRect* subset) {
1072     fDL->saveBehind(subset);
1073     return false;
1074 }
1075 
didConcat44(const SkM44 & m)1076 void RecordingCanvas::didConcat44(const SkM44& m) {
1077     fDL->concat(m);
1078 }
didSetM44(const SkM44 & matrix)1079 void RecordingCanvas::didSetM44(const SkM44& matrix) {
1080     fDL->setMatrix(matrix);
1081 }
didScale(SkScalar sx,SkScalar sy)1082 void RecordingCanvas::didScale(SkScalar sx, SkScalar sy) {
1083     fDL->scale(sx, sy);
1084 }
didTranslate(SkScalar dx,SkScalar dy)1085 void RecordingCanvas::didTranslate(SkScalar dx, SkScalar dy) {
1086     fDL->translate(dx, dy);
1087 }
1088 
onClipRect(const SkRect & rect,SkClipOp op,ClipEdgeStyle style)1089 void RecordingCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle style) {
1090     fDL->clipRect(rect, op, style == kSoft_ClipEdgeStyle);
1091     if (!getTotalMatrix().isScaleTranslate()) {
1092         setClipMayBeComplex();
1093     }
1094     this->INHERITED::onClipRect(rect, op, style);
1095 }
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle style)1096 void RecordingCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
1097     if (rrect.getType() > SkRRect::kRect_Type || !getTotalMatrix().isScaleTranslate()) {
1098         setClipMayBeComplex();
1099     }
1100     fDL->clipRRect(rrect, op, style == kSoft_ClipEdgeStyle);
1101     this->INHERITED::onClipRRect(rrect, op, style);
1102 }
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle style)1103 void RecordingCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle style) {
1104     setClipMayBeComplex();
1105     fDL->clipPath(path, op, style == kSoft_ClipEdgeStyle);
1106     this->INHERITED::onClipPath(path, op, style);
1107 }
onClipRegion(const SkRegion & region,SkClipOp op)1108 void RecordingCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
1109     if (region.isComplex() || !getTotalMatrix().isScaleTranslate()) {
1110         setClipMayBeComplex();
1111     }
1112     fDL->clipRegion(region, op);
1113     this->INHERITED::onClipRegion(region, op);
1114 }
onResetClip()1115 void RecordingCanvas::onResetClip() {
1116     // This is part of "replace op" emulation, but rely on the following intersection
1117     // clip to potentially mark the clip as complex. If we are already complex, we do
1118     // not reset the complexity so that we don't break the contract that no higher
1119     // save point has a complex clip when "not complex".
1120     fDL->resetClip();
1121     this->INHERITED::onResetClip();
1122 }
1123 
onDrawPaint(const SkPaint & paint)1124 void RecordingCanvas::onDrawPaint(const SkPaint& paint) {
1125     fDL->drawPaint(paint);
1126 }
onDrawBehind(const SkPaint & paint)1127 void RecordingCanvas::onDrawBehind(const SkPaint& paint) {
1128     fDL->drawBehind(paint);
1129 }
onDrawPath(const SkPath & path,const SkPaint & paint)1130 void RecordingCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
1131     fDL->drawPath(path, paint);
1132 }
onDrawRect(const SkRect & rect,const SkPaint & paint)1133 void RecordingCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
1134     fDL->drawRect(rect, paint);
1135 }
onDrawRegion(const SkRegion & region,const SkPaint & paint)1136 void RecordingCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
1137     fDL->drawRegion(region, paint);
1138 }
onDrawOval(const SkRect & oval,const SkPaint & paint)1139 void RecordingCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
1140     fDL->drawOval(oval, paint);
1141 }
onDrawArc(const SkRect & oval,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)1142 void RecordingCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
1143                                 bool useCenter, const SkPaint& paint) {
1144     fDL->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
1145 }
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)1146 void RecordingCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
1147     fDL->drawRRect(rrect, paint);
1148 }
onDrawDRRect(const SkRRect & out,const SkRRect & in,const SkPaint & paint)1149 void RecordingCanvas::onDrawDRRect(const SkRRect& out, const SkRRect& in, const SkPaint& paint) {
1150     fDL->drawDRRect(out, in, paint);
1151 }
1152 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)1153 void RecordingCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
1154     fDL->drawDrawable(drawable, matrix);
1155 }
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)1156 void RecordingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
1157                                     const SkPaint* paint) {
1158     fDL->drawPicture(picture, matrix, paint);
1159 }
onDrawAnnotation(const SkRect & rect,const char key[],SkData * val)1160 void RecordingCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* val) {
1161     fDL->drawAnnotation(rect, key, val);
1162 }
1163 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)1164 void RecordingCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
1165                                      const SkPaint& paint) {
1166     fDL->drawTextBlob(blob, x, y, paint);
1167 }
1168 
drawRippleDrawable(const skiapipeline::RippleDrawableParams & params)1169 void RecordingCanvas::drawRippleDrawable(const skiapipeline::RippleDrawableParams& params) {
1170     fDL->drawRippleDrawable(params);
1171 }
1172 
drawImage(DrawImagePayload && payload,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)1173 void RecordingCanvas::drawImage(DrawImagePayload&& payload, SkScalar x, SkScalar y,
1174                                 const SkSamplingOptions& sampling, const SkPaint* paint) {
1175     fDL->drawImage(std::move(payload), x, y, sampling, paint);
1176 }
1177 
drawImageRect(DrawImagePayload && payload,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)1178 void RecordingCanvas::drawImageRect(DrawImagePayload&& payload, const SkRect& src,
1179                                     const SkRect& dst, const SkSamplingOptions& sampling,
1180                                     const SkPaint* paint, SrcRectConstraint constraint) {
1181     fDL->drawImageRect(std::move(payload), &src, dst, sampling, paint, constraint);
1182 }
1183 
drawImageLattice(DrawImagePayload && payload,const Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)1184 void RecordingCanvas::drawImageLattice(DrawImagePayload&& payload, const Lattice& lattice,
1185                                        const SkRect& dst, SkFilterMode filter,
1186                                        const SkPaint* paint) {
1187     if (!payload.image || dst.isEmpty()) {
1188         return;
1189     }
1190 
1191     SkIRect bounds;
1192     Lattice latticePlusBounds = lattice;
1193     if (!latticePlusBounds.fBounds) {
1194         bounds = SkIRect::MakeWH(payload.image->width(), payload.image->height());
1195         latticePlusBounds.fBounds = &bounds;
1196     }
1197 
1198     if (SkLatticeIter::Valid(payload.image->width(), payload.image->height(), latticePlusBounds)) {
1199         fDL->drawImageLattice(std::move(payload), latticePlusBounds, dst, filter, paint);
1200     } else {
1201         SkSamplingOptions sampling(filter, SkMipmapMode::kNone);
1202         fDL->drawImageRect(std::move(payload), nullptr, dst, sampling, paint,
1203                            kFast_SrcRectConstraint);
1204     }
1205 }
1206 
onDrawImage2(const SkImage * img,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)1207 void RecordingCanvas::onDrawImage2(const SkImage* img, SkScalar x, SkScalar y,
1208                                    const SkSamplingOptions& sampling, const SkPaint* paint) {
1209     fDL->drawImage(DrawImagePayload(img), x, y, sampling, paint);
1210 }
1211 
onDrawImageRect2(const SkImage * img,const SkRect & src,const SkRect & dst,const SkSamplingOptions & sampling,const SkPaint * paint,SrcRectConstraint constraint)1212 void RecordingCanvas::onDrawImageRect2(const SkImage* img, const SkRect& src, const SkRect& dst,
1213                                        const SkSamplingOptions& sampling, const SkPaint* paint,
1214                                        SrcRectConstraint constraint) {
1215     fDL->drawImageRect(DrawImagePayload(img), &src, dst, sampling, paint, constraint);
1216 }
1217 
onDrawImageLattice2(const SkImage * img,const SkCanvas::Lattice & lattice,const SkRect & dst,SkFilterMode filter,const SkPaint * paint)1218 void RecordingCanvas::onDrawImageLattice2(const SkImage* img, const SkCanvas::Lattice& lattice,
1219                                           const SkRect& dst, SkFilterMode filter,
1220                                           const SkPaint* paint) {
1221     fDL->drawImageLattice(DrawImagePayload(img), lattice, dst, filter, paint);
1222 }
1223 
onDrawPatch(const SkPoint cubics[12],const SkColor colors[4],const SkPoint texCoords[4],SkBlendMode bmode,const SkPaint & paint)1224 void RecordingCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
1225                                   const SkPoint texCoords[4], SkBlendMode bmode,
1226                                   const SkPaint& paint) {
1227     fDL->drawPatch(cubics, colors, texCoords, bmode, paint);
1228 }
onDrawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)1229 void RecordingCanvas::onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
1230                                    const SkPaint& paint) {
1231     fDL->drawPoints(mode, count, pts, paint);
1232 }
onDrawVerticesObject(const SkVertices * vertices,SkBlendMode mode,const SkPaint & paint)1233 void RecordingCanvas::onDrawVerticesObject(const SkVertices* vertices,
1234                                            SkBlendMode mode, const SkPaint& paint) {
1235     fDL->drawVertices(vertices, mode, paint);
1236 }
onDrawMesh(const SkMesh & mesh,sk_sp<SkBlender> blender,const SkPaint & paint)1237 void RecordingCanvas::onDrawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender,
1238                                  const SkPaint& paint) {
1239     fDL->drawMesh(mesh, blender, paint);
1240 }
drawMesh(const Mesh & mesh,sk_sp<SkBlender> blender,const SkPaint & paint)1241 void RecordingCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
1242     fDL->drawMesh(mesh, blender, paint);
1243 }
onDrawAtlas2(const SkImage * atlas,const SkRSXform xforms[],const SkRect texs[],const SkColor colors[],int count,SkBlendMode bmode,const SkSamplingOptions & sampling,const SkRect * cull,const SkPaint * paint)1244 void RecordingCanvas::onDrawAtlas2(const SkImage* atlas, const SkRSXform xforms[],
1245                                    const SkRect texs[], const SkColor colors[], int count,
1246                                    SkBlendMode bmode, const SkSamplingOptions& sampling,
1247                                    const SkRect* cull, const SkPaint* paint) {
1248     fDL->drawAtlas(atlas, xforms, texs, colors, count, bmode, sampling, cull, paint);
1249 }
onDrawShadowRec(const SkPath & path,const SkDrawShadowRec & rec)1250 void RecordingCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
1251     fDL->drawShadowRec(path, rec);
1252 }
1253 
drawVectorDrawable(VectorDrawableRoot * tree)1254 void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
1255     fDL->drawVectorDrawable(tree);
1256 }
1257 
drawWebView(skiapipeline::FunctorDrawable * drawable)1258 void RecordingCanvas::drawWebView(skiapipeline::FunctorDrawable* drawable) {
1259     fDL->drawWebView(drawable);
1260 }
1261 
getSkMesh() const1262 [[nodiscard]] const SkMesh& DrawMeshPayload::getSkMesh() const {
1263     LOG_FATAL_IF(!meshWrapper && !mesh, "One of Mesh or Mesh must be non-null");
1264     if (meshWrapper) {
1265         return meshWrapper->getSkMesh();
1266     } else {
1267         return *mesh;
1268     }
1269 }
1270 
1271 }  // namespace uirenderer
1272 }  // namespace android
1273