OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkDrawLooper.h" | 8 #include "SkDrawLooper.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
11 #include "SkPaint.h" | 11 #include "SkPaint.h" |
12 #include "SkRect.h" | 12 #include "SkRect.h" |
13 | 13 |
| 14 #define kDrawLooperContextStorageLongCount (sizeof(void*) + sizeof(int)) |
| 15 |
14 bool SkDrawLooper::canComputeFastBounds(const SkPaint& paint) { | 16 bool SkDrawLooper::canComputeFastBounds(const SkPaint& paint) { |
15 SkCanvas canvas; | 17 SkCanvas canvas; |
| 18 uint32_t storage[kDrawLooperContextStorageLongCount]; |
16 | 19 |
17 this->init(&canvas); | 20 SkDrawLooper::DrawContext* context = |
| 21 this->init(&canvas, storage, sizeof(storage)); |
18 for (;;) { | 22 for (;;) { |
19 SkPaint p(paint); | 23 SkPaint p(paint); |
20 if (this->next(&canvas, &p)) { | 24 if (context->next(&canvas, &p)) { |
21 p.setLooper(NULL); | 25 p.setLooper(NULL); |
22 if (!p.canComputeFastBounds()) { | 26 if (!p.canComputeFastBounds()) { |
| 27 context->cleanup(storage); |
23 return false; | 28 return false; |
24 } | 29 } |
25 } else { | 30 } else { |
26 break; | 31 break; |
27 } | 32 } |
28 } | 33 } |
| 34 context->cleanup(storage); |
29 return true; | 35 return true; |
30 } | 36 } |
31 | 37 |
32 void SkDrawLooper::computeFastBounds(const SkPaint& paint, const SkRect& src, | 38 void SkDrawLooper::computeFastBounds(const SkPaint& paint, const SkRect& src, |
33 SkRect* dst) { | 39 SkRect* dst) { |
34 SkCanvas canvas; | 40 SkCanvas canvas; |
| 41 uint32_t storage[kDrawLooperContextStorageLongCount]; |
35 | 42 |
36 *dst = src; // catch case where there are no loops | 43 *dst = src; // catch case where there are no loops |
37 this->init(&canvas); | 44 SkDrawLooper::DrawContext* context = this->init( |
| 45 &canvas, storage, sizeof(storage)); |
38 for (bool firstTime = true;; firstTime = false) { | 46 for (bool firstTime = true;; firstTime = false) { |
39 SkPaint p(paint); | 47 SkPaint p(paint); |
40 if (this->next(&canvas, &p)) { | 48 if (context->next(&canvas, &p)) { |
41 SkRect r(src); | 49 SkRect r(src); |
42 | 50 |
43 p.setLooper(NULL); | 51 p.setLooper(NULL); |
44 p.computeFastBounds(r, &r); | 52 p.computeFastBounds(r, &r); |
45 canvas.getTotalMatrix().mapRect(&r); | 53 canvas.getTotalMatrix().mapRect(&r); |
46 | 54 |
47 if (firstTime) { | 55 if (firstTime) { |
48 *dst = r; | 56 *dst = r; |
49 } else { | 57 } else { |
50 dst->join(r); | 58 dst->join(r); |
51 } | 59 } |
52 } else { | 60 } else { |
53 break; | 61 break; |
54 } | 62 } |
55 } | 63 } |
| 64 context->cleanup(storage); |
56 } | 65 } |
| 66 |
| 67 void SkDrawLooper::DrawContext::cleanup(void* storage) { |
| 68 if ((void*)this == storage) { |
| 69 this->~DrawContext(); |
| 70 } else { |
| 71 SkDELETE(this); |
| 72 } |
| 73 } |
OLD | NEW |