reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 8 | #include <functional> |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 9 | #include <initializer_list> |
ericrk | b4da01d | 2016-06-13 11:18:14 -0700 | [diff] [blame] | 10 | #include <vector> |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 11 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 12 | #include "SkAutoPixmapStorage.h" |
halcanary | c56c6ef | 2015-09-28 11:55:28 -0700 | [diff] [blame] | 13 | #include "SkBitmap.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 15 | #include "SkColorSpacePriv.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 16 | #include "SkData.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 17 | #include "SkImageEncoder.h" |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 18 | #include "SkImageGenerator.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 19 | #include "SkImage_Base.h" |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 20 | #include "SkImagePriv.h" |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 21 | #include "SkMakeUnique.h" |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 22 | #include "SkPicture.h" |
| 23 | #include "SkPictureRecorder.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 24 | #include "SkRRect.h" |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 25 | #include "SkSerialProcs.h" |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 26 | #include "SkStream.h" |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 27 | #include "SkSurface.h" |
| 28 | #include "SkUtils.h" |
| 29 | #include "Test.h" |
| 30 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 31 | #include "Resources.h" |
Mike Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 32 | #include "sk_pixel_iter.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 33 | #include "sk_tool_utils.h" |
| 34 | |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 35 | #include "GrContextPriv.h" |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 36 | #include "GrGpu.h" |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 37 | #include "GrResourceCache.h" |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 38 | #include "GrTexture.h" |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 39 | #include "SkGr.h" |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 40 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 41 | using namespace sk_gpu_test; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 42 | |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 43 | SkImageInfo read_pixels_info(SkImage* image) { |
| 44 | if (as_IB(image)->onImageInfo().colorSpace()) { |
| 45 | return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType()); |
| 46 | } |
| 47 | |
| 48 | return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); |
| 49 | } |
| 50 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 51 | static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA, |
| 52 | SkImage* b) { |
| 53 | const int widthA = subsetA ? subsetA->width() : a->width(); |
| 54 | const int heightA = subsetA ? subsetA->height() : a->height(); |
| 55 | |
| 56 | REPORTER_ASSERT(reporter, widthA == b->width()); |
| 57 | REPORTER_ASSERT(reporter, heightA == b->height()); |
reed | 1cb3646 | 2016-03-09 15:21:32 -0800 | [diff] [blame] | 58 | |
| 59 | // see https://bug.skia.org/3965 |
| 60 | //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 61 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 62 | SkAutoPixmapStorage pmapA, pmapB; |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 63 | pmapA.alloc(read_pixels_info(a)); |
| 64 | pmapB.alloc(read_pixels_info(b)); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 65 | |
| 66 | const int srcX = subsetA ? subsetA->x() : 0; |
| 67 | const int srcY = subsetA ? subsetA->y() : 0; |
| 68 | |
| 69 | REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY)); |
| 70 | REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0)); |
| 71 | |
Matt Sarett | f575993 | 2017-02-07 21:52:07 +0000 | [diff] [blame] | 72 | const size_t widthBytes = widthA * 4; |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 73 | for (int y = 0; y < heightA; ++y) { |
| 74 | REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes)); |
| 75 | } |
| 76 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 77 | static void draw_image_test_pattern(SkCanvas* canvas) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 78 | canvas->clear(SK_ColorWHITE); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 79 | SkPaint paint; |
| 80 | paint.setColor(SK_ColorBLACK); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 81 | canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); |
| 82 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 83 | static sk_sp<SkImage> create_image() { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 84 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 85 | auto surface(SkSurface::MakeRaster(info)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 86 | draw_image_test_pattern(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 87 | return surface->makeImageSnapshot(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 88 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 89 | static sk_sp<SkData> create_image_data(SkImageInfo* info) { |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 90 | *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 91 | const size_t rowBytes = info->minRowBytes(); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 92 | sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height())); |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 93 | { |
| 94 | SkBitmap bm; |
| 95 | bm.installPixels(*info, data->writable_data(), rowBytes); |
| 96 | SkCanvas canvas(bm); |
| 97 | draw_image_test_pattern(&canvas); |
| 98 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 99 | return data; |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 100 | } |
| 101 | static sk_sp<SkImage> create_data_image() { |
| 102 | SkImageInfo info; |
| 103 | sk_sp<SkData> data(create_image_data(&info)); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 104 | return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes()); |
scroggo | 9d08172 | 2016-04-20 08:27:18 -0700 | [diff] [blame] | 105 | } |
Brian Salomon | 534cbe5 | 2017-01-03 11:35:56 -0500 | [diff] [blame] | 106 | static sk_sp<SkImage> create_image_large(int maxTextureSize) { |
| 107 | const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType); |
bsalomon | d490708 | 2016-06-13 12:13:03 -0700 | [diff] [blame] | 108 | auto surface(SkSurface::MakeRaster(info)); |
| 109 | surface->getCanvas()->clear(SK_ColorWHITE); |
| 110 | SkPaint paint; |
| 111 | paint.setColor(SK_ColorBLACK); |
| 112 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint); |
| 113 | return surface->makeImageSnapshot(); |
| 114 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 115 | static sk_sp<SkImage> create_picture_image() { |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 116 | SkPictureRecorder recorder; |
| 117 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 118 | canvas->clear(SK_ColorCYAN); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 119 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10), |
Matt Sarett | e94255d | 2017-01-09 12:38:59 -0500 | [diff] [blame] | 120 | nullptr, nullptr, SkImage::BitDepth::kU8, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 121 | SkColorSpace::MakeSRGB()); |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 122 | }; |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 123 | // Want to ensure that our Release is called when the owning image is destroyed |
| 124 | struct RasterDataHolder { |
| 125 | RasterDataHolder() : fReleaseCount(0) {} |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 126 | sk_sp<SkData> fData; |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 127 | int fReleaseCount; |
| 128 | static void Release(const void* pixels, void* context) { |
| 129 | RasterDataHolder* self = static_cast<RasterDataHolder*>(context); |
| 130 | self->fReleaseCount++; |
| 131 | self->fData.reset(); |
| 132 | } |
| 133 | }; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 134 | static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 135 | SkASSERT(dataHolder); |
| 136 | SkImageInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 137 | dataHolder->fData = create_image_data(&info); |
| 138 | return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()), |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 139 | RasterDataHolder::Release, dataHolder); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 140 | } |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 141 | static sk_sp<SkImage> create_codec_image() { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 142 | SkImageInfo info; |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 143 | sk_sp<SkData> data(create_image_data(&info)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 144 | SkBitmap bitmap; |
| 145 | bitmap.installPixels(info, data->writable_data(), info.minRowBytes()); |
Leon Scroggins III | 0098ccb | 2018-09-24 15:24:31 -0400 | [diff] [blame] | 146 | auto src = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 100); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 147 | return SkImage::MakeFromEncoded(std::move(src)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 148 | } |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 149 | static sk_sp<SkImage> create_gpu_image(GrContext* context, bool withMips = false) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 150 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 151 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, |
| 152 | kBottomLeft_GrSurfaceOrigin, nullptr, withMips)); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 153 | draw_image_test_pattern(surface->getCanvas()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 154 | return surface->makeImageSnapshot(); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 155 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 156 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 157 | static void test_encode(skiatest::Reporter* reporter, SkImage* image) { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 158 | const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 159 | sk_sp<SkData> origEncoded = image->encodeToData(); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 160 | REPORTER_ASSERT(reporter, origEncoded); |
| 161 | REPORTER_ASSERT(reporter, origEncoded->size() > 0); |
| 162 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 163 | sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded)); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 164 | if (!decoded) { |
| 165 | ERRORF(reporter, "failed to decode image!"); |
| 166 | return; |
| 167 | } |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 168 | REPORTER_ASSERT(reporter, decoded); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 169 | assert_equal(reporter, image, nullptr, decoded.get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 170 | |
| 171 | // Now see if we can instantiate an image from a subset of the surface/origEncoded |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 172 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 173 | decoded = SkImage::MakeFromEncoded(origEncoded, &ir); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 174 | REPORTER_ASSERT(reporter, decoded); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 175 | assert_equal(reporter, image, &ir, decoded.get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 178 | DEF_TEST(ImageEncode, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 179 | test_encode(reporter, create_image().get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 180 | } |
| 181 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 182 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 183 | test_encode(reporter, create_gpu_image(ctxInfo.grContext()).get()); |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 184 | } |
reed | 759373a | 2015-07-03 21:01:10 -0700 | [diff] [blame] | 185 | |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 186 | DEF_TEST(Image_MakeFromRasterBitmap, reporter) { |
| 187 | const struct { |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 188 | SkCopyPixelsMode fCPM; |
| 189 | bool fExpectSameAsMutable; |
| 190 | bool fExpectSameAsImmutable; |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 191 | } recs[] = { |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 192 | { kIfMutable_SkCopyPixelsMode, false, true }, |
| 193 | { kAlways_SkCopyPixelsMode, false, false }, |
| 194 | { kNever_SkCopyPixelsMode, true, true }, |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 195 | }; |
| 196 | for (auto rec : recs) { |
| 197 | SkPixmap pm; |
| 198 | SkBitmap bm; |
| 199 | bm.allocN32Pixels(100, 100); |
| 200 | |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 201 | auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 202 | REPORTER_ASSERT(reporter, img->peekPixels(&pm)); |
| 203 | const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0); |
| 204 | REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable); |
reed | ae29644 | 2016-08-05 13:19:01 -0700 | [diff] [blame] | 205 | REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 206 | |
| 207 | bm.notifyPixelsChanged(); // force a new generation ID |
| 208 | |
| 209 | bm.setImmutable(); |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 210 | img = SkMakeImageFromRasterBitmap(bm, rec.fCPM); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, img->peekPixels(&pm)); |
| 212 | const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0); |
| 213 | REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable); |
reed | ae29644 | 2016-08-05 13:19:01 -0700 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable); |
reed | 2dad769 | 2016-08-01 11:12:58 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 218 | // Test that image encoding failures do not break picture serialization/deserialization. |
| 219 | DEF_TEST(Image_Serialize_Encoding_Failure, reporter) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 220 | auto surface(SkSurface::MakeRasterN32Premul(100, 100)); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 221 | surface->getCanvas()->clear(SK_ColorGREEN); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 222 | sk_sp<SkImage> image(surface->makeImageSnapshot()); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 223 | REPORTER_ASSERT(reporter, image); |
| 224 | |
| 225 | SkPictureRecorder recorder; |
| 226 | SkCanvas* canvas = recorder.beginRecording(100, 100); |
| 227 | canvas->drawImage(image, 0, 0); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 228 | sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 229 | REPORTER_ASSERT(reporter, picture); |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 230 | REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 231 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 232 | bool was_called = false; |
| 233 | SkSerialProcs procs; |
| 234 | procs.fImageProc = [](SkImage*, void* called) { |
| 235 | *(bool*)called = true; |
| 236 | return SkData::MakeEmpty(); |
| 237 | }; |
| 238 | procs.fImageCtx = &was_called; |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 239 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 240 | REPORTER_ASSERT(reporter, !was_called); |
Mike Reed | 47fdf6c | 2017-12-20 14:12:07 -0500 | [diff] [blame] | 241 | auto data = picture->serialize(&procs); |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 242 | REPORTER_ASSERT(reporter, was_called); |
| 243 | REPORTER_ASSERT(reporter, data && data->size() > 0); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 244 | |
Mike Reed | ef03848 | 2017-12-16 08:41:28 -0500 | [diff] [blame] | 245 | auto deserialized = SkPicture::MakeFromData(data->data(), data->size()); |
| 246 | REPORTER_ASSERT(reporter, deserialized); |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 247 | REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0); |
fmalita | c347034 | 2015-09-04 11:36:39 -0700 | [diff] [blame] | 248 | } |
| 249 | |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 250 | // Test that a draw that only partially covers the drawing surface isn't |
| 251 | // interpreted as covering the entire drawing surface (i.e., exercise one of the |
| 252 | // conditions of SkCanvas::wouldOverwriteEntireSurface()). |
| 253 | DEF_TEST(Image_RetainSnapshot, reporter) { |
| 254 | const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0); |
| 255 | const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0); |
| 256 | SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 257 | auto surface(SkSurface::MakeRaster(info)); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 258 | surface->getCanvas()->clear(0xFF00FF00); |
| 259 | |
| 260 | SkPMColor pixels[4]; |
| 261 | memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect |
| 262 | const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2); |
| 263 | const size_t dstRowBytes = 2 * sizeof(SkPMColor); |
| 264 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 265 | sk_sp<SkImage> image1(surface->makeImageSnapshot()); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 266 | REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0)); |
| 267 | for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) { |
| 268 | REPORTER_ASSERT(reporter, pixels[i] == green); |
| 269 | } |
| 270 | |
| 271 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 272 | paint.setBlendMode(SkBlendMode::kSrc); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 273 | paint.setColor(SK_ColorRED); |
| 274 | |
| 275 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint); |
| 276 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 277 | sk_sp<SkImage> image2(surface->makeImageSnapshot()); |
fmalita | 8c0144c | 2015-07-22 05:56:16 -0700 | [diff] [blame] | 278 | REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0)); |
| 279 | REPORTER_ASSERT(reporter, pixels[0] == green); |
| 280 | REPORTER_ASSERT(reporter, pixels[1] == green); |
| 281 | REPORTER_ASSERT(reporter, pixels[2] == green); |
| 282 | REPORTER_ASSERT(reporter, pixels[3] == red); |
| 283 | } |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 284 | |
| 285 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 286 | |
| 287 | static void make_bitmap_mutable(SkBitmap* bm) { |
| 288 | bm->allocN32Pixels(10, 10); |
| 289 | } |
| 290 | |
| 291 | static void make_bitmap_immutable(SkBitmap* bm) { |
| 292 | bm->allocN32Pixels(10, 10); |
| 293 | bm->setImmutable(); |
| 294 | } |
| 295 | |
| 296 | DEF_TEST(image_newfrombitmap, reporter) { |
| 297 | const struct { |
| 298 | void (*fMakeProc)(SkBitmap*); |
| 299 | bool fExpectPeekSuccess; |
| 300 | bool fExpectSharedID; |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 301 | bool fExpectLazy; |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 302 | } rec[] = { |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 303 | { make_bitmap_mutable, true, false, false }, |
| 304 | { make_bitmap_immutable, true, true, false }, |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 308 | SkBitmap bm; |
| 309 | rec[i].fMakeProc(&bm); |
| 310 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 311 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 312 | SkPixmap pmap; |
| 313 | |
| 314 | const bool sharedID = (image->uniqueID() == bm.getGenerationID()); |
| 315 | REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID); |
| 316 | |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 317 | const bool peekSuccess = image->peekPixels(&pmap); |
| 318 | REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess); |
fmalita | ddbbdda | 2015-08-20 08:47:26 -0700 | [diff] [blame] | 319 | |
| 320 | const bool lazy = image->isLazyGenerated(); |
| 321 | REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy); |
reed | 80c772b | 2015-07-30 18:58:23 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 324 | |
| 325 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 326 | |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 327 | #include "SkBitmapCache.h" |
| 328 | |
| 329 | /* |
| 330 | * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image. |
| 331 | * We cache it for performance when drawing into a raster surface. |
| 332 | * |
| 333 | * A cleaner test would know if each drawImage call triggered a read-back from the gpu, |
| 334 | * but we don't have that facility (at the moment) so we use a little internal knowledge |
| 335 | * of *how* the raster version is cached, and look for that. |
| 336 | */ |
Brian Osman | e47e5b6 | 2018-10-04 14:19:39 -0400 | [diff] [blame] | 337 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_Gpu2Cpu, reporter, ctxInfo) { |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 338 | SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 339 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 340 | const auto desc = SkBitmapCacheDesc::Make(image.get()); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 341 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 342 | auto surface(SkSurface::MakeRaster(info)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 343 | |
| 344 | // now we can test drawing a gpu-backed image into a cpu-backed surface |
| 345 | |
| 346 | { |
| 347 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 348 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | surface->getCanvas()->drawImage(image, 0, 0); |
| 352 | { |
| 353 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 354 | if (SkBitmapCache::Find(desc, &cachedBitmap)) { |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 355 | REPORTER_ASSERT(reporter, cachedBitmap.isImmutable()); |
| 356 | REPORTER_ASSERT(reporter, cachedBitmap.getPixels()); |
| 357 | } else { |
| 358 | // unexpected, but not really a bug, since the cache is global and this test may be |
| 359 | // run w/ other threads competing for its budget. |
| 360 | SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n"); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | image.reset(nullptr); |
| 365 | { |
| 366 | SkBitmap cachedBitmap; |
Mike Reed | 5fa3d6d | 2017-03-25 09:51:00 -0400 | [diff] [blame] | 367 | REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap)); |
reed | 6f1216a | 2015-08-04 08:10:13 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
bsalomon | 8e74f80 | 2016-01-30 10:01:40 -0800 | [diff] [blame] | 370 | |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 371 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) { |
| 372 | GrContext* context = contextInfo.grContext(); |
| 373 | sk_gpu_test::TestContext* testContext = contextInfo.testContext(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 374 | GrContextFactory otherFactory; |
Robert Phillips | cdabbcc | 2017-06-08 16:03:17 -0400 | [diff] [blame] | 375 | ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type()); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 376 | testContext->makeCurrent(); |
| 377 | |
| 378 | std::function<sk_sp<SkImage>()> imageFactories[] = { |
| 379 | create_image, |
| 380 | create_codec_image, |
| 381 | create_data_image, |
| 382 | // Create an image from a picture. |
| 383 | create_picture_image, |
| 384 | // Create a texture image. |
| 385 | [context] { return create_gpu_image(context); }, |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 386 | // Create a texture image with mips |
| 387 | //[context] { return create_gpu_image(context, true); }, |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 388 | // Create a texture image in a another GrContext. |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 389 | [otherContextInfo] { |
| 390 | auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 391 | sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext()); |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 392 | otherContextInfo.grContext()->flush(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 393 | return otherContextImage; |
| 394 | } |
| 395 | }; |
| 396 | |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 397 | sk_sp<SkColorSpace> dstColorSpaces[] ={ |
| 398 | nullptr, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 399 | SkColorSpace::MakeSRGB(), |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 400 | }; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 401 | |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 402 | for (auto& dstColorSpace : dstColorSpaces) { |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 403 | for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) { |
| 404 | for (auto factory : imageFactories) { |
| 405 | sk_sp<SkImage> image(factory()); |
| 406 | if (!image) { |
| 407 | ERRORF(reporter, "Error creating image."); |
| 408 | continue; |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 409 | } |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 410 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 411 | sk_sp<SkImage> texImage(image->makeTextureImage(context, dstColorSpace.get(), |
| 412 | mipMapped)); |
| 413 | if (!texImage) { |
| 414 | GrContext* imageContext = as_IB(image)->context(); |
| 415 | |
| 416 | // We expect to fail if image comes from a different GrContext. |
| 417 | if (!image->isTextureBacked() || imageContext == context) { |
| 418 | ERRORF(reporter, "makeTextureImage failed."); |
| 419 | } |
| 420 | continue; |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 421 | } |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 422 | if (!texImage->isTextureBacked()) { |
| 423 | ERRORF(reporter, "makeTextureImage returned non-texture image."); |
| 424 | continue; |
| 425 | } |
| 426 | if (GrMipMapped::kYes == mipMapped && |
Greg Daniel | e141548 | 2018-06-13 13:09:18 -0400 | [diff] [blame] | 427 | as_IB(texImage)->peekProxy()->mipMapped() != mipMapped && |
| 428 | context->contextPriv().caps()->mipMapSupport()) { |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 429 | ERRORF(reporter, "makeTextureImage returned non-mipmapped texture."); |
| 430 | continue; |
| 431 | } |
| 432 | if (image->isTextureBacked()) { |
| 433 | GrSurfaceProxy* origProxy = as_IB(image)->peekProxy(); |
| 434 | GrSurfaceProxy* copyProxy = as_IB(texImage)->peekProxy(); |
| 435 | |
| 436 | if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) { |
| 437 | SkASSERT(origProxy->asTextureProxy()); |
| 438 | if (GrMipMapped::kNo == mipMapped || |
| 439 | GrMipMapped::kYes == origProxy->asTextureProxy()->mipMapped()) { |
| 440 | ERRORF(reporter, "makeTextureImage made unnecessary texture copy."); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | if (image->width() != texImage->width() || image->height() != texImage->height()) { |
| 445 | ERRORF(reporter, "makeTextureImage changed the image size."); |
| 446 | } |
| 447 | if (image->alphaType() != texImage->alphaType()) { |
| 448 | ERRORF(reporter, "makeTextureImage changed image alpha type."); |
| 449 | } |
Brian Osman | e8827d2 | 2017-02-07 12:31:02 -0500 | [diff] [blame] | 450 | } |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 451 | } |
Robert Phillips | d9971c0 | 2017-08-02 11:51:43 -0400 | [diff] [blame] | 452 | context->flush(); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 456 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) { |
| 457 | GrContext* context = contextInfo.grContext(); |
| 458 | |
| 459 | std::function<sk_sp<SkImage>()> imageFactories[] = { |
| 460 | create_image, |
| 461 | create_codec_image, |
| 462 | create_data_image, |
| 463 | create_picture_image, |
| 464 | [context] { return create_gpu_image(context); }, |
| 465 | }; |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 466 | SkColorSpace* legacyColorSpace = nullptr; |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 467 | for (auto factory : imageFactories) { |
| 468 | sk_sp<SkImage> image = factory(); |
| 469 | if (!image->isTextureBacked()) { |
| 470 | REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get()); |
Brian Osman | 041f7df | 2017-02-07 11:23:28 -0500 | [diff] [blame] | 471 | if (!(image = image->makeTextureImage(context, legacyColorSpace))) { |
| 472 | continue; |
| 473 | } |
bsalomon | 634b430 | 2016-07-12 18:11:17 -0700 | [diff] [blame] | 474 | } |
| 475 | auto rasterImage = image->makeNonTextureImage(); |
| 476 | if (!rasterImage) { |
| 477 | ERRORF(reporter, "makeNonTextureImage failed for texture-backed image."); |
| 478 | } |
| 479 | REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked()); |
| 480 | assert_equal(reporter, image.get(), nullptr, rasterImage.get()); |
| 481 | } |
| 482 | } |
| 483 | |
bsalomon | 363bc33 | 2016-06-15 14:15:30 -0700 | [diff] [blame] | 484 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_drawAbandonedGpuImage, reporter, contextInfo) { |
bsalomon | e5b651f | 2016-06-15 07:36:15 -0700 | [diff] [blame] | 485 | auto context = contextInfo.grContext(); |
| 486 | auto image = create_gpu_image(context); |
| 487 | auto info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 488 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
Robert Phillips | 8744405 | 2017-06-23 14:09:30 -0400 | [diff] [blame] | 489 | image->getTexture()->abandon(); |
bsalomon | e5b651f | 2016-06-15 07:36:15 -0700 | [diff] [blame] | 490 | surface->getCanvas()->drawImage(image, 0, 0); |
| 491 | } |
| 492 | |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 493 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) { |
| 494 | for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) { |
| 495 | static constexpr int kSize = 10; |
| 496 | SkColorType colorType = static_cast<SkColorType>(ct); |
| 497 | bool can = ctxInfo.grContext()->colorTypeSupportedAsImage(colorType); |
| 498 | auto* gpu = ctxInfo.grContext()->contextPriv().getGpu(); |
| 499 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 500 | nullptr, kSize, kSize, colorType, false, GrMipMapped::kNo); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 501 | auto img = |
| 502 | SkImage::MakeFromTexture(ctxInfo.grContext(), backendTex, kTopLeft_GrSurfaceOrigin, |
| 503 | colorType, kOpaque_SkAlphaType, nullptr); |
| 504 | REPORTER_ASSERT(reporter, can == SkToBool(img), |
Oleg Maximenko | 5d4604b | 2018-02-26 17:58:58 +0300 | [diff] [blame] | 505 | "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img), |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 506 | colorType); |
| 507 | |
| 508 | img.reset(); |
| 509 | ctxInfo.grContext()->flush(); |
| 510 | if (backendTex.isValid()) { |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 511 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Brian Salomon | 9708af8 | 2018-02-05 12:57:10 -0500 | [diff] [blame] | 516 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) { |
| 517 | SkBitmap bmp; |
| 518 | bmp.allocPixels( |
| 519 | SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr)); |
| 520 | for (int y = 0; y < 256; ++y) { |
| 521 | for (int x = 0; x < 256; ++x) { |
| 522 | *bmp.getAddr32(x, y) = |
| 523 | SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x); |
| 524 | } |
| 525 | } |
| 526 | auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(ctxInfo.grContext(), nullptr); |
| 527 | if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) { |
| 528 | ERRORF(reporter, "Failed to make unpremul texture image."); |
| 529 | return; |
| 530 | } |
| 531 | // The GPU backend always unpremuls the values stored in the texture because it assumes they |
| 532 | // are premul values. (skbug.com/7580). |
| 533 | if (false) { |
| 534 | SkBitmap unpremul; |
| 535 | unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, |
| 536 | kUnpremul_SkAlphaType, nullptr)); |
| 537 | if (!texImage->readPixels(unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(), 0, |
| 538 | 0)) { |
| 539 | ERRORF(reporter, "Unpremul readback failed."); |
| 540 | return; |
| 541 | } |
| 542 | for (int y = 0; y < 256; ++y) { |
| 543 | for (int x = 0; x < 256; ++x) { |
| 544 | if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) { |
| 545 | ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.", |
| 546 | *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y); |
| 547 | return; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | SkBitmap premul; |
| 553 | premul.allocPixels( |
| 554 | SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr)); |
| 555 | if (!texImage->readPixels(premul.info(), premul.getPixels(), premul.rowBytes(), 0, 0)) { |
| 556 | ERRORF(reporter, "Unpremul readback failed."); |
| 557 | return; |
| 558 | } |
| 559 | for (int y = 0; y < 256; ++y) { |
| 560 | for (int x = 0; x < 256; ++x) { |
| 561 | // Treat bmp's color as a pm color even though it may be the r/b swap of a PM color. |
| 562 | // SkPremultiplyColor acts the same on both channels. |
| 563 | uint32_t origColor = SkPreMultiplyColor(*bmp.getAddr32(x, y)); |
| 564 | int32_t origA = (origColor >> 24) & 0xff; |
| 565 | int32_t origB = (origColor >> 16) & 0xff; |
| 566 | int32_t origG = (origColor >> 8) & 0xff; |
| 567 | int32_t origR = (origColor >> 0) & 0xff; |
| 568 | uint32_t read = *premul.getAddr32(x, y); |
| 569 | int32_t readA = (read >> 24) & 0xff; |
| 570 | int32_t readB = (read >> 16) & 0xff; |
| 571 | int32_t readG = (read >> 8) & 0xff; |
| 572 | int32_t readR = (read >> 0) & 0xff; |
| 573 | // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little |
| 574 | // bit of tolerance for GPU vs CPU premul math. |
| 575 | int32_t tol = (origA == 0 || origA == 255) ? 0 : 1; |
| 576 | if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol || |
| 577 | SkTAbs(readR - origR) > tol) { |
| 578 | ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) at %d, %d.", |
| 579 | *bmp.getAddr32(x, y), *premul.getAddr32(x, y), x, y); |
| 580 | return; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Brian Salomon | 8a8dd33 | 2018-05-24 14:08:31 -0400 | [diff] [blame] | 586 | DEF_GPUTEST(AbandonedContextImage, reporter, options) { |
| 587 | using Factory = sk_gpu_test::GrContextFactory; |
| 588 | for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) { |
| 589 | auto type = static_cast<Factory::ContextType>(ct); |
| 590 | std::unique_ptr<Factory> factory(new Factory); |
| 591 | if (!factory->get(type)) { |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | sk_sp<SkImage> img; |
| 596 | auto gsurf = SkSurface::MakeRenderTarget( |
| 597 | factory->get(type), SkBudgeted::kYes, |
| 598 | SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, |
| 599 | nullptr); |
| 600 | if (!gsurf) { |
| 601 | continue; |
| 602 | } |
| 603 | img = gsurf->makeImageSnapshot(); |
| 604 | gsurf.reset(); |
| 605 | |
| 606 | auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100)); |
| 607 | |
| 608 | REPORTER_ASSERT(reporter, img->isValid(factory->get(type))); |
| 609 | REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->getGrContext())); |
| 610 | |
| 611 | factory->get(type)->abandonContext(); |
| 612 | REPORTER_ASSERT(reporter, !img->isValid(factory->get(type))); |
| 613 | REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext())); |
| 614 | // This shouldn't crash. |
| 615 | rsurf->getCanvas()->drawImage(img, 0, 0); |
| 616 | |
| 617 | // Give up all other refs on GrContext. |
| 618 | factory.reset(nullptr); |
| 619 | REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext())); |
| 620 | // This shouldn't crash. |
| 621 | rsurf->getCanvas()->drawImage(img, 0, 0); |
| 622 | } |
| 623 | } |
| 624 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 625 | class EmptyGenerator : public SkImageGenerator { |
| 626 | public: |
| 627 | EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {} |
| 628 | }; |
| 629 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 630 | DEF_TEST(ImageEmpty, reporter) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 631 | const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 632 | SkPixmap pmap(info, nullptr, 0); |
| 633 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap)); |
| 634 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0)); |
| 635 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr)); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 636 | REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator( |
| 637 | skstd::make_unique<EmptyGenerator>())); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 638 | } |
| 639 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 640 | DEF_TEST(ImageDataRef, reporter) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 641 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1); |
| 642 | size_t rowBytes = info.minRowBytes(); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 643 | size_t size = info.computeByteSize(rowBytes); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 644 | sk_sp<SkData> data = SkData::MakeUninitialized(size); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 645 | REPORTER_ASSERT(reporter, data->unique()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 646 | sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 647 | REPORTER_ASSERT(reporter, !data->unique()); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 648 | image.reset(); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 649 | REPORTER_ASSERT(reporter, data->unique()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 650 | } |
| 651 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 652 | static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) { |
| 653 | for (int i = 0; i < count; ++i) { |
| 654 | if (pixels[i] != expected) { |
| 655 | return false; |
| 656 | } |
| 657 | } |
| 658 | return true; |
| 659 | } |
| 660 | |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 661 | static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 662 | if (!image) { |
| 663 | ERRORF(reporter, "Failed to create image!"); |
| 664 | return; |
| 665 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 666 | const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 667 | const SkPMColor notExpected = ~expected; |
| 668 | |
| 669 | const int w = 2, h = 2; |
| 670 | const size_t rowBytes = w * sizeof(SkPMColor); |
| 671 | SkPMColor pixels[w*h]; |
| 672 | |
| 673 | SkImageInfo info; |
| 674 | |
| 675 | info = SkImageInfo::MakeUnknown(w, h); |
| 676 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 677 | |
| 678 | // out-of-bounds should fail |
| 679 | info = SkImageInfo::MakeN32Premul(w, h); |
| 680 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0)); |
| 681 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h)); |
| 682 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0)); |
| 683 | REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height())); |
| 684 | |
| 685 | // top-left should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 686 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 687 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0)); |
| 688 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 689 | |
| 690 | // bottom-right should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 691 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 692 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 693 | image->width() - w, image->height() - h)); |
| 694 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected)); |
| 695 | |
| 696 | // partial top-left should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 697 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 698 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1)); |
| 699 | REPORTER_ASSERT(reporter, pixels[3] == expected); |
| 700 | REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected)); |
| 701 | |
| 702 | // partial bottom-right should succeed |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 703 | sk_memset32(pixels, notExpected, w*h); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 704 | REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, |
| 705 | image->width() - 1, image->height() - 1)); |
| 706 | REPORTER_ASSERT(reporter, pixels[0] == expected); |
| 707 | REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected)); |
| 708 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 709 | DEF_TEST(ImageReadPixels, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 710 | sk_sp<SkImage> image(create_image()); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 711 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 712 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 713 | image = create_data_image(); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 714 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 715 | |
| 716 | RasterDataHolder dataHolder; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 717 | image = create_rasterproc_image(&dataHolder); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 718 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 719 | image.reset(); |
| 720 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 721 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 722 | image = create_codec_image(); |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 723 | image_test_read_pixels(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 724 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 725 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) { |
Robert Phillips | 3500b77 | 2017-01-27 10:11:42 -0500 | [diff] [blame] | 726 | image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 727 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 728 | |
| 729 | static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image, |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 730 | const SkBitmap& bitmap) { |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 731 | REPORTER_ASSERT(reporter, image->width() == bitmap.width()); |
| 732 | REPORTER_ASSERT(reporter, image->height() == bitmap.height()); |
brianosman | 69c166d | 2016-08-17 14:01:05 -0700 | [diff] [blame] | 733 | REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 734 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 735 | REPORTER_ASSERT(reporter, bitmap.isImmutable()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 736 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 737 | REPORTER_ASSERT(reporter, bitmap.getPixels()); |
| 738 | |
| 739 | const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType()); |
| 740 | SkPMColor imageColor; |
| 741 | REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0)); |
| 742 | REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0)); |
| 743 | } |
| 744 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 745 | static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 746 | if (!image) { |
| 747 | ERRORF(reporter, "Failed to create image."); |
| 748 | return; |
| 749 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 750 | SkBitmap bitmap; |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 751 | REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap)); |
| 752 | check_legacy_bitmap(reporter, image, bitmap); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 753 | |
| 754 | // Test subsetting to exercise the rowBytes logic. |
| 755 | SkBitmap tmp; |
| 756 | REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2, |
| 757 | image->height() / 2))); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 758 | sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp)); |
| 759 | REPORTER_ASSERT(reporter, subsetImage.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 760 | |
| 761 | SkBitmap subsetBitmap; |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 762 | REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap)); |
| 763 | check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 764 | } |
| 765 | DEF_TEST(ImageLegacyBitmap, reporter) { |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 766 | sk_sp<SkImage> image(create_image()); |
| 767 | test_legacy_bitmap(reporter, image.get()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 768 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 769 | image = create_data_image(); |
| 770 | test_legacy_bitmap(reporter, image.get()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 771 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 772 | RasterDataHolder dataHolder; |
| 773 | image = create_rasterproc_image(&dataHolder); |
| 774 | test_legacy_bitmap(reporter, image.get()); |
| 775 | image.reset(); |
| 776 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 777 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 778 | image = create_codec_image(); |
| 779 | test_legacy_bitmap(reporter, image.get()); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 780 | } |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 781 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) { |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 782 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
| 783 | test_legacy_bitmap(reporter, image.get()); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 784 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 785 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 786 | static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 787 | if (!image) { |
| 788 | ERRORF(reporter, "Failed to create image!"); |
| 789 | return; |
| 790 | } |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 791 | SkPixmap pm; |
| 792 | bool success = image->peekPixels(&pm); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 793 | REPORTER_ASSERT(reporter, expectPeekSuccess == success); |
| 794 | if (success) { |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 795 | const SkImageInfo& info = pm.info(); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 796 | REPORTER_ASSERT(reporter, 20 == info.width()); |
| 797 | REPORTER_ASSERT(reporter, 20 == info.height()); |
| 798 | REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType()); |
| 799 | REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() || |
| 800 | kOpaque_SkAlphaType == info.alphaType()); |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 801 | REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes()); |
| 802 | REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0)); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 803 | } |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 804 | } |
| 805 | DEF_TEST(ImagePeek, reporter) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 806 | sk_sp<SkImage> image(create_image()); |
| 807 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 808 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 809 | image = create_data_image(); |
| 810 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 811 | |
| 812 | RasterDataHolder dataHolder; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 813 | image = create_rasterproc_image(&dataHolder); |
| 814 | test_peek(reporter, image.get(), true); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 815 | image.reset(); |
| 816 | REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount); |
| 817 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 818 | image = create_codec_image(); |
| 819 | test_peek(reporter, image.get(), false); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 820 | } |
egdaniel | ab527a5 | 2016-06-28 08:07:26 -0700 | [diff] [blame] | 821 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 822 | sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 823 | test_peek(reporter, image.get(), false); |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 824 | } |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 825 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 826 | struct TextureReleaseChecker { |
| 827 | TextureReleaseChecker() : fReleaseCount(0) {} |
| 828 | int fReleaseCount; |
| 829 | static void Release(void* self) { |
| 830 | static_cast<TextureReleaseChecker*>(self)->fReleaseCount++; |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 831 | } |
| 832 | }; |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 833 | |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 834 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) { |
| 835 | const int kWidth = 10; |
| 836 | const int kHeight = 10; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 837 | std::unique_ptr<uint32_t[]> pixels(new uint32_t[kWidth * kHeight]); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 838 | |
| 839 | GrContext* ctx = ctxInfo.grContext(); |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 840 | GrGpu* gpu = ctx->contextPriv().getGpu(); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 841 | |
Robert Phillips | f35fd8d | 2018-01-22 10:48:15 -0500 | [diff] [blame] | 842 | GrBackendTexture backendTex = gpu->createTestingOnlyBackendTexture( |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 843 | pixels.get(), kWidth, kHeight, GrColorType::kRGBA_8888, true, GrMipMapped::kNo); |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 844 | |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 845 | TextureReleaseChecker releaseChecker; |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 846 | GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 847 | sk_sp<SkImage> refImg( |
Greg Daniel | f5d8758 | 2017-12-18 14:48:15 -0500 | [diff] [blame] | 848 | SkImage::MakeFromTexture(ctx, backendTex, texOrigin, kRGBA_8888_SkColorType, |
| 849 | kPremul_SkAlphaType, nullptr, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 850 | TextureReleaseChecker::Release, &releaseChecker)); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 851 | |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 852 | GrSurfaceOrigin readBackOrigin; |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 853 | GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin); |
Greg Daniel | 108bb23 | 2018-07-03 16:18:29 -0400 | [diff] [blame] | 854 | readBackBackendTex.setPixelConfig(kRGBA_8888_GrPixelConfig); |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 855 | if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) { |
| 856 | ERRORF(reporter, "backend mismatch\n"); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 857 | } |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 858 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)); |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 859 | if (readBackOrigin != texOrigin) { |
| 860 | ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 861 | } |
Greg Daniel | 7ef28f3 | 2017-04-20 16:41:55 +0000 | [diff] [blame] | 862 | REPORTER_ASSERT(reporter, readBackOrigin == texOrigin); |
Robert Phillips | 3390e15 | 2017-01-31 17:53:34 -0500 | [diff] [blame] | 863 | |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 864 | // Now exercise the release proc |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 865 | REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 866 | refImg.reset(nullptr); // force a release of the image |
kkinnunen | 7b94c14 | 2015-11-24 07:39:40 -0800 | [diff] [blame] | 867 | REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount); |
brianosman | db2cb10 | 2016-07-22 07:22:04 -0700 | [diff] [blame] | 868 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 869 | gpu->deleteTestingOnlyBackendTexture(backendTex); |
kkinnunen | 4e18413 | 2015-11-17 22:53:28 -0800 | [diff] [blame] | 870 | } |
bsalomon | 0d99686 | 2016-03-09 18:44:43 -0800 | [diff] [blame] | 871 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 872 | static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options, |
Hal Canary | f7d3f61 | 2018-03-22 15:17:42 -0400 | [diff] [blame] | 873 | const char* testName, |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 874 | std::function<sk_sp<SkImage>(GrContext*)> imageMaker) { |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 875 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 876 | GrContextFactory testFactory(options); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 877 | GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i); |
| 878 | ContextInfo ctxInfo = testFactory.getContextInfo(ctxType); |
| 879 | GrContext* ctx = ctxInfo.grContext(); |
| 880 | if (!ctx) { |
| 881 | continue; |
| 882 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 883 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 884 | // If we don't have proper support for this feature, the factory will fallback to returning |
| 885 | // codec-backed images. Those will "work", but some of our checks will fail because we |
| 886 | // expect the cross-context images not to work on multiple contexts at once. |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 887 | if (!ctx->contextPriv().caps()->crossContextTextureSupport()) { |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 888 | continue; |
| 889 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 890 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 891 | // We test three lifetime patterns for a single context: |
| 892 | // 1) Create image, free image |
| 893 | // 2) Create image, draw, flush, free image |
| 894 | // 3) Create image, draw, free image, flush |
| 895 | // ... and then repeat the last two patterns with drawing on a second* context: |
| 896 | // 4) Create image, draw*, flush*, free image |
| 897 | // 5) Create image, draw*, free iamge, flush* |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 898 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 899 | // Case #1: Create image, free image |
| 900 | { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 901 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 902 | refImg.reset(nullptr); // force a release of the image |
| 903 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 904 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 905 | SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128); |
| 906 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info); |
Hal Canary | f7d3f61 | 2018-03-22 15:17:42 -0400 | [diff] [blame] | 907 | if (!surface) { |
| 908 | ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName); |
| 909 | continue; |
| 910 | } |
| 911 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 912 | SkCanvas* canvas = surface->getCanvas(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 913 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 914 | // Case #2: Create image, draw, flush, free image |
| 915 | { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 916 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 917 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 918 | canvas->drawImage(refImg, 0, 0); |
| 919 | canvas->flush(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 920 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 921 | refImg.reset(nullptr); // force a release of the image |
| 922 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 923 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 924 | // Case #3: Create image, draw, free image, flush |
| 925 | { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 926 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 927 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 928 | canvas->drawImage(refImg, 0, 0); |
| 929 | refImg.reset(nullptr); // force a release of the image |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 930 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 931 | canvas->flush(); |
| 932 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 933 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 934 | // Configure second context |
| 935 | sk_gpu_test::TestContext* testContext = ctxInfo.testContext(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 936 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 937 | ContextInfo otherContextInfo = testFactory.getSharedContextInfo(ctx); |
| 938 | GrContext* otherCtx = otherContextInfo.grContext(); |
| 939 | sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 940 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 941 | // Creating a context in a share group may fail |
| 942 | if (!otherCtx) { |
| 943 | continue; |
| 944 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 945 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 946 | surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info); |
| 947 | canvas = surface->getCanvas(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 948 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 949 | // Case #4: Create image, draw*, flush*, free image |
| 950 | { |
| 951 | testContext->makeCurrent(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 952 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 953 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 954 | otherTestContext->makeCurrent(); |
| 955 | canvas->drawImage(refImg, 0, 0); |
| 956 | canvas->flush(); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 957 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 958 | testContext->makeCurrent(); |
| 959 | refImg.reset(nullptr); // force a release of the image |
| 960 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 961 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 962 | // Case #5: Create image, draw*, free image, flush* |
| 963 | { |
| 964 | testContext->makeCurrent(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 965 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 966 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 967 | otherTestContext->makeCurrent(); |
| 968 | canvas->drawImage(refImg, 0, 0); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 969 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 970 | testContext->makeCurrent(); |
| 971 | refImg.reset(nullptr); // force a release of the image |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 972 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 973 | otherTestContext->makeCurrent(); |
| 974 | canvas->flush(); |
Greg Daniel | 3f475d9 | 2017-07-25 16:26:35 -0400 | [diff] [blame] | 975 | |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 976 | // This is specifically here for vulkan to guarantee the command buffer will finish |
| 977 | // which is when we call the ReleaseProc. |
| 978 | otherCtx->contextPriv().getGpu()->testingOnly_flushGpuAndSync(); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 979 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 980 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 981 | // Case #6: Verify that only one context can be using the image at a time |
| 982 | { |
| 983 | testContext->makeCurrent(); |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 984 | sk_sp<SkImage> refImg(imageMaker(ctx)); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 985 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 986 | // Any context should be able to borrow the texture at this point |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 987 | sk_sp<GrTextureProxy> proxy = as_IB(refImg)->asTextureProxyRef( |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 988 | ctx, GrSamplerState::ClampNearest(), nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 989 | REPORTER_ASSERT(reporter, proxy); |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 990 | |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 991 | // But once it's borrowed, no other context should be able to borrow |
| 992 | otherTestContext->makeCurrent(); |
| 993 | sk_sp<GrTextureProxy> otherProxy = as_IB(refImg)->asTextureProxyRef( |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 994 | otherCtx, GrSamplerState::ClampNearest(), nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 995 | REPORTER_ASSERT(reporter, !otherProxy); |
| 996 | |
| 997 | // Original context (that's already borrowing) should be okay |
| 998 | testContext->makeCurrent(); |
| 999 | sk_sp<GrTextureProxy> proxySecondRef = as_IB(refImg)->asTextureProxyRef( |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 1000 | ctx, GrSamplerState::ClampNearest(), nullptr); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 1001 | REPORTER_ASSERT(reporter, proxySecondRef); |
| 1002 | |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 1003 | // Release first ref from the original context |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 1004 | proxy.reset(nullptr); |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 1005 | |
| 1006 | // We released one proxy but not the other from the current borrowing context. Make sure |
| 1007 | // a new context is still not able to borrow the texture. |
| 1008 | otherTestContext->makeCurrent(); |
| 1009 | otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(), |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 1010 | nullptr); |
Greg Daniel | abba998 | 2018-02-01 10:07:57 -0500 | [diff] [blame] | 1011 | REPORTER_ASSERT(reporter, !otherProxy); |
| 1012 | |
| 1013 | // Release second ref from the original context |
| 1014 | testContext->makeCurrent(); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 1015 | proxySecondRef.reset(nullptr); |
| 1016 | |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 1017 | // Now we should be able to borrow the texture from the other context |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 1018 | otherTestContext->makeCurrent(); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 1019 | otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(), |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 1020 | nullptr); |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 1021 | REPORTER_ASSERT(reporter, otherProxy); |
Brian Osman | ceb7a42 | 2017-06-21 15:10:33 -0400 | [diff] [blame] | 1022 | |
| 1023 | // Release everything |
| 1024 | otherProxy.reset(nullptr); |
| 1025 | refImg.reset(nullptr); |
| 1026 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 1030 | DEF_GPUTEST(SkImage_MakeCrossContextFromEncodedRelease, reporter, options) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 1031 | sk_sp<SkData> data = GetResourceAsData("images/mandrill_128.png"); |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 1032 | if (!data) { |
| 1033 | ERRORF(reporter, "missing resource"); |
| 1034 | return; |
| 1035 | } |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 1036 | |
Hal Canary | f7d3f61 | 2018-03-22 15:17:42 -0400 | [diff] [blame] | 1037 | test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromEncodedRelease", |
| 1038 | [&data](GrContext* ctx) { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 1039 | return SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr); |
| 1040 | }); |
| 1041 | } |
| 1042 | |
Brian Salomon | dcfca43 | 2017-11-15 15:48:03 -0500 | [diff] [blame] | 1043 | DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 1044 | SkBitmap bitmap; |
| 1045 | SkPixmap pixmap; |
Hal Canary | baa2a28 | 2018-11-26 15:34:12 -0500 | [diff] [blame] | 1046 | if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) { |
| 1047 | ERRORF(reporter, "missing resource"); |
| 1048 | return; |
| 1049 | } |
Hal Canary | f7d3f61 | 2018-03-22 15:17:42 -0400 | [diff] [blame] | 1050 | test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease", |
| 1051 | [&pixmap](GrContext* ctx) { |
Brian Osman | 63bc48d | 2017-11-07 10:37:00 -0500 | [diff] [blame] | 1052 | return SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr); |
| 1053 | }); |
| 1054 | } |
| 1055 | |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 1056 | DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) { |
| 1057 | |
| 1058 | for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) { |
| 1059 | SkAutoPixmapStorage pixmap; |
| 1060 | pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType)); |
| 1061 | |
| 1062 | for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) { |
| 1063 | GrContextFactory testFactory(options); |
| 1064 | GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i); |
| 1065 | ContextInfo ctxInfo = testFactory.getContextInfo(ctxType); |
| 1066 | GrContext* ctx = ctxInfo.grContext(); |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1067 | if (!ctx || !ctx->contextPriv().caps()->crossContextTextureSupport()) { |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 1068 | continue; |
| 1069 | } |
| 1070 | |
| 1071 | sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr); |
| 1072 | REPORTER_ASSERT(reporter, image); |
| 1073 | |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 1074 | sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef( |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 1075 | ctx, GrSamplerState::ClampNearest(), nullptr); |
Brian Osman | 052ef69 | 2018-03-27 09:56:31 -0400 | [diff] [blame] | 1076 | REPORTER_ASSERT(reporter, proxy); |
| 1077 | |
| 1078 | bool expectAlpha = kAlpha_8_SkColorType == ct; |
| 1079 | REPORTER_ASSERT(reporter, expectAlpha == GrPixelConfigIsAlphaOnly(proxy->config())); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1084 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) { |
| 1085 | GrContext* context = ctxInfo.grContext(); |
| 1086 | sk_gpu_test::TestContext* testContext = ctxInfo.testContext(); |
| 1087 | sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy(); |
| 1088 | |
| 1089 | GrContextFactory otherFactory; |
| 1090 | ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type()); |
| 1091 | |
| 1092 | testContext->makeCurrent(); |
| 1093 | REPORTER_ASSERT(reporter, proxy); |
| 1094 | auto createLarge = [context] { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 1095 | return create_image_large(context->contextPriv().caps()->maxTextureSize()); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1096 | }; |
| 1097 | struct { |
| 1098 | std::function<sk_sp<SkImage> ()> fImageFactory; |
| 1099 | bool fExpectation; |
| 1100 | bool fCanTakeDirectly; |
| 1101 | } testCases[] = { |
| 1102 | { create_image, true, false }, |
| 1103 | { create_codec_image, true, false }, |
| 1104 | { create_data_image, true, false }, |
| 1105 | { create_picture_image, true, false }, |
| 1106 | { [context] { return create_gpu_image(context); }, true, true }, |
| 1107 | // Create a texture image in a another GrContext. |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 1108 | { [otherContextInfo] { |
| 1109 | auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore(); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1110 | sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext()); |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 1111 | otherContextInfo.grContext()->flush(); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1112 | return otherContextImage; |
| 1113 | }, false, false }, |
| 1114 | // Create an image that is too large to be texture backed. |
| 1115 | { createLarge, false, false } |
| 1116 | }; |
| 1117 | |
| 1118 | for (auto testCase : testCases) { |
| 1119 | sk_sp<SkImage> image(testCase.fImageFactory()); |
| 1120 | if (!image) { |
| 1121 | ERRORF(reporter, "Failed to create image!"); |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 1125 | GrBackendTexture origBackend = image->getBackendTexture(true); |
| 1126 | if (testCase.fCanTakeDirectly) { |
| 1127 | SkASSERT(origBackend.isValid()); |
| 1128 | } |
| 1129 | |
| 1130 | GrBackendTexture newBackend; |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1131 | SkImage::BackendTextureReleaseProc proc; |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 1132 | bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image), |
| 1133 | &newBackend, &proc); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1134 | if (result != testCase.fExpectation) { |
| 1135 | static const char *const kFS[] = { "fail", "succeed" }; |
| 1136 | ERRORF(reporter, "This image was expected to %s but did not.", |
| 1137 | kFS[testCase.fExpectation]); |
| 1138 | } |
| 1139 | |
Robert Phillips | ba375a8 | 2018-04-11 10:08:06 -0400 | [diff] [blame] | 1140 | if (result) { |
| 1141 | SkASSERT(newBackend.isValid()); |
| 1142 | } |
| 1143 | |
| 1144 | bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend); |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1145 | if (testCase.fCanTakeDirectly != tookDirectly) { |
| 1146 | static const char *const kExpectedState[] = { "not expected", "expected" }; |
| 1147 | ERRORF(reporter, "This backend texture was %s to be taken directly.", |
| 1148 | kExpectedState[testCase.fCanTakeDirectly]); |
| 1149 | } |
| 1150 | |
Eric Karl | 914a36b | 2017-10-12 12:44:50 -0700 | [diff] [blame] | 1151 | context->flush(); |
| 1152 | } |
| 1153 | } |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1154 | |
| 1155 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1156 | |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1157 | static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) { |
| 1158 | SkPictureRecorder recorder; |
| 1159 | SkCanvas* canvas = recorder.beginRecording(10, 10); |
| 1160 | canvas->clear(SK_ColorCYAN); |
| 1161 | return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10), |
| 1162 | nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space)); |
| 1163 | }; |
| 1164 | |
| 1165 | DEF_TEST(Image_ColorSpace, r) { |
| 1166 | sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB(); |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 1167 | sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg"); |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1168 | REPORTER_ASSERT(r, srgb.get() == image->colorSpace()); |
| 1169 | |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 1170 | image = GetResourceAsImage("images/webp-color-profile-lossy.webp"); |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 1171 | skcms_TransferFunction fn; |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1172 | bool success = image->colorSpace()->isNumericalTransferFn(&fn); |
| 1173 | REPORTER_ASSERT(r, success); |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 1174 | REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g)); |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1175 | |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 1176 | sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, |
| 1177 | SkNamedGamut::kRec2020); |
Matt Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1178 | image = create_picture_image(rec2020); |
| 1179 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1180 | |
| 1181 | SkBitmap bitmap; |
| 1182 | SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020); |
| 1183 | bitmap.allocPixels(info); |
| 1184 | image = SkImage::MakeFromBitmap(bitmap); |
| 1185 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1186 | |
| 1187 | sk_sp<SkSurface> surface = SkSurface::MakeRaster( |
| 1188 | SkImageInfo::MakeN32Premul(SkISize::Make(10, 10))); |
| 1189 | image = surface->makeImageSnapshot(); |
| 1190 | REPORTER_ASSERT(r, nullptr == image->colorSpace()); |
| 1191 | |
| 1192 | surface = SkSurface::MakeRaster(info); |
| 1193 | image = surface->makeImageSnapshot(); |
| 1194 | REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace())); |
| 1195 | } |
| 1196 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1197 | DEF_TEST(Image_makeColorSpace, r) { |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 1198 | sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3); |
| 1199 | skcms_TransferFunction fn; |
| 1200 | fn.a = 1.f; fn.b = 0.f; fn.c = 0.f; fn.d = 0.f; fn.e = 0.f; fn.f = 0.f; fn.g = 1.8f; |
| 1201 | sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1202 | |
| 1203 | SkBitmap srgbBitmap; |
| 1204 | srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType)); |
| 1205 | *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020); |
| 1206 | srgbBitmap.setImmutable(); |
| 1207 | sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap); |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 1208 | sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1209 | SkBitmap p3Bitmap; |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 1210 | bool success = p3Image->asLegacyBitmap(&p3Bitmap); |
Mike Klein | 55d330c | 2018-04-23 15:39:21 -0400 | [diff] [blame] | 1211 | |
| 1212 | auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; }; |
| 1213 | |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1214 | REPORTER_ASSERT(r, success); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1215 | REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0)))); |
| 1216 | REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0)))); |
| 1217 | REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0)))); |
| 1218 | |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 1219 | sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1220 | SkBitmap adobeBitmap; |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 1221 | success = adobeImage->asLegacyBitmap(&adobeBitmap); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1222 | REPORTER_ASSERT(r, success); |
Matt Sarett | 5b1dba8 | 2017-04-03 11:42:52 -0400 | [diff] [blame] | 1223 | 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 Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 1227 | srgbImage = GetResourceAsImage("images/1x1.png"); |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 1228 | p3Image = srgbImage->makeColorSpace(p3); |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 1229 | success = p3Image->asLegacyBitmap(&p3Bitmap); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1230 | REPORTER_ASSERT(r, success); |
Matt Sarett | 6de1310 | 2017-03-14 14:10:48 -0400 | [diff] [blame] | 1231 | 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 Sarett | 0bb6f38 | 2017-03-06 10:28:24 -0500 | [diff] [blame] | 1236 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1237 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1238 | static 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 | |
| 1249 | static 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 Reed | 6f0286f | 2016-11-28 17:26:27 -0500 | [diff] [blame] | 1253 | 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 | } |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | DEF_TEST(image_roundtrip_encode, reporter) { |
| 1265 | SkBitmap bm0; |
| 1266 | make_all_premul(&bm0); |
| 1267 | |
| 1268 | auto img0 = SkImage::MakeFromBitmap(bm0); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 1269 | sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100); |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1270 | auto img1 = SkImage::MakeFromEncoded(data); |
mtklein | 2f3416d | 2016-08-02 16:02:05 -0700 | [diff] [blame] | 1271 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1272 | SkBitmap bm1; |
| 1273 | bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType)); |
| 1274 | img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0); |
mtklein | 2f3416d | 2016-08-02 16:02:05 -0700 | [diff] [blame] | 1275 | |
reed | eb56028 | 2016-07-26 19:42:04 -0700 | [diff] [blame] | 1276 | REPORTER_ASSERT(reporter, equal(bm0, bm1)); |
| 1277 | } |
| 1278 | |
| 1279 | DEF_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 Osman | b8a1392 | 2017-04-26 15:28:30 -0400 | [diff] [blame] | 1293 | |
| 1294 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1295 | |
| 1296 | static 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 | |
| 1310 | static 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 | |
| 1327 | DEF_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 Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 1339 | sk_sp<SkData> data = rasterImage->encodeToData(); |
Brian Osman | b8a1392 | 2017-04-26 15:28:30 -0400 | [diff] [blame] | 1340 | sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data); |
| 1341 | test_scale_pixels(reporter, codecImage.get(), pmRed); |
| 1342 | } |
| 1343 | |
Brian Osman | b8a1392 | 2017-04-26 15:28:30 -0400 | [diff] [blame] | 1344 | DEF_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 Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 1355 | |
| 1356 | static sk_sp<SkImage> any_image_will_do() { |
| 1357 | return GetResourceAsImage("images/mandrill_32.png"); |
| 1358 | } |
| 1359 | |
| 1360 | DEF_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 | |