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