blob: b7dd5b99d60cc298d8b1f73df2f3f06f041a413c [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
Adlai Hollerbcfc5542020-08-27 12:44:07 -040052// image `b` is assumed to be raster
53static void assert_equal(skiatest::Reporter* reporter, GrDirectContext* dContextA, SkImage* a,
54 const SkIRect* subsetA, SkImage* b) {
reed871872f2015-06-22 12:48:26 -070055 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());
reed1cb36462016-03-09 15:21:32 -080060
61 // see https://bug.skia.org/3965
62 //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
reed871872f2015-06-22 12:48:26 -070063
reed871872f2015-06-22 12:48:26 -070064 SkAutoPixmapStorage pmapA, pmapB;
Matt Sarettf5759932017-02-07 21:52:07 +000065 pmapA.alloc(read_pixels_info(a));
66 pmapB.alloc(read_pixels_info(b));
reed871872f2015-06-22 12:48:26 -070067
68 const int srcX = subsetA ? subsetA->x() : 0;
69 const int srcY = subsetA ? subsetA->y() : 0;
70
Adlai Hollerbcfc5542020-08-27 12:44:07 -040071 REPORTER_ASSERT(reporter, a->readPixels(dContextA, pmapA, srcX, srcY));
72 REPORTER_ASSERT(reporter, b->readPixels(nullptr, pmapB, 0, 0));
reed871872f2015-06-22 12:48:26 -070073
Matt Sarettf5759932017-02-07 21:52:07 +000074 const size_t widthBytes = widthA * 4;
reed871872f2015-06-22 12:48:26 -070075 for (int y = 0; y < heightA; ++y) {
76 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
77 }
78}
kkinnunen7b94c142015-11-24 07:39:40 -080079static void draw_image_test_pattern(SkCanvas* canvas) {
reed871872f2015-06-22 12:48:26 -070080 canvas->clear(SK_ColorWHITE);
reed871872f2015-06-22 12:48:26 -070081 SkPaint paint;
82 paint.setColor(SK_ColorBLACK);
kkinnunen7b94c142015-11-24 07:39:40 -080083 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
84}
reed9ce9d672016-03-17 10:51:11 -070085static sk_sp<SkImage> create_image() {
kkinnunen7b94c142015-11-24 07:39:40 -080086 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070087 auto surface(SkSurface::MakeRaster(info));
kkinnunen7b94c142015-11-24 07:39:40 -080088 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -070089 return surface->makeImageSnapshot();
reed871872f2015-06-22 12:48:26 -070090}
bungeman38d909e2016-08-02 14:40:46 -070091static sk_sp<SkData> create_image_data(SkImageInfo* info) {
scroggo9d081722016-04-20 08:27:18 -070092 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
93 const size_t rowBytes = info->minRowBytes();
bungeman38d909e2016-08-02 14:40:46 -070094 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
scroggo9d081722016-04-20 08:27:18 -070095 {
96 SkBitmap bm;
97 bm.installPixels(*info, data->writable_data(), rowBytes);
98 SkCanvas canvas(bm);
99 draw_image_test_pattern(&canvas);
100 }
bungeman38d909e2016-08-02 14:40:46 -0700101 return data;
scroggo9d081722016-04-20 08:27:18 -0700102}
103static sk_sp<SkImage> create_data_image() {
104 SkImageInfo info;
105 sk_sp<SkData> data(create_image_data(&info));
bungeman38d909e2016-08-02 14:40:46 -0700106 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
scroggo9d081722016-04-20 08:27:18 -0700107}
Brian Salomon534cbe52017-01-03 11:35:56 -0500108static sk_sp<SkImage> create_image_large(int maxTextureSize) {
109 const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
bsalomond4907082016-06-13 12:13:03 -0700110 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}
reed9ce9d672016-03-17 10:51:11 -0700117static sk_sp<SkImage> create_picture_image() {
bsalomon8e74f802016-01-30 10:01:40 -0800118 SkPictureRecorder recorder;
119 SkCanvas* canvas = recorder.beginRecording(10, 10);
120 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700121 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
Matt Sarette94255d2017-01-09 12:38:59 -0500122 nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500123 SkColorSpace::MakeSRGB());
bsalomon8e74f802016-01-30 10:01:40 -0800124};
kkinnunen7b94c142015-11-24 07:39:40 -0800125// Want to ensure that our Release is called when the owning image is destroyed
126struct RasterDataHolder {
127 RasterDataHolder() : fReleaseCount(0) {}
bungeman38d909e2016-08-02 14:40:46 -0700128 sk_sp<SkData> fData;
kkinnunen7b94c142015-11-24 07:39:40 -0800129 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};
reed9ce9d672016-03-17 10:51:11 -0700136static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
kkinnunen7b94c142015-11-24 07:39:40 -0800137 SkASSERT(dataHolder);
138 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700139 dataHolder->fData = create_image_data(&info);
140 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
reed9ce9d672016-03-17 10:51:11 -0700141 RasterDataHolder::Release, dataHolder);
kkinnunen7b94c142015-11-24 07:39:40 -0800142}
reed9ce9d672016-03-17 10:51:11 -0700143static sk_sp<SkImage> create_codec_image() {
kkinnunen7b94c142015-11-24 07:39:40 -0800144 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700145 sk_sp<SkData> data(create_image_data(&info));
kkinnunen7b94c142015-11-24 07:39:40 -0800146 SkBitmap bitmap;
147 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
Leon Scroggins III0098ccb2018-09-24 15:24:31 -0400148 auto src = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 100);
bungeman38d909e2016-08-02 14:40:46 -0700149 return SkImage::MakeFromEncoded(std::move(src));
kkinnunen7b94c142015-11-24 07:39:40 -0800150}
Robert Phillipsb25e0652020-07-22 09:35:49 -0400151static sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext,
Brian Salomonbc074a62020-03-18 10:06:13 -0400152 bool withMips = false,
153 SkBudgeted budgeted = SkBudgeted::kYes) {
kkinnunen7b94c142015-11-24 07:39:40 -0800154 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400155 auto surface = SkSurface::MakeRenderTarget(rContext, budgeted, info, 0,
Brian Salomonbc074a62020-03-18 10:06:13 -0400156 kBottomLeft_GrSurfaceOrigin, nullptr, withMips);
kkinnunen7b94c142015-11-24 07:39:40 -0800157 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700158 return surface->makeImageSnapshot();
kkinnunen7b94c142015-11-24 07:39:40 -0800159}
reed871872f2015-06-22 12:48:26 -0700160
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400161static void test_encode(skiatest::Reporter* reporter, GrDirectContext* dContext, SkImage* image) {
reed871872f2015-06-22 12:48:26 -0700162 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
Mike Reed6409f842017-07-11 16:03:13 -0400163 sk_sp<SkData> origEncoded = image->encodeToData();
reed871872f2015-06-22 12:48:26 -0700164 REPORTER_ASSERT(reporter, origEncoded);
165 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
166
reed9ce9d672016-03-17 10:51:11 -0700167 sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
scroggo8e6c7ad2016-09-16 08:20:38 -0700168 if (!decoded) {
169 ERRORF(reporter, "failed to decode image!");
170 return;
171 }
reed871872f2015-06-22 12:48:26 -0700172 REPORTER_ASSERT(reporter, decoded);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400173 assert_equal(reporter, dContext, image, nullptr, decoded.get());
reed871872f2015-06-22 12:48:26 -0700174
175 // Now see if we can instantiate an image from a subset of the surface/origEncoded
mtklein5f939ab2016-03-16 10:28:35 -0700176
Mike Reed564d49e2020-07-28 12:52:31 -0400177 decoded = SkImage::MakeFromEncoded(origEncoded)->makeSubset(ir);
reed871872f2015-06-22 12:48:26 -0700178 REPORTER_ASSERT(reporter, decoded);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400179 assert_equal(reporter, dContext, image, &ir, decoded.get());
reed871872f2015-06-22 12:48:26 -0700180}
181
kkinnunen7b94c142015-11-24 07:39:40 -0800182DEF_TEST(ImageEncode, reporter) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400183 test_encode(reporter, nullptr, create_image().get());
reed871872f2015-06-22 12:48:26 -0700184}
185
bsalomon68d91342016-04-12 09:59:58 -0700186DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400187 auto dContext = ctxInfo.directContext();
188 test_encode(reporter, dContext, create_gpu_image(dContext).get());
reed871872f2015-06-22 12:48:26 -0700189}
reed759373a2015-07-03 21:01:10 -0700190
reed2dad7692016-08-01 11:12:58 -0700191DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
192 const struct {
reed1ec04d92016-08-05 12:07:41 -0700193 SkCopyPixelsMode fCPM;
194 bool fExpectSameAsMutable;
195 bool fExpectSameAsImmutable;
reed2dad7692016-08-01 11:12:58 -0700196 } recs[] = {
reed1ec04d92016-08-05 12:07:41 -0700197 { kIfMutable_SkCopyPixelsMode, false, true },
198 { kAlways_SkCopyPixelsMode, false, false },
199 { kNever_SkCopyPixelsMode, true, true },
reed2dad7692016-08-01 11:12:58 -0700200 };
201 for (auto rec : recs) {
202 SkPixmap pm;
203 SkBitmap bm;
204 bm.allocN32Pixels(100, 100);
205
reed1ec04d92016-08-05 12:07:41 -0700206 auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700207 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);
reedae296442016-08-05 13:19:01 -0700210 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
reed2dad7692016-08-01 11:12:58 -0700211
212 bm.notifyPixelsChanged(); // force a new generation ID
213
214 bm.setImmutable();
reed1ec04d92016-08-05 12:07:41 -0700215 img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700216 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);
reedae296442016-08-05 13:19:01 -0700219 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
reed2dad7692016-08-01 11:12:58 -0700220 }
221}
222
fmalitac3470342015-09-04 11:36:39 -0700223// Test that image encoding failures do not break picture serialization/deserialization.
224DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
reede8f30622016-03-23 18:59:25 -0700225 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
fmalitac3470342015-09-04 11:36:39 -0700226 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700227 sk_sp<SkImage> image(surface->makeImageSnapshot());
fmalitac3470342015-09-04 11:36:39 -0700228 REPORTER_ASSERT(reporter, image);
229
230 SkPictureRecorder recorder;
231 SkCanvas* canvas = recorder.beginRecording(100, 100);
232 canvas->drawImage(image, 0, 0);
reedca2622b2016-03-18 07:25:55 -0700233 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitac3470342015-09-04 11:36:39 -0700234 REPORTER_ASSERT(reporter, picture);
Mike Klein88d90712018-01-27 17:30:04 +0000235 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700236
Mike Reedef038482017-12-16 08:41:28 -0500237 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;
fmalitac3470342015-09-04 11:36:39 -0700244
Mike Reedef038482017-12-16 08:41:28 -0500245 REPORTER_ASSERT(reporter, !was_called);
Mike Reed47fdf6c2017-12-20 14:12:07 -0500246 auto data = picture->serialize(&procs);
Mike Reedef038482017-12-16 08:41:28 -0500247 REPORTER_ASSERT(reporter, was_called);
248 REPORTER_ASSERT(reporter, data && data->size() > 0);
fmalitac3470342015-09-04 11:36:39 -0700249
Mike Reedef038482017-12-16 08:41:28 -0500250 auto deserialized = SkPicture::MakeFromData(data->data(), data->size());
251 REPORTER_ASSERT(reporter, deserialized);
Mike Klein88d90712018-01-27 17:30:04 +0000252 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700253}
254
fmalita8c0144c2015-07-22 05:56:16 -0700255// 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()).
258DEF_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);
reede8f30622016-03-23 18:59:25 -0700262 auto surface(SkSurface::MakeRaster(info));
fmalita8c0144c2015-07-22 05:56:16 -0700263 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
reed9ce9d672016-03-17 10:51:11 -0700270 sk_sp<SkImage> image1(surface->makeImageSnapshot());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400271 REPORTER_ASSERT(reporter, image1->readPixels(nullptr, dstInfo, pixels, dstRowBytes, 0, 0));
fmalita8c0144c2015-07-22 05:56:16 -0700272 for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
273 REPORTER_ASSERT(reporter, pixels[i] == green);
274 }
275
276 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700277 paint.setBlendMode(SkBlendMode::kSrc);
fmalita8c0144c2015-07-22 05:56:16 -0700278 paint.setColor(SK_ColorRED);
279
280 surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
281
reed9ce9d672016-03-17 10:51:11 -0700282 sk_sp<SkImage> image2(surface->makeImageSnapshot());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400283 REPORTER_ASSERT(reporter, image2->readPixels(nullptr, dstInfo, pixels, dstRowBytes, 0, 0));
fmalita8c0144c2015-07-22 05:56:16 -0700284 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}
reed80c772b2015-07-30 18:58:23 -0700289
290/////////////////////////////////////////////////////////////////////////////////////////////////
reed80c772b2015-07-30 18:58:23 -0700291
292static void make_bitmap_mutable(SkBitmap* bm) {
293 bm->allocN32Pixels(10, 10);
294}
295
296static void make_bitmap_immutable(SkBitmap* bm) {
297 bm->allocN32Pixels(10, 10);
298 bm->setImmutable();
299}
300
301DEF_TEST(image_newfrombitmap, reporter) {
302 const struct {
303 void (*fMakeProc)(SkBitmap*);
304 bool fExpectPeekSuccess;
305 bool fExpectSharedID;
fmalitaddbbdda2015-08-20 08:47:26 -0700306 bool fExpectLazy;
reed80c772b2015-07-30 18:58:23 -0700307 } rec[] = {
fmalitaddbbdda2015-08-20 08:47:26 -0700308 { make_bitmap_mutable, true, false, false },
309 { make_bitmap_immutable, true, true, false },
reed80c772b2015-07-30 18:58:23 -0700310 };
311
312 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
313 SkBitmap bm;
314 rec[i].fMakeProc(&bm);
315
reed9ce9d672016-03-17 10:51:11 -0700316 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
reed80c772b2015-07-30 18:58:23 -0700317 SkPixmap pmap;
318
319 const bool sharedID = (image->uniqueID() == bm.getGenerationID());
320 REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
321
reed80c772b2015-07-30 18:58:23 -0700322 const bool peekSuccess = image->peekPixels(&pmap);
323 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
fmalitaddbbdda2015-08-20 08:47:26 -0700324
325 const bool lazy = image->isLazyGenerated();
326 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
reed80c772b2015-07-30 18:58:23 -0700327 }
328}
reed6f1216a2015-08-04 08:10:13 -0700329
330///////////////////////////////////////////////////////////////////////////////////////////////////
reed6f1216a2015-08-04 08:10:13 -0700331
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500332#include "src/core/SkBitmapCache.h"
reed6f1216a2015-08-04 08:10:13 -0700333
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 Osmane47e5b62018-10-04 14:19:39 -0400342DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_Gpu2Cpu, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800343 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400344 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400345 const auto desc = SkBitmapCacheDesc::Make(image.get());
reed6f1216a2015-08-04 08:10:13 -0700346
reede8f30622016-03-23 18:59:25 -0700347 auto surface(SkSurface::MakeRaster(info));
reed6f1216a2015-08-04 08:10:13 -0700348
349 // now we can test drawing a gpu-backed image into a cpu-backed surface
350
351 {
352 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400353 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700354 }
355
356 surface->getCanvas()->drawImage(image, 0, 0);
357 {
358 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400359 if (SkBitmapCache::Find(desc, &cachedBitmap)) {
reed6f1216a2015-08-04 08:10:13 -0700360 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 Reed5fa3d6d2017-03-25 09:51:00 -0400372 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700373 }
374}
bsalomon8e74f802016-01-30 10:01:40 -0800375
Brian Osman041f7df2017-02-07 11:23:28 -0500376DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400377 auto dContext = contextInfo.directContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500378 sk_gpu_test::TestContext* testContext = contextInfo.testContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500379 GrContextFactory otherFactory;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400380 ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type());
Brian Osman041f7df2017-02-07 11:23:28 -0500381 testContext->makeCurrent();
382
383 std::function<sk_sp<SkImage>()> imageFactories[] = {
Brian Salomonbc074a62020-03-18 10:06:13 -0400384 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 Phillipsb25e0652020-07-22 09:35:49 -0400388 [dContext] { return create_gpu_image(dContext, true, SkBudgeted::kYes); },
389 [dContext] { return create_gpu_image(dContext, false, SkBudgeted::kNo); },
Robert Phillipse94b4e12020-07-23 13:54:35 -0400390 // Create a texture image in a another context.
Brian Salomonbc074a62020-03-18 10:06:13 -0400391 [otherContextInfo] {
392 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -0400393 auto otherContextImage = create_gpu_image(otherContextInfo.directContext());
394 otherContextInfo.directContext()->flushAndSubmit();
Brian Salomonbc074a62020-03-18 10:06:13 -0400395 return otherContextImage;
396 }};
Brian Salomon7e67dca2020-07-21 09:27:25 -0400397 for (auto mipMapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400398 for (const auto& factory : imageFactories) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400399 sk_sp<SkImage> image(factory());
400 if (!image) {
401 ERRORF(reporter, "Error creating image.");
402 continue;
403 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400404 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 Phillipsb25e0652020-07-22 09:35:49 -0400410 auto texImage = image->makeTextureImage(dContext, mipMapped, budgeted);
Brian Salomonbc074a62020-03-18 10:06:13 -0400411 if (!texImage) {
Adlai Holler302e8fb2020-09-14 11:58:06 -0400412 auto imageContext = as_IB(image)->context();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400413 // We expect to fail if image comes from a different context
Robert Phillipsb25e0652020-07-22 09:35:49 -0400414 if (!image->isTextureBacked() || imageContext == dContext) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400415 ERRORF(reporter, "makeTextureImage failed.");
416 }
417 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500418 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400419 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 Phillipsb25e0652020-07-22 09:35:49 -0400426 mipMapped == GrMipmapped::kYes && dContext->priv().caps()->mipmapSupport();
Brian Salomon8c82a872020-07-21 12:09:58 -0400427 if (shouldBeMipped && copyProxy->mipmapped() == GrMipmapped::kNo) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400428 ERRORF(reporter, "makeTextureImage returned non-mipmapped texture.");
429 continue;
430 }
Brian Salomon8c82a872020-07-21 12:09:58 -0400431 bool origIsMipped = origProxy && origProxy->mipmapped() == GrMipmapped::kYes;
Brian Salomonbc074a62020-03-18 10:06:13 -0400432 if (image->isTextureBacked() && (!shouldBeMipped || origIsMipped)) {
433 if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400434 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400435 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400436 } else {
437 auto* texProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
438 REPORTER_ASSERT(reporter, !texProxy->getUniqueKey().isValid());
439 REPORTER_ASSERT(reporter, texProxy->isBudgeted() == budgeted);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400440 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400441 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 Osmand566e2e2019-08-14 13:19:04 -0400447 }
Brian Osman041f7df2017-02-07 11:23:28 -0500448 }
449 }
Robert Phillipsb25e0652020-07-22 09:35:49 -0400450 dContext->flushAndSubmit();
Brian Osman041f7df2017-02-07 11:23:28 -0500451}
452
bsalomon634b4302016-07-12 18:11:17 -0700453DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400454 auto dContext = contextInfo.directContext();
bsalomon634b4302016-07-12 18:11:17 -0700455
456 std::function<sk_sp<SkImage>()> imageFactories[] = {
457 create_image,
458 create_codec_image,
459 create_data_image,
460 create_picture_image,
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400461 [dContext] { return create_gpu_image(dContext); },
bsalomon634b4302016-07-12 18:11:17 -0700462 };
John Stilesbd3ffa42020-07-30 20:24:57 -0400463 for (const auto& factory : imageFactories) {
bsalomon634b4302016-07-12 18:11:17 -0700464 sk_sp<SkImage> image = factory();
465 if (!image->isTextureBacked()) {
466 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400467 if (!(image = image->makeTextureImage(dContext))) {
Brian Osman041f7df2017-02-07 11:23:28 -0500468 continue;
469 }
bsalomon634b4302016-07-12 18:11:17 -0700470 }
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 Hollerbcfc5542020-08-27 12:44:07 -0400476 assert_equal(reporter, dContext, image.get(), nullptr, rasterImage.get());
bsalomon634b4302016-07-12 18:11:17 -0700477 }
478}
479
Brian Salomonbdecacf2018-02-02 20:32:49 -0500480DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000481 auto dContext = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400482
483 static constexpr int kSize = 10;
484
Brian Salomonbdecacf2018-02-02 20:32:49 -0500485 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500486 SkColorType colorType = static_cast<SkColorType>(ct);
Adlai Holler14dc7912020-08-11 15:48:49 +0000487 bool can = dContext->colorTypeSupportedAsImage(colorType);
Robert Phillips9b16f812019-05-17 10:01:21 -0400488
Greg Danielc1ad77c2020-05-06 11:40:03 -0400489 GrBackendTexture backendTex;
Adlai Holler14dc7912020-08-11 15:48:49 +0000490 CreateBackendTexture(dContext, &backendTex, kSize, kSize, colorType, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400491 GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Robert Phillips9b16f812019-05-17 10:01:21 -0400492
Adlai Holler14dc7912020-08-11 15:48:49 +0000493 auto img = SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
Robert Phillips9b16f812019-05-17 10:01:21 -0400494 colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500495 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300496 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500497 colorType);
498
499 img.reset();
Adlai Holler14dc7912020-08-11 15:48:49 +0000500 dContext->flushAndSubmit();
501 dContext->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500502 }
503}
504
Brian Salomon9708af82018-02-05 12:57:10 -0500505DEF_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 Hollerbcfc5542020-08-27 12:44:07 -0400515 auto dContext = ctxInfo.directContext();
516 auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(dContext);
Brian Salomon9708af82018-02-05 12:57:10 -0500517 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
518 ERRORF(reporter, "Failed to make unpremul texture image.");
519 return;
520 }
Brian Salomon1d435302019-07-01 13:05:28 -0400521 SkBitmap unpremul;
522 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
523 kUnpremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400524 if (!texImage->readPixels(dContext, unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(),
525 0, 0)) {
Brian Salomon1d435302019-07-01 13:05:28 -0400526 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 Salomon9708af82018-02-05 12:57:10 -0500535 }
536 }
537 }
538 SkBitmap premul;
539 premul.allocPixels(
540 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400541 if (!texImage->readPixels(dContext, premul.info(), premul.getPixels(), premul.rowBytes(),
542 0, 0)) {
Brian Salomon9708af82018-02-05 12:57:10 -0500543 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 Salomon1d435302019-07-01 13:05:28 -0400548 uint32_t origColor = *bmp.getAddr32(x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500549 int32_t origA = (origColor >> 24) & 0xff;
Brian Salomon1d435302019-07-01 13:05:28 -0400550 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 Salomon9708af82018-02-05 12:57:10 -0500555 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 Salomon1d435302019-07-01 13:05:28 -0400565 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 Salomon9708af82018-02-05 12:57:10 -0500567 return;
568 }
569 }
570 }
571}
572
Brian Salomon8a8dd332018-05-24 14:08:31 -0400573DEF_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 Holler302e8fb2020-09-14 11:58:06 -0400596 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400597
598 factory->get(type)->abandonContext();
599 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400600 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400601 // This shouldn't crash.
602 rsurf->getCanvas()->drawImage(img, 0, 0);
603
Robert Phillipse94b4e12020-07-23 13:54:35 -0400604 // Give up all other refs on the context.
Brian Salomon8a8dd332018-05-24 14:08:31 -0400605 factory.reset(nullptr);
Adlai Holler302e8fb2020-09-14 11:58:06 -0400606 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400607 // This shouldn't crash.
608 rsurf->getCanvas()->drawImage(img, 0, 0);
609 }
610}
611
kkinnunen4e184132015-11-17 22:53:28 -0800612class EmptyGenerator : public SkImageGenerator {
613public:
614 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
615};
616
kkinnunen7b94c142015-11-24 07:39:40 -0800617DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800618 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700619 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 Reed185130c2017-02-15 15:14:16 -0500623 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500624 std::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800625}
626
kkinnunen7b94c142015-11-24 07:39:40 -0800627DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800628 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
629 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400630 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700631 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800632 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700633 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800634 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700635 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800636 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800637}
638
kkinnunen4e184132015-11-17 22:53:28 -0800639static 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 Hollerbcfc5542020-08-27 12:44:07 -0400648static void image_test_read_pixels(GrDirectContext* dContext, skiatest::Reporter* reporter,
649 SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700650 if (!image) {
651 ERRORF(reporter, "Failed to create image!");
652 return;
653 }
kkinnunen7b94c142015-11-24 07:39:40 -0800654 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800655 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 Hollerbcfc5542020-08-27 12:44:07 -0400664 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800665
666 // out-of-bounds should fail
667 info = SkImageInfo::MakeN32Premul(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400668 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()));
kkinnunen4e184132015-11-17 22:53:28 -0800674
675 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800676 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400677 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800678 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
679
680 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800681 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400682 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800683 image->width() - w, image->height() - h));
684 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
685
686 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800687 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400688 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, -1, -1));
kkinnunen4e184132015-11-17 22:53:28 -0800689 REPORTER_ASSERT(reporter, pixels[3] == expected);
690 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
691
692 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800693 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400694 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800695 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}
kkinnunen7b94c142015-11-24 07:39:40 -0800699DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700700 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400701 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800702
reed9ce9d672016-03-17 10:51:11 -0700703 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400704 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800705
706 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700707 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400708 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800709 image.reset();
710 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
711
reed9ce9d672016-03-17 10:51:11 -0700712 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400713 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800714}
egdanielab527a52016-06-28 08:07:26 -0700715DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400716 auto dContext = ctxInfo.directContext();
717 image_test_read_pixels(dContext, reporter, create_gpu_image(dContext).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800718}
kkinnunen4e184132015-11-17 22:53:28 -0800719
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400720static void check_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
721 const SkImage* image, const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800722 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
723 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700724 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800725
Cary Clark4f5a79c2018-02-07 15:51:00 -0500726 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800727
kkinnunen4e184132015-11-17 22:53:28 -0800728 REPORTER_ASSERT(reporter, bitmap.getPixels());
729
730 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
731 SkPMColor imageColor;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400732 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, &imageColor, sizeof(SkPMColor),
733 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800734 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
735}
736
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400737static void test_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
738 const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700739 if (!image) {
740 ERRORF(reporter, "Failed to create image.");
741 return;
742 }
kkinnunen7b94c142015-11-24 07:39:40 -0800743 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500744 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400745 check_legacy_bitmap(reporter, dContext, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800746
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)));
reed9ce9d672016-03-17 10:51:11 -0700751 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
752 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800753
754 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500755 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400756 check_legacy_bitmap(reporter, nullptr, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800757}
758DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500759 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400760 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800761
Cary Clark4f5a79c2018-02-07 15:51:00 -0500762 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400763 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800764
Cary Clark4f5a79c2018-02-07 15:51:00 -0500765 RasterDataHolder dataHolder;
766 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400767 test_legacy_bitmap(reporter, nullptr, image.get());
Cary Clark4f5a79c2018-02-07 15:51:00 -0500768 image.reset();
769 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800770
Cary Clark4f5a79c2018-02-07 15:51:00 -0500771 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400772 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800773}
bsalomon68d91342016-04-12 09:59:58 -0700774DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400775 auto dContext = ctxInfo.directContext();
776 sk_sp<SkImage> image(create_gpu_image(dContext));
777 test_legacy_bitmap(reporter, dContext, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800778}
kkinnunen4e184132015-11-17 22:53:28 -0800779
kkinnunen7b94c142015-11-24 07:39:40 -0800780static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700781 if (!image) {
782 ERRORF(reporter, "Failed to create image!");
783 return;
784 }
reed6ceeebd2016-03-09 14:26:26 -0800785 SkPixmap pm;
786 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800787 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
788 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800789 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800790 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());
reed6ceeebd2016-03-09 14:26:26 -0800795 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
796 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800797 }
kkinnunen7b94c142015-11-24 07:39:40 -0800798}
799DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700800 sk_sp<SkImage> image(create_image());
801 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800802
reed9ce9d672016-03-17 10:51:11 -0700803 image = create_data_image();
804 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800805
806 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700807 image = create_rasterproc_image(&dataHolder);
808 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800809 image.reset();
810 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
811
reed9ce9d672016-03-17 10:51:11 -0700812 image = create_codec_image();
813 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800814}
egdanielab527a52016-06-28 08:07:26 -0700815DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400816 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
reed9ce9d672016-03-17 10:51:11 -0700817 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800818}
kkinnunen4e184132015-11-17 22:53:28 -0800819
kkinnunen7b94c142015-11-24 07:39:40 -0800820struct TextureReleaseChecker {
821 TextureReleaseChecker() : fReleaseCount(0) {}
822 int fReleaseCount;
823 static void Release(void* self) {
824 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800825 }
826};
Brian Osman13dddce2017-05-09 13:19:50 -0400827
brianosmandb2cb102016-07-22 07:22:04 -0700828DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
829 const int kWidth = 10;
830 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000831
Adlai Holler14dc7912020-08-11 15:48:49 +0000832 auto dContext = ctxInfo.directContext();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000833
Robert Phillipsee5fd132019-05-07 13:29:22 -0400834 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
835 kPremul_SkAlphaType);
836 GrBackendTexture backendTex;
Adlai Holler14dc7912020-08-11 15:48:49 +0000837 if (!CreateBackendTexture(dContext, &backendTex, ii, SkColors::kRed, GrMipmapped::kNo,
Brian Salomon28a8f282019-10-24 20:07:39 -0400838 GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400839 ERRORF(reporter, "couldn't create backend texture\n");
840 }
brianosmandb2cb102016-07-22 07:22:04 -0700841
kkinnunen7b94c142015-11-24 07:39:40 -0800842 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000843 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
reed9ce9d672016-03-17 10:51:11 -0700844 sk_sp<SkImage> refImg(
Adlai Holler14dc7912020-08-11 15:48:49 +0000845 SkImage::MakeFromTexture(dContext, backendTex, texOrigin, kRGBA_8888_SkColorType,
Greg Danielf5d87582017-12-18 14:48:15 -0500846 kPremul_SkAlphaType, nullptr,
reed9ce9d672016-03-17 10:51:11 -0700847 TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800848
Robert Phillips3390e152017-01-31 17:53:34 -0500849 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400850 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
851 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) {
852 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500853 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400854 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000855 if (readBackOrigin != texOrigin) {
856 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500857 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000858 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500859
kkinnunen4e184132015-11-17 22:53:28 -0800860 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800861 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800862 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800863 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
brianosmandb2cb102016-07-22 07:22:04 -0700864
Adlai Holler14dc7912020-08-11 15:48:49 +0000865 DeleteBackendTexture(dContext, backendTex);
kkinnunen4e184132015-11-17 22:53:28 -0800866}
bsalomon0d996862016-03-09 18:44:43 -0800867
Brian Salomondcfca432017-11-15 15:48:03 -0500868static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400869 const char* testName,
Robert Phillipsb25e0652020-07-22 09:35:49 -0400870 std::function<sk_sp<SkImage>(GrDirectContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400871 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500872 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400873 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
874 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400875 auto dContext = ctxInfo.directContext();
876 if (!dContext) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400877 continue;
878 }
Brian Osman13dddce2017-05-09 13:19:50 -0400879
Brian Osmanceb7a422017-06-21 15:10:33 -0400880 // 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 Phillipsb25e0652020-07-22 09:35:49 -0400883 if (!dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400884 continue;
885 }
Brian Osman13dddce2017-05-09 13:19:50 -0400886
Brian Osmanceb7a422017-06-21 15:10:33 -0400887 // 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 Osman13dddce2017-05-09 13:19:50 -0400894
Brian Osmanceb7a422017-06-21 15:10:33 -0400895 // Case #1: Create image, free image
896 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400897 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osmanceb7a422017-06-21 15:10:33 -0400898 refImg.reset(nullptr); // force a release of the image
899 }
Brian Osman13dddce2017-05-09 13:19:50 -0400900
Brian Osmanceb7a422017-06-21 15:10:33 -0400901 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400902 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400903 if (!surface) {
904 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
905 continue;
906 }
907
Brian Osmanceb7a422017-06-21 15:10:33 -0400908 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400909
Brian Osmanceb7a422017-06-21 15:10:33 -0400910 // Case #2: Create image, draw, flush, free image
911 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400912 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400913
Brian Osmanceb7a422017-06-21 15:10:33 -0400914 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400915 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400916
Brian Osmanceb7a422017-06-21 15:10:33 -0400917 refImg.reset(nullptr); // force a release of the image
918 }
Brian Osman13dddce2017-05-09 13:19:50 -0400919
Brian Osmanceb7a422017-06-21 15:10:33 -0400920 // Case #3: Create image, draw, free image, flush
921 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400922 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400923
Brian Osmanceb7a422017-06-21 15:10:33 -0400924 canvas->drawImage(refImg, 0, 0);
925 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400926
Greg Daniel0a2464f2020-05-14 15:45:44 -0400927 surface->flushAndSubmit();
Brian Osmanceb7a422017-06-21 15:10:33 -0400928 }
Brian Osman13dddce2017-05-09 13:19:50 -0400929
Brian Osmanceb7a422017-06-21 15:10:33 -0400930 // Configure second context
931 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400932
Robert Phillipsb25e0652020-07-22 09:35:49 -0400933 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(dContext);
Robert Phillips6d344c32020-07-06 10:56:46 -0400934 auto otherCtx = otherContextInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400935 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400936
Brian Osmanceb7a422017-06-21 15:10:33 -0400937 // Creating a context in a share group may fail
938 if (!otherCtx) {
939 continue;
940 }
Brian Osman13dddce2017-05-09 13:19:50 -0400941
Brian Osmanceb7a422017-06-21 15:10:33 -0400942 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
943 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400944
Brian Osmanceb7a422017-06-21 15:10:33 -0400945 // Case #4: Create image, draw*, flush*, free image
946 {
947 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400948 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400949
Brian Osmanceb7a422017-06-21 15:10:33 -0400950 otherTestContext->makeCurrent();
951 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400952 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400953
Brian Osmanceb7a422017-06-21 15:10:33 -0400954 testContext->makeCurrent();
955 refImg.reset(nullptr); // force a release of the image
956 }
Brian Osman13dddce2017-05-09 13:19:50 -0400957
Brian Osmanceb7a422017-06-21 15:10:33 -0400958 // Case #5: Create image, draw*, free image, flush*
959 {
960 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400961 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400962
Brian Osmanceb7a422017-06-21 15:10:33 -0400963 otherTestContext->makeCurrent();
964 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400965
Brian Osmanceb7a422017-06-21 15:10:33 -0400966 testContext->makeCurrent();
967 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400968
Brian Osmanceb7a422017-06-21 15:10:33 -0400969 otherTestContext->makeCurrent();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400970 surface->flushAndSubmit();
Greg Daniel3f475d92017-07-25 16:26:35 -0400971
Greg Daniel26b50a42018-03-08 09:49:58 -0500972 // This is specifically here for vulkan to guarantee the command buffer will finish
973 // which is when we call the ReleaseProc.
Robert Phillips9da87e02019-02-04 13:26:26 -0500974 otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400975 }
Brian Osman13dddce2017-05-09 13:19:50 -0400976
Brian Osmanceb7a422017-06-21 15:10:33 -0400977 // Case #6: Verify that only one context can be using the image at a time
978 {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400979 // Suppress warnings about trying to use a texture in two contexts.
Chris Dalton5a5fe792020-02-15 11:41:30 -0700980 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(otherCtx);
981
Brian Osmanceb7a422017-06-21 15:10:33 -0400982 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400983 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400984
Brian Osmanceb7a422017-06-21 15:10:33 -0400985 // Any context should be able to borrow the texture at this point
Robert Phillipsb25e0652020-07-22 09:35:49 -0400986 GrSurfaceProxyView view = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500987 REPORTER_ASSERT(reporter, view);
Brian Osman13dddce2017-05-09 13:19:50 -0400988
Brian Osmanceb7a422017-06-21 15:10:33 -0400989 // But once it's borrowed, no other context should be able to borrow
990 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400991 GrSurfaceProxyView otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500992 REPORTER_ASSERT(reporter, !otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -0400993
994 // Original context (that's already borrowing) should be okay
995 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400996 GrSurfaceProxyView viewSecondRef = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500997 REPORTER_ASSERT(reporter, viewSecondRef);
Brian Osmanceb7a422017-06-21 15:10:33 -0400998
Greg Danielabba9982018-02-01 10:07:57 -0500999 // Release first ref from the original context
Greg Danielfebdedf2020-02-05 17:06:27 -05001000 view.reset();
Greg Danielabba9982018-02-01 10:07:57 -05001001
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 Salomon7e67dca2020-07-21 09:27:25 -04001005 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001006 REPORTER_ASSERT(reporter, !otherView);
Greg Danielabba9982018-02-01 10:07:57 -05001007
1008 // Release second ref from the original context
1009 testContext->makeCurrent();
Greg Danielfebdedf2020-02-05 17:06:27 -05001010 viewSecondRef.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001011
Greg Daniel48661b82018-01-22 16:11:35 -05001012 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001013 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001014 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001015 REPORTER_ASSERT(reporter, otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -04001016
1017 // Release everything
Greg Danielfebdedf2020-02-05 17:06:27 -05001018 otherView.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001019 refImg.reset(nullptr);
1020 }
Brian Osman13dddce2017-05-09 13:19:50 -04001021 }
1022}
1023
Brian Salomondcfca432017-11-15 15:48:03 -05001024DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001025 SkBitmap bitmap;
1026 SkPixmap pixmap;
Hal Canarybaa2a282018-11-26 15:34:12 -05001027 if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
1028 ERRORF(reporter, "missing resource");
1029 return;
1030 }
Hal Canaryf7d3f612018-03-22 15:17:42 -04001031 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
Robert Phillipsb25e0652020-07-22 09:35:49 -04001032 [&pixmap](GrDirectContext* dContext) {
1033 return SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman63bc48d2017-11-07 10:37:00 -05001034 });
1035}
1036
Brian Osman052ef692018-03-27 09:56:31 -04001037DEF_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 Hollerc41ae2a2020-08-11 15:47:47 +00001047 auto dContext = ctxInfo.directContext();
1048 if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001049 continue;
1050 }
1051
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001052 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman052ef692018-03-27 09:56:31 -04001053 REPORTER_ASSERT(reporter, image);
1054
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001055 GrSurfaceProxyView view = as_IB(image)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001056 REPORTER_ASSERT(reporter, view);
Brian Osman052ef692018-03-27 09:56:31 -04001057
1058 bool expectAlpha = kAlpha_8_SkColorType == ct;
Greg Daniela4828a12019-10-11 13:51:02 -04001059 GrColorType grCT = SkColorTypeToGrColorType(image->colorType());
1060 REPORTER_ASSERT(reporter, expectAlpha == GrColorTypeIsAlphaOnly(grCT));
Brian Osman052ef692018-03-27 09:56:31 -04001061 }
1062 }
1063}
1064
Eric Karl914a36b2017-10-12 12:44:50 -07001065DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001066 auto context = ctxInfo.directContext();
Eric Karl914a36b2017-10-12 12:44:50 -07001067 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 Phillips9da87e02019-02-04 13:26:26 -05001076 return create_image_large(context->priv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001077 };
John Stilesbd3ffa42020-07-30 20:24:57 -04001078 struct TestCase {
1079 std::function<sk_sp<SkImage>()> fImageFactory;
1080 bool fExpectation;
1081 bool fCanTakeDirectly;
1082 };
1083 TestCase testCases[] = {
Eric Karl914a36b2017-10-12 12:44:50 -07001084 { 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 Phillipse94b4e12020-07-23 13:54:35 -04001089 // Create a texture image in a another context.
Brian Salomon55ad7742017-11-17 09:25:23 -05001090 { [otherContextInfo] {
1091 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -04001092 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.directContext());
1093 otherContextInfo.directContext()->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001094 return otherContextImage;
1095 }, false, false },
1096 // Create an image that is too large to be texture backed.
1097 { createLarge, false, false }
1098 };
1099
John Stilesbd3ffa42020-07-30 20:24:57 -04001100 for (const TestCase& testCase : testCases) {
Eric Karl914a36b2017-10-12 12:44:50 -07001101 sk_sp<SkImage> image(testCase.fImageFactory());
1102 if (!image) {
1103 ERRORF(reporter, "Failed to create image!");
1104 continue;
1105 }
1106
Robert Phillipsba375a82018-04-11 10:08:06 -04001107 GrBackendTexture origBackend = image->getBackendTexture(true);
1108 if (testCase.fCanTakeDirectly) {
1109 SkASSERT(origBackend.isValid());
1110 }
1111
1112 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001113 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001114 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1115 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001116 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 Phillipsba375a82018-04-11 10:08:06 -04001122 if (result) {
1123 SkASSERT(newBackend.isValid());
1124 }
1125
1126 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001127 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 Daniel0a2464f2020-05-14 15:45:44 -04001133 context->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001134 }
1135}
reedeb560282016-07-26 19:42:04 -07001136
1137///////////////////////////////////////////////////////////////////////////////////////////////////
1138
Matt Sarett0bb6f382017-03-06 10:28:24 -05001139static 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
1147DEF_TEST(Image_ColorSpace, r) {
1148 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001149 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001150 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1151
Hal Canaryc465d132017-12-08 10:21:31 -05001152 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Brian Osman82ebe042019-01-04 17:03:00 -05001153 skcms_TransferFunction fn;
Matt Sarett0bb6f382017-03-06 10:28:24 -05001154 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1155 REPORTER_ASSERT(r, success);
Brian Osman82ebe042019-01-04 17:03:00 -05001156 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
Matt Sarett0bb6f382017-03-06 10:28:24 -05001157
Brian Osman82ebe042019-01-04 17:03:00 -05001158 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
1159 SkNamedGamut::kRec2020);
Matt Sarett0bb6f382017-03-06 10:28:24 -05001160 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 Sarett6de13102017-03-14 14:10:48 -04001179DEF_TEST(Image_makeColorSpace, r) {
Mike Kleinb147ace2020-01-16 11:11:06 -06001180 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Brian Osman82ebe042019-01-04 17:03:00 -05001181 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 Sarett6de13102017-03-14 14:10:48 -04001184
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 Holler3a220172020-07-15 10:37:50 -04001190 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, nullptr);
Matt Sarett6de13102017-03-14 14:10:48 -04001191 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001192 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001193
1194 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1195
Matt Sarett6de13102017-03-14 14:10:48 -04001196 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001197 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 Holler3a220172020-07-15 10:37:50 -04001201 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, nullptr);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001202 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001203 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001204 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001205 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 Canaryc465d132017-12-08 10:21:31 -05001209 srgbImage = GetResourceAsImage("images/1x1.png");
Adlai Holler3a220172020-07-15 10:37:50 -04001210 p3Image = srgbImage->makeColorSpace(p3, nullptr);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001211 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001212 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001213 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 Sarett0bb6f382017-03-06 10:28:24 -05001218///////////////////////////////////////////////////////////////////////////////////////////////////
1219
reedeb560282016-07-26 19:42:04 -07001220static 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 Osman788b9162020-02-07 10:36:46 -05001225 int c = std::min(a, r);
reedeb560282016-07-26 19:42:04 -07001226 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1227 }
1228 }
1229}
1230
1231static 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 Reed6f0286f2016-11-28 17:26:27 -05001235 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 }
reedeb560282016-07-26 19:42:04 -07001241 }
1242 }
1243 return true;
1244}
1245
1246DEF_TEST(image_roundtrip_encode, reporter) {
1247 SkBitmap bm0;
1248 make_all_premul(&bm0);
1249
1250 auto img0 = SkImage::MakeFromBitmap(bm0);
Mike Reed6409f842017-07-11 16:03:13 -04001251 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001252 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001253
reedeb560282016-07-26 19:42:04 -07001254 SkBitmap bm1;
1255 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001256 img1->readPixels(nullptr, bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001257
reedeb560282016-07-26 19:42:04 -07001258 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1259}
1260
1261DEF_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 Osmanb8a13922017-04-26 15:28:30 -04001275
1276///////////////////////////////////////////////////////////////////////////////////////////////////
1277
1278static 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
1292static 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
1309DEF_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 Reed6409f842017-07-11 16:03:13 -04001321 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001322 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1323 test_scale_pixels(reporter, codecImage.get(), pmRed);
1324}
1325
Brian Osmanb8a13922017-04-26 15:28:30 -04001326DEF_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 Phillips6d344c32020-07-06 10:56:46 -04001331 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.directContext(),
1332 SkBudgeted::kNo, info);
Brian Osmanb8a13922017-04-26 15:28:30 -04001333 surface->getCanvas()->clear(red);
1334 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1335 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1336}
Mike Reedc4e31092018-01-30 11:15:27 -05001337
1338static sk_sp<SkImage> any_image_will_do() {
1339 return GetResourceAsImage("images/mandrill_32.png");
1340}
1341
1342DEF_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 Kleinea3f0142019-03-20 11:12:10 -05001358 ToolUtils::PixelIter iter(surf.get());
Mike Reedc4e31092018-01-30 11:15:27 -05001359 while (void* addr = iter.next()) {
1360 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1361 }
1362 }
1363 }
1364}
1365
Robert Phillipsb25e0652020-07-22 09:35:49 -04001366static sk_sp<SkImage> make_yuva_image(GrDirectContext* dContext) {
Brian Salomonad8efda2019-05-10 09:22:49 -04001367 SkAutoPixmapStorage pm;
1368 pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
Brian Salomonc2a9a972020-09-15 11:24:28 -04001369 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 Salomonad8efda2019-05-10 09:22:49 -04001372
Brian Salomonc2a9a972020-09-15 11:24:28 -04001373 return SkImage::MakeFromYUVAPixmaps(dContext, yuvaPixmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001374}
1375
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001376DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -04001377 auto dContext = ctxInfo.directContext();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001378 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001379 auto s = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, ii, 1, nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001380
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 Salomonf9a1fdf2019-05-09 10:30:12 -04001386 // Make a YUVA image.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001387 auto i2 = make_yuva_image(dContext);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001388
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 Phillipsb25e0652020-07-22 09:35:49 -04001391 dContext->flushAndSubmit();
1392 auto numSubmits =
1393 [dContext,
1394 submitCnt = dContext->priv().getGpu()->stats()->numSubmitToGpus()]() mutable {
1395 int curr = dContext->priv().getGpu()->stats()->numSubmitToGpus();
Greg Daniel04283f32020-05-20 13:16:00 -04001396 int n = curr - submitCnt;
1397 submitCnt = curr;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001398 return n;
1399 };
1400
Greg Daniel04283f32020-05-20 13:16:00 -04001401 // Images aren't used therefore flush is ignored, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001402 i0->flushAndSubmit(dContext);
1403 i1->flushAndSubmit(dContext);
1404 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001405 REPORTER_ASSERT(reporter, numSubmits() == 3);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001406
1407 // Syncing forces the flush to happen even if the images aren't used.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001408 i0->flush(dContext);
1409 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001410 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001411 i1->flush(dContext);
1412 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001413 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001414 i2->flush(dContext);
1415 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001416 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001417
1418 // Use image 1
1419 s->getCanvas()->drawImage(i1, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001420 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001421 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001422 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001423 // Flushing image 1 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001424 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001425 REPORTER_ASSERT(reporter, numSubmits() == 1);
1426 // Flushing image 2 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001427 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001428 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001429
1430 // Use image 2
1431 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001432 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001433 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001434 REPORTER_ASSERT(reporter, numSubmits() == 1);
1435 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001436 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001437 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001438 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001439 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001440 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001441 // 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 Salomonad8efda2019-05-10 09:22:49 -04001444 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1445
1446 // Flatten it and repeat.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001447 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001448 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 Daniel04283f32020-05-20 13:16:00 -04001452 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001453 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001454 REPORTER_ASSERT(reporter, numSubmits() == 1);
1455 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001456 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001457 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001458 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001459 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001460 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001461
1462 // Test case where flatten happens before the first flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001463 i2 = make_yuva_image(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001464 // 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 Phillipse94b4e12020-07-23 13:54:35 -04001466 // make the YUVA planes from backend textures rather than pixmaps that the context must upload.
Greg Daniel04283f32020-05-20 13:16:00 -04001467 // Calling numSubmits rebases the flush count from here.
1468 numSubmits();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001469 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001470 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 Daniel04283f32020-05-20 13:16:00 -04001474 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001475 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001476 REPORTER_ASSERT(reporter, numSubmits() == 1);
1477 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001478 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001479 REPORTER_ASSERT(reporter, numSubmits() == 1);
1480 // Flushing image 2 should flush, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001481 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001482 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001483}
Mike Reed3d30ca62020-07-22 16:55:02 -04001484
1485#include "src/shaders/SkImageShader.h"
1486
1487constexpr 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
1493constexpr 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
1499DEF_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 Reedcd043562020-07-29 13:55:45 -04001514
1515DEF_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}