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