OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 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 #ifndef GrRecordReplaceDraw_DEFINED | 8 #ifndef GrRecordReplaceDraw_DEFINED |
9 #define GrRecordReplaceDraw_DEFINED | 9 #define GrRecordReplaceDraw_DEFINED |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 // GrReplacements collects op ranges that can be replaced with | 26 // GrReplacements collects op ranges that can be replaced with |
27 // a single drawBitmap call (using a precomputed bitmap). | 27 // a single drawBitmap call (using a precomputed bitmap). |
28 class GrReplacements { | 28 class GrReplacements { |
29 public: | 29 public: |
30 // All the operations between fStart and fStop (inclusive) will be replaced
with | 30 // All the operations between fStart and fStop (inclusive) will be replaced
with |
31 // a single drawBitmap call using fPos, fImage and fPaint. | 31 // a single drawBitmap call using fPos, fImage and fPaint. |
32 class ReplacementInfo { | 32 class ReplacementInfo { |
33 public: | 33 public: |
34 struct Key { | 34 struct Key { |
35 Key(uint32_t pictureID, unsigned int start, const SkMatrix& ctm) | 35 Key(uint32_t pictureID, unsigned start, const SkMatrix& ctm) |
36 : fPictureID(pictureID) | 36 : fPictureID(pictureID) |
37 , fStart(start) | 37 , fStart(start) |
38 , fCTM(ctm) { | 38 , fCTM(ctm) { |
39 fCTM.getType(); // force initialization of type so hashes match | 39 fCTM.getType(); // force initialization of type so hashes match |
40 | 40 |
41 // Key needs to be tightly packed. | 41 // Key needs to be tightly packed. |
42 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // pictu
re ID | 42 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // pictu
re ID |
43 sizeof(int) + // start | 43 sizeof(int) + // start |
44 9 * sizeof(SkScalar) // 3x3 f
rom CTM | 44 9 * sizeof(SkScalar) // 3x3 f
rom CTM |
45 +sizeof(uint32_t)); // matri
x's type | 45 +sizeof(uint32_t)); // matri
x's type |
46 } | 46 } |
47 | 47 |
48 bool operator==(const Key& other) const { | 48 bool operator==(const Key& other) const { |
49 return fPictureID == other.fPictureID && | 49 return fPictureID == other.fPictureID && |
50 fStart == other.fStart && | 50 fStart == other.fStart && |
51 fCTM.cheapEqualTo(other.fCTM); // TODO: should be fuzzy | 51 fCTM.cheapEqualTo(other.fCTM); // TODO: should be fuzzy |
52 } | 52 } |
53 | 53 |
54 uint32_t pictureID() const { return fPictureID; } | 54 uint32_t pictureID() const { return fPictureID; } |
55 unsigned int start() const { return fStart; } | 55 unsigned int start() const { return fStart; } |
56 | 56 |
57 private: | 57 private: |
58 const uint32_t fPictureID; | 58 const uint32_t fPictureID; |
59 const unsigned int fStart; | 59 const unsigned fStart; |
60 const SkMatrix fCTM; | 60 const SkMatrix fCTM; |
61 }; | 61 }; |
62 | 62 |
63 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } | 63 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } |
64 static uint32_t Hash(const Key& key) { | 64 static uint32_t Hash(const Key& key) { |
65 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); | 65 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); |
66 } | 66 } |
67 | 67 |
68 ReplacementInfo(uint32_t pictureID, unsigned int start, const SkMatrix&
ctm) | 68 ReplacementInfo(uint32_t pictureID, unsigned int start, const SkMatrix&
ctm) |
69 : fKey(pictureID, start, ctm) | 69 : fKey(pictureID, start, ctm) |
70 , fImage(NULL) | 70 , fImage(NULL) |
71 , fPaint(NULL) { | 71 , fPaint(NULL) { |
72 } | 72 } |
73 ~ReplacementInfo() { fImage->unref(); SkDELETE(fPaint); } | 73 ~ReplacementInfo() { fImage->unref(); SkDELETE(fPaint); } |
74 | 74 |
75 unsigned int start() const { return fKey.start(); } | 75 unsigned int start() const { return fKey.start(); } |
76 | 76 |
77 const Key fKey; | 77 const Key fKey; |
78 unsigned fStop; | 78 unsigned fStop; |
79 SkIPoint fPos; | 79 SkIPoint fPos; |
80 SkImage* fImage; // Owns a ref | 80 SkImage* fImage; // Owns a ref |
81 const SkPaint* fPaint; // Owned by this object | 81 const SkPaint* fPaint; // Owned by this object |
82 | 82 |
83 SkIRect fSrcRect; | 83 SkIRect fSrcRect; |
84 }; | 84 }; |
85 | 85 |
86 ~GrReplacements() { this->freeAll(); } | 86 ~GrReplacements() { this->freeAll(); } |
87 | 87 |
88 // Add a new replacement range. | 88 // Add a new replacement range. |
89 ReplacementInfo* newReplacement(uint32_t pictureID, unsigned int start, cons
t SkMatrix& ctm); | 89 ReplacementInfo* newReplacement(uint32_t pictureID, unsigned start, const Sk
Matrix& ctm); |
90 | 90 |
91 // look up a replacement range by its pictureID, start offset and the CTM | 91 // look up a replacement range by its pictureID, start offset and the CTM |
92 // TODO: also need to add clip to lookup | 92 // TODO: also need to add clip to lookup |
93 const ReplacementInfo* lookupByStart(uint32_t pictureID, size_t start, | 93 const ReplacementInfo* lookupByStart(uint32_t pictureID, unsigned start, |
94 const SkMatrix& ctm) const; | 94 const SkMatrix& ctm) const; |
95 | 95 |
96 private: | 96 private: |
97 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key> fReplacementHash; | 97 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key> fReplacementHash; |
98 | 98 |
99 void freeAll(); | 99 void freeAll(); |
100 }; | 100 }; |
101 | 101 |
102 // Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with | 102 // Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with |
103 // drawBitmap calls. A convenience wrapper around SkRecords::Draw. | 103 // drawBitmap calls. A convenience wrapper around SkRecords::Draw. |
104 // It returns the number of saveLayer/restore blocks replaced with drawBitmap ca
lls. | 104 // It returns the number of saveLayer/restore blocks replaced with drawBitmap ca
lls. |
105 int GrRecordReplaceDraw(const SkPicture*, | 105 int GrRecordReplaceDraw(const SkPicture*, |
106 SkCanvas*, | 106 SkCanvas*, |
107 const GrReplacements*, | 107 const GrReplacements*, |
108 const SkMatrix& initialMatrix, | 108 const SkMatrix& initialMatrix, |
109 SkDrawPictureCallback*); | 109 SkDrawPictureCallback*); |
110 | 110 |
111 #endif // GrRecordReplaceDraw_DEFINED | 111 #endif // GrRecordReplaceDraw_DEFINED |
OLD | NEW |