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

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

Issue 966863002: PDF: Canon now owns a reference to all interned objects (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-02-27 (Friday) 15:27:08 EST Created 5 years, 9 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/SkPDFFont.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 "SkPDFBitmap.h" 8 #include "SkPDFBitmap.h"
9 #include "SkPDFCanon.h" 9 #include "SkPDFCanon.h"
10 #include "SkPDFFont.h" 10 #include "SkPDFFont.h"
11 #include "SkPDFGraphicState.h" 11 #include "SkPDFGraphicState.h"
12 #include "SkPDFShader.h" 12 #include "SkPDFShader.h"
13 13
14 //////////////////////////////////////////////////////////////////////////////// 14 ////////////////////////////////////////////////////////////////////////////////
15 15
16 SkPDFCanon::SkPDFCanon() {} 16 void SkPDFCanon::reset() {
17 17 for (int i = 0; i < fFontRecords.count(); ++i) {
18 SkPDFCanon::~SkPDFCanon() {} 18 fFontRecords[i].fFont->unref();
19 }
20 fFontRecords.reset();
21 fFunctionShaderRecords.unrefAll();
22 fFunctionShaderRecords.reset();
23 fAlphaShaderRecords.unrefAll();
24 fAlphaShaderRecords.reset();
25 fImageShaderRecords.unrefAll();
26 fImageShaderRecords.reset();
27 fGraphicStateRecords.unrefAll();
28 fGraphicStateRecords.reset();
29 fBitmapRecords.unrefAll();
30 fBitmapRecords.reset();
31 }
19 32
20 //////////////////////////////////////////////////////////////////////////////// 33 ////////////////////////////////////////////////////////////////////////////////
21 34
22 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; } 35 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; }
23 36
24 template <typename T>
25 bool remove_item(SkTDArray<T>* array, const T& elem) {
26 int i = array->find(elem);
27 if (i >= 0) {
28 array->removeShuffle(i);
29 return true;
30 }
31 return false;
32 }
33
34 // requires `bool T::equals(const U&) const` 37 // requires `bool T::equals(const U&) const`
35 template <typename T, typename U> 38 template <typename T, typename U>
36 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) { 39 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) {
37 for (int i = 0; i < ptrArray.count(); ++i) { 40 for (int i = 0; i < ptrArray.count(); ++i) {
38 if (ptrArray[i]->equals(object)) { 41 if (ptrArray[i]->equals(object)) {
39 return ptrArray[i]; 42 return ptrArray[i];
40 } 43 }
41 } 44 }
42 return NULL; 45 return NULL;
43 } 46 }
(...skipping 15 matching lines...) Expand all
59 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) { 62 } else if (!relatedFont && SkPDFFont::kRelated_Match == match) {
60 relatedFont = fFontRecords[i].fFont; 63 relatedFont = fFontRecords[i].fFont;
61 } 64 }
62 } 65 }
63 *relatedFontPtr = relatedFont; // May still be NULL. 66 *relatedFontPtr = relatedFont; // May still be NULL.
64 return NULL; 67 return NULL;
65 } 68 }
66 69
67 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) { 70 void SkPDFCanon::addFont(SkPDFFont* font, uint32_t fontID, uint16_t fGlyphID) {
68 SkPDFCanon::FontRec* rec = fFontRecords.push(); 71 SkPDFCanon::FontRec* rec = fFontRecords.push();
69 rec->fFont = font; 72 rec->fFont = SkRef(font);
70 rec->fFontID = fontID; 73 rec->fFontID = fontID;
71 rec->fGlyphID = fGlyphID; 74 rec->fGlyphID = fGlyphID;
72 } 75 }
73 76
74 void SkPDFCanon::removeFont(SkPDFFont* pdfFont) {
75 for (int i = 0; i < fFontRecords.count(); i++) {
76 if (fFontRecords[i].fFont == pdfFont) {
77 fFontRecords.removeShuffle(i);
78 return;
79 }
80 }
81 // Not all SkPDFFonts are added to the Canon.
82 }
83
84 //////////////////////////////////////////////////////////////////////////////// 77 ////////////////////////////////////////////////////////////////////////////////
85 78
86 SkPDFFunctionShader* SkPDFCanon::findFunctionShader( 79 SkPDFFunctionShader* SkPDFCanon::findFunctionShader(
87 const SkPDFShader::State& state) const { 80 const SkPDFShader::State& state) const {
88 return find_item(fFunctionShaderRecords, state); 81 return find_item(fFunctionShaderRecords, state);
89 } 82 }
90 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) { 83 void SkPDFCanon::addFunctionShader(SkPDFFunctionShader* pdfShader) {
91 fFunctionShaderRecords.push(assert_ptr(pdfShader)); 84 fFunctionShaderRecords.push(SkRef(pdfShader));
92 }
93 void SkPDFCanon::removeFunctionShader(SkPDFFunctionShader* pdfShader) {
94 SkAssertResult(remove_item(&fFunctionShaderRecords, pdfShader));
95 } 85 }
96 86
97 //////////////////////////////////////////////////////////////////////////////// 87 ////////////////////////////////////////////////////////////////////////////////
98 88
99 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader( 89 SkPDFAlphaFunctionShader* SkPDFCanon::findAlphaShader(
100 const SkPDFShader::State& state) const { 90 const SkPDFShader::State& state) const {
101 return find_item(fAlphaShaderRecords, state); 91 return find_item(fAlphaShaderRecords, state);
102 } 92 }
103 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) { 93 void SkPDFCanon::addAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
104 fAlphaShaderRecords.push(assert_ptr(pdfShader)); 94 fAlphaShaderRecords.push(SkRef(pdfShader));
105 }
106 void SkPDFCanon::removeAlphaShader(SkPDFAlphaFunctionShader* pdfShader) {
107 SkAssertResult(remove_item(&fAlphaShaderRecords, pdfShader));
108 } 95 }
109 96
110 //////////////////////////////////////////////////////////////////////////////// 97 ////////////////////////////////////////////////////////////////////////////////
111 98
112 SkPDFImageShader* SkPDFCanon::findImageShader( 99 SkPDFImageShader* SkPDFCanon::findImageShader(
113 const SkPDFShader::State& state) const { 100 const SkPDFShader::State& state) const {
114 return find_item(fImageShaderRecords, state); 101 return find_item(fImageShaderRecords, state);
115 } 102 }
116 103
117 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) { 104 void SkPDFCanon::addImageShader(SkPDFImageShader* pdfShader) {
118 fImageShaderRecords.push(assert_ptr(pdfShader)); 105 fImageShaderRecords.push(SkRef(pdfShader));
119 }
120
121 void SkPDFCanon::removeImageShader(SkPDFImageShader* pdfShader) {
122 SkAssertResult(remove_item(&fImageShaderRecords, pdfShader));
123 } 106 }
124 107
125 //////////////////////////////////////////////////////////////////////////////// 108 ////////////////////////////////////////////////////////////////////////////////
126 109
127 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const { 110 SkPDFGraphicState* SkPDFCanon::findGraphicState(const SkPaint& paint) const {
128 return find_item(fGraphicStateRecords, paint); 111 return find_item(fGraphicStateRecords, paint);
129 } 112 }
130 113
131 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) { 114 void SkPDFCanon::addGraphicState(SkPDFGraphicState* state) {
132 fGraphicStateRecords.push(assert_ptr(state)); 115 fGraphicStateRecords.push(SkRef(state));
133 }
134
135 void SkPDFCanon::removeGraphicState(SkPDFGraphicState* pdfGraphicState) {
136 SkAssertResult(remove_item(&fGraphicStateRecords, pdfGraphicState));
137 } 116 }
138 117
139 //////////////////////////////////////////////////////////////////////////////// 118 ////////////////////////////////////////////////////////////////////////////////
140 119
141 SkPDFBitmap* SkPDFCanon::findBitmap(const SkBitmap& bm) const { 120 SkPDFBitmap* SkPDFCanon::findBitmap(const SkBitmap& bm) const {
142 return find_item(fBitmapRecords, bm); 121 return find_item(fBitmapRecords, bm);
143 } 122 }
144 123
145 void SkPDFCanon::addBitmap(SkPDFBitmap* pdfBitmap) { 124 void SkPDFCanon::addBitmap(SkPDFBitmap* pdfBitmap) {
146 fBitmapRecords.push(assert_ptr(pdfBitmap)); 125 fBitmapRecords.push(SkRef(pdfBitmap));
147 } 126 }
148
149 void SkPDFCanon::removeBitmap(SkPDFBitmap* pdfBitmap) {
150 SkAssertResult(remove_item(&fBitmapRecords, pdfBitmap));
151 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFCanon.h ('k') | src/pdf/SkPDFFont.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698