Chromium Code Reviews| 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 "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 Loading... | |
| 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 // value choosen so that there is no loss when converting to to RGB565 and b ack |
| 167 TestImageGenerator(TestType type, skiatest::Reporter* reporter) | 167 static SkColor Color() { return 0xff10345a; } |
| 168 : INHERITED(GetMyInfo()), fType(type), fReporter(reporter) { | 168 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); } |
| 169 | |
| 170 TestImageGenerator(TestType type, skiatest::Reporter* reporter, | |
| 171 SkColorType colorType = kN32_SkColorType) | |
| 172 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) { | |
| 169 SkASSERT((fType <= kLast_TestType) && (fType >= 0)); | 173 SkASSERT((fType <= kLast_TestType) && (fType >= 0)); |
| 170 } | 174 } |
| 171 virtual ~TestImageGenerator() { } | 175 virtual ~TestImageGenerator() { } |
| 172 | 176 |
| 173 protected: | 177 protected: |
| 174 static SkImageInfo GetMyInfo() { | 178 static SkImageInfo GetMyInfo(SkColorType colorType) { |
| 175 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenera tor::Height(), | 179 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator ::Height(), |
| 176 kOpaque_SkAlphaType); | 180 colorType, kOpaque_SkAlphaType); |
| 177 } | 181 } |
| 178 | 182 |
| 179 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, | 183 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 180 SkPMColor ctable[], int* ctableCount) override { | 184 SkPMColor ctable[], int* ctableCount) override { |
| 181 REPORTER_ASSERT(fReporter, pixels != nullptr); | 185 REPORTER_ASSERT(fReporter, pixels != nullptr); |
| 182 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); | 186 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); |
| 183 if (fType != kSucceedGetPixels_TestType) { | 187 if (fType != kSucceedGetPixels_TestType) { |
| 184 return false; | 188 return false; |
| 185 } | 189 } |
| 186 if (info.colorType() != kN32_SkColorType) { | 190 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo( ).colorType()) { |
| 187 return false; | 191 return false; |
| 188 } | 192 } |
| 189 char* bytePtr = static_cast<char*>(pixels); | 193 char* bytePtr = static_cast<char*>(pixels); |
| 190 for (int y = 0; y < info.height(); ++y) { | 194 switch (info.colorType()) { |
| 191 sk_memset32(reinterpret_cast<SkColor*>(bytePtr), | 195 case kN32_SkColorType: |
|
scroggo
2015/11/09 20:18:09
nit: in Skia, we indent the case statement from sw
| |
| 192 TestImageGenerator::Color(), info.width()); | 196 for (int y = 0; y < info.height(); ++y) { |
| 193 bytePtr += rowBytes; | 197 sk_memset32((uint32_t*)bytePtr, |
| 198 TestImageGenerator::PMColor(), info.width()); | |
| 199 bytePtr += rowBytes; | |
| 200 } | |
| 201 break; | |
| 202 case kIndex_8_SkColorType: | |
| 203 *ctableCount = 1; | |
| 204 ctable[0] = TestImageGenerator::PMColor(); | |
| 205 for (int y = 0; y < info.height(); ++y) { | |
| 206 memset(bytePtr, 0, info.width()); | |
| 207 bytePtr += rowBytes; | |
| 208 } | |
| 209 break; | |
| 210 case kRGB_565_SkColorType: | |
| 211 for (int y = 0; y < info.height(); ++y) { | |
| 212 sk_memset16((uint16_t*)bytePtr, | |
| 213 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.widt h()); | |
| 214 bytePtr += rowBytes; | |
| 215 } | |
| 216 break; | |
| 217 default: | |
| 218 return false; | |
| 194 } | 219 } |
| 195 return true; | 220 return true; |
| 196 } | 221 } |
| 197 | 222 |
| 198 private: | 223 private: |
| 199 const TestType fType; | 224 const TestType fType; |
| 200 skiatest::Reporter* const fReporter; | 225 skiatest::Reporter* const fReporter; |
| 201 | 226 |
| 202 typedef SkImageGenerator INHERITED; | 227 typedef SkImageGenerator INHERITED; |
| 203 }; | 228 }; |
| 204 | 229 |
| 205 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, | 230 static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, |
| 206 const SkBitmap& bm) { | 231 const SkBitmap& bm) { |
| 207 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); | 232 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); |
| 208 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); | 233 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); |
| 209 SkAutoLockPixels autoLockPixels(bm); | 234 SkAutoLockPixels autoLockPixels(bm); |
| 210 REPORTER_ASSERT(reporter, bm.getPixels()); | 235 REPORTER_ASSERT(reporter, bm.getPixels()); |
| 211 if (nullptr == bm.getPixels()) { | 236 if (nullptr == bm.getPixels()) { |
| 212 return; | 237 return; |
| 213 } | 238 } |
| 214 int errors = 0; | 239 int errors = 0; |
| 215 for (int y = 0; y < bm.height(); ++y) { | 240 for (int y = 0; y < bm.height(); ++y) { |
| 216 for (int x = 0; x < bm.width(); ++x) { | 241 for (int x = 0; x < bm.width(); ++x) { |
| 217 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { | 242 if (TestImageGenerator::Color() != bm.getColor(x, y)) { |
| 218 ++errors; | 243 ++errors; |
| 219 } | 244 } |
| 220 } | 245 } |
| 221 } | 246 } |
| 222 REPORTER_ASSERT(reporter, 0 == errors); | 247 REPORTER_ASSERT(reporter, 0 == errors); |
| 223 } | 248 } |
| 224 | 249 |
| 225 static void check_pixelref(TestImageGenerator::TestType type, | 250 static void check_pixelref(TestImageGenerator::TestType type, |
| 226 skiatest::Reporter* reporter, | 251 skiatest::Reporter* reporter, |
| 227 SkDiscardableMemory::Factory* factory) { | 252 SkDiscardableMemory::Factory* factory, |
| 228 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter)); | 253 SkColorType colorType) { |
| 254 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter, c olorType)); | |
| 229 REPORTER_ASSERT(reporter, gen.get() != nullptr); | 255 REPORTER_ASSERT(reporter, gen.get() != nullptr); |
| 230 SkBitmap lazy; | 256 SkBitmap lazy; |
| 231 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr , &lazy, factory); | 257 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr , &lazy, factory); |
| 232 | 258 |
| 233 REPORTER_ASSERT(reporter, success); | 259 REPORTER_ASSERT(reporter, success); |
| 234 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { | 260 if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
| 235 check_test_image_generator_bitmap(reporter, lazy); | 261 check_test_image_generator_bitmap(reporter, lazy); |
| 236 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { | 262 } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
| 237 SkAutoLockPixels autoLockPixels(lazy); | 263 SkAutoLockPixels autoLockPixels(lazy); |
| 238 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); | 264 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels()); |
| 239 } | 265 } |
| 240 } | 266 } |
| 241 | 267 |
| 242 /** | 268 /** |
| 243 * This tests the basic functionality of SkDiscardablePixelRef with a | 269 * This tests the basic functionality of SkDiscardablePixelRef with a |
| 244 * basic SkImageGenerator implementation and several | 270 * basic SkImageGenerator implementation and several |
| 245 * SkDiscardableMemory::Factory choices. | 271 * SkDiscardableMemory::Factory choices. |
| 246 */ | 272 */ |
| 247 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { | 273 DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
| 248 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nullpt r); | 274 const SkColorType testColorTypes[] = { |
| 249 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nul lptr); | 275 kN32_SkColorType, |
| 276 kIndex_8_SkColorType, | |
| 277 kRGB_565_SkColorType | |
| 278 }; | |
| 279 for (size_t i = 0; i < SK_ARRAY_COUNT(testColorTypes); ++i) { | |
|
scroggo
2015/11/09 20:18:09
nit: we can now use range based for loops. This is
| |
| 280 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nu llptr, | |
| 281 testColorTypes[i]); | |
| 282 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nullptr, | |
| 283 testColorTypes[i]); | |
| 250 | 284 |
| 251 SkAutoTUnref<SkDiscardableMemoryPool> pool( | 285 SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 252 SkDiscardableMemoryPool::Create(1, nullptr)); | 286 SkDiscardableMemoryPool::Create(1, nullptr)); |
| 253 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 287 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 254 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, pool); | 288 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, po ol, |
| 255 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 289 testColorTypes[i]); |
| 256 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, poo l); | 290 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 257 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | 291 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, pool, |
| 292 testColorTypes[i]); | |
| 293 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); | |
| 258 | 294 |
| 259 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); | 295 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool() ; |
| 260 // Only acts differently from nullptr on a platform that has a | 296 // Only acts differently from nullptr on a platform that has a |
| 261 // default discardable memory implementation that differs from the | 297 // default discardable memory implementation that differs from the |
| 262 // global DM pool. | 298 // global DM pool. |
| 263 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, global Pool); | 299 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, gl obalPool, |
| 264 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, glo balPool); | 300 testColorTypes[i]); |
| 301 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, globalPool, | |
| 302 testColorTypes[i]); | |
| 303 } | |
| 265 } | 304 } |
| 266 | 305 |
| 267 //////////////////////////////////////////////////////////////////////////////// | 306 //////////////////////////////////////////////////////////////////////////////// |
| 268 | 307 |
| 269 DEF_TEST(Image_NewFromGenerator, r) { | 308 DEF_TEST(Image_NewFromGenerator, r) { |
| 270 TestImageGenerator::TestType testTypes[] = { | 309 const TestImageGenerator::TestType testTypes[] = { |
| 271 TestImageGenerator::kFailGetPixels_TestType, | 310 TestImageGenerator::kFailGetPixels_TestType, |
| 272 TestImageGenerator::kSucceedGetPixels_TestType, | 311 TestImageGenerator::kSucceedGetPixels_TestType, |
| 273 }; | 312 }; |
| 313 const SkColorType testColorTypes[] = { | |
| 314 kN32_SkColorType, | |
| 315 kIndex_8_SkColorType, | |
| 316 kRGB_565_SkColorType | |
| 317 }; | |
| 274 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { | 318 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
| 275 TestImageGenerator::TestType test = testTypes[i]; | 319 TestImageGenerator::TestType test = testTypes[i]; |
| 276 SkImageGenerator* gen = new TestImageGenerator(test, r); | 320 for (size_t j = 0; j < SK_ARRAY_COUNT(testColorTypes); ++j) { |
| 277 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); | 321 SkImageGenerator* gen = new TestImageGenerator(test, r, testColorTyp es[j]); |
| 278 if (nullptr == image.get()) { | 322 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); |
| 279 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" | 323 if (nullptr == image.get()) { |
| 280 SK_SIZE_T_SPECIFIER "]", i); | 324 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" |
| 281 continue; | 325 SK_SIZE_T_SPECIFIER "]", i); |
| 282 } | 326 continue; |
| 283 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); | 327 } |
| 284 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); | 328 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); |
| 285 REPORTER_ASSERT(r, image->isLazyGenerated()); | 329 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); |
| 330 REPORTER_ASSERT(r, image->isLazyGenerated()); | |
| 286 | 331 |
| 287 SkBitmap bitmap; | 332 SkBitmap bitmap; |
| 288 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::H eight()); | 333 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerato r::Height()); |
| 289 SkCanvas canvas(bitmap); | 334 SkCanvas canvas(bitmap); |
| 290 const SkColor kDefaultColor = 0xffabcdef; | 335 const SkColor kDefaultColor = 0xffabcdef; |
| 291 canvas.clear(kDefaultColor); | 336 canvas.clear(kDefaultColor); |
| 292 canvas.drawImage(image, 0, 0, nullptr); | 337 canvas.drawImage(image, 0, 0, nullptr); |
| 293 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { | 338 if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
| 294 REPORTER_ASSERT( | 339 REPORTER_ASSERT( |
| 295 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); | 340 r, TestImageGenerator::Color() == bitmap.getColor(0, 0)); |
| 296 } else { | 341 } |
| 297 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); | 342 else { |
| 343 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0)); | |
| 344 } | |
| 298 } | 345 } |
| 299 } | 346 } |
| 300 } | 347 } |
| OLD | NEW |