| OLD | NEW |
| 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
| 12 #include "SkImageEncoder.h" | 12 #include "SkImageEncoder.h" |
| 13 #include "SkImageGenerator.h" |
| 13 #include "SkImage_Base.h" | 14 #include "SkImage_Base.h" |
| 14 #include "SkPicture.h" | 15 #include "SkPicture.h" |
| 15 #include "SkPictureRecorder.h" | 16 #include "SkPictureRecorder.h" |
| 16 #include "SkPixelSerializer.h" | 17 #include "SkPixelSerializer.h" |
| 17 #include "SkRRect.h" | 18 #include "SkRRect.h" |
| 18 #include "SkStream.h" | 19 #include "SkStream.h" |
| 19 #include "SkSurface.h" | 20 #include "SkSurface.h" |
| 20 #include "SkUtils.h" | 21 #include "SkUtils.h" |
| 21 #include "Test.h" | 22 #include "Test.h" |
| 22 | 23 |
| 23 #if SK_SUPPORT_GPU | 24 #if SK_SUPPORT_GPU |
| 24 #include "GrContextFactory.h" | 25 #include "GrContext.h" |
| 25 #include "GrTest.h" | |
| 26 #include "gl/GrGLInterface.h" | 26 #include "gl/GrGLInterface.h" |
| 27 #include "gl/GrGLUtil.h" | 27 #include "gl/GrGLUtil.h" |
| 28 #else | |
| 29 class GrContextFactory; | |
| 30 class GrContext; | |
| 31 #endif | 28 #endif |
| 32 | 29 |
| 33 static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect
* subsetA, | 30 static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect
* subsetA, |
| 34 SkImage* b) { | 31 SkImage* b) { |
| 35 const int widthA = subsetA ? subsetA->width() : a->width(); | 32 const int widthA = subsetA ? subsetA->width() : a->width(); |
| 36 const int heightA = subsetA ? subsetA->height() : a->height(); | 33 const int heightA = subsetA ? subsetA->height() : a->height(); |
| 37 | 34 |
| 38 REPORTER_ASSERT(reporter, widthA == b->width()); | 35 REPORTER_ASSERT(reporter, widthA == b->width()); |
| 39 REPORTER_ASSERT(reporter, heightA == b->height()); | 36 REPORTER_ASSERT(reporter, heightA == b->height()); |
| 40 #if 0 | 37 #if 0 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 54 const int srcY = subsetA ? subsetA->y() : 0; | 51 const int srcY = subsetA ? subsetA->y() : 0; |
| 55 | 52 |
| 56 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY)); | 53 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY)); |
| 57 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0)); | 54 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0)); |
| 58 | 55 |
| 59 const size_t widthBytes = widthA * info.bytesPerPixel(); | 56 const size_t widthBytes = widthA * info.bytesPerPixel(); |
| 60 for (int y = 0; y < heightA; ++y) { | 57 for (int y = 0; y < heightA; ++y) { |
| 61 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y)
, widthBytes)); | 58 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y)
, widthBytes)); |
| 62 } | 59 } |
| 63 } | 60 } |
| 64 | 61 static void draw_image_test_pattern(SkCanvas* canvas) { |
| 65 static SkImage* make_image(GrContext* ctx, int w, int h, const SkIRect& ir) { | |
| 66 const SkImageInfo info = SkImageInfo::MakeN32(w, h, kOpaque_SkAlphaType); | |
| 67 SkAutoTUnref<SkSurface> surface(ctx ? | |
| 68 SkSurface::NewRenderTarget(ctx, SkSurface::k
No_Budgeted, info) : | |
| 69 SkSurface::NewRaster(info)); | |
| 70 SkCanvas* canvas = surface->getCanvas(); | |
| 71 canvas->clear(SK_ColorWHITE); | 62 canvas->clear(SK_ColorWHITE); |
| 72 | |
| 73 SkPaint paint; | 63 SkPaint paint; |
| 74 paint.setColor(SK_ColorBLACK); | 64 paint.setColor(SK_ColorBLACK); |
| 75 canvas->drawRect(SkRect::Make(ir), paint); | 65 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); |
| 66 } |
| 67 static SkImage* create_image() { |
| 68 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 69 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 70 draw_image_test_pattern(surface->getCanvas()); |
| 76 return surface->newImageSnapshot(); | 71 return surface->newImageSnapshot(); |
| 77 } | 72 } |
| 73 static SkData* create_image_data(SkImageInfo* info) { |
| 74 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 75 const size_t rowBytes = info->minRowBytes(); |
| 76 SkAutoTUnref<SkData> data(SkData::NewUninitialized(rowBytes * info->height()
)); |
| 77 { |
| 78 SkBitmap bm; |
| 79 bm.installPixels(*info, data->writable_data(), rowBytes); |
| 80 SkCanvas canvas(bm); |
| 81 draw_image_test_pattern(&canvas); |
| 82 } |
| 83 return data.release(); |
| 84 } |
| 85 static SkImage* create_data_image() { |
| 86 SkImageInfo info; |
| 87 SkAutoTUnref<SkData> data(create_image_data(&info)); |
| 88 return SkImage::NewRasterData(info, data, info.minRowBytes()); |
| 89 } |
| 90 // Want to ensure that our Release is called when the owning image is destroyed |
| 91 struct RasterDataHolder { |
| 92 RasterDataHolder() : fReleaseCount(0) {} |
| 93 SkAutoTUnref<SkData> fData; |
| 94 int fReleaseCount; |
| 95 static void Release(const void* pixels, void* context) { |
| 96 RasterDataHolder* self = static_cast<RasterDataHolder*>(context); |
| 97 self->fReleaseCount++; |
| 98 self->fData.reset(); |
| 99 } |
| 100 }; |
| 101 static SkImage* create_rasterproc_image(RasterDataHolder* dataHolder) { |
| 102 SkASSERT(dataHolder); |
| 103 SkImageInfo info; |
| 104 SkAutoTUnref<SkData> data(create_image_data(&info)); |
| 105 dataHolder->fData.reset(SkRef(data.get())); |
| 106 return SkImage::NewFromRaster(info, data->data(), info.minRowBytes(), |
| 107 RasterDataHolder::Release, dataHolder); |
| 108 } |
| 109 static SkImage* create_codec_image() { |
| 110 SkImageInfo info; |
| 111 SkAutoTUnref<SkData> data(create_image_data(&info)); |
| 112 SkBitmap bitmap; |
| 113 bitmap.installPixels(info, data->writable_data(), info.minRowBytes()); |
| 114 SkAutoTUnref<SkData> src( |
| 115 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 100)); |
| 116 return SkImage::NewFromEncoded(src); |
| 117 } |
| 118 #if SK_SUPPORT_GPU |
| 119 static SkImage* create_gpu_image(GrContext* context) { |
| 120 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 121 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, SkSurfac
e::kNo_Budgeted, |
| 122 info)); |
| 123 draw_image_test_pattern(surface->getCanvas()); |
| 124 return surface->newImageSnapshot(); |
| 125 } |
| 126 #endif |
| 78 | 127 |
| 79 static void test_encode(skiatest::Reporter* reporter, GrContext* ctx) { | 128 static void test_encode(skiatest::Reporter* reporter, SkImage* image) { |
| 80 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); | 129 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); |
| 81 SkAutoTUnref<SkImage> orig(make_image(ctx, 20, 20, ir)); | 130 SkAutoTUnref<SkData> origEncoded(image->encode()); |
| 82 SkAutoTUnref<SkData> origEncoded(orig->encode()); | |
| 83 REPORTER_ASSERT(reporter, origEncoded); | 131 REPORTER_ASSERT(reporter, origEncoded); |
| 84 REPORTER_ASSERT(reporter, origEncoded->size() > 0); | 132 REPORTER_ASSERT(reporter, origEncoded->size() > 0); |
| 85 | 133 |
| 86 SkAutoTUnref<SkImage> decoded(SkImage::NewFromEncoded(origEncoded)); | 134 SkAutoTUnref<SkImage> decoded(SkImage::NewFromEncoded(origEncoded)); |
| 87 REPORTER_ASSERT(reporter, decoded); | 135 REPORTER_ASSERT(reporter, decoded); |
| 88 assert_equal(reporter, orig, nullptr, decoded); | 136 assert_equal(reporter, image, nullptr, decoded); |
| 89 | 137 |
| 90 // Now see if we can instantiate an image from a subset of the surface/origE
ncoded | 138 // Now see if we can instantiate an image from a subset of the surface/origE
ncoded |
| 91 | 139 |
| 92 decoded.reset(SkImage::NewFromEncoded(origEncoded, &ir)); | 140 decoded.reset(SkImage::NewFromEncoded(origEncoded, &ir)); |
| 93 REPORTER_ASSERT(reporter, decoded); | 141 REPORTER_ASSERT(reporter, decoded); |
| 94 assert_equal(reporter, orig, &ir, decoded); | 142 assert_equal(reporter, image, &ir, decoded); |
| 95 } | 143 } |
| 96 | 144 |
| 97 DEF_TEST(Image_Encode_Cpu, reporter) { | 145 DEF_TEST(ImageEncode, reporter) { |
| 98 test_encode(reporter, nullptr); | 146 SkAutoTUnref<SkImage> image(create_image()); |
| 147 test_encode(reporter, image); |
| 99 } | 148 } |
| 100 | 149 |
| 101 #if SK_SUPPORT_GPU | 150 #if SK_SUPPORT_GPU |
| 102 DEF_GPUTEST(Image_Encode_Gpu, reporter, factory) { | 151 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, context) { |
| 103 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType); | 152 SkAutoTUnref<SkImage> image(create_gpu_image(context)); |
| 104 if (!ctx) { | 153 test_encode(reporter, image); |
| 105 REPORTER_ASSERT(reporter, false); | |
| 106 return; | |
| 107 } | |
| 108 test_encode(reporter, ctx); | |
| 109 } | 154 } |
| 110 #endif | 155 #endif |
| 111 | 156 |
| 112 namespace { | 157 namespace { |
| 113 | 158 |
| 114 const char* kSerializedData = "serialized"; | 159 const char* kSerializedData = "serialized"; |
| 115 | 160 |
| 116 class MockSerializer : public SkPixelSerializer { | 161 class MockSerializer : public SkPixelSerializer { |
| 117 public: | 162 public: |
| 118 MockSerializer(SkData* (*func)()) : fFunc(func), fDidEncode(false) { } | 163 MockSerializer(SkData* (*func)()) : fFunc(func), fDidEncode(false) { } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 134 bool fDidEncode; | 179 bool fDidEncode; |
| 135 | 180 |
| 136 typedef SkPixelSerializer INHERITED; | 181 typedef SkPixelSerializer INHERITED; |
| 137 }; | 182 }; |
| 138 | 183 |
| 139 } // anonymous namespace | 184 } // anonymous namespace |
| 140 | 185 |
| 141 // Test that SkImage encoding observes custom pixel serializers. | 186 // Test that SkImage encoding observes custom pixel serializers. |
| 142 DEF_TEST(Image_Encode_Serializer, reporter) { | 187 DEF_TEST(Image_Encode_Serializer, reporter) { |
| 143 MockSerializer serializer([]() -> SkData* { return SkData::NewWithCString(kS
erializedData); }); | 188 MockSerializer serializer([]() -> SkData* { return SkData::NewWithCString(kS
erializedData); }); |
| 144 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); | 189 SkAutoTUnref<SkImage> image(create_image()); |
| 145 SkAutoTUnref<SkImage> image(make_image(nullptr, 20, 20, ir)); | |
| 146 SkAutoTUnref<SkData> encoded(image->encode(&serializer)); | 190 SkAutoTUnref<SkData> encoded(image->encode(&serializer)); |
| 147 SkAutoTUnref<SkData> reference(SkData::NewWithCString(kSerializedData)); | 191 SkAutoTUnref<SkData> reference(SkData::NewWithCString(kSerializedData)); |
| 148 | 192 |
| 149 REPORTER_ASSERT(reporter, serializer.didEncode()); | 193 REPORTER_ASSERT(reporter, serializer.didEncode()); |
| 150 REPORTER_ASSERT(reporter, encoded); | 194 REPORTER_ASSERT(reporter, encoded); |
| 151 REPORTER_ASSERT(reporter, encoded->size() > 0); | 195 REPORTER_ASSERT(reporter, encoded->size() > 0); |
| 152 REPORTER_ASSERT(reporter, encoded->equals(reference)); | 196 REPORTER_ASSERT(reporter, encoded->equals(reference)); |
| 153 } | 197 } |
| 154 | 198 |
| 155 // Test that image encoding failures do not break picture serialization/deserial
ization. | 199 // Test that image encoding failures do not break picture serialization/deserial
ization. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess); | 325 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess); |
| 282 | 326 |
| 283 const bool lazy = image->isLazyGenerated(); | 327 const bool lazy = image->isLazyGenerated(); |
| 284 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy); | 328 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy); |
| 285 } | 329 } |
| 286 } | 330 } |
| 287 | 331 |
| 288 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 332 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 289 #if SK_SUPPORT_GPU | 333 #if SK_SUPPORT_GPU |
| 290 | 334 |
| 291 static SkImage* make_gpu_image(GrContext* ctx, const SkImageInfo& info, SkColor
color) { | |
| 292 const SkSurface::Budgeted budgeted = SkSurface::kNo_Budgeted; | |
| 293 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, budgeted, in
fo, 0)); | |
| 294 surface->getCanvas()->drawColor(color); | |
| 295 return surface->newImageSnapshot(); | |
| 296 } | |
| 297 | |
| 298 #include "SkBitmapCache.h" | 335 #include "SkBitmapCache.h" |
| 299 | 336 |
| 300 /* | 337 /* |
| 301 * This tests the caching (and preemptive purge) of the raster equivalent of a
gpu-image. | 338 * This tests the caching (and preemptive purge) of the raster equivalent of a
gpu-image. |
| 302 * We cache it for performance when drawing into a raster surface. | 339 * We cache it for performance when drawing into a raster surface. |
| 303 * | 340 * |
| 304 * A cleaner test would know if each drawImage call triggered a read-back from
the gpu, | 341 * A cleaner test would know if each drawImage call triggered a read-back from
the gpu, |
| 305 * but we don't have that facility (at the moment) so we use a little internal
knowledge | 342 * but we don't have that facility (at the moment) so we use a little internal
knowledge |
| 306 * of *how* the raster version is cached, and look for that. | 343 * of *how* the raster version is cached, and look for that. |
| 307 */ | 344 */ |
| 308 DEF_GPUTEST(SkImage_Gpu2Cpu, reporter, factory) { | 345 DEF_GPUTEST_FOR_NATIVE_CONTEXT(SkImage_Gpu2Cpu, reporter, context) { |
| 309 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType); | 346 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 310 if (!ctx) { | 347 SkAutoTUnref<SkImage> image(create_gpu_image(context)); |
| 311 REPORTER_ASSERT(reporter, false); | |
| 312 return; | |
| 313 } | |
| 314 | |
| 315 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | |
| 316 SkAutoTUnref<SkImage> image(make_gpu_image(ctx, info, SK_ColorRED)); | |
| 317 const uint32_t uniqueID = image->uniqueID(); | 348 const uint32_t uniqueID = image->uniqueID(); |
| 318 | 349 |
| 319 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | 350 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); |
| 320 | 351 |
| 321 // now we can test drawing a gpu-backed image into a cpu-backed surface | 352 // now we can test drawing a gpu-backed image into a cpu-backed surface |
| 322 | 353 |
| 323 { | 354 { |
| 324 SkBitmap cachedBitmap; | 355 SkBitmap cachedBitmap; |
| 325 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(uniqueID, &cachedBitmap))
; | 356 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(uniqueID, &cachedBitmap))
; |
| 326 } | 357 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 355 new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors))); | 386 new SkColorTable(pmColors, SK_ARRAY_COUNT(pmColors))); |
| 356 SkImageInfo info = | 387 SkImageInfo info = |
| 357 SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType); | 388 SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType); |
| 358 bm.allocPixels(info, nullptr, ctable); | 389 bm.allocPixels(info, nullptr, ctable); |
| 359 SkAutoLockPixels autoLockPixels(bm); | 390 SkAutoLockPixels autoLockPixels(bm); |
| 360 *bm.getAddr8(0, 0) = 0; | 391 *bm.getAddr8(0, 0) = 0; |
| 361 SkAutoTUnref<SkImage> img(SkImage::NewFromBitmap(bm)); | 392 SkAutoTUnref<SkImage> img(SkImage::NewFromBitmap(bm)); |
| 362 REPORTER_ASSERT(r, img.get() != nullptr); | 393 REPORTER_ASSERT(r, img.get() != nullptr); |
| 363 } | 394 } |
| 364 | 395 |
| 365 // TODO: The tests below were moved from SurfaceTests and should be reformatted. | |
| 366 | |
| 367 enum ImageType { | |
| 368 kRasterCopy_ImageType, | |
| 369 kRasterData_ImageType, | |
| 370 kRasterProc_ImageType, | |
| 371 kGpu_ImageType, | |
| 372 kCodec_ImageType, | |
| 373 }; | |
| 374 | |
| 375 #include "SkImageGenerator.h" | |
| 376 | |
| 377 class EmptyGenerator : public SkImageGenerator { | 396 class EmptyGenerator : public SkImageGenerator { |
| 378 public: | 397 public: |
| 379 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} | 398 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 380 }; | 399 }; |
| 381 | 400 |
| 382 static void test_empty_image(skiatest::Reporter* reporter) { | 401 DEF_TEST(ImageEmpty, reporter) { |
| 383 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S
kAlphaType); | 402 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_S
kAlphaType); |
| 384 | |
| 385 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterCopy(info, nullptr, 0
)); | 403 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterCopy(info, nullptr, 0
)); |
| 386 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterData(info, nullptr, 0
)); | 404 REPORTER_ASSERT(reporter, nullptr == SkImage::NewRasterData(info, nullptr, 0
)); |
| 387 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromRaster(info, nullptr, 0
, nullptr, nullptr)); | 405 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromRaster(info, nullptr, 0
, nullptr, nullptr)); |
| 388 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromGenerator(new EmptyGene
rator)); | 406 REPORTER_ASSERT(reporter, nullptr == SkImage::NewFromGenerator(new EmptyGene
rator)); |
| 389 } | 407 } |
| 390 | 408 |
| 391 static void test_image(skiatest::Reporter* reporter) { | 409 DEF_TEST(ImageDataRef, reporter) { |
| 392 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | 410 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 393 size_t rowBytes = info.minRowBytes(); | 411 size_t rowBytes = info.minRowBytes(); |
| 394 size_t size = info.getSafeSize(rowBytes); | 412 size_t size = info.getSafeSize(rowBytes); |
| 395 SkData* data = SkData::NewUninitialized(size); | 413 SkData* data = SkData::NewUninitialized(size); |
| 396 | |
| 397 REPORTER_ASSERT(reporter, data->unique()); | 414 REPORTER_ASSERT(reporter, data->unique()); |
| 398 SkImage* image = SkImage::NewRasterData(info, data, rowBytes); | 415 SkImage* image = SkImage::NewRasterData(info, data, rowBytes); |
| 399 REPORTER_ASSERT(reporter, !data->unique()); | 416 REPORTER_ASSERT(reporter, !data->unique()); |
| 400 image->unref(); | 417 image->unref(); |
| 401 REPORTER_ASSERT(reporter, data->unique()); | 418 REPORTER_ASSERT(reporter, data->unique()); |
| 402 data->unref(); | 419 data->unref(); |
| 403 } | 420 } |
| 404 | 421 |
| 405 // Want to ensure that our Release is called when the owning image is destroyed | |
| 406 struct ReleaseDataContext { | |
| 407 skiatest::Reporter* fReporter; | |
| 408 SkData* fData; | |
| 409 | |
| 410 static void Release(const void* pixels, void* context) { | |
| 411 ReleaseDataContext* state = (ReleaseDataContext*)context; | |
| 412 REPORTER_ASSERT(state->fReporter, state->fData); | |
| 413 state->fData->unref(); | |
| 414 state->fData = nullptr; | |
| 415 } | |
| 416 }; | |
| 417 | |
| 418 static SkImage* create_image(skiatest::Reporter* reporter, | |
| 419 ImageType imageType, GrContext* context, SkColor co
lor, | |
| 420 ReleaseDataContext* releaseContext) { | |
| 421 const SkPMColor pmcolor = SkPreMultiplyColor(color); | |
| 422 const SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10); | |
| 423 const size_t rowBytes = info.minRowBytes(); | |
| 424 const size_t size = rowBytes * info.height(); | |
| 425 | |
| 426 SkAutoTUnref<SkData> data(SkData::NewUninitialized(size)); | |
| 427 void* addr = data->writable_data(); | |
| 428 sk_memset32((SkPMColor*)addr, pmcolor, SkToInt(size >> 2)); | |
| 429 | |
| 430 switch (imageType) { | |
| 431 case kRasterCopy_ImageType: | |
| 432 return SkImage::NewRasterCopy(info, addr, rowBytes); | |
| 433 case kRasterData_ImageType: | |
| 434 return SkImage::NewRasterData(info, data, rowBytes); | |
| 435 case kRasterProc_ImageType: | |
| 436 SkASSERT(releaseContext); | |
| 437 releaseContext->fData = SkRef(data.get()); | |
| 438 return SkImage::NewFromRaster(info, addr, rowBytes, | |
| 439 ReleaseDataContext::Release, releaseCo
ntext); | |
| 440 case kGpu_ImageType: { | |
| 441 SkAutoTUnref<SkSurface> surf( | |
| 442 SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, inf
o, 0)); | |
| 443 surf->getCanvas()->clear(color); | |
| 444 return surf->newImageSnapshot(); | |
| 445 } | |
| 446 case kCodec_ImageType: { | |
| 447 SkBitmap bitmap; | |
| 448 bitmap.installPixels(info, addr, rowBytes); | |
| 449 SkAutoTUnref<SkData> src( | |
| 450 SkImageEncoder::EncodeData(bitmap, SkImageEncoder::kPNG_Type, 1
00)); | |
| 451 return SkImage::NewFromEncoded(src); | |
| 452 } | |
| 453 } | |
| 454 SkASSERT(false); | |
| 455 return nullptr; | |
| 456 } | |
| 457 | |
| 458 static void set_pixels(SkPMColor pixels[], int count, SkPMColor color) { | |
| 459 sk_memset32(pixels, color, count); | |
| 460 } | |
| 461 static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected)
{ | 422 static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected)
{ |
| 462 for (int i = 0; i < count; ++i) { | 423 for (int i = 0; i < count; ++i) { |
| 463 if (pixels[i] != expected) { | 424 if (pixels[i] != expected) { |
| 464 return false; | 425 return false; |
| 465 } | 426 } |
| 466 } | 427 } |
| 467 return true; | 428 return true; |
| 468 } | 429 } |
| 469 | 430 |
| 470 static void test_image_readpixels(skiatest::Reporter* reporter, SkImage* image, | 431 static void test_read_pixels(skiatest::Reporter* reporter, SkImage* image) { |
| 471 SkPMColor expected) { | 432 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE); |
| 472 const SkPMColor notExpected = ~expected; | 433 const SkPMColor notExpected = ~expected; |
| 473 | 434 |
| 474 const int w = 2, h = 2; | 435 const int w = 2, h = 2; |
| 475 const size_t rowBytes = w * sizeof(SkPMColor); | 436 const size_t rowBytes = w * sizeof(SkPMColor); |
| 476 SkPMColor pixels[w*h]; | 437 SkPMColor pixels[w*h]; |
| 477 | 438 |
| 478 SkImageInfo info; | 439 SkImageInfo info; |
| 479 | 440 |
| 480 info = SkImageInfo::MakeUnknown(w, h); | 441 info = SkImageInfo::MakeUnknown(w, h); |
| 481 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); | 442 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 482 | 443 |
| 483 // out-of-bounds should fail | 444 // out-of-bounds should fail |
| 484 info = SkImageInfo::MakeN32Premul(w, h); | 445 info = SkImageInfo::MakeN32Premul(w, h); |
| 485 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0))
; | 446 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0))
; |
| 486 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h))
; | 447 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h))
; |
| 487 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->
width(), 0)); | 448 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->
width(), 0)); |
| 488 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, imag
e->height())); | 449 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, imag
e->height())); |
| 489 | 450 |
| 490 // top-left should succeed | 451 // top-left should succeed |
| 491 set_pixels(pixels, w*h, notExpected); | 452 sk_memset32(pixels, notExpected, w*h); |
| 492 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); | 453 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 493 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); | 454 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 494 | 455 |
| 495 // bottom-right should succeed | 456 // bottom-right should succeed |
| 496 set_pixels(pixels, w*h, notExpected); | 457 sk_memset32(pixels, notExpected, w*h); |
| 497 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, | 458 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 498 image->width() - w, image->heigh
t() - h)); | 459 image->width() - w, image->heigh
t() - h)); |
| 499 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); | 460 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 500 | 461 |
| 501 // partial top-left should succeed | 462 // partial top-left should succeed |
| 502 set_pixels(pixels, w*h, notExpected); | 463 sk_memset32(pixels, notExpected, w*h); |
| 503 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1))
; | 464 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1))
; |
| 504 REPORTER_ASSERT(reporter, pixels[3] == expected); | 465 REPORTER_ASSERT(reporter, pixels[3] == expected); |
| 505 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); | 466 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); |
| 506 | 467 |
| 507 // partial bottom-right should succeed | 468 // partial bottom-right should succeed |
| 508 set_pixels(pixels, w*h, notExpected); | 469 sk_memset32(pixels, notExpected, w*h); |
| 509 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, | 470 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 510 image->width() - 1, image->heigh
t() - 1)); | 471 image->width() - 1, image->heigh
t() - 1)); |
| 511 REPORTER_ASSERT(reporter, pixels[0] == expected); | 472 REPORTER_ASSERT(reporter, pixels[0] == expected); |
| 512 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); | 473 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); |
| 513 } | 474 } |
| 475 DEF_TEST(ImageReadPixels, reporter) { |
| 476 SkAutoTUnref<SkImage> image(create_image()); |
| 477 test_read_pixels(reporter, image); |
| 478 |
| 479 image.reset(create_data_image()); |
| 480 test_read_pixels(reporter, image); |
| 481 |
| 482 RasterDataHolder dataHolder; |
| 483 image.reset(create_rasterproc_image(&dataHolder)); |
| 484 test_read_pixels(reporter, image); |
| 485 image.reset(); |
| 486 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 487 |
| 488 image.reset(create_codec_image()); |
| 489 test_read_pixels(reporter, image); |
| 490 } |
| 491 #if SK_SUPPORT_GPU |
| 492 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, context) { |
| 493 SkAutoTUnref<SkImage> image(create_gpu_image(context)); |
| 494 test_read_pixels(reporter, image); |
| 495 } |
| 496 #endif |
| 514 | 497 |
| 515 static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* ima
ge, | 498 static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* ima
ge, |
| 516 const SkBitmap& bitmap, SkImage::LegacyBitmapMod
e mode) { | 499 const SkBitmap& bitmap, SkImage::LegacyBitmapMod
e mode) { |
| 517 REPORTER_ASSERT(reporter, image->width() == bitmap.width()); | 500 REPORTER_ASSERT(reporter, image->width() == bitmap.width()); |
| 518 REPORTER_ASSERT(reporter, image->height() == bitmap.height()); | 501 REPORTER_ASSERT(reporter, image->height() == bitmap.height()); |
| 519 REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque()); | 502 REPORTER_ASSERT(reporter, image->isOpaque() == bitmap.isOpaque()); |
| 520 | 503 |
| 521 if (SkImage::kRO_LegacyBitmapMode == mode) { | 504 if (SkImage::kRO_LegacyBitmapMode == mode) { |
| 522 REPORTER_ASSERT(reporter, bitmap.isImmutable()); | 505 REPORTER_ASSERT(reporter, bitmap.isImmutable()); |
| 523 } | 506 } |
| 524 | 507 |
| 525 SkAutoLockPixels alp(bitmap); | 508 SkAutoLockPixels alp(bitmap); |
| 526 REPORTER_ASSERT(reporter, bitmap.getPixels()); | 509 REPORTER_ASSERT(reporter, bitmap.getPixels()); |
| 527 | 510 |
| 528 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType()); | 511 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType()); |
| 529 SkPMColor imageColor; | 512 SkPMColor imageColor; |
| 530 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMCo
lor), 0, 0)); | 513 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMCo
lor), 0, 0)); |
| 531 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0)); | 514 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0)); |
| 532 } | 515 } |
| 533 | 516 |
| 534 static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* imag
e) { | 517 static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* imag
e, SkImage::LegacyBitmapMode mode) { |
| 518 SkBitmap bitmap; |
| 519 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, mode)); |
| 520 check_legacy_bitmap(reporter, image, bitmap, mode); |
| 521 |
| 522 // Test subsetting to exercise the rowBytes logic. |
| 523 SkBitmap tmp; |
| 524 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->
width() / 2, |
| 525 image->
height() / 2))); |
| 526 SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp)); |
| 527 REPORTER_ASSERT(reporter, subsetImage); |
| 528 |
| 529 SkBitmap subsetBitmap; |
| 530 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, mode)); |
| 531 check_legacy_bitmap(reporter, subsetImage, subsetBitmap, mode); |
| 532 } |
| 533 DEF_TEST(ImageLegacyBitmap, reporter) { |
| 535 const SkImage::LegacyBitmapMode modes[] = { | 534 const SkImage::LegacyBitmapMode modes[] = { |
| 536 SkImage::kRO_LegacyBitmapMode, | 535 SkImage::kRO_LegacyBitmapMode, |
| 537 SkImage::kRW_LegacyBitmapMode, | 536 SkImage::kRW_LegacyBitmapMode, |
| 538 }; | 537 }; |
| 539 for (size_t i = 0; i < SK_ARRAY_COUNT(modes); ++i) { | 538 for (auto& mode : modes) { |
| 540 SkBitmap bitmap; | 539 SkAutoTUnref<SkImage> image(create_image()); |
| 541 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, modes[i])); | 540 test_legacy_bitmap(reporter, image, mode); |
| 542 check_legacy_bitmap(reporter, image, bitmap, modes[i]); | |
| 543 | 541 |
| 544 // Test subsetting to exercise the rowBytes logic. | 542 image.reset(create_data_image()); |
| 545 SkBitmap tmp; | 543 test_legacy_bitmap(reporter, image, mode); |
| 546 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(ima
ge->width() / 2, | |
| 547 ima
ge->height() / 2))); | |
| 548 SkAutoTUnref<SkImage> subsetImage(SkImage::NewFromBitmap(tmp)); | |
| 549 REPORTER_ASSERT(reporter, subsetImage); | |
| 550 | 544 |
| 551 SkBitmap subsetBitmap; | 545 RasterDataHolder dataHolder; |
| 552 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, mod
es[i])); | 546 image.reset(create_rasterproc_image(&dataHolder)); |
| 553 check_legacy_bitmap(reporter, subsetImage, subsetBitmap, modes[i]); | 547 test_legacy_bitmap(reporter, image, mode); |
| 548 image.reset(); |
| 549 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 550 |
| 551 image.reset(create_codec_image()); |
| 552 test_legacy_bitmap(reporter, image, mode); |
| 554 } | 553 } |
| 555 } | 554 } |
| 556 | 555 #if SK_SUPPORT_GPU |
| 557 static void test_imagepeek(skiatest::Reporter* reporter, GrContextFactory* facto
ry) { | 556 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, context) { |
| 558 static const struct { | 557 const SkImage::LegacyBitmapMode modes[] = { |
| 559 ImageType fType; | 558 SkImage::kRO_LegacyBitmapMode, |
| 560 bool fPeekShouldSucceed; | 559 SkImage::kRW_LegacyBitmapMode, |
| 561 const char* fName; | |
| 562 } gRec[] = { | |
| 563 { kRasterCopy_ImageType, true, "RasterCopy" }, | |
| 564 { kRasterData_ImageType, true, "RasterData" }, | |
| 565 { kRasterProc_ImageType, true, "RasterProc" }, | |
| 566 { kGpu_ImageType, false, "Gpu" }, | |
| 567 { kCodec_ImageType, false, "Codec" }, | |
| 568 }; | 560 }; |
| 569 | 561 for (auto& mode : modes) { |
| 570 const SkColor color = SK_ColorRED; | 562 SkAutoTUnref<SkImage> image(create_gpu_image(context)); |
| 571 const SkPMColor pmcolor = SkPreMultiplyColor(color); | 563 test_legacy_bitmap(reporter, image, mode); |
| 572 | |
| 573 GrContext* ctx = nullptr; | |
| 574 #if SK_SUPPORT_GPU | |
| 575 ctx = factory->get(GrContextFactory::kNative_GLContextType); | |
| 576 if (nullptr == ctx) { | |
| 577 return; | |
| 578 } | 564 } |
| 565 } |
| 579 #endif | 566 #endif |
| 580 | 567 |
| 581 ReleaseDataContext releaseCtx; | 568 static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectP
eekSuccess) { |
| 582 releaseCtx.fReporter = reporter; | 569 SkImageInfo info; |
| 570 size_t rowBytes; |
| 571 const void* addr = image->peekPixels(&info, &rowBytes); |
| 572 bool success = SkToBool(addr); |
| 573 REPORTER_ASSERT(reporter, expectPeekSuccess == success); |
| 574 if (success) { |
| 575 REPORTER_ASSERT(reporter, 20 == info.width()); |
| 576 REPORTER_ASSERT(reporter, 20 == info.height()); |
| 577 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); |
| 578 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || |
| 579 kOpaque_SkAlphaType == info.alphaType()); |
| 580 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); |
| 581 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *(const S
kPMColor*)addr); |
| 582 } |
| 583 } |
| 584 DEF_TEST(ImagePeek, reporter) { |
| 585 SkAutoTUnref<SkImage> image(create_image()); |
| 586 test_peek(reporter, image, true); |
| 583 | 587 |
| 584 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { | 588 image.reset(create_data_image()); |
| 585 SkImageInfo info; | 589 test_peek(reporter, image, true); |
| 586 size_t rowBytes; | |
| 587 | 590 |
| 588 releaseCtx.fData = nullptr; | 591 RasterDataHolder dataHolder; |
| 589 SkAutoTUnref<SkImage> image(create_image(reporter, gRec[i].fType, ctx, c
olor, &releaseCtx)); | 592 image.reset(create_rasterproc_image(&dataHolder)); |
| 590 if (!image.get()) { | 593 test_peek(reporter, image, true); |
| 591 SkDebugf("failed to createImage[%d] %s\n", i, gRec[i].fName); | 594 image.reset(); |
| 592 continue; // gpu may not be enabled | 595 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 593 } | |
| 594 if (kRasterProc_ImageType == gRec[i].fType) { | |
| 595 REPORTER_ASSERT(reporter, nullptr != releaseCtx.fData); // we are t
racking the data | |
| 596 } else { | |
| 597 REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we ignor
ed the context | |
| 598 } | |
| 599 | 596 |
| 600 test_legacy_bitmap(reporter, image); | 597 image.reset(create_codec_image()); |
| 601 | 598 test_peek(reporter, image, false); |
| 602 const void* addr = image->peekPixels(&info, &rowBytes); | |
| 603 bool success = SkToBool(addr); | |
| 604 REPORTER_ASSERT(reporter, gRec[i].fPeekShouldSucceed == success); | |
| 605 if (success) { | |
| 606 REPORTER_ASSERT(reporter, 10 == info.width()); | |
| 607 REPORTER_ASSERT(reporter, 10 == info.height()); | |
| 608 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); | |
| 609 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || | |
| 610 kOpaque_SkAlphaType == info.alphaType()); | |
| 611 REPORTER_ASSERT(reporter, info.minRowBytes() <= rowBytes); | |
| 612 REPORTER_ASSERT(reporter, pmcolor == *(const SkPMColor*)addr); | |
| 613 } | |
| 614 | |
| 615 test_image_readpixels(reporter, image, pmcolor); | |
| 616 } | |
| 617 REPORTER_ASSERT(reporter, nullptr == releaseCtx.fData); // we released the
data | |
| 618 } | 599 } |
| 619 #if SK_SUPPORT_GPU | 600 #if SK_SUPPORT_GPU |
| 601 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, context) { |
| 602 SkAutoTUnref<SkImage> image(create_gpu_image(context)); |
| 603 test_peek(reporter, image, false); |
| 604 } |
| 605 #endif |
| 620 | 606 |
| 621 struct ReleaseTextureContext { | 607 #if SK_SUPPORT_GPU |
| 622 ReleaseTextureContext(skiatest::Reporter* reporter) { | 608 struct TextureReleaseChecker { |
| 623 fReporter = reporter; | 609 TextureReleaseChecker() : fReleaseCount(0) {} |
| 624 fIsReleased = false; | 610 int fReleaseCount; |
| 625 } | 611 static void Release(void* self) { |
| 626 | 612 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++; |
| 627 skiatest::Reporter* fReporter; | |
| 628 bool fIsReleased; | |
| 629 | |
| 630 void doRelease() { | |
| 631 REPORTER_ASSERT(fReporter, false == fIsReleased); | |
| 632 fIsReleased = true; | |
| 633 } | |
| 634 | |
| 635 static void ReleaseProc(void* context) { | |
| 636 ((ReleaseTextureContext*)context)->doRelease(); | |
| 637 } | 613 } |
| 638 }; | 614 }; |
| 639 | 615 static void check_image_color(skiatest::Reporter* reporter, SkImage* image, SkPM
Color expected) { |
| 640 static SkImage* make_desc_image(GrContext* ctx, int w, int h, GrBackendObject te
xID, | |
| 641 ReleaseTextureContext* releaseContext) { | |
| 642 GrBackendTextureDesc desc; | |
| 643 desc.fConfig = kSkia8888_GrPixelConfig; | |
| 644 // need to be a rendertarget for now... | |
| 645 desc.fFlags = kRenderTarget_GrBackendTextureFlag; | |
| 646 desc.fWidth = w; | |
| 647 desc.fHeight = h; | |
| 648 desc.fSampleCnt = 0; | |
| 649 desc.fTextureHandle = texID; | |
| 650 return releaseContext | |
| 651 ? SkImage::NewFromTexture(ctx, desc, kPremul_SkAlphaType, | |
| 652 ReleaseTextureContext::ReleaseProc, re
leaseContext) | |
| 653 : SkImage::NewFromTextureCopy(ctx, desc, kPremul_SkAlphaType); | |
| 654 } | |
| 655 | |
| 656 static void test_image_color(skiatest::Reporter* reporter, SkImage* image, SkPMC
olor expected) { | |
| 657 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); | 616 const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 658 SkPMColor pixel; | 617 SkPMColor pixel; |
| 659 REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0,
0)); | 618 REPORTER_ASSERT(reporter, image->readPixels(info, &pixel, sizeof(pixel), 0,
0)); |
| 660 REPORTER_ASSERT(reporter, pixel == expected); | 619 REPORTER_ASSERT(reporter, pixel == expected); |
| 661 } | 620 } |
| 662 | 621 DEF_GPUTEST_FOR_NATIVE_CONTEXT(SkImage_NewFromTexture, reporter, context) { |
| 663 DEF_GPUTEST(SkImage_NewFromTexture, reporter, factory) { | 622 GrTextureProvider* provider = context->textureProvider(); |
| 664 GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType); | |
| 665 if (!ctx) { | |
| 666 REPORTER_ASSERT(reporter, false); | |
| 667 return; | |
| 668 } | |
| 669 GrTextureProvider* provider = ctx->textureProvider(); | |
| 670 | |
| 671 const int w = 10; | 623 const int w = 10; |
| 672 const int h = 10; | 624 const int h = 10; |
| 673 SkPMColor storage[w * h]; | 625 SkPMColor storage[w * h]; |
| 674 const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED); | 626 const SkPMColor expected0 = SkPreMultiplyColor(SK_ColorRED); |
| 675 sk_memset32(storage, expected0, w * h); | 627 sk_memset32(storage, expected0, w * h); |
| 676 | |
| 677 GrSurfaceDesc desc; | 628 GrSurfaceDesc desc; |
| 678 desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget fo
r readpixels(); | 629 desc.fFlags = kRenderTarget_GrSurfaceFlag; // needs to be a rendertarget fo
r readpixels(); |
| 679 desc.fOrigin = kDefault_GrSurfaceOrigin; | 630 desc.fOrigin = kDefault_GrSurfaceOrigin; |
| 680 desc.fWidth = w; | 631 desc.fWidth = w; |
| 681 desc.fHeight = h; | 632 desc.fHeight = h; |
| 682 desc.fConfig = kSkia8888_GrPixelConfig; | 633 desc.fConfig = kSkia8888_GrPixelConfig; |
| 683 desc.fSampleCnt = 0; | 634 desc.fSampleCnt = 0; |
| 684 | |
| 685 SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w
* 4)); | 635 SkAutoTUnref<GrTexture> tex(provider->createTexture(desc, false, storage, w
* 4)); |
| 686 if (!tex) { | 636 if (!tex) { |
| 687 REPORTER_ASSERT(reporter, false); | 637 REPORTER_ASSERT(reporter, false); |
| 688 return; | 638 return; |
| 689 } | 639 } |
| 690 | 640 |
| 691 GrBackendObject srcTex = tex->getTextureHandle(); | 641 GrBackendTextureDesc backendDesc; |
| 692 ReleaseTextureContext releaseCtx(reporter); | 642 backendDesc.fConfig = kSkia8888_GrPixelConfig; |
| 643 backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; |
| 644 backendDesc.fWidth = w; |
| 645 backendDesc.fHeight = h; |
| 646 backendDesc.fSampleCnt = 0; |
| 647 backendDesc.fTextureHandle = tex->getTextureHandle(); |
| 648 TextureReleaseChecker releaseChecker; |
| 649 SkAutoTUnref<SkImage> refImg( |
| 650 SkImage::NewFromTexture(context, backendDesc, kPremul_SkAlphaType, |
| 651 TextureReleaseChecker::Release, &releaseChecker)
); |
| 652 SkAutoTUnref<SkImage> cpyImg(SkImage::NewFromTextureCopy(context, backendDes
c, |
| 653 kPremul_SkAlphaType
)); |
| 693 | 654 |
| 694 SkAutoTUnref<SkImage> refImg(make_desc_image(ctx, w, h, srcTex, &releaseCtx)
); | 655 check_image_color(reporter, refImg, expected0); |
| 695 SkAutoTUnref<SkImage> cpyImg(make_desc_image(ctx, w, h, srcTex, nullptr)); | 656 check_image_color(reporter, cpyImg, expected0); |
| 696 | |
| 697 test_image_color(reporter, refImg, expected0); | |
| 698 test_image_color(reporter, cpyImg, expected0); | |
| 699 | 657 |
| 700 // Now lets jam new colors into our "external" texture, and see if the image
s notice | 658 // Now lets jam new colors into our "external" texture, and see if the image
s notice |
| 701 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); | 659 const SkPMColor expected1 = SkPreMultiplyColor(SK_ColorBLUE); |
| 702 sk_memset32(storage, expected1, w * h); | 660 sk_memset32(storage, expected1, w * h); |
| 703 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kF
lushWrites_PixelOp); | 661 tex->writePixels(0, 0, w, h, kSkia8888_GrPixelConfig, storage, GrContext::kF
lushWrites_PixelOp); |
| 704 | 662 |
| 705 // The cpy'd one should still see the old color | 663 // The cpy'd one should still see the old color |
| 706 #if 0 | 664 #if 0 |
| 707 // There is no guarantee that refImg sees the new color. We are free to have
made a copy. Our | 665 // There is no guarantee that refImg sees the new color. We are free to have
made a copy. Our |
| 708 // write pixels call violated the contract with refImg and refImg is now und
efined. | 666 // write pixels call violated the contract with refImg and refImg is now und
efined. |
| 709 test_image_color(reporter, refImg, expected1); | 667 check_image_color(reporter, refImg, expected1); |
| 710 #endif | 668 #endif |
| 711 test_image_color(reporter, cpyImg, expected0); | 669 check_image_color(reporter, cpyImg, expected0); |
| 712 | 670 |
| 713 // Now exercise the release proc | 671 // Now exercise the release proc |
| 714 REPORTER_ASSERT(reporter, !releaseCtx.fIsReleased); | 672 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount); |
| 715 refImg.reset(nullptr); // force a release of the image | 673 refImg.reset(nullptr); // force a release of the image |
| 716 REPORTER_ASSERT(reporter, releaseCtx.fIsReleased); | 674 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount); |
| 717 } | 675 } |
| 718 #endif | 676 #endif |
| 719 DEF_GPUTEST(ImageTestsFromSurfaceTestsTODO, reporter, factory) { | |
| 720 test_image(reporter); | |
| 721 test_empty_image(reporter); | |
| 722 test_imagepeek(reporter, factory); | |
| 723 } | |
| OLD | NEW |