blob: 57ce3fe8527788f6ff8ea8a7483168c3b1d49e23 [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);
233 canvas->drawImage(image, 0, 0);
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
reed9ce9d672016-03-17 10:51:11 -0700317 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
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 Salomon7e67dca2020-07-21 09:27:25 -0400398 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;
406 if (auto sp = as_IB(image)->peekProxy()) {
407 origProxy = sp->asTextureProxy();
408 SkASSERT(origProxy);
409 }
410 for (auto budgeted : {SkBudgeted::kNo, SkBudgeted::kYes}) {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400411 auto texImage = image->makeTextureImage(dContext, mipMapped, budgeted);
Brian Salomonbc074a62020-03-18 10:06:13 -0400412 if (!texImage) {
Adlai Holler302e8fb2020-09-14 11:58:06 -0400413 auto imageContext = as_IB(image)->context();
Robert Phillipse94b4e12020-07-23 13:54:35 -0400414 // We expect to fail if image comes from a different context
Robert Phillips326f1d72020-10-01 09:43:29 -0400415 if (!image->isTextureBacked() || imageContext->priv().matches(dContext)) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400416 ERRORF(reporter, "makeTextureImage failed.");
417 }
418 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500419 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400420 if (!texImage->isTextureBacked()) {
421 ERRORF(reporter, "makeTextureImage returned non-texture image.");
422 continue;
423 }
424 GrTextureProxy* copyProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
425 SkASSERT(copyProxy);
426 bool shouldBeMipped =
Robert Phillipsb25e0652020-07-22 09:35:49 -0400427 mipMapped == GrMipmapped::kYes && dContext->priv().caps()->mipmapSupport();
Brian Salomon8c82a872020-07-21 12:09:58 -0400428 if (shouldBeMipped && copyProxy->mipmapped() == GrMipmapped::kNo) {
Brian Salomonbc074a62020-03-18 10:06:13 -0400429 ERRORF(reporter, "makeTextureImage returned non-mipmapped texture.");
430 continue;
431 }
Brian Salomon8c82a872020-07-21 12:09:58 -0400432 bool origIsMipped = origProxy && origProxy->mipmapped() == GrMipmapped::kYes;
Brian Salomonbc074a62020-03-18 10:06:13 -0400433 if (image->isTextureBacked() && (!shouldBeMipped || origIsMipped)) {
434 if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
Brian Osmand566e2e2019-08-14 13:19:04 -0400435 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400436 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400437 } else {
438 auto* texProxy = as_IB(texImage)->peekProxy()->asTextureProxy();
439 REPORTER_ASSERT(reporter, !texProxy->getUniqueKey().isValid());
440 REPORTER_ASSERT(reporter, texProxy->isBudgeted() == budgeted);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400441 }
Brian Salomonbc074a62020-03-18 10:06:13 -0400442 if (image->width() != texImage->width() || image->height() != texImage->height()) {
443 ERRORF(reporter, "makeTextureImage changed the image size.");
444 }
445 if (image->alphaType() != texImage->alphaType()) {
446 ERRORF(reporter, "makeTextureImage changed image alpha type.");
447 }
Brian Osmand566e2e2019-08-14 13:19:04 -0400448 }
Brian Osman041f7df2017-02-07 11:23:28 -0500449 }
450 }
Robert Phillipsb25e0652020-07-22 09:35:49 -0400451 dContext->flushAndSubmit();
Brian Osman041f7df2017-02-07 11:23:28 -0500452}
453
bsalomon634b4302016-07-12 18:11:17 -0700454DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400455 auto dContext = contextInfo.directContext();
bsalomon634b4302016-07-12 18:11:17 -0700456
457 std::function<sk_sp<SkImage>()> imageFactories[] = {
458 create_image,
459 create_codec_image,
460 create_data_image,
461 create_picture_image,
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400462 [dContext] { return create_gpu_image(dContext); },
bsalomon634b4302016-07-12 18:11:17 -0700463 };
John Stilesbd3ffa42020-07-30 20:24:57 -0400464 for (const auto& factory : imageFactories) {
bsalomon634b4302016-07-12 18:11:17 -0700465 sk_sp<SkImage> image = factory();
466 if (!image->isTextureBacked()) {
467 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400468 if (!(image = image->makeTextureImage(dContext))) {
Brian Osman041f7df2017-02-07 11:23:28 -0500469 continue;
470 }
bsalomon634b4302016-07-12 18:11:17 -0700471 }
472 auto rasterImage = image->makeNonTextureImage();
473 if (!rasterImage) {
474 ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
475 }
476 REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400477 assert_equal(reporter, dContext, image.get(), nullptr, rasterImage.get());
bsalomon634b4302016-07-12 18:11:17 -0700478 }
479}
480
Brian Salomonbdecacf2018-02-02 20:32:49 -0500481DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +0000482 auto dContext = ctxInfo.directContext();
Robert Phillips9b16f812019-05-17 10:01:21 -0400483
484 static constexpr int kSize = 10;
485
Brian Salomonbdecacf2018-02-02 20:32:49 -0500486 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
Brian Salomonbdecacf2018-02-02 20:32:49 -0500487 SkColorType colorType = static_cast<SkColorType>(ct);
Adlai Holler14dc7912020-08-11 15:48:49 +0000488 bool can = dContext->colorTypeSupportedAsImage(colorType);
Robert Phillips9b16f812019-05-17 10:01:21 -0400489
Brian Salomon72050802020-10-12 20:45:06 -0400490 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(
491 dContext, kSize, kSize, colorType, GrMipmapped::kNo, GrRenderable::kNo);
Brian Salomon88d7e622020-11-05 11:11:20 -0500492 sk_sp<SkImage> img;
493 if (mbet) {
494 img = SkImage::MakeFromTexture(dContext, mbet->texture(), kTopLeft_GrSurfaceOrigin,
495 colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomon72050802020-10-12 20:45:06 -0400496 }
Brian Salomonbdecacf2018-02-02 20:32:49 -0500497 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300498 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500499 colorType);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500500 }
501}
502
Brian Salomon9708af82018-02-05 12:57:10 -0500503DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
504 SkBitmap bmp;
505 bmp.allocPixels(
506 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
507 for (int y = 0; y < 256; ++y) {
508 for (int x = 0; x < 256; ++x) {
509 *bmp.getAddr32(x, y) =
510 SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
511 }
512 }
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400513 auto dContext = ctxInfo.directContext();
514 auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(dContext);
Brian Salomon9708af82018-02-05 12:57:10 -0500515 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
516 ERRORF(reporter, "Failed to make unpremul texture image.");
517 return;
518 }
Brian Salomon1d435302019-07-01 13:05:28 -0400519 SkBitmap unpremul;
520 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
521 kUnpremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400522 if (!texImage->readPixels(dContext, unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(),
523 0, 0)) {
Brian Salomon1d435302019-07-01 13:05:28 -0400524 ERRORF(reporter, "Unpremul readback failed.");
525 return;
526 }
527 for (int y = 0; y < 256; ++y) {
528 for (int x = 0; x < 256; ++x) {
529 if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
530 ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
531 *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
532 return;
Brian Salomon9708af82018-02-05 12:57:10 -0500533 }
534 }
535 }
536 SkBitmap premul;
537 premul.allocPixels(
538 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400539 if (!texImage->readPixels(dContext, premul.info(), premul.getPixels(), premul.rowBytes(),
540 0, 0)) {
Brian Salomon9708af82018-02-05 12:57:10 -0500541 ERRORF(reporter, "Unpremul readback failed.");
542 return;
543 }
544 for (int y = 0; y < 256; ++y) {
545 for (int x = 0; x < 256; ++x) {
Brian Salomon1d435302019-07-01 13:05:28 -0400546 uint32_t origColor = *bmp.getAddr32(x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500547 int32_t origA = (origColor >> 24) & 0xff;
Brian Salomon1d435302019-07-01 13:05:28 -0400548 float a = origA / 255.f;
549 int32_t origB = sk_float_round2int(((origColor >> 16) & 0xff) * a);
550 int32_t origG = sk_float_round2int(((origColor >> 8) & 0xff) * a);
551 int32_t origR = sk_float_round2int(((origColor >> 0) & 0xff) * a);
552
Brian Salomon9708af82018-02-05 12:57:10 -0500553 uint32_t read = *premul.getAddr32(x, y);
554 int32_t readA = (read >> 24) & 0xff;
555 int32_t readB = (read >> 16) & 0xff;
556 int32_t readG = (read >> 8) & 0xff;
557 int32_t readR = (read >> 0) & 0xff;
558 // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
559 // bit of tolerance for GPU vs CPU premul math.
560 int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
561 if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
562 SkTAbs(readR - origR) > tol) {
Brian Salomon1d435302019-07-01 13:05:28 -0400563 ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) expected(0x%08x) at %d, %d.",
564 *bmp.getAddr32(x, y), *premul.getAddr32(x, y), origColor, x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500565 return;
566 }
567 }
568 }
569}
570
Brian Salomon8a8dd332018-05-24 14:08:31 -0400571DEF_GPUTEST(AbandonedContextImage, reporter, options) {
572 using Factory = sk_gpu_test::GrContextFactory;
573 for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
574 auto type = static_cast<Factory::ContextType>(ct);
575 std::unique_ptr<Factory> factory(new Factory);
576 if (!factory->get(type)) {
577 continue;
578 }
579
580 sk_sp<SkImage> img;
581 auto gsurf = SkSurface::MakeRenderTarget(
582 factory->get(type), SkBudgeted::kYes,
583 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
584 nullptr);
585 if (!gsurf) {
586 continue;
587 }
588 img = gsurf->makeImageSnapshot();
589 gsurf.reset();
590
591 auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
592
593 REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400594 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400595
596 factory->get(type)->abandonContext();
597 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400598 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400599 // This shouldn't crash.
600 rsurf->getCanvas()->drawImage(img, 0, 0);
601
Robert Phillipse94b4e12020-07-23 13:54:35 -0400602 // Give up all other refs on the context.
Brian Salomon8a8dd332018-05-24 14:08:31 -0400603 factory.reset(nullptr);
Adlai Holler302e8fb2020-09-14 11:58:06 -0400604 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400605 // This shouldn't crash.
606 rsurf->getCanvas()->drawImage(img, 0, 0);
607 }
608}
609
kkinnunen4e184132015-11-17 22:53:28 -0800610class EmptyGenerator : public SkImageGenerator {
611public:
612 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
613};
614
kkinnunen7b94c142015-11-24 07:39:40 -0800615DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800616 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700617 SkPixmap pmap(info, nullptr, 0);
618 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
619 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
620 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500621 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500622 std::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800623}
624
kkinnunen7b94c142015-11-24 07:39:40 -0800625DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800626 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
627 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400628 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700629 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800630 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700631 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800632 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700633 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800634 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800635}
636
kkinnunen4e184132015-11-17 22:53:28 -0800637static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
638 for (int i = 0; i < count; ++i) {
639 if (pixels[i] != expected) {
640 return false;
641 }
642 }
643 return true;
644}
645
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400646static void image_test_read_pixels(GrDirectContext* dContext, skiatest::Reporter* reporter,
647 SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700648 if (!image) {
649 ERRORF(reporter, "Failed to create image!");
650 return;
651 }
kkinnunen7b94c142015-11-24 07:39:40 -0800652 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800653 const SkPMColor notExpected = ~expected;
654
655 const int w = 2, h = 2;
656 const size_t rowBytes = w * sizeof(SkPMColor);
657 SkPMColor pixels[w*h];
658
659 SkImageInfo info;
660
661 info = SkImageInfo::MakeUnknown(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400662 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800663
664 // out-of-bounds should fail
665 info = SkImageInfo::MakeN32Premul(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400666 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, -w, 0));
667 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, -h));
668 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
669 image->width(), 0));
670 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
671 0, image->height()));
kkinnunen4e184132015-11-17 22:53:28 -0800672
673 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800674 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400675 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800676 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
677
678 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800679 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400680 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800681 image->width() - w, image->height() - h));
682 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
683
684 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800685 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400686 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, -1, -1));
kkinnunen4e184132015-11-17 22:53:28 -0800687 REPORTER_ASSERT(reporter, pixels[3] == expected);
688 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
689
690 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800691 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400692 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800693 image->width() - 1, image->height() - 1));
694 REPORTER_ASSERT(reporter, pixels[0] == expected);
695 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
696}
kkinnunen7b94c142015-11-24 07:39:40 -0800697DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700698 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400699 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800700
reed9ce9d672016-03-17 10:51:11 -0700701 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400702 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800703
704 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700705 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400706 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800707 image.reset();
708 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
709
reed9ce9d672016-03-17 10:51:11 -0700710 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400711 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800712}
egdanielab527a52016-06-28 08:07:26 -0700713DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400714 auto dContext = ctxInfo.directContext();
715 image_test_read_pixels(dContext, reporter, create_gpu_image(dContext).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800716}
kkinnunen4e184132015-11-17 22:53:28 -0800717
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400718static void check_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
719 const SkImage* image, const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800720 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
721 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700722 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800723
Cary Clark4f5a79c2018-02-07 15:51:00 -0500724 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800725
kkinnunen4e184132015-11-17 22:53:28 -0800726 REPORTER_ASSERT(reporter, bitmap.getPixels());
727
728 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
729 SkPMColor imageColor;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400730 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, &imageColor, sizeof(SkPMColor),
731 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800732 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
733}
734
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400735static void test_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
736 const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700737 if (!image) {
738 ERRORF(reporter, "Failed to create image.");
739 return;
740 }
kkinnunen7b94c142015-11-24 07:39:40 -0800741 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500742 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400743 check_legacy_bitmap(reporter, dContext, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800744
745 // Test subsetting to exercise the rowBytes logic.
746 SkBitmap tmp;
747 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
748 image->height() / 2)));
reed9ce9d672016-03-17 10:51:11 -0700749 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
750 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800751
752 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500753 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400754 check_legacy_bitmap(reporter, nullptr, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800755}
756DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500757 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400758 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800759
Cary Clark4f5a79c2018-02-07 15:51:00 -0500760 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400761 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800762
Cary Clark4f5a79c2018-02-07 15:51:00 -0500763 RasterDataHolder dataHolder;
764 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400765 test_legacy_bitmap(reporter, nullptr, image.get());
Cary Clark4f5a79c2018-02-07 15:51:00 -0500766 image.reset();
767 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800768
Cary Clark4f5a79c2018-02-07 15:51:00 -0500769 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400770 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800771}
bsalomon68d91342016-04-12 09:59:58 -0700772DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400773 auto dContext = ctxInfo.directContext();
774 sk_sp<SkImage> image(create_gpu_image(dContext));
775 test_legacy_bitmap(reporter, dContext, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800776}
kkinnunen4e184132015-11-17 22:53:28 -0800777
kkinnunen7b94c142015-11-24 07:39:40 -0800778static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700779 if (!image) {
780 ERRORF(reporter, "Failed to create image!");
781 return;
782 }
reed6ceeebd2016-03-09 14:26:26 -0800783 SkPixmap pm;
784 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800785 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
786 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800787 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800788 REPORTER_ASSERT(reporter, 20 == info.width());
789 REPORTER_ASSERT(reporter, 20 == info.height());
790 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
791 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
792 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800793 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
794 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800795 }
kkinnunen7b94c142015-11-24 07:39:40 -0800796}
797DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700798 sk_sp<SkImage> image(create_image());
799 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800800
reed9ce9d672016-03-17 10:51:11 -0700801 image = create_data_image();
802 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800803
804 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700805 image = create_rasterproc_image(&dataHolder);
806 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800807 image.reset();
808 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
809
reed9ce9d672016-03-17 10:51:11 -0700810 image = create_codec_image();
811 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800812}
egdanielab527a52016-06-28 08:07:26 -0700813DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400814 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
reed9ce9d672016-03-17 10:51:11 -0700815 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800816}
kkinnunen4e184132015-11-17 22:53:28 -0800817
kkinnunen7b94c142015-11-24 07:39:40 -0800818struct TextureReleaseChecker {
819 TextureReleaseChecker() : fReleaseCount(0) {}
820 int fReleaseCount;
821 static void Release(void* self) {
822 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800823 }
824};
Brian Osman13dddce2017-05-09 13:19:50 -0400825
brianosmandb2cb102016-07-22 07:22:04 -0700826DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
827 const int kWidth = 10;
828 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000829
Adlai Holler14dc7912020-08-11 15:48:49 +0000830 auto dContext = ctxInfo.directContext();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000831
Brian Salomon72050802020-10-12 20:45:06 -0400832 auto mbet = sk_gpu_test::ManagedBackendTexture::MakeWithoutData(dContext,
833 kWidth,
834 kHeight,
835 kRGBA_8888_SkColorType,
836 GrMipmapped::kNo,
837 GrRenderable::kNo,
838 GrProtected::kNo);
839 if (!mbet) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400840 ERRORF(reporter, "couldn't create backend texture\n");
Brian Salomon88d7e622020-11-05 11:11:20 -0500841 return;
Robert Phillipsee5fd132019-05-07 13:29:22 -0400842 }
brianosmandb2cb102016-07-22 07:22:04 -0700843
kkinnunen7b94c142015-11-24 07:39:40 -0800844 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000845 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon72050802020-10-12 20:45:06 -0400846 sk_sp<SkImage> refImg = SkImage::MakeFromTexture(
847 dContext,
848 mbet->texture(),
849 texOrigin,
850 kRGBA_8888_SkColorType,
851 kPremul_SkAlphaType,
852 /*color space*/nullptr,
853 sk_gpu_test::ManagedBackendTexture::ReleaseProc,
854 mbet->releaseContext(TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800855
Robert Phillips3390e152017-01-31 17:53:34 -0500856 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400857 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
Brian Salomon72050802020-10-12 20:45:06 -0400858 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, mbet->texture())) {
Robert Phillipsc5509952018-04-04 15:54:55 -0400859 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500860 }
Brian Salomon72050802020-10-12 20:45:06 -0400861 REPORTER_ASSERT(reporter,
862 GrBackendTexture::TestingOnly_Equals(readBackBackendTex, mbet->texture()));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000863 if (readBackOrigin != texOrigin) {
864 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500865 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000866 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500867
kkinnunen4e184132015-11-17 22:53:28 -0800868 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800869 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800870 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800871 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800872}
bsalomon0d996862016-03-09 18:44:43 -0800873
Brian Salomondcfca432017-11-15 15:48:03 -0500874static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400875 const char* testName,
Robert Phillipsb25e0652020-07-22 09:35:49 -0400876 std::function<sk_sp<SkImage>(GrDirectContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400877 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500878 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400879 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
880 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400881 auto dContext = ctxInfo.directContext();
882 if (!dContext) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400883 continue;
884 }
Brian Osman13dddce2017-05-09 13:19:50 -0400885
Brian Osmanceb7a422017-06-21 15:10:33 -0400886 // If we don't have proper support for this feature, the factory will fallback to returning
887 // codec-backed images. Those will "work", but some of our checks will fail because we
888 // expect the cross-context images not to work on multiple contexts at once.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400889 if (!dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400890 continue;
891 }
Brian Osman13dddce2017-05-09 13:19:50 -0400892
Brian Osmanceb7a422017-06-21 15:10:33 -0400893 // We test three lifetime patterns for a single context:
894 // 1) Create image, free image
895 // 2) Create image, draw, flush, free image
896 // 3) Create image, draw, free image, flush
897 // ... and then repeat the last two patterns with drawing on a second* context:
898 // 4) Create image, draw*, flush*, free image
899 // 5) Create image, draw*, free iamge, flush*
Brian Osman13dddce2017-05-09 13:19:50 -0400900
Brian Osmanceb7a422017-06-21 15:10:33 -0400901 // Case #1: Create image, free image
902 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400903 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osmanceb7a422017-06-21 15:10:33 -0400904 refImg.reset(nullptr); // force a release of the image
905 }
Brian Osman13dddce2017-05-09 13:19:50 -0400906
Brian Osmanceb7a422017-06-21 15:10:33 -0400907 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400908 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400909 if (!surface) {
910 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
911 continue;
912 }
913
Brian Osmanceb7a422017-06-21 15:10:33 -0400914 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400915
Brian Osmanceb7a422017-06-21 15:10:33 -0400916 // Case #2: Create image, draw, flush, free image
917 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400918 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400919
Brian Osmanceb7a422017-06-21 15:10:33 -0400920 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400921 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400922
Brian Osmanceb7a422017-06-21 15:10:33 -0400923 refImg.reset(nullptr); // force a release of the image
924 }
Brian Osman13dddce2017-05-09 13:19:50 -0400925
Brian Osmanceb7a422017-06-21 15:10:33 -0400926 // Case #3: Create image, draw, free image, flush
927 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400928 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400929
Brian Osmanceb7a422017-06-21 15:10:33 -0400930 canvas->drawImage(refImg, 0, 0);
931 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400932
Greg Daniel0a2464f2020-05-14 15:45:44 -0400933 surface->flushAndSubmit();
Brian Osmanceb7a422017-06-21 15:10:33 -0400934 }
Brian Osman13dddce2017-05-09 13:19:50 -0400935
Brian Osmanceb7a422017-06-21 15:10:33 -0400936 // Configure second context
937 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400938
Robert Phillipsb25e0652020-07-22 09:35:49 -0400939 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(dContext);
Robert Phillips6d344c32020-07-06 10:56:46 -0400940 auto otherCtx = otherContextInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400941 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400942
Brian Osmanceb7a422017-06-21 15:10:33 -0400943 // Creating a context in a share group may fail
944 if (!otherCtx) {
945 continue;
946 }
Brian Osman13dddce2017-05-09 13:19:50 -0400947
Brian Osmanceb7a422017-06-21 15:10:33 -0400948 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
949 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400950
Brian Osmanceb7a422017-06-21 15:10:33 -0400951 // Case #4: Create image, draw*, flush*, free image
952 {
953 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400954 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400955
Brian Osmanceb7a422017-06-21 15:10:33 -0400956 otherTestContext->makeCurrent();
957 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400958 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400959
Brian Osmanceb7a422017-06-21 15:10:33 -0400960 testContext->makeCurrent();
961 refImg.reset(nullptr); // force a release of the image
962 }
Brian Osman13dddce2017-05-09 13:19:50 -0400963
Brian Osmanceb7a422017-06-21 15:10:33 -0400964 // Case #5: Create image, draw*, free image, flush*
965 {
966 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400967 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400968
Brian Osmanceb7a422017-06-21 15:10:33 -0400969 otherTestContext->makeCurrent();
970 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400971
Brian Osmanceb7a422017-06-21 15:10:33 -0400972 testContext->makeCurrent();
973 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400974
Brian Osmanceb7a422017-06-21 15:10:33 -0400975 otherTestContext->makeCurrent();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400976 surface->flushAndSubmit();
Greg Daniel3f475d92017-07-25 16:26:35 -0400977
Greg Daniel26b50a42018-03-08 09:49:58 -0500978 // This is specifically here for vulkan to guarantee the command buffer will finish
979 // which is when we call the ReleaseProc.
Robert Phillips9da87e02019-02-04 13:26:26 -0500980 otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400981 }
Brian Osman13dddce2017-05-09 13:19:50 -0400982
Brian Osmanceb7a422017-06-21 15:10:33 -0400983 // Case #6: Verify that only one context can be using the image at a time
984 {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400985 // Suppress warnings about trying to use a texture in two contexts.
Chris Dalton5a5fe792020-02-15 11:41:30 -0700986 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(otherCtx);
987
Brian Osmanceb7a422017-06-21 15:10:33 -0400988 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400989 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400990
Brian Osmanceb7a422017-06-21 15:10:33 -0400991 // Any context should be able to borrow the texture at this point
Robert Phillipsb25e0652020-07-22 09:35:49 -0400992 GrSurfaceProxyView view = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500993 REPORTER_ASSERT(reporter, view);
Brian Osman13dddce2017-05-09 13:19:50 -0400994
Brian Osmanceb7a422017-06-21 15:10:33 -0400995 // But once it's borrowed, no other context should be able to borrow
996 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400997 GrSurfaceProxyView otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500998 REPORTER_ASSERT(reporter, !otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -0400999
1000 // Original context (that's already borrowing) should be okay
1001 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001002 GrSurfaceProxyView viewSecondRef = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001003 REPORTER_ASSERT(reporter, viewSecondRef);
Brian Osmanceb7a422017-06-21 15:10:33 -04001004
Greg Danielabba9982018-02-01 10:07:57 -05001005 // Release first ref from the original context
Greg Danielfebdedf2020-02-05 17:06:27 -05001006 view.reset();
Greg Danielabba9982018-02-01 10:07:57 -05001007
1008 // We released one proxy but not the other from the current borrowing context. Make sure
1009 // a new context is still not able to borrow the texture.
1010 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001011 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001012 REPORTER_ASSERT(reporter, !otherView);
Greg Danielabba9982018-02-01 10:07:57 -05001013
1014 // Release second ref from the original context
1015 testContext->makeCurrent();
Greg Danielfebdedf2020-02-05 17:06:27 -05001016 viewSecondRef.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001017
Greg Daniel48661b82018-01-22 16:11:35 -05001018 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001019 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);
Brian Osmanceb7a422017-06-21 15:10:33 -04001022
1023 // Release everything
Greg Danielfebdedf2020-02-05 17:06:27 -05001024 otherView.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001025 refImg.reset(nullptr);
1026 }
Brian Osman13dddce2017-05-09 13:19:50 -04001027 }
1028}
1029
Brian Salomondcfca432017-11-15 15:48:03 -05001030DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001031 SkBitmap bitmap;
1032 SkPixmap pixmap;
Hal Canarybaa2a282018-11-26 15:34:12 -05001033 if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
1034 ERRORF(reporter, "missing resource");
1035 return;
1036 }
Hal Canaryf7d3f612018-03-22 15:17:42 -04001037 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
Robert Phillipsb25e0652020-07-22 09:35:49 -04001038 [&pixmap](GrDirectContext* dContext) {
1039 return SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman63bc48d2017-11-07 10:37:00 -05001040 });
1041}
1042
Brian Osman052ef692018-03-27 09:56:31 -04001043DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
1044
1045 for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
1046 SkAutoPixmapStorage pixmap;
1047 pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
1048
1049 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
1050 GrContextFactory testFactory(options);
1051 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
1052 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001053 auto dContext = ctxInfo.directContext();
1054 if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001055 continue;
1056 }
1057
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001058 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman052ef692018-03-27 09:56:31 -04001059 REPORTER_ASSERT(reporter, image);
1060
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001061 GrSurfaceProxyView view = as_IB(image)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001062 REPORTER_ASSERT(reporter, view);
Brian Osman052ef692018-03-27 09:56:31 -04001063
1064 bool expectAlpha = kAlpha_8_SkColorType == ct;
Greg Daniela4828a12019-10-11 13:51:02 -04001065 GrColorType grCT = SkColorTypeToGrColorType(image->colorType());
1066 REPORTER_ASSERT(reporter, expectAlpha == GrColorTypeIsAlphaOnly(grCT));
Brian Osman052ef692018-03-27 09:56:31 -04001067 }
1068 }
1069}
1070
Eric Karl914a36b2017-10-12 12:44:50 -07001071DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001072 auto context = ctxInfo.directContext();
Eric Karl914a36b2017-10-12 12:44:50 -07001073 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
1074 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
1075
1076 GrContextFactory otherFactory;
1077 ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
1078
1079 testContext->makeCurrent();
1080 REPORTER_ASSERT(reporter, proxy);
1081 auto createLarge = [context] {
Robert Phillips9da87e02019-02-04 13:26:26 -05001082 return create_image_large(context->priv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001083 };
John Stilesbd3ffa42020-07-30 20:24:57 -04001084 struct TestCase {
1085 std::function<sk_sp<SkImage>()> fImageFactory;
1086 bool fExpectation;
1087 bool fCanTakeDirectly;
1088 };
1089 TestCase testCases[] = {
Eric Karl914a36b2017-10-12 12:44:50 -07001090 { create_image, true, false },
1091 { create_codec_image, true, false },
1092 { create_data_image, true, false },
1093 { create_picture_image, true, false },
1094 { [context] { return create_gpu_image(context); }, true, true },
Robert Phillipse94b4e12020-07-23 13:54:35 -04001095 // Create a texture image in a another context.
Brian Salomon55ad7742017-11-17 09:25:23 -05001096 { [otherContextInfo] {
1097 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -04001098 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.directContext());
1099 otherContextInfo.directContext()->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001100 return otherContextImage;
1101 }, false, false },
1102 // Create an image that is too large to be texture backed.
1103 { createLarge, false, false }
1104 };
1105
John Stilesbd3ffa42020-07-30 20:24:57 -04001106 for (const TestCase& testCase : testCases) {
Eric Karl914a36b2017-10-12 12:44:50 -07001107 sk_sp<SkImage> image(testCase.fImageFactory());
1108 if (!image) {
1109 ERRORF(reporter, "Failed to create image!");
1110 continue;
1111 }
1112
Robert Phillipsba375a82018-04-11 10:08:06 -04001113 GrBackendTexture origBackend = image->getBackendTexture(true);
1114 if (testCase.fCanTakeDirectly) {
1115 SkASSERT(origBackend.isValid());
1116 }
1117
1118 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001119 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001120 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1121 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001122 if (result != testCase.fExpectation) {
1123 static const char *const kFS[] = { "fail", "succeed" };
1124 ERRORF(reporter, "This image was expected to %s but did not.",
1125 kFS[testCase.fExpectation]);
1126 }
1127
Robert Phillipsba375a82018-04-11 10:08:06 -04001128 if (result) {
1129 SkASSERT(newBackend.isValid());
1130 }
1131
1132 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001133 if (testCase.fCanTakeDirectly != tookDirectly) {
1134 static const char *const kExpectedState[] = { "not expected", "expected" };
1135 ERRORF(reporter, "This backend texture was %s to be taken directly.",
1136 kExpectedState[testCase.fCanTakeDirectly]);
1137 }
1138
Greg Daniel0a2464f2020-05-14 15:45:44 -04001139 context->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001140 }
1141}
reedeb560282016-07-26 19:42:04 -07001142
1143///////////////////////////////////////////////////////////////////////////////////////////////////
1144
Matt Sarett0bb6f382017-03-06 10:28:24 -05001145static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1146 SkPictureRecorder recorder;
1147 SkCanvas* canvas = recorder.beginRecording(10, 10);
1148 canvas->clear(SK_ColorCYAN);
1149 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1150 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1151};
1152
1153DEF_TEST(Image_ColorSpace, r) {
1154 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001155 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001156 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1157
Hal Canaryc465d132017-12-08 10:21:31 -05001158 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Brian Osman82ebe042019-01-04 17:03:00 -05001159 skcms_TransferFunction fn;
Matt Sarett0bb6f382017-03-06 10:28:24 -05001160 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1161 REPORTER_ASSERT(r, success);
Brian Osman82ebe042019-01-04 17:03:00 -05001162 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
Matt Sarett0bb6f382017-03-06 10:28:24 -05001163
Brian Osman82ebe042019-01-04 17:03:00 -05001164 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
1165 SkNamedGamut::kRec2020);
Matt Sarett0bb6f382017-03-06 10:28:24 -05001166 image = create_picture_image(rec2020);
1167 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1168
1169 SkBitmap bitmap;
1170 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1171 bitmap.allocPixels(info);
1172 image = SkImage::MakeFromBitmap(bitmap);
1173 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1174
1175 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1176 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1177 image = surface->makeImageSnapshot();
1178 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1179
1180 surface = SkSurface::MakeRaster(info);
1181 image = surface->makeImageSnapshot();
1182 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1183}
1184
Matt Sarett6de13102017-03-14 14:10:48 -04001185DEF_TEST(Image_makeColorSpace, r) {
Mike Kleinb147ace2020-01-16 11:11:06 -06001186 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Brian Osman82ebe042019-01-04 17:03:00 -05001187 skcms_TransferFunction fn;
1188 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;
1189 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB);
Matt Sarett6de13102017-03-14 14:10:48 -04001190
1191 SkBitmap srgbBitmap;
1192 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1193 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1194 srgbBitmap.setImmutable();
1195 sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
Adlai Holler3a220172020-07-15 10:37:50 -04001196 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, nullptr);
Matt Sarett6de13102017-03-14 14:10:48 -04001197 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001198 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001199
1200 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1201
Matt Sarett6de13102017-03-14 14:10:48 -04001202 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001203 REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1204 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1205 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1206
Adlai Holler3a220172020-07-15 10:37:50 -04001207 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, nullptr);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001208 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001209 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001210 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001211 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1212 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1213 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1214
Hal Canaryc465d132017-12-08 10:21:31 -05001215 srgbImage = GetResourceAsImage("images/1x1.png");
Adlai Holler3a220172020-07-15 10:37:50 -04001216 p3Image = srgbImage->makeColorSpace(p3, nullptr);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001217 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001218 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001219 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1220 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1221 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1222}
1223
Matt Sarett0bb6f382017-03-06 10:28:24 -05001224///////////////////////////////////////////////////////////////////////////////////////////////////
1225
reedeb560282016-07-26 19:42:04 -07001226static void make_all_premul(SkBitmap* bm) {
1227 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1228 for (int a = 0; a < 256; ++a) {
1229 for (int r = 0; r < 256; ++r) {
1230 // make all valid premul combinations
Brian Osman788b9162020-02-07 10:36:46 -05001231 int c = std::min(a, r);
reedeb560282016-07-26 19:42:04 -07001232 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1233 }
1234 }
1235}
1236
1237static bool equal(const SkBitmap& a, const SkBitmap& b) {
1238 SkASSERT(a.width() == b.width());
1239 SkASSERT(a.height() == b.height());
1240 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001241 for (int x = 0; x < a.width(); ++x) {
1242 SkPMColor pa = *a.getAddr32(x, y);
1243 SkPMColor pb = *b.getAddr32(x, y);
1244 if (pa != pb) {
1245 return false;
1246 }
reedeb560282016-07-26 19:42:04 -07001247 }
1248 }
1249 return true;
1250}
1251
1252DEF_TEST(image_roundtrip_encode, reporter) {
1253 SkBitmap bm0;
1254 make_all_premul(&bm0);
1255
1256 auto img0 = SkImage::MakeFromBitmap(bm0);
Mike Reed6409f842017-07-11 16:03:13 -04001257 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001258 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001259
reedeb560282016-07-26 19:42:04 -07001260 SkBitmap bm1;
1261 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001262 img1->readPixels(nullptr, bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001263
reedeb560282016-07-26 19:42:04 -07001264 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1265}
1266
1267DEF_TEST(image_roundtrip_premul, reporter) {
1268 SkBitmap bm0;
1269 make_all_premul(&bm0);
1270
1271 SkBitmap bm1;
1272 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1273 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1274
1275 SkBitmap bm2;
1276 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1277 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1278
1279 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1280}
Brian Osmanb8a13922017-04-26 15:28:30 -04001281
1282///////////////////////////////////////////////////////////////////////////////////////////////////
1283
1284static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
1285 // Verify that all pixels contain the original test color
1286 for (auto y = 0; y < pmap->height(); ++y) {
1287 for (auto x = 0; x < pmap->width(); ++x) {
1288 uint32_t pixel = *pmap->addr32(x, y);
1289 if (pixel != expected) {
1290 ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
1291 x, y, pixel, expected);
1292 return;
1293 }
1294 }
1295 }
1296}
1297
1298static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
1299 uint32_t expected) {
1300 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
1301
1302 // Make sure to test kDisallow first, so we don't just get a cache hit in that case
1303 for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
1304 SkAutoPixmapStorage scaled;
1305 scaled.alloc(info);
Mike Reed2aa3b8d2020-11-24 09:19:21 -05001306 if (!image->scalePixels(scaled, SkSamplingOptions(SkFilterMode::kLinear,
1307 SkMipmapMode::kNone), chint)) {
Brian Osmanb8a13922017-04-26 15:28:30 -04001308 ERRORF(reporter, "Failed to scale image");
1309 continue;
1310 }
1311
1312 check_scaled_pixels(reporter, &scaled, expected);
1313 }
1314}
1315
1316DEF_TEST(ImageScalePixels, reporter) {
1317 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1318 const SkColor red = SK_ColorRED;
1319
1320 // Test raster image
1321 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1322 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
1323 surface->getCanvas()->clear(red);
1324 sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
1325 test_scale_pixels(reporter, rasterImage.get(), pmRed);
1326
1327 // Test encoded image
Mike Reed6409f842017-07-11 16:03:13 -04001328 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001329 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1330 test_scale_pixels(reporter, codecImage.get(), pmRed);
1331}
1332
Brian Osmanb8a13922017-04-26 15:28:30 -04001333DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
1334 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1335 const SkColor red = SK_ColorRED;
1336
1337 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
Robert Phillips6d344c32020-07-06 10:56:46 -04001338 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.directContext(),
1339 SkBudgeted::kNo, info);
Brian Osmanb8a13922017-04-26 15:28:30 -04001340 surface->getCanvas()->clear(red);
1341 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1342 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1343}
Mike Reedc4e31092018-01-30 11:15:27 -05001344
1345static sk_sp<SkImage> any_image_will_do() {
1346 return GetResourceAsImage("images/mandrill_32.png");
1347}
1348
1349DEF_TEST(Image_nonfinite_dst, reporter) {
1350 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
1351 auto img = any_image_will_do();
1352 SkPaint paint;
1353
1354 for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
1355 for (int bits = 1; bits <= 15; ++bits) {
1356 SkRect dst = { 0, 0, 10, 10 };
1357 if (bits & 1) dst.fLeft = bad;
1358 if (bits & 2) dst.fTop = bad;
1359 if (bits & 4) dst.fRight = bad;
1360 if (bits & 8) dst.fBottom = bad;
1361
1362 surf->getCanvas()->drawImageRect(img, dst, &paint);
1363
1364 // we should draw nothing
Mike Kleinea3f0142019-03-20 11:12:10 -05001365 ToolUtils::PixelIter iter(surf.get());
Mike Reedc4e31092018-01-30 11:15:27 -05001366 while (void* addr = iter.next()) {
1367 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1368 }
1369 }
1370 }
1371}
1372
Robert Phillipsb25e0652020-07-22 09:35:49 -04001373static sk_sp<SkImage> make_yuva_image(GrDirectContext* dContext) {
Brian Salomonad8efda2019-05-10 09:22:49 -04001374 SkAutoPixmapStorage pm;
1375 pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
Brian Salomone4387382020-11-11 16:34:19 -05001376 SkYUVAInfo yuvaInfo({1, 1},
1377 SkYUVAInfo::PlaneConfig::kY_U_V,
1378 SkYUVAInfo::Subsampling::k444,
1379 kJPEG_Full_SkYUVColorSpace);
Brian Salomonc2a9a972020-09-15 11:24:28 -04001380 const SkPixmap pmaps[] = {pm, pm, pm};
1381 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001382
Brian Salomonc2a9a972020-09-15 11:24:28 -04001383 return SkImage::MakeFromYUVAPixmaps(dContext, yuvaPixmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001384}
1385
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001386DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -04001387 auto dContext = ctxInfo.directContext();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001388 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001389 auto s = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, ii, 1, nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001390
1391 s->getCanvas()->clear(SK_ColorRED);
1392 auto i0 = s->makeImageSnapshot();
1393 s->getCanvas()->clear(SK_ColorBLUE);
1394 auto i1 = s->makeImageSnapshot();
1395 s->getCanvas()->clear(SK_ColorGREEN);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001396 // Make a YUVA image.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001397 auto i2 = make_yuva_image(dContext);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001398
1399 // Flush all the setup work we did above and then make little lambda that reports the flush
1400 // count delta since the last time it was called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001401 dContext->flushAndSubmit();
1402 auto numSubmits =
1403 [dContext,
1404 submitCnt = dContext->priv().getGpu()->stats()->numSubmitToGpus()]() mutable {
1405 int curr = dContext->priv().getGpu()->stats()->numSubmitToGpus();
Greg Daniel04283f32020-05-20 13:16:00 -04001406 int n = curr - submitCnt;
1407 submitCnt = curr;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001408 return n;
1409 };
1410
Greg Daniel04283f32020-05-20 13:16:00 -04001411 // Images aren't used therefore flush is ignored, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001412 i0->flushAndSubmit(dContext);
1413 i1->flushAndSubmit(dContext);
1414 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001415 REPORTER_ASSERT(reporter, numSubmits() == 3);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001416
1417 // Syncing forces the flush to happen even if the images aren't used.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001418 i0->flush(dContext);
1419 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001420 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001421 i1->flush(dContext);
1422 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001423 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001424 i2->flush(dContext);
1425 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001426 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001427
1428 // Use image 1
1429 s->getCanvas()->drawImage(i1, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001430 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001431 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001432 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001433 // Flushing image 1 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001434 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001435 REPORTER_ASSERT(reporter, numSubmits() == 1);
1436 // Flushing image 2 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001437 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001438 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001439
1440 // Use image 2
1441 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001442 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001443 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001444 REPORTER_ASSERT(reporter, numSubmits() == 1);
1445 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001446 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001447 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001448 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001449 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001450 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001451 // Since we just did a simple image draw it should not have been flattened.
1452 REPORTER_ASSERT(reporter,
1453 !static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
Brian Salomonad8efda2019-05-10 09:22:49 -04001454 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1455
1456 // Flatten it and repeat.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001457 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001458 REPORTER_ASSERT(reporter,
1459 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1460 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1461 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001462 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001463 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001464 REPORTER_ASSERT(reporter, numSubmits() == 1);
1465 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001466 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001467 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001468 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001469 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001470 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001471
1472 // Test case where flatten happens before the first flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001473 i2 = make_yuva_image(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001474 // On some systems where preferVRAMUseOverFlushes is false (ANGLE on Windows) the above may
1475 // actually flush in order to make textures for the YUV planes. TODO: Remove this when we
Robert Phillipse94b4e12020-07-23 13:54:35 -04001476 // make the YUVA planes from backend textures rather than pixmaps that the context must upload.
Greg Daniel04283f32020-05-20 13:16:00 -04001477 // Calling numSubmits rebases the flush count from here.
1478 numSubmits();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001479 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001480 REPORTER_ASSERT(reporter,
1481 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1482 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1483 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001484 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001485 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001486 REPORTER_ASSERT(reporter, numSubmits() == 1);
1487 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001488 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001489 REPORTER_ASSERT(reporter, numSubmits() == 1);
1490 // Flushing image 2 should flush, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001491 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001492 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001493}
Mike Reed3d30ca62020-07-22 16:55:02 -04001494
1495#include "src/shaders/SkImageShader.h"
1496
1497constexpr SkM44 gCentripetalCatmulRom
1498 (0.0f/2, -1.0f/2, 2.0f/2, -1.0f/2,
1499 2.0f/2, 0.0f/2, -5.0f/2, 3.0f/2,
1500 0.0f/2, 1.0f/2, 4.0f/2, -3.0f/2,
1501 0.0f/2, 0.0f/2, -1.0f/2, 1.0f/2);
1502
1503constexpr SkM44 gMitchellNetravali
1504 ( 1.0f/18, -9.0f/18, 15.0f/18, -7.0f/18,
1505 16.0f/18, 0.0f/18, -36.0f/18, 21.0f/18,
1506 1.0f/18, 9.0f/18, 27.0f/18, -21.0f/18,
1507 0.0f/18, 0.0f/18, -6.0f/18, 7.0f/18);
1508
1509DEF_TEST(image_cubicresampler, reporter) {
1510 auto diff = [reporter](const SkM44& a, const SkM44& b) {
1511 const float tolerance = 0.000001f;
1512 for (int r = 0; r < 4; ++r) {
1513 for (int c = 0; c < 4; ++c) {
1514 float d = std::abs(a.rc(r, c) - b.rc(r, c));
1515 REPORTER_ASSERT(reporter, d <= tolerance);
1516 }
1517 }
1518 };
1519
1520 diff(SkImageShader::CubicResamplerMatrix(1.0f/3, 1.0f/3), gMitchellNetravali);
1521
1522 diff(SkImageShader::CubicResamplerMatrix(0, 1.0f/2), gCentripetalCatmulRom);
1523}
Mike Reedcd043562020-07-29 13:55:45 -04001524
1525DEF_TEST(image_subset_encode_skbug_7752, reporter) {
1526 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_128.png");
1527 const int W = image->width();
1528 const int H = image->height();
1529
1530 auto check_roundtrip = [&](sk_sp<SkImage> img) {
1531 auto img2 = SkImage::MakeFromEncoded(img->encodeToData());
1532 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), img2.get()));
1533 };
1534 check_roundtrip(image); // should trivially pass
1535 check_roundtrip(image->makeSubset({0, 0, W/2, H/2}));
1536 check_roundtrip(image->makeSubset({W/2, H/2, W, H}));
1537 check_roundtrip(image->makeColorSpace(SkColorSpace::MakeSRGBLinear()));
1538}