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