Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: src/pdf/SkPDFCanon.cpp

Issue 873543002: More changes to SkPDFShader to eliminate multiple inheritance! (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 8 #include "SkLazyPtr.h"
9 #include "SkPDFCanon.h" 9 #include "SkPDFCanon.h"
10 #include "SkPDFFont.h" 10 #include "SkPDFFont.h"
(...skipping 19 matching lines...) Expand all
30 SkPDFCanon& SkPDFCanon::GetCanon() { return *singleton.get(); } 30 SkPDFCanon& SkPDFCanon::GetCanon() { return *singleton.get(); }
31 31
32 //////////////////////////////////////////////////////////////////////////////// 32 ////////////////////////////////////////////////////////////////////////////////
33 33
34 static void assert_mutex_held(const SkPDFCanon* canon, SkBaseMutex& mutex) { 34 static void assert_mutex_held(const SkPDFCanon* canon, SkBaseMutex& mutex) {
35 if (canon == singleton.get()) { 35 if (canon == singleton.get()) {
36 mutex.assertHeld(); 36 mutex.assertHeld();
37 } 37 }
38 } 38 }
39 39
40 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; }
41
42 // TODO(halcanary): add this method to SkTDArray.
mtklein 2015/01/23 14:16:11 I'm on board with a ? at the end. Generally I don
43 template <typename T>
44 bool remove_item(SkTDArray<T>* array, const T& elem) {
45 int i = array->find(elem);
46 if (i >= 0) {
47 array->removeShuffle(i);
48 return true;
49 }
50 return false;
51 }
52
53 // requires `bool T::equals(const U&) const`
54 template <typename T, typename U>
55 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) {
mtklein 2015/01/23 14:16:11 consider object -> key ?
56 for (int i = 0; i < ptrArray.count(); ++i) {
57 if (ptrArray[i]->equals(object)) {
58 return ptrArray[i];
59 }
60 }
61 return NULL;
62 }
63
64 ////////////////////////////////////////////////////////////////////////////////
65
40 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, 66 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID,
41 uint16_t glyphID, 67 uint16_t glyphID,
42 SkPDFFont** relatedFontPtr) const { 68 SkPDFFont** relatedFontPtr) const {
43 assert_mutex_held(this, gSkPDFCanonFontMutex); 69 assert_mutex_held(this, gSkPDFCanonFontMutex);
44 SkASSERT(relatedFontPtr); 70 SkASSERT(relatedFontPtr);
45 71
46 SkPDFFont* relatedFont = NULL; 72 SkPDFFont* relatedFont = NULL;
47 for (int i = 0; i < fFontRecords.count(); ++i) { 73 for (int i = 0; i < fFontRecords.count(); ++i) {
48 SkPDFFont::Match match = SkPDFFont::IsMatch( 74 SkPDFFont::Match match = SkPDFFont::IsMatch(
49 fFontRecords[i].fFont, fFontRecords[i].fFontID, 75 fFontRecords[i].fFont, fFontRecords[i].fFontID,
(...skipping 22 matching lines...) Expand all
72 if (fFontRecords[i].fFont == pdfFont) { 98 if (fFontRecords[i].fFont == pdfFont) {
73 fFontRecords.removeShuffle(i); 99 fFontRecords.removeShuffle(i);
74 return; 100 return;
75 } 101 }
76 } 102 }
77 // Not all SkPDFFonts are added to the Canon. 103 // Not all SkPDFFonts are added to the Canon.
78 } 104 }
79 105
80 //////////////////////////////////////////////////////////////////////////////// 106 ////////////////////////////////////////////////////////////////////////////////
81 107
82 SkPDFShader* SkPDFCanon::findShader(const SkPDFShader::State& state) const { 108 SkPDFFunctionShader* SkPDFCanon::findFunctionShader(
109 const SkPDFShader::State& state) const {
83 assert_mutex_held(this, gSkPDFCanonShaderMutex); 110 assert_mutex_held(this, gSkPDFCanonShaderMutex);
84 for (int i = 0; i < fShaderRecords.count(); ++i) { 111 return find_item(fFunctionShaderRecords, state);
85 if (fShaderRecords[i]->equals(state)) { 112 }
86 return fShaderRecords[i]; 113 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) {
87 } 114 assert_mutex_held(this, gSkPDFCanonShaderMutex);
88 } 115 fFunctionShaderRecords.push(assert_ptr(pdfShader));
89 return NULL; 116 }
117 void SkPDFCanon::removeFunctionShader(SkPDFFunctionShader* pdfShader) {
118 assert_mutex_held(this, gSkPDFCanonShaderMutex);
119 SkAssertResult(remove_item(&fFunctionShaderRecords, pdfShader));
90 } 120 }
91 121
92 void SkPDFCanon::addShader(SkPDFShader* shader) { 122 ////////////////////////////////////////////////////////////////////////////////
123
124 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader(
125 const SkPDFShader::State& state) const {
93 assert_mutex_held(this, gSkPDFCanonShaderMutex); 126 assert_mutex_held(this, gSkPDFCanonShaderMutex);
94 SkASSERT(shader); 127 return find_item(fAlphaShaderRecords, state);
95 fShaderRecords.push(shader); 128 }
129 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
130 assert_mutex_held(this, gSkPDFCanonShaderMutex);
131 fAlphaShaderRecords.push(assert_ptr(pdfShader));
132 }
133 void SkPDFCanon::removeAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
134 assert_mutex_held(this, gSkPDFCanonShaderMutex);
135 SkAssertResult(remove_item(&fAlphaShaderRecords, pdfShader));
96 } 136 }
97 137
98 void SkPDFCanon::removeShader(SkPDFShader* pdfShader) { 138 ////////////////////////////////////////////////////////////////////////////////
139
140 SkPDFImageShader* SkPDFCanon::findImageShader(
141 const SkPDFShader::State& state) const {
99 assert_mutex_held(this, gSkPDFCanonShaderMutex); 142 assert_mutex_held(this, gSkPDFCanonShaderMutex);
100 for (int i = 0; i < fShaderRecords.count(); ++i) { 143 return find_item(fImageShaderRecords, state);
101 if (fShaderRecords[i] == pdfShader) { 144 }
102 fShaderRecords.removeShuffle(i); 145
103 return; 146 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) {
104 } 147 assert_mutex_held(this, gSkPDFCanonShaderMutex);
105 } 148 fImageShaderRecords.push(assert_ptr(pdfShader));
106 SkDEBUGFAIL("pdfShader not found"); 149 }
150
151 void SkPDFCanon::removeImageShader(SkPDFImageShader* pdfShader) {
152 assert_mutex_held(this, gSkPDFCanonShaderMutex);
153 SkAssertResult(remove_item(&fImageShaderRecords, pdfShader));
107 } 154 }
108 155
109 //////////////////////////////////////////////////////////////////////////////// 156 ////////////////////////////////////////////////////////////////////////////////
110 157
111 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { 158 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const {
112 assert_mutex_held(this, gSkPDFCanonPaintMutex); 159 assert_mutex_held(this, gSkPDFCanonPaintMutex);
113 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { 160 return find_item(fGraphicStateRecords, paint);
114 if (fGraphicStateRecords[i]->equals(paint)) {
115 return fGraphicStateRecords[i];
116 }
117 }
118 return NULL;
119 } 161 }
120 162
121 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { 163 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) {
122 assert_mutex_held(this, gSkPDFCanonPaintMutex); 164 assert_mutex_held(this, gSkPDFCanonPaintMutex);
123 SkASSERT(state); 165 fGraphicStateRecords.push(assert_ptr(state));
124 fGraphicStateRecords.push(state);
125 } 166 }
126 167
127 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) { 168 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) {
128 assert_mutex_held(this, gSkPDFCanonPaintMutex); 169 assert_mutex_held(this, gSkPDFCanonPaintMutex);
129 for (int i = 0; i < fGraphicStateRecords.count(); ++i) { 170 SkAssertResult(remove_item(&fGraphicStateRecords, pdfGraphicState));
130 if (fGraphicStateRecords[i] == pdfGraphicState) {
131 fGraphicStateRecords.removeShuffle(i);
132 return;
133 }
134 }
135 SkDEBUGFAIL("pdfGraphicState not found");
136 } 171 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFCanon.h ('k') | src/pdf/SkPDFShader.h » ('j') | src/pdf/SkPDFShader.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698