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

Side by Side Diff: src/core/SkResourceCache.cpp

Issue 1426753006: SkResourceCache::GetAllocator() index8 and other color types handling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: after review comments Created 5 years, 1 month 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 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 "SkChecksum.h" 8 #include "SkChecksum.h"
9 #include "SkMessageBus.h" 9 #include "SkMessageBus.h"
10 #include "SkMipMap.h" 10 #include "SkMipMap.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 fTotalByteLimit = 0; 72 fTotalByteLimit = 0;
73 fDiscardableFactory = nullptr; 73 fDiscardableFactory = nullptr;
74 } 74 }
75 75
76 #include "SkDiscardableMemory.h" 76 #include "SkDiscardableMemory.h"
77 77
78 class SkOneShotDiscardablePixelRef : public SkPixelRef { 78 class SkOneShotDiscardablePixelRef : public SkPixelRef {
79 public: 79 public:
80 80
81 // Ownership of the discardablememory is transfered to the pixelref 81 // Ownership of the discardablememory is transfered to the pixelref
82 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes); 82 SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_ t rowBytes, SkColorTable* ctable);
scroggo 2015/11/09 20:18:09 nit: over 100 chars. Also, the name "ctable" adds
83 ~SkOneShotDiscardablePixelRef(); 83 ~SkOneShotDiscardablePixelRef();
84 84
85 protected: 85 protected:
86 bool onNewLockPixels(LockRec*) override; 86 bool onNewLockPixels(LockRec*) override;
87 void onUnlockPixels() override; 87 void onUnlockPixels() override;
88 size_t getAllocatedSizeInBytes() const override; 88 size_t getAllocatedSizeInBytes() const override;
89 89
90 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur n fDM; } 90 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { retur n fDM; }
91 91
92 private: 92 private:
93 SkDiscardableMemory* fDM; 93 SkDiscardableMemory* fDM;
94 size_t fRB; 94 size_t fRB;
95 bool fFirstTime; 95 bool fFirstTime;
96 SkColorTable* fCTable;
scroggo 2015/11/09 20:18:09 Can you add a comment about ownership of the color
96 97
97 typedef SkPixelRef INHERITED; 98 typedef SkPixelRef INHERITED;
98 }; 99 };
99 100
100 SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& in fo, 101 SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& in fo,
101 SkDiscardableMemory* dm, 102 SkDiscardableMemory* dm,
102 size_t rowBytes) 103 size_t rowBytes,
104 SkColorTable* ctable)
103 : INHERITED(info) 105 : INHERITED(info)
104 , fDM(dm) 106 , fDM(dm)
105 , fRB(rowBytes) 107 , fRB(rowBytes)
108 , fCTable(ctable)
106 { 109 {
107 SkASSERT(dm->data()); 110 SkASSERT(dm->data());
108 fFirstTime = true; 111 fFirstTime = true;
112 SkSafeRef(ctable);
109 } 113 }
110 114
111 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { delete fDM; } 115 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() {
116 delete fDM;
117 SkSafeUnref(fCTable);
118 }
112 119
113 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { 120 bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) {
114 if (fFirstTime) { 121 if (fFirstTime) {
115 // we're already locked 122 // we're already locked
116 SkASSERT(fDM->data()); 123 SkASSERT(fDM->data());
117 fFirstTime = false; 124 fFirstTime = false;
118 goto SUCCESS; 125 goto SUCCESS;
119 } 126 }
120 127
121 // A previous call to onUnlock may have deleted our DM, so check for that 128 // A previous call to onUnlock may have deleted our DM, so check for that
122 if (nullptr == fDM) { 129 if (nullptr == fDM) {
123 return false; 130 return false;
124 } 131 }
125 132
126 if (!fDM->lock()) { 133 if (!fDM->lock()) {
127 // since it failed, we delete it now, to free-up the resource 134 // since it failed, we delete it now, to free-up the resource
128 delete fDM; 135 delete fDM;
129 fDM = nullptr; 136 fDM = nullptr;
130 return false; 137 return false;
131 } 138 }
132 139
133 SUCCESS: 140 SUCCESS:
134 rec->fPixels = fDM->data(); 141 rec->fPixels = fDM->data();
135 rec->fColorTable = nullptr; 142 rec->fColorTable = fCTable;
136 rec->fRowBytes = fRB; 143 rec->fRowBytes = fRB;
137 return true; 144 return true;
138 } 145 }
139 146
140 void SkOneShotDiscardablePixelRef::onUnlockPixels() { 147 void SkOneShotDiscardablePixelRef::onUnlockPixels() {
141 SkASSERT(!fFirstTime); 148 SkASSERT(!fFirstTime);
142 fDM->unlock(); 149 fDM->unlock();
143 } 150 }
144 151
145 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { 152 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
(...skipping 13 matching lines...) Expand all
159 SkResourceCache::DiscardableFactory fFactory; 166 SkResourceCache::DiscardableFactory fFactory;
160 }; 167 };
161 168
162 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo rTable* ctable) { 169 bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColo rTable* ctable) {
163 size_t size = bitmap->getSize(); 170 size_t size = bitmap->getSize();
164 uint64_t size64 = bitmap->computeSize64(); 171 uint64_t size64 = bitmap->computeSize64();
165 if (0 == size || size64 > (uint64_t)size) { 172 if (0 == size || size64 > (uint64_t)size) {
166 return false; 173 return false;
167 } 174 }
168 175
176 if (kIndex_8_SkColorType == bitmap->colorType()) {
177 if (!ctable) {
178 return false;
179 }
180 } else {
181 ctable = nullptr;
182 }
183
169 SkDiscardableMemory* dm = fFactory(size); 184 SkDiscardableMemory* dm = fFactory(size);
170 if (nullptr == dm) { 185 if (nullptr == dm) {
171 return false; 186 return false;
172 } 187 }
173 188
174 // can we relax this?
175 if (kN32_SkColorType != bitmap->colorType()) {
176 return false;
177 }
178
179 SkImageInfo info = bitmap->info(); 189 SkImageInfo info = bitmap->info();
180 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy tes()))->unref(); 190 bitmap->setPixelRef(new SkOneShotDiscardablePixelRef(info, dm, bitmap->rowBy tes(), ctable))->unref();
scroggo 2015/11/09 20:18:09 nit: over 100 chars
181 bitmap->lockPixels(); 191 bitmap->lockPixels();
182 return bitmap->readyToDraw(); 192 return bitmap->readyToDraw();
183 } 193 }
184 194
185 SkResourceCache::SkResourceCache(DiscardableFactory factory) { 195 SkResourceCache::SkResourceCache(DiscardableFactory factory) {
186 this->init(); 196 this->init();
187 fDiscardableFactory = factory; 197 fDiscardableFactory = factory;
188 198
189 fAllocator = new SkResourceCacheDiscardableAllocator(factory); 199 fAllocator = new SkResourceCacheDiscardableAllocator(factory);
190 } 200 }
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed( )); 697 dump->dumpNumericValue(dumpName.c_str(), "size", "bytes", rec.bytesUsed( ));
688 dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr); 698 dump->setMemoryBacking(dumpName.c_str(), "malloc", nullptr);
689 } 699 }
690 } 700 }
691 701
692 void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) { 702 void SkResourceCache::DumpMemoryStatistics(SkTraceMemoryDump* dump) {
693 // Since resource could be backed by malloc or discardable, the cache always dumps detailed 703 // Since resource could be backed by malloc or discardable, the cache always dumps detailed
694 // stats to be accurate. 704 // stats to be accurate.
695 VisitAll(sk_trace_dump_visitor, dump); 705 VisitAll(sk_trace_dump_visitor, dump);
696 } 706 }
OLDNEW
« no previous file with comments | « no previous file | tests/CachedDecodingPixelRefTest.cpp » ('j') | tests/CachedDecodingPixelRefTest.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698