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 start, const SkMatrix& ctm) | 35 Key(uint32_t pictureID, const SkMatrix& initialMat, |
36 : fPictureID(pictureID) | 36 const int* key, int keySize, bool copyKey = false) |
37 , fStart(start) | 37 : fKeySize(keySize) |
38 , fCTM(ctm) { | 38 , fFreeKey(copyKey) { |
39 fCTM.getType(); // force initialization of type so hashes match | 39 fIDMatrix.fPictureID = pictureID; |
| 40 fIDMatrix.fInitialMat = initialMat; |
| 41 fIDMatrix.fInitialMat.getType(); // force initialization of type
so hashes match |
40 | 42 |
41 // Key needs to be tightly packed. | 43 if (copyKey) { |
42 GR_STATIC_ASSERT(sizeof(Key) == sizeof(uint32_t) + // pictu
re ID | 44 int* tempKey = SkNEW_ARRAY(int, keySize); |
43 sizeof(int) + // start | 45 memcpy(tempKey, key, keySize * sizeof(int)); |
44 9 * sizeof(SkScalar) // 3x3 f
rom CTM | 46 fKey = tempKey; |
45 +sizeof(uint32_t)); // matri
x's type | 47 } else { |
| 48 fKey = key; |
| 49 } |
| 50 |
| 51 // The pictureID/matrix portion needs to be tightly packed. |
| 52 GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+
// pictureID |
| 53 9 * sizeof(SkScalar)+sizeof(uint32
_t)); // matrix |
46 } | 54 } |
47 | 55 |
48 bool operator==(const Key& other) const { | 56 ~Key() { |
49 return fPictureID == other.fPictureID && | 57 if (fFreeKey) { |
50 fStart == other.fStart && | 58 SkDELETE_ARRAY(fKey); |
51 fCTM.cheapEqualTo(other.fCTM); // TODO: should be fuzzy | 59 } |
| 60 } |
| 61 bool operator==(const Key& other) const { |
| 62 if (fKeySize != other.fKeySize) { |
| 63 return false; |
| 64 } |
| 65 return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID && |
| 66 fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fIniti
alMat) && |
| 67 !memcmp(fKey, other.fKey, fKeySize * sizeof(int)); |
52 } | 68 } |
53 | 69 |
54 uint32_t pictureID() const { return fPictureID; } | 70 uint32_t hash() const { |
55 unsigned int start() const { return fStart; } | 71 uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint3
2_t*>(fKey), |
| 72 fKeySize * sizeof(int)); |
| 73 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fI
DMatrix), |
| 74 sizeof(IDMatrix), hash); |
| 75 } |
56 | 76 |
57 private: | 77 private: |
58 const uint32_t fPictureID; | 78 struct IDMatrix { |
59 const unsigned fStart; | 79 uint32_t fPictureID; |
60 const SkMatrix fCTM; | 80 SkMatrix fInitialMat; |
| 81 } fIDMatrix; |
| 82 |
| 83 const int* fKey; |
| 84 const int fKeySize; |
| 85 const bool fFreeKey; |
61 }; | 86 }; |
62 | 87 |
63 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } | 88 static const Key& GetKey(const ReplacementInfo& layer) { return layer.fK
ey; } |
64 static uint32_t Hash(const Key& key) { | 89 static uint32_t Hash(const Key& key) { return key.hash(); } |
65 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); | |
66 } | |
67 | 90 |
68 ReplacementInfo(uint32_t pictureID, unsigned int start, const SkMatrix&
ctm) | 91 ReplacementInfo(uint32_t pictureID, const SkMatrix& initialMat, |
69 : fKey(pictureID, start, ctm) | 92 const int* key, int keySize) |
| 93 : fKey(pictureID, initialMat, key, keySize, true) |
70 , fImage(NULL) | 94 , fImage(NULL) |
71 , fPaint(NULL) { | 95 , fPaint(NULL) { |
72 } | 96 } |
73 ~ReplacementInfo() { fImage->unref(); SkDELETE(fPaint); } | 97 ~ReplacementInfo() { fImage->unref(); SkDELETE(fPaint); } |
74 | 98 |
75 unsigned int start() const { return fKey.start(); } | |
76 | |
77 const Key fKey; | 99 const Key fKey; |
78 unsigned fStop; | 100 unsigned fStop; |
79 SkIPoint fPos; | 101 SkIPoint fPos; |
80 SkImage* fImage; // Owns a ref | 102 SkImage* fImage; // Owns a ref |
81 const SkPaint* fPaint; // Owned by this object | 103 const SkPaint* fPaint; // Owned by this object |
82 | 104 |
83 SkIRect fSrcRect; | 105 SkIRect fSrcRect; |
84 }; | 106 }; |
85 | 107 |
86 ~GrReplacements() { this->freeAll(); } | 108 ~GrReplacements() { this->freeAll(); } |
87 | 109 |
88 // Add a new replacement range. | 110 // Add a new replacement range. |
89 ReplacementInfo* newReplacement(uint32_t pictureID, unsigned start, const Sk
Matrix& ctm); | 111 ReplacementInfo* newReplacement(uint32_t pictureID, const SkMatrix& initialM
at, |
| 112 const int* key, int keySize); |
90 | 113 |
91 // look up a replacement range by its pictureID, start offset and the CTM | 114 const ReplacementInfo* lookup(uint32_t pictureID, const SkMatrix& initalMat, |
92 // TODO: also need to add clip to lookup | 115 const int* key, int keySize) const; |
93 const ReplacementInfo* lookupByStart(uint32_t pictureID, unsigned start, | |
94 const SkMatrix& ctm) const; | |
95 | 116 |
96 private: | 117 private: |
97 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key> fReplacementHash; | 118 SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key> fReplacementHash; |
98 | 119 |
99 void freeAll(); | 120 void freeAll(); |
100 }; | 121 }; |
101 | 122 |
102 // Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with | 123 // Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with |
103 // drawBitmap calls. A convenience wrapper around SkRecords::Draw. | 124 // drawBitmap calls. A convenience wrapper around SkRecords::Draw. |
104 // It returns the number of saveLayer/restore blocks replaced with drawBitmap ca
lls. | 125 // It returns the number of saveLayer/restore blocks replaced with drawBitmap ca
lls. |
105 int GrRecordReplaceDraw(const SkPicture*, | 126 int GrRecordReplaceDraw(const SkPicture*, |
106 SkCanvas*, | 127 SkCanvas*, |
107 const GrReplacements*, | 128 const GrReplacements*, |
108 const SkMatrix& initialMatrix, | 129 const SkMatrix& initialMatrix, |
109 SkDrawPictureCallback*); | 130 SkDrawPictureCallback*); |
110 | 131 |
111 #endif // GrRecordReplaceDraw_DEFINED | 132 #endif // GrRecordReplaceDraw_DEFINED |
OLD | NEW |