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

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

Issue 2193973002: SkPDF: PDFShader code modernized. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-07-29 (Friday) 17:54:35 EDT Created 4 years, 4 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 "SkImage.h" 8 #include "SkImage.h"
9 #include "SkPDFBitmap.h" 9 #include "SkPDFBitmap.h"
10 #include "SkPDFCanon.h" 10 #include "SkPDFCanon.h"
11 #include "SkPDFFont.h" 11 #include "SkPDFFont.h"
12 12
13 //////////////////////////////////////////////////////////////////////////////// 13 ////////////////////////////////////////////////////////////////////////////////
14 14
15 void SkPDFCanon::reset() { 15 void SkPDFCanon::reset() {
16 for (int i = 0; i < fFontRecords.count(); ++i) { 16 for (int i = 0; i < fFontRecords.count(); ++i) {
17 fFontRecords[i].fFont->unref(); 17 fFontRecords[i].fFont->unref();
18 } 18 }
19 fFontRecords.reset(); 19 fFontRecords.reset();
20 fFunctionShaderRecords.unrefAll(); 20
21 fFunctionShaderRecords.reset(); 21 fFunctionShaderRecords.reset();
22 fAlphaShaderRecords.unrefAll();
23 fAlphaShaderRecords.reset(); 22 fAlphaShaderRecords.reset();
24 fImageShaderRecords.unrefAll();
25 fImageShaderRecords.reset(); 23 fImageShaderRecords.reset();
24
26 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); }); 25 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); });
tomhudson 2016/08/02 20:59:12 Nit: TODO(halcanary) replace with sk_sp<> so we do
hal.canary 2016/08/02 21:25:01 Done.
27 fGraphicStateRecords.reset(); 26 fGraphicStateRecords.reset();
28 27
29 fPDFBitmapMap.foreach([](SkBitmapKey, SkPDFObject** p) { (*p)->unref(); }); 28 fPDFBitmapMap.foreach([](SkBitmapKey, SkPDFObject** p) { (*p)->unref(); });
30 fPDFBitmapMap.reset(); 29 fPDFBitmapMap.reset();
31 } 30 }
32 31
33 //////////////////////////////////////////////////////////////////////////////// 32 ////////////////////////////////////////////////////////////////////////////////
34 33
35 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; }
36
37 // requires `bool T::equals(const U&) const`
38 template <typename T, typename U>
39 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) {
40 for (int i = 0; i < ptrArray.count(); ++i) {
41 if (ptrArray[i]->equals(object)) {
42 return ptrArray[i];
43 }
44 }
45 return nullptr;
46 }
47
48 ////////////////////////////////////////////////////////////////////////////////
49
50 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID, 34 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID,
51 uint16_t glyphID, 35 uint16_t glyphID,
52 SkPDFFont** relatedFontPtr) const { 36 SkPDFFont** relatedFontPtr) const {
53 SkASSERT(relatedFontPtr); 37 SkASSERT(relatedFontPtr);
54 38
55 SkPDFFont* relatedFont = nullptr; 39 SkPDFFont* relatedFont = nullptr;
56 for (int i = 0; i < fFontRecords.count(); ++i) { 40 for (int i = 0; i < fFontRecords.count(); ++i) {
57 SkPDFFont::Match match = SkPDFFont::IsMatch( 41 SkPDFFont::Match match = SkPDFFont::IsMatch(
58 fFontRecords[i].fFont, fFontRecords[i].fFontID, 42 fFontRecords[i].fFont, fFontRecords[i].fFontID,
59 fFontRecords[i].fGlyphID, fontID, glyphID); 43 fFontRecords[i].fGlyphID, fontID, glyphID);
60 if (SkPDFFont::kExact_Match == match) { 44 if (SkPDFFont::kExact_Match == match) {
61 return fFontRecords[i].fFont; 45 return fFontRecords[i].fFont;
62 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { 46 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) {
63 relatedFont = fFontRecords[i].fFont; 47 relatedFont = fFontRecords[i].fFont;
64 } 48 }
65 } 49 }
66 *relatedFontPtr = relatedFont; // May still be nullptr. 50 *relatedFontPtr = relatedFont; // May still be nullptr.
67 return nullptr; 51 return nullptr;
68 } 52 }
69 53
70 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { 54 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) {
71 SkPDFCanon::FontRec* rec = fFontRecords.push(); 55 SkPDFCanon::FontRec* rec = fFontRecords.push();
72 rec->fFont = SkRef(font); 56 rec->fFont = SkRef(font);
73 rec->fFontID = fontID; 57 rec->fFontID = fontID;
74 rec->fGlyphID = fGlyphID; 58 rec->fGlyphID = fGlyphID;
75 } 59 }
76 60
77 //////////////////////////////////////////////////////////////////////////////// 61 ////////////////////////////////////////////////////////////////////////////////
78 62
79 SkPDFFunctionShader* SkPDFCanon::findFunctionShader( 63 template <typename T>
64 sk_sp<SkPDFObject> find_shader(const SkTArray<T>& records,
65 const SkPDFShader::State& state) {
66 for (const T& record : records) {
67 if (record.fShaderState == state) {
68 return record.fShaderObject;
69 }
70 }
71 return nullptr;
72 }
73
74 sk_sp<SkPDFObject> SkPDFCanon::findFunctionShader(
80 const SkPDFShader::State& state) const { 75 const SkPDFShader::State& state) const {
81 return find_item(fFunctionShaderRecords, state); 76 return find_shader(fFunctionShaderRecords, state);
82 } 77 }
83 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) { 78 void SkPDFCanon::addFunctionShader(sk_sp<SkPDFObject> pdfShader,
84 fFunctionShaderRecords.push(SkRef(pdfShader)); 79 SkPDFShader::State state) {
80 fFunctionShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
81 }
82
83 sk_sp<SkPDFObject> SkPDFCanon::findAlphaShader(
84 const SkPDFShader::State& state) const {
85 return find_shader(fAlphaShaderRecords, state);
86 }
87 void SkPDFCanon::addAlphaShader(sk_sp<SkPDFObject> pdfShader,
88 SkPDFShader::State state) {
89 fAlphaShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
90 }
91
92 sk_sp<SkPDFObject> SkPDFCanon::findImageShader(
93 const SkPDFShader::State& state) const {
94 return find_shader(fImageShaderRecords, state);
95 }
96
97 void SkPDFCanon::addImageShader(sk_sp<SkPDFObject> pdfShader,
98 SkPDFShader::State state) {
99 fImageShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
85 } 100 }
86 101
87 //////////////////////////////////////////////////////////////////////////////// 102 ////////////////////////////////////////////////////////////////////////////////
88
89 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader(
90 const SkPDFShader::State& state) const {
91 return find_item(fAlphaShaderRecords, state);
92 }
93 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
94 fAlphaShaderRecords.push(SkRef(pdfShader));
95 }
96
97 ////////////////////////////////////////////////////////////////////////////////
98
99 SkPDFImageShader* SkPDFCanon::findImageShader(
100 const SkPDFShader::State& state) const {
101 return find_item(fImageShaderRecords, state);
102 }
103
104 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) {
105 fImageShaderRecords.push(SkRef(pdfShader));
106 }
107
108 ////////////////////////////////////////////////////////////////////////////////
109 103
110 const SkPDFGraphicState* SkPDFCanon::findGraphicState( 104 const SkPDFGraphicState* SkPDFCanon::findGraphicState(
111 const SkPDFGraphicState& key) const { 105 const SkPDFGraphicState& key) const {
112 const WrapGS* ptr = fGraphicStateRecords.find(WrapGS(&key)); 106 const WrapGS* ptr = fGraphicStateRecords.find(WrapGS(&key));
113 return ptr ? ptr->fPtr : nullptr; 107 return ptr ? ptr->fPtr : nullptr;
114 } 108 }
115 109
116 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) { 110 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) {
117 SkASSERT(state); 111 SkASSERT(state);
118 WrapGS w(SkRef(state)); 112 WrapGS w(SkRef(state));
(...skipping 28 matching lines...) Expand all
147 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState(); 141 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState();
148 return fNoSmaskGraphicState; 142 return fNoSmaskGraphicState;
149 } 143 }
150 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() { 144 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() {
151 if (fRangeObject) { 145 if (fRangeObject) {
152 return fRangeObject; 146 return fRangeObject;
153 } 147 }
154 fRangeObject = SkPDFShader::MakeRangeObject(); 148 fRangeObject = SkPDFShader::MakeRangeObject();
155 return fRangeObject; 149 return fRangeObject;
156 } 150 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698