blob: 22004a07c6b7302777628519fc574f1e10d2881c [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"
fmalita2be71252015-09-03 07:17:25 -070024#include "SkPixelSerializer.h"
reed871872f2015-06-22 12:48:26 -070025#include "SkRRect.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"
Hal Canarydb683012016-11-23 08:55:18 -070032#include "sk_tool_utils.h"
33
brianosmandb2cb102016-07-22 07:22:04 -070034#if SK_SUPPORT_GPU
35#include "GrGpu.h"
36#endif
37
bsalomonf2f1c172016-04-05 12:59:06 -070038using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070039
Matt Sarettf5759932017-02-07 21:52:07 +000040SkImageInfo read_pixels_info(SkImage* image) {
41 if (as_IB(image)->onImageInfo().colorSpace()) {
42 return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType());
43 }
44
45 return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
46}
47
reed871872f2015-06-22 12:48:26 -070048static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
49 SkImage* b) {
50 const int widthA = subsetA ? subsetA->width() : a->width();
51 const int heightA = subsetA ? subsetA->height() : a->height();
52
53 REPORTER_ASSERT(reporter, widthA == b->width());
54 REPORTER_ASSERT(reporter, heightA == b->height());
reed1cb36462016-03-09 15:21:32 -080055
56 // see https://bug.skia.org/3965
57 //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
reed871872f2015-06-22 12:48:26 -070058
reed871872f2015-06-22 12:48:26 -070059 SkAutoPixmapStorage pmapA, pmapB;
Matt Sarettf5759932017-02-07 21:52:07 +000060 pmapA.alloc(read_pixels_info(a));
61 pmapB.alloc(read_pixels_info(b));
reed871872f2015-06-22 12:48:26 -070062
63 const int srcX = subsetA ? subsetA->x() : 0;
64 const int srcY = subsetA ? subsetA->y() : 0;
65
66 REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
67 REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
68
Matt Sarettf5759932017-02-07 21:52:07 +000069 const size_t widthBytes = widthA * 4;
reed871872f2015-06-22 12:48:26 -070070 for (int y = 0; y < heightA; ++y) {
71 REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
72 }
73}
kkinnunen7b94c142015-11-24 07:39:40 -080074static void draw_image_test_pattern(SkCanvas* canvas) {
reed871872f2015-06-22 12:48:26 -070075 canvas->clear(SK_ColorWHITE);
reed871872f2015-06-22 12:48:26 -070076 SkPaint paint;
77 paint.setColor(SK_ColorBLACK);
kkinnunen7b94c142015-11-24 07:39:40 -080078 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
79}
reed9ce9d672016-03-17 10:51:11 -070080static sk_sp<SkImage> create_image() {
kkinnunen7b94c142015-11-24 07:39:40 -080081 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070082 auto surface(SkSurface::MakeRaster(info));
kkinnunen7b94c142015-11-24 07:39:40 -080083 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -070084 return surface->makeImageSnapshot();
reed871872f2015-06-22 12:48:26 -070085}
bungeman38d909e2016-08-02 14:40:46 -070086static sk_sp<SkData> create_image_data(SkImageInfo* info) {
scroggo9d081722016-04-20 08:27:18 -070087 *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
88 const size_t rowBytes = info->minRowBytes();
bungeman38d909e2016-08-02 14:40:46 -070089 sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
scroggo9d081722016-04-20 08:27:18 -070090 {
91 SkBitmap bm;
92 bm.installPixels(*info, data->writable_data(), rowBytes);
93 SkCanvas canvas(bm);
94 draw_image_test_pattern(&canvas);
95 }
bungeman38d909e2016-08-02 14:40:46 -070096 return data;
scroggo9d081722016-04-20 08:27:18 -070097}
98static sk_sp<SkImage> create_data_image() {
99 SkImageInfo info;
100 sk_sp<SkData> data(create_image_data(&info));
bungeman38d909e2016-08-02 14:40:46 -0700101 return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
scroggo9d081722016-04-20 08:27:18 -0700102}
103#if SK_SUPPORT_GPU // not gpu-specific but currently only used in GPU tests
Brian Salomon534cbe52017-01-03 11:35:56 -0500104static sk_sp<SkImage> create_image_large(int maxTextureSize) {
105 const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
bsalomond4907082016-06-13 12:13:03 -0700106 auto surface(SkSurface::MakeRaster(info));
107 surface->getCanvas()->clear(SK_ColorWHITE);
108 SkPaint paint;
109 paint.setColor(SK_ColorBLACK);
110 surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint);
111 return surface->makeImageSnapshot();
112}
reed9ce9d672016-03-17 10:51:11 -0700113static sk_sp<SkImage> create_picture_image() {
bsalomon8e74f802016-01-30 10:01:40 -0800114 SkPictureRecorder recorder;
115 SkCanvas* canvas = recorder.beginRecording(10, 10);
116 canvas->clear(SK_ColorCYAN);
reedca2622b2016-03-18 07:25:55 -0700117 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
Matt Sarette94255d2017-01-09 12:38:59 -0500118 nullptr, nullptr, SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500119 SkColorSpace::MakeSRGB());
bsalomon8e74f802016-01-30 10:01:40 -0800120};
121#endif
kkinnunen7b94c142015-11-24 07:39:40 -0800122// Want to ensure that our Release is called when the owning image is destroyed
123struct RasterDataHolder {
124 RasterDataHolder() : fReleaseCount(0) {}
bungeman38d909e2016-08-02 14:40:46 -0700125 sk_sp<SkData> fData;
kkinnunen7b94c142015-11-24 07:39:40 -0800126 int fReleaseCount;
127 static void Release(const void* pixels, void* context) {
128 RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
129 self->fReleaseCount++;
130 self->fData.reset();
131 }
132};
reed9ce9d672016-03-17 10:51:11 -0700133static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
kkinnunen7b94c142015-11-24 07:39:40 -0800134 SkASSERT(dataHolder);
135 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700136 dataHolder->fData = create_image_data(&info);
137 return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
reed9ce9d672016-03-17 10:51:11 -0700138 RasterDataHolder::Release, dataHolder);
kkinnunen7b94c142015-11-24 07:39:40 -0800139}
reed9ce9d672016-03-17 10:51:11 -0700140static sk_sp<SkImage> create_codec_image() {
kkinnunen7b94c142015-11-24 07:39:40 -0800141 SkImageInfo info;
bungeman38d909e2016-08-02 14:40:46 -0700142 sk_sp<SkData> data(create_image_data(&info));
kkinnunen7b94c142015-11-24 07:39:40 -0800143 SkBitmap bitmap;
144 bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
Hal Canarydb683012016-11-23 08:55:18 -0700145 sk_sp<SkData> src(sk_tool_utils::EncodeImageToData(bitmap, SkEncodedImageFormat::kPNG, 100));
bungeman38d909e2016-08-02 14:40:46 -0700146 return SkImage::MakeFromEncoded(std::move(src));
kkinnunen7b94c142015-11-24 07:39:40 -0800147}
148#if SK_SUPPORT_GPU
reed9ce9d672016-03-17 10:51:11 -0700149static sk_sp<SkImage> create_gpu_image(GrContext* context) {
kkinnunen7b94c142015-11-24 07:39:40 -0800150 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -0700151 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
kkinnunen7b94c142015-11-24 07:39:40 -0800152 draw_image_test_pattern(surface->getCanvas());
reed9ce9d672016-03-17 10:51:11 -0700153 return surface->makeImageSnapshot();
kkinnunen7b94c142015-11-24 07:39:40 -0800154}
155#endif
reed871872f2015-06-22 12:48:26 -0700156
kkinnunen7b94c142015-11-24 07:39:40 -0800157static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
reed871872f2015-06-22 12:48:26 -0700158 const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
reed9ce9d672016-03-17 10:51:11 -0700159 sk_sp<SkData> origEncoded(image->encode());
reed871872f2015-06-22 12:48:26 -0700160 REPORTER_ASSERT(reporter, origEncoded);
161 REPORTER_ASSERT(reporter, origEncoded->size() > 0);
162
reed9ce9d672016-03-17 10:51:11 -0700163 sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
scroggo8e6c7ad2016-09-16 08:20:38 -0700164 if (!decoded) {
165 ERRORF(reporter, "failed to decode image!");
166 return;
167 }
reed871872f2015-06-22 12:48:26 -0700168 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700169 assert_equal(reporter, image, nullptr, decoded.get());
reed871872f2015-06-22 12:48:26 -0700170
171 // Now see if we can instantiate an image from a subset of the surface/origEncoded
mtklein5f939ab2016-03-16 10:28:35 -0700172
reed9ce9d672016-03-17 10:51:11 -0700173 decoded = SkImage::MakeFromEncoded(origEncoded, &ir);
reed871872f2015-06-22 12:48:26 -0700174 REPORTER_ASSERT(reporter, decoded);
reed9ce9d672016-03-17 10:51:11 -0700175 assert_equal(reporter, image, &ir, decoded.get());
reed871872f2015-06-22 12:48:26 -0700176}
177
kkinnunen7b94c142015-11-24 07:39:40 -0800178DEF_TEST(ImageEncode, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700179 test_encode(reporter, create_image().get());
reed871872f2015-06-22 12:48:26 -0700180}
181
182#if SK_SUPPORT_GPU
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}
186#endif
reed759373a2015-07-03 21:01:10 -0700187
reed2dad7692016-08-01 11:12:58 -0700188DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
189 const struct {
reed1ec04d92016-08-05 12:07:41 -0700190 SkCopyPixelsMode fCPM;
191 bool fExpectSameAsMutable;
192 bool fExpectSameAsImmutable;
reed2dad7692016-08-01 11:12:58 -0700193 } recs[] = {
reed1ec04d92016-08-05 12:07:41 -0700194 { kIfMutable_SkCopyPixelsMode, false, true },
195 { kAlways_SkCopyPixelsMode, false, false },
196 { kNever_SkCopyPixelsMode, true, true },
reed2dad7692016-08-01 11:12:58 -0700197 };
198 for (auto rec : recs) {
199 SkPixmap pm;
200 SkBitmap bm;
201 bm.allocN32Pixels(100, 100);
202
reed1ec04d92016-08-05 12:07:41 -0700203 auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700204 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
205 const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
206 REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable);
reedae296442016-08-05 13:19:01 -0700207 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
reed2dad7692016-08-01 11:12:58 -0700208
209 bm.notifyPixelsChanged(); // force a new generation ID
210
211 bm.setImmutable();
reed1ec04d92016-08-05 12:07:41 -0700212 img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
reed2dad7692016-08-01 11:12:58 -0700213 REPORTER_ASSERT(reporter, img->peekPixels(&pm));
214 const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
215 REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
reedae296442016-08-05 13:19:01 -0700216 REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
reed2dad7692016-08-01 11:12:58 -0700217 }
218}
219
fmalita2be71252015-09-03 07:17:25 -0700220namespace {
221
222const char* kSerializedData = "serialized";
223
224class MockSerializer : public SkPixelSerializer {
fmalitac3470342015-09-04 11:36:39 -0700225public:
bungeman38d909e2016-08-02 14:40:46 -0700226 MockSerializer(sk_sp<SkData> (*func)()) : fFunc(func), fDidEncode(false) { }
fmalitac3470342015-09-04 11:36:39 -0700227
228 bool didEncode() const { return fDidEncode; }
229
fmalita2be71252015-09-03 07:17:25 -0700230protected:
reedc9e190d2015-09-28 09:58:41 -0700231 bool onUseEncodedData(const void*, size_t) override {
232 return false;
fmalita2be71252015-09-03 07:17:25 -0700233 }
234
halcanary99073712015-12-10 09:30:57 -0800235 SkData* onEncode(const SkPixmap&) override {
fmalitac3470342015-09-04 11:36:39 -0700236 fDidEncode = true;
bungeman38d909e2016-08-02 14:40:46 -0700237 return fFunc().release();
fmalita2be71252015-09-03 07:17:25 -0700238 }
fmalitac3470342015-09-04 11:36:39 -0700239
240private:
bungeman38d909e2016-08-02 14:40:46 -0700241 sk_sp<SkData> (*fFunc)();
fmalitac3470342015-09-04 11:36:39 -0700242 bool fDidEncode;
243
244 typedef SkPixelSerializer INHERITED;
fmalita2be71252015-09-03 07:17:25 -0700245};
246
247} // anonymous namespace
248
249// Test that SkImage encoding observes custom pixel serializers.
250DEF_TEST(Image_Encode_Serializer, reporter) {
bungeman38d909e2016-08-02 14:40:46 -0700251 MockSerializer serializer([]() -> sk_sp<SkData> {
252 return SkData::MakeWithCString(kSerializedData);
253 });
reed9ce9d672016-03-17 10:51:11 -0700254 sk_sp<SkImage> image(create_image());
bungeman38d909e2016-08-02 14:40:46 -0700255 sk_sp<SkData> encoded(image->encode(&serializer));
256 sk_sp<SkData> reference(SkData::MakeWithCString(kSerializedData));
fmalita2be71252015-09-03 07:17:25 -0700257
fmalitac3470342015-09-04 11:36:39 -0700258 REPORTER_ASSERT(reporter, serializer.didEncode());
fmalita2be71252015-09-03 07:17:25 -0700259 REPORTER_ASSERT(reporter, encoded);
260 REPORTER_ASSERT(reporter, encoded->size() > 0);
bungeman38d909e2016-08-02 14:40:46 -0700261 REPORTER_ASSERT(reporter, encoded->equals(reference.get()));
fmalita2be71252015-09-03 07:17:25 -0700262}
263
fmalitac3470342015-09-04 11:36:39 -0700264// Test that image encoding failures do not break picture serialization/deserialization.
265DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
reede8f30622016-03-23 18:59:25 -0700266 auto surface(SkSurface::MakeRasterN32Premul(100, 100));
fmalitac3470342015-09-04 11:36:39 -0700267 surface->getCanvas()->clear(SK_ColorGREEN);
reed9ce9d672016-03-17 10:51:11 -0700268 sk_sp<SkImage> image(surface->makeImageSnapshot());
fmalitac3470342015-09-04 11:36:39 -0700269 REPORTER_ASSERT(reporter, image);
270
271 SkPictureRecorder recorder;
272 SkCanvas* canvas = recorder.beginRecording(100, 100);
273 canvas->drawImage(image, 0, 0);
reedca2622b2016-03-18 07:25:55 -0700274 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
fmalitac3470342015-09-04 11:36:39 -0700275 REPORTER_ASSERT(reporter, picture);
276 REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
277
bungeman38d909e2016-08-02 14:40:46 -0700278 MockSerializer emptySerializer([]() -> sk_sp<SkData> { return SkData::MakeEmpty(); });
279 MockSerializer nullSerializer([]() -> sk_sp<SkData> { return nullptr; });
fmalitac3470342015-09-04 11:36:39 -0700280 MockSerializer* serializers[] = { &emptySerializer, &nullSerializer };
281
282 for (size_t i = 0; i < SK_ARRAY_COUNT(serializers); ++i) {
283 SkDynamicMemoryWStream wstream;
284 REPORTER_ASSERT(reporter, !serializers[i]->didEncode());
285 picture->serialize(&wstream, serializers[i]);
286 REPORTER_ASSERT(reporter, serializers[i]->didEncode());
287
Ben Wagner145dbcd2016-11-03 14:40:50 -0400288 std::unique_ptr<SkStream> rstream(wstream.detachAsStream());
289 sk_sp<SkPicture> deserialized(SkPicture::MakeFromStream(rstream.get()));
fmalitac3470342015-09-04 11:36:39 -0700290 REPORTER_ASSERT(reporter, deserialized);
291 REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
292 }
293}
294
reed759373a2015-07-03 21:01:10 -0700295DEF_TEST(Image_NewRasterCopy, reporter) {
296 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
297 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
298 const SkPMColor blue = SkPackARGB32(0xFF, 0, 0, 0xFF);
299 SkPMColor colors[] = { red, green, blue, 0 };
Hal Canary342b7ac2016-11-04 11:49:42 -0400300 sk_sp<SkColorTable> ctable(new SkColorTable(colors, SK_ARRAY_COUNT(colors)));
reed759373a2015-07-03 21:01:10 -0700301 // The colortable made a copy, so we can trash the original colors
302 memset(colors, 0xFF, sizeof(colors));
303
304 const SkImageInfo srcInfo = SkImageInfo::Make(2, 2, kIndex_8_SkColorType, kPremul_SkAlphaType);
305 const size_t srcRowBytes = 2 * sizeof(uint8_t);
306 uint8_t indices[] = { 0, 1, 2, 3 };
Hal Canary342b7ac2016-11-04 11:49:42 -0400307 auto image = SkImage::MakeRasterCopy(SkPixmap(srcInfo, indices, srcRowBytes, ctable.get()));
reed759373a2015-07-03 21:01:10 -0700308 // The image made a copy, so we can trash the original indices
309 memset(indices, 0xFF, sizeof(indices));
310
311 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
312 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
313 SkPMColor pixels[4];
314 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
315 image->readPixels(dstInfo, pixels, dstRowBytes, 0, 0);
316 REPORTER_ASSERT(reporter, red == pixels[0]);
317 REPORTER_ASSERT(reporter, green == pixels[1]);
318 REPORTER_ASSERT(reporter, blue == pixels[2]);
319 REPORTER_ASSERT(reporter, 0 == pixels[3]);
320}
fmalita8c0144c2015-07-22 05:56:16 -0700321
322// Test that a draw that only partially covers the drawing surface isn't
323// interpreted as covering the entire drawing surface (i.e., exercise one of the
324// conditions of SkCanvas::wouldOverwriteEntireSurface()).
325DEF_TEST(Image_RetainSnapshot, reporter) {
326 const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
327 const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
328 SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
reede8f30622016-03-23 18:59:25 -0700329 auto surface(SkSurface::MakeRaster(info));
fmalita8c0144c2015-07-22 05:56:16 -0700330 surface->getCanvas()->clear(0xFF00FF00);
331
332 SkPMColor pixels[4];
333 memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
334 const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
335 const size_t dstRowBytes = 2 * sizeof(SkPMColor);
336
reed9ce9d672016-03-17 10:51:11 -0700337 sk_sp<SkImage> image1(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700338 REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
339 for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
340 REPORTER_ASSERT(reporter, pixels[i] == green);
341 }
342
343 SkPaint paint;
reed374772b2016-10-05 17:33:02 -0700344 paint.setBlendMode(SkBlendMode::kSrc);
fmalita8c0144c2015-07-22 05:56:16 -0700345 paint.setColor(SK_ColorRED);
346
347 surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
348
reed9ce9d672016-03-17 10:51:11 -0700349 sk_sp<SkImage> image2(surface->makeImageSnapshot());
fmalita8c0144c2015-07-22 05:56:16 -0700350 REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
351 REPORTER_ASSERT(reporter, pixels[0] == green);
352 REPORTER_ASSERT(reporter, pixels[1] == green);
353 REPORTER_ASSERT(reporter, pixels[2] == green);
354 REPORTER_ASSERT(reporter, pixels[3] == red);
355}
reed80c772b2015-07-30 18:58:23 -0700356
357/////////////////////////////////////////////////////////////////////////////////////////////////
reed80c772b2015-07-30 18:58:23 -0700358
359static void make_bitmap_mutable(SkBitmap* bm) {
360 bm->allocN32Pixels(10, 10);
361}
362
363static void make_bitmap_immutable(SkBitmap* bm) {
364 bm->allocN32Pixels(10, 10);
365 bm->setImmutable();
366}
367
368DEF_TEST(image_newfrombitmap, reporter) {
369 const struct {
370 void (*fMakeProc)(SkBitmap*);
371 bool fExpectPeekSuccess;
372 bool fExpectSharedID;
fmalitaddbbdda2015-08-20 08:47:26 -0700373 bool fExpectLazy;
reed80c772b2015-07-30 18:58:23 -0700374 } rec[] = {
fmalitaddbbdda2015-08-20 08:47:26 -0700375 { make_bitmap_mutable, true, false, false },
376 { make_bitmap_immutable, true, true, false },
reed80c772b2015-07-30 18:58:23 -0700377 };
378
379 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
380 SkBitmap bm;
381 rec[i].fMakeProc(&bm);
382
reed9ce9d672016-03-17 10:51:11 -0700383 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
reed80c772b2015-07-30 18:58:23 -0700384 SkPixmap pmap;
385
386 const bool sharedID = (image->uniqueID() == bm.getGenerationID());
387 REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
388
reed80c772b2015-07-30 18:58:23 -0700389 const bool peekSuccess = image->peekPixels(&pmap);
390 REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
fmalitaddbbdda2015-08-20 08:47:26 -0700391
392 const bool lazy = image->isLazyGenerated();
393 REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
reed80c772b2015-07-30 18:58:23 -0700394 }
395}
reed6f1216a2015-08-04 08:10:13 -0700396
397///////////////////////////////////////////////////////////////////////////////////////////////////
398#if SK_SUPPORT_GPU
399
reed6f1216a2015-08-04 08:10:13 -0700400#include "SkBitmapCache.h"
401
402/*
403 * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image.
404 * We cache it for performance when drawing into a raster surface.
405 *
406 * A cleaner test would know if each drawImage call triggered a read-back from the gpu,
407 * but we don't have that facility (at the moment) so we use a little internal knowledge
408 * of *how* the raster version is cached, and look for that.
409 */
bsalomon68d91342016-04-12 09:59:58 -0700410DEF_GPUTEST_FOR_RENDERING_CONTEXTS(c, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800411 SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
bsalomon8b7451a2016-05-11 06:33:06 -0700412 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
reed6f1216a2015-08-04 08:10:13 -0700413 const uint32_t uniqueID = image->uniqueID();
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400414 const auto desc = SkBitmapCacheDesc::Make(image.get());
reed6f1216a2015-08-04 08:10:13 -0700415
reede8f30622016-03-23 18:59:25 -0700416 auto surface(SkSurface::MakeRaster(info));
reed6f1216a2015-08-04 08:10:13 -0700417
418 // now we can test drawing a gpu-backed image into a cpu-backed surface
419
420 {
421 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400422 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700423 }
424
425 surface->getCanvas()->drawImage(image, 0, 0);
426 {
427 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400428 if (SkBitmapCache::Find(desc, &cachedBitmap)) {
reed6f1216a2015-08-04 08:10:13 -0700429 REPORTER_ASSERT(reporter, cachedBitmap.getGenerationID() == uniqueID);
430 REPORTER_ASSERT(reporter, cachedBitmap.isImmutable());
431 REPORTER_ASSERT(reporter, cachedBitmap.getPixels());
432 } else {
433 // unexpected, but not really a bug, since the cache is global and this test may be
434 // run w/ other threads competing for its budget.
435 SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n");
436 }
437 }
438
439 image.reset(nullptr);
440 {
441 SkBitmap cachedBitmap;
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400442 REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
reed6f1216a2015-08-04 08:10:13 -0700443 }
444}
bsalomon8e74f802016-01-30 10:01:40 -0800445
Brian Salomon6405e712017-03-20 08:54:16 -0400446GrContextFactory::ContextType pick_second_context_type(const sk_gpu_test::ContextInfo& info) {
447 switch (info.backend()) {
448 case kOpenGL_GrBackend:
Brian Salomon107524c2017-03-20 17:08:43 -0400449#if defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_UNIX) || defined (SK_BUILD_FOR_MAC)
450 return GrContextFactory::kGL_ContextType;
451#else
452 return GrContextFactory::kGLES_ContextType;
453#endif
Brian Salomon6405e712017-03-20 08:54:16 -0400454 case kVulkan_GrBackend:
455 return GrContextFactory::kVulkan_ContextType;
456 }
457 SkFAIL("Unknown backend type.");
458 return GrContextFactory::kGL_ContextType;
459}
460
Brian Osman041f7df2017-02-07 11:23:28 -0500461DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
462 GrContext* context = contextInfo.grContext();
463 sk_gpu_test::TestContext* testContext = contextInfo.testContext();
Brian Osman041f7df2017-02-07 11:23:28 -0500464 GrContextFactory otherFactory;
Brian Salomon6405e712017-03-20 08:54:16 -0400465 GrContextFactory::ContextType otherContextType = pick_second_context_type(contextInfo);
Brian Osman041f7df2017-02-07 11:23:28 -0500466 ContextInfo otherContextInfo = otherFactory.getContextInfo(otherContextType);
467 testContext->makeCurrent();
468
469 std::function<sk_sp<SkImage>()> imageFactories[] = {
470 create_image,
471 create_codec_image,
472 create_data_image,
473 // Create an image from a picture.
474 create_picture_image,
475 // Create a texture image.
476 [context] { return create_gpu_image(context); },
477 // Create a texture image in a another GrContext.
478 [testContext, otherContextInfo] {
479 otherContextInfo.testContext()->makeCurrent();
480 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
481 testContext->makeCurrent();
482 return otherContextImage;
483 }
484 };
485
Brian Osmane8827d22017-02-07 12:31:02 -0500486 sk_sp<SkColorSpace> dstColorSpaces[] ={
487 nullptr,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500488 SkColorSpace::MakeSRGB(),
Brian Osmane8827d22017-02-07 12:31:02 -0500489 };
Brian Osman041f7df2017-02-07 11:23:28 -0500490
Brian Osmane8827d22017-02-07 12:31:02 -0500491 for (auto& dstColorSpace : dstColorSpaces) {
492 for (auto factory : imageFactories) {
493 sk_sp<SkImage> image(factory());
494 if (!image) {
495 ERRORF(reporter, "Error creating image.");
496 continue;
Brian Osman041f7df2017-02-07 11:23:28 -0500497 }
Brian Osmane8827d22017-02-07 12:31:02 -0500498 GrTexture* origTexture = as_IB(image)->peekTexture();
499
500 sk_sp<SkImage> texImage(image->makeTextureImage(context, dstColorSpace.get()));
501 if (!texImage) {
Robert Phillips256c37b2017-03-01 14:32:46 -0500502 // We expect to fail if image comes from a different GrContext.
Brian Osmane8827d22017-02-07 12:31:02 -0500503 if (!origTexture || origTexture->getContext() == context) {
504 ERRORF(reporter, "makeTextureImage failed.");
505 }
506 continue;
Brian Osman041f7df2017-02-07 11:23:28 -0500507 }
Brian Osmane8827d22017-02-07 12:31:02 -0500508 GrTexture* copyTexture = as_IB(texImage)->peekTexture();
509 if (!copyTexture) {
510 ERRORF(reporter, "makeTextureImage returned non-texture image.");
511 continue;
512 }
513 if (origTexture) {
514 if (origTexture != copyTexture) {
515 ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
516 }
517 }
518 if (image->width() != texImage->width() || image->height() != texImage->height()) {
519 ERRORF(reporter, "makeTextureImage changed the image size.");
520 }
521 if (image->alphaType() != texImage->alphaType()) {
522 ERRORF(reporter, "makeTextureImage changed image alpha type.");
523 }
Brian Osman041f7df2017-02-07 11:23:28 -0500524 }
525 }
526}
527
bsalomon634b4302016-07-12 18:11:17 -0700528DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
529 GrContext* context = contextInfo.grContext();
530
531 std::function<sk_sp<SkImage>()> imageFactories[] = {
532 create_image,
533 create_codec_image,
534 create_data_image,
535 create_picture_image,
536 [context] { return create_gpu_image(context); },
537 };
Brian Osman041f7df2017-02-07 11:23:28 -0500538 SkColorSpace* legacyColorSpace = nullptr;
bsalomon634b4302016-07-12 18:11:17 -0700539 for (auto factory : imageFactories) {
540 sk_sp<SkImage> image = factory();
541 if (!image->isTextureBacked()) {
542 REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
Brian Osman041f7df2017-02-07 11:23:28 -0500543 if (!(image = image->makeTextureImage(context, legacyColorSpace))) {
544 continue;
545 }
bsalomon634b4302016-07-12 18:11:17 -0700546 }
547 auto rasterImage = image->makeNonTextureImage();
548 if (!rasterImage) {
549 ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
550 }
551 REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
552 assert_equal(reporter, image.get(), nullptr, rasterImage.get());
553 }
554}
555
bsalomon363bc332016-06-15 14:15:30 -0700556DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_drawAbandonedGpuImage, reporter, contextInfo) {
bsalomone5b651f2016-06-15 07:36:15 -0700557 auto context = contextInfo.grContext();
558 auto image = create_gpu_image(context);
559 auto info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
560 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
561 as_IB(image)->peekTexture()->abandon();
562 surface->getCanvas()->drawImage(image, 0, 0);
563}
564
reed6f1216a2015-08-04 08:10:13 -0700565#endif
halcanaryc56c6ef2015-09-28 11:55:28 -0700566
halcanary6950de62015-11-07 05:29:00 -0800567// https://bug.skia.org/4390
halcanaryc56c6ef2015-09-28 11:55:28 -0700568DEF_TEST(ImageFromIndex8Bitmap, r) {
569 SkPMColor pmColors[1] = {SkPreMultiplyColor(SK_ColorWHITE)};
570 SkBitmap bm;
Hal Canary342b7ac2016-11-04 11:49:42 -0400571 SkImageInfo info = SkImageInfo::Make(1, 1, kIndex_8_SkColorType, kPremul_SkAlphaType);
Mike Reed6b3155c2017-04-03 14:41:44 -0400572 bm.allocPixels(info, SkColorTable::Make(pmColors, SK_ARRAY_COUNT(pmColors)));
halcanaryc56c6ef2015-09-28 11:55:28 -0700573 *bm.getAddr8(0, 0) = 0;
reed9ce9d672016-03-17 10:51:11 -0700574 sk_sp<SkImage> img(SkImage::MakeFromBitmap(bm));
575 REPORTER_ASSERT(r, img != nullptr);
halcanaryc56c6ef2015-09-28 11:55:28 -0700576}
kkinnunen4e184132015-11-17 22:53:28 -0800577
kkinnunen4e184132015-11-17 22:53:28 -0800578class EmptyGenerator : public SkImageGenerator {
579public:
580 EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
581};
582
kkinnunen7b94c142015-11-24 07:39:40 -0800583DEF_TEST(ImageEmpty, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800584 const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
reed9ce9d672016-03-17 10:51:11 -0700585 SkPixmap pmap(info, nullptr, 0);
586 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
587 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
588 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
Mike Reed185130c2017-02-15 15:14:16 -0500589 REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
590 skstd::make_unique<EmptyGenerator>()));
kkinnunen4e184132015-11-17 22:53:28 -0800591}
592
kkinnunen7b94c142015-11-24 07:39:40 -0800593DEF_TEST(ImageDataRef, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800594 SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
595 size_t rowBytes = info.minRowBytes();
596 size_t size = info.getSafeSize(rowBytes);
reed9ce9d672016-03-17 10:51:11 -0700597 sk_sp<SkData> data = SkData::MakeUninitialized(size);
kkinnunen4e184132015-11-17 22:53:28 -0800598 REPORTER_ASSERT(reporter, data->unique());
reed9ce9d672016-03-17 10:51:11 -0700599 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
kkinnunen4e184132015-11-17 22:53:28 -0800600 REPORTER_ASSERT(reporter, !data->unique());
reed9ce9d672016-03-17 10:51:11 -0700601 image.reset();
kkinnunen4e184132015-11-17 22:53:28 -0800602 REPORTER_ASSERT(reporter, data->unique());
kkinnunen4e184132015-11-17 22:53:28 -0800603}
604
kkinnunen4e184132015-11-17 22:53:28 -0800605static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
606 for (int i = 0; i < count; ++i) {
607 if (pixels[i] != expected) {
608 return false;
609 }
610 }
611 return true;
612}
613
Robert Phillips3500b772017-01-27 10:11:42 -0500614static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700615 if (!image) {
616 ERRORF(reporter, "Failed to create image!");
617 return;
618 }
kkinnunen7b94c142015-11-24 07:39:40 -0800619 const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
kkinnunen4e184132015-11-17 22:53:28 -0800620 const SkPMColor notExpected = ~expected;
621
622 const int w = 2, h = 2;
623 const size_t rowBytes = w * sizeof(SkPMColor);
624 SkPMColor pixels[w*h];
625
626 SkImageInfo info;
627
628 info = SkImageInfo::MakeUnknown(w, h);
629 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
630
631 // out-of-bounds should fail
632 info = SkImageInfo::MakeN32Premul(w, h);
633 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
634 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
635 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
636 REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
637
638 // top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800639 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800640 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
641 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
642
643 // bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800644 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800645 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
646 image->width() - w, image->height() - h));
647 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
648
649 // partial top-left should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800650 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800651 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
652 REPORTER_ASSERT(reporter, pixels[3] == expected);
653 REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
654
655 // partial bottom-right should succeed
kkinnunen7b94c142015-11-24 07:39:40 -0800656 sk_memset32(pixels, notExpected, w*h);
kkinnunen4e184132015-11-17 22:53:28 -0800657 REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
658 image->width() - 1, image->height() - 1));
659 REPORTER_ASSERT(reporter, pixels[0] == expected);
660 REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
661}
kkinnunen7b94c142015-11-24 07:39:40 -0800662DEF_TEST(ImageReadPixels, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700663 sk_sp<SkImage> image(create_image());
Robert Phillips3500b772017-01-27 10:11:42 -0500664 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800665
reed9ce9d672016-03-17 10:51:11 -0700666 image = create_data_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500667 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800668
669 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700670 image = create_rasterproc_image(&dataHolder);
Robert Phillips3500b772017-01-27 10:11:42 -0500671 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800672 image.reset();
673 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
674
reed9ce9d672016-03-17 10:51:11 -0700675 image = create_codec_image();
Robert Phillips3500b772017-01-27 10:11:42 -0500676 image_test_read_pixels(reporter, image.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800677}
678#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700679DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
Robert Phillips3500b772017-01-27 10:11:42 -0500680 image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get());
kkinnunen7b94c142015-11-24 07:39:40 -0800681}
682#endif
kkinnunen4e184132015-11-17 22:53:28 -0800683
684static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
685 const SkBitmap& bitmap, SkImage::LegacyBitmapMode mode) {
686 REPORTER_ASSERT(reporter, image->width() == bitmap.width());
687 REPORTER_ASSERT(reporter, image->height() == bitmap.height());
brianosman69c166d2016-08-17 14:01:05 -0700688 REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
kkinnunen4e184132015-11-17 22:53:28 -0800689
690 if (SkImage::kRO_LegacyBitmapMode == mode) {
691 REPORTER_ASSERT(reporter, bitmap.isImmutable());
692 }
693
694 SkAutoLockPixels alp(bitmap);
695 REPORTER_ASSERT(reporter, bitmap.getPixels());
696
697 const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
698 SkPMColor imageColor;
699 REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
700 REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
701}
702
kkinnunen7b94c142015-11-24 07:39:40 -0800703static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image, SkImage::LegacyBitmapMode mode) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700704 if (!image) {
705 ERRORF(reporter, "Failed to create image.");
706 return;
707 }
kkinnunen7b94c142015-11-24 07:39:40 -0800708 SkBitmap bitmap;
709 REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap, mode));
710 check_legacy_bitmap(reporter, image, bitmap, mode);
711
712 // Test subsetting to exercise the rowBytes logic.
713 SkBitmap tmp;
714 REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
715 image->height() / 2)));
reed9ce9d672016-03-17 10:51:11 -0700716 sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
717 REPORTER_ASSERT(reporter, subsetImage.get());
kkinnunen7b94c142015-11-24 07:39:40 -0800718
719 SkBitmap subsetBitmap;
720 REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap, mode));
reed9ce9d672016-03-17 10:51:11 -0700721 check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap, mode);
kkinnunen7b94c142015-11-24 07:39:40 -0800722}
723DEF_TEST(ImageLegacyBitmap, reporter) {
kkinnunen4e184132015-11-17 22:53:28 -0800724 const SkImage::LegacyBitmapMode modes[] = {
725 SkImage::kRO_LegacyBitmapMode,
726 SkImage::kRW_LegacyBitmapMode,
727 };
kkinnunen7b94c142015-11-24 07:39:40 -0800728 for (auto& mode : modes) {
reed9ce9d672016-03-17 10:51:11 -0700729 sk_sp<SkImage> image(create_image());
730 test_legacy_bitmap(reporter, image.get(), mode);
kkinnunen4e184132015-11-17 22:53:28 -0800731
reed9ce9d672016-03-17 10:51:11 -0700732 image = create_data_image();
733 test_legacy_bitmap(reporter, image.get(), mode);
kkinnunen4e184132015-11-17 22:53:28 -0800734
kkinnunen7b94c142015-11-24 07:39:40 -0800735 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700736 image = create_rasterproc_image(&dataHolder);
737 test_legacy_bitmap(reporter, image.get(), mode);
kkinnunen7b94c142015-11-24 07:39:40 -0800738 image.reset();
739 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
740
reed9ce9d672016-03-17 10:51:11 -0700741 image = create_codec_image();
742 test_legacy_bitmap(reporter, image.get(), mode);
kkinnunen4e184132015-11-17 22:53:28 -0800743 }
744}
kkinnunen4e184132015-11-17 22:53:28 -0800745#if SK_SUPPORT_GPU
bsalomon68d91342016-04-12 09:59:58 -0700746DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
kkinnunen7b94c142015-11-24 07:39:40 -0800747 const SkImage::LegacyBitmapMode modes[] = {
748 SkImage::kRO_LegacyBitmapMode,
749 SkImage::kRW_LegacyBitmapMode,
750 };
751 for (auto& mode : modes) {
bsalomon8b7451a2016-05-11 06:33:06 -0700752 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
reed9ce9d672016-03-17 10:51:11 -0700753 test_legacy_bitmap(reporter, image.get(), mode);
kkinnunen4e184132015-11-17 22:53:28 -0800754 }
kkinnunen7b94c142015-11-24 07:39:40 -0800755}
kkinnunen4e184132015-11-17 22:53:28 -0800756#endif
757
kkinnunen7b94c142015-11-24 07:39:40 -0800758static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700759 if (!image) {
760 ERRORF(reporter, "Failed to create image!");
761 return;
762 }
reed6ceeebd2016-03-09 14:26:26 -0800763 SkPixmap pm;
764 bool success = image->peekPixels(&pm);
kkinnunen7b94c142015-11-24 07:39:40 -0800765 REPORTER_ASSERT(reporter, expectPeekSuccess == success);
766 if (success) {
reed6ceeebd2016-03-09 14:26:26 -0800767 const SkImageInfo& info = pm.info();
kkinnunen7b94c142015-11-24 07:39:40 -0800768 REPORTER_ASSERT(reporter, 20 == info.width());
769 REPORTER_ASSERT(reporter, 20 == info.height());
770 REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
771 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
772 kOpaque_SkAlphaType == info.alphaType());
reed6ceeebd2016-03-09 14:26:26 -0800773 REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
774 REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
kkinnunen4e184132015-11-17 22:53:28 -0800775 }
kkinnunen7b94c142015-11-24 07:39:40 -0800776}
777DEF_TEST(ImagePeek, reporter) {
reed9ce9d672016-03-17 10:51:11 -0700778 sk_sp<SkImage> image(create_image());
779 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800780
reed9ce9d672016-03-17 10:51:11 -0700781 image = create_data_image();
782 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800783
784 RasterDataHolder dataHolder;
reed9ce9d672016-03-17 10:51:11 -0700785 image = create_rasterproc_image(&dataHolder);
786 test_peek(reporter, image.get(), true);
kkinnunen7b94c142015-11-24 07:39:40 -0800787 image.reset();
788 REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
789
reed9ce9d672016-03-17 10:51:11 -0700790 image = create_codec_image();
791 test_peek(reporter, image.get(), false);
kkinnunen4e184132015-11-17 22:53:28 -0800792}
793#if SK_SUPPORT_GPU
egdanielab527a52016-06-28 08:07:26 -0700794DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700795 sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
reed9ce9d672016-03-17 10:51:11 -0700796 test_peek(reporter, image.get(), false);
kkinnunen7b94c142015-11-24 07:39:40 -0800797}
798#endif
kkinnunen4e184132015-11-17 22:53:28 -0800799
kkinnunen7b94c142015-11-24 07:39:40 -0800800#if SK_SUPPORT_GPU
801struct TextureReleaseChecker {
802 TextureReleaseChecker() : fReleaseCount(0) {}
803 int fReleaseCount;
804 static void Release(void* self) {
805 static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
kkinnunen4e184132015-11-17 22:53:28 -0800806 }
807};
brianosmandb2cb102016-07-22 07:22:04 -0700808DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
809 const int kWidth = 10;
810 const int kHeight = 10;
Ben Wagner7ecc5962016-11-02 17:07:33 -0400811 std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]);
kkinnunen7b94c142015-11-24 07:39:40 -0800812 GrBackendTextureDesc backendDesc;
kkinnunen7b94c142015-11-24 07:39:40 -0800813 backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag;
Robert Phillips3390e152017-01-31 17:53:34 -0500814 backendDesc.fOrigin = kBottomLeft_GrSurfaceOrigin;
815 backendDesc.fConfig = kRGBA_8888_GrPixelConfig;
brianosmandb2cb102016-07-22 07:22:04 -0700816 backendDesc.fWidth = kWidth;
817 backendDesc.fHeight = kHeight;
kkinnunen7b94c142015-11-24 07:39:40 -0800818 backendDesc.fSampleCnt = 0;
brianosmandb2cb102016-07-22 07:22:04 -0700819 backendDesc.fTextureHandle = ctxInfo.grContext()->getGpu()->createTestingOnlyBackendTexture(
820 pixels.get(), kWidth, kHeight, kRGBA_8888_GrPixelConfig, true);
821
kkinnunen7b94c142015-11-24 07:39:40 -0800822 TextureReleaseChecker releaseChecker;
reed9ce9d672016-03-17 10:51:11 -0700823 sk_sp<SkImage> refImg(
bsalomon8b7451a2016-05-11 06:33:06 -0700824 SkImage::MakeFromTexture(ctxInfo.grContext(), backendDesc, kPremul_SkAlphaType,
reed9ce9d672016-03-17 10:51:11 -0700825 TextureReleaseChecker::Release, &releaseChecker));
kkinnunen4e184132015-11-17 22:53:28 -0800826
Robert Phillips3390e152017-01-31 17:53:34 -0500827 GrSurfaceOrigin readBackOrigin;
828 GrBackendObject readBackHandle = refImg->getTextureHandle(false, &readBackOrigin);
829 // TODO: Make it so we can check this (see skbug.com/5019)
830#if 0
831 if (*readBackHandle != *(backendDesc.fTextureHandle)) {
832 ERRORF(reporter, "backend mismatch %d %d\n",
833 (int)readBackHandle, (int)backendDesc.fTextureHandle);
834 }
835 REPORTER_ASSERT(reporter, readBackHandle == backendDesc.fTextureHandle);
836#else
837 REPORTER_ASSERT(reporter, SkToBool(readBackHandle));
838#endif
839 if (readBackOrigin != backendDesc.fOrigin) {
840 ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, backendDesc.fOrigin);
841 }
842 REPORTER_ASSERT(reporter, readBackOrigin == backendDesc.fOrigin);
843
kkinnunen4e184132015-11-17 22:53:28 -0800844 // Now exercise the release proc
kkinnunen7b94c142015-11-24 07:39:40 -0800845 REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
kkinnunen4e184132015-11-17 22:53:28 -0800846 refImg.reset(nullptr); // force a release of the image
kkinnunen7b94c142015-11-24 07:39:40 -0800847 REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
brianosmandb2cb102016-07-22 07:22:04 -0700848
849 ctxInfo.grContext()->getGpu()->deleteTestingOnlyBackendTexture(backendDesc.fTextureHandle);
kkinnunen4e184132015-11-17 22:53:28 -0800850}
bsalomon0d996862016-03-09 18:44:43 -0800851
852static void check_images_same(skiatest::Reporter* reporter, const SkImage* a, const SkImage* b) {
853 if (a->width() != b->width() || a->height() != b->height()) {
854 ERRORF(reporter, "Images must have the same size");
855 return;
856 }
brianosman69c166d2016-08-17 14:01:05 -0700857 if (a->alphaType() != b->alphaType()) {
858 ERRORF(reporter, "Images must have the same alpha type");
bsalomon0d996862016-03-09 18:44:43 -0800859 return;
860 }
861
862 SkImageInfo info = SkImageInfo::MakeN32Premul(a->width(), a->height());
863 SkAutoPixmapStorage apm;
864 SkAutoPixmapStorage bpm;
865
866 apm.alloc(info);
867 bpm.alloc(info);
868
869 if (!a->readPixels(apm, 0, 0)) {
870 ERRORF(reporter, "Could not read image a's pixels");
871 return;
872 }
873 if (!b->readPixels(bpm, 0, 0)) {
874 ERRORF(reporter, "Could not read image b's pixels");
875 return;
876 }
877
878 for (auto y = 0; y < info.height(); ++y) {
879 for (auto x = 0; x < info.width(); ++x) {
880 uint32_t pixelA = *apm.addr32(x, y);
881 uint32_t pixelB = *bpm.addr32(x, y);
882 if (pixelA != pixelB) {
883 ERRORF(reporter, "Expected image pixels to be the same. At %d,%d 0x%08x != 0x%08x",
884 x, y, pixelA, pixelB);
885 return;
886 }
887 }
888 }
889}
890
Mike Kleinc168a3a2016-11-14 14:53:13 +0000891DEF_GPUTEST_FOR_RENDERING_CONTEXTS(DeferredTextureImage, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700892 GrContext* context = ctxInfo.grContext();
bsalomon0fd3c812016-05-11 10:38:05 -0700893 sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
bungeman6bd52842016-10-27 09:30:08 -0700894 sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
bsalomon41b952c2016-03-11 06:46:33 -0800895
896 GrContextFactory otherFactory;
bsalomonf2f1c172016-04-05 12:59:06 -0700897 ContextInfo otherContextInfo =
Brian Salomon6405e712017-03-20 08:54:16 -0400898 otherFactory.getContextInfo(pick_second_context_type(ctxInfo));
bsalomon41b952c2016-03-11 06:46:33 -0800899
bsalomon0fd3c812016-05-11 10:38:05 -0700900 testContext->makeCurrent();
bsalomon41b952c2016-03-11 06:46:33 -0800901 REPORTER_ASSERT(reporter, proxy);
Brian Salomon534cbe52017-01-03 11:35:56 -0500902 auto createLarge = [context] {
903 return create_image_large(context->caps()->maxTextureSize());
904 };
bsalomon41b952c2016-03-11 06:46:33 -0800905 struct {
ericrkb4da01d2016-06-13 11:18:14 -0700906 std::function<sk_sp<SkImage> ()> fImageFactory;
907 std::vector<SkImage::DeferredTextureImageUsageParams> fParams;
908 SkFilterQuality fExpectedQuality;
909 int fExpectedScaleFactor;
910 bool fExpectation;
bsalomon41b952c2016-03-11 06:46:33 -0800911 } testCases[] = {
cblume33e0cb52016-08-30 12:09:23 -0700912 { create_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
913 kNone_SkFilterQuality, 1, true },
914 { create_codec_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
915 kNone_SkFilterQuality, 1, true },
916 { create_data_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
917 kNone_SkFilterQuality, 1, true },
918 { create_picture_image, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
919 kNone_SkFilterQuality, 1, false },
920 { [context] { return create_gpu_image(context); },
921 {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
922 kNone_SkFilterQuality, 1, false },
bsalomon41b952c2016-03-11 06:46:33 -0800923 // Create a texture image in a another GrContext.
bsalomon0fd3c812016-05-11 10:38:05 -0700924 { [testContext, otherContextInfo] {
925 otherContextInfo.testContext()->makeCurrent();
bsalomon8b7451a2016-05-11 06:33:06 -0700926 sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
bsalomon0fd3c812016-05-11 10:38:05 -0700927 testContext->makeCurrent();
bsalomon41b952c2016-03-11 06:46:33 -0800928 return otherContextImage;
cblume33e0cb52016-08-30 12:09:23 -0700929 }, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
Brian Salomon107524c2017-03-20 17:08:43 -0400930 kNone_SkFilterQuality, 1, false },
ericrkb4da01d2016-06-13 11:18:14 -0700931 // Create an image that is too large to upload.
Brian Salomon534cbe52017-01-03 11:35:56 -0500932 { createLarge, {{SkMatrix::I(), kNone_SkFilterQuality, 0}},
cblume33e0cb52016-08-30 12:09:23 -0700933 kNone_SkFilterQuality, 1, false },
ericrkb4da01d2016-06-13 11:18:14 -0700934 // Create an image that is too large, but is scaled to an acceptable size.
Brian Salomon534cbe52017-01-03 11:35:56 -0500935 { createLarge, {{SkMatrix::I(), kMedium_SkFilterQuality, 4}},
ericrkb4da01d2016-06-13 11:18:14 -0700936 kMedium_SkFilterQuality, 16, true},
937 // Create an image with multiple low filter qualities, make sure we round up.
Brian Salomon534cbe52017-01-03 11:35:56 -0500938 { createLarge, {{SkMatrix::I(), kNone_SkFilterQuality, 4},
939 {SkMatrix::I(), kMedium_SkFilterQuality, 4}},
ericrkb4da01d2016-06-13 11:18:14 -0700940 kMedium_SkFilterQuality, 16, true},
941 // Create an image with multiple prescale levels, make sure we chose the minimum scale.
Brian Salomon534cbe52017-01-03 11:35:56 -0500942 { createLarge, {{SkMatrix::I(), kMedium_SkFilterQuality, 5},
943 {SkMatrix::I(), kMedium_SkFilterQuality, 4}},
ericrkb4da01d2016-06-13 11:18:14 -0700944 kMedium_SkFilterQuality, 16, true},
bsalomon41b952c2016-03-11 06:46:33 -0800945 };
946
947
948 for (auto testCase : testCases) {
reed9ce9d672016-03-17 10:51:11 -0700949 sk_sp<SkImage> image(testCase.fImageFactory());
scroggo8e6c7ad2016-09-16 08:20:38 -0700950 if (!image) {
951 ERRORF(reporter, "Failed to create image!");
952 continue;
953 }
954
ericrkb4da01d2016-06-13 11:18:14 -0700955 size_t size = image->getDeferredTextureImageData(*proxy, testCase.fParams.data(),
956 static_cast<int>(testCase.fParams.size()),
Brian Osman6c15cc72016-10-17 09:51:37 -0400957 nullptr, nullptr);
bsalomon41b952c2016-03-11 06:46:33 -0800958 static const char *const kFS[] = { "fail", "succeed" };
959 if (SkToBool(size) != testCase.fExpectation) {
960 ERRORF(reporter, "This image was expected to %s but did not.",
961 kFS[testCase.fExpectation]);
962 }
963 if (size) {
964 void* buffer = sk_malloc_throw(size);
965 void* misaligned = reinterpret_cast<void*>(reinterpret_cast<intptr_t>(buffer) + 3);
ericrkb4da01d2016-06-13 11:18:14 -0700966 if (image->getDeferredTextureImageData(*proxy, testCase.fParams.data(),
967 static_cast<int>(testCase.fParams.size()),
Brian Osman6c15cc72016-10-17 09:51:37 -0400968 misaligned, nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800969 ERRORF(reporter, "Should fail when buffer is misaligned.");
970 }
ericrkb4da01d2016-06-13 11:18:14 -0700971 if (!image->getDeferredTextureImageData(*proxy, testCase.fParams.data(),
972 static_cast<int>(testCase.fParams.size()),
Brian Osman6c15cc72016-10-17 09:51:37 -0400973 buffer, nullptr)) {
bsalomon41b952c2016-03-11 06:46:33 -0800974 ERRORF(reporter, "deferred image size succeeded but creation failed.");
975 } else {
976 for (auto budgeted : { SkBudgeted::kNo, SkBudgeted::kYes }) {
reed9ce9d672016-03-17 10:51:11 -0700977 sk_sp<SkImage> newImage(
978 SkImage::MakeFromDeferredTextureImageData(context, buffer, budgeted));
mtklein5f939ab2016-03-16 10:28:35 -0700979 REPORTER_ASSERT(reporter, newImage != nullptr);
bsalomon41b952c2016-03-11 06:46:33 -0800980 if (newImage) {
ericrkb4da01d2016-06-13 11:18:14 -0700981 // Scale the image in software for comparison.
982 SkImageInfo scaled_info = SkImageInfo::MakeN32(
brianosman69c166d2016-08-17 14:01:05 -0700983 image->width() / testCase.fExpectedScaleFactor,
984 image->height() / testCase.fExpectedScaleFactor,
985 image->alphaType());
ericrkb4da01d2016-06-13 11:18:14 -0700986 SkAutoPixmapStorage scaled;
987 scaled.alloc(scaled_info);
988 image->scalePixels(scaled, testCase.fExpectedQuality);
989 sk_sp<SkImage> scaledImage = SkImage::MakeRasterCopy(scaled);
990 check_images_same(reporter, scaledImage.get(), newImage.get());
bsalomon41b952c2016-03-11 06:46:33 -0800991 }
992 // The other context should not be able to create images from texture data
993 // created by the original context.
reed9ce9d672016-03-17 10:51:11 -0700994 sk_sp<SkImage> newImage2(SkImage::MakeFromDeferredTextureImageData(
bsalomon8b7451a2016-05-11 06:33:06 -0700995 otherContextInfo.grContext(), buffer, budgeted));
bsalomon41b952c2016-03-11 06:46:33 -0800996 REPORTER_ASSERT(reporter, !newImage2);
bsalomon0fd3c812016-05-11 10:38:05 -0700997 testContext->makeCurrent();
bsalomon41b952c2016-03-11 06:46:33 -0800998 }
999 }
1000 sk_free(buffer);
1001 }
1002 }
1003}
kkinnunen4e184132015-11-17 22:53:28 -08001004#endif
reedeb560282016-07-26 19:42:04 -07001005
1006///////////////////////////////////////////////////////////////////////////////////////////////////
1007
Matt Sarett0bb6f382017-03-06 10:28:24 -05001008static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
1009 SkPictureRecorder recorder;
1010 SkCanvas* canvas = recorder.beginRecording(10, 10);
1011 canvas->clear(SK_ColorCYAN);
1012 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
1013 nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
1014};
1015
Matt Sarett6de13102017-03-14 14:10:48 -04001016static inline bool almost_equal(int a, int b) {
1017 return SkTAbs(a - b) <= 1;
1018}
1019
Matt Sarett0bb6f382017-03-06 10:28:24 -05001020DEF_TEST(Image_ColorSpace, r) {
1021 sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
1022 sk_sp<SkImage> image = GetResourceAsImage("mandrill_512_q075.jpg");
1023 REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
1024
1025 image = GetResourceAsImage("webp-color-profile-lossy.webp");
1026 SkColorSpaceTransferFn fn;
1027 bool success = image->colorSpace()->isNumericalTransferFn(&fn);
1028 REPORTER_ASSERT(r, success);
1029 REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.fG));
1030
1031 sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1032 SkColorSpace::kRec2020_Gamut);
1033 image = create_picture_image(rec2020);
1034 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1035
1036 SkBitmap bitmap;
1037 SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
1038 bitmap.allocPixels(info);
1039 image = SkImage::MakeFromBitmap(bitmap);
1040 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1041
1042 sk_sp<SkSurface> surface = SkSurface::MakeRaster(
1043 SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
1044 image = surface->makeImageSnapshot();
1045 REPORTER_ASSERT(r, nullptr == image->colorSpace());
1046
1047 surface = SkSurface::MakeRaster(info);
1048 image = surface->makeImageSnapshot();
1049 REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
1050}
1051
Matt Sarett6de13102017-03-14 14:10:48 -04001052DEF_TEST(Image_makeColorSpace, r) {
1053 sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma,
1054 SkColorSpace::kDCIP3_D65_Gamut);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001055 SkColorSpaceTransferFn fn;
1056 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;
1057 sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkColorSpace::kAdobeRGB_Gamut);
Matt Sarett6de13102017-03-14 14:10:48 -04001058
1059 SkBitmap srgbBitmap;
1060 srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
1061 *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
1062 srgbBitmap.setImmutable();
1063 sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
Matt Sarettcb874232017-04-05 11:41:27 -04001064 sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore);
Matt Sarett6de13102017-03-14 14:10:48 -04001065 SkBitmap p3Bitmap;
1066 bool success = p3Image->asLegacyBitmap(&p3Bitmap, SkImage::kRO_LegacyBitmapMode);
1067 REPORTER_ASSERT(r, success);
1068 p3Bitmap.lockPixels();
1069 REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1070 REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1071 REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1072
Matt Sarettcb874232017-04-05 11:41:27 -04001073 sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut,
1074 SkTransferFunctionBehavior::kIgnore);
Matt Sarett5b1dba82017-04-03 11:42:52 -04001075 SkBitmap adobeBitmap;
1076 success = adobeImage->asLegacyBitmap(&adobeBitmap, SkImage::kRO_LegacyBitmapMode);
1077 REPORTER_ASSERT(r, success);
1078 adobeBitmap.lockPixels();
1079 REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
1080 REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
1081 REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
1082
Matt Sarett6de13102017-03-14 14:10:48 -04001083 srgbImage = GetResourceAsImage("1x1.png");
Matt Sarettcb874232017-04-05 11:41:27 -04001084 p3Image = srgbImage->makeColorSpace(p3, SkTransferFunctionBehavior::kIgnore);
Matt Sarett6de13102017-03-14 14:10:48 -04001085 success = p3Image->asLegacyBitmap(&p3Bitmap, SkImage::kRO_LegacyBitmapMode);
1086 REPORTER_ASSERT(r, success);
1087 p3Bitmap.lockPixels();
1088 REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
1089 REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
1090 REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
1091}
1092
Matt Sarett0bb6f382017-03-06 10:28:24 -05001093///////////////////////////////////////////////////////////////////////////////////////////////////
1094
reedeb560282016-07-26 19:42:04 -07001095static void make_all_premul(SkBitmap* bm) {
1096 bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1097 for (int a = 0; a < 256; ++a) {
1098 for (int r = 0; r < 256; ++r) {
1099 // make all valid premul combinations
1100 int c = SkTMin(a, r);
1101 *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
1102 }
1103 }
1104}
1105
1106static bool equal(const SkBitmap& a, const SkBitmap& b) {
1107 SkASSERT(a.width() == b.width());
1108 SkASSERT(a.height() == b.height());
1109 for (int y = 0; y < a.height(); ++y) {
Mike Reed6f0286f2016-11-28 17:26:27 -05001110 for (int x = 0; x < a.width(); ++x) {
1111 SkPMColor pa = *a.getAddr32(x, y);
1112 SkPMColor pb = *b.getAddr32(x, y);
1113 if (pa != pb) {
1114 return false;
1115 }
reedeb560282016-07-26 19:42:04 -07001116 }
1117 }
1118 return true;
1119}
1120
1121DEF_TEST(image_roundtrip_encode, reporter) {
1122 SkBitmap bm0;
1123 make_all_premul(&bm0);
1124
1125 auto img0 = SkImage::MakeFromBitmap(bm0);
Hal Canarydb683012016-11-23 08:55:18 -07001126 sk_sp<SkData> data(img0->encode(SkEncodedImageFormat::kPNG, 100));
reedeb560282016-07-26 19:42:04 -07001127 auto img1 = SkImage::MakeFromEncoded(data);
mtklein2f3416d2016-08-02 16:02:05 -07001128
reedeb560282016-07-26 19:42:04 -07001129 SkBitmap bm1;
1130 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1131 img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
mtklein2f3416d2016-08-02 16:02:05 -07001132
reedeb560282016-07-26 19:42:04 -07001133 REPORTER_ASSERT(reporter, equal(bm0, bm1));
1134}
1135
1136DEF_TEST(image_roundtrip_premul, reporter) {
1137 SkBitmap bm0;
1138 make_all_premul(&bm0);
1139
1140 SkBitmap bm1;
1141 bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
1142 bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
1143
1144 SkBitmap bm2;
1145 bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
1146 bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
1147
1148 REPORTER_ASSERT(reporter, equal(bm0, bm2));
1149}