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

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-08-02 (Tuesday) 17:24:52 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
« no previous file with comments | « src/pdf/SkPDFCanon.h ('k') | src/pdf/SkPDFDevice.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
25 // TODO(halcanary): make SkTHashSet work nicely with sk_sp<>,
26 // or use std::unordered_set<>
26 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); }); 27 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); });
27 fGraphicStateRecords.reset(); 28 fGraphicStateRecords.reset();
28 29
29 fPDFBitmapMap.foreach([](SkBitmapKey, SkPDFObject** p) { (*p)->unref(); }); 30 fPDFBitmapMap.foreach([](SkBitmapKey, SkPDFObject** p) { (*p)->unref(); });
30 fPDFBitmapMap.reset(); 31 fPDFBitmapMap.reset();
31 } 32 }
32 33
33 //////////////////////////////////////////////////////////////////////////////// 34 ////////////////////////////////////////////////////////////////////////////////
34 35
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, 36 SkPDFFont* SkPDFCanon::findFont(uint32_t fontID,
51 uint16_t glyphID, 37 uint16_t glyphID,
52 SkPDFFont** relatedFontPtr) const { 38 SkPDFFont** relatedFontPtr) const {
53 SkASSERT(relatedFontPtr); 39 SkASSERT(relatedFontPtr);
54 40
55 SkPDFFont* relatedFont = nullptr; 41 SkPDFFont* relatedFont = nullptr;
56 for (int i = 0; i < fFontRecords.count(); ++i) { 42 for (int i = 0; i < fFontRecords.count(); ++i) {
57 SkPDFFont::Match match = SkPDFFont::IsMatch( 43 SkPDFFont::Match match = SkPDFFont::IsMatch(
58 fFontRecords[i].fFont, fFontRecords[i].fFontID, 44 fFontRecords[i].fFont, fFontRecords[i].fFontID,
59 fFontRecords[i].fGlyphID, fontID, glyphID); 45 fFontRecords[i].fGlyphID, fontID, glyphID);
60 if (SkPDFFont::kExact_Match == match) { 46 if (SkPDFFont::kExact_Match == match) {
61 return fFontRecords[i].fFont; 47 return fFontRecords[i].fFont;
62 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { 48 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) {
63 relatedFont = fFontRecords[i].fFont; 49 relatedFont = fFontRecords[i].fFont;
64 } 50 }
65 } 51 }
66 *relatedFontPtr = relatedFont; // May still be nullptr. 52 *relatedFontPtr = relatedFont; // May still be nullptr.
67 return nullptr; 53 return nullptr;
68 } 54 }
69 55
70 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { 56 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) {
71 SkPDFCanon::FontRec* rec = fFontRecords.push(); 57 SkPDFCanon::FontRec* rec = fFontRecords.push();
72 rec->fFont = SkRef(font); 58 rec->fFont = SkRef(font);
73 rec->fFontID = fontID; 59 rec->fFontID = fontID;
74 rec->fGlyphID = fGlyphID; 60 rec->fGlyphID = fGlyphID;
75 } 61 }
76 62
77 //////////////////////////////////////////////////////////////////////////////// 63 ////////////////////////////////////////////////////////////////////////////////
78 64
79 SkPDFFunctionShader* SkPDFCanon::findFunctionShader( 65 template <typename T>
66 sk_sp<SkPDFObject> find_shader(const SkTArray<T>& records,
67 const SkPDFShader::State& state) {
68 for (const T& record : records) {
69 if (record.fShaderState == state) {
70 return record.fShaderObject;
71 }
72 }
73 return nullptr;
74 }
75
76 sk_sp<SkPDFObject> SkPDFCanon::findFunctionShader(
80 const SkPDFShader::State& state) const { 77 const SkPDFShader::State& state) const {
81 return find_item(fFunctionShaderRecords, state); 78 return find_shader(fFunctionShaderRecords, state);
82 } 79 }
83 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) { 80 void SkPDFCanon::addFunctionShader(sk_sp<SkPDFObject> pdfShader,
84 fFunctionShaderRecords.push(SkRef(pdfShader)); 81 SkPDFShader::State state) {
82 fFunctionShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
83 }
84
85 sk_sp<SkPDFObject> SkPDFCanon::findAlphaShader(
86 const SkPDFShader::State& state) const {
87 return find_shader(fAlphaShaderRecords, state);
88 }
89 void SkPDFCanon::addAlphaShader(sk_sp<SkPDFObject> pdfShader,
90 SkPDFShader::State state) {
91 fAlphaShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
92 }
93
94 sk_sp<SkPDFObject> SkPDFCanon::findImageShader(
95 const SkPDFShader::State& state) const {
96 return find_shader(fImageShaderRecords, state);
97 }
98
99 void SkPDFCanon::addImageShader(sk_sp<SkPDFObject> pdfShader,
100 SkPDFShader::State state) {
101 fImageShaderRecords.emplace_back(std::move(state), std::move(pdfShader));
85 } 102 }
86 103
87 //////////////////////////////////////////////////////////////////////////////// 104 ////////////////////////////////////////////////////////////////////////////////
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 105
110 const SkPDFGraphicState* SkPDFCanon::findGraphicState( 106 const SkPDFGraphicState* SkPDFCanon::findGraphicState(
111 const SkPDFGraphicState& key) const { 107 const SkPDFGraphicState& key) const {
112 const WrapGS* ptr = fGraphicStateRecords.find(WrapGS(&key)); 108 const WrapGS* ptr = fGraphicStateRecords.find(WrapGS(&key));
113 return ptr ? ptr->fPtr : nullptr; 109 return ptr ? ptr->fPtr : nullptr;
114 } 110 }
115 111
116 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) { 112 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) {
117 SkASSERT(state); 113 SkASSERT(state);
118 WrapGS w(SkRef(state)); 114 WrapGS w(SkRef(state));
(...skipping 28 matching lines...) Expand all
147 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState(); 143 fNoSmaskGraphicState = SkPDFGraphicState::MakeNoSmaskGraphicState();
148 return fNoSmaskGraphicState; 144 return fNoSmaskGraphicState;
149 } 145 }
150 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() { 146 sk_sp<SkPDFArray> SkPDFCanon::makeRangeObject() {
151 if (fRangeObject) { 147 if (fRangeObject) {
152 return fRangeObject; 148 return fRangeObject;
153 } 149 }
154 fRangeObject = SkPDFShader::MakeRangeObject(); 150 fRangeObject = SkPDFShader::MakeRangeObject();
155 return fRangeObject; 151 return fRangeObject;
156 } 152 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFCanon.h ('k') | src/pdf/SkPDFDevice.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698