Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / gm / texturedomaineffect.cpp
1 /*
2  * Copyright 2014 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
8 // This test only works with the GPU backend.
9
10 #include "gm.h"
11
12 #if SK_SUPPORT_GPU
13
14 #include "GrRenderTargetContextPriv.h"
15 #include "GrContext.h"
16 #include "SkBitmap.h"
17 #include "SkGr.h"
18 #include "SkGradientShader.h"
19 #include "batches/GrDrawOp.h"
20 #include "batches/GrRectBatchFactory.h"
21 #include "effects/GrTextureDomain.h"
22
23 namespace skiagm {
24 /**
25  * This GM directly exercises GrTextureDomainEffect.
26  */
27 class TextureDomainEffect : public GM {
28 public:
29     TextureDomainEffect() {
30         this->setBGColor(0xFFFFFFFF);
31     }
32
33 protected:
34     SkString onShortName() override {
35         return SkString("texture_domain_effect");
36     }
37
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);
43     }
44
45     void onOnceBeforeDraw() override {
46         fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
47         SkCanvas canvas(fBmp);
48         canvas.clear(0x00000000);
49         SkPaint paint;
50
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),
55                         paint);
56
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),
62                         paint);
63
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),
69                         paint);
70     }
71
72     void onDraw(SkCanvas* canvas) override {
73         GrRenderTargetContext* renderTargetContext =
74             canvas->internal_private_accessTopLayerRenderTargetContext();
75         if (!renderTargetContext) {
76             skiagm::GM::DrawGpuOnlyMessage(canvas);
77             return;
78         }
79
80         GrContext* context = canvas->getGrContext();
81         if (!context) {
82             return;
83         }
84
85         sk_sp<GrTexture> texture(
86             GrRefCachedBitmapTexture(context, fBmp, GrSamplerParams::ClampNoFilter()));
87         if (!texture) {
88             return;
89         }
90
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);
97
98         const SkIRect texelDomains[] = {
99             fBmp.bounds(),
100             SkIRect::MakeXYWH(fBmp.width() / 4,
101                               fBmp.height() / 4,
102                               fBmp.width() / 2,
103                               fBmp.height() / 2),
104         };
105
106         SkRect renderRect = SkRect::Make(fBmp.bounds());
107         renderRect.outset(kDrawPad, kDrawPad);
108
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;
115                     GrPaint grPaint;
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(),
120                                                                                  texelDomains[d]),
121                                                 mode, GrSamplerParams::kNone_FilterMode));
122
123                     if (!fp) {
124                         continue;
125                     }
126                     const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
127                     grPaint.addColorFragmentProcessor(std::move(fp));
128
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;
134                 }
135                 y += renderRect.height() + kTestPad;
136             }
137         }
138     }
139
140 private:
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;
145     SkBitmap fBmp;
146
147     typedef GM INHERITED;
148 };
149
150 DEF_GM(return new TextureDomainEffect;)
151 }
152
153 #endif