| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkLazyPtr.h" | |
| 9 #include "SkPDFBitmap.h" | 8 #include "SkPDFBitmap.h" |
| 10 #include "SkPDFCanon.h" | 9 #include "SkPDFCanon.h" |
| 11 #include "SkPDFFont.h" | 10 #include "SkPDFFont.h" |
| 12 #include "SkPDFGraphicState.h" | 11 #include "SkPDFGraphicState.h" |
| 13 #include "SkPDFShader.h" | 12 #include "SkPDFShader.h" |
| 14 | 13 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
| 16 | 15 |
| 17 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonFontMutex); | |
| 18 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonShaderMutex); | |
| 19 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonPaintMutex); | |
| 20 SK_DECLARE_STATIC_MUTEX(gSkPDFCanonBitmapMutex); | |
| 21 | |
| 22 SkBaseMutex& SkPDFCanon::GetFontMutex() { return gSkPDFCanonFontMutex; } | |
| 23 SkBaseMutex& SkPDFCanon::GetShaderMutex() { return gSkPDFCanonShaderMutex; } | |
| 24 SkBaseMutex& SkPDFCanon::GetPaintMutex() { return gSkPDFCanonPaintMutex; } | |
| 25 SkBaseMutex& SkPDFCanon::GetBitmapMutex() { return gSkPDFCanonBitmapMutex; } | |
| 26 | |
| 27 SkPDFCanon::SkPDFCanon() {} | 16 SkPDFCanon::SkPDFCanon() {} |
| 28 | 17 |
| 29 SkPDFCanon::~SkPDFCanon() {} | 18 SkPDFCanon::~SkPDFCanon() {} |
| 30 | 19 |
| 31 SK_DECLARE_STATIC_LAZY_PTR(SkPDFCanon, singleton); | |
| 32 | |
| 33 SkPDFCanon& SkPDFCanon::GetCanon() { return *singleton.get(); } | |
| 34 | |
| 35 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
| 36 | 21 |
| 37 static void assert_mutex_held(const SkPDFCanon* canon, SkBaseMutex& mutex) { | |
| 38 if (canon == singleton.get()) { | |
| 39 mutex.assertHeld(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; } | 22 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; } |
| 44 | 23 |
| 45 template <typename T> | 24 template <typename T> |
| 46 bool remove_item(SkTDArray<T>* array, const T& elem) { | 25 bool remove_item(SkTDArray<T>* array, const T& elem) { |
| 47 int i = array->find(elem); | 26 int i = array->find(elem); |
| 48 if (i >= 0) { | 27 if (i >= 0) { |
| 49 array->removeShuffle(i); | 28 array->removeShuffle(i); |
| 50 return true; | 29 return true; |
| 51 } | 30 } |
| 52 return false; | 31 return false; |
| 53 } | 32 } |
| 54 | 33 |
| 55 // requires `bool T::equals(const U&) const` | 34 // requires `bool T::equals(const U&) const` |
| 56 template <typename T, typename U> | 35 template <typename T, typename U> |
| 57 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) { | 36 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) { |
| 58 for (int i = 0; i < ptrArray.count(); ++i) { | 37 for (int i = 0; i < ptrArray.count(); ++i) { |
| 59 if (ptrArray[i]->equals(object)) { | 38 if (ptrArray[i]->equals(object)) { |
| 60 return ptrArray[i]; | 39 return ptrArray[i]; |
| 61 } | 40 } |
| 62 } | 41 } |
| 63 return NULL; | 42 return NULL; |
| 64 } | 43 } |
| 65 | 44 |
| 66 //////////////////////////////////////////////////////////////////////////////// | 45 //////////////////////////////////////////////////////////////////////////////// |
| 67 | 46 |
| 68 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, | 47 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, |
| 69 uint16_t glyphID, | 48 uint16_t glyphID, |
| 70 SkPDFFont** relatedFontPtr) const { | 49 SkPDFFont** relatedFontPtr) const { |
| 71 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
| 72 SkASSERT(relatedFontPtr); | 50 SkASSERT(relatedFontPtr); |
| 73 | 51 |
| 74 SkPDFFont* relatedFont = NULL; | 52 SkPDFFont* relatedFont = NULL; |
| 75 for (int i = 0; i < fFontRecords.count(); ++i) { | 53 for (int i = 0; i < fFontRecords.count(); ++i) { |
| 76 SkPDFFont::Match match = SkPDFFont::IsMatch( | 54 SkPDFFont::Match match = SkPDFFont::IsMatch( |
| 77 fFontRecords[i].fFont, fFontRecords[i].fFontID, | 55 fFontRecords[i].fFont, fFontRecords[i].fFontID, |
| 78 fFontRecords[i].fGlyphID, fontID, glyphID); | 56 fFontRecords[i].fGlyphID, fontID, glyphID); |
| 79 if (SkPDFFont::kExact_Match == match) { | 57 if (SkPDFFont::kExact_Match == match) { |
| 80 return fFontRecords[i].fFont; | 58 return fFontRecords[i].fFont; |
| 81 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { | 59 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { |
| 82 relatedFont = fFontRecords[i].fFont; | 60 relatedFont = fFontRecords[i].fFont; |
| 83 } | 61 } |
| 84 } | 62 } |
| 85 *relatedFontPtr = relatedFont; // May still be NULL. | 63 *relatedFontPtr = relatedFont; // May still be NULL. |
| 86 return NULL; | 64 return NULL; |
| 87 } | 65 } |
| 88 | 66 |
| 89 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { | 67 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { |
| 90 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
| 91 SkPDFCanon::FontRec* rec = fFontRecords.push(); | 68 SkPDFCanon::FontRec* rec = fFontRecords.push(); |
| 92 rec->fFont = font; | 69 rec->fFont = font; |
| 93 rec->fFontID = fontID; | 70 rec->fFontID = fontID; |
| 94 rec->fGlyphID = fGlyphID; | 71 rec->fGlyphID = fGlyphID; |
| 95 } | 72 } |
| 96 | 73 |
| 97 void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { | 74 void SkPDFCanon::removeFont(SkPDFFont* pdfFont) { |
| 98 assert_mutex_held(this, gSkPDFCanonFontMutex); | |
| 99 for (int i = 0; i < fFontRecords.count(); i++) { | 75 for (int i = 0; i < fFontRecords.count(); i++) { |
| 100 if (fFontRecords[i].fFont == pdfFont) { | 76 if (fFontRecords[i].fFont == pdfFont) { |
| 101 fFontRecords.removeShuffle(i); | 77 fFontRecords.removeShuffle(i); |
| 102 return; | 78 return; |
| 103 } | 79 } |
| 104 } | 80 } |
| 105 // Not all SkPDFFonts are added to the Canon. | 81 // Not all SkPDFFonts are added to the Canon. |
| 106 } | 82 } |
| 107 | 83 |
| 108 //////////////////////////////////////////////////////////////////////////////// | 84 //////////////////////////////////////////////////////////////////////////////// |
| 109 | 85 |
| 110 SkPDFFunctionShader* SkPDFCanon::findFunctionShader( | 86 SkPDFFunctionShader* SkPDFCanon::findFunctionShader( |
| 111 const SkPDFShader::State& state) const { | 87 const SkPDFShader::State& state) const { |
| 112 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 113 return find_item(fFunctionShaderRecords, state); | 88 return find_item(fFunctionShaderRecords, state); |
| 114 } | 89 } |
| 115 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) { | 90 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) { |
| 116 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 117 fFunctionShaderRecords.push(assert_ptr(pdfShader)); | 91 fFunctionShaderRecords.push(assert_ptr(pdfShader)); |
| 118 } | 92 } |
| 119 void SkPDFCanon::removeFunctionShader(SkPDFFunctionShader* pdfShader) { | 93 void SkPDFCanon::removeFunctionShader(SkPDFFunctionShader* pdfShader) { |
| 120 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 121 SkAssertResult(remove_item(&fFunctionShaderRecords, pdfShader)); | 94 SkAssertResult(remove_item(&fFunctionShaderRecords, pdfShader)); |
| 122 } | 95 } |
| 123 | 96 |
| 124 //////////////////////////////////////////////////////////////////////////////// | 97 //////////////////////////////////////////////////////////////////////////////// |
| 125 | 98 |
| 126 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader( | 99 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader( |
| 127 const SkPDFShader::State& state) const { | 100 const SkPDFShader::State& state) const { |
| 128 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 129 return find_item(fAlphaShaderRecords, state); | 101 return find_item(fAlphaShaderRecords, state); |
| 130 } | 102 } |
| 131 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) { | 103 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) { |
| 132 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 133 fAlphaShaderRecords.push(assert_ptr(pdfShader)); | 104 fAlphaShaderRecords.push(assert_ptr(pdfShader)); |
| 134 } | 105 } |
| 135 void SkPDFCanon::removeAlphaShader(SkPDFAlphaFunctionShader* pdfShader) { | 106 void SkPDFCanon::removeAlphaShader(SkPDFAlphaFunctionShader* pdfShader) { |
| 136 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 137 SkAssertResult(remove_item(&fAlphaShaderRecords, pdfShader)); | 107 SkAssertResult(remove_item(&fAlphaShaderRecords, pdfShader)); |
| 138 } | 108 } |
| 139 | 109 |
| 140 //////////////////////////////////////////////////////////////////////////////// | 110 //////////////////////////////////////////////////////////////////////////////// |
| 141 | 111 |
| 142 SkPDFImageShader* SkPDFCanon::findImageShader( | 112 SkPDFImageShader* SkPDFCanon::findImageShader( |
| 143 const SkPDFShader::State& state) const { | 113 const SkPDFShader::State& state) const { |
| 144 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 145 return find_item(fImageShaderRecords, state); | 114 return find_item(fImageShaderRecords, state); |
| 146 } | 115 } |
| 147 | 116 |
| 148 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) { | 117 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) { |
| 149 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 150 fImageShaderRecords.push(assert_ptr(pdfShader)); | 118 fImageShaderRecords.push(assert_ptr(pdfShader)); |
| 151 } | 119 } |
| 152 | 120 |
| 153 void SkPDFCanon::removeImageShader(SkPDFImageShader* pdfShader) { | 121 void SkPDFCanon::removeImageShader(SkPDFImageShader* pdfShader) { |
| 154 assert_mutex_held(this, gSkPDFCanonShaderMutex); | |
| 155 SkAssertResult(remove_item(&fImageShaderRecords, pdfShader)); | 122 SkAssertResult(remove_item(&fImageShaderRecords, pdfShader)); |
| 156 } | 123 } |
| 157 | 124 |
| 158 //////////////////////////////////////////////////////////////////////////////// | 125 //////////////////////////////////////////////////////////////////////////////// |
| 159 | 126 |
| 160 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { | 127 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { |
| 161 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
| 162 return find_item(fGraphicStateRecords, paint); | 128 return find_item(fGraphicStateRecords, paint); |
| 163 } | 129 } |
| 164 | 130 |
| 165 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { | 131 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { |
| 166 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
| 167 fGraphicStateRecords.push(assert_ptr(state)); | 132 fGraphicStateRecords.push(assert_ptr(state)); |
| 168 } | 133 } |
| 169 | 134 |
| 170 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { | 135 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { |
| 171 assert_mutex_held(this, gSkPDFCanonPaintMutex); | |
| 172 SkAssertResult(remove_item(&fGraphicStateRecords, pdfGraphicState)); | 136 SkAssertResult(remove_item(&fGraphicStateRecords, pdfGraphicState)); |
| 173 } | 137 } |
| 174 | 138 |
| 175 //////////////////////////////////////////////////////////////////////////////// | 139 //////////////////////////////////////////////////////////////////////////////// |
| 176 | 140 |
| 177 SkPDFBitmap* SkPDFCanon::findBitmap(const SkBitmap& bm) const { | 141 SkPDFBitmap* SkPDFCanon::findBitmap(const SkBitmap& bm) const { |
| 178 assert_mutex_held(this, gSkPDFCanonBitmapMutex); | |
| 179 return find_item(fBitmapRecords, bm); | 142 return find_item(fBitmapRecords, bm); |
| 180 } | 143 } |
| 181 | 144 |
| 182 void SkPDFCanon::addBitmap(SkPDFBitmap* pdfBitmap) { | 145 void SkPDFCanon::addBitmap(SkPDFBitmap* pdfBitmap) { |
| 183 assert_mutex_held(this, gSkPDFCanonBitmapMutex); | |
| 184 fBitmapRecords.push(assert_ptr(pdfBitmap)); | 146 fBitmapRecords.push(assert_ptr(pdfBitmap)); |
| 185 } | 147 } |
| 186 | 148 |
| 187 void SkPDFCanon::removeBitmap(SkPDFBitmap* pdfBitmap) { | 149 void SkPDFCanon::removeBitmap(SkPDFBitmap* pdfBitmap) { |
| 188 assert_mutex_held(this, gSkPDFCanonBitmapMutex); | |
| 189 SkAssertResult(remove_item(&fBitmapRecords, pdfBitmap)); | 150 SkAssertResult(remove_item(&fBitmapRecords, pdfBitmap)); |
| 190 } | 151 } |
| OLD | NEW |