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