mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCanvas.h" |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 9 | #include "SkData.h" |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 10 | #include "SkImageFilter.h" |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 11 | #include "SkLiteDL.h" |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 12 | #include "SkMath.h" |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 13 | #include "SkPicture.h" |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 14 | #include "SkMutex.h" |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 15 | #include "SkRSXform.h" |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 16 | #include "SkSpinlock.h" |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 17 | #include "SkTextBlob.h" |
| 18 | |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 19 | #ifndef SKLITEDL_PAGE |
| 20 | #define SKLITEDL_PAGE 4096 |
| 21 | #endif |
| 22 | |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 23 | // A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer(). |
mtklein | 3ff2cc8 | 2016-08-10 08:31:42 -0700 | [diff] [blame] | 24 | static const SkRect kUnset = { SK_ScalarInfinity, 0,0,0}; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 25 | static const SkRect* maybe_unset(const SkRect& r) { |
mtklein | 3ff2cc8 | 2016-08-10 08:31:42 -0700 | [diff] [blame] | 26 | return r.left() == SK_ScalarInfinity ? nullptr : &r; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 27 | } |
| 28 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 29 | // copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst. |
| 30 | static void copy_v(void* dst) {} |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 31 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 32 | template <typename S, typename... Rest> |
| 33 | static void copy_v(void* dst, const S* src, int n, Rest&&... rest) { |
| 34 | SkASSERTF(((uintptr_t)dst & (alignof(S)-1)) == 0, |
| 35 | "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S)); |
| 36 | sk_careful_memcpy(dst, src, n*sizeof(S)); |
| 37 | copy_v(SkTAddOffset<void>(dst, n*sizeof(S)), std::forward<Rest>(rest)...); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 38 | } |
| 39 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 40 | // Helper for getting back at arrays which have been copy_v'd together after an Op. |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 41 | template <typename D, typename T> |
| 42 | static D* pod(T* op, size_t offset = 0) { |
| 43 | return SkTAddOffset<D>(op+1, offset); |
| 44 | } |
| 45 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 46 | // Pre-cache lazy non-threadsafe fields on SkPath and/or SkMatrix. |
| 47 | static void make_threadsafe(SkPath* path, SkMatrix* matrix) { |
| 48 | if (path) { path->updateBoundsCache(); } |
| 49 | if (matrix) { (void)matrix->getType(); } |
| 50 | } |
| 51 | |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 52 | namespace { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 53 | #define TYPES(M) \ |
| 54 | M(Save) M(Restore) M(SaveLayer) \ |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame^] | 55 | M(Concat) M(SetMatrix) M(Translate) M(TranslateZ) \ |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 56 | M(ClipPath) M(ClipRect) M(ClipRRect) M(ClipRegion) \ |
| 57 | M(DrawPaint) M(DrawPath) M(DrawRect) M(DrawOval) M(DrawRRect) M(DrawDRRect) \ |
| 58 | M(DrawAnnotation) M(DrawDrawable) M(DrawPicture) M(DrawShadowedPicture) \ |
| 59 | M(DrawImage) M(DrawImageNine) M(DrawImageRect) M(DrawImageLattice) \ |
| 60 | M(DrawText) M(DrawPosText) M(DrawPosTextH) \ |
| 61 | M(DrawTextOnPath) M(DrawTextRSXform) M(DrawTextBlob) \ |
| 62 | M(DrawPatch) M(DrawPoints) M(DrawVertices) M(DrawAtlas) |
| 63 | |
| 64 | #define M(T) T, |
| 65 | enum class Type : uint8_t { TYPES(M) }; |
| 66 | #undef M |
| 67 | |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 68 | struct Op { |
mtklein | 91cd280 | 2016-08-17 11:39:48 -0700 | [diff] [blame] | 69 | void makeThreadsafe() {} |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 70 | |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 71 | uint32_t type : 8; |
| 72 | uint32_t skip : 24; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 73 | }; |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 74 | static_assert(sizeof(Op) == 4, ""); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 75 | |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 76 | struct Save final : Op { |
| 77 | static const auto kType = Type::Save; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 78 | void draw(SkCanvas* c, const SkMatrix&) { c->save(); } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 79 | }; |
| 80 | struct Restore final : Op { |
| 81 | static const auto kType = Type::Restore; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 82 | void draw(SkCanvas* c, const SkMatrix&) { c->restore(); } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 83 | }; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 84 | struct SaveLayer final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 85 | static const auto kType = Type::SaveLayer; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 86 | SaveLayer(const SkRect* bounds, const SkPaint* paint, |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 87 | const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) { |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 88 | if (bounds) { this->bounds = *bounds; } |
| 89 | if (paint) { this->paint = *paint; } |
| 90 | this->backdrop = sk_ref_sp(backdrop); |
| 91 | this->flags = flags; |
| 92 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 93 | SkRect bounds = kUnset; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 94 | SkPaint paint; |
| 95 | sk_sp<const SkImageFilter> backdrop; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 96 | SkCanvas::SaveLayerFlags flags; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 97 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 98 | c->saveLayer({ maybe_unset(bounds), &paint, backdrop.get(), flags }); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 99 | } |
| 100 | }; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 101 | |
| 102 | struct Concat final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 103 | static const auto kType = Type::Concat; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 104 | Concat(const SkMatrix& matrix) : matrix(matrix) {} |
| 105 | SkMatrix matrix; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 106 | void draw(SkCanvas* c, const SkMatrix&) { c->concat(matrix); } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 107 | void makeThreadsafe() { make_threadsafe(nullptr, &matrix); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 108 | }; |
| 109 | struct SetMatrix final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 110 | static const auto kType = Type::SetMatrix; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 111 | SetMatrix(const SkMatrix& matrix) : matrix(matrix) {} |
| 112 | SkMatrix matrix; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 113 | void draw(SkCanvas* c, const SkMatrix& original) { |
| 114 | c->setMatrix(SkMatrix::Concat(original, matrix)); |
| 115 | } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 116 | void makeThreadsafe() { make_threadsafe(nullptr, &matrix); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 117 | }; |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame^] | 118 | struct Translate final : Op { |
| 119 | static const auto kType = Type::Translate; |
| 120 | Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {} |
| 121 | SkScalar dx,dy; |
| 122 | void draw(SkCanvas* c, const SkMatrix&) { |
| 123 | c->translate(dx, dy); |
| 124 | } |
| 125 | }; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 126 | struct TranslateZ final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 127 | static const auto kType = Type::TranslateZ; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 128 | TranslateZ(SkScalar dz) : dz(dz) {} |
| 129 | SkScalar dz; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 130 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 131 | #ifdef SK_EXPERIMENTAL_SHADOWING |
| 132 | c->translateZ(dz); |
| 133 | #endif |
| 134 | } |
| 135 | }; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 136 | |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 137 | struct ClipPath final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 138 | static const auto kType = Type::ClipPath; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 139 | ClipPath(const SkPath& path, SkRegion::Op op, bool aa) : path(path), op(op), aa(aa) {} |
| 140 | SkPath path; |
| 141 | SkRegion::Op op; |
| 142 | bool aa; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 143 | void draw(SkCanvas* c, const SkMatrix&) { c->clipPath(path, op, aa); } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 144 | void makeThreadsafe() { make_threadsafe(&path, nullptr); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 145 | }; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 146 | struct ClipRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 147 | static const auto kType = Type::ClipRect; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 148 | ClipRect(const SkRect& rect, SkRegion::Op op, bool aa) : rect(rect), op(op), aa(aa) {} |
| 149 | SkRect rect; |
| 150 | SkRegion::Op op; |
| 151 | bool aa; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 152 | void draw(SkCanvas* c, const SkMatrix&) { c->clipRect(rect, op, aa); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 153 | }; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 154 | struct ClipRRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 155 | static const auto kType = Type::ClipRRect; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 156 | ClipRRect(const SkRRect& rrect, SkRegion::Op op, bool aa) : rrect(rrect), op(op), aa(aa) {} |
| 157 | SkRRect rrect; |
| 158 | SkRegion::Op op; |
| 159 | bool aa; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 160 | void draw(SkCanvas* c, const SkMatrix&) { c->clipRRect(rrect, op, aa); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 161 | }; |
| 162 | struct ClipRegion final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 163 | static const auto kType = Type::ClipRegion; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 164 | ClipRegion(const SkRegion& region, SkRegion::Op op) : region(region), op(op) {} |
| 165 | SkRegion region; |
| 166 | SkRegion::Op op; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 167 | void draw(SkCanvas* c, const SkMatrix&) { c->clipRegion(region, op); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 168 | }; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 169 | |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 170 | struct DrawPaint final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 171 | static const auto kType = Type::DrawPaint; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 172 | DrawPaint(const SkPaint& paint) : paint(paint) {} |
| 173 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 174 | void draw(SkCanvas* c, const SkMatrix&) { c->drawPaint(paint); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 175 | }; |
| 176 | struct DrawPath final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 177 | static const auto kType = Type::DrawPath; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 178 | DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {} |
| 179 | SkPath path; |
| 180 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 181 | void draw(SkCanvas* c, const SkMatrix&) { c->drawPath(path, paint); } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 182 | void makeThreadsafe() { make_threadsafe(&path, nullptr); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 183 | }; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 184 | struct DrawRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 185 | static const auto kType = Type::DrawRect; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 186 | DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {} |
| 187 | SkRect rect; |
| 188 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 189 | void draw(SkCanvas* c, const SkMatrix&) { c->drawRect(rect, paint); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 190 | }; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 191 | struct DrawOval final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 192 | static const auto kType = Type::DrawOval; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 193 | DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {} |
| 194 | SkRect oval; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 195 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 196 | void draw(SkCanvas* c, const SkMatrix&) { c->drawOval(oval, paint); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 197 | }; |
| 198 | struct DrawRRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 199 | static const auto kType = Type::DrawRRect; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 200 | DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {} |
| 201 | SkRRect rrect; |
| 202 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 203 | void draw(SkCanvas* c, const SkMatrix&) { c->drawRRect(rrect, paint); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 204 | }; |
| 205 | struct DrawDRRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 206 | static const auto kType = Type::DrawDRRect; |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 207 | DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) |
| 208 | : outer(outer), inner(inner), paint(paint) {} |
| 209 | SkRRect outer, inner; |
| 210 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 211 | void draw(SkCanvas* c, const SkMatrix&) { c->drawDRRect(outer, inner, paint); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 214 | struct DrawAnnotation final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 215 | static const auto kType = Type::DrawAnnotation; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 216 | DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {} |
| 217 | SkRect rect; |
| 218 | sk_sp<SkData> value; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 219 | void draw(SkCanvas* c, const SkMatrix&) { |
| 220 | c->drawAnnotation(rect, pod<char>(this), value.get()); |
| 221 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 222 | }; |
| 223 | struct DrawDrawable final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 224 | static const auto kType = Type::DrawDrawable; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 225 | DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) { |
| 226 | if (matrix) { this->matrix = *matrix; } |
| 227 | } |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 228 | sk_sp<SkDrawable> drawable; |
| 229 | sk_sp<const SkPicture> snapped; |
| 230 | SkMatrix matrix = SkMatrix::I(); |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 231 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 232 | snapped ? c->drawPicture(snapped.get(), &matrix, nullptr) |
| 233 | : c->drawDrawable(drawable.get(), &matrix); |
| 234 | } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 235 | void makeThreadsafe() { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 236 | snapped.reset(drawable->newPictureSnapshot()); |
| 237 | make_threadsafe(nullptr, &matrix); |
| 238 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 239 | }; |
| 240 | struct DrawPicture final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 241 | static const auto kType = Type::DrawPicture; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 242 | DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) |
| 243 | : picture(sk_ref_sp(picture)) { |
| 244 | if (matrix) { this->matrix = *matrix; } |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 245 | if (paint) { this->paint = *paint; has_paint = true; } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 246 | } |
| 247 | sk_sp<const SkPicture> picture; |
| 248 | SkMatrix matrix = SkMatrix::I(); |
| 249 | SkPaint paint; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 250 | bool has_paint = false; // TODO: why is a default paint not the same? |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 251 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 252 | c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr); |
| 253 | } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 254 | void makeThreadsafe() { make_threadsafe(nullptr, &matrix); } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 255 | }; |
| 256 | struct DrawShadowedPicture final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 257 | static const auto kType = Type::DrawShadowedPicture; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 258 | DrawShadowedPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) |
| 259 | : picture(sk_ref_sp(picture)) { |
| 260 | if (matrix) { this->matrix = *matrix; } |
| 261 | if (paint) { this->paint = *paint; } |
| 262 | } |
| 263 | sk_sp<const SkPicture> picture; |
| 264 | SkMatrix matrix = SkMatrix::I(); |
| 265 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 266 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 267 | #ifdef SK_EXPERIMENTAL_SHADOWING |
| 268 | c->drawShadowedPicture(picture.get(), &matrix, &paint); |
| 269 | #endif |
| 270 | } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 271 | void makeThreadsafe() { make_threadsafe(nullptr, &matrix); } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 274 | struct DrawImage final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 275 | static const auto kType = Type::DrawImage; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 276 | DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint) |
mtklein | baeec6d | 2016-08-09 15:09:39 -0700 | [diff] [blame] | 277 | : image(std::move(image)), x(x), y(y) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 278 | if (paint) { this->paint = *paint; } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 279 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 280 | sk_sp<const SkImage> image; |
| 281 | SkScalar x,y; |
| 282 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 283 | void draw(SkCanvas* c, const SkMatrix&) { c->drawImage(image.get(), x,y, &paint); } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 284 | }; |
| 285 | struct DrawImageNine final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 286 | static const auto kType = Type::DrawImageNine; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 287 | DrawImageNine(sk_sp<const SkImage>&& image, |
| 288 | const SkIRect& center, const SkRect& dst, const SkPaint* paint) |
mtklein | baeec6d | 2016-08-09 15:09:39 -0700 | [diff] [blame] | 289 | : image(std::move(image)), center(center), dst(dst) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 290 | if (paint) { this->paint = *paint; } |
| 291 | } |
| 292 | sk_sp<const SkImage> image; |
| 293 | SkIRect center; |
| 294 | SkRect dst; |
| 295 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 296 | void draw(SkCanvas* c, const SkMatrix&) { |
| 297 | c->drawImageNine(image.get(), center, dst, &paint); |
| 298 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 299 | }; |
| 300 | struct DrawImageRect final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 301 | static const auto kType = Type::DrawImageRect; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 302 | DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst, |
| 303 | const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) |
mtklein | baeec6d | 2016-08-09 15:09:39 -0700 | [diff] [blame] | 304 | : image(std::move(image)), dst(dst), constraint(constraint) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 305 | this->src = src ? *src : SkRect::MakeIWH(image->width(), image->height()); |
| 306 | if (paint) { this->paint = *paint; } |
| 307 | } |
| 308 | sk_sp<const SkImage> image; |
| 309 | SkRect src, dst; |
| 310 | SkPaint paint; |
| 311 | SkCanvas::SrcRectConstraint constraint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 312 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 313 | c->drawImageRect(image.get(), src, dst, &paint, constraint); |
| 314 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 315 | }; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 316 | struct DrawImageLattice final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 317 | static const auto kType = Type::DrawImageLattice; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 318 | DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, |
| 319 | const SkRect& dst, const SkPaint* paint) |
mtklein | baeec6d | 2016-08-09 15:09:39 -0700 | [diff] [blame] | 320 | : image(std::move(image)), xs(xs), ys(ys), dst(dst) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 321 | if (paint) { this->paint = *paint; } |
| 322 | } |
| 323 | sk_sp<const SkImage> image; |
| 324 | int xs, ys; |
| 325 | SkRect dst; |
| 326 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 327 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 328 | auto xdivs = pod<int>(this, 0), |
| 329 | ydivs = pod<int>(this, xs*sizeof(int)); |
| 330 | c->drawImageLattice(image.get(), {xdivs, xs, ydivs, ys}, dst, &paint); |
| 331 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 332 | }; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 333 | |
| 334 | struct DrawText final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 335 | static const auto kType = Type::DrawText; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 336 | DrawText(size_t bytes, SkScalar x, SkScalar y, const SkPaint& paint) |
| 337 | : bytes(bytes), x(x), y(y), paint(paint) {} |
| 338 | size_t bytes; |
| 339 | SkScalar x,y; |
| 340 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 341 | void draw(SkCanvas* c, const SkMatrix&) { |
| 342 | c->drawText(pod<void>(this), bytes, x,y, paint); |
| 343 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 344 | }; |
| 345 | struct DrawPosText final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 346 | static const auto kType = Type::DrawPosText; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 347 | DrawPosText(size_t bytes, const SkPaint& paint, int n) |
| 348 | : bytes(bytes), paint(paint), n(n) {} |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 349 | size_t bytes; |
| 350 | SkPaint paint; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 351 | int n; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 352 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 353 | auto points = pod<SkPoint>(this); |
| 354 | auto text = pod<void>(this, n*sizeof(SkPoint)); |
| 355 | c->drawPosText(text, bytes, points, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 356 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 357 | }; |
| 358 | struct DrawPosTextH final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 359 | static const auto kType = Type::DrawPosTextH; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 360 | DrawPosTextH(size_t bytes, SkScalar y, const SkPaint& paint, int n) |
| 361 | : bytes(bytes), y(y), paint(paint), n(n) {} |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 362 | size_t bytes; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 363 | SkScalar y; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 364 | SkPaint paint; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 365 | int n; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 366 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 367 | auto xs = pod<SkScalar>(this); |
| 368 | auto text = pod<void>(this, n*sizeof(SkScalar)); |
| 369 | c->drawPosTextH(text, bytes, xs, y, paint); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 370 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 371 | }; |
| 372 | struct DrawTextOnPath final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 373 | static const auto kType = Type::DrawTextOnPath; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 374 | DrawTextOnPath(size_t bytes, const SkPath& path, |
| 375 | const SkMatrix* matrix, const SkPaint& paint) |
| 376 | : bytes(bytes), path(path), paint(paint) { |
| 377 | if (matrix) { this->matrix = *matrix; } |
| 378 | } |
| 379 | size_t bytes; |
| 380 | SkPath path; |
| 381 | SkMatrix matrix = SkMatrix::I(); |
| 382 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 383 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 384 | c->drawTextOnPath(pod<void>(this), bytes, path, &matrix, paint); |
| 385 | } |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 386 | void makeThreadsafe() { make_threadsafe(&path, &matrix); } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 387 | }; |
| 388 | struct DrawTextRSXform final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 389 | static const auto kType = Type::DrawTextRSXform; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 390 | DrawTextRSXform(size_t bytes, const SkRect* cull, const SkPaint& paint) |
| 391 | : bytes(bytes), paint(paint) { |
| 392 | if (cull) { this->cull = *cull; } |
| 393 | } |
| 394 | size_t bytes; |
| 395 | SkRect cull = kUnset; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 396 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 397 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 398 | c->drawTextRSXform(pod<void>(this), bytes, pod<SkRSXform>(this, bytes), |
| 399 | maybe_unset(cull), paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 400 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 401 | }; |
| 402 | struct DrawTextBlob final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 403 | static const auto kType = Type::DrawTextBlob; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 404 | DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) |
| 405 | : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {} |
| 406 | sk_sp<const SkTextBlob> blob; |
| 407 | SkScalar x,y; |
| 408 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 409 | void draw(SkCanvas* c, const SkMatrix&) { |
| 410 | c->drawTextBlob(blob.get(), x,y, paint); |
| 411 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 412 | }; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 413 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 414 | struct DrawPatch final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 415 | static const auto kType = Type::DrawPatch; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 416 | DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4], |
| 417 | SkXfermode* xfermode, const SkPaint& paint) |
| 418 | : xfermode(sk_ref_sp(xfermode)), paint(paint) { |
| 419 | copy_v(this->cubics, cubics, 12); |
| 420 | if (colors) { copy_v(this->colors, colors, 4); has_colors = true; } |
| 421 | if (texs ) { copy_v(this->texs , texs , 4); has_texs = true; } |
| 422 | } |
| 423 | SkPoint cubics[12]; |
| 424 | SkColor colors[4]; |
| 425 | SkPoint texs[4]; |
| 426 | sk_sp<SkXfermode> xfermode; |
| 427 | SkPaint paint; |
| 428 | bool has_colors = false; |
| 429 | bool has_texs = false; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 430 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 431 | c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr, |
| 432 | xfermode.get(), paint); |
| 433 | } |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 434 | }; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 435 | struct DrawPoints final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 436 | static const auto kType = Type::DrawPoints; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 437 | DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint) |
| 438 | : mode(mode), count(count), paint(paint) {} |
| 439 | SkCanvas::PointMode mode; |
| 440 | size_t count; |
| 441 | SkPaint paint; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 442 | void draw(SkCanvas* c, const SkMatrix&) { |
| 443 | c->drawPoints(mode, count, pod<SkPoint>(this), paint); |
| 444 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 445 | }; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 446 | struct DrawVertices final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 447 | static const auto kType = Type::DrawVertices; |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 448 | DrawVertices(SkCanvas::VertexMode mode, int count, SkXfermode* xfermode, int nindices, |
| 449 | const SkPaint& paint, bool has_texs, bool has_colors, bool has_indices) |
| 450 | : mode(mode), count(count), xfermode(sk_ref_sp(xfermode)), nindices(nindices) |
| 451 | , paint(paint), has_texs(has_texs), has_colors(has_colors), has_indices(has_indices) {} |
| 452 | SkCanvas::VertexMode mode; |
| 453 | int count; |
| 454 | sk_sp<SkXfermode> xfermode; |
| 455 | int nindices; |
| 456 | SkPaint paint; |
| 457 | bool has_texs; |
| 458 | bool has_colors; |
| 459 | bool has_indices; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 460 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 461 | SkPoint* vertices = pod<SkPoint>(this, 0); |
| 462 | size_t offset = count*sizeof(SkPoint); |
| 463 | |
| 464 | SkPoint* texs = nullptr; |
| 465 | if (has_texs) { |
| 466 | texs = pod<SkPoint>(this, offset); |
| 467 | offset += count*sizeof(SkPoint); |
| 468 | } |
| 469 | |
| 470 | SkColor* colors = nullptr; |
| 471 | if (has_colors) { |
| 472 | colors = pod<SkColor>(this, offset); |
| 473 | offset += count*sizeof(SkColor); |
| 474 | } |
| 475 | |
| 476 | uint16_t* indices = nullptr; |
| 477 | if (has_indices) { |
| 478 | indices = pod<uint16_t>(this, offset); |
| 479 | } |
| 480 | c->drawVertices(mode, count, vertices, texs, colors, xfermode.get(), |
| 481 | indices, nindices, paint); |
| 482 | } |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 483 | }; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 484 | struct DrawAtlas final : Op { |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 485 | static const auto kType = Type::DrawAtlas; |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 486 | DrawAtlas(const SkImage* atlas, int count, SkXfermode::Mode xfermode, |
| 487 | const SkRect* cull, const SkPaint* paint, bool has_colors) |
| 488 | : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) { |
| 489 | if (cull) { this->cull = *cull; } |
| 490 | if (paint) { this->paint = *paint; } |
| 491 | } |
| 492 | sk_sp<const SkImage> atlas; |
| 493 | int count; |
| 494 | SkXfermode::Mode xfermode; |
| 495 | SkRect cull = kUnset; |
| 496 | SkPaint paint; |
| 497 | bool has_colors; |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 498 | void draw(SkCanvas* c, const SkMatrix&) { |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 499 | auto xforms = pod<SkRSXform>(this, 0); |
| 500 | auto texs = pod<SkRect>(this, count*sizeof(SkRSXform)); |
| 501 | auto colors = has_colors |
| 502 | ? pod<SkColor>(this, count*(sizeof(SkRSXform) + sizeof(SkRect))) |
| 503 | : nullptr; |
| 504 | c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode, |
| 505 | maybe_unset(cull), &paint); |
| 506 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 507 | }; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | template <typename T, typename... Args> |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 511 | void* SkLiteDL::push(size_t pod, Args&&... args) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 512 | size_t skip = SkAlignPtr(sizeof(T) + pod); |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 513 | SkASSERT(skip < (1<<24)); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 514 | if (fUsed + skip > fReserved) { |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 515 | static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2."); |
| 516 | // Next greater multiple of SKLITEDL_PAGE. |
| 517 | fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE-1); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 518 | fBytes.realloc(fReserved); |
| 519 | } |
| 520 | SkASSERT(fUsed + skip <= fReserved); |
| 521 | auto op = (T*)(fBytes.get() + fUsed); |
| 522 | fUsed += skip; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 523 | new (op) T{ std::forward<Args>(args)... }; |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 524 | op->type = (uint32_t)T::kType; |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 525 | op->skip = skip; |
| 526 | return op+1; |
| 527 | } |
| 528 | |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 529 | template <typename Fn, typename... Args> |
| 530 | inline void SkLiteDL::map(const Fn fns[], Args... args) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 531 | auto end = fBytes.get() + fUsed; |
| 532 | for (uint8_t* ptr = fBytes.get(); ptr < end; ) { |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 533 | auto op = (Op*)ptr; |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 534 | auto type = op->type; |
| 535 | auto skip = op->skip; |
| 536 | if (auto fn = fns[type]) { // We replace no-op functions with nullptrs |
| 537 | fn(op, args...); // to avoid the overhead of a pointless call. |
| 538 | } |
| 539 | ptr += skip; |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 543 | void SkLiteDL:: save() { this->push <Save>(0); } |
| 544 | void SkLiteDL::restore() { this->push<Restore>(0); } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 545 | void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint, |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 546 | const SkImageFilter* backdrop, SkCanvas::SaveLayerFlags flags) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 547 | this->push<SaveLayer>(0, bounds, paint, backdrop, flags); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 548 | } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 549 | |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame^] | 550 | void SkLiteDL:: concat(const SkMatrix& matrix) { this->push <Concat>(0, matrix); } |
| 551 | void SkLiteDL::setMatrix(const SkMatrix& matrix) { this->push<SetMatrix>(0, matrix); } |
| 552 | void SkLiteDL::translate(SkScalar dx, SkScalar dy) { this->push<Translate>(0, dx, dy); } |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 553 | void SkLiteDL::translateZ(SkScalar dz) { this->push<TranslateZ>(0, dz); } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 554 | |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 555 | void SkLiteDL::clipPath(const SkPath& path, SkRegion::Op op, bool aa) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 556 | this->push<ClipPath>(0, path, op, aa); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 557 | } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 558 | void SkLiteDL::clipRect(const SkRect& rect, SkRegion::Op op, bool aa) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 559 | this->push<ClipRect>(0, rect, op, aa); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 560 | } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 561 | void SkLiteDL::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool aa) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 562 | this->push<ClipRRect>(0, rrect, op, aa); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 563 | } |
| 564 | void SkLiteDL::clipRegion(const SkRegion& region, SkRegion::Op op) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 565 | this->push<ClipRegion>(0, region, op); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 566 | } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 567 | |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 568 | void SkLiteDL::drawPaint(const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 569 | this->push<DrawPaint>(0, paint); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 570 | } |
| 571 | void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 572 | this->push<DrawPath>(0, path, paint); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 573 | } |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 574 | void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 575 | this->push<DrawRect>(0, rect, paint); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 576 | } |
| 577 | void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 578 | this->push<DrawOval>(0, oval, paint); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 579 | } |
| 580 | void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 581 | this->push<DrawRRect>(0, rrect, paint); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 582 | } |
| 583 | void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 584 | this->push<DrawDRRect>(0, outer, inner, paint); |
mtklein | 6f8411d | 2016-08-06 15:28:11 -0700 | [diff] [blame] | 585 | } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 586 | |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 587 | void SkLiteDL::drawAnnotation(const SkRect& rect, const char* key, SkData* value) { |
| 588 | size_t bytes = strlen(key)+1; |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 589 | void* pod = this->push<DrawAnnotation>(bytes, rect, value); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 590 | copy_v(pod, key,bytes); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 591 | } |
| 592 | void SkLiteDL::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 593 | this->push<DrawDrawable>(0, drawable, matrix); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 594 | } |
| 595 | void SkLiteDL::drawPicture(const SkPicture* picture, |
| 596 | const SkMatrix* matrix, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 597 | this->push<DrawPicture>(0, picture, matrix, paint); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 598 | } |
| 599 | void SkLiteDL::drawShadowedPicture(const SkPicture* picture, |
| 600 | const SkMatrix* matrix, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 601 | this->push<DrawShadowedPicture>(0, picture, matrix, paint); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 602 | } |
| 603 | |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 604 | void SkLiteDL::drawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 605 | this->push<DrawImage>(0, SkImage::MakeFromBitmap(bm), x,y, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 606 | } |
| 607 | void SkLiteDL::drawBitmapNine(const SkBitmap& bm, const SkIRect& center, |
| 608 | const SkRect& dst, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 609 | this->push<DrawImageNine>(0, SkImage::MakeFromBitmap(bm), center, dst, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 610 | } |
| 611 | void SkLiteDL::drawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst, |
| 612 | const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 613 | this->push<DrawImageRect>(0, SkImage::MakeFromBitmap(bm), src, dst, paint, constraint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 614 | } |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 615 | void SkLiteDL::drawBitmapLattice(const SkBitmap& bm, const SkCanvas::Lattice& lattice, |
| 616 | const SkRect& dst, const SkPaint* paint) { |
| 617 | int xs = lattice.fXCount, ys = lattice.fYCount; |
| 618 | size_t bytes = (xs + ys) * sizeof(int); |
| 619 | void* pod = this->push<DrawImageLattice>(bytes, SkImage::MakeFromBitmap(bm), xs, ys, dst, |
| 620 | paint); |
| 621 | copy_v(pod, lattice.fXDivs, xs, |
| 622 | lattice.fYDivs, ys); |
| 623 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 624 | |
| 625 | void SkLiteDL::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 626 | this->push<DrawImage>(0, sk_ref_sp(image), x,y, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 627 | } |
| 628 | void SkLiteDL::drawImageNine(const SkImage* image, const SkIRect& center, |
| 629 | const SkRect& dst, const SkPaint* paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 630 | this->push<DrawImageNine>(0, sk_ref_sp(image), center, dst, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 631 | } |
| 632 | void SkLiteDL::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 633 | const SkPaint* paint, SkCanvas::SrcRectConstraint constraint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 634 | this->push<DrawImageRect>(0, sk_ref_sp(image), src, dst, paint, constraint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 635 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 636 | void SkLiteDL::drawImageLattice(const SkImage* image, const SkCanvas::Lattice& lattice, |
| 637 | const SkRect& dst, const SkPaint* paint) { |
| 638 | int xs = lattice.fXCount, ys = lattice.fYCount; |
| 639 | size_t bytes = (xs + ys) * sizeof(int); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 640 | void* pod = this->push<DrawImageLattice>(bytes, sk_ref_sp(image), xs, ys, dst, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 641 | copy_v(pod, lattice.fXDivs, xs, |
| 642 | lattice.fYDivs, ys); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 643 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 644 | |
| 645 | void SkLiteDL::drawText(const void* text, size_t bytes, |
| 646 | SkScalar x, SkScalar y, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 647 | void* pod = this->push<DrawText>(bytes, bytes, x, y, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 648 | copy_v(pod, (const char*)text,bytes); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 649 | } |
| 650 | void SkLiteDL::drawPosText(const void* text, size_t bytes, |
| 651 | const SkPoint pos[], const SkPaint& paint) { |
| 652 | int n = paint.countText(text, bytes); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 653 | void* pod = this->push<DrawPosText>(n*sizeof(SkPoint)+bytes, bytes, paint, n); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 654 | copy_v(pod, pos,n, (const char*)text,bytes); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 655 | } |
| 656 | void SkLiteDL::drawPosTextH(const void* text, size_t bytes, |
| 657 | const SkScalar xs[], SkScalar y, const SkPaint& paint) { |
| 658 | int n = paint.countText(text, bytes); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 659 | void* pod = this->push<DrawPosTextH>(n*sizeof(SkScalar)+bytes, bytes, y, paint, n); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 660 | copy_v(pod, xs,n, (const char*)text,bytes); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 661 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 662 | void SkLiteDL::drawTextOnPath(const void* text, size_t bytes, |
| 663 | const SkPath& path, const SkMatrix* matrix, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 664 | void* pod = this->push<DrawTextOnPath>(bytes, bytes, path, matrix, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 665 | copy_v(pod, (const char*)text,bytes); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 666 | } |
| 667 | void SkLiteDL::drawTextRSXform(const void* text, size_t bytes, |
| 668 | const SkRSXform xforms[], const SkRect* cull, const SkPaint& paint) { |
| 669 | int n = paint.countText(text, bytes); |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 670 | void* pod = this->push<DrawTextRSXform>(bytes+n*sizeof(SkRSXform), bytes, cull, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 671 | copy_v(pod, (const char*)text,bytes, xforms,n); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 672 | } |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 673 | void SkLiteDL::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 674 | this->push<DrawTextBlob>(0, blob, x,y, paint); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 675 | } |
| 676 | |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 677 | void SkLiteDL::drawPatch(const SkPoint points[12], const SkColor colors[4], const SkPoint texs[4], |
| 678 | SkXfermode* xfermode, const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 679 | this->push<DrawPatch>(0, points, colors, texs, xfermode, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 680 | } |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 681 | void SkLiteDL::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[], |
| 682 | const SkPaint& paint) { |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 683 | void* pod = this->push<DrawPoints>(count*sizeof(SkPoint), mode, count, paint); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 684 | copy_v(pod, points,count); |
| 685 | } |
| 686 | void SkLiteDL::drawVertices(SkCanvas::VertexMode mode, int count, const SkPoint vertices[], |
| 687 | const SkPoint texs[], const SkColor colors[], SkXfermode* xfermode, |
| 688 | const uint16_t indices[], int nindices, const SkPaint& paint) { |
| 689 | size_t bytes = count * sizeof(SkPoint); |
| 690 | if (texs ) { bytes += count * sizeof(SkPoint); } |
| 691 | if (colors) { bytes += count * sizeof(SkColor); } |
| 692 | if (indices) { bytes += nindices * sizeof(uint16_t); } |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 693 | void* pod = this->push<DrawVertices>(bytes, mode, count, xfermode, nindices, paint, |
| 694 | texs != nullptr, colors != nullptr, indices != nullptr); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 695 | copy_v(pod, vertices, count, |
| 696 | texs, texs ? count : 0, |
| 697 | colors, colors ? count : 0, |
| 698 | indices, indices ? nindices : 0); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 699 | } |
| 700 | void SkLiteDL::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[], |
| 701 | const SkColor colors[], int count, SkXfermode::Mode xfermode, |
| 702 | const SkRect* cull, const SkPaint* paint) { |
| 703 | size_t bytes = count*(sizeof(SkRSXform) + sizeof(SkRect)); |
| 704 | if (colors) { |
| 705 | bytes += count*sizeof(SkColor); |
| 706 | } |
mtklein | b202833 | 2016-08-09 15:13:18 -0700 | [diff] [blame] | 707 | void* pod = this->push<DrawAtlas>(bytes, |
| 708 | atlas, count, xfermode, cull, paint, colors != nullptr); |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 709 | copy_v(pod, xforms, count, |
| 710 | texs, count, |
| 711 | colors, colors ? count : 0); |
mtklein | 29de8d9 | 2016-08-07 11:35:08 -0700 | [diff] [blame] | 712 | } |
| 713 | |
mtklein | 91cd280 | 2016-08-17 11:39:48 -0700 | [diff] [blame] | 714 | typedef void(*draw_fn)(void*, SkCanvas*, const SkMatrix&); |
| 715 | typedef void(*void_fn)(void*); |
mtklein | ec55959 | 2016-08-06 20:31:30 -0700 | [diff] [blame] | 716 | |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 717 | // All ops implement draw(). |
mtklein | 6480619 | 2016-08-16 17:18:27 -0700 | [diff] [blame] | 718 | #define M(T) [](void* op, SkCanvas* c, const SkMatrix& original) { ((T*)op)->draw(c, original); }, |
| 719 | static const draw_fn draw_fns[] = { TYPES(M) }; |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 720 | #undef M |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 721 | |
mtklein | 91cd280 | 2016-08-17 11:39:48 -0700 | [diff] [blame] | 722 | #define M(T) [](void* op) { ((T*)op)->makeThreadsafe(); }, |
mtklein | d559eb2 | 2016-08-11 07:08:28 -0700 | [diff] [blame] | 723 | static const void_fn make_threadsafe_fns[] = { TYPES(M) }; |
| 724 | #undef M |
| 725 | |
| 726 | // Older libstdc++ has pre-standard std::has_trivial_destructor. |
| 727 | #if defined(__GLIBCXX__) && (__GLIBCXX__ < 20130000) |
| 728 | template <typename T> using can_skip_destructor = std::has_trivial_destructor<T>; |
| 729 | #else |
| 730 | template <typename T> using can_skip_destructor = std::is_trivially_destructible<T>; |
| 731 | #endif |
| 732 | |
| 733 | // Most state ops (matrix, clip, save, restore) have a trivial destructor. |
| 734 | #define M(T) !can_skip_destructor<T>::value ? [](void* op) { ((T*)op)->~T(); } : (void_fn)nullptr, |
| 735 | static const void_fn dtor_fns[] = { TYPES(M) }; |
| 736 | #undef M |
| 737 | |
mtklein | 91cd280 | 2016-08-17 11:39:48 -0700 | [diff] [blame] | 738 | void SkLiteDL::onDraw(SkCanvas* canvas) { this->map(draw_fns, canvas, canvas->getTotalMatrix()); } |
| 739 | void SkLiteDL::makeThreadsafe() { this->map(make_threadsafe_fns); } |
mtklein | 03591a7 | 2016-08-07 13:35:46 -0700 | [diff] [blame] | 740 | |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 741 | SkRect SkLiteDL::onGetBounds() { |
| 742 | return fBounds; |
| 743 | } |
| 744 | |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 745 | SkLiteDL:: SkLiteDL(SkRect bounds) : fUsed(0), fReserved(0), fBounds(bounds) {} |
mtklein | 765b6e6 | 2016-08-08 12:23:02 -0700 | [diff] [blame] | 746 | |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 747 | SkLiteDL::~SkLiteDL() { |
| 748 | this->reset(SkRect::MakeEmpty()); |
| 749 | } |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 750 | |
| 751 | sk_sp<SkLiteDL> SkLiteDL::New(SkRect bounds) { |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 752 | return sk_sp<SkLiteDL>(new SkLiteDL(bounds)); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 753 | } |
| 754 | |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 755 | void SkLiteDL::reset(SkRect bounds) { |
| 756 | SkASSERT(this->unique()); |
| 757 | this->map(dtor_fns); |
mtklein | 9c5052f | 2016-08-06 12:51:51 -0700 | [diff] [blame] | 758 | |
mtklein | 883c8ef | 2016-08-16 09:36:18 -0700 | [diff] [blame] | 759 | // Leave fBytes and fReserved alone. |
| 760 | fUsed = 0; |
| 761 | fBounds = bounds; |
mtklein | 0c753e5 | 2016-08-09 07:40:23 -0700 | [diff] [blame] | 762 | } |