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