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