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

Side by Side Diff: tests/CachedDecodingPixelRefTest.cpp

Issue 1426753006: SkResourceCache::GetAllocator() index8 and other color types handling (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 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 "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkData.h" 10 #include "SkData.h"
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 156
157 class TestImageGenerator : public SkImageGenerator { 157 class TestImageGenerator : public SkImageGenerator {
158 public: 158 public:
159 enum TestType { 159 enum TestType {
160 kFailGetPixels_TestType, 160 kFailGetPixels_TestType,
161 kSucceedGetPixels_TestType, 161 kSucceedGetPixels_TestType,
162 kLast_TestType = kSucceedGetPixels_TestType 162 kLast_TestType = kSucceedGetPixels_TestType
163 }; 163 };
164 static int Width() { return 10; } 164 static int Width() { return 10; }
165 static int Height() { return 10; } 165 static int Height() { return 10; }
166 static uint32_t Color() { return 0xff123456; } 166 static uint32_t Color() { return 0xff10345a; } // value choosen so that ther e is no loss when converting to to RGB565 and back
reed1 2015/11/09 14:14:19 I know the old code used uint32_t for this type, b
reed1 2015/11/09 14:14:19 nits: skia limits itself to 100 columns
aleksandar.stojiljkovic 2015/11/09 15:28:00 Acknowledged.
aleksandar.stojiljkovic 2015/11/09 15:28:00 Acknowledged.
167 TestImageGenerator(TestType type, skiatest::Reporter* reporter) 167 TestImageGenerator(TestType type, skiatest::Reporter* reporter, SkColorType colorType = kN32_SkColorType)
168 : INHERITED(GetMyInfo()), fType(type), fReporter(reporter) { 168 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
169 SkASSERT((fType <= kLast_TestType) && (fType >= 0)); 169 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
170 } 170 }
171 virtual ~TestImageGenerator() { } 171 virtual ~TestImageGenerator() { }
172 172
173 protected: 173 protected:
174 static SkImageInfo GetMyInfo() { 174 static SkImageInfo GetMyInfo(SkColorType colorType) {
175 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenera tor::Height(), 175 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator ::Height(), colorType,
reed1 2015/11/09 14:14:19 100 cols
aleksandar.stojiljkovic 2015/11/09 15:28:00 Acknowledged.
176 kOpaque_SkAlphaType); 176 kOpaque_SkAlphaType);
177 } 177 }
178 178
179 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, 179 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
180 SkPMColor ctable[], int* ctableCount) override { 180 SkPMColor ctable[], int* ctableCount) override {
181 REPORTER_ASSERT(fReporter, pixels != nullptr); 181 REPORTER_ASSERT(fReporter, pixels != nullptr);
182 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); 182 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
183 if (fType != kSucceedGetPixels_TestType) { 183 if (fType != kSucceedGetPixels_TestType) {
184 return false; 184 return false;
185 } 185 }
186 if (info.colorType() != kN32_SkColorType) { 186 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo( ).colorType()) {
187 return false; 187 return false;
188 } 188 }
189 char* bytePtr = static_cast<char*>(pixels); 189 char* bytePtr = static_cast<char*>(pixels);
190 for (int y = 0; y < info.height(); ++y) { 190 switch (info.colorType()) {
191 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), 191 case kN32_SkColorType:
192 TestImageGenerator::Color(), info.width()); 192 for (int y = 0; y < info.height(); ++y) {
193 bytePtr += rowBytes; 193 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
194 TestImageGenerator::Color(), info.width());
195 bytePtr += rowBytes;
196 }
197 break;
198 case kIndex_8_SkColorType:
199 *ctableCount = 1;
200 ctable[0] = TestImageGenerator::Color();
201 for (int y = 0; y < info.height(); ++y) {
202 memset(bytePtr, 0, info.width());
203 bytePtr += rowBytes;
204 }
205 break;
206 case kRGB_565_SkColorType:
207 for (int y = 0; y < info.height(); ++y) {
208 sk_memset16((uint16_t*)bytePtr,
209 SkDitherPixel32ToPixel16(TestImageGenerator::Color()), info. width());
reed1 2015/11/09 14:14:19 why the dither conversion? Since you're repeating
aleksandar.stojiljkovic 2015/11/09 15:28:00 I just didn't know better: SkDitherPixel32ToPixel1
reed1 2015/11/09 15:32:47 That dither call is the compliment to SkPixel32ToP
210 bytePtr += rowBytes;
211 }
212 break;
213 default:
214 return false;
194 } 215 }
195 return true; 216 return true;
196 } 217 }
197 218
198 private: 219 private:
199 const TestType fType; 220 const TestType fType;
200 skiatest::Reporter* const fReporter; 221 skiatest::Reporter* const fReporter;
201 222
202 typedef SkImageGenerator INHERITED; 223 typedef SkImageGenerator INHERITED;
203 }; 224 };
204 225
205 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, 226 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
206 const SkBitmap& bm) { 227 const SkBitmap& bm) {
207 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); 228 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
208 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); 229 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
209 SkAutoLockPixels autoLockPixels(bm); 230 SkAutoLockPixels autoLockPixels(bm);
210 REPORTER_ASSERT(reporter, bm.getPixels()); 231 REPORTER_ASSERT(reporter, bm.getPixels());
211 if (nullptr == bm.getPixels()) { 232 if (nullptr == bm.getPixels()) {
212 return; 233 return;
213 } 234 }
214 int errors = 0; 235 int errors = 0;
215 for (int y = 0; y < bm.height(); ++y) { 236 for (int y = 0; y < bm.height(); ++y) {
216 for (int x = 0; x < bm.width(); ++x) { 237 for (int x = 0; x < bm.width(); ++x) {
217 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { 238 if (TestImageGenerator::Color() != bm.getColor(x, y)) {
218 ++errors; 239 ++errors;
219 } 240 }
220 } 241 }
221 } 242 }
222 REPORTER_ASSERT(reporter, 0 == errors); 243 REPORTER_ASSERT(reporter, 0 == errors);
223 } 244 }
224 245
225 static void check_pixelref(TestImageGenerator::TestType type, 246 static void check_pixelref(TestImageGenerator::TestType type,
226 skiatest::Reporter* reporter, 247 skiatest::Reporter* reporter,
227 SkDiscardableMemory::Factory* factory) { 248 SkDiscardableMemory::Factory* factory,
228 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); 249 SkColorType colorType) {
250 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter, c olorType));
229 REPORTER_ASSERT(reporter, gen.get() != nullptr); 251 REPORTER_ASSERT(reporter, gen.get() != nullptr);
230 SkBitmap lazy; 252 SkBitmap lazy;
231 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr , &lazy, factory); 253 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr , &lazy, factory);
232 254
233 REPORTER_ASSERT(reporter, success); 255 REPORTER_ASSERT(reporter, success);
234 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { 256 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
235 check_test_image_generator_bitmap(reporter, lazy); 257 check_test_image_generator_bitmap(reporter, lazy);
236 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { 258 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
237 SkAutoLockPixels autoLockPixels(lazy); 259 SkAutoLockPixels autoLockPixels(lazy);
238 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); 260 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
239 } 261 }
240 } 262 }
241 263
242 /** 264 /**
243 * This tests the basic functionality of SkDiscardablePixelRef with a 265 * This tests the basic functionality of SkDiscardablePixelRef with a
244 * basic SkImageGenerator implementation and several 266 * basic SkImageGenerator implementation and several
245 * SkDiscardableMemory::Factory choices. 267 * SkDiscardableMemory::Factory choices.
246 */ 268 */
247 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { 269 DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
248 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nullpt r); 270 SkColorType testColorTypes[] = {
reed1 2015/11/09 14:14:19 nit: const
249 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nul lptr); 271 kN32_SkColorType,
272 kIndex_8_SkColorType,
273 kRGB_565_SkColorType
274 };
275 for (size_t i = 0; i < SK_ARRAY_COUNT(testColorTypes); ++i) {
276 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nu llptr, testColorTypes[i]);
reed1 2015/11/09 14:14:19 100 cols
277 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nullptr, testColorTypes[i]);
250 278
251 SkAutoTUnref<SkDiscardableMemoryPool> pool( 279 SkAutoTUnref<SkDiscardableMemoryPool> pool(
252 SkDiscardableMemoryPool::Create(1, nullptr)); 280 SkDiscardableMemoryPool::Create(1, nullptr));
253 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); 281 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
254 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, pool); 282 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, po ol, testColorTypes[i]);
255 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); 283 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
256 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, poo l); 284 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, pool, testColorTypes[i]);
257 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); 285 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
258 286
259 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); 287 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool() ;
260 // Only acts differently from nullptr on a platform that has a 288 // Only acts differently from nullptr on a platform that has a
261 // default discardable memory implementation that differs from the 289 // default discardable memory implementation that differs from the
262 // global DM pool. 290 // global DM pool.
263 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, global Pool); 291 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, gl obalPool, testColorTypes[i]);
264 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, glo balPool); 292 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, globalPool, testColorTypes[i]);
293 }
265 } 294 }
266 295
267 //////////////////////////////////////////////////////////////////////////////// 296 ////////////////////////////////////////////////////////////////////////////////
268 297
269 DEF_TEST(Image_NewFromGenerator, r) { 298 DEF_TEST(Image_NewFromGenerator, r) {
270 TestImageGenerator::TestType testTypes[] = { 299 TestImageGenerator::TestType testTypes[] = {
271 TestImageGenerator::kFailGetPixels_TestType, 300 TestImageGenerator::kFailGetPixels_TestType,
272 TestImageGenerator::kSucceedGetPixels_TestType, 301 TestImageGenerator::kSucceedGetPixels_TestType,
273 }; 302 };
303 SkColorType testColorTypes[] = {
reed1 2015/11/09 14:14:19 const
304 kN32_SkColorType,
305 kIndex_8_SkColorType,
306 kRGB_565_SkColorType
307 };
274 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { 308 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
275 TestImageGenerator::TestType test = testTypes[i]; 309 TestImageGenerator::TestType test = testTypes[i];
276 SkImageGenerator* gen = new TestImageGenerator(test, r); 310 for (size_t j = 0; j < SK_ARRAY_COUNT(testColorTypes); ++j) {
277 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); 311 SkImageGenerator* gen = new TestImageGenerator(test, r, testColorTyp es[j]);
278 if (nullptr == image.get()) { 312 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen));
279 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" 313 if (nullptr == image.get()) {
280 SK_SIZE_T_SPECIFIER "]", i); 314 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
281 continue; 315 SK_SIZE_T_SPECIFIER "]", i);
282 } 316 continue;
283 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); 317 }
284 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); 318 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
285 REPORTER_ASSERT(r, image->isLazyGenerated()); 319 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
320 REPORTER_ASSERT(r, image->isLazyGenerated());
286 321
287 SkBitmap bitmap; 322 SkBitmap bitmap;
288 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H eight()); 323 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerato r::Height());
289 SkCanvas canvas(bitmap); 324 SkCanvas canvas(bitmap);
290 const SkColor kDefaultColor = 0xffabcdef; 325 const SkColor kDefaultColor = 0xffabcdef;
291 canvas.clear(kDefaultColor); 326 canvas.clear(kDefaultColor);
292 canvas.drawImage(image, 0, 0, nullptr); 327 canvas.drawImage(image, 0, 0, nullptr);
293 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { 328 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
294 REPORTER_ASSERT( 329 REPORTER_ASSERT(
295 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); 330 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
296 } else { 331 }
297 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); 332 else {
333 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
334 }
298 } 335 }
299 } 336 }
300 } 337 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698