blob: dc7576acaddd12ee5ea8c0c3715e34ca17a2e371 [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
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkAutoPixmapStorage.h"
halcanaryc56c6ef2015-09-28 11:55:28 -070013#include "SkBitmap.h"
reed871872f2015-06-22 12:48:26 -070014#include "SkCanvas.h"
Matt Sarett0bb6f382017-03-06 10:28:24 -050015#include "SkColorSpacePriv.h"
reed871872f2015-06-22 12:48:26 -070016#include "SkData.h"
reed871872f2015-06-22 12:48:26 -070017#include "SkImageEncoder.h"
kkinnunen7b94c142015-11-24 07:39:40 -080018#include "SkImageGenerator.h"
reed871872f2015-06-22 12:48:26 -070019#include "SkImage_Base.h"
reed2dad7692016-08-01 11:12:58 -070020#include "SkImagePriv.h"
Mike Reed185130c2017-02-15 15:14:16 -050021#include "SkMakeUnique.h"
fmalitac3470342015-09-04 11:36:39 -070022#include "SkPicture.h"
23#include "SkPictureRecorder.h"
reed871872f2015-06-22 12:48:26 -070024#include "SkRRect.h"
Mike Reedef038482017-12-16 08:41:28 -050025#include "SkSerialProcs.h"
fmalitac3470342015-09-04 11:36:39 -070026#include "SkStream.h"
reed871872f2015-06-22 12:48:26 -070027#include "SkSurface.h"
28#include "SkUtils.h"
29#include "Test.h"
30
Matt Sarett0bb6f382017-03-06 10:28:24 -050031#include "Resources.h"
Mike Reedc4e31092018-01-30 11:15:27 -050032#include "sk_pixel_iter.h"
Hal Canarydb683012016-11-23 08:55:18 -070033#include "sk_tool_utils.h"
34
Greg Daniel7ef28f32017-04-20 16:41:55 +000035#include "GrContextPriv.h"
brianosmandb2cb102016-07-22 07:22:04 -070036#include "GrGpu.h"
Brian Osman13dddce2017-05-09 13:19:50 -040037#include "GrResourceCache.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000038#include "GrTest.h"
Robert Phillips646e4292017-06-13 12:44:56 -040039#include "GrTexture.h"
Brian Salomonbdecacf2018-02-02 20:32:49 -050040#include "SkGr.h"
brianosmandb2cb102016-07-22 07:22:04 -070041
bsalomonf2f1c172016-04-05 12:59:06 -070042using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070043
Matt Sarettf5759932017-02-07 21:52:07 +000044SkImageInfo read_pixels_info(SkImage* image) {
45 if (as_IB(image)->onImageInfo().colorSpace()) {
46 return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType());
47 }
48
49 return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
50}
51
reed871872f2015-06-22 12:48:26 -070052static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
53 SkImage* b) {
54 const int widthA = subsetA ? subsetA->width() : a->width();
55 const int heightA = subsetA ? subsetA->height() : a->height();
56
57 REPORTER_ASSERT(reporter, widthA == b->width());
58 REPORTER_ASSERT(reporter, heightA == b->height());
reed1cb36462016-03-09 15:21:32 -080059
60 // see https://bug.skia.org/3965
61 //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
reed871872f2015-06-22 12:48:26 -070062
reed871872f2015-06-22 12:48:26 -070063 SkAutoPixmapStorage pmapA, pmapB;
Matt Sarettf5759932017-02-07 21:52:07 +000064 pmapA.alloc(read_pixels_info(a));
65 pmapB.alloc(read_pixels_info(b));
reed871872f2015-06-22 12:48:26 -070066
67 const int srcX = subsetA ? subsetA->x() : 0;
68 const int srcY = subsetA ? subsetA->y() : 0;
69
70 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
71 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
72
Matt Sarettf5759932017-02-07 21:52:07 +000073 const size_t widthBytes = widthA * 4;
reed871872f2015-06-22 12:48:26 -070074 for (int y = 0; y < heightA; ++y) {
75 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
76 }
77}
kkinnunen7b94c142015-11-24 07:39:40 -080078static void draw_image_test_pattern(SkCanvas* canvas) {
reed871872f2015-06-22 12:48:26 -070079 canvas->clear(SK_ColorWHITE);
reed871872f2015-06-22 12:48:26 -070080 SkPaint paint;
81 paint.setColor(SK_ColorBLACK);
kkinnunen7b94c142015-11-24 07:39:40 -080082 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
83}
reed9ce9d672016-03-17 10:51:11 -070084static sk_sp<SkImage> create_image() {
kkinnunen7b94c142015-11-24 07:39:40 -080085 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070086 auto surface(SkSurface::MakeRaster(info));
kkinnunen7b94c142015-11-24 07:39:40 -080087 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -070088 return surface->makeImageSnapshot();
reed871872f2015-06-22 12:48:26 -070089}
bungeman38d909e2016-08-02 14:40:46 -070090static sk_sp<SkData> create_image_data(SkImageInfo* info) {
scroggo9d081722016-04-20 08:27:18 -070091 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
92 const size_t rowBytes = info->minRowBytes();
bungeman38d909e2016-08-02 14:40:46 -070093 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
scroggo9d081722016-04-20 08:27:18 -070094 {
95 SkBitmap bm;
96 bm.installPixels(*info, data->writable_data(), rowBytes);
97 SkCanvas canvas(bm);
98 draw_image_test_pattern(&canvas);
99 }
bungeman38d909e2016-08-02 14:40:46 -0700100 return data;
scroggo9d081722016-04-20 08:27:18 -0700101}
102static sk_sp<SkImage> create_data_image() {
103 SkImageInfo info;
104 sk_sp<SkData> data(create_image_data(&info));
bungeman38d909e2016-08-02 14:40:46 -0700105 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
scroggo9d081722016-04-20 08:27:18 -0700106}
Brian Salomon534cbe52017-01-03 11:35:56 -0500107static sk_sp<SkImage> create_image_large(int maxTextureSize) {
108 const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
bsalomond4907082016-06-13 12:13:03 -0700109 auto surface(SkSurface::MakeRaster(info));
110 surface->getCanvas()->clear(SK_ColorWHITE);
111 SkPaint paint;
112 paint.setColor(SK_ColorBLACK);
113 surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint);
114 return surface->makeImageSnapshot();
115}
reed9ce9d672016-03-17 10:51:11 -0700116static sk_sp<SkImage> create_picture_image() {
bsalomon8e74f802016-01-30 10:01:40 -0800117 SkPictureRecorder recorder;
118 SkCanvas* canvas = recorder.beginRecording(10, 10);
119 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700120 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
Matt Sarette94255d2017-01-09 12:38:59 -0500121 nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500122 SkColorSpace::MakeSRGB());
bsalomon8e74f802016-01-30 10:01:40 -0800123};
kkinnunen7b94c142015-11-24 07:39:40 -0800124// Want to ensure that our Release is called when the owning image is destroyed
125struct RasterDataHolder {
126 RasterDataHolder() : fReleaseCount(0) {}
bungeman38d909e2016-08-02 14:40:46 -0700127 sk_sp<SkData> fData;
kkinnunen7b94c142015-11-24 07:39:40 -0800128 int fReleaseCount;
129 static void Release(const void* pixels, void* context) {
130 RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
131 self->fReleaseCount++;
132 self->fData.reset();
133 }
134};
reed9ce9d672016-03-17 10:51:11 -0700135static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
kkinnunen7b94c142015-11-24 07:39:40 -0800136 SkASSERT(dataHolder);
137 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700138 dataHolder->fData = create_image_data(&info);
139 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
reed9ce9d672016-03-17 10:51:11 -0700140 RasterDataHolder::Release, dataHolder);
kkinnunen7b94c142015-11-24 07:39:40 -0800141}
reed9ce9d672016-03-17 10:51:11 -0700142static sk_sp<SkImage> create_codec_image() {
kkinnunen7b94c142015-11-24 07:39:40 -0800143 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700144 sk_sp<SkData> data(create_image_data(&info));
kkinnunen7b94c142015-11-24 07:39:40 -0800145 SkBitmap bitmap;
146 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
Hal Canarydb683012016-11-23 08:55:18 -0700147 sk_sp<SkData> src(sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG, 100));
bungeman38d909e2016-08-02 14:40:46 -0700148 return SkImage::MakeFromEncoded(std::move(src));
kkinnunen7b94c142015-11-24 07:39:40 -0800149}
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400150static sk_sp<SkImage> create_gpu_image(GrContext* context, bool withMips = false) {
kkinnunen7b94c142015-11-24 07:39:40 -0800151 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400152 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
153 kBottomLeft_GrSurfaceOrigin, nullptr, withMips));
kkinnunen7b94c142015-11-24 07:39:40 -0800154 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700155 return surface->makeImageSnapshot();
kkinnunen7b94c142015-11-24 07:39:40 -0800156}
reed871872f2015-06-22 12:48:26 -0700157
kkinnunen7b94c142015-11-24 07:39:40 -0800158static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
reed871872f2015-06-22 12:48:26 -0700159 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
Mike Reed6409f842017-07-11 16:03:13 -0400160 sk_sp<SkData> origEncoded = image->encodeToData();
reed871872f2015-06-22 12:48:26 -0700161 REPORTER_ASSERT(reporter, origEncoded);
162 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
163
reed9ce9d672016-03-17 10:51:11 -0700164 sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
scroggo8e6c7ad2016-09-16 08:20:38 -0700165 if (!decoded) {
166 ERRORF(reporter, "failed to decode image!");
167 return;
168 }
reed871872f2015-06-22 12:48:26 -0700169 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700170 assert_equal(reporter, image, nullptr, decoded.get());
reed871872f2015-06-22 12:48:26 -0700171
172 // Now see if we can instantiate an image from a subset of the surface/origEncoded
mtklein5f939ab2016-03-16 10:28:35 -0700173
reed9ce9d672016-03-17 10:51:11 -0700174 decoded = SkImage::MakeFromEncoded(origEncoded, &ir);
reed871872f2015-06-22 12:48:26 -0700175 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700176 assert_equal(reporter, image, &ir, decoded.get());
reed871872f2015-06-22 12:48:26 -0700177}
178
kkinnunen7b94c142015-11-24 07:39:40 -0800179DEF_TEST(ImageEncode, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700180 test_encode(reporter, create_image().get());
reed871872f2015-06-22 12:48:26 -0700181}
182
bsalomon68d91342016-04-12 09:59:58 -0700183DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700184 test_encode(reporter, create_gpu_image(ctxInfo.grContext()).get());
reed871872f2015-06-22 12:48:26 -0700185}
reed759373a2015-07-03 21:01:10 -0700186
reed2dad7692016-08-01 11:12:58 -0700187DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
188 const struct {
reed1ec04d92016-08-05 12:07:41 -0700189 SkCopyPixelsMode fCPM;
190 bool fExpectSameAsMutable;
191 bool fExpectSameAsImmutable;
reed2dad7692016-08-01 11:12:58 -0700192 } recs[] = {
reed1ec04d92016-08-05 12:07:41 -0700193 { kIfMutable_SkCopyPixelsMode, false, true },
194 { kAlways_SkCopyPixelsMode, false, false },
195 { kNever_SkCopyPixelsMode, true, true },
reed2dad7692016-08-01 11:12:58 -0700196 };
197 for (auto rec : recs) {
198 SkPixmap pm;
199 SkBitmap bm;
200 bm.allocN32Pixels(100, 100);
201
reed1ec04d92016-08-05 12:07:41 -0700202 auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700203 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
204 const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
205 REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable);
reedae296442016-08-05 13:19:01 -0700206 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
reed2dad7692016-08-01 11:12:58 -0700207
208 bm.notifyPixelsChanged(); // force a new generation ID
209
210 bm.setImmutable();
reed1ec04d92016-08-05 12:07:41 -0700211 img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700212 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
213 const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
214 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
reedae296442016-08-05 13:19:01 -0700215 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
reed2dad7692016-08-01 11:12:58 -0700216 }
217}
218
fmalitac3470342015-09-04 11:36:39 -0700219// Test that image encoding failures do not break picture serialization/deserialization.
220DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
reede8f30622016-03-23 18:59:25 -0700221 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
fmalitac3470342015-09-04 11:36:39 -0700222 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700223 sk_sp<SkImage> image(surface->makeImageSnapshot());
fmalitac3470342015-09-04 11:36:39 -0700224 REPORTER_ASSERT(reporter, image);
225
226 SkPictureRecorder recorder;
227 SkCanvas* canvas = recorder.beginRecording(100, 100);
228 canvas->drawImage(image, 0, 0);
reedca2622b2016-03-18 07:25:55 -0700229 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitac3470342015-09-04 11:36:39 -0700230 REPORTER_ASSERT(reporter, picture);
Mike Klein88d90712018-01-27 17:30:04 +0000231 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700232
Mike Reedef038482017-12-16 08:41:28 -0500233 bool was_called = false;
234 SkSerialProcs procs;
235 procs.fImageProc = [](SkImage*, void* called) {
236 *(bool*)called = true;
237 return SkData::MakeEmpty();
238 };
239 procs.fImageCtx = &was_called;
fmalitac3470342015-09-04 11:36:39 -0700240
Mike Reedef038482017-12-16 08:41:28 -0500241 REPORTER_ASSERT(reporter, !was_called);
Mike Reed47fdf6c2017-12-20 14:12:07 -0500242 auto data = picture->serialize(&procs);
Mike Reedef038482017-12-16 08:41:28 -0500243 REPORTER_ASSERT(reporter, was_called);
244 REPORTER_ASSERT(reporter, data && data->size() > 0);
fmalitac3470342015-09-04 11:36:39 -0700245
Mike Reedef038482017-12-16 08:41:28 -0500246 auto deserialized = SkPicture::MakeFromData(data->data(), data->size());
247 REPORTER_ASSERT(reporter, deserialized);
Mike Klein88d90712018-01-27 17:30:04 +0000248 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
fmalitac3470342015-09-04 11:36:39 -0700249}
250
fmalita8c0144c2015-07-22 05:56:16 -0700251// Test that a draw that only partially covers the drawing surface isn't
252// interpreted as covering the entire drawing surface (i.e., exercise one of the
253// conditions of SkCanvas::wouldOverwriteEntireSurface()).
254DEF_TEST(Image_RetainSnapshot, reporter) {
255 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
256 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
257 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
reede8f30622016-03-23 18:59:25 -0700258 auto surface(SkSurface::MakeRaster(info));
fmalita8c0144c2015-07-22 05:56:16 -0700259 surface->getCanvas()->clear(0xFF00FF00);
260
261 SkPMColor pixels[4];
262 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
263 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
264 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
265
reed9ce9d672016-03-17 10:51:11 -0700266 sk_sp<SkImage> image1(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700267 REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
268 for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
269 REPORTER_ASSERT(reporter, pixels[i] == green);
270 }
271
272 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700273 paint.setBlendMode(SkBlendMode::kSrc);
fmalita8c0144c2015-07-22 05:56:16 -0700274 paint.setColor(SK_ColorRED);
275
276 surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
277
reed9ce9d672016-03-17 10:51:11 -0700278 sk_sp<SkImage> image2(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700279 REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
280 REPORTER_ASSERT(reporter, pixels[0] == green);
281 REPORTER_ASSERT(reporter, pixels[1] == green);
282 REPORTER_ASSERT(reporter, pixels[2] == green);
283 REPORTER_ASSERT(reporter, pixels[3] == red);
284}
reed80c772b2015-07-30 18:58:23 -0700285
286/////////////////////////////////////////////////////////////////////////////////////////////////
reed80c772b2015-07-30 18:58:23 -0700287
288static void make_bitmap_mutable(SkBitmap* bm) {
289 bm->allocN32Pixels(10, 10);
290}
291
292static void make_bitmap_immutable(SkBitmap* bm) {
293 bm->allocN32Pixels(10, 10);
294 bm->setImmutable();
295}
296
297DEF_TEST(image_newfrombitmap, reporter) {
298 const struct {
299 void (*fMakeProc)(SkBitmap*);
300 bool fExpectPeekSuccess;
301 bool fExpectSharedID;
fmalitaddbbdda2015-08-20 08:47:26 -0700302 bool fExpectLazy;
reed80c772b2015-07-30 18:58:23 -0700303 } rec[] = {
fmalitaddbbdda2015-08-20 08:47:26 -0700304 { make_bitmap_mutable, true, false, false },
305 { make_bitmap_immutable, true, true, false },
reed80c772b2015-07-30 18:58:23 -0700306 };
307
308 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
309 SkBitmap bm;
310 rec[i].fMakeProc(&bm);
311
reed9ce9d672016-03-17 10:51:11 -0700312 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
reed80c772b2015-07-30 18:58:23 -0700313 SkPixmap pmap;
314
315 const bool sharedID = (image->uniqueID() == bm.getGenerationID());
316 REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
317
reed80c772b2015-07-30 18:58:23 -0700318 const bool peekSuccess = image->peekPixels(&pmap);
319 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
fmalitaddbbdda2015-08-20 08:47:26 -0700320
321 const bool lazy = image->isLazyGenerated();
322 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
reed80c772b2015-07-30 18:58:23 -0700323 }
324}
reed6f1216a2015-08-04 08:10:13 -0700325
326///////////////////////////////////////////////////////////////////////////////////////////////////
reed6f1216a2015-08-04 08:10:13 -0700327
reed6f1216a2015-08-04 08:10:13 -0700328#include "SkBitmapCache.h"
329
330/*
331 * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image.
332 * We cache it for performance when drawing into a raster surface.
333 *
334 * A cleaner test would know if each drawImage call triggered a read-back from the gpu,
335 * but we don't have that facility (at the moment) so we use a little internal knowledge
336 * of *how* the raster version is cached, and look for that.
337 */
bsalomon68d91342016-04-12 09:59:58 -0700338DEF_GPUTEST_FOR_RENDERING_CONTEXTS(c, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800339 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
bsalomon8b7451a2016-05-11 06:33:06 -0700340 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
reed6f1216a2015-08-04 08:10:13 -0700341 const uint32_t uniqueID = image->uniqueID();
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400342 const auto desc = SkBitmapCacheDesc::Make(image.get());
reed6f1216a2015-08-04 08:10:13 -0700343
reede8f30622016-03-23 18:59:25 -0700344 auto surface(SkSurface::MakeRaster(info));
reed6f1216a2015-08-04 08:10:13 -0700345
346 // now we can test drawing a gpu-backed image into a cpu-backed surface
347
348 {
349 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400350 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700351 }
352
353 surface->getCanvas()->drawImage(image, 0, 0);
354 {
355 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400356 if (SkBitmapCache::Find(desc, &cachedBitmap)) {
reed6f1216a2015-08-04 08:10:13 -0700357 REPORTER_ASSERT(reporter, cachedBitmap.getGenerationID() == uniqueID);
358 REPORTER_ASSERT(reporter, cachedBitmap.isImmutable());
359 REPORTER_ASSERT(reporter, cachedBitmap.getPixels());
360 } else {
361 // unexpected, but not really a bug, since the cache is global and this test may be
362 // run w/ other threads competing for its budget.
363 SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n");
364 }
365 }
366
367 image.reset(nullptr);
368 {
369 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400370 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700371 }
372}
bsalomon8e74f802016-01-30 10:01:40 -0800373
Brian Osman041f7df2017-02-07 11:23:28 -0500374DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
375 GrContext* context = contextInfo.grContext();
376 sk_gpu_test::TestContext* testContext = contextInfo.testContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500377 GrContextFactory otherFactory;
Robert Phillipscdabbcc2017-06-08 16:03:17 -0400378 ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type());
Brian Osman041f7df2017-02-07 11:23:28 -0500379 testContext->makeCurrent();
380
381 std::function<sk_sp<SkImage>()> imageFactories[] = {
382 create_image,
383 create_codec_image,
384 create_data_image,
385 // Create an image from a picture.
386 create_picture_image,
387 // Create a texture image.
388 [context] { return create_gpu_image(context); },
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400389 // Create a texture image with mips
390 //[context] { return create_gpu_image(context, true); },
Brian Osman041f7df2017-02-07 11:23:28 -0500391 // Create a texture image in a another GrContext.
Brian Salomon55ad7742017-11-17 09:25:23 -0500392 [otherContextInfo] {
393 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Brian Osman041f7df2017-02-07 11:23:28 -0500394 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
Brian Salomon55ad7742017-11-17 09:25:23 -0500395 otherContextInfo.grContext()->flush();
Brian Osman041f7df2017-02-07 11:23:28 -0500396 return otherContextImage;
397 }
398 };
399
Brian Osmane8827d22017-02-07 12:31:02 -0500400 sk_sp<SkColorSpace> dstColorSpaces[] ={
401 nullptr,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500402 SkColorSpace::MakeSRGB(),
Brian Osmane8827d22017-02-07 12:31:02 -0500403 };
Brian Osman041f7df2017-02-07 11:23:28 -0500404
Brian Osmane8827d22017-02-07 12:31:02 -0500405 for (auto& dstColorSpace : dstColorSpaces) {
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400406 for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
407 for (auto factory : imageFactories) {
408 sk_sp<SkImage> image(factory());
409 if (!image) {
410 ERRORF(reporter, "Error creating image.");
411 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500412 }
Robert Phillips87444052017-06-23 14:09:30 -0400413
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400414 sk_sp<SkImage> texImage(image->makeTextureImage(context, dstColorSpace.get(),
415 mipMapped));
416 if (!texImage) {
417 GrContext* imageContext = as_IB(image)->context();
418
419 // We expect to fail if image comes from a different GrContext.
420 if (!image->isTextureBacked() || imageContext == context) {
421 ERRORF(reporter, "makeTextureImage failed.");
422 }
423 continue;
Brian Osmane8827d22017-02-07 12:31:02 -0500424 }
Greg Daniel5f4b09d2018-06-12 16:39:59 -0400425 if (!texImage->isTextureBacked()) {
426 ERRORF(reporter, "makeTextureImage returned non-texture image.");
427 continue;
428 }
429 if (GrMipMapped::kYes == mipMapped &&
430 as_IB(texImage)->peekProxy()->mipMapped() != mipMapped) {
431 ERRORF(reporter, "makeTextureImage returned non-mipmapped texture.");
432 continue;
433 }
434 if (image->isTextureBacked()) {
435 GrSurfaceProxy* origProxy = as_IB(image)->peekProxy();
436 GrSurfaceProxy* copyProxy = as_IB(texImage)->peekProxy();
437
438 if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
439 SkASSERT(origProxy->asTextureProxy());
440 if (GrMipMapped::kNo == mipMapped ||
441 GrMipMapped::kYes == origProxy->asTextureProxy()->mipMapped()) {
442 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
443 }
444 }
445 }
446 if (image->width() != texImage->width() || image->height() != texImage->height()) {
447 ERRORF(reporter, "makeTextureImage changed the image size.");
448 }
449 if (image->alphaType() != texImage->alphaType()) {
450 ERRORF(reporter, "makeTextureImage changed image alpha type.");
451 }
Brian Osmane8827d22017-02-07 12:31:02 -0500452 }
Brian Osman041f7df2017-02-07 11:23:28 -0500453 }
Robert Phillipsd9971c02017-08-02 11:51:43 -0400454 context->flush();
Brian Osman041f7df2017-02-07 11:23:28 -0500455 }
456}
457
bsalomon634b4302016-07-12 18:11:17 -0700458DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
459 GrContext* context = contextInfo.grContext();
460
461 std::function<sk_sp<SkImage>()> imageFactories[] = {
462 create_image,
463 create_codec_image,
464 create_data_image,
465 create_picture_image,
466 [context] { return create_gpu_image(context); },
467 };
Brian Osman041f7df2017-02-07 11:23:28 -0500468 SkColorSpace* legacyColorSpace = nullptr;
bsalomon634b4302016-07-12 18:11:17 -0700469 for (auto factory : imageFactories) {
470 sk_sp<SkImage> image = factory();
471 if (!image->isTextureBacked()) {
472 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Brian Osman041f7df2017-02-07 11:23:28 -0500473 if (!(image = image->makeTextureImage(context, legacyColorSpace))) {
474 continue;
475 }
bsalomon634b4302016-07-12 18:11:17 -0700476 }
477 auto rasterImage = image->makeNonTextureImage();
478 if (!rasterImage) {
479 ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
480 }
481 REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
482 assert_equal(reporter, image.get(), nullptr, rasterImage.get());
483 }
484}
485
bsalomon363bc332016-06-15 14:15:30 -0700486DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_drawAbandonedGpuImage, reporter, contextInfo) {
bsalomone5b651f2016-06-15 07:36:15 -0700487 auto context = contextInfo.grContext();
488 auto image = create_gpu_image(context);
489 auto info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
490 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
Robert Phillips87444052017-06-23 14:09:30 -0400491 image->getTexture()->abandon();
bsalomone5b651f2016-06-15 07:36:15 -0700492 surface->getCanvas()->drawImage(image, 0, 0);
493}
494
Brian Salomonbdecacf2018-02-02 20:32:49 -0500495DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
496 for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
497 static constexpr int kSize = 10;
498 SkColorType colorType = static_cast<SkColorType>(ct);
499 bool can = ctxInfo.grContext()->colorTypeSupportedAsImage(colorType);
500 auto* gpu = ctxInfo.grContext()->contextPriv().getGpu();
501 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Brian Osman2b23c4b2018-06-01 12:25:08 -0400502 nullptr, kSize, kSize, colorType, false, GrMipMapped::kNo);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500503 auto img =
504 SkImage::MakeFromTexture(ctxInfo.grContext(), backendTex, kTopLeft_GrSurfaceOrigin,
505 colorType, kOpaque_SkAlphaType, nullptr);
506 REPORTER_ASSERT(reporter, can == SkToBool(img),
Oleg Maximenko5d4604b2018-02-26 17:58:58 +0300507 "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
Brian Salomonbdecacf2018-02-02 20:32:49 -0500508 colorType);
509
510 img.reset();
511 ctxInfo.grContext()->flush();
512 if (backendTex.isValid()) {
Brian Salomon26102cb2018-03-09 09:33:19 -0500513 gpu->deleteTestingOnlyBackendTexture(backendTex);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500514 }
515 }
516}
517
Brian Salomon9708af82018-02-05 12:57:10 -0500518DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
519 SkBitmap bmp;
520 bmp.allocPixels(
521 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
522 for (int y = 0; y < 256; ++y) {
523 for (int x = 0; x < 256; ++x) {
524 *bmp.getAddr32(x, y) =
525 SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
526 }
527 }
528 auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(ctxInfo.grContext(), nullptr);
529 if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
530 ERRORF(reporter, "Failed to make unpremul texture image.");
531 return;
532 }
533 // The GPU backend always unpremuls the values stored in the texture because it assumes they
534 // are premul values. (skbug.com/7580).
535 if (false) {
536 SkBitmap unpremul;
537 unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
538 kUnpremul_SkAlphaType, nullptr));
539 if (!texImage->readPixels(unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(), 0,
540 0)) {
541 ERRORF(reporter, "Unpremul readback failed.");
542 return;
543 }
544 for (int y = 0; y < 256; ++y) {
545 for (int x = 0; x < 256; ++x) {
546 if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
547 ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
548 *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
549 return;
550 }
551 }
552 }
553 }
554 SkBitmap premul;
555 premul.allocPixels(
556 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
557 if (!texImage->readPixels(premul.info(), premul.getPixels(), premul.rowBytes(), 0, 0)) {
558 ERRORF(reporter, "Unpremul readback failed.");
559 return;
560 }
561 for (int y = 0; y < 256; ++y) {
562 for (int x = 0; x < 256; ++x) {
563 // Treat bmp's color as a pm color even though it may be the r/b swap of a PM color.
564 // SkPremultiplyColor acts the same on both channels.
565 uint32_t origColor = SkPreMultiplyColor(*bmp.getAddr32(x, y));
566 int32_t origA = (origColor >> 24) & 0xff;
567 int32_t origB = (origColor >> 16) & 0xff;
568 int32_t origG = (origColor >> 8) & 0xff;
569 int32_t origR = (origColor >> 0) & 0xff;
570 uint32_t read = *premul.getAddr32(x, y);
571 int32_t readA = (read >> 24) & 0xff;
572 int32_t readB = (read >> 16) & 0xff;
573 int32_t readG = (read >> 8) & 0xff;
574 int32_t readR = (read >> 0) & 0xff;
575 // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
576 // bit of tolerance for GPU vs CPU premul math.
577 int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
578 if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
579 SkTAbs(readR - origR) > tol) {
580 ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) at %d, %d.",
581 *bmp.getAddr32(x, y), *premul.getAddr32(x, y), x, y);
582 return;
583 }
584 }
585 }
586}
587
Brian Salomon8a8dd332018-05-24 14:08:31 -0400588DEF_GPUTEST(AbandonedContextImage, reporter, options) {
589 using Factory = sk_gpu_test::GrContextFactory;
590 for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
591 auto type = static_cast<Factory::ContextType>(ct);
592 std::unique_ptr<Factory> factory(new Factory);
593 if (!factory->get(type)) {
594 continue;
595 }
596
597 sk_sp<SkImage> img;
598 auto gsurf = SkSurface::MakeRenderTarget(
599 factory->get(type), SkBudgeted::kYes,
600 SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
601 nullptr);
602 if (!gsurf) {
603 continue;
604 }
605 img = gsurf->makeImageSnapshot();
606 gsurf.reset();
607
608 auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
609
610 REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
611 REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->getGrContext()));
612
613 factory->get(type)->abandonContext();
614 REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
615 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
616 // This shouldn't crash.
617 rsurf->getCanvas()->drawImage(img, 0, 0);
618
619 // Give up all other refs on GrContext.
620 factory.reset(nullptr);
621 REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
622 // This shouldn't crash.
623 rsurf->getCanvas()->drawImage(img, 0, 0);
624 }
625}
626
kkinnunen4e184132015-11-17 22:53:28 -0800627class EmptyGenerator : public SkImageGenerator {
628public:
629 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
630};
631
kkinnunen7b94c142015-11-24 07:39:40 -0800632DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800633 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700634 SkPixmap pmap(info, nullptr, 0);
635 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
636 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
637 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500638 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
639 skstd::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800640}
641
kkinnunen7b94c142015-11-24 07:39:40 -0800642DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800643 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
644 size_t rowBytes = info.minRowBytes();
Mike Reedf0ffb892017-10-03 14:47:21 -0400645 size_t size = info.computeByteSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700646 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800647 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700648 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800649 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700650 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800651 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800652}
653
kkinnunen4e184132015-11-17 22:53:28 -0800654static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
655 for (int i = 0; i < count; ++i) {
656 if (pixels[i] != expected) {
657 return false;
658 }
659 }
660 return true;
661}
662
Robert Phillips3500b772017-01-27 10:11:42 -0500663static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700664 if (!image) {
665 ERRORF(reporter, "Failed to create image!");
666 return;
667 }
kkinnunen7b94c142015-11-24 07:39:40 -0800668 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800669 const SkPMColor notExpected = ~expected;
670
671 const int w = 2, h = 2;
672 const size_t rowBytes = w * sizeof(SkPMColor);
673 SkPMColor pixels[w*h];
674
675 SkImageInfo info;
676
677 info = SkImageInfo::MakeUnknown(w, h);
678 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
679
680 // out-of-bounds should fail
681 info = SkImageInfo::MakeN32Premul(w, h);
682 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
683 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
684 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
685 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
686
687 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800688 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800689 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
690 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
691
692 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800693 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800694 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
695 image->width() - w, image->height() - h));
696 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
697
698 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800699 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800700 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
701 REPORTER_ASSERT(reporter, pixels[3] == expected);
702 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
703
704 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800705 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800706 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
707 image->width() - 1, image->height() - 1));
708 REPORTER_ASSERT(reporter, pixels[0] == expected);
709 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
710}
kkinnunen7b94c142015-11-24 07:39:40 -0800711DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700712 sk_sp<SkImage> image(create_image());
Robert Phillips3500b772017-01-27 10:11:42 -0500713 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800714
reed9ce9d672016-03-17 10:51:11 -0700715 image = create_data_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500716 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800717
718 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700719 image = create_rasterproc_image(&dataHolder);
Robert Phillips3500b772017-01-27 10:11:42 -0500720 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800721 image.reset();
722 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
723
reed9ce9d672016-03-17 10:51:11 -0700724 image = create_codec_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500725 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800726}
egdanielab527a52016-06-28 08:07:26 -0700727DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Robert Phillips3500b772017-01-27 10:11:42 -0500728 image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800729}
kkinnunen4e184132015-11-17 22:53:28 -0800730
731static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
Cary Clark4f5a79c2018-02-07 15:51:00 -0500732 const SkBitmap& bitmap) {
kkinnunen4e184132015-11-17 22:53:28 -0800733 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
734 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700735 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800736
Cary Clark4f5a79c2018-02-07 15:51:00 -0500737 REPORTER_ASSERT(reporter, bitmap.isImmutable());
kkinnunen4e184132015-11-17 22:53:28 -0800738
kkinnunen4e184132015-11-17 22:53:28 -0800739 REPORTER_ASSERT(reporter, bitmap.getPixels());
740
741 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
742 SkPMColor imageColor;
743 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
744 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
745}
746
Cary Clark4f5a79c2018-02-07 15:51:00 -0500747static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700748 if (!image) {
749 ERRORF(reporter, "Failed to create image.");
750 return;
751 }
kkinnunen7b94c142015-11-24 07:39:40 -0800752 SkBitmap bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500753 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
754 check_legacy_bitmap(reporter, image, bitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800755
756 // Test subsetting to exercise the rowBytes logic.
757 SkBitmap tmp;
758 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
759 image->height() / 2)));
reed9ce9d672016-03-17 10:51:11 -0700760 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
761 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800762
763 SkBitmap subsetBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -0500764 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
765 check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap);
kkinnunen7b94c142015-11-24 07:39:40 -0800766}
767DEF_TEST(ImageLegacyBitmap, reporter) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500768 sk_sp<SkImage> image(create_image());
769 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800770
Cary Clark4f5a79c2018-02-07 15:51:00 -0500771 image = create_data_image();
772 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800773
Cary Clark4f5a79c2018-02-07 15:51:00 -0500774 RasterDataHolder dataHolder;
775 image = create_rasterproc_image(&dataHolder);
776 test_legacy_bitmap(reporter, image.get());
777 image.reset();
778 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
kkinnunen7b94c142015-11-24 07:39:40 -0800779
Cary Clark4f5a79c2018-02-07 15:51:00 -0500780 image = create_codec_image();
781 test_legacy_bitmap(reporter, image.get());
kkinnunen4e184132015-11-17 22:53:28 -0800782}
bsalomon68d91342016-04-12 09:59:58 -0700783DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
Cary Clark4f5a79c2018-02-07 15:51:00 -0500784 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
785 test_legacy_bitmap(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800786}
kkinnunen4e184132015-11-17 22:53:28 -0800787
kkinnunen7b94c142015-11-24 07:39:40 -0800788static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700789 if (!image) {
790 ERRORF(reporter, "Failed to create image!");
791 return;
792 }
reed6ceeebd2016-03-09 14:26:26 -0800793 SkPixmap pm;
794 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800795 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
796 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800797 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800798 REPORTER_ASSERT(reporter, 20 == info.width());
799 REPORTER_ASSERT(reporter, 20 == info.height());
800 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
801 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
802 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800803 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
804 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800805 }
kkinnunen7b94c142015-11-24 07:39:40 -0800806}
807DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700808 sk_sp<SkImage> image(create_image());
809 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800810
reed9ce9d672016-03-17 10:51:11 -0700811 image = create_data_image();
812 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800813
814 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700815 image = create_rasterproc_image(&dataHolder);
816 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800817 image.reset();
818 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
819
reed9ce9d672016-03-17 10:51:11 -0700820 image = create_codec_image();
821 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800822}
egdanielab527a52016-06-28 08:07:26 -0700823DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700824 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
reed9ce9d672016-03-17 10:51:11 -0700825 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800826}
kkinnunen4e184132015-11-17 22:53:28 -0800827
kkinnunen7b94c142015-11-24 07:39:40 -0800828struct TextureReleaseChecker {
829 TextureReleaseChecker() : fReleaseCount(0) {}
830 int fReleaseCount;
831 static void Release(void* self) {
832 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800833 }
834};
Brian Osman13dddce2017-05-09 13:19:50 -0400835
brianosmandb2cb102016-07-22 07:22:04 -0700836DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
837 const int kWidth = 10;
838 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400839 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
Greg Daniel7ef28f32017-04-20 16:41:55 +0000840
841 GrContext* ctx = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500842 GrGpu* gpu = ctx->contextPriv().getGpu();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000843
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500844 GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture(
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500845 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true, GrMipMapped::kNo);
brianosmandb2cb102016-07-22 07:22:04 -0700846
kkinnunen7b94c142015-11-24 07:39:40 -0800847 TextureReleaseChecker releaseChecker;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000848 GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
reed9ce9d672016-03-17 10:51:11 -0700849 sk_sp<SkImage> refImg(
Greg Danielf5d87582017-12-18 14:48:15 -0500850 SkImage::MakeFromTexture(ctx, backendTex, texOrigin, kRGBA_8888_SkColorType,
851 kPremul_SkAlphaType, nullptr,
reed9ce9d672016-03-17 10:51:11 -0700852 TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800853
Robert Phillips3390e152017-01-31 17:53:34 -0500854 GrSurfaceOrigin readBackOrigin;
Robert Phillipsc5509952018-04-04 15:54:55 -0400855 GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
856 if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) {
857 ERRORF(reporter, "backend mismatch\n");
Robert Phillips3390e152017-01-31 17:53:34 -0500858 }
Robert Phillipsc5509952018-04-04 15:54:55 -0400859 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex));
Greg Daniel7ef28f32017-04-20 16:41:55 +0000860 if (readBackOrigin != texOrigin) {
861 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500862 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000863 REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
Robert Phillips3390e152017-01-31 17:53:34 -0500864
kkinnunen4e184132015-11-17 22:53:28 -0800865 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800866 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800867 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800868 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
brianosmandb2cb102016-07-22 07:22:04 -0700869
Brian Salomon26102cb2018-03-09 09:33:19 -0500870 gpu->deleteTestingOnlyBackendTexture(backendTex);
kkinnunen4e184132015-11-17 22:53:28 -0800871}
bsalomon0d996862016-03-09 18:44:43 -0800872
Brian Salomondcfca432017-11-15 15:48:03 -0500873static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
Hal Canaryf7d3f612018-03-22 15:17:42 -0400874 const char* testName,
Brian Osman63bc48d2017-11-07 10:37:00 -0500875 std::function<sk_sp<SkImage>(GrContext*)> imageMaker) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400876 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
Brian Salomondcfca432017-11-15 15:48:03 -0500877 GrContextFactory testFactory(options);
Brian Osmanceb7a422017-06-21 15:10:33 -0400878 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
879 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
880 GrContext* ctx = ctxInfo.grContext();
881 if (!ctx) {
882 continue;
883 }
Brian Osman13dddce2017-05-09 13:19:50 -0400884
Brian Osmanceb7a422017-06-21 15:10:33 -0400885 // If we don't have proper support for this feature, the factory will fallback to returning
886 // codec-backed images. Those will "work", but some of our checks will fail because we
887 // expect the cross-context images not to work on multiple contexts at once.
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400888 if (!ctx->contextPriv().caps()->crossContextTextureSupport()) {
Brian Osmanceb7a422017-06-21 15:10:33 -0400889 continue;
890 }
Brian Osman13dddce2017-05-09 13:19:50 -0400891
Brian Osmanceb7a422017-06-21 15:10:33 -0400892 // We test three lifetime patterns for a single context:
893 // 1) Create image, free image
894 // 2) Create image, draw, flush, free image
895 // 3) Create image, draw, free image, flush
896 // ... and then repeat the last two patterns with drawing on a second* context:
897 // 4) Create image, draw*, flush*, free image
898 // 5) Create image, draw*, free iamge, flush*
Brian Osman13dddce2017-05-09 13:19:50 -0400899
Brian Osmanceb7a422017-06-21 15:10:33 -0400900 // Case #1: Create image, free image
901 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500902 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osmanceb7a422017-06-21 15:10:33 -0400903 refImg.reset(nullptr); // force a release of the image
904 }
Brian Osman13dddce2017-05-09 13:19:50 -0400905
Brian Osmanceb7a422017-06-21 15:10:33 -0400906 SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
907 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
Hal Canaryf7d3f612018-03-22 15:17:42 -0400908 if (!surface) {
909 ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
910 continue;
911 }
912
Brian Osmanceb7a422017-06-21 15:10:33 -0400913 SkCanvas* canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400914
Brian Osmanceb7a422017-06-21 15:10:33 -0400915 // Case #2: Create image, draw, flush, free image
916 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500917 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400918
Brian Osmanceb7a422017-06-21 15:10:33 -0400919 canvas->drawImage(refImg, 0, 0);
920 canvas->flush();
Brian Osman13dddce2017-05-09 13:19:50 -0400921
Brian Osmanceb7a422017-06-21 15:10:33 -0400922 refImg.reset(nullptr); // force a release of the image
923 }
Brian Osman13dddce2017-05-09 13:19:50 -0400924
Brian Osmanceb7a422017-06-21 15:10:33 -0400925 // Case #3: Create image, draw, free image, flush
926 {
Brian Osman63bc48d2017-11-07 10:37:00 -0500927 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400928
Brian Osmanceb7a422017-06-21 15:10:33 -0400929 canvas->drawImage(refImg, 0, 0);
930 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400931
Brian Osmanceb7a422017-06-21 15:10:33 -0400932 canvas->flush();
933 }
Brian Osman13dddce2017-05-09 13:19:50 -0400934
Brian Osmanceb7a422017-06-21 15:10:33 -0400935 // Configure second context
936 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400937
Brian Osmanceb7a422017-06-21 15:10:33 -0400938 ContextInfo otherContextInfo = testFactory.getSharedContextInfo(ctx);
939 GrContext* otherCtx = otherContextInfo.grContext();
940 sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
Brian Osman13dddce2017-05-09 13:19:50 -0400941
Brian Osmanceb7a422017-06-21 15:10:33 -0400942 // Creating a context in a share group may fail
943 if (!otherCtx) {
944 continue;
945 }
Brian Osman13dddce2017-05-09 13:19:50 -0400946
Brian Osmanceb7a422017-06-21 15:10:33 -0400947 surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
948 canvas = surface->getCanvas();
Brian Osman13dddce2017-05-09 13:19:50 -0400949
Brian Osmanceb7a422017-06-21 15:10:33 -0400950 // Case #4: Create image, draw*, flush*, free image
951 {
952 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500953 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400954
Brian Osmanceb7a422017-06-21 15:10:33 -0400955 otherTestContext->makeCurrent();
956 canvas->drawImage(refImg, 0, 0);
957 canvas->flush();
Brian Osman13dddce2017-05-09 13:19:50 -0400958
Brian Osmanceb7a422017-06-21 15:10:33 -0400959 testContext->makeCurrent();
960 refImg.reset(nullptr); // force a release of the image
961 }
Brian Osman13dddce2017-05-09 13:19:50 -0400962
Brian Osmanceb7a422017-06-21 15:10:33 -0400963 // Case #5: Create image, draw*, free image, flush*
964 {
965 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500966 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400967
Brian Osmanceb7a422017-06-21 15:10:33 -0400968 otherTestContext->makeCurrent();
969 canvas->drawImage(refImg, 0, 0);
Brian Osman13dddce2017-05-09 13:19:50 -0400970
Brian Osmanceb7a422017-06-21 15:10:33 -0400971 testContext->makeCurrent();
972 refImg.reset(nullptr); // force a release of the image
Brian Osman13dddce2017-05-09 13:19:50 -0400973
Brian Osmanceb7a422017-06-21 15:10:33 -0400974 otherTestContext->makeCurrent();
975 canvas->flush();
Greg Daniel3f475d92017-07-25 16:26:35 -0400976
Greg Daniel26b50a42018-03-08 09:49:58 -0500977 // This is specifically here for vulkan to guarantee the command buffer will finish
978 // which is when we call the ReleaseProc.
979 otherCtx->contextPriv().getGpu()->testingOnly_flushGpuAndSync();
Brian Osmanceb7a422017-06-21 15:10:33 -0400980 }
Brian Osman13dddce2017-05-09 13:19:50 -0400981
Brian Osmanceb7a422017-06-21 15:10:33 -0400982 // Case #6: Verify that only one context can be using the image at a time
983 {
984 testContext->makeCurrent();
Brian Osman63bc48d2017-11-07 10:37:00 -0500985 sk_sp<SkImage> refImg(imageMaker(ctx));
Brian Osman13dddce2017-05-09 13:19:50 -0400986
Brian Osmanceb7a422017-06-21 15:10:33 -0400987 // Any context should be able to borrow the texture at this point
988 sk_sp<SkColorSpace> texColorSpace;
989 sk_sp<GrTextureProxy> proxy = as_IB(refImg)->asTextureProxyRef(
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400990 ctx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr);
Brian Osmanceb7a422017-06-21 15:10:33 -0400991 REPORTER_ASSERT(reporter, proxy);
Brian Osman13dddce2017-05-09 13:19:50 -0400992
Brian Osmanceb7a422017-06-21 15:10:33 -0400993 // But once it's borrowed, no other context should be able to borrow
994 otherTestContext->makeCurrent();
995 sk_sp<GrTextureProxy> otherProxy = as_IB(refImg)->asTextureProxyRef(
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400996 otherCtx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr);
Brian Osmanceb7a422017-06-21 15:10:33 -0400997 REPORTER_ASSERT(reporter, !otherProxy);
998
999 // Original context (that's already borrowing) should be okay
1000 testContext->makeCurrent();
1001 sk_sp<GrTextureProxy> proxySecondRef = as_IB(refImg)->asTextureProxyRef(
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001002 ctx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr);
Brian Osmanceb7a422017-06-21 15:10:33 -04001003 REPORTER_ASSERT(reporter, proxySecondRef);
1004
Greg Danielabba9982018-02-01 10:07:57 -05001005 // Release first ref from the original context
Brian Osmanceb7a422017-06-21 15:10:33 -04001006 proxy.reset(nullptr);
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();
1011 otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(),
1012 nullptr, &texColorSpace, nullptr);
1013 REPORTER_ASSERT(reporter, !otherProxy);
1014
1015 // Release second ref from the original context
1016 testContext->makeCurrent();
Brian Osmanceb7a422017-06-21 15:10:33 -04001017 proxySecondRef.reset(nullptr);
1018
Greg Daniel48661b82018-01-22 16:11:35 -05001019 // Now we should be able to borrow the texture from the other context
Brian Osmanceb7a422017-06-21 15:10:33 -04001020 otherTestContext->makeCurrent();
Brian Salomon2bbdcc42017-09-07 12:36:34 -04001021 otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(),
1022 nullptr, &texColorSpace, nullptr);
Greg Daniel48661b82018-01-22 16:11:35 -05001023 REPORTER_ASSERT(reporter, otherProxy);
Brian Osmanceb7a422017-06-21 15:10:33 -04001024
1025 // Release everything
1026 otherProxy.reset(nullptr);
1027 refImg.reset(nullptr);
1028 }
Brian Osman13dddce2017-05-09 13:19:50 -04001029 }
1030}
1031
Brian Salomondcfca432017-11-15 15:48:03 -05001032DEF_GPUTEST(SkImage_MakeCrossContextFromEncodedRelease, reporter, options) {
Hal Canaryc465d132017-12-08 10:21:31 -05001033 sk_sp<SkData> data = GetResourceAsData("images/mandrill_128.png");
Brian Osman63bc48d2017-11-07 10:37:00 -05001034 SkASSERT(data.get());
1035
Hal Canaryf7d3f612018-03-22 15:17:42 -04001036 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromEncodedRelease",
1037 [&data](GrContext* ctx) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001038 return SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr);
1039 });
1040}
1041
Brian Salomondcfca432017-11-15 15:48:03 -05001042DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001043 SkBitmap bitmap;
1044 SkPixmap pixmap;
Hal Canaryc465d132017-12-08 10:21:31 -05001045 SkAssertResult(GetResourceAsBitmap("images/mandrill_128.png", &bitmap) && bitmap.peekPixels(&pixmap));
Brian Osman63bc48d2017-11-07 10:37:00 -05001046
Hal Canaryf7d3f612018-03-22 15:17:42 -04001047 test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
1048 [&pixmap](GrContext* ctx) {
Brian Osman63bc48d2017-11-07 10:37:00 -05001049 return SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr);
1050 });
1051}
1052
Brian Osman052ef692018-03-27 09:56:31 -04001053DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
1054
1055 for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
1056 SkAutoPixmapStorage pixmap;
1057 pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
1058
1059 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
1060 GrContextFactory testFactory(options);
1061 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
1062 ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
1063 GrContext* ctx = ctxInfo.grContext();
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001064 if (!ctx || !ctx->contextPriv().caps()->crossContextTextureSupport()) {
Brian Osman052ef692018-03-27 09:56:31 -04001065 continue;
1066 }
1067
1068 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr);
1069 REPORTER_ASSERT(reporter, image);
1070
1071 sk_sp<SkColorSpace> texColorSpace;
1072 sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(
1073 ctx, GrSamplerState::ClampNearest(), nullptr, &texColorSpace, nullptr);
1074 REPORTER_ASSERT(reporter, proxy);
1075
1076 bool expectAlpha = kAlpha_8_SkColorType == ct;
1077 REPORTER_ASSERT(reporter, expectAlpha == GrPixelConfigIsAlphaOnly(proxy->config()));
1078 }
1079 }
1080}
1081
Eric Karl914a36b2017-10-12 12:44:50 -07001082DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
1083 GrContext* context = ctxInfo.grContext();
1084 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
1085 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
1086
1087 GrContextFactory otherFactory;
1088 ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
1089
1090 testContext->makeCurrent();
1091 REPORTER_ASSERT(reporter, proxy);
1092 auto createLarge = [context] {
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001093 return create_image_large(context->contextPriv().caps()->maxTextureSize());
Eric Karl914a36b2017-10-12 12:44:50 -07001094 };
1095 struct {
1096 std::function<sk_sp<SkImage> ()> fImageFactory;
1097 bool fExpectation;
1098 bool fCanTakeDirectly;
1099 } testCases[] = {
1100 { create_image, true, false },
1101 { create_codec_image, true, false },
1102 { create_data_image, true, false },
1103 { create_picture_image, true, false },
1104 { [context] { return create_gpu_image(context); }, true, true },
1105 // Create a texture image in a another GrContext.
Brian Salomon55ad7742017-11-17 09:25:23 -05001106 { [otherContextInfo] {
1107 auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
Eric Karl914a36b2017-10-12 12:44:50 -07001108 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
Brian Salomon55ad7742017-11-17 09:25:23 -05001109 otherContextInfo.grContext()->flush();
Eric Karl914a36b2017-10-12 12:44:50 -07001110 return otherContextImage;
1111 }, false, false },
1112 // Create an image that is too large to be texture backed.
1113 { createLarge, false, false }
1114 };
1115
1116 for (auto testCase : testCases) {
1117 sk_sp<SkImage> image(testCase.fImageFactory());
1118 if (!image) {
1119 ERRORF(reporter, "Failed to create image!");
1120 continue;
1121 }
1122
Robert Phillipsba375a82018-04-11 10:08:06 -04001123 GrBackendTexture origBackend = image->getBackendTexture(true);
1124 if (testCase.fCanTakeDirectly) {
1125 SkASSERT(origBackend.isValid());
1126 }
1127
1128 GrBackendTexture newBackend;
Eric Karl914a36b2017-10-12 12:44:50 -07001129 SkImage::BackendTextureReleaseProc proc;
Robert Phillipsba375a82018-04-11 10:08:06 -04001130 bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
1131 &newBackend, &proc);
Eric Karl914a36b2017-10-12 12:44:50 -07001132 if (result != testCase.fExpectation) {
1133 static const char *const kFS[] = { "fail", "succeed" };
1134 ERRORF(reporter, "This image was expected to %s but did not.",
1135 kFS[testCase.fExpectation]);
1136 }
1137
Robert Phillipsba375a82018-04-11 10:08:06 -04001138 if (result) {
1139 SkASSERT(newBackend.isValid());
1140 }
1141
1142 bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
Eric Karl914a36b2017-10-12 12:44:50 -07001143 if (testCase.fCanTakeDirectly != tookDirectly) {
1144 static const char *const kExpectedState[] = { "not expected", "expected" };
1145 ERRORF(reporter, "This backend texture was %s to be taken directly.",
1146 kExpectedState[testCase.fCanTakeDirectly]);
1147 }
1148
Eric Karl914a36b2017-10-12 12:44:50 -07001149 context->flush();
1150 }
1151}
reedeb560282016-07-26 19:42:04 -07001152
1153///////////////////////////////////////////////////////////////////////////////////////////////////
1154
Matt Sarett0bb6f382017-03-06 10:28:24 -05001155static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1156 SkPictureRecorder recorder;
1157 SkCanvas* canvas = recorder.beginRecording(10, 10);
1158 canvas->clear(SK_ColorCYAN);
1159 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1160 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1161};
1162
1163DEF_TEST(Image_ColorSpace, r) {
1164 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
Hal Canaryc465d132017-12-08 10:21:31 -05001165 sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001166 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1167
Hal Canaryc465d132017-12-08 10:21:31 -05001168 image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
Matt Sarett0bb6f382017-03-06 10:28:24 -05001169 SkColorSpaceTransferFn fn;
1170 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1171 REPORTER_ASSERT(r, success);
1172 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.fG));
1173
1174 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1175 SkColorSpace::kRec2020_Gamut);
1176 image = create_picture_image(rec2020);
1177 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1178
1179 SkBitmap bitmap;
1180 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1181 bitmap.allocPixels(info);
1182 image = SkImage::MakeFromBitmap(bitmap);
1183 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1184
1185 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1186 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1187 image = surface->makeImageSnapshot();
1188 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1189
1190 surface = SkSurface::MakeRaster(info);
1191 image = surface->makeImageSnapshot();
1192 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1193}
1194
Matt Sarett6de13102017-03-14 14:10:48 -04001195DEF_TEST(Image_makeColorSpace, r) {
1196 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1197 SkColorSpace::kDCIP3_D65_Gamut);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001198 SkColorSpaceTransferFn fn;
1199 fn.fA = 1.f; fn.fB = 0.f; fn.fC = 0.f; fn.fD = 0.f; fn.fE = 0.f; fn.fF = 0.f; fn.fG = 1.8f;
1200 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkColorSpace::kAdobeRGB_Gamut);
Matt Sarett6de13102017-03-14 14:10:48 -04001201
1202 SkBitmap srgbBitmap;
1203 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1204 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1205 srgbBitmap.setImmutable();
1206 sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
Matt Sarettcb874232017-04-05 11:41:27 -04001207 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore);
Matt Sarett6de13102017-03-14 14:10:48 -04001208 SkBitmap p3Bitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001209 bool success = p3Image->asLegacyBitmap(&p3Bitmap);
Mike Klein55d330c2018-04-23 15:39:21 -04001210
1211 auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
1212
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(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1215 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1216 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1217
Matt Sarettcb874232017-04-05 11:41:27 -04001218 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut,
1219 SkTransferFunctionBehavior::kIgnore);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001220 SkBitmap adobeBitmap;
Cary Clark4f5a79c2018-02-07 15:51:00 -05001221 success = adobeImage->asLegacyBitmap(&adobeBitmap);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001222 REPORTER_ASSERT(r, success);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001223 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1224 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1225 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1226
Hal Canaryc465d132017-12-08 10:21:31 -05001227 srgbImage = GetResourceAsImage("images/1x1.png");
Matt Sarettcb874232017-04-05 11:41:27 -04001228 p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001229 success = p3Image->asLegacyBitmap(&p3Bitmap);
Matt Sarett6de13102017-03-14 14:10:48 -04001230 REPORTER_ASSERT(r, success);
Matt Sarett6de13102017-03-14 14:10:48 -04001231 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1232 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1233 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1234}
1235
Matt Sarett0bb6f382017-03-06 10:28:24 -05001236///////////////////////////////////////////////////////////////////////////////////////////////////
1237
reedeb560282016-07-26 19:42:04 -07001238static void make_all_premul(SkBitmap* bm) {
1239 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1240 for (int a = 0; a < 256; ++a) {
1241 for (int r = 0; r < 256; ++r) {
1242 // make all valid premul combinations
1243 int c = SkTMin(a, r);
1244 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1245 }
1246 }
1247}
1248
1249static bool equal(const SkBitmap& a, const SkBitmap& b) {
1250 SkASSERT(a.width() == b.width());
1251 SkASSERT(a.height() == b.height());
1252 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001253 for (int x = 0; x < a.width(); ++x) {
1254 SkPMColor pa = *a.getAddr32(x, y);
1255 SkPMColor pb = *b.getAddr32(x, y);
1256 if (pa != pb) {
1257 return false;
1258 }
reedeb560282016-07-26 19:42:04 -07001259 }
1260 }
1261 return true;
1262}
1263
1264DEF_TEST(image_roundtrip_encode, reporter) {
1265 SkBitmap bm0;
1266 make_all_premul(&bm0);
1267
1268 auto img0 = SkImage::MakeFromBitmap(bm0);
Mike Reed6409f842017-07-11 16:03:13 -04001269 sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
reedeb560282016-07-26 19:42:04 -07001270 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001271
reedeb560282016-07-26 19:42:04 -07001272 SkBitmap bm1;
1273 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1274 img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001275
reedeb560282016-07-26 19:42:04 -07001276 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1277}
1278
1279DEF_TEST(image_roundtrip_premul, reporter) {
1280 SkBitmap bm0;
1281 make_all_premul(&bm0);
1282
1283 SkBitmap bm1;
1284 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1285 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1286
1287 SkBitmap bm2;
1288 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1289 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1290
1291 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1292}
Brian Osmanb8a13922017-04-26 15:28:30 -04001293
1294///////////////////////////////////////////////////////////////////////////////////////////////////
1295
1296static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
1297 // Verify that all pixels contain the original test color
1298 for (auto y = 0; y < pmap->height(); ++y) {
1299 for (auto x = 0; x < pmap->width(); ++x) {
1300 uint32_t pixel = *pmap->addr32(x, y);
1301 if (pixel != expected) {
1302 ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
1303 x, y, pixel, expected);
1304 return;
1305 }
1306 }
1307 }
1308}
1309
1310static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
1311 uint32_t expected) {
1312 SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
1313
1314 // Make sure to test kDisallow first, so we don't just get a cache hit in that case
1315 for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
1316 SkAutoPixmapStorage scaled;
1317 scaled.alloc(info);
1318 if (!image->scalePixels(scaled, kLow_SkFilterQuality, chint)) {
1319 ERRORF(reporter, "Failed to scale image");
1320 continue;
1321 }
1322
1323 check_scaled_pixels(reporter, &scaled, expected);
1324 }
1325}
1326
1327DEF_TEST(ImageScalePixels, reporter) {
1328 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1329 const SkColor red = SK_ColorRED;
1330
1331 // Test raster image
1332 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
1333 sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
1334 surface->getCanvas()->clear(red);
1335 sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
1336 test_scale_pixels(reporter, rasterImage.get(), pmRed);
1337
1338 // Test encoded image
Mike Reed6409f842017-07-11 16:03:13 -04001339 sk_sp<SkData> data = rasterImage->encodeToData();
Brian Osmanb8a13922017-04-26 15:28:30 -04001340 sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
1341 test_scale_pixels(reporter, codecImage.get(), pmRed);
1342}
1343
Brian Osmanb8a13922017-04-26 15:28:30 -04001344DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
1345 const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
1346 const SkColor red = SK_ColorRED;
1347
1348 SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
1349 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
1350 info);
1351 surface->getCanvas()->clear(red);
1352 sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
1353 test_scale_pixels(reporter, gpuImage.get(), pmRed);
1354}
Mike Reedc4e31092018-01-30 11:15:27 -05001355
1356static sk_sp<SkImage> any_image_will_do() {
1357 return GetResourceAsImage("images/mandrill_32.png");
1358}
1359
1360DEF_TEST(Image_nonfinite_dst, reporter) {
1361 auto surf = SkSurface::MakeRasterN32Premul(10, 10);
1362 auto img = any_image_will_do();
1363 SkPaint paint;
1364
1365 for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
1366 for (int bits = 1; bits <= 15; ++bits) {
1367 SkRect dst = { 0, 0, 10, 10 };
1368 if (bits & 1) dst.fLeft = bad;
1369 if (bits & 2) dst.fTop = bad;
1370 if (bits & 4) dst.fRight = bad;
1371 if (bits & 8) dst.fBottom = bad;
1372
1373 surf->getCanvas()->drawImageRect(img, dst, &paint);
1374
1375 // we should draw nothing
1376 sk_tool_utils::PixelIter iter(surf.get());
1377 while (void* addr = iter.next()) {
1378 REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
1379 }
1380 }
1381 }
1382}
1383