2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // This test only works with the GPU backend.
14 #include "GrRenderTargetContextPriv.h"
15 #include "GrContext.h"
18 #include "SkGradientShader.h"
19 #include "batches/GrDrawOp.h"
20 #include "batches/GrRectBatchFactory.h"
21 #include "effects/GrTextureDomain.h"
25 * This GM directly exercises GrTextureDomainEffect.
27 class TextureDomainEffect : public GM {
29 TextureDomainEffect() {
30 this->setBGColor(0xFFFFFFFF);
34 SkString onShortName() override {
35 return SkString("texture_domain_effect");
38 SkISize onISize() override {
39 const SkScalar canvasWidth = kDrawPad +
40 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
41 kTestPad * GrTextureDomain::kModeCount;
42 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
45 void onOnceBeforeDraw() override {
46 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
47 SkCanvas canvas(fBmp);
48 canvas.clear(0x00000000);
51 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
52 paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
53 SK_ARRAY_COUNT(colors1)));
54 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
57 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
58 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
59 SK_ARRAY_COUNT(colors2)));
60 paint.setBlendMode(SkBlendMode::kDarken);
61 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
64 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
65 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
66 SK_ARRAY_COUNT(colors3)));
67 paint.setBlendMode(SkBlendMode::kLighten);
68 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
72 void onDraw(SkCanvas* canvas) override {
73 GrRenderTargetContext* renderTargetContext =
74 canvas->internal_private_accessTopLayerRenderTargetContext();
75 if (!renderTargetContext) {
76 skiagm::GM::DrawGpuOnlyMessage(canvas);
80 GrContext* context = canvas->getGrContext();
85 sk_sp<GrTexture> texture(
86 GrRefCachedBitmapTexture(context, fBmp, GrSamplerParams::ClampNoFilter()));
91 SkTArray<SkMatrix> textureMatrices;
92 textureMatrices.push_back().setIDiv(texture->width(), texture->height());
93 textureMatrices.push_back() = textureMatrices[0];
94 textureMatrices.back().postScale(1.5f, 0.85f);
95 textureMatrices.push_back() = textureMatrices[0];
96 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
98 const SkIRect texelDomains[] = {
100 SkIRect::MakeXYWH(fBmp.width() / 4,
106 SkRect renderRect = SkRect::Make(fBmp.bounds());
107 renderRect.outset(kDrawPad, kDrawPad);
109 SkScalar y = kDrawPad + kTestPad;
110 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
111 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
112 SkScalar x = kDrawPad + kTestPad;
113 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
114 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
116 grPaint.setXPFactory(GrPorterDuffXPFactory::Make(SkBlendMode::kSrc));
117 sk_sp<GrFragmentProcessor> fp(
118 GrTextureDomainEffect::Make(texture.get(), nullptr, textureMatrices[tm],
119 GrTextureDomain::MakeTexelDomain(texture.get(),
121 mode, GrSamplerParams::kNone_FilterMode));
126 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
127 grPaint.addColorFragmentProcessor(std::move(fp));
129 sk_sp<GrDrawOp> batch(
130 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
131 renderRect, nullptr, nullptr));
132 renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get());
133 x += renderRect.width() + kTestPad;
135 y += renderRect.height() + kTestPad;
141 static constexpr SkScalar kDrawPad = 10.f;
142 static constexpr SkScalar kTestPad = 10.f;;
143 static constexpr int kTargetWidth = 100;
144 static constexpr int kTargetHeight = 100;
147 typedef GM INHERITED;
150 DEF_GM(return new TextureDomainEffect;)