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( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 166 | SkIRect::MakeWH(kFullSize, kFullSize), |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 167 | fullImage)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 168 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 169 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 170 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 171 | { |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 172 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 173 | test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 177 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 178 | test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 179 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 182 | DEF_TEST(SpecialImage_Image_Legacy, reporter) { |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 183 | test_specialimage_image(reporter); |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 184 | } |
| 185 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 186 | static void test_texture_backed(skiatest::Reporter* reporter, |
| 187 | const sk_sp<SkSpecialImage>& orig, |
| 188 | const sk_sp<SkSpecialImage>& gpuBacked) { |
| 189 | REPORTER_ASSERT(reporter, gpuBacked); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID()); |
| 192 | REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() && |
| 193 | gpuBacked->subset().height() == orig->subset().height()); |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Test out the SkSpecialImage::makeTextureImage entry point |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 198 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 199 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 200 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 201 | SkBitmap bm = create_bm(); |
| 202 | |
| 203 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 204 | |
| 205 | { |
| 206 | // raster |
| 207 | sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster( |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 208 | SkIRect::MakeWH(kFullSize, |
| 209 | kFullSize), |
| 210 | bm)); |
| 211 | |
| 212 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 213 | sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 214 | test_texture_backed(reporter, rasterImage, fromRaster); |
| 215 | } |
| 216 | |
| 217 | { |
| 218 | sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset)); |
| 219 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 220 | sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 221 | test_texture_backed(reporter, subRasterImage, fromSubRaster); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | { |
| 226 | // gpu |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 227 | sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm); |
| 228 | sk_sp<GrTextureProxy> proxy = |
| 229 | proxyProvider->createTextureProxy(rasterImage, kNone_GrSurfaceFlags, 1, |
| 230 | SkBudgeted::kNo, SkBackingFit::kExact); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 231 | if (!proxy) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 235 | sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeDeferredFromGpu( |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 236 | context, |
| 237 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 238 | kNeedNewImageUniqueID_SpecialImage, |
| 239 | std::move(proxy), nullptr)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 240 | |
| 241 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 242 | sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 243 | test_texture_backed(reporter, gpuImage, fromGPU); |
| 244 | } |
| 245 | |
| 246 | { |
| 247 | sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset)); |
| 248 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 249 | sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 250 | test_texture_backed(reporter, subGPUImage, fromSubGPU); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 255 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 256 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 0bd24dc | 2018-01-16 08:06:32 -0500 | [diff] [blame] | 257 | GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 258 | SkBitmap bm = create_bm(); |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 259 | sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 260 | |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 261 | sk_sp<GrTextureProxy> proxy = |
Brian Osman | 2700abc | 2018-09-12 10:19:41 -0400 | [diff] [blame] | 262 | proxyProvider->createTextureProxy(rasterImage, kNone_GrSurfaceFlags, 1, |
| 263 | SkBudgeted::kNo, SkBackingFit::kExact); |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 264 | if (!proxy) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 268 | sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu( |
| 269 | context, |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 270 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 271 | kNeedNewImageUniqueID_SpecialImage, |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 272 | proxy, 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 | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 280 | std::move(proxy), nullptr)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 281 | test_image(subSImg1, reporter, context, true, kPad, kFullSize); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 285 | sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 286 | test_image(subSImg2, reporter, context, true, kPad, kFullSize); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 287 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 288 | } |
Brian Osman | ba65168 | 2018-10-05 11:13:04 -0400 | [diff] [blame] | 289 | |
| 290 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_ReadbackAndCachingSubsets_Gpu, reporter, ctxInfo) { |
| 291 | GrContext* context = ctxInfo.grContext(); |
| 292 | SkImageInfo ii = SkImageInfo::Make(50, 50, kN32_SkColorType, kPremul_SkAlphaType); |
| 293 | auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii); |
| 294 | |
| 295 | // Fill out our surface: |
| 296 | // Green | Blue |
| 297 | // Red | Green |
| 298 | { |
| 299 | surface->getCanvas()->clear(SK_ColorGREEN); |
| 300 | SkPaint p; |
| 301 | p.setColor(SK_ColorRED); |
| 302 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(0, 25, 25, 25), p); |
| 303 | p.setColor(SK_ColorBLUE); |
| 304 | surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 0, 25, 25), p); |
| 305 | } |
| 306 | |
| 307 | auto image = surface->makeImageSnapshot(); |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 308 | auto redImg = SkSpecialImage::MakeFromImage(SkIRect::MakeXYWH(10, 30, 10, 10), image); |
| 309 | auto blueImg = SkSpecialImage::MakeFromImage(SkIRect::MakeXYWH(30, 10, 10, 10), image); |
Brian Osman | ba65168 | 2018-10-05 11:13:04 -0400 | [diff] [blame] | 310 | |
| 311 | // This isn't necessary, but if it ever becomes false, then the cache collision bug that we're |
| 312 | // checking below is irrelevant. |
| 313 | REPORTER_ASSERT(reporter, redImg->uniqueID() == blueImg->uniqueID()); |
| 314 | |
| 315 | SkBitmap redBM, blueBM; |
| 316 | SkAssertResult(redImg->getROPixels(&redBM)); |
| 317 | SkAssertResult(blueImg->getROPixels(&blueBM)); |
| 318 | |
| 319 | // Each image should read from the correct sub-rect. Past bugs (skbug.com/8448) have included: |
| 320 | // - Always reading back from (0, 0), producing green |
| 321 | // - Incorrectly hitting the cache on the 2nd read-back, causing blueBM to be red |
| 322 | REPORTER_ASSERT(reporter, redBM.getColor(0, 0) == SK_ColorRED, |
| 323 | "0x%08x != 0x%08x", redBM.getColor(0, 0), SK_ColorRED); |
| 324 | REPORTER_ASSERT(reporter, blueBM.getColor(0, 0) == SK_ColorBLUE, |
| 325 | "0x%08x != 0x%08x", blueBM.getColor(0, 0), SK_ColorBLUE); |
| 326 | } |