blob: a0deb41842826c7732bf26af69e829cf95515e32 [file] [log] [blame]
reed871872f2015-06-22 12:48:26 -07001/*
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
bsalomon8e74f802016-01-30 10:01:40 -08008#include <functional>
bsalomon0d996862016-03-09 18:44:43 -08009#include <initializer_list>
ericrkb4da01d2016-06-13 11:18:14 -070010#include <vector>
bsalomon8e74f802016-01-30 10:01:40 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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 Salomonf9a1fdf2019-05-09 10:30:12 -040023#include "include/gpu/GrContextThreadSafeProxy.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040024#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkAutoPixmapStorage.h"
26#include "src/core/SkColorSpacePriv.h"
27#include "src/core/SkImagePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrContextPriv.h"
30#include "src/gpu/GrGpu.h"
Chris Dalton5a5fe792020-02-15 11:41:30 -070031#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrResourceCache.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000033#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/SkGr.h"
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040035#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"
brianosmandb2cb102016-07-22 07:22:04 -070041
bsalomonf2f1c172016-04-05 12:59:06 -070042using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070043
Matt Sarettf5759932017-02-07 21:52:07 +000044SkImageInfo read_pixels_info(SkImage* image) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040045 if (image->colorSpace()) {
Matt Sarettf5759932017-02-07 21:52:07 +000046 return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType());
47 }
48
49 return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
50}
51
reed871872f2015-06-22 12:48:26 -070052static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
53 SkImage* b) {
54 const int widthA = subsetA ? subsetA->width() : a->width();
55 const int heightA = subsetA ? subsetA->height() : a->height();
56
57 REPORTER_ASSERT(reporter, widthA == b->width());
58 REPORTER_ASSERT(reporter, heightA == b->height());
reed1cb36462016-03-09 15:21:32 -080059
60 // see https://bug.skia.org/3965
61 //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
reed871872f2015-06-22 12:48:26 -070062
reed871872f2015-06-22 12:48:26 -070063 SkAutoPixmapStorage pmapA, pmapB;
Matt Sarettf5759932017-02-07 21:52:07 +000064 pmapA.alloc(read_pixels_info(a));
65 pmapB.alloc(read_pixels_info(b));
reed871872f2015-06-22 12:48:26 -070066
67 const int srcX = subsetA ? subsetA->x() : 0;
68 const int srcY = subsetA ? subsetA->y() : 0;
69
70 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
71 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
72
Matt Sarettf5759932017-02-07 21:52:07 +000073 const size_t widthBytes = widthA * 4;
reed871872f2015-06-22 12:48:26 -070074 for (int y = 0; y < heightA; ++y) {
75 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
76 }
77}
kkinnunen7b94c142015-11-24 07:39:40 -080078static void draw_image_test_pattern(SkCanvas* canvas) {
reed871872f2015-06-22 12:48:26 -070079 canvas->clear(SK_ColorWHITE);
reed871872f2015-06-22 12:48:26 -070080 SkPaint paint;
81 paint.setColor(SK_ColorBLACK);
kkinnunen7b94c142015-11-24 07:39:40 -080082 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
83}
reed9ce9d672016-03-17 10:51:11 -070084static sk_sp<SkImage> create_image() {
kkinnunen7b94c142015-11-24 07:39:40 -080085 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070086 auto surface(SkSurface::MakeRaster(info));
kkinnunen7b94c142015-11-24 07:39:40 -080087 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -070088 return surface->makeImageSnapshot();
reed871872f2015-06-22 12:48:26 -070089}
bungeman38d909e2016-08-02 14:40:46 -070090static sk_sp<SkData> create_image_data(SkImageInfo* info) {
scroggo9d081722016-04-20 08:27:18 -070091 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
92 const size_t rowBytes = info->minRowBytes();
bungeman38d909e2016-08-02 14:40:46 -070093 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
scroggo9d081722016-04-20 08:27:18 -070094 {
95 SkBitmap bm;
96 bm.installPixels(*info, data->writable_data(), rowBytes);
97 SkCanvas canvas(bm);
98 draw_image_test_pattern(&canvas);
99 }
bungeman38d909e2016-08-02 14:40:46 -0700100 return data;
scroggo9d081722016-04-20 08:27:18 -0700101}
102static sk_sp<SkImage> create_data_image() {
103 SkImageInfo info;
104 sk_sp<SkData> data(create_image_data(&info));
bungeman38d909e2016-08-02 14:40:46 -0700105 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
scroggo9d081722016-04-20 08:27:18 -0700106}
Brian Salomon534cbe52017-01-03 11:35:56 -0500107static sk_sp<SkImage> create_image_large(int maxTextureSize) {
108 const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
bsalomond4907082016-06-13 12:13:03 -0700109 auto surface(SkSurface::MakeRaster(info));
110 surface->getCanvas()->clear(SK_ColorWHITE);
111 SkPaint paint;
112 paint.setColor(SK_ColorBLACK);
113 surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint);
114 return surface->makeImageSnapshot();
115}
reed9ce9d672016-03-17 10:51:11 -0700116static sk_sp<SkImage> create_picture_image() {
bsalomon8e74f802016-01-30 10:01:40 -0800117 SkPictureRecorder recorder;
118 SkCanvas* canvas = recorder.beginRecording(10, 10);
119 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700120 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
Matt Sarette94255d2017-01-09 12:38:59 -0500121 nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500122 SkColorSpace::MakeSRGB());
bsalomon8e74f802016-01-30 10:01:40 -0800123};
kkinnunen7b94c142015-11-24 07:39:40 -0800124// Want to ensure that our Release is called when the owning image is destroyed
125struct RasterDataHolder {
126 RasterDataHolder() : fReleaseCount(0) {}
bungeman38d909e2016-08-02 14:40:46 -0700127 sk_sp<SkData> fData;
kkinnunen7b94c142015-11-24 07:39:40 -0800128 int fReleaseCount;
129 static void Release(const void* pixels, void* context) {
130 RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
131 self->fReleaseCount++;
132 self->fData.reset();
133 }
134};
reed9ce9d672016-03-17 10:51:11 -0700135static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
kkinnunen7b94c142015-11-24 07:39:40 -0800136 SkASSERT(dataHolder);
137 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700138 dataHolder->fData = create_image_data(&info);
139 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
reed9ce9d672016-03-17 10:51:11 -0700140 RasterDataHolder::Release, dataHolder);
kkinnunen7b94c142015-11-24 07:39:40 -0800141}
reed9ce9d672016-03-17 10:51:11 -0700142static sk_sp<SkImage> create_codec_image() {
kkinnunen7b94c142015-11-24 07:39:40 -0800143 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700144 sk_sp<SkData> data(create_image_data(&info));
kkinnunen7b94c142015-11-24 07:39:40 -0800145 SkBitmap bitmap;
146 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
Leon Scroggins III0098ccb2018-09-24 15:24:31 -0400147 auto src = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 100);
bungeman38d909e2016-08-02 14:40:46 -0700148 return SkImage::MakeFromEncoded(std::move(src));
kkinnunen7b94c142015-11-24 07:39:40 -0800149}
Brian Salomonbc074a62020-03-18 10:06:13 -0400150static sk_sp<SkImage> create_gpu_image(GrContext* context,
151 bool withMips = false,
152 SkBudgeted budgeted = SkBudgeted::kYes) {
kkinnunen7b94c142015-11-24 07:39:40 -0800153 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Brian Salomonbc074a62020-03-18 10:06:13 -0400154 auto surface = SkSurface::MakeRenderTarget(context, budgeted, info, 0,
155 kBottomLeft_GrSurfaceOrigin, nullptr, withMips);
kkinnunen7b94c142015-11-24 07:39:40 -0800156 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700157 return surface->makeImageSnapshot();
kkinnunen7b94c142015-11-24 07:39:40 -0800158}
reed871872f2015-06-22 12:48:26 -0700159
kkinnunen7b94c142015-11-24 07:39:40 -0800160static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
reed871872f2015-06-22 12:48:26 -0700161 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
Mike Reed6409f842017-07-11 16:03:13 -0400162 sk_sp<SkData> origEncoded = image->encodeToData();
reed871872f2015-06-22 12:48:26 -0700163 REPORTER_ASSERT(reporter, origEncoded);
164 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
165
reed9ce9d672016-03-17 10:51:11 -0700166 sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
scroggo8e6c7ad2016-09-16 08:20:38 -0700167 if (!decoded) {
168 ERRORF(reporter, "failed to decode image!");
169 return;
170 }
reed871872f2015-06-22 12:48:26 -0700171 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700172 assert_equal(reporter, image, nullptr, decoded.get());
reed871872f2015-06-22 12:48:26 -0700173
174 // Now see if we can instantiate an image from a subset of the surface/origEncoded
mtklein5f939ab2016-03-16 10:28:35 -0700175
reed9ce9d672016-03-17 10:51:11 -0700176 decoded = SkImage::MakeFromEncoded(origEncoded, &ir);
reed871872f2015-06-22 12:48:26 -0700177 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700178 assert_equal(reporter, image, &ir, decoded.get());
reed871872f2015-06-22 12:48:26 -0700179}
180
kkinnunen7b94c142015-11-24 07:39:40 -0800181DEF_TEST(ImageEncode, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700182 test_encode(reporter, create_image().get());
reed871872f2015-06-22 12:48:26 -0700183}
184
bsalomon68d91342016-04-12 09:59:58 -0700185DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400186 test_encode(reporter, create_gpu_image(ctxInfo.directContext()).get());
reed871872f2015-06-22 12:48:26 -0700187}
reed759373a2015-07-03 21:01:10 -0700188
reed2dad7692016-08-01 11:12:58 -0700189DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
190 const struct {
reed1ec04d92016-08-05 12:07:41 -0700191 SkCopyPixelsMode fCPM;
192 bool fExpectSameAsMutable;
193 bool fExpectSameAsImmutable;
reed2dad7692016-08-01 11:12:58 -0700194 } recs[] = {
reed1ec04d92016-08-05 12:07:41 -0700195 { kIfMutable_SkCopyPixelsMode, false, true },
196 { kAlways_SkCopyPixelsMode, false, false },
197 { kNever_SkCopyPixelsMode, true, true },
reed2dad7692016-08-01 11:12:58 -0700198 };
199 for (auto rec : recs) {
200 SkPixmap pm;
201 SkBitmap bm;
202 bm.allocN32Pixels(100, 100);
203
reed1ec04d92016-08-05 12:07:41 -0700204 auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700205 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
206 const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
207 REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable);
reedae296442016-08-05 13:19:01 -0700208 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
reed2dad7692016-08-01 11:12:58 -0700209
210 bm.notifyPixelsChanged(); // force a new generation ID
211
212 bm.setImmutable();
reed1ec04d92016-08-05 12:07:41 -0700213 img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700214 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
215 const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
216 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
reedae296442016-08-05 13:19:01 -0700217 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
reed2dad7692016-08-01 11:12:58 -0700218 }
219}
220
fmalitac3470342015-09-04 11:36:39 -0700221// Test that image encoding failures do not break picture serialization/deserialization.
222DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
reede8f30622016-03-23 18:59:25 -0700223 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
fmalitac3470342015-09-04 11:36:39 -0700224 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700225 sk_sp<SkImage> image(surface->makeImageSnapshot());
fmalitac3470342015-09-04 11:36:39 -0700226 REPORTER_ASSERT(reporter, image);
227
228 SkPictureRecorder recorder;
229 SkCanvas* canvas = recorder.beginRecording(100, 100);
230 canvas->drawImage(image, 0, 0);
reedca2622b2016-03-18 07:25:55 -0700231 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitac3470342015-09-04 11:36:39 -0700232 REPORTER_ASSERT(reporter, picture);
Mike Klein88d90712018-01-27 17:30:04 +0000233 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700234
Mike Reedef038482017-12-16 08:41:28 -0500235 bool was_called = false;
236 SkSerialProcs procs;
237 procs.fImageProc = [](SkImage*, void* called) {
238 *(bool*)called = true;
239 return SkData::MakeEmpty();
240 };
241 procs.fImageCtx = &was_called;
fmalitac3470342015-09-04 11:36:39 -0700242
Mike Reedef038482017-12-16 08:41:28 -0500243 REPORTER_ASSERT(reporter, !was_called);
Mike Reed47fdf6c2017-12-20 14:12:07 -0500244 auto data = picture->serialize(&procs);
Mike Reedef038482017-12-16 08:41:28 -0500245 REPORTER_ASSERT(reporter, was_called);
246 REPORTER_ASSERT(reporter, data && data->size() > 0);
fmalitac3470342015-09-04 11:36:39 -0700247
Mike Reedef038482017-12-16 08:41:28 -0500248 auto deserialized = SkPicture::MakeFromData(data->data(), data->size());
249 REPORTER_ASSERT(reporter, deserialized);
Mike Klein88d90712018-01-27 17:30:04 +0000250 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700251}
252
fmalita8c0144c2015-07-22 05:56:16 -0700253// Test that a draw that only partially covers the drawing surface isn't
254// interpreted as covering the entire drawing surface (i.e., exercise one of the
255// conditions of SkCanvas::wouldOverwriteEntireSurface()).
256DEF_TEST(Image_RetainSnapshot, reporter) {
257 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
258 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
259 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
reede8f30622016-03-23 18:59:25 -0700260 auto surface(SkSurface::MakeRaster(info));
fmalita8c0144c2015-07-22 05:56:16 -0700261 surface->getCanvas()->clear(0xFF00FF00);
262
263 SkPMColor pixels[4];
264 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
265 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
266 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
267
reed9ce9d672016-03-17 10:51:11 -0700268 sk_sp<SkImage> image1(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700269 REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
270 for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
271 REPORTER_ASSERT(reporter, pixels[i] == green);
272 }
273
274 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700275 paint.setBlendMode(SkBlendMode::kSrc);
fmalita8c0144c2015-07-22 05:56:16 -0700276 paint.setColor(SK_ColorRED);
277
278 surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
279
reed9ce9d672016-03-17 10:51:11 -0700280 sk_sp<SkImage> image2(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700281 REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
282 REPORTER_ASSERT(reporter, pixels[0] == green);
283 REPORTER_ASSERT(reporter, pixels[1] == green);
284 REPORTER_ASSERT(reporter, pixels[2] == green);
285 REPORTER_ASSERT(reporter, pixels[3] == red);
286}
reed80c772b2015-07-30 18:58:23 -0700287
288/////////////////////////////////////////////////////////////////////////////////////////////////
reed80c772b2015-07-30 18:58:23 -0700289
290static void make_bitmap_mutable(SkBitmap* bm) {
291 bm->allocN32Pixels(10, 10);
292}
293
294static void make_bitmap_immutable(SkBitmap* bm) {
295 bm->allocN32Pixels(10, 10);
296 bm->setImmutable();
297}
298
299DEF_TEST(image_newfrombitmap, reporter) {
300 const struct {
301 void (*fMakeProc)(SkBitmap*);
302 bool fExpectPeekSuccess;
303 bool fExpectSharedID;
fmalitaddbbdda2015-08-20 08:47:26 -0700304 bool fExpectLazy;
reed80c772b2015-07-30 18:58:23 -0700305 } rec[] = {
fmalitaddbbdda2015-08-20 08:47:26 -0700306 { make_bitmap_mutable, true, false, false },
307 { make_bitmap_immutable, true, true, false },
reed80c772b2015-07-30 18:58:23 -0700308 };
309
310 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
311 SkBitmap bm;
312 rec[i].fMakeProc(&bm);
313
reed9ce9d672016-03-17 10:51:11 -0700314 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
reed80c772b2015-07-30 18:58:23 -0700315 SkPixmap pmap;
316
317 const bool sharedID = (image->uniqueID() == bm.getGenerationID());
318 REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
319
reed80c772b2015-07-30 18:58:23 -0700320 const bool peekSuccess = image->peekPixels(&pmap);
321 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
fmalitaddbbdda2015-08-20 08:47:26 -0700322
323 const bool lazy = image->isLazyGenerated();
324 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
reed80c772b2015-07-30 18:58:23 -0700325 }
326}
reed6f1216a2015-08-04 08:10:13 -0700327
328///////////////////////////////////////////////////////////////////////////////////////////////////
reed6f1216a2015-08-04 08:10:13 -0700329
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500330#include "src/core/SkBitmapCache.h"
reed6f1216a2015-08-04 08:10:13 -0700331
332/*
333 * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image.
334 * We cache it for performance when drawing into a raster surface.
335 *
336 * A cleaner test would know if each drawImage call triggered a read-back from the gpu,
337 * but we don't have that facility (at the moment) so we use a little internal knowledge
338 * of *how* the raster version is cached, and look for that.
339 */
Brian Osmane47e5b62018-10-04 14:19:39 -0400340DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_Gpu2Cpu, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800341 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400342 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400343 const auto desc = SkBitmapCacheDesc::Make(image.get());
reed6f1216a2015-08-04 08:10:13 -0700344
reede8f30622016-03-23 18:59:25 -0700345 auto surface(SkSurface::MakeRaster(info));
reed6f1216a2015-08-04 08:10:13 -0700346
347 // now we can test drawing a gpu-backed image into a cpu-backed surface
348
349 {
350 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400351 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700352 }
353
354 surface->getCanvas()->drawImage(image, 0, 0);
355 {
356 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400357 if (SkBitmapCache::Find(desc, &cachedBitmap)) {
reed6f1216a2015-08-04 08:10:13 -0700358 REPORTER_ASSERT(reporter, cachedBitmap.isImmutable());
359 REPORTER_ASSERT(reporter, cachedBitmap.getPixels());
360 } else {
361 // unexpected, but not really a bug, since the cache is global and this test may be
362 // run w/ other threads competing for its budget.
363 SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n");
364 }
365 }
366
367 image.reset(nullptr);
368 {
369 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400370 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700371 }
372}
bsalomon8e74f802016-01-30 10:01:40 -0800373
Brian Osman041f7df2017-02-07 11:23:28 -0500374DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400375 auto context = contextInfo.directContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500376 sk_gpu_test::TestContext* testContext = contextInfo.testContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500377 GrContextFactory otherFactory;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400378 ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type());
Brian Osman041f7df2017-02-07 11:23:28 -0500379 testContext->makeCurrent();
380
381 std::function<sk_sp<SkImage>()> imageFactories[] = {
Brian Salomonbc074a62020-03-18 10:06:13 -0400382 create_image, create_codec_image, create_data_image,
383 // Create an image from a picture.
384 create_picture_image,
385 // Create a texture image.
386 [context] { return create_gpu_image(context, true, SkBudgeted::kYes); },
387 [context] { return create_gpu_image(context, false, SkBudgeted::kNo); },
388 // Create a texture image in a another GrContext.
389 [otherContextInfo] {
390 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -0400391 auto otherContextImage = create_gpu_image(otherContextInfo.directContext());
392 otherContextInfo.directContext()->flushAndSubmit();
Brian Salomonbc074a62020-03-18 10:06:13 -0400393 return otherContextImage;
394 }};
Brian Salomon7e67dca2020-07-21 09:27:25 -0400395 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400396 for (auto factory : imageFactories) {
397 sk_sp<SkImage> image(factory());
398 if (!image) {
399 ERRORF(reporter, "Error creating image.");
400 continue;
401 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400402 GrTextureProxy* origProxy = nullptr;
403 if (auto sp = as_IB(image)->peekProxy()) {
404 origProxy = sp->asTextureProxy();
405 SkASSERT(origProxy);
406 }
407 for (auto budgeted : {SkBudgeted::kNo, SkBudgeted::kYes}) {
408 auto texImage = image->makeTextureImage(context, mipMapped, budgeted);
409 if (!texImage) {
410 GrContext* imageContext = as_IB(image)->context();
411 // We expect to fail if image comes from a different GrContext
412 if (!image->isTextureBacked() || imageContext == context) {
413 ERRORF(reporter, "makeTextureImage failed.");
414 }
415 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500416 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400417 if (!texImage->isTextureBacked()) {
418 ERRORF(reporter, "makeTextureImage returned non-texture image.");
419 continue;
420 }
421 GrTextureProxy* copyProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
422 SkASSERT(copyProxy);
423 bool shouldBeMipped =
Brian Salomon7e67dca2020-07-21 09:27:25 -0400424 mipMapped == GrMipmapped::kYes && context->priv().caps()->mipMapSupport();
425 if (shouldBeMipped && copyProxy->mipMapped() == GrMipmapped::kNo) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400426 ERRORF(reporter, "makeTextureImage returned non-mipmapped texture.");
427 continue;
428 }
Brian Salomon7e67dca2020-07-21 09:27:25 -0400429 bool origIsMipped = origProxy && origProxy->mipMapped() == GrMipmapped::kYes;
Brian Salomonbc074a62020-03-18 10:06:13 -0400430 if (image->isTextureBacked() && (!shouldBeMipped || origIsMipped)) {
431 if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400432 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400433 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400434 } else {
435 auto* texProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
436 REPORTER_ASSERT(reporter, !texProxy->getUniqueKey().isValid());
437 REPORTER_ASSERT(reporter, texProxy->isBudgeted() == budgeted);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400438 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400439 if (image->width() != texImage->width() || image->height() != texImage->height()) {
440 ERRORF(reporter, "makeTextureImage changed the image size.");
441 }
442 if (image->alphaType() != texImage->alphaType()) {
443 ERRORF(reporter, "makeTextureImage changed image alpha type.");
444 }
Brian Osmand566e2e2019-08-14 13:19:04 -0400445 }
Brian Osman041f7df2017-02-07 11:23:28 -0500446 }
447 }
Greg Daniel0a2464f2020-05-14 15:45:44 -0400448 context->flushAndSubmit();
Brian Osman041f7df2017-02-07 11:23:28 -0500449}
450
bsalomon634b4302016-07-12 18:11:17 -0700451DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400452 auto context = contextInfo.directContext();
bsalomon634b4302016-07-12 18:11:17 -0700453
454 std::function<sk_sp<SkImage>()> imageFactories[] = {
455 create_image,
456 create_codec_image,
457 create_data_image,
458 create_picture_image,
459 [context] { return create_gpu_image(context); },
460 };
461 for (auto factory : imageFactories) {
462 sk_sp<SkImage> image = factory();
463 if (!image->isTextureBacked()) {
464 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Brian Osmand566e2e2019-08-14 13:19:04 -0400465 if (!(image = image->makeTextureImage(context))) {
Brian Osman041f7df2017-02-07 11:23:28 -0500466 continue;
467 }
bsalomon634b4302016-07-12 18:11:17 -0700468 }
469 auto rasterImage = image->makeNonTextureImage();
470 if (!rasterImage) {
471 ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
472 }
473 REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
474 assert_equal(reporter, image.get(), nullptr, rasterImage.get());
475 }
476}
477
Brian Salomonbdecacf2018-02-02 20:32:49 -0500478DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400479 auto context = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400480
481 static constexpr int kSize = 10;
482
Brian Salomonbdecacf2018-02-02 20:32:49 -0500483 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500484 SkColorType colorType = static_cast<SkColorType>(ct);
Robert Phillips9b16f812019-05-17 10:01:21 -0400485 bool can = context->colorTypeSupportedAsImage(colorType);
486
Greg Danielc1ad77c2020-05-06 11:40:03 -0400487 GrBackendTexture backendTex;
488 CreateBackendTexture(context, &backendTex, kSize, kSize, colorType, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400489 GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Robert Phillips9b16f812019-05-17 10:01:21 -0400490
491 auto img = SkImage::MakeFromTexture(context, backendTex, kTopLeft_GrSurfaceOrigin,
492 colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500493 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300494 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500495 colorType);
496
497 img.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400498 context->flushAndSubmit();
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400499 context->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500500 }
501}
502
Brian Salomon9708af82018-02-05 12:57:10 -0500503DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
504 SkBitmap bmp;
505 bmp.allocPixels(
506 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
507 for (int y = 0; y < 256; ++y) {
508 for (int x = 0; x < 256; ++x) {
509 *bmp.getAddr32(x, y) =
510 SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
511 }
512 }
Robert Phillips6d344c32020-07-06 10:56:46 -0400513 auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(ctxInfo.directContext());
Brian Salomon9708af82018-02-05 12:57:10 -0500514 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
515 ERRORF(reporter, "Failed to make unpremul texture image.");
516 return;
517 }
Brian Salomon1d435302019-07-01 13:05:28 -0400518 SkBitmap unpremul;
519 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
520 kUnpremul_SkAlphaType, nullptr));
521 if (!texImage->readPixels(unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(), 0,
522 0)) {
523 ERRORF(reporter, "Unpremul readback failed.");
524 return;
525 }
526 for (int y = 0; y < 256; ++y) {
527 for (int x = 0; x < 256; ++x) {
528 if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
529 ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
530 *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
531 return;
Brian Salomon9708af82018-02-05 12:57:10 -0500532 }
533 }
534 }
535 SkBitmap premul;
536 premul.allocPixels(
537 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
538 if (!texImage->readPixels(premul.info(), premul.getPixels(), premul.rowBytes(), 0, 0)) {
539 ERRORF(reporter, "Unpremul readback failed.");
540 return;
541 }
542 for (int y = 0; y < 256; ++y) {
543 for (int x = 0; x < 256; ++x) {
Brian Salomon1d435302019-07-01 13:05:28 -0400544 uint32_t origColor = *bmp.getAddr32(x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500545 int32_t origA = (origColor >> 24) & 0xff;
Brian Salomon1d435302019-07-01 13:05:28 -0400546 float a = origA / 255.f;
547 int32_t origB = sk_float_round2int(((origColor >> 16) & 0xff) * a);
548 int32_t origG = sk_float_round2int(((origColor >> 8) & 0xff) * a);
549 int32_t origR = sk_float_round2int(((origColor >> 0) & 0xff) * a);
550
Brian Salomon9708af82018-02-05 12:57:10 -0500551 uint32_t read = *premul.getAddr32(x, y);
552 int32_t readA = (read >> 24) & 0xff;
553 int32_t readB = (read >> 16) & 0xff;
554 int32_t readG = (read >> 8) & 0xff;
555 int32_t readR = (read >> 0) & 0xff;
556 // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
557 // bit of tolerance for GPU vs CPU premul math.
558 int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
559 if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
560 SkTAbs(readR - origR) > tol) {
Brian Salomon1d435302019-07-01 13:05:28 -0400561 ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) expected(0x%08x) at %d, %d.",
562 *bmp.getAddr32(x, y), *premul.getAddr32(x, y), origColor, x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500563 return;
564 }
565 }
566 }
567}
568
Brian Salomon8a8dd332018-05-24 14:08:31 -0400569DEF_GPUTEST(AbandonedContextImage, reporter, options) {
570 using Factory = sk_gpu_test::GrContextFactory;
571 for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
572 auto type = static_cast<Factory::ContextType>(ct);
573 std::unique_ptr<Factory> factory(new Factory);
574 if (!factory->get(type)) {
575 continue;
576 }
577
578 sk_sp<SkImage> img;
579 auto gsurf = SkSurface::MakeRenderTarget(
580 factory->get(type), SkBudgeted::kYes,
581 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
582 nullptr);
583 if (!gsurf) {
584 continue;
585 }
586 img = gsurf->makeImageSnapshot();
587 gsurf.reset();
588
589 auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
590
591 REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
592 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->getGrContext()));
593
594 factory->get(type)->abandonContext();
595 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
596 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
597 // This shouldn't crash.
598 rsurf->getCanvas()->drawImage(img, 0, 0);
599
600 // Give up all other refs on GrContext.
601 factory.reset(nullptr);
602 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
603 // This shouldn't crash.
604 rsurf->getCanvas()->drawImage(img, 0, 0);
605 }
606}
607
kkinnunen4e184132015-11-17 22:53:28 -0800608class EmptyGenerator : public SkImageGenerator {
609public:
610 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
611};
612
kkinnunen7b94c142015-11-24 07:39:40 -0800613DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800614 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700615 SkPixmap pmap(info, nullptr, 0);
616 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
617 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
618 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500619 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500620 std::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800621}
622
kkinnunen7b94c142015-11-24 07:39:40 -0800623DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800624 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
625 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400626 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700627 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800628 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700629 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800630 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700631 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800632 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800633}
634
kkinnunen4e184132015-11-17 22:53:28 -0800635static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
636 for (int i = 0; i < count; ++i) {
637 if (pixels[i] != expected) {
638 return false;
639 }
640 }
641 return true;
642}
643
Robert Phillips3500b772017-01-27 10:11:42 -0500644static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700645 if (!image) {
646 ERRORF(reporter, "Failed to create image!");
647 return;
648 }
kkinnunen7b94c142015-11-24 07:39:40 -0800649 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800650 const SkPMColor notExpected = ~expected;
651
652 const int w = 2, h = 2;
653 const size_t rowBytes = w * sizeof(SkPMColor);
654 SkPMColor pixels[w*h];
655
656 SkImageInfo info;
657
658 info = SkImageInfo::MakeUnknown(w, h);
659 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
660
661 // out-of-bounds should fail
662 info = SkImageInfo::MakeN32Premul(w, h);
663 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
664 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
665 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
666 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
667
668 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800669 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800670 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
671 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
672
673 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800674 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800675 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
676 image->width() - w, image->height() - h));
677 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
678
679 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800680 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800681 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
682 REPORTER_ASSERT(reporter, pixels[3] == expected);
683 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
684
685 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800686 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800687 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
688 image->width() - 1, image->height() - 1));
689 REPORTER_ASSERT(reporter, pixels[0] == expected);
690 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
691}
kkinnunen7b94c142015-11-24 07:39:40 -0800692DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700693 sk_sp<SkImage> image(create_image());
Robert Phillips3500b772017-01-27 10:11:42 -0500694 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800695
reed9ce9d672016-03-17 10:51:11 -0700696 image = create_data_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500697 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800698
699 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700700 image = create_rasterproc_image(&dataHolder);
Robert Phillips3500b772017-01-27 10:11:42 -0500701 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800702 image.reset();
703 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
704
reed9ce9d672016-03-17 10:51:11 -0700705 image = create_codec_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500706 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800707}
egdanielab527a52016-06-28 08:07:26 -0700708DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400709 image_test_read_pixels(reporter, create_gpu_image(ctxInfo.directContext()).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800710}
kkinnunen4e184132015-11-17 22:53:28 -0800711
712static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
Cary Clark4f5a79c2018-02-07 15:51:00 -0500713 const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800714 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
715 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700716 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800717
Cary Clark4f5a79c2018-02-07 15:51:00 -0500718 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800719
kkinnunen4e184132015-11-17 22:53:28 -0800720 REPORTER_ASSERT(reporter, bitmap.getPixels());
721
722 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
723 SkPMColor imageColor;
724 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
725 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
726}
727
Cary Clark4f5a79c2018-02-07 15:51:00 -0500728static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700729 if (!image) {
730 ERRORF(reporter, "Failed to create image.");
731 return;
732 }
kkinnunen7b94c142015-11-24 07:39:40 -0800733 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500734 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
735 check_legacy_bitmap(reporter, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800736
737 // Test subsetting to exercise the rowBytes logic.
738 SkBitmap tmp;
739 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
740 image->height() / 2)));
reed9ce9d672016-03-17 10:51:11 -0700741 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
742 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800743
744 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500745 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
746 check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800747}
748DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500749 sk_sp<SkImage> image(create_image());
750 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800751
Cary Clark4f5a79c2018-02-07 15:51:00 -0500752 image = create_data_image();
753 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800754
Cary Clark4f5a79c2018-02-07 15:51:00 -0500755 RasterDataHolder dataHolder;
756 image = create_rasterproc_image(&dataHolder);
757 test_legacy_bitmap(reporter, image.get());
758 image.reset();
759 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800760
Cary Clark4f5a79c2018-02-07 15:51:00 -0500761 image = create_codec_image();
762 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800763}
bsalomon68d91342016-04-12 09:59:58 -0700764DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400765 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
Cary Clark4f5a79c2018-02-07 15:51:00 -0500766 test_legacy_bitmap(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800767}
kkinnunen4e184132015-11-17 22:53:28 -0800768
kkinnunen7b94c142015-11-24 07:39:40 -0800769static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700770 if (!image) {
771 ERRORF(reporter, "Failed to create image!");
772 return;
773 }
reed6ceeebd2016-03-09 14:26:26 -0800774 SkPixmap pm;
775 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800776 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
777 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800778 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800779 REPORTER_ASSERT(reporter, 20 == info.width());
780 REPORTER_ASSERT(reporter, 20 == info.height());
781 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
782 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
783 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800784 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
785 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800786 }
kkinnunen7b94c142015-11-24 07:39:40 -0800787}
788DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700789 sk_sp<SkImage> image(create_image());
790 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800791
reed9ce9d672016-03-17 10:51:11 -0700792 image = create_data_image();
793 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800794
795 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700796 image = create_rasterproc_image(&dataHolder);
797 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800798 image.reset();
799 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
800
reed9ce9d672016-03-17 10:51:11 -0700801 image = create_codec_image();
802 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800803}
egdanielab527a52016-06-28 08:07:26 -0700804DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400805 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
reed9ce9d672016-03-17 10:51:11 -0700806 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800807}
kkinnunen4e184132015-11-17 22:53:28 -0800808
kkinnunen7b94c142015-11-24 07:39:40 -0800809struct TextureReleaseChecker {
810 TextureReleaseChecker() : fReleaseCount(0) {}
811 int fReleaseCount;
812 static void Release(void* self) {
813 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800814 }
815};
Brian Osman13dddce2017-05-09 13:19:50 -0400816
brianosmandb2cb102016-07-22 07:22:04 -0700817DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
818 const int kWidth = 10;
819 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000820
Robert Phillips6d344c32020-07-06 10:56:46 -0400821 auto ctx = ctxInfo.directContext();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000822
Robert Phillipsee5fd132019-05-07 13:29:22 -0400823 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
824 kPremul_SkAlphaType);
825 GrBackendTexture backendTex;
Brian Salomon7e67dca2020-07-21 09:27:25 -0400826 if (!CreateBackendTexture(ctx, &backendTex, ii, SkColors::kRed, GrMipmapped::kNo,
Brian Salomon28a8f282019-10-24 20:07:39 -0400827 GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400828 ERRORF(reporter, "couldn't create backend texture\n");
829 }
brianosmandb2cb102016-07-22 07:22:04 -0700830
kkinnunen7b94c142015-11-24 07:39:40 -0800831 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000832 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
reed9ce9d672016-03-17 10:51:11 -0700833 sk_sp<SkImage> refImg(
Greg Danielf5d87582017-12-18 14:48:15 -0500834 SkImage::MakeFromTexture(ctx, backendTex, texOrigin, kRGBA_8888_SkColorType,
835 kPremul_SkAlphaType, nullptr,
reed9ce9d672016-03-17 10:51:11 -0700836 TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800837
Robert Phillips3390e152017-01-31 17:53:34 -0500838 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400839 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
840 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) {
841 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500842 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400843 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000844 if (readBackOrigin != texOrigin) {
845 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500846 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000847 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500848
kkinnunen4e184132015-11-17 22:53:28 -0800849 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800850 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800851 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800852 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
brianosmandb2cb102016-07-22 07:22:04 -0700853
Brian Salomon28a8f282019-10-24 20:07:39 -0400854 DeleteBackendTexture(ctx, backendTex);
kkinnunen4e184132015-11-17 22:53:28 -0800855}
bsalomon0d996862016-03-09 18:44:43 -0800856
Brian Salomondcfca432017-11-15 15:48:03 -0500857static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400858 const char* testName,
Brian Osman63bc48d2017-11-07 10:37:00 -0500859 std::function<sk_sp<SkImage>(GrContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400860 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500861 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400862 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
863 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillips00f78de2020-07-01 16:09:43 -0400864 auto ctx = ctxInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400865 if (!ctx) {
866 continue;
867 }
Brian Osman13dddce2017-05-09 13:19:50 -0400868
Brian Osmanceb7a422017-06-21 15:10:33 -0400869 // If we don't have proper support for this feature, the factory will fallback to returning
870 // codec-backed images. Those will "work", but some of our checks will fail because we
871 // expect the cross-context images not to work on multiple contexts at once.
Robert Phillips9da87e02019-02-04 13:26:26 -0500872 if (!ctx->priv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400873 continue;
874 }
Brian Osman13dddce2017-05-09 13:19:50 -0400875
Brian Osmanceb7a422017-06-21 15:10:33 -0400876 // We test three lifetime patterns for a single context:
877 // 1) Create image, free image
878 // 2) Create image, draw, flush, free image
879 // 3) Create image, draw, free image, flush
880 // ... and then repeat the last two patterns with drawing on a second* context:
881 // 4) Create image, draw*, flush*, free image
882 // 5) Create image, draw*, free iamge, flush*
Brian Osman13dddce2017-05-09 13:19:50 -0400883
Brian Osmanceb7a422017-06-21 15:10:33 -0400884 // Case #1: Create image, free image
885 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500886 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osmanceb7a422017-06-21 15:10:33 -0400887 refImg.reset(nullptr); // force a release of the image
888 }
Brian Osman13dddce2017-05-09 13:19:50 -0400889
Brian Osmanceb7a422017-06-21 15:10:33 -0400890 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
891 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400892 if (!surface) {
893 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
894 continue;
895 }
896
Brian Osmanceb7a422017-06-21 15:10:33 -0400897 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400898
Brian Osmanceb7a422017-06-21 15:10:33 -0400899 // Case #2: Create image, draw, flush, free image
900 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500901 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400902
Brian Osmanceb7a422017-06-21 15:10:33 -0400903 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400904 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400905
Brian Osmanceb7a422017-06-21 15:10:33 -0400906 refImg.reset(nullptr); // force a release of the image
907 }
Brian Osman13dddce2017-05-09 13:19:50 -0400908
Brian Osmanceb7a422017-06-21 15:10:33 -0400909 // Case #3: Create image, draw, free image, flush
910 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500911 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400912
Brian Osmanceb7a422017-06-21 15:10:33 -0400913 canvas->drawImage(refImg, 0, 0);
914 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400915
Greg Daniel0a2464f2020-05-14 15:45:44 -0400916 surface->flushAndSubmit();
Brian Osmanceb7a422017-06-21 15:10:33 -0400917 }
Brian Osman13dddce2017-05-09 13:19:50 -0400918
Brian Osmanceb7a422017-06-21 15:10:33 -0400919 // Configure second context
920 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400921
Brian Osmanceb7a422017-06-21 15:10:33 -0400922 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(ctx);
Robert Phillips6d344c32020-07-06 10:56:46 -0400923 auto otherCtx = otherContextInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400924 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400925
Brian Osmanceb7a422017-06-21 15:10:33 -0400926 // Creating a context in a share group may fail
927 if (!otherCtx) {
928 continue;
929 }
Brian Osman13dddce2017-05-09 13:19:50 -0400930
Brian Osmanceb7a422017-06-21 15:10:33 -0400931 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
932 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400933
Brian Osmanceb7a422017-06-21 15:10:33 -0400934 // Case #4: Create image, draw*, flush*, free image
935 {
936 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500937 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400938
Brian Osmanceb7a422017-06-21 15:10:33 -0400939 otherTestContext->makeCurrent();
940 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400941 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400942
Brian Osmanceb7a422017-06-21 15:10:33 -0400943 testContext->makeCurrent();
944 refImg.reset(nullptr); // force a release of the image
945 }
Brian Osman13dddce2017-05-09 13:19:50 -0400946
Brian Osmanceb7a422017-06-21 15:10:33 -0400947 // Case #5: Create image, draw*, free image, flush*
948 {
949 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500950 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400951
Brian Osmanceb7a422017-06-21 15:10:33 -0400952 otherTestContext->makeCurrent();
953 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400954
Brian Osmanceb7a422017-06-21 15:10:33 -0400955 testContext->makeCurrent();
956 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400957
Brian Osmanceb7a422017-06-21 15:10:33 -0400958 otherTestContext->makeCurrent();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400959 surface->flushAndSubmit();
Greg Daniel3f475d92017-07-25 16:26:35 -0400960
Greg Daniel26b50a42018-03-08 09:49:58 -0500961 // This is specifically here for vulkan to guarantee the command buffer will finish
962 // which is when we call the ReleaseProc.
Robert Phillips9da87e02019-02-04 13:26:26 -0500963 otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400964 }
Brian Osman13dddce2017-05-09 13:19:50 -0400965
Brian Osmanceb7a422017-06-21 15:10:33 -0400966 // Case #6: Verify that only one context can be using the image at a time
967 {
Chris Dalton5a5fe792020-02-15 11:41:30 -0700968 // Suppress warnings about trying to use a texture on two GrContexts.
969 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(otherCtx);
970
Brian Osmanceb7a422017-06-21 15:10:33 -0400971 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500972 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400973
Brian Osmanceb7a422017-06-21 15:10:33 -0400974 // Any context should be able to borrow the texture at this point
Brian Salomon7e67dca2020-07-21 09:27:25 -0400975 GrSurfaceProxyView view = as_IB(refImg)->refView(ctx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500976 REPORTER_ASSERT(reporter, view);
Brian Osman13dddce2017-05-09 13:19:50 -0400977
Brian Osmanceb7a422017-06-21 15:10:33 -0400978 // But once it's borrowed, no other context should be able to borrow
979 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400980 GrSurfaceProxyView otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500981 REPORTER_ASSERT(reporter, !otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -0400982
983 // Original context (that's already borrowing) should be okay
984 testContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400985 GrSurfaceProxyView viewSecondRef = as_IB(refImg)->refView(ctx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500986 REPORTER_ASSERT(reporter, viewSecondRef);
Brian Osmanceb7a422017-06-21 15:10:33 -0400987
Greg Danielabba9982018-02-01 10:07:57 -0500988 // Release first ref from the original context
Greg Danielfebdedf2020-02-05 17:06:27 -0500989 view.reset();
Greg Danielabba9982018-02-01 10:07:57 -0500990
991 // We released one proxy but not the other from the current borrowing context. Make sure
992 // a new context is still not able to borrow the texture.
993 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400994 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500995 REPORTER_ASSERT(reporter, !otherView);
Greg Danielabba9982018-02-01 10:07:57 -0500996
997 // Release second ref from the original context
998 testContext->makeCurrent();
Greg Danielfebdedf2020-02-05 17:06:27 -0500999 viewSecondRef.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001000
Greg Daniel48661b82018-01-22 16:11:35 -05001001 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001002 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001003 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001004 REPORTER_ASSERT(reporter, otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -04001005
1006 // Release everything
Greg Danielfebdedf2020-02-05 17:06:27 -05001007 otherView.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001008 refImg.reset(nullptr);
1009 }
Brian Osman13dddce2017-05-09 13:19:50 -04001010 }
1011}
1012
Brian Salomondcfca432017-11-15 15:48:03 -05001013DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001014 SkBitmap bitmap;
1015 SkPixmap pixmap;
Hal Canarybaa2a282018-11-26 15:34:12 -05001016 if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
1017 ERRORF(reporter, "missing resource");
1018 return;
1019 }
Hal Canaryf7d3f612018-03-22 15:17:42 -04001020 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
1021 [&pixmap](GrContext* ctx) {
Brian Osman4c3fd342019-08-15 12:13:53 -04001022 return SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false);
Brian Osman63bc48d2017-11-07 10:37:00 -05001023 });
1024}
1025
Brian Osman052ef692018-03-27 09:56:31 -04001026DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
1027
1028 for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
1029 SkAutoPixmapStorage pixmap;
1030 pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
1031
1032 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
1033 GrContextFactory testFactory(options);
1034 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
1035 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillips6d344c32020-07-06 10:56:46 -04001036 auto ctx = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -05001037 if (!ctx || !ctx->priv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001038 continue;
1039 }
1040
Brian Osman4c3fd342019-08-15 12:13:53 -04001041 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false);
Brian Osman052ef692018-03-27 09:56:31 -04001042 REPORTER_ASSERT(reporter, image);
1043
Brian Salomon7e67dca2020-07-21 09:27:25 -04001044 GrSurfaceProxyView view = as_IB(image)->refView(ctx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001045 REPORTER_ASSERT(reporter, view);
Brian Osman052ef692018-03-27 09:56:31 -04001046
1047 bool expectAlpha = kAlpha_8_SkColorType == ct;
Greg Daniela4828a12019-10-11 13:51:02 -04001048 GrColorType grCT = SkColorTypeToGrColorType(image->colorType());
1049 REPORTER_ASSERT(reporter, expectAlpha == GrColorTypeIsAlphaOnly(grCT));
Brian Osman052ef692018-03-27 09:56:31 -04001050 }
1051 }
1052}
1053
Eric Karl914a36b2017-10-12 12:44:50 -07001054DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001055 auto context = ctxInfo.directContext();
Eric Karl914a36b2017-10-12 12:44:50 -07001056 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
1057 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
1058
1059 GrContextFactory otherFactory;
1060 ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
1061
1062 testContext->makeCurrent();
1063 REPORTER_ASSERT(reporter, proxy);
1064 auto createLarge = [context] {
Robert Phillips9da87e02019-02-04 13:26:26 -05001065 return create_image_large(context->priv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001066 };
1067 struct {
1068 std::function<sk_sp<SkImage> ()> fImageFactory;
1069 bool fExpectation;
1070 bool fCanTakeDirectly;
1071 } testCases[] = {
1072 { create_image, true, false },
1073 { create_codec_image, true, false },
1074 { create_data_image, true, false },
1075 { create_picture_image, true, false },
1076 { [context] { return create_gpu_image(context); }, true, true },
1077 // Create a texture image in a another GrContext.
Brian Salomon55ad7742017-11-17 09:25:23 -05001078 { [otherContextInfo] {
1079 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -04001080 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.directContext());
1081 otherContextInfo.directContext()->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001082 return otherContextImage;
1083 }, false, false },
1084 // Create an image that is too large to be texture backed.
1085 { createLarge, false, false }
1086 };
1087
1088 for (auto testCase : testCases) {
1089 sk_sp<SkImage> image(testCase.fImageFactory());
1090 if (!image) {
1091 ERRORF(reporter, "Failed to create image!");
1092 continue;
1093 }
1094
Robert Phillipsba375a82018-04-11 10:08:06 -04001095 GrBackendTexture origBackend = image->getBackendTexture(true);
1096 if (testCase.fCanTakeDirectly) {
1097 SkASSERT(origBackend.isValid());
1098 }
1099
1100 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001101 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001102 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1103 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001104 if (result != testCase.fExpectation) {
1105 static const char *const kFS[] = { "fail", "succeed" };
1106 ERRORF(reporter, "This image was expected to %s but did not.",
1107 kFS[testCase.fExpectation]);
1108 }
1109
Robert Phillipsba375a82018-04-11 10:08:06 -04001110 if (result) {
1111 SkASSERT(newBackend.isValid());
1112 }
1113
1114 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001115 if (testCase.fCanTakeDirectly != tookDirectly) {
1116 static const char *const kExpectedState[] = { "not expected", "expected" };
1117 ERRORF(reporter, "This backend texture was %s to be taken directly.",
1118 kExpectedState[testCase.fCanTakeDirectly]);
1119 }
1120
Greg Daniel0a2464f2020-05-14 15:45:44 -04001121 context->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001122 }
1123}
reedeb560282016-07-26 19:42:04 -07001124
1125///////////////////////////////////////////////////////////////////////////////////////////////////
1126
Matt Sarett0bb6f382017-03-06 10:28:24 -05001127static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1128 SkPictureRecorder recorder;
1129 SkCanvas* canvas = recorder.beginRecording(10, 10);
1130 canvas->clear(SK_ColorCYAN);
1131 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1132 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1133};
1134
1135DEF_TEST(Image_ColorSpace, r) {
1136 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001137 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001138 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1139
Hal Canaryc465d132017-12-08 10:21:31 -05001140 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Brian Osman82ebe042019-01-04 17:03:00 -05001141 skcms_TransferFunction fn;
Matt Sarett0bb6f382017-03-06 10:28:24 -05001142 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1143 REPORTER_ASSERT(r, success);
Brian Osman82ebe042019-01-04 17:03:00 -05001144 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
Matt Sarett0bb6f382017-03-06 10:28:24 -05001145
Brian Osman82ebe042019-01-04 17:03:00 -05001146 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
1147 SkNamedGamut::kRec2020);
Matt Sarett0bb6f382017-03-06 10:28:24 -05001148 image = create_picture_image(rec2020);
1149 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1150
1151 SkBitmap bitmap;
1152 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1153 bitmap.allocPixels(info);
1154 image = SkImage::MakeFromBitmap(bitmap);
1155 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1156
1157 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1158 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1159 image = surface->makeImageSnapshot();
1160 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1161
1162 surface = SkSurface::MakeRaster(info);
1163 image = surface->makeImageSnapshot();
1164 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1165}
1166
Matt Sarett6de13102017-03-14 14:10:48 -04001167DEF_TEST(Image_makeColorSpace, r) {
Mike Kleinb147ace2020-01-16 11:11:06 -06001168 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Brian Osman82ebe042019-01-04 17:03:00 -05001169 skcms_TransferFunction fn;
1170 fn.a = 1.f; fn.b = 0.f; fn.c = 0.f; fn.d = 0.f; fn.e = 0.f; fn.f = 0.f; fn.g = 1.8f;
1171 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB);
Matt Sarett6de13102017-03-14 14:10:48 -04001172
1173 SkBitmap srgbBitmap;
1174 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1175 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1176 srgbBitmap.setImmutable();
1177 sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
Adlai Holler3a220172020-07-15 10:37:50 -04001178 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, nullptr);
Matt Sarett6de13102017-03-14 14:10:48 -04001179 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001180 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001181
1182 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1183
Matt Sarett6de13102017-03-14 14:10:48 -04001184 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001185 REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1186 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1187 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1188
Adlai Holler3a220172020-07-15 10:37:50 -04001189 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, nullptr);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001190 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001191 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001192 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001193 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1194 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1195 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1196
Hal Canaryc465d132017-12-08 10:21:31 -05001197 srgbImage = GetResourceAsImage("images/1x1.png");
Adlai Holler3a220172020-07-15 10:37:50 -04001198 p3Image = srgbImage->makeColorSpace(p3, nullptr);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001199 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001200 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001201 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1202 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1203 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1204}
1205
Matt Sarett0bb6f382017-03-06 10:28:24 -05001206///////////////////////////////////////////////////////////////////////////////////////////////////
1207
reedeb560282016-07-26 19:42:04 -07001208static void make_all_premul(SkBitmap* bm) {
1209 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1210 for (int a = 0; a < 256; ++a) {
1211 for (int r = 0; r < 256; ++r) {
1212 // make all valid premul combinations
Brian Osman788b9162020-02-07 10:36:46 -05001213 int c = std::min(a, r);
reedeb560282016-07-26 19:42:04 -07001214 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1215 }
1216 }
1217}
1218
1219static bool equal(const SkBitmap& a, const SkBitmap& b) {
1220 SkASSERT(a.width() == b.width());
1221 SkASSERT(a.height() == b.height());
1222 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001223 for (int x = 0; x < a.width(); ++x) {
1224 SkPMColor pa = *a.getAddr32(x, y);
1225 SkPMColor pb = *b.getAddr32(x, y);
1226 if (pa != pb) {
1227 return false;
1228 }
reedeb560282016-07-26 19:42:04 -07001229 }
1230 }
1231 return true;
1232}
1233
1234DEF_TEST(image_roundtrip_encode, reporter) {
1235 SkBitmap bm0;
1236 make_all_premul(&bm0);
1237
1238 auto img0 = SkImage::MakeFromBitmap(bm0);
Mike Reed6409f842017-07-11 16:03:13 -04001239 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001240 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001241
reedeb560282016-07-26 19:42:04 -07001242 SkBitmap bm1;
1243 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1244 img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001245
reedeb560282016-07-26 19:42:04 -07001246 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1247}
1248
1249DEF_TEST(image_roundtrip_premul, reporter) {
1250 SkBitmap bm0;
1251 make_all_premul(&bm0);
1252
1253 SkBitmap bm1;
1254 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1255 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1256
1257 SkBitmap bm2;
1258 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1259 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1260
1261 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1262}
Brian Osmanb8a13922017-04-26 15:28:30 -04001263
1264///////////////////////////////////////////////////////////////////////////////////////////////////
1265
1266static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
1267 // Verify that all pixels contain the original test color
1268 for (auto y = 0; y < pmap->height(); ++y) {
1269 for (auto x = 0; x < pmap->width(); ++x) {
1270 uint32_t pixel = *pmap->addr32(x, y);
1271 if (pixel != expected) {
1272 ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
1273 x, y, pixel, expected);
1274 return;
1275 }
1276 }
1277 }
1278}
1279
1280static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
1281 uint32_t expected) {
1282 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
1283
1284 // Make sure to test kDisallow first, so we don't just get a cache hit in that case
1285 for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
1286 SkAutoPixmapStorage scaled;
1287 scaled.alloc(info);
1288 if (!image->scalePixels(scaled, kLow_SkFilterQuality, chint)) {
1289 ERRORF(reporter, "Failed to scale image");
1290 continue;
1291 }
1292
1293 check_scaled_pixels(reporter, &scaled, expected);
1294 }
1295}
1296
1297DEF_TEST(ImageScalePixels, reporter) {
1298 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1299 const SkColor red = SK_ColorRED;
1300
1301 // Test raster image
1302 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1303 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
1304 surface->getCanvas()->clear(red);
1305 sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
1306 test_scale_pixels(reporter, rasterImage.get(), pmRed);
1307
1308 // Test encoded image
Mike Reed6409f842017-07-11 16:03:13 -04001309 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001310 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1311 test_scale_pixels(reporter, codecImage.get(), pmRed);
1312}
1313
Brian Osmanb8a13922017-04-26 15:28:30 -04001314DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
1315 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1316 const SkColor red = SK_ColorRED;
1317
1318 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
Robert Phillips6d344c32020-07-06 10:56:46 -04001319 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.directContext(),
1320 SkBudgeted::kNo, info);
Brian Osmanb8a13922017-04-26 15:28:30 -04001321 surface->getCanvas()->clear(red);
1322 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1323 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1324}
Mike Reedc4e31092018-01-30 11:15:27 -05001325
1326static sk_sp<SkImage> any_image_will_do() {
1327 return GetResourceAsImage("images/mandrill_32.png");
1328}
1329
1330DEF_TEST(Image_nonfinite_dst, reporter) {
1331 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
1332 auto img = any_image_will_do();
1333 SkPaint paint;
1334
1335 for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
1336 for (int bits = 1; bits <= 15; ++bits) {
1337 SkRect dst = { 0, 0, 10, 10 };
1338 if (bits & 1) dst.fLeft = bad;
1339 if (bits & 2) dst.fTop = bad;
1340 if (bits & 4) dst.fRight = bad;
1341 if (bits & 8) dst.fBottom = bad;
1342
1343 surf->getCanvas()->drawImageRect(img, dst, &paint);
1344
1345 // we should draw nothing
Mike Kleinea3f0142019-03-20 11:12:10 -05001346 ToolUtils::PixelIter iter(surf.get());
Mike Reedc4e31092018-01-30 11:15:27 -05001347 while (void* addr = iter.next()) {
1348 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1349 }
1350 }
1351 }
1352}
1353
Brian Salomonad8efda2019-05-10 09:22:49 -04001354static sk_sp<SkImage> make_yuva_image(GrContext* c) {
1355 SkAutoPixmapStorage pm;
1356 pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
1357 const SkPixmap pmaps[] = {pm, pm, pm, pm};
1358 SkYUVAIndex indices[] = {{0, SkColorChannel::kA},
1359 {1, SkColorChannel::kA},
1360 {2, SkColorChannel::kA},
1361 {3, SkColorChannel::kA}};
1362
1363 return SkImage::MakeFromYUVAPixmaps(c, kJPEG_SkYUVColorSpace, pmaps, indices,
1364 SkISize::Make(1, 1), kTopLeft_GrSurfaceOrigin, false);
1365}
1366
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001367DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001368 auto direct = ctxInfo.directContext();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001369 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -04001370 auto s = SkSurface::MakeRenderTarget(direct, SkBudgeted::kYes, ii, 1, nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001371
1372 s->getCanvas()->clear(SK_ColorRED);
1373 auto i0 = s->makeImageSnapshot();
1374 s->getCanvas()->clear(SK_ColorBLUE);
1375 auto i1 = s->makeImageSnapshot();
1376 s->getCanvas()->clear(SK_ColorGREEN);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001377 // Make a YUVA image.
Robert Phillips6d344c32020-07-06 10:56:46 -04001378 auto i2 = make_yuva_image(direct);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001379
1380 // Flush all the setup work we did above and then make little lambda that reports the flush
1381 // count delta since the last time it was called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001382 direct->flushAndSubmit();
1383 auto numSubmits = [direct,
1384 submitCnt = direct->priv().getGpu()->stats()->numSubmitToGpus()]() mutable {
1385 int curr = direct->priv().getGpu()->stats()->numSubmitToGpus();
Greg Daniel04283f32020-05-20 13:16:00 -04001386 int n = curr - submitCnt;
1387 submitCnt = curr;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001388 return n;
1389 };
1390
Greg Daniel04283f32020-05-20 13:16:00 -04001391 // Images aren't used therefore flush is ignored, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001392 i0->flushAndSubmit(direct);
1393 i1->flushAndSubmit(direct);
1394 i2->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001395 REPORTER_ASSERT(reporter, numSubmits() == 3);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001396
1397 // Syncing forces the flush to happen even if the images aren't used.
Robert Phillips6d344c32020-07-06 10:56:46 -04001398 i0->flush(direct);
1399 direct->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001400 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillips6d344c32020-07-06 10:56:46 -04001401 i1->flush(direct);
1402 direct->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001403 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillips6d344c32020-07-06 10:56:46 -04001404 i2->flush(direct);
1405 direct->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001406 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001407
1408 // Use image 1
1409 s->getCanvas()->drawImage(i1, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001410 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001411 i0->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001412 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001413 // Flushing image 1 should flush.
Robert Phillips6d344c32020-07-06 10:56:46 -04001414 i1->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001415 REPORTER_ASSERT(reporter, numSubmits() == 1);
1416 // Flushing image 2 should do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001417 i2->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001418 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001419
1420 // Use image 2
1421 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001422 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001423 i0->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001424 REPORTER_ASSERT(reporter, numSubmits() == 1);
1425 // Flushing image 1 do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001426 i1->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001427 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001428 // Flushing image 2 should flush.
Robert Phillips6d344c32020-07-06 10:56:46 -04001429 i2->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001430 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001431 // Since we just did a simple image draw it should not have been flattened.
1432 REPORTER_ASSERT(reporter,
1433 !static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
Brian Salomonad8efda2019-05-10 09:22:49 -04001434 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1435
1436 // Flatten it and repeat.
Robert Phillips6d344c32020-07-06 10:56:46 -04001437 as_IB(i2.get())->view(direct);
Brian Salomonad8efda2019-05-10 09:22:49 -04001438 REPORTER_ASSERT(reporter,
1439 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1440 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1441 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001442 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001443 i0->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001444 REPORTER_ASSERT(reporter, numSubmits() == 1);
1445 // Flushing image 1 do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001446 i1->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001447 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001448 // Flushing image 2 should flush.
Robert Phillips6d344c32020-07-06 10:56:46 -04001449 i2->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001450 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001451
1452 // Test case where flatten happens before the first flush.
Robert Phillips6d344c32020-07-06 10:56:46 -04001453 i2 = make_yuva_image(direct);
Brian Salomonad8efda2019-05-10 09:22:49 -04001454 // On some systems where preferVRAMUseOverFlushes is false (ANGLE on Windows) the above may
1455 // actually flush in order to make textures for the YUV planes. TODO: Remove this when we
1456 // make the YUVA planes from backend textures rather than pixmaps that GrContext must upload.
Greg Daniel04283f32020-05-20 13:16:00 -04001457 // Calling numSubmits rebases the flush count from here.
1458 numSubmits();
Robert Phillips6d344c32020-07-06 10:56:46 -04001459 as_IB(i2.get())->view(direct);
Brian Salomonad8efda2019-05-10 09:22:49 -04001460 REPORTER_ASSERT(reporter,
1461 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1462 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1463 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001464 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001465 i0->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001466 REPORTER_ASSERT(reporter, numSubmits() == 1);
1467 // Flushing image 1 do nothing, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001468 i1->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001469 REPORTER_ASSERT(reporter, numSubmits() == 1);
1470 // Flushing image 2 should flush, but submit is still called.
Robert Phillips6d344c32020-07-06 10:56:46 -04001471 i2->flushAndSubmit(direct);
Greg Daniel04283f32020-05-20 13:16:00 -04001472 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001473}