OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkImageFilter.h" |
9 #include "SkLiteDL.h" | 10 #include "SkLiteDL.h" |
10 #include "SkMutex.h" | 11 #include "SkMutex.h" |
11 #include "SkSpinlock.h" | 12 #include "SkSpinlock.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 struct Op { | 15 struct Op { |
15 virtual ~Op() {} | 16 virtual ~Op() {} |
16 virtual void draw(SkCanvas*) = 0; | 17 virtual void draw(SkCanvas*) = 0; |
17 | 18 |
18 size_t skip; | 19 size_t skip; |
19 }; | 20 }; |
20 | 21 |
21 struct Save final : Op { void draw(SkCanvas* c) override { c-> save();
} }; | 22 struct Save final : Op { void draw(SkCanvas* c) override { c-> save();
} }; |
22 struct Restore final : Op { void draw(SkCanvas* c) override { c->restore();
} }; | 23 struct Restore final : Op { void draw(SkCanvas* c) override { c->restore();
} }; |
| 24 struct SaveLayer final : Op { |
| 25 SaveLayer(const SkRect* bounds, const SkPaint* paint, |
| 26 const SkImageFilter* backdrop, uint32_t flags) { |
| 27 if (bounds) { this->bounds = *bounds; } |
| 28 if (paint) { this->paint = *paint; } |
| 29 this->backdrop = sk_ref_sp(backdrop); |
| 30 this->flags = flags; |
| 31 } |
| 32 SkRect bounds = {SK_ScalarMin,SK_ScalarMin, SK_Scala
rMax,SK_ScalarMax}; |
| 33 SkPaint paint; |
| 34 sk_sp<const SkImageFilter> backdrop; |
| 35 uint32_t flags; |
| 36 void draw(SkCanvas* c) override { |
| 37 c->saveLayer({ &bounds, &paint, backdrop.get(), flags }); |
| 38 } |
| 39 }; |
23 | 40 |
24 struct Concat final : Op { | 41 struct Concat final : Op { |
25 Concat(const SkMatrix& matrix) : matrix(matrix) {} | 42 Concat(const SkMatrix& matrix) : matrix(matrix) {} |
26 SkMatrix matrix; | 43 SkMatrix matrix; |
27 void draw(SkCanvas* c) override { c->concat(matrix); } | 44 void draw(SkCanvas* c) override { c->concat(matrix); } |
28 }; | 45 }; |
29 struct SetMatrix final : Op { | 46 struct SetMatrix final : Op { |
30 SetMatrix(const SkMatrix& matrix) : matrix(matrix) {} | 47 SetMatrix(const SkMatrix& matrix) : matrix(matrix) {} |
31 SkMatrix matrix; | 48 SkMatrix matrix; |
32 void draw(SkCanvas* c) override { c->setMatrix(matrix); } | 49 void draw(SkCanvas* c) override { c->setMatrix(matrix); } |
33 }; | 50 }; |
34 | 51 |
| 52 struct ClipPath final : Op { |
| 53 ClipPath(const SkPath& path, SkRegion::Op op, bool aa) : path(path), op(
op), aa(aa) {} |
| 54 SkPath path; |
| 55 SkRegion::Op op; |
| 56 bool aa; |
| 57 void draw(SkCanvas* c) override { c->clipPath(path, op, aa); } |
| 58 }; |
35 struct ClipRect final : Op { | 59 struct ClipRect final : Op { |
36 ClipRect(const SkRect& rect, SkRegion::Op op, bool aa) : rect(rect), op(
op), aa(aa) {} | 60 ClipRect(const SkRect& rect, SkRegion::Op op, bool aa) : rect(rect), op(
op), aa(aa) {} |
37 SkRect rect; | 61 SkRect rect; |
38 SkRegion::Op op; | 62 SkRegion::Op op; |
39 bool aa; | 63 bool aa; |
40 void draw(SkCanvas* c) override { c->clipRect(rect, op, aa); } | 64 void draw(SkCanvas* c) override { c->clipRect(rect, op, aa); } |
41 }; | 65 }; |
| 66 struct ClipRRect final : Op { |
| 67 ClipRRect(const SkRRect& rrect, SkRegion::Op op, bool aa) : rrect(rrect)
, op(op), aa(aa) {} |
| 68 SkRRect rrect; |
| 69 SkRegion::Op op; |
| 70 bool aa; |
| 71 void draw(SkCanvas* c) override { c->clipRRect(rrect, op, aa); } |
| 72 }; |
| 73 struct ClipRegion final : Op { |
| 74 ClipRegion(const SkRegion& region, SkRegion::Op op) : region(region), op
(op) {} |
| 75 SkRegion region; |
| 76 SkRegion::Op op; |
| 77 void draw(SkCanvas* c) override { c->clipRegion(region, op); } |
| 78 }; |
42 | 79 |
| 80 struct DrawPaint final : Op { |
| 81 DrawPaint(const SkPaint& paint) : paint(paint) {} |
| 82 SkPaint paint; |
| 83 void draw(SkCanvas* c) override { c->drawPaint(paint); } |
| 84 }; |
| 85 struct DrawPath final : Op { |
| 86 DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(p
aint) {} |
| 87 SkPath path; |
| 88 SkPaint paint; |
| 89 void draw(SkCanvas* c) override { c->drawPath(path, paint); } |
| 90 }; |
43 struct DrawRect final : Op { | 91 struct DrawRect final : Op { |
44 DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(p
aint) {} | 92 DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(p
aint) {} |
45 SkRect rect; | 93 SkRect rect; |
46 SkPaint paint; | 94 SkPaint paint; |
47 void draw(SkCanvas* c) override { c->drawRect(rect, paint); } | 95 void draw(SkCanvas* c) override { c->drawRect(rect, paint); } |
48 }; | 96 }; |
49 | 97 struct DrawOval final : Op { |
50 struct DrawPath final : Op { | 98 DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(p
aint) {} |
51 DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(p
aint) {} | 99 SkRect oval; |
52 SkPath path; | |
53 SkPaint paint; | 100 SkPaint paint; |
54 void draw(SkCanvas* c) override { c->drawPath(path, paint); } | 101 void draw(SkCanvas* c) override { c->drawOval(oval, paint); } |
| 102 }; |
| 103 struct DrawRRect final : Op { |
| 104 DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), pa
int(paint) {} |
| 105 SkRRect rrect; |
| 106 SkPaint paint; |
| 107 void draw(SkCanvas* c) override { c->drawRRect(rrect, paint); } |
| 108 }; |
| 109 struct DrawDRRect final : Op { |
| 110 DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& pa
int) |
| 111 : outer(outer), inner(inner), paint(paint) {} |
| 112 SkRRect outer, inner; |
| 113 SkPaint paint; |
| 114 void draw(SkCanvas* c) override { c->drawDRRect(outer, inner, paint); } |
55 }; | 115 }; |
56 | 116 |
57 template <typename T, typename... Args> | 117 template <typename T, typename... Args> |
58 static void* push(SkTDArray<uint8_t>* bytes, size_t pod, Args&&... args) { | 118 static void* push(SkTDArray<uint8_t>* bytes, size_t pod, Args&&... args) { |
59 size_t skip = SkAlignPtr(sizeof(T) + pod); | 119 size_t skip = SkAlignPtr(sizeof(T) + pod); |
60 auto op = (T*)bytes->append(skip); | 120 auto op = (T*)bytes->append(skip); |
61 new (op) T{ std::forward<Args>(args)... }; | 121 new (op) T{ std::forward<Args>(args)... }; |
62 op->skip = skip; | 122 op->skip = skip; |
63 return op+1; | 123 return op+1; |
64 } | 124 } |
65 | 125 |
66 template <typename Fn> | 126 template <typename Fn> |
67 static void map(SkTDArray<uint8_t>* bytes, Fn&& fn) { | 127 static void map(SkTDArray<uint8_t>* bytes, Fn&& fn) { |
68 for (uint8_t* ptr = bytes->begin(); ptr < bytes->end(); ) { | 128 for (uint8_t* ptr = bytes->begin(); ptr < bytes->end(); ) { |
69 auto op = (Op*)ptr; | 129 auto op = (Op*)ptr; |
70 fn(op); | 130 fn(op); |
71 ptr += op->skip; | 131 ptr += op->skip; |
72 } | 132 } |
73 } | 133 } |
74 } | 134 } |
75 | 135 |
76 void SkLiteDL:: save() { push <Save>(&fBytes, 0); } | 136 void SkLiteDL:: save() { push <Save>(&fBytes, 0); } |
77 void SkLiteDL::restore() { push<Restore>(&fBytes, 0); } | 137 void SkLiteDL::restore() { push<Restore>(&fBytes, 0); } |
| 138 void SkLiteDL::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 139 const SkImageFilter* backdrop, uint32_t flags) { |
| 140 push<SaveLayer>(&fBytes, 0, bounds, paint, backdrop, flags); |
| 141 } |
78 | 142 |
79 void SkLiteDL:: concat(const SkMatrix& matrix) { push <Concat>(&fBytes, 0, m
atrix); } | 143 void SkLiteDL:: concat(const SkMatrix& matrix) { push <Concat>(&fBytes, 0, m
atrix); } |
80 void SkLiteDL::setMatrix(const SkMatrix& matrix) { push<SetMatrix>(&fBytes, 0, m
atrix); } | 144 void SkLiteDL::setMatrix(const SkMatrix& matrix) { push<SetMatrix>(&fBytes, 0, m
atrix); } |
81 | 145 |
| 146 void SkLiteDL::clipPath(const SkPath& path, SkRegion::Op op, bool aa) { |
| 147 push<ClipPath>(&fBytes, 0, path, op, aa); |
| 148 } |
82 void SkLiteDL::clipRect(const SkRect& rect, SkRegion::Op op, bool aa) { | 149 void SkLiteDL::clipRect(const SkRect& rect, SkRegion::Op op, bool aa) { |
83 push<ClipRect>(&fBytes, 0, rect, op, aa); | 150 push<ClipRect>(&fBytes, 0, rect, op, aa); |
84 } | 151 } |
| 152 void SkLiteDL::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool aa) { |
| 153 push<ClipRRect>(&fBytes, 0, rrect, op, aa); |
| 154 } |
| 155 void SkLiteDL::clipRegion(const SkRegion& region, SkRegion::Op op) { |
| 156 push<ClipRegion>(&fBytes, 0, region, op); |
| 157 } |
85 | 158 |
86 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) { | 159 void SkLiteDL::drawPaint(const SkPaint& paint) { |
87 push<DrawRect>(&fBytes, 0, rect, paint); | 160 push<DrawPaint>(&fBytes, 0, paint); |
88 } | 161 } |
89 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) { | 162 void SkLiteDL::drawPath(const SkPath& path, const SkPaint& paint) { |
90 push<DrawPath>(&fBytes, 0, path, paint); | 163 push<DrawPath>(&fBytes, 0, path, paint); |
91 } | 164 } |
| 165 void SkLiteDL::drawRect(const SkRect& rect, const SkPaint& paint) { |
| 166 push<DrawRect>(&fBytes, 0, rect, paint); |
| 167 } |
| 168 void SkLiteDL::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 169 push<DrawOval>(&fBytes, 0, oval, paint); |
| 170 } |
| 171 void SkLiteDL::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| 172 push<DrawRRect>(&fBytes, 0, rrect, paint); |
| 173 } |
| 174 void SkLiteDL::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPa
int& paint) { |
| 175 push<DrawDRRect>(&fBytes, 0, outer, inner, paint); |
| 176 } |
92 | 177 |
93 void SkLiteDL::onDraw(SkCanvas* canvas) { | 178 void SkLiteDL::onDraw(SkCanvas* canvas) { |
94 map(&fBytes, [canvas](Op* op) { op->draw(canvas); }); | 179 map(&fBytes, [canvas](Op* op) { op->draw(canvas); }); |
95 } | 180 } |
96 | 181 |
97 SkRect SkLiteDL::onGetBounds() { | 182 SkRect SkLiteDL::onGetBounds() { |
98 return fBounds; | 183 return fBounds; |
99 } | 184 } |
100 | 185 |
101 SkLiteDL:: SkLiteDL() {} | 186 SkLiteDL:: SkLiteDL() {} |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 if (gFreeStackCount < kFreeStackCountLimit) { | 226 if (gFreeStackCount < kFreeStackCountLimit) { |
142 self->fNext = gFreeStack; | 227 self->fNext = gFreeStack; |
143 gFreeStack = self; | 228 gFreeStack = self; |
144 gFreeStackCount++; | 229 gFreeStackCount++; |
145 return; | 230 return; |
146 } | 231 } |
147 } | 232 } |
148 | 233 |
149 delete this; | 234 delete this; |
150 } | 235 } |
OLD | NEW |