blob: cf70c793fab36a29cbd49d5aefabd03e8db93b11 [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"
Adlai Hollera0693042020-10-14 11:23:11 -040029#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrGpu.h"
Robert Phillips326f1d72020-10-01 09:43:29 -040031#include "src/gpu/GrImageContextPriv.h"
Chris Dalton5a5fe792020-02-15 11:41:30 -070032#include "src/gpu/GrRecordingContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrResourceCache.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000034#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/SkGr.h"
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040036#include "src/image/SkImage_Base.h"
37#include "src/image/SkImage_GpuYUVA.h"
38#include "tests/Test.h"
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040039#include "tools/Resources.h"
40#include "tools/ToolUtils.h"
Brian Salomon72050802020-10-12 20:45:06 -040041#include "tools/gpu/ManagedBackendTexture.h"
brianosmandb2cb102016-07-22 07:22:04 -070042
bsalomonf2f1c172016-04-05 12:59:06 -070043using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070044
Matt Sarettf5759932017-02-07 21:52:07 +000045SkImageInfo read_pixels_info(SkImage* image) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -040046 if (image->colorSpace()) {
Matt Sarettf5759932017-02-07 21:52:07 +000047 return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType());
48 }
49
50 return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
51}
52
Adlai Hollerbcfc5542020-08-27 12:44:07 -040053// image `b` is assumed to be raster
54static void assert_equal(skiatest::Reporter* reporter, GrDirectContext* dContextA, SkImage* a,
55 const SkIRect* subsetA, SkImage* b) {
reed871872f2015-06-22 12:48:26 -070056 const int widthA = subsetA ? subsetA->width() : a->width();
57 const int heightA = subsetA ? subsetA->height() : a->height();
58
59 REPORTER_ASSERT(reporter, widthA == b->width());
60 REPORTER_ASSERT(reporter, heightA == b->height());
reed1cb36462016-03-09 15:21:32 -080061
62 // see https://bug.skia.org/3965
63 //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
reed871872f2015-06-22 12:48:26 -070064
reed871872f2015-06-22 12:48:26 -070065 SkAutoPixmapStorage pmapA, pmapB;
Matt Sarettf5759932017-02-07 21:52:07 +000066 pmapA.alloc(read_pixels_info(a));
67 pmapB.alloc(read_pixels_info(b));
reed871872f2015-06-22 12:48:26 -070068
69 const int srcX = subsetA ? subsetA->x() : 0;
70 const int srcY = subsetA ? subsetA->y() : 0;
71
Adlai Hollerbcfc5542020-08-27 12:44:07 -040072 REPORTER_ASSERT(reporter, a->readPixels(dContextA, pmapA, srcX, srcY));
73 REPORTER_ASSERT(reporter, b->readPixels(nullptr, pmapB, 0, 0));
reed871872f2015-06-22 12:48:26 -070074
Matt Sarettf5759932017-02-07 21:52:07 +000075 const size_t widthBytes = widthA * 4;
reed871872f2015-06-22 12:48:26 -070076 for (int y = 0; y < heightA; ++y) {
77 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
78 }
79}
kkinnunen7b94c142015-11-24 07:39:40 -080080static void draw_image_test_pattern(SkCanvas* canvas) {
reed871872f2015-06-22 12:48:26 -070081 canvas->clear(SK_ColorWHITE);
reed871872f2015-06-22 12:48:26 -070082 SkPaint paint;
83 paint.setColor(SK_ColorBLACK);
kkinnunen7b94c142015-11-24 07:39:40 -080084 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
85}
reed9ce9d672016-03-17 10:51:11 -070086static sk_sp<SkImage> create_image() {
kkinnunen7b94c142015-11-24 07:39:40 -080087 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070088 auto surface(SkSurface::MakeRaster(info));
kkinnunen7b94c142015-11-24 07:39:40 -080089 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -070090 return surface->makeImageSnapshot();
reed871872f2015-06-22 12:48:26 -070091}
bungeman38d909e2016-08-02 14:40:46 -070092static sk_sp<SkData> create_image_data(SkImageInfo* info) {
scroggo9d081722016-04-20 08:27:18 -070093 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
94 const size_t rowBytes = info->minRowBytes();
bungeman38d909e2016-08-02 14:40:46 -070095 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
scroggo9d081722016-04-20 08:27:18 -070096 {
97 SkBitmap bm;
98 bm.installPixels(*info, data->writable_data(), rowBytes);
99 SkCanvas canvas(bm);
100 draw_image_test_pattern(&canvas);
101 }
bungeman38d909e2016-08-02 14:40:46 -0700102 return data;
scroggo9d081722016-04-20 08:27:18 -0700103}
104static sk_sp<SkImage> create_data_image() {
105 SkImageInfo info;
106 sk_sp<SkData> data(create_image_data(&info));
bungeman38d909e2016-08-02 14:40:46 -0700107 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
scroggo9d081722016-04-20 08:27:18 -0700108}
Brian Salomon534cbe52017-01-03 11:35:56 -0500109static sk_sp<SkImage> create_image_large(int maxTextureSize) {
110 const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
bsalomond4907082016-06-13 12:13:03 -0700111 auto surface(SkSurface::MakeRaster(info));
112 surface->getCanvas()->clear(SK_ColorWHITE);
113 SkPaint paint;
114 paint.setColor(SK_ColorBLACK);
115 surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint);
116 return surface->makeImageSnapshot();
117}
reed9ce9d672016-03-17 10:51:11 -0700118static sk_sp<SkImage> create_picture_image() {
bsalomon8e74f802016-01-30 10:01:40 -0800119 SkPictureRecorder recorder;
120 SkCanvas* canvas = recorder.beginRecording(10, 10);
121 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700122 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
Matt Sarette94255d2017-01-09 12:38:59 -0500123 nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500124 SkColorSpace::MakeSRGB());
bsalomon8e74f802016-01-30 10:01:40 -0800125};
kkinnunen7b94c142015-11-24 07:39:40 -0800126// Want to ensure that our Release is called when the owning image is destroyed
127struct RasterDataHolder {
128 RasterDataHolder() : fReleaseCount(0) {}
bungeman38d909e2016-08-02 14:40:46 -0700129 sk_sp<SkData> fData;
kkinnunen7b94c142015-11-24 07:39:40 -0800130 int fReleaseCount;
131 static void Release(const void* pixels, void* context) {
132 RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
133 self->fReleaseCount++;
134 self->fData.reset();
135 }
136};
reed9ce9d672016-03-17 10:51:11 -0700137static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
kkinnunen7b94c142015-11-24 07:39:40 -0800138 SkASSERT(dataHolder);
139 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700140 dataHolder->fData = create_image_data(&info);
141 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
reed9ce9d672016-03-17 10:51:11 -0700142 RasterDataHolder::Release, dataHolder);
kkinnunen7b94c142015-11-24 07:39:40 -0800143}
reed9ce9d672016-03-17 10:51:11 -0700144static sk_sp<SkImage> create_codec_image() {
kkinnunen7b94c142015-11-24 07:39:40 -0800145 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700146 sk_sp<SkData> data(create_image_data(&info));
kkinnunen7b94c142015-11-24 07:39:40 -0800147 SkBitmap bitmap;
148 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
Leon Scroggins III0098ccb2018-09-24 15:24:31 -0400149 auto src = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 100);
bungeman38d909e2016-08-02 14:40:46 -0700150 return SkImage::MakeFromEncoded(std::move(src));
kkinnunen7b94c142015-11-24 07:39:40 -0800151}
Robert Phillipsb25e0652020-07-22 09:35:49 -0400152static sk_sp<SkImage> create_gpu_image(GrRecordingContext* rContext,
Brian Salomonbc074a62020-03-18 10:06:13 -0400153 bool withMips = false,
154 SkBudgeted budgeted = SkBudgeted::kYes) {
kkinnunen7b94c142015-11-24 07:39:40 -0800155 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400156 auto surface = SkSurface::MakeRenderTarget(rContext, budgeted, info, 0,
Brian Salomonbc074a62020-03-18 10:06:13 -0400157 kBottomLeft_GrSurfaceOrigin, nullptr, withMips);
kkinnunen7b94c142015-11-24 07:39:40 -0800158 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700159 return surface->makeImageSnapshot();
kkinnunen7b94c142015-11-24 07:39:40 -0800160}
reed871872f2015-06-22 12:48:26 -0700161
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400162static void test_encode(skiatest::Reporter* reporter, GrDirectContext* dContext, SkImage* image) {
reed871872f2015-06-22 12:48:26 -0700163 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
Mike Reed6409f842017-07-11 16:03:13 -0400164 sk_sp<SkData> origEncoded = image->encodeToData();
reed871872f2015-06-22 12:48:26 -0700165 REPORTER_ASSERT(reporter, origEncoded);
166 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
167
reed9ce9d672016-03-17 10:51:11 -0700168 sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
scroggo8e6c7ad2016-09-16 08:20:38 -0700169 if (!decoded) {
170 ERRORF(reporter, "failed to decode image!");
171 return;
172 }
reed871872f2015-06-22 12:48:26 -0700173 REPORTER_ASSERT(reporter, decoded);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400174 assert_equal(reporter, dContext, image, nullptr, decoded.get());
reed871872f2015-06-22 12:48:26 -0700175
176 // Now see if we can instantiate an image from a subset of the surface/origEncoded
mtklein5f939ab2016-03-16 10:28:35 -0700177
Mike Reed564d49e2020-07-28 12:52:31 -0400178 decoded = SkImage::MakeFromEncoded(origEncoded)->makeSubset(ir);
reed871872f2015-06-22 12:48:26 -0700179 REPORTER_ASSERT(reporter, decoded);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400180 assert_equal(reporter, dContext, image, &ir, decoded.get());
reed871872f2015-06-22 12:48:26 -0700181}
182
kkinnunen7b94c142015-11-24 07:39:40 -0800183DEF_TEST(ImageEncode, reporter) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400184 test_encode(reporter, nullptr, create_image().get());
reed871872f2015-06-22 12:48:26 -0700185}
186
bsalomon68d91342016-04-12 09:59:58 -0700187DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400188 auto dContext = ctxInfo.directContext();
189 test_encode(reporter, dContext, create_gpu_image(dContext).get());
reed871872f2015-06-22 12:48:26 -0700190}
reed759373a2015-07-03 21:01:10 -0700191
reed2dad7692016-08-01 11:12:58 -0700192DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
193 const struct {
reed1ec04d92016-08-05 12:07:41 -0700194 SkCopyPixelsMode fCPM;
195 bool fExpectSameAsMutable;
196 bool fExpectSameAsImmutable;
reed2dad7692016-08-01 11:12:58 -0700197 } recs[] = {
reed1ec04d92016-08-05 12:07:41 -0700198 { kIfMutable_SkCopyPixelsMode, false, true },
199 { kAlways_SkCopyPixelsMode, false, false },
200 { kNever_SkCopyPixelsMode, true, true },
reed2dad7692016-08-01 11:12:58 -0700201 };
202 for (auto rec : recs) {
203 SkPixmap pm;
204 SkBitmap bm;
205 bm.allocN32Pixels(100, 100);
206
reed1ec04d92016-08-05 12:07:41 -0700207 auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700208 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
209 const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
210 REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable);
reedae296442016-08-05 13:19:01 -0700211 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
reed2dad7692016-08-01 11:12:58 -0700212
213 bm.notifyPixelsChanged(); // force a new generation ID
214
215 bm.setImmutable();
reed1ec04d92016-08-05 12:07:41 -0700216 img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700217 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
218 const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
219 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
reedae296442016-08-05 13:19:01 -0700220 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
reed2dad7692016-08-01 11:12:58 -0700221 }
222}
223
fmalitac3470342015-09-04 11:36:39 -0700224// Test that image encoding failures do not break picture serialization/deserialization.
225DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
reede8f30622016-03-23 18:59:25 -0700226 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
fmalitac3470342015-09-04 11:36:39 -0700227 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700228 sk_sp<SkImage> image(surface->makeImageSnapshot());
fmalitac3470342015-09-04 11:36:39 -0700229 REPORTER_ASSERT(reporter, image);
230
231 SkPictureRecorder recorder;
232 SkCanvas* canvas = recorder.beginRecording(100, 100);
Mike Reed4f23dec2020-12-30 14:22:42 +0000233 canvas->drawImage(image.get(), 0, 0, SkSamplingOptions());
reedca2622b2016-03-18 07:25:55 -0700234 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitac3470342015-09-04 11:36:39 -0700235 REPORTER_ASSERT(reporter, picture);
Mike Klein88d90712018-01-27 17:30:04 +0000236 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700237
Mike Reedef038482017-12-16 08:41:28 -0500238 bool was_called = false;
239 SkSerialProcs procs;
240 procs.fImageProc = [](SkImage*, void* called) {
241 *(bool*)called = true;
242 return SkData::MakeEmpty();
243 };
244 procs.fImageCtx = &was_called;
fmalitac3470342015-09-04 11:36:39 -0700245
Mike Reedef038482017-12-16 08:41:28 -0500246 REPORTER_ASSERT(reporter, !was_called);
Mike Reed47fdf6c2017-12-20 14:12:07 -0500247 auto data = picture->serialize(&procs);
Mike Reedef038482017-12-16 08:41:28 -0500248 REPORTER_ASSERT(reporter, was_called);
249 REPORTER_ASSERT(reporter, data && data->size() > 0);
fmalitac3470342015-09-04 11:36:39 -0700250
Mike Reedef038482017-12-16 08:41:28 -0500251 auto deserialized = SkPicture::MakeFromData(data->data(), data->size());
252 REPORTER_ASSERT(reporter, deserialized);
Mike Klein88d90712018-01-27 17:30:04 +0000253 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700254}
255
fmalita8c0144c2015-07-22 05:56:16 -0700256// Test that a draw that only partially covers the drawing surface isn't
257// interpreted as covering the entire drawing surface (i.e., exercise one of the
258// conditions of SkCanvas::wouldOverwriteEntireSurface()).
259DEF_TEST(Image_RetainSnapshot, reporter) {
260 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
261 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
262 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
reede8f30622016-03-23 18:59:25 -0700263 auto surface(SkSurface::MakeRaster(info));
fmalita8c0144c2015-07-22 05:56:16 -0700264 surface->getCanvas()->clear(0xFF00FF00);
265
266 SkPMColor pixels[4];
267 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
268 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
269 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
270
reed9ce9d672016-03-17 10:51:11 -0700271 sk_sp<SkImage> image1(surface->makeImageSnapshot());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400272 REPORTER_ASSERT(reporter, image1->readPixels(nullptr, dstInfo, pixels, dstRowBytes, 0, 0));
fmalita8c0144c2015-07-22 05:56:16 -0700273 for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
274 REPORTER_ASSERT(reporter, pixels[i] == green);
275 }
276
277 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700278 paint.setBlendMode(SkBlendMode::kSrc);
fmalita8c0144c2015-07-22 05:56:16 -0700279 paint.setColor(SK_ColorRED);
280
281 surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
282
reed9ce9d672016-03-17 10:51:11 -0700283 sk_sp<SkImage> image2(surface->makeImageSnapshot());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400284 REPORTER_ASSERT(reporter, image2->readPixels(nullptr, dstInfo, pixels, dstRowBytes, 0, 0));
fmalita8c0144c2015-07-22 05:56:16 -0700285 REPORTER_ASSERT(reporter, pixels[0] == green);
286 REPORTER_ASSERT(reporter, pixels[1] == green);
287 REPORTER_ASSERT(reporter, pixels[2] == green);
288 REPORTER_ASSERT(reporter, pixels[3] == red);
289}
reed80c772b2015-07-30 18:58:23 -0700290
291/////////////////////////////////////////////////////////////////////////////////////////////////
reed80c772b2015-07-30 18:58:23 -0700292
293static void make_bitmap_mutable(SkBitmap* bm) {
294 bm->allocN32Pixels(10, 10);
295}
296
297static void make_bitmap_immutable(SkBitmap* bm) {
298 bm->allocN32Pixels(10, 10);
299 bm->setImmutable();
300}
301
302DEF_TEST(image_newfrombitmap, reporter) {
303 const struct {
304 void (*fMakeProc)(SkBitmap*);
305 bool fExpectPeekSuccess;
306 bool fExpectSharedID;
fmalitaddbbdda2015-08-20 08:47:26 -0700307 bool fExpectLazy;
reed80c772b2015-07-30 18:58:23 -0700308 } rec[] = {
fmalitaddbbdda2015-08-20 08:47:26 -0700309 { make_bitmap_mutable, true, false, false },
310 { make_bitmap_immutable, true, true, false },
reed80c772b2015-07-30 18:58:23 -0700311 };
312
313 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
314 SkBitmap bm;
315 rec[i].fMakeProc(&bm);
316
Mike Reeddc607e32020-12-23 11:50:36 -0500317 sk_sp<SkImage> image(bm.asImage());
reed80c772b2015-07-30 18:58:23 -0700318 SkPixmap pmap;
319
320 const bool sharedID = (image->uniqueID() == bm.getGenerationID());
321 REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
322
reed80c772b2015-07-30 18:58:23 -0700323 const bool peekSuccess = image->peekPixels(&pmap);
324 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
fmalitaddbbdda2015-08-20 08:47:26 -0700325
326 const bool lazy = image->isLazyGenerated();
327 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
reed80c772b2015-07-30 18:58:23 -0700328 }
329}
reed6f1216a2015-08-04 08:10:13 -0700330
331///////////////////////////////////////////////////////////////////////////////////////////////////
reed6f1216a2015-08-04 08:10:13 -0700332
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500333#include "src/core/SkBitmapCache.h"
reed6f1216a2015-08-04 08:10:13 -0700334
335/*
336 * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image.
337 * We cache it for performance when drawing into a raster surface.
338 *
339 * A cleaner test would know if each drawImage call triggered a read-back from the gpu,
340 * but we don't have that facility (at the moment) so we use a little internal knowledge
341 * of *how* the raster version is cached, and look for that.
342 */
Brian Osmane47e5b62018-10-04 14:19:39 -0400343DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_Gpu2Cpu, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800344 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400345 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400346 const auto desc = SkBitmapCacheDesc::Make(image.get());
reed6f1216a2015-08-04 08:10:13 -0700347
reede8f30622016-03-23 18:59:25 -0700348 auto surface(SkSurface::MakeRaster(info));
reed6f1216a2015-08-04 08:10:13 -0700349
350 // now we can test drawing a gpu-backed image into a cpu-backed surface
351
352 {
353 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400354 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700355 }
356
357 surface->getCanvas()->drawImage(image, 0, 0);
358 {
359 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400360 if (SkBitmapCache::Find(desc, &cachedBitmap)) {
reed6f1216a2015-08-04 08:10:13 -0700361 REPORTER_ASSERT(reporter, cachedBitmap.isImmutable());
362 REPORTER_ASSERT(reporter, cachedBitmap.getPixels());
363 } else {
364 // unexpected, but not really a bug, since the cache is global and this test may be
365 // run w/ other threads competing for its budget.
366 SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n");
367 }
368 }
369
370 image.reset(nullptr);
371 {
372 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400373 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700374 }
375}
bsalomon8e74f802016-01-30 10:01:40 -0800376
Brian Osman041f7df2017-02-07 11:23:28 -0500377DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400378 auto dContext = contextInfo.directContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500379 sk_gpu_test::TestContext* testContext = contextInfo.testContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500380 GrContextFactory otherFactory;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400381 ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type());
Brian Osman041f7df2017-02-07 11:23:28 -0500382 testContext->makeCurrent();
383
384 std::function<sk_sp<SkImage>()> imageFactories[] = {
Brian Salomonbc074a62020-03-18 10:06:13 -0400385 create_image, create_codec_image, create_data_image,
386 // Create an image from a picture.
387 create_picture_image,
388 // Create a texture image.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400389 [dContext] { return create_gpu_image(dContext, true, SkBudgeted::kYes); },
390 [dContext] { return create_gpu_image(dContext, false, SkBudgeted::kNo); },
Robert Phillipse94b4e12020-07-23 13:54:35 -0400391 // Create a texture image in a another context.
Brian Salomonbc074a62020-03-18 10:06:13 -0400392 [otherContextInfo] {
393 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -0400394 auto otherContextImage = create_gpu_image(otherContextInfo.directContext());
395 otherContextInfo.directContext()->flushAndSubmit();
Brian Salomonbc074a62020-03-18 10:06:13 -0400396 return otherContextImage;
397 }};
Brian Salomon7d2757f2021-01-26 17:46:50 -0500398 for (auto mipmapped : {GrMipmapped::kNo, GrMipmapped::kYes}) {
John Stilesbd3ffa42020-07-30 20:24:57 -0400399 for (const auto& factory : imageFactories) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400400 sk_sp<SkImage> image(factory());
401 if (!image) {
402 ERRORF(reporter, "Error creating image.");
403 continue;
404 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400405 GrTextureProxy* origProxy = nullptr;
Brian Salomon7d2757f2021-01-26 17:46:50 -0500406 bool origIsMippedTexture = false;
407
Brian Salomonbc074a62020-03-18 10:06:13 -0400408 if (auto sp = as_IB(image)->peekProxy()) {
409 origProxy = sp->asTextureProxy();
410 SkASSERT(origProxy);
Brian Salomon7d2757f2021-01-26 17:46:50 -0500411 REPORTER_ASSERT(reporter, (origProxy->mipmapped() == GrMipmapped::kYes) ==
412 image->hasMipmaps());
413 origIsMippedTexture = image->hasMipmaps();
Brian Salomonbc074a62020-03-18 10:06:13 -0400414 }
415 for (auto budgeted : {SkBudgeted::kNo, SkBudgeted::kYes}) {
Brian Salomon7d2757f2021-01-26 17:46:50 -0500416 auto texImage = image->makeTextureImage(dContext, mipmapped, budgeted);
Brian Salomonbc074a62020-03-18 10:06:13 -0400417 if (!texImage) {
Adlai Holler302e8fb2020-09-14 11:58:06 -0400418 auto imageContext = as_IB(image)->context();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400419 // We expect to fail if image comes from a different context
Robert Phillips326f1d72020-10-01 09:43:29 -0400420 if (!image->isTextureBacked() || imageContext->priv().matches(dContext)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400421 ERRORF(reporter, "makeTextureImage failed.");
422 }
423 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500424 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400425 if (!texImage->isTextureBacked()) {
426 ERRORF(reporter, "makeTextureImage returned non-texture image.");
427 continue;
428 }
429 GrTextureProxy* copyProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
430 SkASSERT(copyProxy);
Brian Salomon7d2757f2021-01-26 17:46:50 -0500431 // Did we ask for MIPs on a context that supports them?
432 bool validRequestForMips = (mipmapped == GrMipmapped::kYes &&
433 dContext->priv().caps()->mipmapSupport());
434 // Do we expect the "copy" to have MIPs?
435 bool shouldBeMipped = origIsMippedTexture || validRequestForMips;
436 REPORTER_ASSERT(reporter, shouldBeMipped == texImage->hasMipmaps());
437 REPORTER_ASSERT(reporter,
438 shouldBeMipped == (copyProxy->mipmapped() == GrMipmapped::kYes));
439
440 // We should only make a copy of an already texture-backed image if it didn't
441 // already have MIPs but we asked for MIPs and the context supports it.
442 if (image->isTextureBacked() && (!validRequestForMips || origIsMippedTexture)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400443 if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400444 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400445 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400446 } else {
447 auto* texProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
448 REPORTER_ASSERT(reporter, !texProxy->getUniqueKey().isValid());
449 REPORTER_ASSERT(reporter, texProxy->isBudgeted() == budgeted);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400450 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400451 if (image->width() != texImage->width() || image->height() != texImage->height()) {
452 ERRORF(reporter, "makeTextureImage changed the image size.");
453 }
454 if (image->alphaType() != texImage->alphaType()) {
455 ERRORF(reporter, "makeTextureImage changed image alpha type.");
456 }
Brian Osmand566e2e2019-08-14 13:19:04 -0400457 }
Brian Osman041f7df2017-02-07 11:23:28 -0500458 }
459 }
Robert Phillipsb25e0652020-07-22 09:35:49 -0400460 dContext->flushAndSubmit();
Brian Osman041f7df2017-02-07 11:23:28 -0500461}
462
bsalomon634b4302016-07-12 18:11:17 -0700463DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400464 auto dContext = contextInfo.directContext();
bsalomon634b4302016-07-12 18:11:17 -0700465
466 std::function<sk_sp<SkImage>()> imageFactories[] = {
467 create_image,
468 create_codec_image,
469 create_data_image,
470 create_picture_image,
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400471 [dContext] { return create_gpu_image(dContext); },
bsalomon634b4302016-07-12 18:11:17 -0700472 };
John Stilesbd3ffa42020-07-30 20:24:57 -0400473 for (const auto& factory : imageFactories) {
bsalomon634b4302016-07-12 18:11:17 -0700474 sk_sp<SkImage> image = factory();
475 if (!image->isTextureBacked()) {
476 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400477 if (!(image = image->makeTextureImage(dContext))) {
Brian Osman041f7df2017-02-07 11:23:28 -0500478 continue;
479 }
bsalomon634b4302016-07-12 18:11:17 -0700480 }
481 auto rasterImage = image->makeNonTextureImage();
482 if (!rasterImage) {
483 ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
484 }
485 REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400486 assert_equal(reporter, dContext, image.get(), nullptr, rasterImage.get());
bsalomon634b4302016-07-12 18:11:17 -0700487 }
488}
489
Brian Salomonbdecacf2018-02-02 20:32:49 -0500490DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000491 auto dContext = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400492
493 static constexpr int kSize = 10;
494
Brian Salomonbdecacf2018-02-02 20:32:49 -0500495 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500496 SkColorType colorType = static_cast<SkColorType>(ct);
Adlai Holler14dc7912020-08-11 15:48:49 +0000497 bool can = dContext->colorTypeSupportedAsImage(colorType);
Robert Phillips9b16f812019-05-17 10:01:21 -0400498
Brian Salomon72050802020-10-12 20:45:06 -0400499 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
500 dContext, kSize, kSize, colorType, GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -0500501 sk_sp<SkImage> img;
502 if (mbet) {
503 img = SkImage::MakeFromTexture(dContext, mbet->texture(), kTopLeft_GrSurfaceOrigin,
504 colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -0400505 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500506 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300507 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500508 colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500509 }
510}
511
Brian Salomon9708af82018-02-05 12:57:10 -0500512DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
513 SkBitmap bmp;
514 bmp.allocPixels(
515 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
516 for (int y = 0; y < 256; ++y) {
517 for (int x = 0; x < 256; ++x) {
518 *bmp.getAddr32(x, y) =
519 SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
520 }
521 }
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400522 auto dContext = ctxInfo.directContext();
Mike Reeddc607e32020-12-23 11:50:36 -0500523 auto texImage = bmp.asImage()->makeTextureImage(dContext);
Brian Salomon9708af82018-02-05 12:57:10 -0500524 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
525 ERRORF(reporter, "Failed to make unpremul texture image.");
526 return;
527 }
Brian Salomon1d435302019-07-01 13:05:28 -0400528 SkBitmap unpremul;
529 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
530 kUnpremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400531 if (!texImage->readPixels(dContext, unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(),
532 0, 0)) {
Brian Salomon1d435302019-07-01 13:05:28 -0400533 ERRORF(reporter, "Unpremul readback failed.");
534 return;
535 }
536 for (int y = 0; y < 256; ++y) {
537 for (int x = 0; x < 256; ++x) {
538 if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
539 ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
540 *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
541 return;
Brian Salomon9708af82018-02-05 12:57:10 -0500542 }
543 }
544 }
545 SkBitmap premul;
546 premul.allocPixels(
547 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400548 if (!texImage->readPixels(dContext, premul.info(), premul.getPixels(), premul.rowBytes(),
549 0, 0)) {
Brian Salomon9708af82018-02-05 12:57:10 -0500550 ERRORF(reporter, "Unpremul readback failed.");
551 return;
552 }
553 for (int y = 0; y < 256; ++y) {
554 for (int x = 0; x < 256; ++x) {
Brian Salomon1d435302019-07-01 13:05:28 -0400555 uint32_t origColor = *bmp.getAddr32(x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500556 int32_t origA = (origColor >> 24) & 0xff;
Brian Salomon1d435302019-07-01 13:05:28 -0400557 float a = origA / 255.f;
558 int32_t origB = sk_float_round2int(((origColor >> 16) & 0xff) * a);
559 int32_t origG = sk_float_round2int(((origColor >> 8) & 0xff) * a);
560 int32_t origR = sk_float_round2int(((origColor >> 0) & 0xff) * a);
561
Brian Salomon9708af82018-02-05 12:57:10 -0500562 uint32_t read = *premul.getAddr32(x, y);
563 int32_t readA = (read >> 24) & 0xff;
564 int32_t readB = (read >> 16) & 0xff;
565 int32_t readG = (read >> 8) & 0xff;
566 int32_t readR = (read >> 0) & 0xff;
567 // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
568 // bit of tolerance for GPU vs CPU premul math.
569 int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
570 if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
571 SkTAbs(readR - origR) > tol) {
Brian Salomon1d435302019-07-01 13:05:28 -0400572 ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) expected(0x%08x) at %d, %d.",
573 *bmp.getAddr32(x, y), *premul.getAddr32(x, y), origColor, x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500574 return;
575 }
576 }
577 }
578}
579
Brian Salomon8a8dd332018-05-24 14:08:31 -0400580DEF_GPUTEST(AbandonedContextImage, reporter, options) {
581 using Factory = sk_gpu_test::GrContextFactory;
582 for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
583 auto type = static_cast<Factory::ContextType>(ct);
584 std::unique_ptr<Factory> factory(new Factory);
585 if (!factory->get(type)) {
586 continue;
587 }
588
589 sk_sp<SkImage> img;
590 auto gsurf = SkSurface::MakeRenderTarget(
591 factory->get(type), SkBudgeted::kYes,
592 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
593 nullptr);
594 if (!gsurf) {
595 continue;
596 }
597 img = gsurf->makeImageSnapshot();
598 gsurf.reset();
599
600 auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
601
602 REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400603 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400604
605 factory->get(type)->abandonContext();
606 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400607 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400608 // This shouldn't crash.
609 rsurf->getCanvas()->drawImage(img, 0, 0);
610
Robert Phillipse94b4e12020-07-23 13:54:35 -0400611 // Give up all other refs on the context.
Brian Salomon8a8dd332018-05-24 14:08:31 -0400612 factory.reset(nullptr);
Adlai Holler302e8fb2020-09-14 11:58:06 -0400613 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400614 // This shouldn't crash.
615 rsurf->getCanvas()->drawImage(img, 0, 0);
616 }
617}
618
kkinnunen4e184132015-11-17 22:53:28 -0800619class EmptyGenerator : public SkImageGenerator {
620public:
621 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
622};
623
kkinnunen7b94c142015-11-24 07:39:40 -0800624DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800625 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700626 SkPixmap pmap(info, nullptr, 0);
627 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
628 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
629 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500630 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500631 std::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800632}
633
kkinnunen7b94c142015-11-24 07:39:40 -0800634DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800635 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
636 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400637 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700638 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800639 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700640 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800641 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700642 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800643 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800644}
645
kkinnunen4e184132015-11-17 22:53:28 -0800646static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
647 for (int i = 0; i < count; ++i) {
648 if (pixels[i] != expected) {
649 return false;
650 }
651 }
652 return true;
653}
654
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400655static void image_test_read_pixels(GrDirectContext* dContext, skiatest::Reporter* reporter,
656 SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700657 if (!image) {
658 ERRORF(reporter, "Failed to create image!");
659 return;
660 }
kkinnunen7b94c142015-11-24 07:39:40 -0800661 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800662 const SkPMColor notExpected = ~expected;
663
664 const int w = 2, h = 2;
665 const size_t rowBytes = w * sizeof(SkPMColor);
666 SkPMColor pixels[w*h];
667
668 SkImageInfo info;
669
670 info = SkImageInfo::MakeUnknown(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400671 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800672
673 // out-of-bounds should fail
674 info = SkImageInfo::MakeN32Premul(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400675 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, -w, 0));
676 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, -h));
677 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
678 image->width(), 0));
679 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
680 0, image->height()));
kkinnunen4e184132015-11-17 22:53:28 -0800681
682 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800683 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400684 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800685 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
686
687 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800688 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400689 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800690 image->width() - w, image->height() - h));
691 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
692
693 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800694 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400695 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, -1, -1));
kkinnunen4e184132015-11-17 22:53:28 -0800696 REPORTER_ASSERT(reporter, pixels[3] == expected);
697 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
698
699 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800700 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400701 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800702 image->width() - 1, image->height() - 1));
703 REPORTER_ASSERT(reporter, pixels[0] == expected);
704 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
705}
kkinnunen7b94c142015-11-24 07:39:40 -0800706DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700707 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400708 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800709
reed9ce9d672016-03-17 10:51:11 -0700710 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400711 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800712
713 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700714 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400715 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800716 image.reset();
717 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
718
reed9ce9d672016-03-17 10:51:11 -0700719 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400720 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800721}
egdanielab527a52016-06-28 08:07:26 -0700722DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400723 auto dContext = ctxInfo.directContext();
724 image_test_read_pixels(dContext, reporter, create_gpu_image(dContext).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800725}
kkinnunen4e184132015-11-17 22:53:28 -0800726
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400727static void check_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
728 const SkImage* image, const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800729 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
730 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700731 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800732
Cary Clark4f5a79c2018-02-07 15:51:00 -0500733 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800734
kkinnunen4e184132015-11-17 22:53:28 -0800735 REPORTER_ASSERT(reporter, bitmap.getPixels());
736
737 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
738 SkPMColor imageColor;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400739 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, &imageColor, sizeof(SkPMColor),
740 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800741 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
742}
743
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400744static void test_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
745 const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700746 if (!image) {
747 ERRORF(reporter, "Failed to create image.");
748 return;
749 }
kkinnunen7b94c142015-11-24 07:39:40 -0800750 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500751 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400752 check_legacy_bitmap(reporter, dContext, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800753
754 // Test subsetting to exercise the rowBytes logic.
755 SkBitmap tmp;
756 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
757 image->height() / 2)));
Mike Reeddc607e32020-12-23 11:50:36 -0500758 sk_sp<SkImage> subsetImage(tmp.asImage());
reed9ce9d672016-03-17 10:51:11 -0700759 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800760
761 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500762 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400763 check_legacy_bitmap(reporter, nullptr, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800764}
765DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500766 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400767 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800768
Cary Clark4f5a79c2018-02-07 15:51:00 -0500769 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400770 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800771
Cary Clark4f5a79c2018-02-07 15:51:00 -0500772 RasterDataHolder dataHolder;
773 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400774 test_legacy_bitmap(reporter, nullptr, image.get());
Cary Clark4f5a79c2018-02-07 15:51:00 -0500775 image.reset();
776 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800777
Cary Clark4f5a79c2018-02-07 15:51:00 -0500778 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400779 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800780}
bsalomon68d91342016-04-12 09:59:58 -0700781DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400782 auto dContext = ctxInfo.directContext();
783 sk_sp<SkImage> image(create_gpu_image(dContext));
784 test_legacy_bitmap(reporter, dContext, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800785}
kkinnunen4e184132015-11-17 22:53:28 -0800786
kkinnunen7b94c142015-11-24 07:39:40 -0800787static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700788 if (!image) {
789 ERRORF(reporter, "Failed to create image!");
790 return;
791 }
reed6ceeebd2016-03-09 14:26:26 -0800792 SkPixmap pm;
793 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800794 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
795 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800796 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800797 REPORTER_ASSERT(reporter, 20 == info.width());
798 REPORTER_ASSERT(reporter, 20 == info.height());
799 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
800 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
801 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800802 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
803 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800804 }
kkinnunen7b94c142015-11-24 07:39:40 -0800805}
806DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700807 sk_sp<SkImage> image(create_image());
808 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800809
reed9ce9d672016-03-17 10:51:11 -0700810 image = create_data_image();
811 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800812
813 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700814 image = create_rasterproc_image(&dataHolder);
815 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800816 image.reset();
817 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
818
reed9ce9d672016-03-17 10:51:11 -0700819 image = create_codec_image();
820 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800821}
egdanielab527a52016-06-28 08:07:26 -0700822DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400823 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
reed9ce9d672016-03-17 10:51:11 -0700824 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800825}
kkinnunen4e184132015-11-17 22:53:28 -0800826
kkinnunen7b94c142015-11-24 07:39:40 -0800827struct TextureReleaseChecker {
828 TextureReleaseChecker() : fReleaseCount(0) {}
829 int fReleaseCount;
830 static void Release(void* self) {
831 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800832 }
833};
Brian Osman13dddce2017-05-09 13:19:50 -0400834
brianosmandb2cb102016-07-22 07:22:04 -0700835DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
836 const int kWidth = 10;
837 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000838
Adlai Holler14dc7912020-08-11 15:48:49 +0000839 auto dContext = ctxInfo.directContext();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000840
Brian Salomon72050802020-10-12 20:45:06 -0400841 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext,
842 kWidth,
843 kHeight,
844 kRGBA_8888_SkColorType,
845 GrMipmapped::kNo,
846 GrRenderable::kNo,
847 GrProtected::kNo);
848 if (!mbet) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400849 ERRORF(reporter, "couldn't create backend texture\n");
Brian Salomon88d7e622020-11-05 11:11:20 -0500850 return;
Robert Phillipsee5fd132019-05-07 13:29:22 -0400851 }
brianosmandb2cb102016-07-22 07:22:04 -0700852
kkinnunen7b94c142015-11-24 07:39:40 -0800853 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000854 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon72050802020-10-12 20:45:06 -0400855 sk_sp<SkImage> refImg = SkImage::MakeFromTexture(
856 dContext,
857 mbet->texture(),
858 texOrigin,
859 kRGBA_8888_SkColorType,
860 kPremul_SkAlphaType,
861 /*color space*/nullptr,
862 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
863 mbet->releaseContext(TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800864
Robert Phillips3390e152017-01-31 17:53:34 -0500865 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400866 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
Brian Salomon72050802020-10-12 20:45:06 -0400867 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, mbet->texture())) {
Robert Phillipsc5509952018-04-04 15:54:55 -0400868 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500869 }
Brian Salomon72050802020-10-12 20:45:06 -0400870 REPORTER_ASSERT(reporter,
871 GrBackendTexture::TestingOnly_Equals(readBackBackendTex, mbet->texture()));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000872 if (readBackOrigin != texOrigin) {
873 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500874 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000875 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500876
kkinnunen4e184132015-11-17 22:53:28 -0800877 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800878 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800879 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800880 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800881}
bsalomon0d996862016-03-09 18:44:43 -0800882
Brian Salomondcfca432017-11-15 15:48:03 -0500883static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400884 const char* testName,
Robert Phillipsb25e0652020-07-22 09:35:49 -0400885 std::function<sk_sp<SkImage>(GrDirectContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400886 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500887 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400888 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
889 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400890 auto dContext = ctxInfo.directContext();
891 if (!dContext) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400892 continue;
893 }
Brian Osman13dddce2017-05-09 13:19:50 -0400894
Brian Osmanceb7a422017-06-21 15:10:33 -0400895 // If we don't have proper support for this feature, the factory will fallback to returning
896 // codec-backed images. Those will "work", but some of our checks will fail because we
897 // expect the cross-context images not to work on multiple contexts at once.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400898 if (!dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400899 continue;
900 }
Brian Osman13dddce2017-05-09 13:19:50 -0400901
Brian Osmanceb7a422017-06-21 15:10:33 -0400902 // We test three lifetime patterns for a single context:
903 // 1) Create image, free image
904 // 2) Create image, draw, flush, free image
905 // 3) Create image, draw, free image, flush
906 // ... and then repeat the last two patterns with drawing on a second* context:
907 // 4) Create image, draw*, flush*, free image
908 // 5) Create image, draw*, free iamge, flush*
Brian Osman13dddce2017-05-09 13:19:50 -0400909
Brian Osmanceb7a422017-06-21 15:10:33 -0400910 // Case #1: Create image, free image
911 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400912 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osmanceb7a422017-06-21 15:10:33 -0400913 refImg.reset(nullptr); // force a release of the image
914 }
Brian Osman13dddce2017-05-09 13:19:50 -0400915
Brian Osmanceb7a422017-06-21 15:10:33 -0400916 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400917 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400918 if (!surface) {
919 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
920 continue;
921 }
922
Brian Osmanceb7a422017-06-21 15:10:33 -0400923 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400924
Brian Osmanceb7a422017-06-21 15:10:33 -0400925 // Case #2: Create image, draw, flush, free image
926 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400927 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400928
Brian Osmanceb7a422017-06-21 15:10:33 -0400929 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400930 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400931
Brian Osmanceb7a422017-06-21 15:10:33 -0400932 refImg.reset(nullptr); // force a release of the image
933 }
Brian Osman13dddce2017-05-09 13:19:50 -0400934
Brian Osmanceb7a422017-06-21 15:10:33 -0400935 // Case #3: Create image, draw, free image, flush
936 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400937 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400938
Brian Osmanceb7a422017-06-21 15:10:33 -0400939 canvas->drawImage(refImg, 0, 0);
940 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400941
Greg Daniel0a2464f2020-05-14 15:45:44 -0400942 surface->flushAndSubmit();
Brian Osmanceb7a422017-06-21 15:10:33 -0400943 }
Brian Osman13dddce2017-05-09 13:19:50 -0400944
Brian Osmanceb7a422017-06-21 15:10:33 -0400945 // Configure second context
946 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400947
Robert Phillipsb25e0652020-07-22 09:35:49 -0400948 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(dContext);
Robert Phillips6d344c32020-07-06 10:56:46 -0400949 auto otherCtx = otherContextInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400950 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400951
Brian Osmanceb7a422017-06-21 15:10:33 -0400952 // Creating a context in a share group may fail
953 if (!otherCtx) {
954 continue;
955 }
Brian Osman13dddce2017-05-09 13:19:50 -0400956
Brian Osmanceb7a422017-06-21 15:10:33 -0400957 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
958 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400959
Brian Osmanceb7a422017-06-21 15:10:33 -0400960 // Case #4: Create image, draw*, flush*, free image
961 {
962 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400963 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400964
Brian Osmanceb7a422017-06-21 15:10:33 -0400965 otherTestContext->makeCurrent();
966 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400967 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400968
Brian Osmanceb7a422017-06-21 15:10:33 -0400969 testContext->makeCurrent();
970 refImg.reset(nullptr); // force a release of the image
971 }
Brian Osman13dddce2017-05-09 13:19:50 -0400972
Brian Osmanceb7a422017-06-21 15:10:33 -0400973 // Case #5: Create image, draw*, free image, flush*
974 {
975 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400976 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400977
Brian Osmanceb7a422017-06-21 15:10:33 -0400978 otherTestContext->makeCurrent();
979 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400980
Brian Osmanceb7a422017-06-21 15:10:33 -0400981 testContext->makeCurrent();
982 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400983
Brian Osmanceb7a422017-06-21 15:10:33 -0400984 otherTestContext->makeCurrent();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400985 surface->flushAndSubmit();
Greg Daniel3f475d92017-07-25 16:26:35 -0400986
Greg Daniel26b50a42018-03-08 09:49:58 -0500987 // This is specifically here for vulkan to guarantee the command buffer will finish
988 // which is when we call the ReleaseProc.
Robert Phillips9da87e02019-02-04 13:26:26 -0500989 otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400990 }
Brian Osman13dddce2017-05-09 13:19:50 -0400991
Brian Osmanceb7a422017-06-21 15:10:33 -0400992 // Case #6: Verify that only one context can be using the image at a time
993 {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400994 // Suppress warnings about trying to use a texture in two contexts.
Chris Dalton5a5fe792020-02-15 11:41:30 -0700995 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(otherCtx);
996
Brian Osmanceb7a422017-06-21 15:10:33 -0400997 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400998 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400999
Brian Osmanceb7a422017-06-21 15:10:33 -04001000 // Any context should be able to borrow the texture at this point
Robert Phillipsb25e0652020-07-22 09:35:49 -04001001 GrSurfaceProxyView view = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001002 REPORTER_ASSERT(reporter, view);
Brian Osman13dddce2017-05-09 13:19:50 -04001003
Brian Osmanceb7a422017-06-21 15:10:33 -04001004 // But once it's borrowed, no other context should be able to borrow
1005 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001006 GrSurfaceProxyView otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001007 REPORTER_ASSERT(reporter, !otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -04001008
1009 // Original context (that's already borrowing) should be okay
1010 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001011 GrSurfaceProxyView viewSecondRef = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001012 REPORTER_ASSERT(reporter, viewSecondRef);
Brian Osmanceb7a422017-06-21 15:10:33 -04001013
Greg Danielabba9982018-02-01 10:07:57 -05001014 // Release first ref from the original context
Greg Danielfebdedf2020-02-05 17:06:27 -05001015 view.reset();
Greg Danielabba9982018-02-01 10:07:57 -05001016
1017 // We released one proxy but not the other from the current borrowing context. Make sure
1018 // a new context is still not able to borrow the texture.
1019 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001020 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001021 REPORTER_ASSERT(reporter, !otherView);
Greg Danielabba9982018-02-01 10:07:57 -05001022
1023 // Release second ref from the original context
1024 testContext->makeCurrent();
Greg Danielfebdedf2020-02-05 17:06:27 -05001025 viewSecondRef.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001026
Greg Daniel48661b82018-01-22 16:11:35 -05001027 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001028 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001029 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001030 REPORTER_ASSERT(reporter, otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -04001031
1032 // Release everything
Greg Danielfebdedf2020-02-05 17:06:27 -05001033 otherView.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001034 refImg.reset(nullptr);
1035 }
Brian Osman13dddce2017-05-09 13:19:50 -04001036 }
1037}
1038
Brian Salomondcfca432017-11-15 15:48:03 -05001039DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001040 SkBitmap bitmap;
1041 SkPixmap pixmap;
Hal Canarybaa2a282018-11-26 15:34:12 -05001042 if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
1043 ERRORF(reporter, "missing resource");
1044 return;
1045 }
Hal Canaryf7d3f612018-03-22 15:17:42 -04001046 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
Robert Phillipsb25e0652020-07-22 09:35:49 -04001047 [&pixmap](GrDirectContext* dContext) {
1048 return SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman63bc48d2017-11-07 10:37:00 -05001049 });
1050}
1051
Brian Osman052ef692018-03-27 09:56:31 -04001052DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
1053
1054 for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
1055 SkAutoPixmapStorage pixmap;
1056 pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
1057
1058 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
1059 GrContextFactory testFactory(options);
1060 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
1061 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001062 auto dContext = ctxInfo.directContext();
1063 if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001064 continue;
1065 }
1066
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001067 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman052ef692018-03-27 09:56:31 -04001068 REPORTER_ASSERT(reporter, image);
1069
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001070 GrSurfaceProxyView view = as_IB(image)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001071 REPORTER_ASSERT(reporter, view);
Brian Osman052ef692018-03-27 09:56:31 -04001072
1073 bool expectAlpha = kAlpha_8_SkColorType == ct;
Greg Daniela4828a12019-10-11 13:51:02 -04001074 GrColorType grCT = SkColorTypeToGrColorType(image->colorType());
1075 REPORTER_ASSERT(reporter, expectAlpha == GrColorTypeIsAlphaOnly(grCT));
Brian Osman052ef692018-03-27 09:56:31 -04001076 }
1077 }
1078}
1079
Eric Karl914a36b2017-10-12 12:44:50 -07001080DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001081 auto context = ctxInfo.directContext();
Eric Karl914a36b2017-10-12 12:44:50 -07001082 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
1083 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
1084
1085 GrContextFactory otherFactory;
1086 ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
1087
1088 testContext->makeCurrent();
1089 REPORTER_ASSERT(reporter, proxy);
1090 auto createLarge = [context] {
Robert Phillips9da87e02019-02-04 13:26:26 -05001091 return create_image_large(context->priv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001092 };
John Stilesbd3ffa42020-07-30 20:24:57 -04001093 struct TestCase {
1094 std::function<sk_sp<SkImage>()> fImageFactory;
1095 bool fExpectation;
1096 bool fCanTakeDirectly;
1097 };
1098 TestCase testCases[] = {
Eric Karl914a36b2017-10-12 12:44:50 -07001099 { create_image, true, false },
1100 { create_codec_image, true, false },
1101 { create_data_image, true, false },
1102 { create_picture_image, true, false },
1103 { [context] { return create_gpu_image(context); }, true, true },
Robert Phillipse94b4e12020-07-23 13:54:35 -04001104 // Create a texture image in a another context.
Brian Salomon55ad7742017-11-17 09:25:23 -05001105 { [otherContextInfo] {
1106 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -04001107 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.directContext());
1108 otherContextInfo.directContext()->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001109 return otherContextImage;
1110 }, false, false },
1111 // Create an image that is too large to be texture backed.
1112 { createLarge, false, false }
1113 };
1114
John Stilesbd3ffa42020-07-30 20:24:57 -04001115 for (const TestCase& testCase : testCases) {
Eric Karl914a36b2017-10-12 12:44:50 -07001116 sk_sp<SkImage> image(testCase.fImageFactory());
1117 if (!image) {
1118 ERRORF(reporter, "Failed to create image!");
1119 continue;
1120 }
1121
Robert Phillipsba375a82018-04-11 10:08:06 -04001122 GrBackendTexture origBackend = image->getBackendTexture(true);
1123 if (testCase.fCanTakeDirectly) {
1124 SkASSERT(origBackend.isValid());
1125 }
1126
1127 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001128 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001129 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1130 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001131 if (result != testCase.fExpectation) {
1132 static const char *const kFS[] = { "fail", "succeed" };
1133 ERRORF(reporter, "This image was expected to %s but did not.",
1134 kFS[testCase.fExpectation]);
1135 }
1136
Robert Phillipsba375a82018-04-11 10:08:06 -04001137 if (result) {
1138 SkASSERT(newBackend.isValid());
1139 }
1140
1141 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001142 if (testCase.fCanTakeDirectly != tookDirectly) {
1143 static const char *const kExpectedState[] = { "not expected", "expected" };
1144 ERRORF(reporter, "This backend texture was %s to be taken directly.",
1145 kExpectedState[testCase.fCanTakeDirectly]);
1146 }
1147
Greg Daniel0a2464f2020-05-14 15:45:44 -04001148 context->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001149 }
1150}
reedeb560282016-07-26 19:42:04 -07001151
1152///////////////////////////////////////////////////////////////////////////////////////////////////
1153
Matt Sarett0bb6f382017-03-06 10:28:24 -05001154static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1155 SkPictureRecorder recorder;
1156 SkCanvas* canvas = recorder.beginRecording(10, 10);
1157 canvas->clear(SK_ColorCYAN);
1158 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1159 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1160};
1161
1162DEF_TEST(Image_ColorSpace, r) {
1163 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001164 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001165 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1166
Hal Canaryc465d132017-12-08 10:21:31 -05001167 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Brian Osman82ebe042019-01-04 17:03:00 -05001168 skcms_TransferFunction fn;
Matt Sarett0bb6f382017-03-06 10:28:24 -05001169 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1170 REPORTER_ASSERT(r, success);
Brian Osman82ebe042019-01-04 17:03:00 -05001171 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
Matt Sarett0bb6f382017-03-06 10:28:24 -05001172
Brian Osman82ebe042019-01-04 17:03:00 -05001173 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
1174 SkNamedGamut::kRec2020);
Matt Sarett0bb6f382017-03-06 10:28:24 -05001175 image = create_picture_image(rec2020);
1176 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1177
1178 SkBitmap bitmap;
1179 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1180 bitmap.allocPixels(info);
Mike Reeddc607e32020-12-23 11:50:36 -05001181 image = bitmap.asImage();
Matt Sarett0bb6f382017-03-06 10:28:24 -05001182 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1183
1184 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1185 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1186 image = surface->makeImageSnapshot();
1187 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1188
1189 surface = SkSurface::MakeRaster(info);
1190 image = surface->makeImageSnapshot();
1191 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1192}
1193
Matt Sarett6de13102017-03-14 14:10:48 -04001194DEF_TEST(Image_makeColorSpace, r) {
Mike Kleinb147ace2020-01-16 11:11:06 -06001195 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Brian Osman82ebe042019-01-04 17:03:00 -05001196 skcms_TransferFunction fn;
1197 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;
1198 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB);
Matt Sarett6de13102017-03-14 14:10:48 -04001199
1200 SkBitmap srgbBitmap;
1201 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1202 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1203 srgbBitmap.setImmutable();
Mike Reeddc607e32020-12-23 11:50:36 -05001204 sk_sp<SkImage> srgbImage = srgbBitmap.asImage();
Adlai Holler3a220172020-07-15 10:37:50 -04001205 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, nullptr);
Matt Sarett6de13102017-03-14 14:10:48 -04001206 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001207 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001208
1209 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1210
Matt Sarett6de13102017-03-14 14:10:48 -04001211 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001212 REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1213 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1214 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1215
Adlai Holler3a220172020-07-15 10:37:50 -04001216 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, nullptr);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001217 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001218 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001219 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001220 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1221 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1222 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1223
Hal Canaryc465d132017-12-08 10:21:31 -05001224 srgbImage = GetResourceAsImage("images/1x1.png");
Adlai Holler3a220172020-07-15 10:37:50 -04001225 p3Image = srgbImage->makeColorSpace(p3, nullptr);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001226 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001227 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001228 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1229 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1230 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1231}
1232
Matt Sarett0bb6f382017-03-06 10:28:24 -05001233///////////////////////////////////////////////////////////////////////////////////////////////////
1234
reedeb560282016-07-26 19:42:04 -07001235static void make_all_premul(SkBitmap* bm) {
1236 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1237 for (int a = 0; a < 256; ++a) {
1238 for (int r = 0; r < 256; ++r) {
1239 // make all valid premul combinations
Brian Osman788b9162020-02-07 10:36:46 -05001240 int c = std::min(a, r);
reedeb560282016-07-26 19:42:04 -07001241 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1242 }
1243 }
1244}
1245
1246static bool equal(const SkBitmap& a, const SkBitmap& b) {
1247 SkASSERT(a.width() == b.width());
1248 SkASSERT(a.height() == b.height());
1249 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001250 for (int x = 0; x < a.width(); ++x) {
1251 SkPMColor pa = *a.getAddr32(x, y);
1252 SkPMColor pb = *b.getAddr32(x, y);
1253 if (pa != pb) {
1254 return false;
1255 }
reedeb560282016-07-26 19:42:04 -07001256 }
1257 }
1258 return true;
1259}
1260
1261DEF_TEST(image_roundtrip_encode, reporter) {
1262 SkBitmap bm0;
1263 make_all_premul(&bm0);
1264
Mike Reeddc607e32020-12-23 11:50:36 -05001265 auto img0 = bm0.asImage();
Mike Reed6409f842017-07-11 16:03:13 -04001266 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001267 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001268
reedeb560282016-07-26 19:42:04 -07001269 SkBitmap bm1;
1270 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001271 img1->readPixels(nullptr, bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001272
reedeb560282016-07-26 19:42:04 -07001273 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1274}
1275
1276DEF_TEST(image_roundtrip_premul, reporter) {
1277 SkBitmap bm0;
1278 make_all_premul(&bm0);
1279
1280 SkBitmap bm1;
1281 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1282 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1283
1284 SkBitmap bm2;
1285 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1286 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1287
1288 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1289}
Brian Osmanb8a13922017-04-26 15:28:30 -04001290
1291///////////////////////////////////////////////////////////////////////////////////////////////////
1292
1293static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
1294 // Verify that all pixels contain the original test color
1295 for (auto y = 0; y < pmap->height(); ++y) {
1296 for (auto x = 0; x < pmap->width(); ++x) {
1297 uint32_t pixel = *pmap->addr32(x, y);
1298 if (pixel != expected) {
1299 ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
1300 x, y, pixel, expected);
1301 return;
1302 }
1303 }
1304 }
1305}
1306
1307static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
1308 uint32_t expected) {
1309 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
1310
1311 // Make sure to test kDisallow first, so we don't just get a cache hit in that case
1312 for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
1313 SkAutoPixmapStorage scaled;
1314 scaled.alloc(info);
Mike Reed5ec22382021-01-14 21:59:01 -05001315 if (!image->scalePixels(scaled, SkSamplingOptions(SkFilterMode::kLinear), chint)) {
Brian Osmanb8a13922017-04-26 15:28:30 -04001316 ERRORF(reporter, "Failed to scale image");
1317 continue;
1318 }
1319
1320 check_scaled_pixels(reporter, &scaled, expected);
1321 }
1322}
1323
1324DEF_TEST(ImageScalePixels, reporter) {
1325 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1326 const SkColor red = SK_ColorRED;
1327
1328 // Test raster image
1329 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1330 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
1331 surface->getCanvas()->clear(red);
1332 sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
1333 test_scale_pixels(reporter, rasterImage.get(), pmRed);
1334
1335 // Test encoded image
Mike Reed6409f842017-07-11 16:03:13 -04001336 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001337 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1338 test_scale_pixels(reporter, codecImage.get(), pmRed);
1339}
1340
Brian Osmanb8a13922017-04-26 15:28:30 -04001341DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
1342 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1343 const SkColor red = SK_ColorRED;
1344
1345 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
Robert Phillips6d344c32020-07-06 10:56:46 -04001346 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.directContext(),
1347 SkBudgeted::kNo, info);
Brian Osmanb8a13922017-04-26 15:28:30 -04001348 surface->getCanvas()->clear(red);
1349 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1350 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1351}
Mike Reedc4e31092018-01-30 11:15:27 -05001352
1353static sk_sp<SkImage> any_image_will_do() {
1354 return GetResourceAsImage("images/mandrill_32.png");
1355}
1356
1357DEF_TEST(Image_nonfinite_dst, reporter) {
1358 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
1359 auto img = any_image_will_do();
Mike Reedc4e31092018-01-30 11:15:27 -05001360
1361 for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
1362 for (int bits = 1; bits <= 15; ++bits) {
1363 SkRect dst = { 0, 0, 10, 10 };
1364 if (bits & 1) dst.fLeft = bad;
1365 if (bits & 2) dst.fTop = bad;
1366 if (bits & 4) dst.fRight = bad;
1367 if (bits & 8) dst.fBottom = bad;
1368
Mike Reed039f1362021-01-27 21:21:08 -05001369 surf->getCanvas()->drawImageRect(img, dst, SkSamplingOptions());
Mike Reedc4e31092018-01-30 11:15:27 -05001370
1371 // we should draw nothing
Mike Kleinea3f0142019-03-20 11:12:10 -05001372 ToolUtils::PixelIter iter(surf.get());
Mike Reedc4e31092018-01-30 11:15:27 -05001373 while (void* addr = iter.next()) {
1374 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1375 }
1376 }
1377 }
1378}
1379
Robert Phillipsb25e0652020-07-22 09:35:49 -04001380static sk_sp<SkImage> make_yuva_image(GrDirectContext* dContext) {
Brian Salomonad8efda2019-05-10 09:22:49 -04001381 SkAutoPixmapStorage pm;
1382 pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
Brian Salomone4387382020-11-11 16:34:19 -05001383 SkYUVAInfo yuvaInfo({1, 1},
1384 SkYUVAInfo::PlaneConfig::kY_U_V,
1385 SkYUVAInfo::Subsampling::k444,
1386 kJPEG_Full_SkYUVColorSpace);
Brian Salomonc2a9a972020-09-15 11:24:28 -04001387 const SkPixmap pmaps[] = {pm, pm, pm};
1388 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001389
Brian Salomonc2a9a972020-09-15 11:24:28 -04001390 return SkImage::MakeFromYUVAPixmaps(dContext, yuvaPixmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001391}
1392
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001393DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -04001394 auto dContext = ctxInfo.directContext();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001395 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001396 auto s = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, ii, 1, nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001397
1398 s->getCanvas()->clear(SK_ColorRED);
1399 auto i0 = s->makeImageSnapshot();
1400 s->getCanvas()->clear(SK_ColorBLUE);
1401 auto i1 = s->makeImageSnapshot();
1402 s->getCanvas()->clear(SK_ColorGREEN);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001403 // Make a YUVA image.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001404 auto i2 = make_yuva_image(dContext);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001405
1406 // Flush all the setup work we did above and then make little lambda that reports the flush
1407 // count delta since the last time it was called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001408 dContext->flushAndSubmit();
1409 auto numSubmits =
1410 [dContext,
1411 submitCnt = dContext->priv().getGpu()->stats()->numSubmitToGpus()]() mutable {
1412 int curr = dContext->priv().getGpu()->stats()->numSubmitToGpus();
Greg Daniel04283f32020-05-20 13:16:00 -04001413 int n = curr - submitCnt;
1414 submitCnt = curr;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001415 return n;
1416 };
1417
Greg Daniel04283f32020-05-20 13:16:00 -04001418 // Images aren't used therefore flush is ignored, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001419 i0->flushAndSubmit(dContext);
1420 i1->flushAndSubmit(dContext);
1421 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001422 REPORTER_ASSERT(reporter, numSubmits() == 3);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001423
1424 // Syncing forces the flush to happen even if the images aren't used.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001425 i0->flush(dContext);
1426 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001427 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001428 i1->flush(dContext);
1429 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001430 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001431 i2->flush(dContext);
1432 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001433 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001434
1435 // Use image 1
1436 s->getCanvas()->drawImage(i1, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001437 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001438 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001439 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001440 // Flushing image 1 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001441 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001442 REPORTER_ASSERT(reporter, numSubmits() == 1);
1443 // Flushing image 2 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001444 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001445 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001446
1447 // Use image 2
1448 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001449 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001450 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001451 REPORTER_ASSERT(reporter, numSubmits() == 1);
1452 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001453 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001454 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001455 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001456 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001457 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001458 // Since we just did a simple image draw it should not have been flattened.
1459 REPORTER_ASSERT(reporter,
1460 !static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
Brian Salomonad8efda2019-05-10 09:22:49 -04001461 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1462
1463 // Flatten it and repeat.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001464 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001465 REPORTER_ASSERT(reporter,
1466 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1467 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1468 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001469 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001470 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001471 REPORTER_ASSERT(reporter, numSubmits() == 1);
1472 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001473 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001474 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001475 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001476 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001477 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001478
1479 // Test case where flatten happens before the first flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001480 i2 = make_yuva_image(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001481 // On some systems where preferVRAMUseOverFlushes is false (ANGLE on Windows) the above may
1482 // actually flush in order to make textures for the YUV planes. TODO: Remove this when we
Robert Phillipse94b4e12020-07-23 13:54:35 -04001483 // make the YUVA planes from backend textures rather than pixmaps that the context must upload.
Greg Daniel04283f32020-05-20 13:16:00 -04001484 // Calling numSubmits rebases the flush count from here.
1485 numSubmits();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001486 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001487 REPORTER_ASSERT(reporter,
1488 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1489 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1490 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001491 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001492 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001493 REPORTER_ASSERT(reporter, numSubmits() == 1);
1494 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001495 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001496 REPORTER_ASSERT(reporter, numSubmits() == 1);
1497 // Flushing image 2 should flush, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001498 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001499 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001500}
Mike Reed3d30ca62020-07-22 16:55:02 -04001501
1502#include "src/shaders/SkImageShader.h"
1503
1504constexpr SkM44 gCentripetalCatmulRom
1505 (0.0f/2, -1.0f/2, 2.0f/2, -1.0f/2,
1506 2.0f/2, 0.0f/2, -5.0f/2, 3.0f/2,
1507 0.0f/2, 1.0f/2, 4.0f/2, -3.0f/2,
1508 0.0f/2, 0.0f/2, -1.0f/2, 1.0f/2);
1509
1510constexpr SkM44 gMitchellNetravali
1511 ( 1.0f/18, -9.0f/18, 15.0f/18, -7.0f/18,
1512 16.0f/18, 0.0f/18, -36.0f/18, 21.0f/18,
1513 1.0f/18, 9.0f/18, 27.0f/18, -21.0f/18,
1514 0.0f/18, 0.0f/18, -6.0f/18, 7.0f/18);
1515
1516DEF_TEST(image_cubicresampler, reporter) {
1517 auto diff = [reporter](const SkM44& a, const SkM44& b) {
1518 const float tolerance = 0.000001f;
1519 for (int r = 0; r < 4; ++r) {
1520 for (int c = 0; c < 4; ++c) {
1521 float d = std::abs(a.rc(r, c) - b.rc(r, c));
1522 REPORTER_ASSERT(reporter, d <= tolerance);
1523 }
1524 }
1525 };
1526
1527 diff(SkImageShader::CubicResamplerMatrix(1.0f/3, 1.0f/3), gMitchellNetravali);
1528
1529 diff(SkImageShader::CubicResamplerMatrix(0, 1.0f/2), gCentripetalCatmulRom);
1530}
Mike Reedcd043562020-07-29 13:55:45 -04001531
1532DEF_TEST(image_subset_encode_skbug_7752, reporter) {
1533 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_128.png");
1534 const int W = image->width();
1535 const int H = image->height();
1536
1537 auto check_roundtrip = [&](sk_sp<SkImage> img) {
1538 auto img2 = SkImage::MakeFromEncoded(img->encodeToData());
1539 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), img2.get()));
1540 };
1541 check_roundtrip(image); // should trivially pass
1542 check_roundtrip(image->makeSubset({0, 0, W/2, H/2}));
1543 check_roundtrip(image->makeSubset({W/2, H/2, W, H}));
1544 check_roundtrip(image->makeColorSpace(SkColorSpace::MakeSRGBLinear()));
1545}