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

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

Issue 1372783003: SkPDF: Implement drawImage*() properly (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-09-29 (Tuesday) 14:09:03 EDT Created 5 years, 2 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 "SkPDFBitmap.h" 9 #include "SkPDFBitmap.h"
9 #include "SkPDFCanon.h" 10 #include "SkPDFCanon.h"
10 #include "SkPDFFont.h" 11 #include "SkPDFFont.h"
11 #include "SkPDFShader.h" 12 #include "SkPDFShader.h"
12 13
13 //////////////////////////////////////////////////////////////////////////////// 14 ////////////////////////////////////////////////////////////////////////////////
14 15
15 void SkPDFCanon::reset() { 16 void SkPDFCanon::reset() {
16 for (int i = 0; i < fFontRecords.count(); ++i) { 17 for (int i = 0; i < fFontRecords.count(); ++i) {
17 fFontRecords[i].fFont->unref(); 18 fFontRecords[i].fFont->unref();
18 } 19 }
19 fFontRecords.reset(); 20 fFontRecords.reset();
20 fFunctionShaderRecords.unrefAll(); 21 fFunctionShaderRecords.unrefAll();
21 fFunctionShaderRecords.reset(); 22 fFunctionShaderRecords.reset();
22 fAlphaShaderRecords.unrefAll(); 23 fAlphaShaderRecords.unrefAll();
23 fAlphaShaderRecords.reset(); 24 fAlphaShaderRecords.reset();
24 fImageShaderRecords.unrefAll(); 25 fImageShaderRecords.unrefAll();
25 fImageShaderRecords.reset(); 26 fImageShaderRecords.reset();
26 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); }); 27 fGraphicStateRecords.foreach ([](WrapGS w) { w.fPtr->unref(); });
27 fGraphicStateRecords.reset(); 28 fGraphicStateRecords.reset();
28 fBitmapRecords.unrefAll(); 29
29 fBitmapRecords.reset(); 30 fBitmapToImageMap.foreach(
31 [](SkBitmapKey, const SkImage** p) { SkSafeUnref(*p); });
32 fBitmapToImageMap.reset();
33
34 fPDFBitmapMap.foreach([](uint32_t, SkPDFObject** p) { SkSafeUnref(*p); });
35 fPDFBitmapMap.reset();
30 } 36 }
31 37
32 //////////////////////////////////////////////////////////////////////////////// 38 ////////////////////////////////////////////////////////////////////////////////
33 39
34 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; } 40 template <class T> T* assert_ptr(T* p) { SkASSERT(p); return p; }
35 41
36 // requires `bool T::equals(const U&) const` 42 // requires `bool T::equals(const U&) const`
37 template <typename T, typename U> 43 template <typename T, typename U>
38 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) { 44 T* find_item(const SkTDArray<T*>& ptrArray, const U& object) {
39 for (int i = 0; i < ptrArray.count(); ++i) { 45 for (int i = 0; i < ptrArray.count(); ++i) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 120
115 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) { 121 void SkPDFCanon::addGraphicState(const SkPDFGraphicState* state) {
116 SkASSERT(state); 122 SkASSERT(state);
117 WrapGS w(SkRef(state)); 123 WrapGS w(SkRef(state));
118 SkASSERT(!fGraphicStateRecords.contains(w)); 124 SkASSERT(!fGraphicStateRecords.contains(w));
119 fGraphicStateRecords.add(w); 125 fGraphicStateRecords.add(w);
120 } 126 }
121 127
122 //////////////////////////////////////////////////////////////////////////////// 128 ////////////////////////////////////////////////////////////////////////////////
123 129
124 SkPDFBitmap* SkPDFCanon::findBitmap(const SkBitmap& bm) const { 130 SkPDFObject* SkPDFCanon::findPDFBitmap(const SkImage* image) const {
125 return find_item(fBitmapRecords, bm); 131 SkPDFObject** ptr = fPDFBitmapMap.find(image->uniqueID());
132 return ptr ? *ptr : nullptr;
126 } 133 }
127 134
128 void SkPDFCanon::addBitmap(SkPDFBitmap* pdfBitmap) { 135 void SkPDFCanon::addPDFBitmap(uint32_t imageUniqueID, SkPDFObject* pdfBitmap) {
129 fBitmapRecords.push(SkRef(pdfBitmap)); 136 fPDFBitmapMap.set(imageUniqueID, SkRef(pdfBitmap));
130 } 137 }
138
139 const SkImage* SkPDFCanon::bitmapToImage(const SkBitmap& bm) {
140 // reference remains owned by the fBitmapToImageMap!
141 SkBitmapKey key(bm);
142 if (const SkImage** img = fBitmapToImageMap.find(key)) {
143 return *img;
144 }
145 if (SkImage* image = SkImage::NewFromBitmap(bm)) {
146 return *fBitmapToImageMap.set(key, image);
147 }
148 SkBitmap n32bitmap; // SkImage::NewFromBitmap can be finicky.
149 bm.copyTo(&n32bitmap, kN32_SkColorType);
150 return *fBitmapToImageMap.set(key, SkImage::NewFromBitmap(n32bitmap));
151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698