Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC |
| 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 | |
| 8 | #include "gm/gm.h" |
Kevin Lubick | 677a12f | 2022-03-09 08:23:14 -0500 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkSize.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/gpu/GrDirectContext.h" |
Kevin Lubick | 406dacd | 2023-03-23 10:15:46 -0400 | [diff] [blame] | 16 | #include "src/base/SkRectMemcpy.h" |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 17 | #include "src/core/SkCanvasPriv.h" |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 18 | #include "src/core/SkConvertPixels.h" |
Kevin Lubick | acdc108 | 2023-06-09 11:05:24 -0400 | [diff] [blame] | 19 | #include "src/gpu/ganesh/GrCanvas.h" |
Kevin Lubick | 502553f | 2022-11-28 12:42:38 -0500 | [diff] [blame] | 20 | #include "src/gpu/ganesh/GrCaps.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 21 | #include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 22 | #include "src/gpu/ganesh/GrPaint.h" |
| 23 | #include "src/gpu/ganesh/GrProxyProvider.h" |
| 24 | #include "src/gpu/ganesh/GrResourceProvider.h" |
Kevin Lubick | c96cb63 | 2022-11-17 14:51:41 -0500 | [diff] [blame] | 25 | #include "src/gpu/ganesh/GrTexture.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 26 | #include "src/gpu/ganesh/SkGr.h" |
Robert Phillips | c1b9408 | 2022-08-09 17:16:19 -0400 | [diff] [blame] | 27 | #include "src/gpu/ganesh/SurfaceDrawContext.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 28 | #include "src/gpu/ganesh/effects/GrTextureEffect.h" |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 29 | #include "tools/gpu/ProxyUtils.h" |
| 30 | |
| 31 | static GrSurfaceProxyView create_view(GrDirectContext* dContext, |
| 32 | const SkBitmap& src, |
| 33 | GrSurfaceOrigin origin) { |
| 34 | SkASSERT(src.colorType() == kRGBA_8888_SkColorType); |
| 35 | |
| 36 | #define USE_LAZY_PROXIES 1 // Toggle this to generate the reference images |
| 37 | |
| 38 | if (USE_LAZY_PROXIES) { |
| 39 | auto format = dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 40 | GrRenderable::kNo); |
| 41 | if (!format.isValid()) { |
| 42 | return {}; |
| 43 | } |
| 44 | |
| 45 | sk_sp<GrTextureProxy> proxy = GrProxyProvider::MakeFullyLazyProxy( |
Kevin Lubick | df73d16 | 2023-09-11 11:56:53 -0400 | [diff] [blame] | 46 | [src](GrResourceProvider* rp, const GrSurfaceProxy::LazySurfaceDesc& desc) |
| 47 | -> GrSurfaceProxy::LazyCallbackResult { |
| 48 | SkASSERT(desc.fMipmapped == skgpu::Mipmapped::kNo); |
Brian Salomon | 2c67340 | 2021-04-02 14:36:58 -0400 | [diff] [blame] | 49 | GrMipLevel mipLevel = {src.getPixels(), src.rowBytes(), nullptr}; |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 50 | auto colorType = SkColorTypeToGrColorType(src.colorType()); |
| 51 | |
Greg Daniel | 0e9d34d | 2021-08-13 16:20:18 -0400 | [diff] [blame] | 52 | return rp->createTexture(src.dimensions(), |
| 53 | desc.fFormat, |
Greg Daniel | e56d31f | 2021-08-16 09:29:56 -0400 | [diff] [blame] | 54 | desc.fTextureType, |
Greg Daniel | 0e9d34d | 2021-08-13 16:20:18 -0400 | [diff] [blame] | 55 | colorType, |
| 56 | desc.fRenderable, |
| 57 | desc.fSampleCnt, |
| 58 | desc.fBudgeted, |
| 59 | desc.fFit, |
| 60 | desc.fProtected, |
Aditya Kushwah | a696f8a | 2022-05-03 13:00:24 -0700 | [diff] [blame] | 61 | mipLevel, |
| 62 | desc.fLabel); |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 63 | }, |
Kevin Lubick | df73d16 | 2023-09-11 11:56:53 -0400 | [diff] [blame] | 64 | format, |
| 65 | GrRenderable::kNo, |
| 66 | 1, |
| 67 | GrProtected::kNo, |
| 68 | *dContext->priv().caps(), |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 69 | GrSurfaceProxy::UseAllocator::kYes); |
| 70 | |
| 71 | if (!proxy) { |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | auto swizzle = dContext->priv().caps()->getReadSwizzle(proxy->backendFormat(), |
| 76 | GrColorType::kRGBA_8888); |
| 77 | return GrSurfaceProxyView(std::move(proxy), origin, swizzle); |
| 78 | } |
| 79 | |
| 80 | return sk_gpu_test::MakeTextureProxyViewFromData(dContext, |
| 81 | GrRenderable::kNo, |
| 82 | origin, |
| 83 | src.pixmap()); |
| 84 | } |
| 85 | |
| 86 | // Create an over large texture which is initialized to opaque black outside of the content |
| 87 | // rect. The inside of the content rect consists of a grey coordinate frame lacking the -Y axis. |
| 88 | // The -X and +X axes have a red and green dot at their ends (respectively). Finally, the content |
| 89 | // rect has a 1-pixel wide blue border. |
| 90 | static SkBitmap create_bitmap(SkIRect contentRect, SkISize fullSize, GrSurfaceOrigin origin) { |
| 91 | |
| 92 | const int kContentSize = contentRect.width(); |
| 93 | SkBitmap contentBM; |
| 94 | |
| 95 | { |
| 96 | SkImageInfo contentInfo = SkImageInfo::Make(kContentSize, kContentSize, |
| 97 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 98 | contentBM.allocPixels(contentInfo); |
| 99 | |
| 100 | contentBM.eraseColor(SK_ColorWHITE); |
| 101 | |
| 102 | const int halfM1 = kContentSize/2 - 1; |
| 103 | |
| 104 | // The coordinate frame |
| 105 | contentBM.eraseArea(SkIRect::MakeXYWH(halfM1, 2,2, halfM1), SK_ColorGRAY); |
| 106 | contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, kContentSize-4, 2), SK_ColorGRAY); |
| 107 | |
| 108 | contentBM.eraseArea(SkIRect::MakeXYWH(2, halfM1, 2, 2), SK_ColorRED); |
| 109 | contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-4, halfM1, 2, 2), SK_ColorGREEN); |
| 110 | |
| 111 | // The 1-pixel wide rim around the content rect |
| 112 | contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, kContentSize, 1), SK_ColorBLUE); |
| 113 | contentBM.eraseArea(SkIRect::MakeXYWH(0, 0, 1, kContentSize), SK_ColorBLUE); |
| 114 | contentBM.eraseArea(SkIRect::MakeXYWH(kContentSize-1, 0, 1, kContentSize), SK_ColorBLUE); |
| 115 | contentBM.eraseArea(SkIRect::MakeXYWH(0, kContentSize-1, kContentSize, 1), SK_ColorBLUE); |
| 116 | } |
| 117 | |
| 118 | SkBitmap bigBM; |
| 119 | |
| 120 | { |
| 121 | const int kLeft = contentRect.fLeft; |
| 122 | const int kTop = contentRect.fTop; |
| 123 | |
| 124 | SkImageInfo bigInfo = SkImageInfo::Make(fullSize.fWidth, fullSize.fHeight, |
| 125 | kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 126 | |
| 127 | bigBM.allocPixels(bigInfo); |
| 128 | |
| 129 | bigBM.eraseColor(SK_ColorBLACK); |
| 130 | |
| 131 | const char* src = static_cast<const char*>(contentBM.getPixels()); |
| 132 | size_t srcRB = contentBM.rowBytes(); |
| 133 | size_t dstRB = bigBM.rowBytes(); |
| 134 | |
| 135 | if (USE_LAZY_PROXIES && origin == kBottomLeft_GrSurfaceOrigin) { |
| 136 | char* dst = static_cast<char*>(bigBM.getAddr(kLeft, fullSize.height() - kTop - 1)); |
| 137 | for (int y = 0; y < contentBM.height(); ++y) { |
| 138 | memcpy(dst, src, srcRB); |
| 139 | src = src + srcRB; |
| 140 | dst = dst - dstRB; |
| 141 | } |
| 142 | } else { |
| 143 | char* dst = static_cast<char*>(bigBM.getAddr(kLeft, kTop)); |
| 144 | SkRectMemcpy(dst, dstRB, src, srcRB, |
| 145 | contentBM.rowBytes(), contentBM.height()); |
| 146 | } |
| 147 | |
| 148 | bigBM.setAlphaType(kOpaque_SkAlphaType); |
| 149 | bigBM.setImmutable(); |
| 150 | } |
| 151 | |
| 152 | return bigBM; |
| 153 | } |
| 154 | |
| 155 | static void draw_texture(const GrCaps* caps, |
Kevin Lubick | f24283f | 2023-03-17 16:10:23 -0400 | [diff] [blame] | 156 | skgpu::ganesh::SurfaceDrawContext* sdc, |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 157 | const GrSurfaceProxyView& src, |
| 158 | const SkIRect& srcRect, |
| 159 | const SkIRect& drawRect, |
| 160 | const SkMatrix& mat, |
| 161 | GrSamplerState::WrapMode xTileMode, |
| 162 | GrSamplerState::WrapMode yTileMode) { |
| 163 | GrSamplerState sampler(xTileMode, yTileMode, SkFilterMode::kNearest); |
| 164 | |
| 165 | auto fp = GrTextureEffect::MakeSubset(src, kOpaque_SkAlphaType, mat, |
| 166 | sampler, SkRect::Make(srcRect), *caps); |
| 167 | GrPaint paint; |
| 168 | paint.setColorFragmentProcessor(std::move(fp)); |
| 169 | |
| 170 | sdc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), SkRect::Make(drawRect)); |
| 171 | } |
| 172 | |
| 173 | namespace skiagm { |
| 174 | |
| 175 | // This GM exercises all the different tile modes for a texture that cannot be normalized |
| 176 | // early (i.e., rectangle or fully-lazy). |
| 177 | // TODO: should we also test w/ mipmapping? |
| 178 | class LazyTilingGM : public GpuGM { |
| 179 | public: |
| 180 | LazyTilingGM(GrSurfaceOrigin origin) |
| 181 | : fOrigin(origin) |
| 182 | , fContentRect(SkIRect::MakeXYWH(kLeftContentOffset, kTopContentOffset, |
| 183 | kContentSize, kContentSize)) { |
| 184 | this->setBGColor(0xFFCCCCCC); |
| 185 | } |
| 186 | |
| 187 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 188 | SkString getName() const override { |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 189 | return SkStringPrintf("lazytiling_%s", fOrigin == kTopLeft_GrSurfaceOrigin ? "tl" : "bl"); |
| 190 | } |
| 191 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 192 | SkISize getISize() override { return SkISize::Make(kTotalWidth, kTotalHeight); } |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 193 | |
Brian Salomon | c759bbf | 2023-12-05 11:11:27 -0500 | [diff] [blame] | 194 | DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override { |
Jim Van Verth | a862443 | 2023-02-13 16:48:09 -0500 | [diff] [blame] | 195 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 196 | if (!dContext || dContext->abandoned()) { |
| 197 | return DrawResult::kSkip; |
| 198 | } |
| 199 | |
| 200 | auto bm = create_bitmap(fContentRect, |
| 201 | { kLeftContentOffset + kContentSize + kRightContentPadding, |
| 202 | kTopContentOffset + kContentSize + kBottomContentPadding }, |
| 203 | fOrigin); |
| 204 | |
| 205 | fView = create_view(dContext, bm, fOrigin); |
| 206 | if (!fView.proxy()) { |
| 207 | *errorMsg = "Failed to create proxy"; |
| 208 | return DrawResult::kFail; |
| 209 | } |
| 210 | |
| 211 | return DrawResult::kOk; |
| 212 | } |
| 213 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 214 | DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { |
Kevin Lubick | acdc108 | 2023-06-09 11:05:24 -0400 | [diff] [blame] | 215 | auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 216 | if (!sdc) { |
| 217 | *errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 218 | return DrawResult::kSkip; |
| 219 | } |
| 220 | |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 221 | int y = kPad; |
| 222 | for (auto yMode : { SkTileMode::kClamp, SkTileMode::kRepeat, |
| 223 | SkTileMode::kMirror, SkTileMode::kDecal }) { |
| 224 | int x = kPad; |
| 225 | for (auto xMode : { SkTileMode::kClamp, SkTileMode::kRepeat, |
| 226 | SkTileMode::kMirror, SkTileMode::kDecal }) { |
| 227 | SkIRect cellRect = SkIRect::MakeXYWH(x, y, 2*kContentSize, 2*kContentSize); |
| 228 | SkRect contentRect = SkRect::MakeXYWH(x+kContentSize/2, y+kContentSize/2, |
| 229 | kContentSize, kContentSize); |
| 230 | |
| 231 | SkMatrix texMatrix = SkMatrix::RectToRect(contentRect, SkRect::Make(fContentRect)); |
| 232 | |
| 233 | draw_texture(rContext->priv().caps(), |
| 234 | sdc, |
| 235 | fView, |
| 236 | fContentRect, |
| 237 | cellRect, |
| 238 | texMatrix, |
| 239 | SkTileModeToWrapMode(xMode), |
| 240 | SkTileModeToWrapMode(yMode)); |
| 241 | |
| 242 | x += 2*kContentSize+kPad; |
| 243 | } |
| 244 | |
| 245 | y += 2*kContentSize+kPad; |
| 246 | } |
| 247 | |
Robert Phillips | 7a0d3c3 | 2021-07-21 15:39:51 -0400 | [diff] [blame] | 248 | return DrawResult::kOk; |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 252 | inline static constexpr int kLeftContentOffset = 8; |
| 253 | inline static constexpr int kTopContentOffset = 16; |
| 254 | inline static constexpr int kRightContentPadding = 24; |
| 255 | inline static constexpr int kBottomContentPadding = 80; |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 256 | |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 257 | inline static constexpr int kPad = 4; // on-screen padding between cells |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 258 | |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 259 | inline static constexpr int kContentSize = 32; |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 260 | |
| 261 | // Each cell in this GM's grid is a square - 2*kContentSize on a side |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 262 | inline static constexpr int kTotalWidth = (2*kContentSize+kPad) * kSkTileModeCount + kPad; |
| 263 | inline static constexpr int kTotalHeight = (2*kContentSize+kPad) * kSkTileModeCount + kPad; |
Robert Phillips | 94e6791 | 2021-01-21 13:39:08 -0500 | [diff] [blame] | 264 | |
| 265 | GrSurfaceOrigin fOrigin; |
| 266 | SkIRect fContentRect; |
| 267 | GrSurfaceProxyView fView; |
| 268 | |
| 269 | using INHERITED = GM; |
| 270 | }; |
| 271 | |
| 272 | ////////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | DEF_GM(return new LazyTilingGM(kTopLeft_GrSurfaceOrigin);) |
| 275 | DEF_GM(return new LazyTilingGM(kBottomLeft_GrSurfaceOrigin);) |
| 276 | |
| 277 | } // namespace skiagm |