robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkPixmap.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "src/core/SkAutoPixmapStorage.h" |
| 14 | #include "src/core/SkSpecialImage.h" |
| 15 | #include "src/core/SkSpecialSurface.h" |
| 16 | #include "tests/Test.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 17 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/gpu/GrBackendSurface.h" |
| 19 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrContextPriv.h" |
| 21 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrSurfaceProxy.h" |
| 23 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/SkGr.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 25 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 26 | |
| 27 | // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 28 | // It then wraps them in an SkSpecialImage with only the center (red) region being active. |
| 29 | // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none |
| 30 | // of the inactive (green) region leaked out. |
| 31 | |
| 32 | static const int kSmallerSize = 10; |
| 33 | static const int kPad = 3; |
| 34 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 35 | |
| 36 | // Create a bitmap with red in the center and green around it |
| 37 | static SkBitmap create_bm() { |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 38 | SkImageInfo ii = SkImageInfo::Make(kFullSize, kFullSize, kRGBA_8888_SkColorType, |
| 39 | kPremul_SkAlphaType); |
| 40 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 41 | SkBitmap bm; |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 42 | bm.allocPixels(ii); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 43 | |
| 44 | SkCanvas temp(bm); |
| 45 | |
| 46 | temp.clear(SK_ColorGREEN); |
| 47 | SkPaint p; |
| 48 | p.setColor(SK_ColorRED); |
| 49 | p.setAntiAlias(false); |
| 50 | |
| 51 | temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 52 | SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 53 | p); |
| 54 | |
| 55 | return bm; |
| 56 | } |
| 57 | |
| 58 | // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw) |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 59 | static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter, |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 60 | GrContext* context, bool isGPUBacked) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 61 | const SkIRect subset = img->subset(); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 62 | REPORTER_ASSERT(reporter, kPad == subset.left()); |
| 63 | REPORTER_ASSERT(reporter, kPad == subset.top()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 64 | REPORTER_ASSERT(reporter, kSmallerSize == subset.width()); |
| 65 | REPORTER_ASSERT(reporter, kSmallerSize == subset.height()); |
| 66 | |
| 67 | //-------------- |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 68 | // Test that isTextureBacked reports the correct backing type |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 69 | REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked()); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 70 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 71 | //-------------- |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 72 | // Test asTextureProxyRef - as long as there is a context this should succeed |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 73 | if (context) { |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 74 | sk_sp<GrTextureProxy> proxy(img->asTextureProxyRef(context)); |
| 75 | REPORTER_ASSERT(reporter, proxy); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 76 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 77 | |
| 78 | //-------------- |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 79 | // Test getROPixels - this should always succeed regardless of backing store |
| 80 | SkBitmap bitmap; |
| 81 | REPORTER_ASSERT(reporter, img->getROPixels(&bitmap)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 82 | REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width()); |
| 83 | REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 84 | |
| 85 | //-------------- |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 86 | // Test that draw restricts itself to the subset |
Michael Ludwig | 03f9ca3 | 2019-08-14 14:35:15 -0400 | [diff] [blame] | 87 | sk_sp<SkSpecialSurface> surf(img->makeSurface(kN32_SkColorType, img->getColorSpace(), |
| 88 | SkISize::Make(kFullSize, kFullSize), |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 89 | kPremul_SkAlphaType)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 90 | |
| 91 | SkCanvas* canvas = surf->getCanvas(); |
| 92 | |
| 93 | canvas->clear(SK_ColorBLUE); |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 94 | img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 95 | |
| 96 | SkBitmap bm; |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 97 | bm.allocN32Pixels(kFullSize, kFullSize, false); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 98 | |
| 99 | bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
| 100 | SkASSERT_RELEASE(result); |
| 101 | |
| 102 | // Only the center (red) portion should've been drawn into the canvas |
| 103 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
| 104 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 105 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 106 | kSmallerSize+kPad-1)); |
| 107 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 108 | kSmallerSize+kPad)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 109 | |
| 110 | //-------------- |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 111 | // Test that asImage & makeTightSurface return appropriately sized objects |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 112 | // of the correct backing type |
| 113 | SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); |
| 114 | { |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 115 | sk_sp<SkImage> tightImg(img->asImage(&newSubset)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 116 | |
| 117 | REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); |
| 118 | REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 119 | REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked()); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 120 | SkPixmap tmpPixmap; |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 122 | } |
| 123 | { |
Michael Ludwig | 03f9ca3 | 2019-08-14 14:35:15 -0400 | [diff] [blame] | 124 | sk_sp<SkSurface> tightSurf(img->makeTightSurface(kN32_SkColorType, img->getColorSpace(), |
| 125 | subset.size())); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 126 | |
| 127 | REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); |
| 128 | REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 129 | GrBackendTexture backendTex = tightSurf->getBackendTexture( |
| 130 | SkSurface::kDiscardWrite_BackendHandleAccess); |
| 131 | REPORTER_ASSERT(reporter, isGPUBacked == backendTex.isValid()); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 132 | SkPixmap tmpPixmap; |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 134 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | DEF_TEST(SpecialImage_Raster, reporter) { |
| 138 | SkBitmap bm = create_bm(); |
| 139 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 140 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 141 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 142 | bm)); |
| 143 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 144 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 145 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 146 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 147 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 148 | test_image(subSImg1, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 152 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 153 | test_image(subSImg2, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 154 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 157 | static void test_specialimage_image(skiatest::Reporter* reporter) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 158 | SkBitmap bm = create_bm(); |
| 159 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 160 | sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 161 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 162 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 163 | nullptr, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 164 | SkIRect::MakeWH(kFullSize, kFullSize), |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 165 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 166 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 167 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 168 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 169 | { |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 170 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset, fullImage)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 171 | test_image(subSImg1, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 175 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 176 | test_image(subSImg2, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 177 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 180 | DEF_TEST(SpecialImage_Image_Legacy, reporter) { |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 181 | test_specialimage_image(reporter); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 182 | } |
| 183 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 184 | static void test_texture_backed(skiatest::Reporter* reporter, |
| 185 | const sk_sp<SkSpecialImage>& orig, |
| 186 | const sk_sp<SkSpecialImage>& gpuBacked) { |
| 187 | REPORTER_ASSERT(reporter, gpuBacked); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 188 | REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); |
| 190 | REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && |
| 191 | gpuBacked->subset().height() == orig->subset().height()); |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 192 | REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Test out the SkSpecialImage::makeTextureImage entry point |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 196 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 197 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 198 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 199 | SkBitmap bm = create_bm(); |
| 200 | |
| 201 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 202 | |
| 203 | { |
| 204 | // raster |
| 205 | sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 206 | SkIRect::MakeWH(kFullSize, |
| 207 | kFullSize), |
| 208 | bm)); |
| 209 | |
| 210 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 211 | sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 212 | test_texture_backed(reporter, rasterImage, fromRaster); |
| 213 | } |
| 214 | |
| 215 | { |
| 216 | sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)); |
| 217 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 218 | sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 219 | test_texture_backed(reporter, subRasterImage, fromSubRaster); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | // gpu |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 225 | sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm); |
Brian Salomon | f2c2ba9 | 2019-07-17 09:59:59 -0400 | [diff] [blame] | 226 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy( |
Brian Salomon | 96b383a | 2019-08-13 16:55:41 -0400 | [diff] [blame] | 227 | rasterImage, 1, SkBudgeted::kNo, SkBackingFit::kExact); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 228 | if (!proxy) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 229 | return; |
| 230 | } |
| 231 | |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 232 | sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeDeferredFromGpu( |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 233 | context, |
| 234 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 235 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 236 | std::move(proxy), |
| 237 | GrColorType::kRGBA_8888, |
| 238 | nullptr)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 239 | |
| 240 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 241 | sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 242 | test_texture_backed(reporter, gpuImage, fromGPU); |
| 243 | } |
| 244 | |
| 245 | { |
| 246 | sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); |
| 247 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 248 | sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 249 | test_texture_backed(reporter, subGPUImage, fromSubGPU); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 254 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 255 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 256 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 257 | SkBitmap bm = create_bm(); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 258 | sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 259 | |
Brian Salomon | 96b383a | 2019-08-13 16:55:41 -0400 | [diff] [blame] | 260 | sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(rasterImage, 1, SkBudgeted::kNo, |
| 261 | SkBackingFit::kExact); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 262 | if (!proxy) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 266 | sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu( |
| 267 | context, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 268 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 269 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 270 | proxy, |
| 271 | GrColorType::kRGBA_8888, |
| 272 | nullptr)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 273 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 274 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 275 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 276 | { |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 277 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu( |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 278 | context, subset, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 279 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 280 | std::move(proxy), |
| 281 | GrColorType::kRGBA_8888, |
| 282 | nullptr)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 283 | test_image(subSImg1, reporter, context, true); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 287 | sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 288 | test_image(subSImg2, reporter, context, true); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 289 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 290 | } |
Brian Osman | ba65168 | 2018-10-05 11:13:04 -0400 | [diff] [blame] | 291 | |
| 292 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_ReadbackAndCachingSubsets_Gpu, reporter, ctxInfo) { |
| 293 | GrContext* context = ctxInfo.grContext(); |
| 294 | SkImageInfo ii = SkImageInfo::Make(50, 50, kN32_SkColorType, kPremul_SkAlphaType); |
| 295 | auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii); |
| 296 | |
| 297 | // Fill out our surface: |
| 298 | // Green | Blue |
| 299 | // Red | Green |
| 300 | { |
| 301 | surface->getCanvas()->clear(SK_ColorGREEN); |
| 302 | SkPaint p; |
| 303 | p.setColor(SK_ColorRED); |
| 304 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(0, 25, 25, 25), p); |
| 305 | p.setColor(SK_ColorBLUE); |
| 306 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 0, 25, 25), p); |
| 307 | } |
| 308 | |
| 309 | auto image = surface->makeImageSnapshot(); |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 310 | auto redImg = SkSpecialImage::MakeFromImage(context, SkIRect::MakeXYWH(10, 30, 10, 10), image); |
| 311 | auto blueImg = SkSpecialImage::MakeFromImage(context, SkIRect::MakeXYWH(30, 10, 10, 10), image); |
Brian Osman | ba65168 | 2018-10-05 11:13:04 -0400 | [diff] [blame] | 312 | |
| 313 | // This isn't necessary, but if it ever becomes false, then the cache collision bug that we're |
| 314 | // checking below is irrelevant. |
| 315 | REPORTER_ASSERT(reporter, redImg->uniqueID() == blueImg->uniqueID()); |
| 316 | |
| 317 | SkBitmap redBM, blueBM; |
| 318 | SkAssertResult(redImg->getROPixels(&redBM)); |
| 319 | SkAssertResult(blueImg->getROPixels(&blueBM)); |
| 320 | |
| 321 | // Each image should read from the correct sub-rect. Past bugs (skbug.com/8448) have included: |
| 322 | // - Always reading back from (0, 0), producing green |
| 323 | // - Incorrectly hitting the cache on the 2nd read-back, causing blueBM to be red |
| 324 | REPORTER_ASSERT(reporter, redBM.getColor(0, 0) == SK_ColorRED, |
| 325 | "0x%08x != 0x%08x", redBM.getColor(0, 0), SK_ColorRED); |
| 326 | REPORTER_ASSERT(reporter, blueBM.getColor(0, 0) == SK_ColorBLUE, |
| 327 | "0x%08x != 0x%08x", blueBM.getColor(0, 0), SK_ColorBLUE); |
| 328 | } |