| 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" |
| 9 |
#include "include/core/SkBitmap.h" |
| 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/ganesh/GrDirectContext.h" |
| 16 |
#include "src/base/SkRectMemcpy.h" |
| 17 |
#include "src/core/SkCanvasPriv.h" |
| 18 |
#include "src/core/SkConvertPixels.h" |
| 19 |
#include "src/gpu/ganesh/GrCanvas.h" |
| 20 |
#include "src/gpu/ganesh/GrCaps.h" |
| 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" |
| 25 |
#include "src/gpu/ganesh/GrTexture.h" |
| 26 |
#include "src/gpu/ganesh/SkGr.h" |
| 27 |
#include "src/gpu/ganesh/SurfaceDrawContext.h" |
| 28 |
#include "src/gpu/ganesh/effects/GrTextureEffect.h" |
| 29 |
#include "tools/ganesh/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( |
| 46 |
[src](GrResourceProvider* rp, const GrSurfaceProxy::LazySurfaceDesc& desc) |
| 47 |
-> GrSurfaceProxy::LazyCallbackResult { |
| 48 |
SkASSERT(desc.fMipmapped == skgpu::Mipmapped::kNo); |
| 49 |
GrMipLevel mipLevel = {src.getPixels(), src.rowBytes(), nullptr}; |
| 50 |
auto colorType = SkColorTypeToGrColorType(src.colorType()); |
| 51 |
|
| 52 |
return rp->createTexture(src.dimensions(), |
| 53 |
desc.fFormat, |
| 54 |
desc.fTextureType, |
| 55 |
colorType, |
| 56 |
desc.fRenderable, |
| 57 |
desc.fSampleCnt, |
| 58 |
desc.fBudgeted, |
| 59 |
desc.fFit, |
| 60 |
desc.fProtected, |
| 61 |
mipLevel, |
| 62 |
desc.fLabel); |
| 63 |
}, |
| 64 |
format, |
| 65 |
GrRenderable::kNo, |
| 66 |
1, |
| 67 |
GrProtected::kNo, |
| 68 |
*dContext->priv().caps(), |
| 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, |
| 156 |
skgpu::ganesh::SurfaceDrawContext* sdc, |
| 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: |
| 188 |
SkString getName() const override { |
| 189 |
return SkStringPrintf("lazytiling_%s", fOrigin == kTopLeft_GrSurfaceOrigin ? "tl" : "bl"); |
| 190 |
} |
| 191 |
|
| 192 |
SkISize getISize() override { return SkISize::Make(kTotalWidth, kTotalHeight); } |
| 193 |
|
| 194 |
DrawResult onGpuSetup(SkCanvas* canvas, SkString* errorMsg, GraphiteTestContext*) override { |
| 195 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 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 |
|
| 214 |
DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override { |
| 215 |
auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas); |
| 216 |
if (!sdc) { |
| 217 |
*errorMsg = kErrorMsg_DrawSkippedGpuOnly; |
| 218 |
return DrawResult::kSkip; |
| 219 |
} |
| 220 |
|
| 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::RectToRectOrIdentity(contentRect, |
| 232 |
SkRect::Make(fContentRect)); |
| 233 |
|
| 234 |
draw_texture(rContext->priv().caps(), |
| 235 |
sdc, |
| 236 |
fView, |
| 237 |
fContentRect, |
| 238 |
cellRect, |
| 239 |
texMatrix, |
| 240 |
SkTileModeToWrapMode(xMode), |
| 241 |
SkTileModeToWrapMode(yMode)); |
| 242 |
|
| 243 |
x += 2*kContentSize+kPad; |
| 244 |
} |
| 245 |
|
| 246 |
y += 2*kContentSize+kPad; |
| 247 |
} |
| 248 |
|
| 249 |
return DrawResult::kOk; |
| 250 |
} |
| 251 |
|
| 252 |
private: |
| 253 |
inline static constexpr int kLeftContentOffset = 8; |
| 254 |
inline static constexpr int kTopContentOffset = 16; |
| 255 |
inline static constexpr int kRightContentPadding = 24; |
| 256 |
inline static constexpr int kBottomContentPadding = 80; |
| 257 |
|
| 258 |
inline static constexpr int kPad = 4; // on-screen padding between cells |
| 259 |
|
| 260 |
inline static constexpr int kContentSize = 32; |
| 261 |
|
| 262 |
// Each cell in this GM's grid is a square - 2*kContentSize on a side |
| 263 |
inline static constexpr int kTotalWidth = (2*kContentSize+kPad) * kSkTileModeCount + kPad; |
| 264 |
inline static constexpr int kTotalHeight = (2*kContentSize+kPad) * kSkTileModeCount + kPad; |
| 265 |
|
| 266 |
GrSurfaceOrigin fOrigin; |
| 267 |
SkIRect fContentRect; |
| 268 |
GrSurfaceProxyView fView; |
| 269 |
|
| 270 |
using INHERITED = GM; |
| 271 |
}; |
| 272 |
|
| 273 |
////////////////////////////////////////////////////////////////////////////// |
| 274 |
|
| 275 |
DEF_GM(return new LazyTilingGM(kTopLeft_GrSurfaceOrigin);) |
| 276 |
DEF_GM(return new LazyTilingGM(kBottomLeft_GrSurfaceOrigin);) |
| 277 |
|
| 278 |
} // namespace skiagm |