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