OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkDiscardablePixelRef.h" | 8 #include "SkDiscardablePixelRef.h" |
9 #include "SkDiscardableMemory.h" | 9 #include "SkDiscardableMemory.h" |
10 #include "SkImageGenerator.h" | 10 #include "SkImageGenerator.h" |
11 | 11 |
12 SkDiscardablePixelRef::SkDiscardablePixelRef(const SkImageInfo& info, | 12 SkDiscardablePixelRef::SkDiscardablePixelRef(const SkImageInfo& info, |
13 SkImageGenerator* generator, | 13 SkImageGenerator* generator, |
14 size_t rowBytes, | 14 size_t rowBytes, |
15 SkDiscardableMemory::Factory* fact) | 15 SkDiscardableMemory::Factory* fact) |
16 : INHERITED(info) | 16 : INHERITED(info) |
17 , fGenerator(generator) | 17 , fGenerator(generator) |
18 , fDMFactory(fact) | 18 , fDMFactory(fact) |
19 , fRowBytes(rowBytes) | 19 , fRowBytes(rowBytes) |
20 , fDiscardableMemory(NULL) | 20 , fDiscardableMemory(nullptr) |
21 , fDiscardableMemoryIsLocked(false) | 21 , fDiscardableMemoryIsLocked(false) |
22 { | 22 { |
23 SkASSERT(fGenerator != NULL); | 23 SkASSERT(fGenerator != nullptr); |
24 SkASSERT(fRowBytes > 0); | 24 SkASSERT(fRowBytes > 0); |
25 // The SkImageGenerator contract requires fGenerator to always | 25 // The SkImageGenerator contract requires fGenerator to always |
26 // decode the same image on each call to getPixels(). | 26 // decode the same image on each call to getPixels(). |
27 this->setImmutable(); | 27 this->setImmutable(); |
28 SkSafeRef(fDMFactory); | 28 SkSafeRef(fDMFactory); |
29 } | 29 } |
30 | 30 |
31 SkDiscardablePixelRef::~SkDiscardablePixelRef() { | 31 SkDiscardablePixelRef::~SkDiscardablePixelRef() { |
32 if (fDiscardableMemoryIsLocked) { | 32 if (fDiscardableMemoryIsLocked) { |
33 fDiscardableMemory->unlock(); | 33 fDiscardableMemory->unlock(); |
34 fDiscardableMemoryIsLocked = false; | 34 fDiscardableMemoryIsLocked = false; |
35 } | 35 } |
36 delete fDiscardableMemory; | 36 delete fDiscardableMemory; |
37 SkSafeUnref(fDMFactory); | 37 SkSafeUnref(fDMFactory); |
38 delete fGenerator; | 38 delete fGenerator; |
39 } | 39 } |
40 | 40 |
41 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { | 41 bool SkDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
42 if (fDiscardableMemory != NULL) { | 42 if (fDiscardableMemory != nullptr) { |
43 if (fDiscardableMemory->lock()) { | 43 if (fDiscardableMemory->lock()) { |
44 fDiscardableMemoryIsLocked = true; | 44 fDiscardableMemoryIsLocked = true; |
45 rec->fPixels = fDiscardableMemory->data(); | 45 rec->fPixels = fDiscardableMemory->data(); |
46 rec->fColorTable = fCTable.get(); | 46 rec->fColorTable = fCTable.get(); |
47 rec->fRowBytes = fRowBytes; | 47 rec->fRowBytes = fRowBytes; |
48 return true; | 48 return true; |
49 } | 49 } |
50 delete fDiscardableMemory; | 50 delete fDiscardableMemory; |
51 fDiscardableMemory = NULL; | 51 fDiscardableMemory = nullptr; |
52 fDiscardableMemoryIsLocked = false; | 52 fDiscardableMemoryIsLocked = false; |
53 } | 53 } |
54 | 54 |
55 const size_t size = this->info().getSafeSize(fRowBytes); | 55 const size_t size = this->info().getSafeSize(fRowBytes); |
56 | 56 |
57 if (fDMFactory != NULL) { | 57 if (fDMFactory != nullptr) { |
58 fDiscardableMemory = fDMFactory->create(size); | 58 fDiscardableMemory = fDMFactory->create(size); |
59 fDiscardableMemoryIsLocked = true; | 59 fDiscardableMemoryIsLocked = true; |
60 } else { | 60 } else { |
61 fDiscardableMemory = SkDiscardableMemory::Create(size); | 61 fDiscardableMemory = SkDiscardableMemory::Create(size); |
62 fDiscardableMemoryIsLocked = true; | 62 fDiscardableMemoryIsLocked = true; |
63 } | 63 } |
64 if (NULL == fDiscardableMemory) { | 64 if (nullptr == fDiscardableMemory) { |
65 fDiscardableMemoryIsLocked = false; | 65 fDiscardableMemoryIsLocked = false; |
66 return false; // Memory allocation failed. | 66 return false; // Memory allocation failed. |
67 } | 67 } |
68 | 68 |
69 void* pixels = fDiscardableMemory->data(); | 69 void* pixels = fDiscardableMemory->data(); |
70 const SkImageInfo& info = this->info(); | 70 const SkImageInfo& info = this->info(); |
71 SkPMColor colors[256]; | 71 SkPMColor colors[256]; |
72 int colorCount = 0; | 72 int colorCount = 0; |
73 | 73 |
74 if (!fGenerator->getPixels(info, pixels, fRowBytes, colors, &colorCount)) { | 74 if (!fGenerator->getPixels(info, pixels, fRowBytes, colors, &colorCount)) { |
75 fDiscardableMemory->unlock(); | 75 fDiscardableMemory->unlock(); |
76 fDiscardableMemoryIsLocked = false; | 76 fDiscardableMemoryIsLocked = false; |
77 delete fDiscardableMemory; | 77 delete fDiscardableMemory; |
78 fDiscardableMemory = NULL; | 78 fDiscardableMemory = nullptr; |
79 return false; | 79 return false; |
80 } | 80 } |
81 | 81 |
82 // Note: our ctable is not purgeable, as it is not stored in the discardable
memory block. | 82 // Note: our ctable is not purgeable, as it is not stored in the discardable
memory block. |
83 // This is because SkColorTable is refcntable, and therefore our caller coul
d hold onto it | 83 // This is because SkColorTable is refcntable, and therefore our caller coul
d hold onto it |
84 // beyond the scope of a lock/unlock. If we change the API/lifecycle for SkC
olorTable, we | 84 // beyond the scope of a lock/unlock. If we change the API/lifecycle for SkC
olorTable, we |
85 // could move it into the block, but then again perhaps it is small enough t
hat this doesn't | 85 // could move it into the block, but then again perhaps it is small enough t
hat this doesn't |
86 // really matter. | 86 // really matter. |
87 if (colorCount > 0) { | 87 if (colorCount > 0) { |
88 fCTable.reset(new SkColorTable(colors, colorCount)); | 88 fCTable.reset(new SkColorTable(colors, colorCount)); |
89 } else { | 89 } else { |
90 fCTable.reset(NULL); | 90 fCTable.reset(nullptr); |
91 } | 91 } |
92 | 92 |
93 rec->fPixels = pixels; | 93 rec->fPixels = pixels; |
94 rec->fColorTable = fCTable.get(); | 94 rec->fColorTable = fCTable.get(); |
95 rec->fRowBytes = fRowBytes; | 95 rec->fRowBytes = fRowBytes; |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 void SkDiscardablePixelRef::onUnlockPixels() { | 99 void SkDiscardablePixelRef::onUnlockPixels() { |
100 fDiscardableMemory->unlock(); | 100 fDiscardableMemory->unlock(); |
101 fDiscardableMemoryIsLocked = false; | 101 fDiscardableMemoryIsLocked = false; |
102 } | 102 } |
103 | 103 |
104 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, const SkIRect* su
bset, SkBitmap* dst, | 104 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, const SkIRect* su
bset, SkBitmap* dst, |
105 SkDiscardableMemory::Factory* factory) { | 105 SkDiscardableMemory::Factory* factory) { |
106 SkAutoTDelete<SkImageGenerator> autoGenerator(generator); | 106 SkAutoTDelete<SkImageGenerator> autoGenerator(generator); |
107 if (NULL == autoGenerator.get()) { | 107 if (nullptr == autoGenerator.get()) { |
108 return false; | 108 return false; |
109 } | 109 } |
110 | 110 |
111 SkImageInfo prInfo = autoGenerator->getInfo(); | 111 SkImageInfo prInfo = autoGenerator->getInfo(); |
112 if (prInfo.isEmpty()) { | 112 if (prInfo.isEmpty()) { |
113 return false; | 113 return false; |
114 } | 114 } |
115 | 115 |
116 SkIPoint origin = SkIPoint::Make(0, 0); | 116 SkIPoint origin = SkIPoint::Make(0, 0); |
117 SkImageInfo bmInfo = prInfo; | 117 SkImageInfo bmInfo = prInfo; |
(...skipping 20 matching lines...) Expand all Loading... |
138 } | 138 } |
139 SkAutoTUnref<SkDiscardablePixelRef> ref( | 139 SkAutoTUnref<SkDiscardablePixelRef> ref( |
140 new SkDiscardablePixelRef(prInfo, autoGenerator.detach(), dst->rowBy
tes(), factory)); | 140 new SkDiscardablePixelRef(prInfo, autoGenerator.detach(), dst->rowBy
tes(), factory)); |
141 dst->setPixelRef(ref, origin.x(), origin.y()); | 141 dst->setPixelRef(ref, origin.x(), origin.y()); |
142 return true; | 142 return true; |
143 } | 143 } |
144 | 144 |
145 // These are the public API | 145 // These are the public API |
146 | 146 |
147 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { | 147 bool SkInstallDiscardablePixelRef(SkImageGenerator* generator, SkBitmap* dst) { |
148 return SkInstallDiscardablePixelRef(generator, NULL, dst, NULL); | 148 return SkInstallDiscardablePixelRef(generator, nullptr, dst, nullptr); |
149 } | 149 } |
150 | 150 |
151 bool SkInstallDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { | 151 bool SkInstallDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
152 SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); | 152 SkImageGenerator* generator = SkImageGenerator::NewFromEncoded(encoded); |
153 return generator ? SkInstallDiscardablePixelRef(generator, NULL, dst, NULL)
: false; | 153 return generator ? SkInstallDiscardablePixelRef(generator, nullptr, dst, nul
lptr) : false; |
154 } | 154 } |
OLD | NEW |