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