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" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrBackendSurface.h" |
| 14 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/core/SkAutoPixmapStorage.h" |
| 16 | #include "src/core/SkSpecialImage.h" |
| 17 | #include "src/core/SkSpecialSurface.h" |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrBitmapTextureMaker.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrContextPriv.h" |
| 20 | #include "src/gpu/GrProxyProvider.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrSurfaceProxy.h" |
| 22 | #include "src/gpu/GrTextureProxy.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/SkGr.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 24 | #include "tests/Test.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 25 | |
| 26 | // This test creates backing resources exactly sized to [kFullSize x kFullSize]. |
| 27 | // It then wraps them in an SkSpecialImage with only the center (red) region being active. |
| 28 | // It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none |
| 29 | // of the inactive (green) region leaked out. |
| 30 | |
| 31 | static const int kSmallerSize = 10; |
| 32 | static const int kPad = 3; |
| 33 | static const int kFullSize = kSmallerSize + 2 * kPad; |
| 34 | |
| 35 | // Create a bitmap with red in the center and green around it |
| 36 | static SkBitmap create_bm() { |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 37 | SkImageInfo ii = SkImageInfo::Make(kFullSize, kFullSize, kRGBA_8888_SkColorType, |
| 38 | kPremul_SkAlphaType); |
| 39 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 40 | SkBitmap bm; |
Robert Phillips | 40b05c3 | 2019-09-20 12:40:55 -0400 | [diff] [blame] | 41 | bm.allocPixels(ii); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 42 | |
| 43 | SkCanvas temp(bm); |
| 44 | |
| 45 | temp.clear(SK_ColorGREEN); |
| 46 | SkPaint p; |
| 47 | p.setColor(SK_ColorRED); |
| 48 | p.setAntiAlias(false); |
| 49 | |
| 50 | temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 51 | SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)), |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 52 | p); |
| 53 | |
Greg Daniel | 6f5441a | 2020-01-28 17:02:49 -0500 | [diff] [blame] | 54 | bm.setImmutable(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 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 | //-------------- |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 72 | // Test view - as long as there is a context this should succeed |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 73 | if (context) { |
Greg Daniel | 37c127f | 2020-02-05 10:37:27 -0500 | [diff] [blame] | 74 | GrSurfaceProxyView view = img->view(context); |
Greg Daniel | 8354717 | 2020-01-29 11:11:03 -0500 | [diff] [blame] | 75 | REPORTER_ASSERT(reporter, view.asTextureProxy()); |
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 | //-------------- |
Brian Osman | e7ad8c0 | 2020-06-22 14:51:22 -0400 | [diff] [blame] | 79 | // Test getROPixels - this only works for raster-backed special images |
| 80 | if (!img->isTextureBacked()) { |
| 81 | SkBitmap bitmap; |
| 82 | REPORTER_ASSERT(reporter, img->getROPixels(&bitmap)); |
| 83 | REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width()); |
| 84 | REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height()); |
| 85 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 86 | |
| 87 | //-------------- |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 88 | // Test that draw restricts itself to the subset |
Michael Ludwig | 03f9ca3 | 2019-08-14 14:35:15 -0400 | [diff] [blame] | 89 | sk_sp<SkSpecialSurface> surf(img->makeSurface(kN32_SkColorType, img->getColorSpace(), |
| 90 | SkISize::Make(kFullSize, kFullSize), |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 91 | kPremul_SkAlphaType)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 92 | |
| 93 | SkCanvas* canvas = surf->getCanvas(); |
| 94 | |
| 95 | canvas->clear(SK_ColorBLUE); |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 96 | img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 97 | |
| 98 | SkBitmap bm; |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 99 | bm.allocN32Pixels(kFullSize, kFullSize, false); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 100 | |
| 101 | bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0); |
| 102 | SkASSERT_RELEASE(result); |
| 103 | |
| 104 | // Only the center (red) portion should've been drawn into the canvas |
| 105 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1)); |
| 106 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad)); |
| 107 | REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1, |
| 108 | kSmallerSize+kPad-1)); |
| 109 | REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad, |
| 110 | kSmallerSize+kPad)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 111 | |
| 112 | //-------------- |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 113 | // Test that asImage & makeTightSurface return appropriately sized objects |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 114 | // of the correct backing type |
| 115 | SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height()); |
| 116 | { |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 117 | sk_sp<SkImage> tightImg(img->asImage(&newSubset)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 118 | |
| 119 | REPORTER_ASSERT(reporter, tightImg->width() == subset.width()); |
| 120 | REPORTER_ASSERT(reporter, tightImg->height() == subset.height()); |
Robert Phillips | 0ae6faa | 2017-03-21 16:22:00 -0400 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked()); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 122 | SkPixmap tmpPixmap; |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 123 | REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 124 | } |
| 125 | { |
Michael Ludwig | 03f9ca3 | 2019-08-14 14:35:15 -0400 | [diff] [blame] | 126 | sk_sp<SkSurface> tightSurf(img->makeTightSurface(kN32_SkColorType, img->getColorSpace(), |
| 127 | subset.size())); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 128 | |
| 129 | REPORTER_ASSERT(reporter, tightSurf->width() == subset.width()); |
| 130 | REPORTER_ASSERT(reporter, tightSurf->height() == subset.height()); |
Robert Phillips | 8caf85f | 2018-04-05 09:30:38 -0400 | [diff] [blame] | 131 | GrBackendTexture backendTex = tightSurf->getBackendTexture( |
| 132 | SkSurface::kDiscardWrite_BackendHandleAccess); |
| 133 | REPORTER_ASSERT(reporter, isGPUBacked == backendTex.isValid()); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 134 | SkPixmap tmpPixmap; |
Robert Phillips | 6de9904 | 2017-01-31 11:31:39 -0500 | [diff] [blame] | 135 | REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap)); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 136 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | DEF_TEST(SpecialImage_Raster, reporter) { |
| 140 | SkBitmap bm = create_bm(); |
| 141 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 142 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster( |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 143 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 144 | bm)); |
| 145 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 146 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 147 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 148 | { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 149 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 150 | test_image(subSImg1, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 154 | sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 155 | test_image(subSImg2, reporter, nullptr, false); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 156 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Brian Osman | b9c4978 | 2018-10-12 12:01:22 -0400 | [diff] [blame] | 159 | static void test_specialimage_image(skiatest::Reporter* reporter) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 160 | SkBitmap bm = create_bm(); |
| 161 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 162 | sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm)); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 163 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 164 | sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage( |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 165 | nullptr, |
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 | { |
Robert Phillips | 2746765 | 2019-01-10 16:34:22 -0500 | [diff] [blame] | 172 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset, fullImage)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 173 | test_image(subSImg1, reporter, nullptr, false); |
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)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 178 | test_image(subSImg2, reporter, nullptr, false); |
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 | |
bsalomon | 68d9134 | 2016-04-12 09:59:58 -0700 | [diff] [blame] | 186 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 187 | auto context = ctxInfo.directContext(); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 188 | SkBitmap bm = create_bm(); |
Brian Salomon | bc074a6 | 2020-03-18 10:06:13 -0400 | [diff] [blame] | 189 | GrBitmapTextureMaker maker(context, bm, GrImageTexGenPolicy::kNew_Uncached_Budgeted); |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 190 | auto view = maker.view(GrMipMapped::kNo); |
Greg Daniel | cc104db | 2020-02-03 14:17:08 -0500 | [diff] [blame] | 191 | if (!view.proxy()) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 195 | sk_sp<SkSpecialImage> fullSImg( |
| 196 | SkSpecialImage::MakeDeferredFromGpu(context, |
| 197 | SkIRect::MakeWH(kFullSize, kFullSize), |
| 198 | kNeedNewImageUniqueID_SpecialImage, |
| 199 | view, |
| 200 | maker.colorType(), |
| 201 | nullptr)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 202 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 203 | const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize); |
| 204 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 205 | { |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 206 | sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu( |
Brian Salomon | ecbb0fb | 2020-02-28 18:07:32 -0500 | [diff] [blame] | 207 | context, |
| 208 | subset, |
| 209 | kNeedNewImageUniqueID_SpecialImage, |
| 210 | std::move(view), |
| 211 | maker.colorType(), |
| 212 | nullptr)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 213 | test_image(subSImg1, reporter, context, true); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | { |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 217 | sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset)); |
Michael Ludwig | b458035 | 2019-06-21 16:01:42 -0400 | [diff] [blame] | 218 | test_image(subSImg2, reporter, context, true); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 219 | } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 220 | } |