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 int start, const int* key, int keyS
ize) |
36 : fPictureID(pictureID) | 36 : fPictureID(pictureID) |
37 , fStart(start) | 37 , fStart(start) |
38 , fCTM(ctm) { | 38 , fKey(key) |
39 fCTM.getType(); // force initialization of type so hashes match | 39 , fKeySize(keySize) { |
40 | |
41 // Key needs to be tightly packed. | |
42 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // pictu
re ID | |
43 sizeof(int) + // start | |
44 9 * sizeof(SkScalar) // 3x3 f
rom CTM | |
45 +sizeof(uint32_t)); // matri
x's type | |
46 } | 40 } |
47 | 41 |
48 bool operator==(const Key& other) const { | 42 bool operator==(const Key& other) const { |
49 return fPictureID == other.fPictureID && | 43 if (fKeySize != other.fKeySize) { |
50 fStart == other.fStart && | 44 return false; |
51 fCTM.cheapEqualTo(other.fCTM); // TODO: should be fuzzy | 45 } |
| 46 return !memcmp(fKey, other.fKey, fKeySize * sizeof(int)); |
52 } | 47 } |
53 | 48 |
54 uint32_t pictureID() const { return fPictureID; } | 49 uint32_t pictureID() const { return fPictureID; } |
55 unsigned int start() const { return fStart; } | 50 unsigned int start() const { return fStart; } |
56 | 51 |
| 52 const int* key() const { return fKey; } |
| 53 int keySize() const { return fKeySize; } |
| 54 |
57 private: | 55 private: |
58 const uint32_t fPictureID; | 56 const uint32_t fPictureID; |
59 const unsigned int fStart; | 57 const unsigned int fStart; |
60 const SkMatrix fCTM; | 58 const int* fKey; |
| 59 const int fKeySize; |
61 }; | 60 }; |
62 | 61 |
63 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } | 62 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } |
64 static uint32_t Hash(const Key& key) { | 63 static uint32_t Hash(const Key& key) { |
65 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); | 64 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(key.key
()), |
| 65 key.keySize()*sizeof(int)); |
66 } | 66 } |
67 | 67 |
68 ReplacementInfo(uint32_t pictureID, unsigned int start, const SkMatrix&
ctm) | 68 ReplacementInfo(uint32_t pictureID, unsigned int start, const int* key,
int keySize) |
69 : fKey(pictureID, start, ctm) | 69 : fKey(pictureID, start, key, keySize) |
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 int start, cons
t int* key, int keySize); |
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, int start, |
94 const SkMatrix& ctm) const; | 94 const int* key, int keyBytes) 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 |