reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 8 | #include <functional> |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 9 | #include <initializer_list> |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 10 | #include <vector> |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 11 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 12 | #include "SkAutoPixmapStorage.h" |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 13 | #include "SkBitmap.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 15 | #include "SkColorSpacePriv.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 16 | #include "SkData.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 17 | #include "SkImageEncoder.h" |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 18 | #include "SkImageGenerator.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 19 | #include "SkImage_Base.h" |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 20 | #include "SkImagePriv.h" |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 21 | #include "SkMakeUnique.h" |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 22 | #include "SkPicture.h" |
| 23 | #include "SkPictureRecorder.h" |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 24 | #include "SkPixelSerializer.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 25 | #include "SkRRect.h" |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 26 | #include "SkStream.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 27 | #include "SkSurface.h" |
| 28 | #include "SkUtils.h" |
| 29 | #include "Test.h" |
| 30 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 31 | #include "Resources.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 32 | #include "sk_tool_utils.h" |
| 33 | |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 34 | #if SK_SUPPORT_GPU |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 35 | #include "GrContextPriv.h" |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 36 | #include "GrGpu.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 37 | #include "GrResourceCache.h" |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 38 | #include "GrTest.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 39 | #include "GrTexture.h" |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 40 | #endif |
| 41 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 42 | using namespace sk_gpu_test; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 43 | |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 44 | SkImageInfo read_pixels_info(SkImage* image) { |
| 45 | if (as_IB(image)->onImageInfo().colorSpace()) { |
| 46 | return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType()); |
| 47 | } |
| 48 | |
| 49 | return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); |
| 50 | } |
| 51 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 52 | static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA, |
| 53 | SkImage* b) { |
| 54 | const int widthA = subsetA ? subsetA->width() : a->width(); |
| 55 | const int heightA = subsetA ? subsetA->height() : a->height(); |
| 56 | |
| 57 | REPORTER_ASSERT(reporter, widthA == b->width()); |
| 58 | REPORTER_ASSERT(reporter, heightA == b->height()); |
reed | 1cb3646 | 2016-03-09 15:21:32 -0800 | [diff] [blame] | 59 | |
| 60 | // see https://bug.skia.org/3965 |
| 61 | //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 62 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 63 | SkAutoPixmapStorage pmapA, pmapB; |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 64 | pmapA.alloc(read_pixels_info(a)); |
| 65 | pmapB.alloc(read_pixels_info(b)); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 66 | |
| 67 | const int srcX = subsetA ? subsetA->x() : 0; |
| 68 | const int srcY = subsetA ? subsetA->y() : 0; |
| 69 | |
| 70 | REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY)); |
| 71 | REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0)); |
| 72 | |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 73 | const size_t widthBytes = widthA * 4; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 74 | for (int y = 0; y < heightA; ++y) { |
| 75 | REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes)); |
| 76 | } |
| 77 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 78 | static void draw_image_test_pattern(SkCanvas* canvas) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 79 | canvas->clear(SK_ColorWHITE); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 80 | SkPaint paint; |
| 81 | paint.setColor(SK_ColorBLACK); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 82 | canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); |
| 83 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 84 | static sk_sp<SkImage> create_image() { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 85 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 86 | auto surface(SkSurface::MakeRaster(info)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 87 | draw_image_test_pattern(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 88 | return surface->makeImageSnapshot(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 89 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 90 | static sk_sp<SkData> create_image_data(SkImageInfo* info) { |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 91 | *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 92 | const size_t rowBytes = info->minRowBytes(); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 93 | sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height())); |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 94 | { |
| 95 | SkBitmap bm; |
| 96 | bm.installPixels(*info, data->writable_data(), rowBytes); |
| 97 | SkCanvas canvas(bm); |
| 98 | draw_image_test_pattern(&canvas); |
| 99 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 100 | return data; |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 101 | } |
| 102 | static sk_sp<SkImage> create_data_image() { |
| 103 | SkImageInfo info; |
| 104 | sk_sp<SkData> data(create_image_data(&info)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 105 | return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes()); |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 106 | } |
| 107 | #if SK_SUPPORT_GPU // not gpu-specific but currently only used in GPU tests |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 108 | static sk_sp<SkImage> create_image_large(int maxTextureSize) { |
| 109 | const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType); |
bsalomon | d490708 | 2016-06-13 12:13:03 -0700 | [diff] [blame] | 110 | auto surface(SkSurface::MakeRaster(info)); |
| 111 | surface->getCanvas()->clear(SK_ColorWHITE); |
| 112 | SkPaint paint; |
| 113 | paint.setColor(SK_ColorBLACK); |
| 114 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint); |
| 115 | return surface->makeImageSnapshot(); |
| 116 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 117 | static sk_sp<SkImage> create_picture_image() { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 118 | SkPictureRecorder recorder; |
| 119 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 120 | canvas->clear(SK_ColorCYAN); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 121 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10), |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 122 | nullptr, nullptr, SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 123 | SkColorSpace::MakeSRGB()); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 124 | }; |
| 125 | #endif |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 126 | // Want to ensure that our Release is called when the owning image is destroyed |
| 127 | struct RasterDataHolder { |
| 128 | RasterDataHolder() : fReleaseCount(0) {} |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 129 | sk_sp<SkData> fData; |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 130 | int fReleaseCount; |
| 131 | static void Release(const void* pixels, void* context) { |
| 132 | RasterDataHolder* self = static_cast<RasterDataHolder*>(context); |
| 133 | self->fReleaseCount++; |
| 134 | self->fData.reset(); |
| 135 | } |
| 136 | }; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 137 | static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 138 | SkASSERT(dataHolder); |
| 139 | SkImageInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 140 | dataHolder->fData = create_image_data(&info); |
| 141 | return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()), |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 142 | RasterDataHolder::Release, dataHolder); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 143 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 144 | static sk_sp<SkImage> create_codec_image() { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 145 | SkImageInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 146 | sk_sp<SkData> data(create_image_data(&info)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 147 | SkBitmap bitmap; |
| 148 | bitmap.installPixels(info, data->writable_data(), info.minRowBytes()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 149 | sk_sp<SkData> src(sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG, 100)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 150 | return SkImage::MakeFromEncoded(std::move(src)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 151 | } |
| 152 | #if SK_SUPPORT_GPU |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 153 | static sk_sp<SkImage> create_gpu_image(GrContext* context) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 154 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 155 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 156 | draw_image_test_pattern(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 157 | return surface->makeImageSnapshot(); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 158 | } |
| 159 | #endif |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 160 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 161 | static void test_encode(skiatest::Reporter* reporter, SkImage* image) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 162 | const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 163 | sk_sp<SkData> origEncoded = image->encodeToData(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 164 | REPORTER_ASSERT(reporter, origEncoded); |
| 165 | REPORTER_ASSERT(reporter, origEncoded->size() > 0); |
| 166 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 167 | sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded)); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 168 | if (!decoded) { |
| 169 | ERRORF(reporter, "failed to decode image!"); |
| 170 | return; |
| 171 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, decoded); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 173 | assert_equal(reporter, image, nullptr, decoded.get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 174 | |
| 175 | // Now see if we can instantiate an image from a subset of the surface/origEncoded |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 176 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 177 | decoded = SkImage::MakeFromEncoded(origEncoded, &ir); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, decoded); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 179 | assert_equal(reporter, image, &ir, decoded.get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 180 | } |
| 181 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 182 | DEF_TEST(ImageEncode, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 183 | test_encode(reporter, create_image().get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 187 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 188 | test_encode(reporter, create_gpu_image(ctxInfo.grContext()).get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 189 | } |
| 190 | #endif |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 191 | |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 192 | DEF_TEST(Image_MakeFromRasterBitmap, reporter) { |
| 193 | const struct { |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 194 | SkCopyPixelsMode fCPM; |
| 195 | bool fExpectSameAsMutable; |
| 196 | bool fExpectSameAsImmutable; |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 197 | } recs[] = { |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 198 | { kIfMutable_SkCopyPixelsMode, false, true }, |
| 199 | { kAlways_SkCopyPixelsMode, false, false }, |
| 200 | { kNever_SkCopyPixelsMode, true, true }, |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 201 | }; |
| 202 | for (auto rec : recs) { |
| 203 | SkPixmap pm; |
| 204 | SkBitmap bm; |
| 205 | bm.allocN32Pixels(100, 100); |
| 206 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 207 | auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 208 | REPORTER_ASSERT(reporter, img->peekPixels(&pm)); |
| 209 | const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0); |
| 210 | REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable); |
reed | ae29644 | 2016-08-05 13:19:01 -0700 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 212 | |
| 213 | bm.notifyPixelsChanged(); // force a new generation ID |
| 214 | |
| 215 | bm.setImmutable(); |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 216 | img = SkMakeImageFromRasterBitmap(bm, rec.fCPM); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, img->peekPixels(&pm)); |
| 218 | const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0); |
| 219 | REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable); |
reed | ae29644 | 2016-08-05 13:19:01 -0700 | [diff] [blame] | 220 | REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 224 | namespace { |
| 225 | |
| 226 | const char* kSerializedData = "serialized"; |
| 227 | |
| 228 | class MockSerializer : public SkPixelSerializer { |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 229 | public: |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 230 | MockSerializer(sk_sp<SkData> (*func)()) : fFunc(func), fDidEncode(false) { } |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 231 | |
| 232 | bool didEncode() const { return fDidEncode; } |
| 233 | |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 234 | protected: |
reed | c9e190d | 2015-09-28 09:58:41 -0700 | [diff] [blame] | 235 | bool onUseEncodedData(const void*, size_t) override { |
| 236 | return false; |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 237 | } |
| 238 | |
halcanary | 9907371 | 2015-12-10 09:30:57 -0800 | [diff] [blame] | 239 | SkData* onEncode(const SkPixmap&) override { |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 240 | fDidEncode = true; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 241 | return fFunc().release(); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 242 | } |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 243 | |
| 244 | private: |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 245 | sk_sp<SkData> (*fFunc)(); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 246 | bool fDidEncode; |
| 247 | |
| 248 | typedef SkPixelSerializer INHERITED; |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | } // anonymous namespace |
| 252 | |
| 253 | // Test that SkImage encoding observes custom pixel serializers. |
| 254 | DEF_TEST(Image_Encode_Serializer, reporter) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 255 | MockSerializer serializer([]() -> sk_sp<SkData> { |
| 256 | return SkData::MakeWithCString(kSerializedData); |
| 257 | }); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 258 | sk_sp<SkImage> image(create_image()); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 259 | sk_sp<SkData> encoded = image->encodeToData(&serializer); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 260 | sk_sp<SkData> reference(SkData::MakeWithCString(kSerializedData)); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 261 | |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 262 | REPORTER_ASSERT(reporter, serializer.didEncode()); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 263 | REPORTER_ASSERT(reporter, encoded); |
| 264 | REPORTER_ASSERT(reporter, encoded->size() > 0); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 265 | REPORTER_ASSERT(reporter, encoded->equals(reference.get())); |
fmalita | 2be7125 | 2015-09-03 07:17:25 -0700 | [diff] [blame] | 266 | } |
| 267 | |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 268 | // Test that image encoding failures do not break picture serialization/deserialization. |
| 269 | DEF_TEST(Image_Serialize_Encoding_Failure, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 270 | auto surface(SkSurface::MakeRasterN32Premul(100, 100)); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 271 | surface->getCanvas()->clear(SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 272 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 273 | REPORTER_ASSERT(reporter, image); |
| 274 | |
| 275 | SkPictureRecorder recorder; |
| 276 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 277 | canvas->drawImage(image, 0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 278 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 279 | REPORTER_ASSERT(reporter, picture); |
| 280 | REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0); |
| 281 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 282 | MockSerializer emptySerializer([]() -> sk_sp<SkData> { return SkData::MakeEmpty(); }); |
| 283 | MockSerializer nullSerializer([]() -> sk_sp<SkData> { return nullptr; }); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 284 | MockSerializer* serializers[] = { &emptySerializer, &nullSerializer }; |
| 285 | |
| 286 | for (size_t i = 0; i < SK_ARRAY_COUNT(serializers); ++i) { |
| 287 | SkDynamicMemoryWStream wstream; |
| 288 | REPORTER_ASSERT(reporter, !serializers[i]->didEncode()); |
| 289 | picture->serialize(&wstream, serializers[i]); |
| 290 | REPORTER_ASSERT(reporter, serializers[i]->didEncode()); |
| 291 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 292 | std::unique_ptr<SkStream> rstream(wstream.detachAsStream()); |
| 293 | sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rstream.get())); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 294 | REPORTER_ASSERT(reporter, deserialized); |
| 295 | REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0); |
| 296 | } |
| 297 | } |
| 298 | |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 299 | // Test that a draw that only partially covers the drawing surface isn't |
| 300 | // interpreted as covering the entire drawing surface (i.e., exercise one of the |
| 301 | // conditions of SkCanvas::wouldOverwriteEntireSurface()). |
| 302 | DEF_TEST(Image_RetainSnapshot, reporter) { |
| 303 | const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0); |
| 304 | const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0); |
| 305 | SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 306 | auto surface(SkSurface::MakeRaster(info)); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 307 | surface->getCanvas()->clear(0xFF00FF00); |
| 308 | |
| 309 | SkPMColor pixels[4]; |
| 310 | memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect |
| 311 | const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2); |
| 312 | const size_t dstRowBytes = 2 * sizeof(SkPMColor); |
| 313 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 314 | sk_sp<SkImage> image1(surface->makeImageSnapshot()); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0)); |
| 316 | for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) { |
| 317 | REPORTER_ASSERT(reporter, pixels[i] == green); |
| 318 | } |
| 319 | |
| 320 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 321 | paint.setBlendMode(SkBlendMode::kSrc); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 322 | paint.setColor(SK_ColorRED); |
| 323 | |
| 324 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint); |
| 325 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 326 | sk_sp<SkImage> image2(surface->makeImageSnapshot()); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 327 | REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0)); |
| 328 | REPORTER_ASSERT(reporter, pixels[0] == green); |
| 329 | REPORTER_ASSERT(reporter, pixels[1] == green); |
| 330 | REPORTER_ASSERT(reporter, pixels[2] == green); |
| 331 | REPORTER_ASSERT(reporter, pixels[3] == red); |
| 332 | } |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 333 | |
| 334 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 335 | |
| 336 | static void make_bitmap_mutable(SkBitmap* bm) { |
| 337 | bm->allocN32Pixels(10, 10); |
| 338 | } |
| 339 | |
| 340 | static void make_bitmap_immutable(SkBitmap* bm) { |
| 341 | bm->allocN32Pixels(10, 10); |
| 342 | bm->setImmutable(); |
| 343 | } |
| 344 | |
| 345 | DEF_TEST(image_newfrombitmap, reporter) { |
| 346 | const struct { |
| 347 | void (*fMakeProc)(SkBitmap*); |
| 348 | bool fExpectPeekSuccess; |
| 349 | bool fExpectSharedID; |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 350 | bool fExpectLazy; |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 351 | } rec[] = { |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 352 | { make_bitmap_mutable, true, false, false }, |
| 353 | { make_bitmap_immutable, true, true, false }, |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 357 | SkBitmap bm; |
| 358 | rec[i].fMakeProc(&bm); |
| 359 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 360 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 361 | SkPixmap pmap; |
| 362 | |
| 363 | const bool sharedID = (image->uniqueID() == bm.getGenerationID()); |
| 364 | REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID); |
| 365 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 366 | const bool peekSuccess = image->peekPixels(&pmap); |
| 367 | REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess); |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 368 | |
| 369 | const bool lazy = image->isLazyGenerated(); |
| 370 | REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 373 | |
| 374 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 375 | #if SK_SUPPORT_GPU |
| 376 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 377 | #include "SkBitmapCache.h" |
| 378 | |
| 379 | /* |
| 380 | * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image. |
| 381 | * We cache it for performance when drawing into a raster surface. |
| 382 | * |
| 383 | * A cleaner test would know if each drawImage call triggered a read-back from the gpu, |
| 384 | * but we don't have that facility (at the moment) so we use a little internal knowledge |
| 385 | * of *how* the raster version is cached, and look for that. |
| 386 | */ |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 387 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(c, reporter, ctxInfo) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 388 | SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 389 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 390 | const uint32_t uniqueID = image->uniqueID(); |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 391 | const auto desc = SkBitmapCacheDesc::Make(image.get()); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 392 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 393 | auto surface(SkSurface::MakeRaster(info)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 394 | |
| 395 | // now we can test drawing a gpu-backed image into a cpu-backed surface |
| 396 | |
| 397 | { |
| 398 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 399 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | surface->getCanvas()->drawImage(image, 0, 0); |
| 403 | { |
| 404 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 405 | if (SkBitmapCache::Find(desc, &cachedBitmap)) { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 406 | REPORTER_ASSERT(reporter, cachedBitmap.getGenerationID() == uniqueID); |
| 407 | REPORTER_ASSERT(reporter, cachedBitmap.isImmutable()); |
| 408 | REPORTER_ASSERT(reporter, cachedBitmap.getPixels()); |
| 409 | } else { |
| 410 | // unexpected, but not really a bug, since the cache is global and this test may be |
| 411 | // run w/ other threads competing for its budget. |
| 412 | SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | image.reset(nullptr); |
| 417 | { |
| 418 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 419 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 422 | |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 423 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) { |
| 424 | GrContext* context = contextInfo.grContext(); |
| 425 | sk_gpu_test::TestContext* testContext = contextInfo.testContext(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 426 | GrContextFactory otherFactory; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 427 | ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type()); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 428 | testContext->makeCurrent(); |
| 429 | |
| 430 | std::function<sk_sp<SkImage>()> imageFactories[] = { |
| 431 | create_image, |
| 432 | create_codec_image, |
| 433 | create_data_image, |
| 434 | // Create an image from a picture. |
| 435 | create_picture_image, |
| 436 | // Create a texture image. |
| 437 | [context] { return create_gpu_image(context); }, |
| 438 | // Create a texture image in a another GrContext. |
| 439 | [testContext, otherContextInfo] { |
| 440 | otherContextInfo.testContext()->makeCurrent(); |
| 441 | sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext()); |
| 442 | testContext->makeCurrent(); |
| 443 | return otherContextImage; |
| 444 | } |
| 445 | }; |
| 446 | |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 447 | sk_sp<SkColorSpace> dstColorSpaces[] ={ |
| 448 | nullptr, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 449 | SkColorSpace::MakeSRGB(), |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 450 | }; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 451 | |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 452 | for (auto& dstColorSpace : dstColorSpaces) { |
| 453 | for (auto factory : imageFactories) { |
| 454 | sk_sp<SkImage> image(factory()); |
| 455 | if (!image) { |
| 456 | ERRORF(reporter, "Error creating image."); |
| 457 | continue; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 458 | } |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 459 | |
| 460 | sk_sp<SkImage> texImage(image->makeTextureImage(context, dstColorSpace.get())); |
| 461 | if (!texImage) { |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 462 | GrContext* imageContext = as_IB(image)->context(); |
| 463 | |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 464 | // We expect to fail if image comes from a different GrContext. |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 465 | if (!image->isTextureBacked() || imageContext == context) { |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 466 | ERRORF(reporter, "makeTextureImage failed."); |
| 467 | } |
| 468 | continue; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 469 | } |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 470 | if (!texImage->isTextureBacked()) { |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 471 | ERRORF(reporter, "makeTextureImage returned non-texture image."); |
| 472 | continue; |
| 473 | } |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 474 | if (image->isTextureBacked()) { |
| 475 | GrSurfaceProxy* origProxy = as_IB(image)->peekProxy(); |
| 476 | GrSurfaceProxy* copyProxy = as_IB(texImage)->peekProxy(); |
| 477 | |
| 478 | if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) { |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 479 | ERRORF(reporter, "makeTextureImage made unnecessary texture copy."); |
| 480 | } |
| 481 | } |
| 482 | if (image->width() != texImage->width() || image->height() != texImage->height()) { |
| 483 | ERRORF(reporter, "makeTextureImage changed the image size."); |
| 484 | } |
| 485 | if (image->alphaType() != texImage->alphaType()) { |
| 486 | ERRORF(reporter, "makeTextureImage changed image alpha type."); |
| 487 | } |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 488 | } |
Robert Phillips | d9971c0 | 2017-08-02 11:51:43 -0400 | [diff] [blame] | 489 | |
| 490 | testContext->makeCurrent(); |
| 491 | context->flush(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 495 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) { |
| 496 | GrContext* context = contextInfo.grContext(); |
| 497 | |
| 498 | std::function<sk_sp<SkImage>()> imageFactories[] = { |
| 499 | create_image, |
| 500 | create_codec_image, |
| 501 | create_data_image, |
| 502 | create_picture_image, |
| 503 | [context] { return create_gpu_image(context); }, |
| 504 | }; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 505 | SkColorSpace* legacyColorSpace = nullptr; |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 506 | for (auto factory : imageFactories) { |
| 507 | sk_sp<SkImage> image = factory(); |
| 508 | if (!image->isTextureBacked()) { |
| 509 | REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get()); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 510 | if (!(image = image->makeTextureImage(context, legacyColorSpace))) { |
| 511 | continue; |
| 512 | } |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 513 | } |
| 514 | auto rasterImage = image->makeNonTextureImage(); |
| 515 | if (!rasterImage) { |
| 516 | ERRORF(reporter, "makeNonTextureImage failed for texture-backed image."); |
| 517 | } |
| 518 | REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked()); |
| 519 | assert_equal(reporter, image.get(), nullptr, rasterImage.get()); |
| 520 | } |
| 521 | } |
| 522 | |
bsalomon | 363bc33 | 2016-06-15 14:15:30 -0700 | [diff] [blame] | 523 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_drawAbandonedGpuImage, reporter, contextInfo) { |
bsalomon | e5b651f | 2016-06-15 07:36:15 -0700 | [diff] [blame] | 524 | auto context = contextInfo.grContext(); |
| 525 | auto image = create_gpu_image(context); |
| 526 | auto info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 527 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 528 | image->getTexture()->abandon(); |
bsalomon | e5b651f | 2016-06-15 07:36:15 -0700 | [diff] [blame] | 529 | surface->getCanvas()->drawImage(image, 0, 0); |
| 530 | } |
| 531 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 532 | #endif |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 533 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 534 | class EmptyGenerator : public SkImageGenerator { |
| 535 | public: |
| 536 | EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 537 | }; |
| 538 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 539 | DEF_TEST(ImageEmpty, reporter) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 540 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 541 | SkPixmap pmap(info, nullptr, 0); |
| 542 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap)); |
| 543 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0)); |
| 544 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr)); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 545 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator( |
| 546 | skstd::make_unique<EmptyGenerator>())); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 547 | } |
| 548 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 549 | DEF_TEST(ImageDataRef, reporter) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 550 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 551 | size_t rowBytes = info.minRowBytes(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 552 | size_t size = info.computeByteSize(rowBytes); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 553 | sk_sp<SkData> data = SkData::MakeUninitialized(size); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 554 | REPORTER_ASSERT(reporter, data->unique()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 555 | sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 556 | REPORTER_ASSERT(reporter, !data->unique()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 557 | image.reset(); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 558 | REPORTER_ASSERT(reporter, data->unique()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 559 | } |
| 560 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 561 | static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) { |
| 562 | for (int i = 0; i < count; ++i) { |
| 563 | if (pixels[i] != expected) { |
| 564 | return false; |
| 565 | } |
| 566 | } |
| 567 | return true; |
| 568 | } |
| 569 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 570 | static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 571 | if (!image) { |
| 572 | ERRORF(reporter, "Failed to create image!"); |
| 573 | return; |
| 574 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 575 | const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 576 | const SkPMColor notExpected = ~expected; |
| 577 | |
| 578 | const int w = 2, h = 2; |
| 579 | const size_t rowBytes = w * sizeof(SkPMColor); |
| 580 | SkPMColor pixels[w*h]; |
| 581 | |
| 582 | SkImageInfo info; |
| 583 | |
| 584 | info = SkImageInfo::MakeUnknown(w, h); |
| 585 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 586 | |
| 587 | // out-of-bounds should fail |
| 588 | info = SkImageInfo::MakeN32Premul(w, h); |
| 589 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0)); |
| 590 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h)); |
| 591 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0)); |
| 592 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height())); |
| 593 | |
| 594 | // top-left should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 595 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 596 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 597 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 598 | |
| 599 | // bottom-right should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 600 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 601 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 602 | image->width() - w, image->height() - h)); |
| 603 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 604 | |
| 605 | // partial top-left should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 606 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 607 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1)); |
| 608 | REPORTER_ASSERT(reporter, pixels[3] == expected); |
| 609 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); |
| 610 | |
| 611 | // partial bottom-right should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 612 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 613 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 614 | image->width() - 1, image->height() - 1)); |
| 615 | REPORTER_ASSERT(reporter, pixels[0] == expected); |
| 616 | REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); |
| 617 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 618 | DEF_TEST(ImageReadPixels, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 619 | sk_sp<SkImage> image(create_image()); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 620 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 621 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 622 | image = create_data_image(); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 623 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 624 | |
| 625 | RasterDataHolder dataHolder; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 626 | image = create_rasterproc_image(&dataHolder); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 627 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 628 | image.reset(); |
| 629 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 630 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 631 | image = create_codec_image(); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 632 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 633 | } |
| 634 | #if SK_SUPPORT_GPU |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 635 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 636 | image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 637 | } |
| 638 | #endif |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 639 | |
| 640 | static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image, |
| 641 | const SkBitmap& bitmap, SkImage::LegacyBitmapMode mode) { |
| 642 | REPORTER_ASSERT(reporter, image->width() == bitmap.width()); |
| 643 | REPORTER_ASSERT(reporter, image->height() == bitmap.height()); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 644 | REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 645 | |
| 646 | if (SkImage::kRO_LegacyBitmapMode == mode) { |
| 647 | REPORTER_ASSERT(reporter, bitmap.isImmutable()); |
| 648 | } |
| 649 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 650 | REPORTER_ASSERT(reporter, bitmap.getPixels()); |
| 651 | |
| 652 | const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType()); |
| 653 | SkPMColor imageColor; |
| 654 | REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0)); |
| 655 | REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0)); |
| 656 | } |
| 657 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 658 | static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image, SkImage::LegacyBitmapMode mode) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 659 | if (!image) { |
| 660 | ERRORF(reporter, "Failed to create image."); |
| 661 | return; |
| 662 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 663 | SkBitmap bitmap; |
| 664 | REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, mode)); |
| 665 | check_legacy_bitmap(reporter, image, bitmap, mode); |
| 666 | |
| 667 | // Test subsetting to exercise the rowBytes logic. |
| 668 | SkBitmap tmp; |
| 669 | REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2, |
| 670 | image->height() / 2))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 671 | sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp)); |
| 672 | REPORTER_ASSERT(reporter, subsetImage.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 673 | |
| 674 | SkBitmap subsetBitmap; |
| 675 | REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, mode)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 676 | check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap, mode); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 677 | } |
| 678 | DEF_TEST(ImageLegacyBitmap, reporter) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 679 | const SkImage::LegacyBitmapMode modes[] = { |
| 680 | SkImage::kRO_LegacyBitmapMode, |
| 681 | SkImage::kRW_LegacyBitmapMode, |
| 682 | }; |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 683 | for (auto& mode : modes) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 684 | sk_sp<SkImage> image(create_image()); |
| 685 | test_legacy_bitmap(reporter, image.get(), mode); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 686 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 687 | image = create_data_image(); |
| 688 | test_legacy_bitmap(reporter, image.get(), mode); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 689 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 690 | RasterDataHolder dataHolder; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 691 | image = create_rasterproc_image(&dataHolder); |
| 692 | test_legacy_bitmap(reporter, image.get(), mode); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 693 | image.reset(); |
| 694 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 695 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 696 | image = create_codec_image(); |
| 697 | test_legacy_bitmap(reporter, image.get(), mode); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 698 | } |
| 699 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 700 | #if SK_SUPPORT_GPU |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 701 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 702 | const SkImage::LegacyBitmapMode modes[] = { |
| 703 | SkImage::kRO_LegacyBitmapMode, |
| 704 | SkImage::kRW_LegacyBitmapMode, |
| 705 | }; |
| 706 | for (auto& mode : modes) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 707 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 708 | test_legacy_bitmap(reporter, image.get(), mode); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 709 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 710 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 711 | #endif |
| 712 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 713 | static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 714 | if (!image) { |
| 715 | ERRORF(reporter, "Failed to create image!"); |
| 716 | return; |
| 717 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 718 | SkPixmap pm; |
| 719 | bool success = image->peekPixels(&pm); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 720 | REPORTER_ASSERT(reporter, expectPeekSuccess == success); |
| 721 | if (success) { |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 722 | const SkImageInfo& info = pm.info(); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 723 | REPORTER_ASSERT(reporter, 20 == info.width()); |
| 724 | REPORTER_ASSERT(reporter, 20 == info.height()); |
| 725 | REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); |
| 726 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || |
| 727 | kOpaque_SkAlphaType == info.alphaType()); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 728 | REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes()); |
| 729 | REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0)); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 730 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 731 | } |
| 732 | DEF_TEST(ImagePeek, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 733 | sk_sp<SkImage> image(create_image()); |
| 734 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 735 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 736 | image = create_data_image(); |
| 737 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 738 | |
| 739 | RasterDataHolder dataHolder; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 740 | image = create_rasterproc_image(&dataHolder); |
| 741 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 742 | image.reset(); |
| 743 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 744 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 745 | image = create_codec_image(); |
| 746 | test_peek(reporter, image.get(), false); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 747 | } |
| 748 | #if SK_SUPPORT_GPU |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 749 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 750 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 751 | test_peek(reporter, image.get(), false); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 752 | } |
| 753 | #endif |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 754 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 755 | #if SK_SUPPORT_GPU |
| 756 | struct TextureReleaseChecker { |
| 757 | TextureReleaseChecker() : fReleaseCount(0) {} |
| 758 | int fReleaseCount; |
| 759 | static void Release(void* self) { |
| 760 | static_cast<TextureReleaseChecker*>(self)->fReleaseCount++; |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 761 | } |
| 762 | }; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 763 | |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 764 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) { |
| 765 | const int kWidth = 10; |
| 766 | const int kHeight = 10; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 767 | std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 768 | |
| 769 | GrContext* ctx = ctxInfo.grContext(); |
| 770 | |
| 771 | GrBackendObject backendTexHandle = |
| 772 | ctxInfo.grContext()->getGpu()->createTestingOnlyBackendTexture( |
| 773 | pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true); |
| 774 | |
| 775 | GrBackendTexture backendTex = GrTest::CreateBackendTexture(ctx->contextPriv().getBackend(), |
| 776 | kWidth, |
| 777 | kHeight, |
| 778 | kRGBA_8888_GrPixelConfig, |
| 779 | backendTexHandle); |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 780 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 781 | TextureReleaseChecker releaseChecker; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 782 | GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 783 | sk_sp<SkImage> refImg( |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 784 | SkImage::MakeFromTexture(ctx, backendTex, texOrigin, kPremul_SkAlphaType, nullptr, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 785 | TextureReleaseChecker::Release, &releaseChecker)); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 786 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 787 | GrSurfaceOrigin readBackOrigin; |
| 788 | GrBackendObject readBackHandle = refImg->getTextureHandle(false, &readBackOrigin); |
| 789 | // TODO: Make it so we can check this (see skbug.com/5019) |
| 790 | #if 0 |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 791 | if (*readBackHandle != *(backendTexHandle)) { |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 792 | ERRORF(reporter, "backend mismatch %d %d\n", |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 793 | (int)readBackHandle, (int)backendTexHandle); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 794 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 795 | REPORTER_ASSERT(reporter, readBackHandle == backendTexHandle); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 796 | #else |
| 797 | REPORTER_ASSERT(reporter, SkToBool(readBackHandle)); |
| 798 | #endif |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 799 | if (readBackOrigin != texOrigin) { |
| 800 | ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 801 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 802 | REPORTER_ASSERT(reporter, readBackOrigin == texOrigin); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 803 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 804 | // Now exercise the release proc |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 805 | REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 806 | refImg.reset(nullptr); // force a release of the image |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 807 | REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount); |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 808 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 809 | ctxInfo.grContext()->getGpu()->deleteTestingOnlyBackendTexture(backendTexHandle); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 810 | } |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 811 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 812 | DEF_GPUTEST(SkImage_MakeCrossContextRelease, reporter, /*factory*/) { |
| 813 | GrContextFactory testFactory; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 814 | |
| 815 | sk_sp<SkData> data = GetResourceAsData("mandrill_128.png"); |
| 816 | SkASSERT(data.get()); |
| 817 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 818 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
| 819 | GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i); |
| 820 | ContextInfo ctxInfo = testFactory.getContextInfo(ctxType); |
| 821 | GrContext* ctx = ctxInfo.grContext(); |
| 822 | if (!ctx) { |
| 823 | continue; |
| 824 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 825 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 826 | // If we don't have proper support for this feature, the factory will fallback to returning |
| 827 | // codec-backed images. Those will "work", but some of our checks will fail because we |
| 828 | // expect the cross-context images not to work on multiple contexts at once. |
| 829 | if (!ctx->caps()->crossContextTextureSupport()) { |
| 830 | continue; |
| 831 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 832 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 833 | // We test three lifetime patterns for a single context: |
| 834 | // 1) Create image, free image |
| 835 | // 2) Create image, draw, flush, free image |
| 836 | // 3) Create image, draw, free image, flush |
| 837 | // ... and then repeat the last two patterns with drawing on a second* context: |
| 838 | // 4) Create image, draw*, flush*, free image |
| 839 | // 5) Create image, draw*, free iamge, flush* |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 840 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 841 | // Case #1: Create image, free image |
| 842 | { |
| 843 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
| 844 | refImg.reset(nullptr); // force a release of the image |
| 845 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 846 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 847 | SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128); |
| 848 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info); |
| 849 | SkCanvas* canvas = surface->getCanvas(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 850 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 851 | // Case #2: Create image, draw, flush, free image |
| 852 | { |
| 853 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 854 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 855 | canvas->drawImage(refImg, 0, 0); |
| 856 | canvas->flush(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 857 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 858 | refImg.reset(nullptr); // force a release of the image |
| 859 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 860 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 861 | // Case #3: Create image, draw, free image, flush |
| 862 | { |
| 863 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 864 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 865 | canvas->drawImage(refImg, 0, 0); |
| 866 | refImg.reset(nullptr); // force a release of the image |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 867 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 868 | canvas->flush(); |
| 869 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 870 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 871 | // Configure second context |
| 872 | sk_gpu_test::TestContext* testContext = ctxInfo.testContext(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 873 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 874 | ContextInfo otherContextInfo = testFactory.getSharedContextInfo(ctx); |
| 875 | GrContext* otherCtx = otherContextInfo.grContext(); |
| 876 | sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 877 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 878 | // Creating a context in a share group may fail |
| 879 | if (!otherCtx) { |
| 880 | continue; |
| 881 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 882 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 883 | surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info); |
| 884 | canvas = surface->getCanvas(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 885 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 886 | // Case #4: Create image, draw*, flush*, free image |
| 887 | { |
| 888 | testContext->makeCurrent(); |
| 889 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 890 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 891 | otherTestContext->makeCurrent(); |
| 892 | canvas->drawImage(refImg, 0, 0); |
| 893 | canvas->flush(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 894 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 895 | testContext->makeCurrent(); |
| 896 | refImg.reset(nullptr); // force a release of the image |
| 897 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 898 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 899 | // Case #5: Create image, draw*, free image, flush* |
| 900 | { |
| 901 | testContext->makeCurrent(); |
| 902 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 903 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 904 | otherTestContext->makeCurrent(); |
| 905 | canvas->drawImage(refImg, 0, 0); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 906 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 907 | testContext->makeCurrent(); |
| 908 | refImg.reset(nullptr); // force a release of the image |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 909 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 910 | otherTestContext->makeCurrent(); |
| 911 | canvas->flush(); |
Greg Daniel | 3f475d9 | 2017-07-25 16:26:35 -0400 | [diff] [blame] | 912 | |
| 913 | // This readPixels call is needed for Vulkan to make sure the ReleaseProc is called. |
| 914 | // Even though we flushed above, this does not guarantee the command buffer will finish |
| 915 | // which is when we call the ReleaseProc. The readPixels forces a CPU sync so we know |
| 916 | // that the command buffer has finished and we've called the ReleaseProc. |
| 917 | SkBitmap bitmap; |
| 918 | bitmap.allocPixels(info); |
| 919 | canvas->readPixels(bitmap, 0, 0); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 920 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 921 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 922 | // Case #6: Verify that only one context can be using the image at a time |
| 923 | { |
| 924 | testContext->makeCurrent(); |
| 925 | sk_sp<SkImage> refImg(SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 926 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 927 | // Any context should be able to borrow the texture at this point |
| 928 | sk_sp<SkColorSpace> texColorSpace; |
| 929 | sk_sp<GrTextureProxy> proxy = as_IB(refImg)->asTextureProxyRef( |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 930 | ctx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 931 | REPORTER_ASSERT(reporter, proxy); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 932 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 933 | // But once it's borrowed, no other context should be able to borrow |
| 934 | otherTestContext->makeCurrent(); |
| 935 | sk_sp<GrTextureProxy> otherProxy = as_IB(refImg)->asTextureProxyRef( |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 936 | otherCtx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 937 | REPORTER_ASSERT(reporter, !otherProxy); |
| 938 | |
| 939 | // Original context (that's already borrowing) should be okay |
| 940 | testContext->makeCurrent(); |
| 941 | sk_sp<GrTextureProxy> proxySecondRef = as_IB(refImg)->asTextureProxyRef( |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 942 | ctx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 943 | REPORTER_ASSERT(reporter, proxySecondRef); |
| 944 | |
| 945 | // Releae all refs from the original context |
| 946 | proxy.reset(nullptr); |
| 947 | proxySecondRef.reset(nullptr); |
| 948 | |
| 949 | // Now we should be able to borrow the texture from the other context |
| 950 | otherTestContext->makeCurrent(); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 951 | otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(), |
| 952 | nullptr, &texColorSpace, nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 953 | REPORTER_ASSERT(reporter, otherProxy); |
| 954 | |
| 955 | // Release everything |
| 956 | otherProxy.reset(nullptr); |
| 957 | refImg.reset(nullptr); |
| 958 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 962 | static void check_images_same(skiatest::Reporter* reporter, const SkImage* a, const SkImage* b) { |
| 963 | if (a->width() != b->width() || a->height() != b->height()) { |
| 964 | ERRORF(reporter, "Images must have the same size"); |
| 965 | return; |
| 966 | } |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 967 | if (a->alphaType() != b->alphaType()) { |
| 968 | ERRORF(reporter, "Images must have the same alpha type"); |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 969 | return; |
| 970 | } |
| 971 | |
| 972 | SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height()); |
| 973 | SkAutoPixmapStorage apm; |
| 974 | SkAutoPixmapStorage bpm; |
| 975 | |
| 976 | apm.alloc(info); |
| 977 | bpm.alloc(info); |
| 978 | |
| 979 | if (!a->readPixels(apm, 0, 0)) { |
| 980 | ERRORF(reporter, "Could not read image a's pixels"); |
| 981 | return; |
| 982 | } |
| 983 | if (!b->readPixels(bpm, 0, 0)) { |
| 984 | ERRORF(reporter, "Could not read image b's pixels"); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | for (auto y = 0; y < info.height(); ++y) { |
| 989 | for (auto x = 0; x < info.width(); ++x) { |
| 990 | uint32_t pixelA = *apm.addr32(x, y); |
| 991 | uint32_t pixelB = *bpm.addr32(x, y); |
| 992 | if (pixelA != pixelB) { |
| 993 | ERRORF(reporter, "Expected image pixels to be the same. At %d,%d 0x%08x != 0x%08x", |
| 994 | x, y, pixelA, pixelB); |
| 995 | return; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
Mike Klein | c168a3a | 2016-11-14 14:53:13 +0000 | [diff] [blame] | 1001 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredTextureImage, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1002 | GrContext* context = ctxInfo.grContext(); |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 1003 | sk_gpu_test::TestContext* testContext = ctxInfo.testContext(); |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 1004 | sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy(); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1005 | |
| 1006 | GrContextFactory otherFactory; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 1007 | ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1008 | |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 1009 | testContext->makeCurrent(); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1010 | REPORTER_ASSERT(reporter, proxy); |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 1011 | auto createLarge = [context] { |
| 1012 | return create_image_large(context->caps()->maxTextureSize()); |
| 1013 | }; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1014 | struct { |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1015 | std::function<sk_sp<SkImage> ()> fImageFactory; |
| 1016 | std::vector<SkImage::DeferredTextureImageUsageParams> fParams; |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1017 | sk_sp<SkColorSpace> fColorSpace; |
| 1018 | SkColorType fColorType; |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1019 | SkFilterQuality fExpectedQuality; |
| 1020 | int fExpectedScaleFactor; |
| 1021 | bool fExpectation; |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1022 | } testCases[] = { |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1023 | { create_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1024 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, true }, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1025 | { create_codec_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1026 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, true }, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1027 | { create_data_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1028 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, true }, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1029 | { create_picture_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1030 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, false }, |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1031 | { [context] { return create_gpu_image(context); }, |
| 1032 | {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1033 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, false }, |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1034 | // Create a texture image in a another GrContext. |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 1035 | { [testContext, otherContextInfo] { |
| 1036 | otherContextInfo.testContext()->makeCurrent(); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1037 | sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext()); |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 1038 | testContext->makeCurrent(); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1039 | return otherContextImage; |
cblume | 33e0cb5 | 2016-08-30 12:09:23 -0700 | [diff] [blame] | 1040 | }, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1041 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, false }, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1042 | // Create an image that is too large to upload. |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 1043 | { createLarge, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1044 | nullptr, kN32_SkColorType, kNone_SkFilterQuality, 1, false }, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1045 | // Create an image that is too large, but is scaled to an acceptable size. |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 1046 | { createLarge, {{SkMatrix::I(), kMedium_SkFilterQuality, 4}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1047 | nullptr, kN32_SkColorType, kMedium_SkFilterQuality, 16, true}, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1048 | // Create an image with multiple low filter qualities, make sure we round up. |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 1049 | { createLarge, {{SkMatrix::I(), kNone_SkFilterQuality, 4}, |
| 1050 | {SkMatrix::I(), kMedium_SkFilterQuality, 4}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1051 | nullptr, kN32_SkColorType, kMedium_SkFilterQuality, 16, true}, |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1052 | // Create an image with multiple prescale levels, make sure we chose the minimum scale. |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 1053 | { createLarge, {{SkMatrix::I(), kMedium_SkFilterQuality, 5}, |
| 1054 | {SkMatrix::I(), kMedium_SkFilterQuality, 4}}, |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1055 | nullptr, kN32_SkColorType, kMedium_SkFilterQuality, 16, true}, |
| 1056 | // Create a images which are decoded to a 4444 backing. |
| 1057 | { create_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
| 1058 | nullptr, kARGB_4444_SkColorType, kNone_SkFilterQuality, 1, true }, |
| 1059 | { create_codec_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
| 1060 | nullptr, kARGB_4444_SkColorType, kNone_SkFilterQuality, 1, true }, |
| 1061 | { create_data_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
| 1062 | nullptr, kARGB_4444_SkColorType, kNone_SkFilterQuality, 1, true }, |
| 1063 | // Valid SkColorSpace and SkColorType. |
| 1064 | { create_data_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
| 1065 | SkColorSpace::MakeSRGB(), kN32_SkColorType, kNone_SkFilterQuality, 1, true }, |
| 1066 | // Invalid SkColorSpace and SkColorType. |
| 1067 | { create_data_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}}, |
| 1068 | SkColorSpace::MakeSRGB(), kARGB_4444_SkColorType, kNone_SkFilterQuality, 1, false }, |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1069 | }; |
| 1070 | |
| 1071 | |
| 1072 | for (auto testCase : testCases) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1073 | sk_sp<SkImage> image(testCase.fImageFactory()); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1074 | if (!image) { |
| 1075 | ERRORF(reporter, "Failed to create image!"); |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1079 | size_t size = image->getDeferredTextureImageData(*proxy, testCase.fParams.data(), |
| 1080 | static_cast<int>(testCase.fParams.size()), |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1081 | nullptr, testCase.fColorSpace.get(), |
| 1082 | testCase.fColorType); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1083 | static const char *const kFS[] = { "fail", "succeed" }; |
| 1084 | if (SkToBool(size) != testCase.fExpectation) { |
| 1085 | ERRORF(reporter, "This image was expected to %s but did not.", |
| 1086 | kFS[testCase.fExpectation]); |
| 1087 | } |
| 1088 | if (size) { |
| 1089 | void* buffer = sk_malloc_throw(size); |
| 1090 | void* misaligned = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(buffer) + 3); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1091 | if (image->getDeferredTextureImageData(*proxy, testCase.fParams.data(), |
| 1092 | static_cast<int>(testCase.fParams.size()), |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1093 | misaligned, testCase.fColorSpace.get(), |
| 1094 | testCase.fColorType)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1095 | ERRORF(reporter, "Should fail when buffer is misaligned."); |
| 1096 | } |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1097 | if (!image->getDeferredTextureImageData(*proxy, testCase.fParams.data(), |
| 1098 | static_cast<int>(testCase.fParams.size()), |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 1099 | buffer, testCase.fColorSpace.get(), |
| 1100 | testCase.fColorType)) { |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1101 | ERRORF(reporter, "deferred image size succeeded but creation failed."); |
| 1102 | } else { |
| 1103 | for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1104 | sk_sp<SkImage> newImage( |
| 1105 | SkImage::MakeFromDeferredTextureImageData(context, buffer, budgeted)); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1106 | REPORTER_ASSERT(reporter, newImage != nullptr); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1107 | if (newImage) { |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1108 | // Scale the image in software for comparison. |
| 1109 | SkImageInfo scaled_info = SkImageInfo::MakeN32( |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 1110 | image->width() / testCase.fExpectedScaleFactor, |
| 1111 | image->height() / testCase.fExpectedScaleFactor, |
| 1112 | image->alphaType()); |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 1113 | SkAutoPixmapStorage scaled; |
| 1114 | scaled.alloc(scaled_info); |
| 1115 | image->scalePixels(scaled, testCase.fExpectedQuality); |
| 1116 | sk_sp<SkImage> scaledImage = SkImage::MakeRasterCopy(scaled); |
| 1117 | check_images_same(reporter, scaledImage.get(), newImage.get()); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1118 | } |
| 1119 | // The other context should not be able to create images from texture data |
| 1120 | // created by the original context. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 1121 | sk_sp<SkImage> newImage2(SkImage::MakeFromDeferredTextureImageData( |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 1122 | otherContextInfo.grContext(), buffer, budgeted)); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1123 | REPORTER_ASSERT(reporter, !newImage2); |
bsalomon | 0fd3c81 | 2016-05-11 10:38:05 -0700 | [diff] [blame] | 1124 | testContext->makeCurrent(); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | sk_free(buffer); |
| 1128 | } |
Robert Phillips | d9971c0 | 2017-08-02 11:51:43 -0400 | [diff] [blame] | 1129 | |
| 1130 | testContext->makeCurrent(); |
| 1131 | context->flush(); |
bsalomon | 41b952c | 2016-03-11 06:46:33 -0800 | [diff] [blame] | 1132 | } |
| 1133 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 1134 | #endif |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1135 | |
| 1136 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1137 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1138 | static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) { |
| 1139 | SkPictureRecorder recorder; |
| 1140 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 1141 | canvas->clear(SK_ColorCYAN); |
| 1142 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10), |
| 1143 | nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space)); |
| 1144 | }; |
| 1145 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1146 | static inline bool almost_equal(int a, int b) { |
| 1147 | return SkTAbs(a - b) <= 1; |
| 1148 | } |
| 1149 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1150 | DEF_TEST(Image_ColorSpace, r) { |
| 1151 | sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
| 1152 | sk_sp<SkImage> image = GetResourceAsImage("mandrill_512_q075.jpg"); |
| 1153 | REPORTER_ASSERT(r, srgb.get() == image->colorSpace()); |
| 1154 | |
| 1155 | image = GetResourceAsImage("webp-color-profile-lossy.webp"); |
| 1156 | SkColorSpaceTransferFn fn; |
| 1157 | bool success = image->colorSpace()->isNumericalTransferFn(&fn); |
| 1158 | REPORTER_ASSERT(r, success); |
| 1159 | REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.fG)); |
| 1160 | |
| 1161 | sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 1162 | SkColorSpace::kRec2020_Gamut); |
| 1163 | image = create_picture_image(rec2020); |
| 1164 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1165 | |
| 1166 | SkBitmap bitmap; |
| 1167 | SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020); |
| 1168 | bitmap.allocPixels(info); |
| 1169 | image = SkImage::MakeFromBitmap(bitmap); |
| 1170 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1171 | |
| 1172 | sk_sp<SkSurface> surface = SkSurface::MakeRaster( |
| 1173 | SkImageInfo::MakeN32Premul(SkISize::Make(10, 10))); |
| 1174 | image = surface->makeImageSnapshot(); |
| 1175 | REPORTER_ASSERT(r, nullptr == image->colorSpace()); |
| 1176 | |
| 1177 | surface = SkSurface::MakeRaster(info); |
| 1178 | image = surface->makeImageSnapshot(); |
| 1179 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1180 | } |
| 1181 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1182 | DEF_TEST(Image_makeColorSpace, r) { |
| 1183 | sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, |
| 1184 | SkColorSpace::kDCIP3_D65_Gamut); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1185 | SkColorSpaceTransferFn fn; |
| 1186 | fn.fA = 1.f; fn.fB = 0.f; fn.fC = 0.f; fn.fD = 0.f; fn.fE = 0.f; fn.fF = 0.f; fn.fG = 1.8f; |
| 1187 | sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkColorSpace::kAdobeRGB_Gamut); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1188 | |
| 1189 | SkBitmap srgbBitmap; |
| 1190 | srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType)); |
| 1191 | *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020); |
| 1192 | srgbBitmap.setImmutable(); |
| 1193 | sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap); |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 1194 | sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1195 | SkBitmap p3Bitmap; |
| 1196 | bool success = p3Image->asLegacyBitmap(&p3Bitmap, SkImage::kRO_LegacyBitmapMode); |
| 1197 | REPORTER_ASSERT(r, success); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1198 | REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0)))); |
| 1199 | REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0)))); |
| 1200 | REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0)))); |
| 1201 | |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 1202 | sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, |
| 1203 | SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1204 | SkBitmap adobeBitmap; |
| 1205 | success = adobeImage->asLegacyBitmap(&adobeBitmap, SkImage::kRO_LegacyBitmapMode); |
| 1206 | REPORTER_ASSERT(r, success); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1207 | REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0)))); |
| 1208 | REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0)))); |
| 1209 | REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0)))); |
| 1210 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1211 | srgbImage = GetResourceAsImage("1x1.png"); |
Matt Sarett | cb87423 | 2017-04-05 11:41:27 -0400 | [diff] [blame] | 1212 | p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1213 | success = p3Image->asLegacyBitmap(&p3Bitmap, SkImage::kRO_LegacyBitmapMode); |
| 1214 | REPORTER_ASSERT(r, success); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1215 | REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0)))); |
| 1216 | REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0)))); |
| 1217 | REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0)))); |
| 1218 | } |
| 1219 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1220 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1221 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1222 | static void make_all_premul(SkBitmap* bm) { |
| 1223 | bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType)); |
| 1224 | for (int a = 0; a < 256; ++a) { |
| 1225 | for (int r = 0; r < 256; ++r) { |
| 1226 | // make all valid premul combinations |
| 1227 | int c = SkTMin(a, r); |
| 1228 | *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c); |
| 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | static bool equal(const SkBitmap& a, const SkBitmap& b) { |
| 1234 | SkASSERT(a.width() == b.width()); |
| 1235 | SkASSERT(a.height() == b.height()); |
| 1236 | for (int y = 0; y < a.height(); ++y) { |
Mike Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 1237 | for (int x = 0; x < a.width(); ++x) { |
| 1238 | SkPMColor pa = *a.getAddr32(x, y); |
| 1239 | SkPMColor pb = *b.getAddr32(x, y); |
| 1240 | if (pa != pb) { |
| 1241 | return false; |
| 1242 | } |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | return true; |
| 1246 | } |
| 1247 | |
| 1248 | DEF_TEST(image_roundtrip_encode, reporter) { |
| 1249 | SkBitmap bm0; |
| 1250 | make_all_premul(&bm0); |
| 1251 | |
| 1252 | auto img0 = SkImage::MakeFromBitmap(bm0); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 1253 | sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100); |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1254 | auto img1 = SkImage::MakeFromEncoded(data); |
mtklein | 2f3416d | 2016-08-02 16:02:05 -0700 | [diff] [blame] | 1255 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1256 | SkBitmap bm1; |
| 1257 | bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType)); |
| 1258 | img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0); |
mtklein | 2f3416d | 2016-08-02 16:02:05 -0700 | [diff] [blame] | 1259 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1260 | REPORTER_ASSERT(reporter, equal(bm0, bm1)); |
| 1261 | } |
| 1262 | |
| 1263 | DEF_TEST(image_roundtrip_premul, reporter) { |
| 1264 | SkBitmap bm0; |
| 1265 | make_all_premul(&bm0); |
| 1266 | |
| 1267 | SkBitmap bm1; |
| 1268 | bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType)); |
| 1269 | bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0); |
| 1270 | |
| 1271 | SkBitmap bm2; |
| 1272 | bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType)); |
| 1273 | bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0); |
| 1274 | |
| 1275 | REPORTER_ASSERT(reporter, equal(bm0, bm2)); |
| 1276 | } |
Brian Osman | b8a1392 | 2017-04-26 15:28:30 -0400 | [diff] [blame] | 1277 | |
| 1278 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1279 | |
| 1280 | static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) { |
| 1281 | // Verify that all pixels contain the original test color |
| 1282 | for (auto y = 0; y < pmap->height(); ++y) { |
| 1283 | for (auto x = 0; x < pmap->width(); ++x) { |
| 1284 | uint32_t pixel = *pmap->addr32(x, y); |
| 1285 | if (pixel != expected) { |
| 1286 | ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x", |
| 1287 | x, y, pixel, expected); |
| 1288 | return; |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image, |
| 1295 | uint32_t expected) { |
| 1296 | SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2); |
| 1297 | |
| 1298 | // Make sure to test kDisallow first, so we don't just get a cache hit in that case |
| 1299 | for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) { |
| 1300 | SkAutoPixmapStorage scaled; |
| 1301 | scaled.alloc(info); |
| 1302 | if (!image->scalePixels(scaled, kLow_SkFilterQuality, chint)) { |
| 1303 | ERRORF(reporter, "Failed to scale image"); |
| 1304 | continue; |
| 1305 | } |
| 1306 | |
| 1307 | check_scaled_pixels(reporter, &scaled, expected); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | DEF_TEST(ImageScalePixels, reporter) { |
| 1312 | const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0); |
| 1313 | const SkColor red = SK_ColorRED; |
| 1314 | |
| 1315 | // Test raster image |
| 1316 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 1317 | sk_sp<SkSurface> surface = SkSurface::MakeRaster(info); |
| 1318 | surface->getCanvas()->clear(red); |
| 1319 | sk_sp<SkImage> rasterImage = surface->makeImageSnapshot(); |
| 1320 | test_scale_pixels(reporter, rasterImage.get(), pmRed); |
| 1321 | |
| 1322 | // Test encoded image |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 1323 | sk_sp<SkData> data = rasterImage->encodeToData(); |
Brian Osman | b8a1392 | 2017-04-26 15:28:30 -0400 | [diff] [blame] | 1324 | sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data); |
| 1325 | test_scale_pixels(reporter, codecImage.get(), pmRed); |
| 1326 | } |
| 1327 | |
| 1328 | #if SK_SUPPORT_GPU |
| 1329 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) { |
| 1330 | const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0); |
| 1331 | const SkColor red = SK_ColorRED; |
| 1332 | |
| 1333 | SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16); |
| 1334 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, |
| 1335 | info); |
| 1336 | surface->getCanvas()->clear(red); |
| 1337 | sk_sp<SkImage> gpuImage = surface->makeImageSnapshot(); |
| 1338 | test_scale_pixels(reporter, gpuImage.get(), pmRed); |
| 1339 | } |
| 1340 | #endif |