blob: 2a9c4da959d2a816fabe2f33e6a78b02a29b24f4 [file] [log] [blame]
reed871872f2015-06-22 12:48:26 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon8e74f802016-01-30 10:01:40 -08008#include <functional>
bsalomon0d996862016-03-09 18:44:43 -08009#include <initializer_list>
ericrkb4da01d2016-06-13 11:18:14 -070010#include <vector>
bsalomon8e74f802016-01-30 10:01:40 -080011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkBitmap.h"
13#include "include/core/SkCanvas.h"
14#include "include/core/SkData.h"
15#include "include/core/SkImageEncoder.h"
16#include "include/core/SkImageGenerator.h"
17#include "include/core/SkPicture.h"
18#include "include/core/SkPictureRecorder.h"
19#include "include/core/SkRRect.h"
20#include "include/core/SkSerialProcs.h"
21#include "include/core/SkStream.h"
22#include "include/core/SkSurface.h"
Brian Salomonf9a1fdf2019-05-09 10:30:12 -040023#include "include/gpu/GrContextThreadSafeProxy.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040024#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/core/SkAutoPixmapStorage.h"
26#include "src/core/SkColorSpacePriv.h"
27#include "src/core/SkImagePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/core/SkUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrContextPriv.h"
30#include "src/gpu/GrGpu.h"
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"
39#include "tests/TestUtils.h"
40#include "tools/Resources.h"
41#include "tools/ToolUtils.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
Greg Danielc1ad77c2020-05-06 11:40:03 -0400490 GrBackendTexture backendTex;
Adlai Holler14dc7912020-08-11 15:48:49 +0000491 CreateBackendTexture(dContext, &backendTex, kSize, kSize, colorType, SkColors::kTransparent,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400492 GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Robert Phillips9b16f812019-05-17 10:01:21 -0400493
Adlai Holler14dc7912020-08-11 15:48:49 +0000494 auto img = SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
Robert Phillips9b16f812019-05-17 10:01:21 -0400495 colorType, kOpaque_SkAlphaType, nullptr);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500496 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300497 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500498 colorType);
499
500 img.reset();
Adlai Holler14dc7912020-08-11 15:48:49 +0000501 dContext->flushAndSubmit();
502 dContext->deleteBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500503 }
504}
505
Brian Salomon9708af82018-02-05 12:57:10 -0500506DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
507 SkBitmap bmp;
508 bmp.allocPixels(
509 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
510 for (int y = 0; y < 256; ++y) {
511 for (int x = 0; x < 256; ++x) {
512 *bmp.getAddr32(x, y) =
513 SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
514 }
515 }
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400516 auto dContext = ctxInfo.directContext();
517 auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(dContext);
Brian Salomon9708af82018-02-05 12:57:10 -0500518 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
519 ERRORF(reporter, "Failed to make unpremul texture image.");
520 return;
521 }
Brian Salomon1d435302019-07-01 13:05:28 -0400522 SkBitmap unpremul;
523 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
524 kUnpremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400525 if (!texImage->readPixels(dContext, unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(),
526 0, 0)) {
Brian Salomon1d435302019-07-01 13:05:28 -0400527 ERRORF(reporter, "Unpremul readback failed.");
528 return;
529 }
530 for (int y = 0; y < 256; ++y) {
531 for (int x = 0; x < 256; ++x) {
532 if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
533 ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
534 *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
535 return;
Brian Salomon9708af82018-02-05 12:57:10 -0500536 }
537 }
538 }
539 SkBitmap premul;
540 premul.allocPixels(
541 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400542 if (!texImage->readPixels(dContext, premul.info(), premul.getPixels(), premul.rowBytes(),
543 0, 0)) {
Brian Salomon9708af82018-02-05 12:57:10 -0500544 ERRORF(reporter, "Unpremul readback failed.");
545 return;
546 }
547 for (int y = 0; y < 256; ++y) {
548 for (int x = 0; x < 256; ++x) {
Brian Salomon1d435302019-07-01 13:05:28 -0400549 uint32_t origColor = *bmp.getAddr32(x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500550 int32_t origA = (origColor >> 24) & 0xff;
Brian Salomon1d435302019-07-01 13:05:28 -0400551 float a = origA / 255.f;
552 int32_t origB = sk_float_round2int(((origColor >> 16) & 0xff) * a);
553 int32_t origG = sk_float_round2int(((origColor >> 8) & 0xff) * a);
554 int32_t origR = sk_float_round2int(((origColor >> 0) & 0xff) * a);
555
Brian Salomon9708af82018-02-05 12:57:10 -0500556 uint32_t read = *premul.getAddr32(x, y);
557 int32_t readA = (read >> 24) & 0xff;
558 int32_t readB = (read >> 16) & 0xff;
559 int32_t readG = (read >> 8) & 0xff;
560 int32_t readR = (read >> 0) & 0xff;
561 // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
562 // bit of tolerance for GPU vs CPU premul math.
563 int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
564 if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
565 SkTAbs(readR - origR) > tol) {
Brian Salomon1d435302019-07-01 13:05:28 -0400566 ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) expected(0x%08x) at %d, %d.",
567 *bmp.getAddr32(x, y), *premul.getAddr32(x, y), origColor, x, y);
Brian Salomon9708af82018-02-05 12:57:10 -0500568 return;
569 }
570 }
571 }
572}
573
Brian Salomon8a8dd332018-05-24 14:08:31 -0400574DEF_GPUTEST(AbandonedContextImage, reporter, options) {
575 using Factory = sk_gpu_test::GrContextFactory;
576 for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
577 auto type = static_cast<Factory::ContextType>(ct);
578 std::unique_ptr<Factory> factory(new Factory);
579 if (!factory->get(type)) {
580 continue;
581 }
582
583 sk_sp<SkImage> img;
584 auto gsurf = SkSurface::MakeRenderTarget(
585 factory->get(type), SkBudgeted::kYes,
586 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
587 nullptr);
588 if (!gsurf) {
589 continue;
590 }
591 img = gsurf->makeImageSnapshot();
592 gsurf.reset();
593
594 auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
595
596 REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400597 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400598
599 factory->get(type)->abandonContext();
600 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
Adlai Holler302e8fb2020-09-14 11:58:06 -0400601 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->recordingContext()));
Brian Salomon8a8dd332018-05-24 14:08:31 -0400602 // This shouldn't crash.
603 rsurf->getCanvas()->drawImage(img, 0, 0);
604
Robert Phillipse94b4e12020-07-23 13:54:35 -0400605 // Give up all other refs on the context.
Brian Salomon8a8dd332018-05-24 14:08:31 -0400606 factory.reset(nullptr);
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 }
611}
612
kkinnunen4e184132015-11-17 22:53:28 -0800613class EmptyGenerator : public SkImageGenerator {
614public:
615 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
616};
617
kkinnunen7b94c142015-11-24 07:39:40 -0800618DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800619 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700620 SkPixmap pmap(info, nullptr, 0);
621 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
622 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
623 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500624 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
Mike Kleinf46d5ca2019-12-11 10:45:01 -0500625 std::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800626}
627
kkinnunen7b94c142015-11-24 07:39:40 -0800628DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800629 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
630 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400631 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700632 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800633 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700634 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800635 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700636 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800637 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800638}
639
kkinnunen4e184132015-11-17 22:53:28 -0800640static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
641 for (int i = 0; i < count; ++i) {
642 if (pixels[i] != expected) {
643 return false;
644 }
645 }
646 return true;
647}
648
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400649static void image_test_read_pixels(GrDirectContext* dContext, skiatest::Reporter* reporter,
650 SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700651 if (!image) {
652 ERRORF(reporter, "Failed to create image!");
653 return;
654 }
kkinnunen7b94c142015-11-24 07:39:40 -0800655 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800656 const SkPMColor notExpected = ~expected;
657
658 const int w = 2, h = 2;
659 const size_t rowBytes = w * sizeof(SkPMColor);
660 SkPMColor pixels[w*h];
661
662 SkImageInfo info;
663
664 info = SkImageInfo::MakeUnknown(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400665 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800666
667 // out-of-bounds should fail
668 info = SkImageInfo::MakeN32Premul(w, h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400669 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, -w, 0));
670 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes, 0, -h));
671 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
672 image->width(), 0));
673 REPORTER_ASSERT(reporter, !image->readPixels(dContext, info, pixels, rowBytes,
674 0, image->height()));
kkinnunen4e184132015-11-17 22:53:28 -0800675
676 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800677 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400678 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes, 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800679 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
680
681 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800682 sk_memset32(pixels, notExpected, w*h);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400683 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, pixels, rowBytes,
kkinnunen4e184132015-11-17 22:53:28 -0800684 image->width() - w, image->height() - h));
685 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
686
687 // partial top-left 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, -1, -1));
kkinnunen4e184132015-11-17 22:53:28 -0800690 REPORTER_ASSERT(reporter, pixels[3] == expected);
691 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
692
693 // partial bottom-right 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,
kkinnunen4e184132015-11-17 22:53:28 -0800696 image->width() - 1, image->height() - 1));
697 REPORTER_ASSERT(reporter, pixels[0] == expected);
698 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
699}
kkinnunen7b94c142015-11-24 07:39:40 -0800700DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700701 sk_sp<SkImage> image(create_image());
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400702 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800703
reed9ce9d672016-03-17 10:51:11 -0700704 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400705 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800706
707 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700708 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400709 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800710 image.reset();
711 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
712
reed9ce9d672016-03-17 10:51:11 -0700713 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400714 image_test_read_pixels(nullptr, reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800715}
egdanielab527a52016-06-28 08:07:26 -0700716DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400717 auto dContext = ctxInfo.directContext();
718 image_test_read_pixels(dContext, reporter, create_gpu_image(dContext).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800719}
kkinnunen4e184132015-11-17 22:53:28 -0800720
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400721static void check_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
722 const SkImage* image, const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800723 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
724 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700725 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800726
Cary Clark4f5a79c2018-02-07 15:51:00 -0500727 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800728
kkinnunen4e184132015-11-17 22:53:28 -0800729 REPORTER_ASSERT(reporter, bitmap.getPixels());
730
731 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
732 SkPMColor imageColor;
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400733 REPORTER_ASSERT(reporter, image->readPixels(dContext, info, &imageColor, sizeof(SkPMColor),
734 0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800735 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
736}
737
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400738static void test_legacy_bitmap(skiatest::Reporter* reporter, GrDirectContext* dContext,
739 const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700740 if (!image) {
741 ERRORF(reporter, "Failed to create image.");
742 return;
743 }
kkinnunen7b94c142015-11-24 07:39:40 -0800744 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500745 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400746 check_legacy_bitmap(reporter, dContext, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800747
748 // Test subsetting to exercise the rowBytes logic.
749 SkBitmap tmp;
750 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
751 image->height() / 2)));
reed9ce9d672016-03-17 10:51:11 -0700752 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
753 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800754
755 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500756 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400757 check_legacy_bitmap(reporter, nullptr, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800758}
759DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500760 sk_sp<SkImage> image(create_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 image = create_data_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400764 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800765
Cary Clark4f5a79c2018-02-07 15:51:00 -0500766 RasterDataHolder dataHolder;
767 image = create_rasterproc_image(&dataHolder);
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400768 test_legacy_bitmap(reporter, nullptr, image.get());
Cary Clark4f5a79c2018-02-07 15:51:00 -0500769 image.reset();
770 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800771
Cary Clark4f5a79c2018-02-07 15:51:00 -0500772 image = create_codec_image();
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400773 test_legacy_bitmap(reporter, nullptr, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800774}
bsalomon68d91342016-04-12 09:59:58 -0700775DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Adlai Hollerbcfc5542020-08-27 12:44:07 -0400776 auto dContext = ctxInfo.directContext();
777 sk_sp<SkImage> image(create_gpu_image(dContext));
778 test_legacy_bitmap(reporter, dContext, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800779}
kkinnunen4e184132015-11-17 22:53:28 -0800780
kkinnunen7b94c142015-11-24 07:39:40 -0800781static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700782 if (!image) {
783 ERRORF(reporter, "Failed to create image!");
784 return;
785 }
reed6ceeebd2016-03-09 14:26:26 -0800786 SkPixmap pm;
787 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800788 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
789 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800790 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800791 REPORTER_ASSERT(reporter, 20 == info.width());
792 REPORTER_ASSERT(reporter, 20 == info.height());
793 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
794 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
795 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800796 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
797 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800798 }
kkinnunen7b94c142015-11-24 07:39:40 -0800799}
800DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700801 sk_sp<SkImage> image(create_image());
802 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800803
reed9ce9d672016-03-17 10:51:11 -0700804 image = create_data_image();
805 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800806
807 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700808 image = create_rasterproc_image(&dataHolder);
809 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800810 image.reset();
811 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
812
reed9ce9d672016-03-17 10:51:11 -0700813 image = create_codec_image();
814 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800815}
egdanielab527a52016-06-28 08:07:26 -0700816DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400817 sk_sp<SkImage> image(create_gpu_image(ctxInfo.directContext()));
reed9ce9d672016-03-17 10:51:11 -0700818 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800819}
kkinnunen4e184132015-11-17 22:53:28 -0800820
kkinnunen7b94c142015-11-24 07:39:40 -0800821struct TextureReleaseChecker {
822 TextureReleaseChecker() : fReleaseCount(0) {}
823 int fReleaseCount;
824 static void Release(void* self) {
825 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800826 }
827};
Brian Osman13dddce2017-05-09 13:19:50 -0400828
brianosmandb2cb102016-07-22 07:22:04 -0700829DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
830 const int kWidth = 10;
831 const int kHeight = 10;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000832
Adlai Holler14dc7912020-08-11 15:48:49 +0000833 auto dContext = ctxInfo.directContext();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000834
Robert Phillipsee5fd132019-05-07 13:29:22 -0400835 SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
836 kPremul_SkAlphaType);
837 GrBackendTexture backendTex;
Adlai Holler14dc7912020-08-11 15:48:49 +0000838 if (!CreateBackendTexture(dContext, &backendTex, ii, SkColors::kRed, GrMipmapped::kNo,
Brian Salomon28a8f282019-10-24 20:07:39 -0400839 GrRenderable::kNo)) {
Robert Phillipsee5fd132019-05-07 13:29:22 -0400840 ERRORF(reporter, "couldn't create backend texture\n");
841 }
brianosmandb2cb102016-07-22 07:22:04 -0700842
kkinnunen7b94c142015-11-24 07:39:40 -0800843 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000844 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
reed9ce9d672016-03-17 10:51:11 -0700845 sk_sp<SkImage> refImg(
Adlai Holler14dc7912020-08-11 15:48:49 +0000846 SkImage::MakeFromTexture(dContext, backendTex, texOrigin, kRGBA_8888_SkColorType,
Greg Danielf5d87582017-12-18 14:48:15 -0500847 kPremul_SkAlphaType, nullptr,
reed9ce9d672016-03-17 10:51:11 -0700848 TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800849
Robert Phillips3390e152017-01-31 17:53:34 -0500850 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400851 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
852 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) {
853 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500854 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400855 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000856 if (readBackOrigin != texOrigin) {
857 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500858 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000859 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500860
kkinnunen4e184132015-11-17 22:53:28 -0800861 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800862 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800863 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800864 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
brianosmandb2cb102016-07-22 07:22:04 -0700865
Adlai Holler14dc7912020-08-11 15:48:49 +0000866 DeleteBackendTexture(dContext, backendTex);
kkinnunen4e184132015-11-17 22:53:28 -0800867}
bsalomon0d996862016-03-09 18:44:43 -0800868
Brian Salomondcfca432017-11-15 15:48:03 -0500869static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400870 const char* testName,
Robert Phillipsb25e0652020-07-22 09:35:49 -0400871 std::function<sk_sp<SkImage>(GrDirectContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400872 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500873 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400874 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
875 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400876 auto dContext = ctxInfo.directContext();
877 if (!dContext) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400878 continue;
879 }
Brian Osman13dddce2017-05-09 13:19:50 -0400880
Brian Osmanceb7a422017-06-21 15:10:33 -0400881 // If we don't have proper support for this feature, the factory will fallback to returning
882 // codec-backed images. Those will "work", but some of our checks will fail because we
883 // expect the cross-context images not to work on multiple contexts at once.
Robert Phillipsb25e0652020-07-22 09:35:49 -0400884 if (!dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400885 continue;
886 }
Brian Osman13dddce2017-05-09 13:19:50 -0400887
Brian Osmanceb7a422017-06-21 15:10:33 -0400888 // We test three lifetime patterns for a single context:
889 // 1) Create image, free image
890 // 2) Create image, draw, flush, free image
891 // 3) Create image, draw, free image, flush
892 // ... and then repeat the last two patterns with drawing on a second* context:
893 // 4) Create image, draw*, flush*, free image
894 // 5) Create image, draw*, free iamge, flush*
Brian Osman13dddce2017-05-09 13:19:50 -0400895
Brian Osmanceb7a422017-06-21 15:10:33 -0400896 // Case #1: Create image, free image
897 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400898 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osmanceb7a422017-06-21 15:10:33 -0400899 refImg.reset(nullptr); // force a release of the image
900 }
Brian Osman13dddce2017-05-09 13:19:50 -0400901
Brian Osmanceb7a422017-06-21 15:10:33 -0400902 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
Robert Phillipsb25e0652020-07-22 09:35:49 -0400903 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400904 if (!surface) {
905 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
906 continue;
907 }
908
Brian Osmanceb7a422017-06-21 15:10:33 -0400909 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400910
Brian Osmanceb7a422017-06-21 15:10:33 -0400911 // Case #2: Create image, draw, flush, free image
912 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400913 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400914
Brian Osmanceb7a422017-06-21 15:10:33 -0400915 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400916 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400917
Brian Osmanceb7a422017-06-21 15:10:33 -0400918 refImg.reset(nullptr); // force a release of the image
919 }
Brian Osman13dddce2017-05-09 13:19:50 -0400920
Brian Osmanceb7a422017-06-21 15:10:33 -0400921 // Case #3: Create image, draw, free image, flush
922 {
Robert Phillipsb25e0652020-07-22 09:35:49 -0400923 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400924
Brian Osmanceb7a422017-06-21 15:10:33 -0400925 canvas->drawImage(refImg, 0, 0);
926 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400927
Greg Daniel0a2464f2020-05-14 15:45:44 -0400928 surface->flushAndSubmit();
Brian Osmanceb7a422017-06-21 15:10:33 -0400929 }
Brian Osman13dddce2017-05-09 13:19:50 -0400930
Brian Osmanceb7a422017-06-21 15:10:33 -0400931 // Configure second context
932 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400933
Robert Phillipsb25e0652020-07-22 09:35:49 -0400934 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(dContext);
Robert Phillips6d344c32020-07-06 10:56:46 -0400935 auto otherCtx = otherContextInfo.directContext();
Brian Osmanceb7a422017-06-21 15:10:33 -0400936 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400937
Brian Osmanceb7a422017-06-21 15:10:33 -0400938 // Creating a context in a share group may fail
939 if (!otherCtx) {
940 continue;
941 }
Brian Osman13dddce2017-05-09 13:19:50 -0400942
Brian Osmanceb7a422017-06-21 15:10:33 -0400943 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
944 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400945
Brian Osmanceb7a422017-06-21 15:10:33 -0400946 // Case #4: Create image, draw*, flush*, free image
947 {
948 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400949 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400950
Brian Osmanceb7a422017-06-21 15:10:33 -0400951 otherTestContext->makeCurrent();
952 canvas->drawImage(refImg, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -0400953 surface->flushAndSubmit();
Brian Osman13dddce2017-05-09 13:19:50 -0400954
Brian Osmanceb7a422017-06-21 15:10:33 -0400955 testContext->makeCurrent();
956 refImg.reset(nullptr); // force a release of the image
957 }
Brian Osman13dddce2017-05-09 13:19:50 -0400958
Brian Osmanceb7a422017-06-21 15:10:33 -0400959 // Case #5: Create image, draw*, free image, flush*
960 {
961 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400962 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400963
Brian Osmanceb7a422017-06-21 15:10:33 -0400964 otherTestContext->makeCurrent();
965 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400966
Brian Osmanceb7a422017-06-21 15:10:33 -0400967 testContext->makeCurrent();
968 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400969
Brian Osmanceb7a422017-06-21 15:10:33 -0400970 otherTestContext->makeCurrent();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400971 surface->flushAndSubmit();
Greg Daniel3f475d92017-07-25 16:26:35 -0400972
Greg Daniel26b50a42018-03-08 09:49:58 -0500973 // This is specifically here for vulkan to guarantee the command buffer will finish
974 // which is when we call the ReleaseProc.
Robert Phillips9da87e02019-02-04 13:26:26 -0500975 otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400976 }
Brian Osman13dddce2017-05-09 13:19:50 -0400977
Brian Osmanceb7a422017-06-21 15:10:33 -0400978 // Case #6: Verify that only one context can be using the image at a time
979 {
Robert Phillipse94b4e12020-07-23 13:54:35 -0400980 // Suppress warnings about trying to use a texture in two contexts.
Chris Dalton5a5fe792020-02-15 11:41:30 -0700981 GrRecordingContextPriv::AutoSuppressWarningMessages aswm(otherCtx);
982
Brian Osmanceb7a422017-06-21 15:10:33 -0400983 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400984 sk_sp<SkImage> refImg(imageMaker(dContext));
Brian Osman13dddce2017-05-09 13:19:50 -0400985
Brian Osmanceb7a422017-06-21 15:10:33 -0400986 // Any context should be able to borrow the texture at this point
Robert Phillipsb25e0652020-07-22 09:35:49 -0400987 GrSurfaceProxyView view = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500988 REPORTER_ASSERT(reporter, view);
Brian Osman13dddce2017-05-09 13:19:50 -0400989
Brian Osmanceb7a422017-06-21 15:10:33 -0400990 // But once it's borrowed, no other context should be able to borrow
991 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400992 GrSurfaceProxyView otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500993 REPORTER_ASSERT(reporter, !otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -0400994
995 // Original context (that's already borrowing) should be okay
996 testContext->makeCurrent();
Robert Phillipsb25e0652020-07-22 09:35:49 -0400997 GrSurfaceProxyView viewSecondRef = as_IB(refImg)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -0500998 REPORTER_ASSERT(reporter, viewSecondRef);
Brian Osmanceb7a422017-06-21 15:10:33 -0400999
Greg Danielabba9982018-02-01 10:07:57 -05001000 // Release first ref from the original context
Greg Danielfebdedf2020-02-05 17:06:27 -05001001 view.reset();
Greg Danielabba9982018-02-01 10:07:57 -05001002
1003 // We released one proxy but not the other from the current borrowing context. Make sure
1004 // a new context is still not able to borrow the texture.
1005 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001006 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001007 REPORTER_ASSERT(reporter, !otherView);
Greg Danielabba9982018-02-01 10:07:57 -05001008
1009 // Release second ref from the original context
1010 testContext->makeCurrent();
Greg Danielfebdedf2020-02-05 17:06:27 -05001011 viewSecondRef.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001012
Greg Daniel48661b82018-01-22 16:11:35 -05001013 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001014 otherTestContext->makeCurrent();
Brian Salomon7e67dca2020-07-21 09:27:25 -04001015 otherView = as_IB(refImg)->refView(otherCtx, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001016 REPORTER_ASSERT(reporter, otherView);
Brian Osmanceb7a422017-06-21 15:10:33 -04001017
1018 // Release everything
Greg Danielfebdedf2020-02-05 17:06:27 -05001019 otherView.reset();
Brian Osmanceb7a422017-06-21 15:10:33 -04001020 refImg.reset(nullptr);
1021 }
Brian Osman13dddce2017-05-09 13:19:50 -04001022 }
1023}
1024
Brian Salomondcfca432017-11-15 15:48:03 -05001025DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001026 SkBitmap bitmap;
1027 SkPixmap pixmap;
Hal Canarybaa2a282018-11-26 15:34:12 -05001028 if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
1029 ERRORF(reporter, "missing resource");
1030 return;
1031 }
Hal Canaryf7d3f612018-03-22 15:17:42 -04001032 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
Robert Phillipsb25e0652020-07-22 09:35:49 -04001033 [&pixmap](GrDirectContext* dContext) {
1034 return SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman63bc48d2017-11-07 10:37:00 -05001035 });
1036}
1037
Brian Osman052ef692018-03-27 09:56:31 -04001038DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
1039
1040 for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
1041 SkAutoPixmapStorage pixmap;
1042 pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
1043
1044 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
1045 GrContextFactory testFactory(options);
1046 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
1047 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001048 auto dContext = ctxInfo.directContext();
1049 if (!dContext || !dContext->priv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001050 continue;
1051 }
1052
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001053 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(dContext, pixmap, false);
Brian Osman052ef692018-03-27 09:56:31 -04001054 REPORTER_ASSERT(reporter, image);
1055
Adlai Hollerc41ae2a2020-08-11 15:47:47 +00001056 GrSurfaceProxyView view = as_IB(image)->refView(dContext, GrMipmapped::kNo);
Greg Danielfebdedf2020-02-05 17:06:27 -05001057 REPORTER_ASSERT(reporter, view);
Brian Osman052ef692018-03-27 09:56:31 -04001058
1059 bool expectAlpha = kAlpha_8_SkColorType == ct;
Greg Daniela4828a12019-10-11 13:51:02 -04001060 GrColorType grCT = SkColorTypeToGrColorType(image->colorType());
1061 REPORTER_ASSERT(reporter, expectAlpha == GrColorTypeIsAlphaOnly(grCT));
Brian Osman052ef692018-03-27 09:56:31 -04001062 }
1063 }
1064}
1065
Eric Karl914a36b2017-10-12 12:44:50 -07001066DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -04001067 auto context = ctxInfo.directContext();
Eric Karl914a36b2017-10-12 12:44:50 -07001068 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
1069 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
1070
1071 GrContextFactory otherFactory;
1072 ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
1073
1074 testContext->makeCurrent();
1075 REPORTER_ASSERT(reporter, proxy);
1076 auto createLarge = [context] {
Robert Phillips9da87e02019-02-04 13:26:26 -05001077 return create_image_large(context->priv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001078 };
John Stilesbd3ffa42020-07-30 20:24:57 -04001079 struct TestCase {
1080 std::function<sk_sp<SkImage>()> fImageFactory;
1081 bool fExpectation;
1082 bool fCanTakeDirectly;
1083 };
1084 TestCase testCases[] = {
Eric Karl914a36b2017-10-12 12:44:50 -07001085 { create_image, true, false },
1086 { create_codec_image, true, false },
1087 { create_data_image, true, false },
1088 { create_picture_image, true, false },
1089 { [context] { return create_gpu_image(context); }, true, true },
Robert Phillipse94b4e12020-07-23 13:54:35 -04001090 // Create a texture image in a another context.
Brian Salomon55ad7742017-11-17 09:25:23 -05001091 { [otherContextInfo] {
1092 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Robert Phillips6d344c32020-07-06 10:56:46 -04001093 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.directContext());
1094 otherContextInfo.directContext()->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001095 return otherContextImage;
1096 }, false, false },
1097 // Create an image that is too large to be texture backed.
1098 { createLarge, false, false }
1099 };
1100
John Stilesbd3ffa42020-07-30 20:24:57 -04001101 for (const TestCase& testCase : testCases) {
Eric Karl914a36b2017-10-12 12:44:50 -07001102 sk_sp<SkImage> image(testCase.fImageFactory());
1103 if (!image) {
1104 ERRORF(reporter, "Failed to create image!");
1105 continue;
1106 }
1107
Robert Phillipsba375a82018-04-11 10:08:06 -04001108 GrBackendTexture origBackend = image->getBackendTexture(true);
1109 if (testCase.fCanTakeDirectly) {
1110 SkASSERT(origBackend.isValid());
1111 }
1112
1113 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001114 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001115 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1116 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001117 if (result != testCase.fExpectation) {
1118 static const char *const kFS[] = { "fail", "succeed" };
1119 ERRORF(reporter, "This image was expected to %s but did not.",
1120 kFS[testCase.fExpectation]);
1121 }
1122
Robert Phillipsba375a82018-04-11 10:08:06 -04001123 if (result) {
1124 SkASSERT(newBackend.isValid());
1125 }
1126
1127 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001128 if (testCase.fCanTakeDirectly != tookDirectly) {
1129 static const char *const kExpectedState[] = { "not expected", "expected" };
1130 ERRORF(reporter, "This backend texture was %s to be taken directly.",
1131 kExpectedState[testCase.fCanTakeDirectly]);
1132 }
1133
Greg Daniel0a2464f2020-05-14 15:45:44 -04001134 context->flushAndSubmit();
Eric Karl914a36b2017-10-12 12:44:50 -07001135 }
1136}
reedeb560282016-07-26 19:42:04 -07001137
1138///////////////////////////////////////////////////////////////////////////////////////////////////
1139
Matt Sarett0bb6f382017-03-06 10:28:24 -05001140static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1141 SkPictureRecorder recorder;
1142 SkCanvas* canvas = recorder.beginRecording(10, 10);
1143 canvas->clear(SK_ColorCYAN);
1144 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1145 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1146};
1147
1148DEF_TEST(Image_ColorSpace, r) {
1149 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001150 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001151 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1152
Hal Canaryc465d132017-12-08 10:21:31 -05001153 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Brian Osman82ebe042019-01-04 17:03:00 -05001154 skcms_TransferFunction fn;
Matt Sarett0bb6f382017-03-06 10:28:24 -05001155 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1156 REPORTER_ASSERT(r, success);
Brian Osman82ebe042019-01-04 17:03:00 -05001157 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
Matt Sarett0bb6f382017-03-06 10:28:24 -05001158
Brian Osman82ebe042019-01-04 17:03:00 -05001159 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
1160 SkNamedGamut::kRec2020);
Matt Sarett0bb6f382017-03-06 10:28:24 -05001161 image = create_picture_image(rec2020);
1162 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1163
1164 SkBitmap bitmap;
1165 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1166 bitmap.allocPixels(info);
1167 image = SkImage::MakeFromBitmap(bitmap);
1168 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1169
1170 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1171 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1172 image = surface->makeImageSnapshot();
1173 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1174
1175 surface = SkSurface::MakeRaster(info);
1176 image = surface->makeImageSnapshot();
1177 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1178}
1179
Matt Sarett6de13102017-03-14 14:10:48 -04001180DEF_TEST(Image_makeColorSpace, r) {
Mike Kleinb147ace2020-01-16 11:11:06 -06001181 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Brian Osman82ebe042019-01-04 17:03:00 -05001182 skcms_TransferFunction fn;
1183 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;
1184 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB);
Matt Sarett6de13102017-03-14 14:10:48 -04001185
1186 SkBitmap srgbBitmap;
1187 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1188 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1189 srgbBitmap.setImmutable();
1190 sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
Adlai Holler3a220172020-07-15 10:37:50 -04001191 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, nullptr);
Matt Sarett6de13102017-03-14 14:10:48 -04001192 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001193 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001194
1195 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1196
Matt Sarett6de13102017-03-14 14:10:48 -04001197 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001198 REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1199 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1200 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1201
Adlai Holler3a220172020-07-15 10:37:50 -04001202 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut, nullptr);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001203 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001204 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001205 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001206 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1207 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1208 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1209
Hal Canaryc465d132017-12-08 10:21:31 -05001210 srgbImage = GetResourceAsImage("images/1x1.png");
Adlai Holler3a220172020-07-15 10:37:50 -04001211 p3Image = srgbImage->makeColorSpace(p3, nullptr);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001212 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001213 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001214 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1215 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1216 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1217}
1218
Matt Sarett0bb6f382017-03-06 10:28:24 -05001219///////////////////////////////////////////////////////////////////////////////////////////////////
1220
reedeb560282016-07-26 19:42:04 -07001221static void make_all_premul(SkBitmap* bm) {
1222 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1223 for (int a = 0; a < 256; ++a) {
1224 for (int r = 0; r < 256; ++r) {
1225 // make all valid premul combinations
Brian Osman788b9162020-02-07 10:36:46 -05001226 int c = std::min(a, r);
reedeb560282016-07-26 19:42:04 -07001227 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1228 }
1229 }
1230}
1231
1232static bool equal(const SkBitmap& a, const SkBitmap& b) {
1233 SkASSERT(a.width() == b.width());
1234 SkASSERT(a.height() == b.height());
1235 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001236 for (int x = 0; x < a.width(); ++x) {
1237 SkPMColor pa = *a.getAddr32(x, y);
1238 SkPMColor pb = *b.getAddr32(x, y);
1239 if (pa != pb) {
1240 return false;
1241 }
reedeb560282016-07-26 19:42:04 -07001242 }
1243 }
1244 return true;
1245}
1246
1247DEF_TEST(image_roundtrip_encode, reporter) {
1248 SkBitmap bm0;
1249 make_all_premul(&bm0);
1250
1251 auto img0 = SkImage::MakeFromBitmap(bm0);
Mike Reed6409f842017-07-11 16:03:13 -04001252 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001253 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001254
reedeb560282016-07-26 19:42:04 -07001255 SkBitmap bm1;
1256 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
Adlai Hollerbcfc5542020-08-27 12:44:07 -04001257 img1->readPixels(nullptr, bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001258
reedeb560282016-07-26 19:42:04 -07001259 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1260}
1261
1262DEF_TEST(image_roundtrip_premul, reporter) {
1263 SkBitmap bm0;
1264 make_all_premul(&bm0);
1265
1266 SkBitmap bm1;
1267 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1268 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1269
1270 SkBitmap bm2;
1271 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1272 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1273
1274 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1275}
Brian Osmanb8a13922017-04-26 15:28:30 -04001276
1277///////////////////////////////////////////////////////////////////////////////////////////////////
1278
1279static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
1280 // Verify that all pixels contain the original test color
1281 for (auto y = 0; y < pmap->height(); ++y) {
1282 for (auto x = 0; x < pmap->width(); ++x) {
1283 uint32_t pixel = *pmap->addr32(x, y);
1284 if (pixel != expected) {
1285 ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
1286 x, y, pixel, expected);
1287 return;
1288 }
1289 }
1290 }
1291}
1292
1293static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
1294 uint32_t expected) {
1295 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
1296
1297 // Make sure to test kDisallow first, so we don't just get a cache hit in that case
1298 for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
1299 SkAutoPixmapStorage scaled;
1300 scaled.alloc(info);
1301 if (!image->scalePixels(scaled, kLow_SkFilterQuality, chint)) {
1302 ERRORF(reporter, "Failed to scale image");
1303 continue;
1304 }
1305
1306 check_scaled_pixels(reporter, &scaled, expected);
1307 }
1308}
1309
1310DEF_TEST(ImageScalePixels, reporter) {
1311 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1312 const SkColor red = SK_ColorRED;
1313
1314 // Test raster image
1315 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1316 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
1317 surface->getCanvas()->clear(red);
1318 sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
1319 test_scale_pixels(reporter, rasterImage.get(), pmRed);
1320
1321 // Test encoded image
Mike Reed6409f842017-07-11 16:03:13 -04001322 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001323 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1324 test_scale_pixels(reporter, codecImage.get(), pmRed);
1325}
1326
Brian Osmanb8a13922017-04-26 15:28:30 -04001327DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
1328 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1329 const SkColor red = SK_ColorRED;
1330
1331 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
Robert Phillips6d344c32020-07-06 10:56:46 -04001332 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.directContext(),
1333 SkBudgeted::kNo, info);
Brian Osmanb8a13922017-04-26 15:28:30 -04001334 surface->getCanvas()->clear(red);
1335 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1336 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1337}
Mike Reedc4e31092018-01-30 11:15:27 -05001338
1339static sk_sp<SkImage> any_image_will_do() {
1340 return GetResourceAsImage("images/mandrill_32.png");
1341}
1342
1343DEF_TEST(Image_nonfinite_dst, reporter) {
1344 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
1345 auto img = any_image_will_do();
1346 SkPaint paint;
1347
1348 for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
1349 for (int bits = 1; bits <= 15; ++bits) {
1350 SkRect dst = { 0, 0, 10, 10 };
1351 if (bits & 1) dst.fLeft = bad;
1352 if (bits & 2) dst.fTop = bad;
1353 if (bits & 4) dst.fRight = bad;
1354 if (bits & 8) dst.fBottom = bad;
1355
1356 surf->getCanvas()->drawImageRect(img, dst, &paint);
1357
1358 // we should draw nothing
Mike Kleinea3f0142019-03-20 11:12:10 -05001359 ToolUtils::PixelIter iter(surf.get());
Mike Reedc4e31092018-01-30 11:15:27 -05001360 while (void* addr = iter.next()) {
1361 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1362 }
1363 }
1364 }
1365}
1366
Robert Phillipsb25e0652020-07-22 09:35:49 -04001367static sk_sp<SkImage> make_yuva_image(GrDirectContext* dContext) {
Brian Salomonad8efda2019-05-10 09:22:49 -04001368 SkAutoPixmapStorage pm;
1369 pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
Brian Salomonc2a9a972020-09-15 11:24:28 -04001370 SkYUVAInfo yuvaInfo({1, 1}, SkYUVAInfo::PlanarConfig::kY_U_V_444, kJPEG_Full_SkYUVColorSpace);
1371 const SkPixmap pmaps[] = {pm, pm, pm};
1372 auto yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001373
Brian Salomonc2a9a972020-09-15 11:24:28 -04001374 return SkImage::MakeFromYUVAPixmaps(dContext, yuvaPixmaps);
Brian Salomonad8efda2019-05-10 09:22:49 -04001375}
1376
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001377DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
Robert Phillipsb25e0652020-07-22 09:35:49 -04001378 auto dContext = ctxInfo.directContext();
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001379 auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001380 auto s = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, ii, 1, nullptr);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001381
1382 s->getCanvas()->clear(SK_ColorRED);
1383 auto i0 = s->makeImageSnapshot();
1384 s->getCanvas()->clear(SK_ColorBLUE);
1385 auto i1 = s->makeImageSnapshot();
1386 s->getCanvas()->clear(SK_ColorGREEN);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001387 // Make a YUVA image.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001388 auto i2 = make_yuva_image(dContext);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001389
1390 // Flush all the setup work we did above and then make little lambda that reports the flush
1391 // count delta since the last time it was called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001392 dContext->flushAndSubmit();
1393 auto numSubmits =
1394 [dContext,
1395 submitCnt = dContext->priv().getGpu()->stats()->numSubmitToGpus()]() mutable {
1396 int curr = dContext->priv().getGpu()->stats()->numSubmitToGpus();
Greg Daniel04283f32020-05-20 13:16:00 -04001397 int n = curr - submitCnt;
1398 submitCnt = curr;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001399 return n;
1400 };
1401
Greg Daniel04283f32020-05-20 13:16:00 -04001402 // Images aren't used therefore flush is ignored, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001403 i0->flushAndSubmit(dContext);
1404 i1->flushAndSubmit(dContext);
1405 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001406 REPORTER_ASSERT(reporter, numSubmits() == 3);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001407
1408 // Syncing forces the flush to happen even if the images aren't used.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001409 i0->flush(dContext);
1410 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001411 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001412 i1->flush(dContext);
1413 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001414 REPORTER_ASSERT(reporter, numSubmits() == 1);
Robert Phillipsb25e0652020-07-22 09:35:49 -04001415 i2->flush(dContext);
1416 dContext->submit(true);
Greg Daniel04283f32020-05-20 13:16:00 -04001417 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001418
1419 // Use image 1
1420 s->getCanvas()->drawImage(i1, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001421 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001422 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001423 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001424 // Flushing image 1 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001425 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001426 REPORTER_ASSERT(reporter, numSubmits() == 1);
1427 // Flushing image 2 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001428 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001429 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001430
1431 // Use image 2
1432 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001433 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001434 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001435 REPORTER_ASSERT(reporter, numSubmits() == 1);
1436 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001437 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001438 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001439 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001440 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001441 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001442 // Since we just did a simple image draw it should not have been flattened.
1443 REPORTER_ASSERT(reporter,
1444 !static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
Brian Salomonad8efda2019-05-10 09:22:49 -04001445 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1446
1447 // Flatten it and repeat.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001448 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001449 REPORTER_ASSERT(reporter,
1450 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1451 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1452 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001453 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001454 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001455 REPORTER_ASSERT(reporter, numSubmits() == 1);
1456 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001457 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001458 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001459 // Flushing image 2 should flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001460 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001461 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonad8efda2019-05-10 09:22:49 -04001462
1463 // Test case where flatten happens before the first flush.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001464 i2 = make_yuva_image(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001465 // On some systems where preferVRAMUseOverFlushes is false (ANGLE on Windows) the above may
1466 // actually flush in order to make textures for the YUV planes. TODO: Remove this when we
Robert Phillipse94b4e12020-07-23 13:54:35 -04001467 // make the YUVA planes from backend textures rather than pixmaps that the context must upload.
Greg Daniel04283f32020-05-20 13:16:00 -04001468 // Calling numSubmits rebases the flush count from here.
1469 numSubmits();
Robert Phillipsb25e0652020-07-22 09:35:49 -04001470 as_IB(i2.get())->view(dContext);
Brian Salomonad8efda2019-05-10 09:22:49 -04001471 REPORTER_ASSERT(reporter,
1472 static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
1473 REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
1474 s->getCanvas()->drawImage(i2, 0, 0);
Greg Daniel04283f32020-05-20 13:16:00 -04001475 // Flushing image 0 should do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001476 i0->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001477 REPORTER_ASSERT(reporter, numSubmits() == 1);
1478 // Flushing image 1 do nothing, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001479 i1->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001480 REPORTER_ASSERT(reporter, numSubmits() == 1);
1481 // Flushing image 2 should flush, but submit is still called.
Robert Phillipsb25e0652020-07-22 09:35:49 -04001482 i2->flushAndSubmit(dContext);
Greg Daniel04283f32020-05-20 13:16:00 -04001483 REPORTER_ASSERT(reporter, numSubmits() == 1);
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001484}
Mike Reed3d30ca62020-07-22 16:55:02 -04001485
1486#include "src/shaders/SkImageShader.h"
1487
1488constexpr SkM44 gCentripetalCatmulRom
1489 (0.0f/2, -1.0f/2, 2.0f/2, -1.0f/2,
1490 2.0f/2, 0.0f/2, -5.0f/2, 3.0f/2,
1491 0.0f/2, 1.0f/2, 4.0f/2, -3.0f/2,
1492 0.0f/2, 0.0f/2, -1.0f/2, 1.0f/2);
1493
1494constexpr SkM44 gMitchellNetravali
1495 ( 1.0f/18, -9.0f/18, 15.0f/18, -7.0f/18,
1496 16.0f/18, 0.0f/18, -36.0f/18, 21.0f/18,
1497 1.0f/18, 9.0f/18, 27.0f/18, -21.0f/18,
1498 0.0f/18, 0.0f/18, -6.0f/18, 7.0f/18);
1499
1500DEF_TEST(image_cubicresampler, reporter) {
1501 auto diff = [reporter](const SkM44& a, const SkM44& b) {
1502 const float tolerance = 0.000001f;
1503 for (int r = 0; r < 4; ++r) {
1504 for (int c = 0; c < 4; ++c) {
1505 float d = std::abs(a.rc(r, c) - b.rc(r, c));
1506 REPORTER_ASSERT(reporter, d <= tolerance);
1507 }
1508 }
1509 };
1510
1511 diff(SkImageShader::CubicResamplerMatrix(1.0f/3, 1.0f/3), gMitchellNetravali);
1512
1513 diff(SkImageShader::CubicResamplerMatrix(0, 1.0f/2), gCentripetalCatmulRom);
1514}
Mike Reedcd043562020-07-29 13:55:45 -04001515
1516DEF_TEST(image_subset_encode_skbug_7752, reporter) {
1517 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_128.png");
1518 const int W = image->width();
1519 const int H = image->height();
1520
1521 auto check_roundtrip = [&](sk_sp<SkImage> img) {
1522 auto img2 = SkImage::MakeFromEncoded(img->encodeToData());
1523 REPORTER_ASSERT(reporter, ToolUtils::equal_pixels(img.get(), img2.get()));
1524 };
1525 check_roundtrip(image); // should trivially pass
1526 check_roundtrip(image->makeSubset({0, 0, W/2, H/2}));
1527 check_roundtrip(image->makeSubset({W/2, H/2, W, H}));
1528 check_roundtrip(image->makeColorSpace(SkColorSpace::MakeSRGBLinear()));
1529}