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