Clean up test drawContext usage
[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 "GrDrawContextPriv.h"
15 #include "GrContext.h"
16 #include "SkBitmap.h"
17 #include "SkGr.h"
18 #include "SkGradientShader.h"
19 #include "batches/GrDrawBatch.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.setXfermodeMode(SkXfermode::kDarken_Mode);
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.setXfermodeMode(SkXfermode::kLighten_Mode);
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         GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
74         if (!drawContext) {
75             skiagm::GM::DrawGpuOnlyMessage(canvas);
76             return;
77         }
78
79         GrContext* context = canvas->getGrContext();
80         if (!context) {
81             return;
82         }
83
84         SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp,
85                                                                  GrTextureParams::ClampNoFilter()));
86         if (!texture) {
87             return;
88         }
89
90         SkTArray<SkMatrix> textureMatrices;
91         textureMatrices.push_back().setIDiv(texture->width(), texture->height());
92         textureMatrices.push_back() = textureMatrices[0];
93         textureMatrices.back().postScale(1.5f, 0.85f);
94         textureMatrices.push_back() = textureMatrices[0];
95         textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
96
97         const SkIRect texelDomains[] = {
98             fBmp.bounds(),
99             SkIRect::MakeXYWH(fBmp.width() / 4,
100                               fBmp.height() / 4,
101                               fBmp.width() / 2,
102                               fBmp.height() / 2),
103         };
104
105         SkRect renderRect = SkRect::Make(fBmp.bounds());
106         renderRect.outset(kDrawPad, kDrawPad);
107
108         SkScalar y = kDrawPad + kTestPad;
109         for (int tm = 0; tm < textureMatrices.count(); ++tm) {
110             for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
111                 SkScalar x = kDrawPad + kTestPad;
112                 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
113                     GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
114                     GrPipelineBuilder pipelineBuilder;
115                     pipelineBuilder.setXPFactory(
116                         GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
117                     SkAutoTUnref<const GrFragmentProcessor> fp(
118                         GrTextureDomainEffect::Create(texture, textureMatrices[tm],
119                                                 GrTextureDomain::MakeTexelDomain(texture,
120                                                                                 texelDomains[d]),
121                                                 mode, GrTextureParams::kNone_FilterMode));
122
123                     if (!fp) {
124                         continue;
125                     }
126                     const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
127                     pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
128                     pipelineBuilder.addColorFragmentProcessor(fp);
129
130                     SkAutoTUnref<GrDrawBatch> batch(
131                             GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
132                                                                 renderRect, nullptr, nullptr));
133                     drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
134                     x += renderRect.width() + kTestPad;
135                 }
136                 y += renderRect.height() + kTestPad;
137             }
138         }
139     }
140
141 private:
142     static const SkScalar kDrawPad;
143     static const SkScalar kTestPad;
144     static const int      kTargetWidth = 100;
145     static const int      kTargetHeight = 100;
146     SkBitmap fBmp;
147
148     typedef GM INHERITED;
149 };
150
151 // Windows builds did not like SkScalar initialization in class :(
152 const SkScalar TextureDomainEffect::kDrawPad = 10.f;
153 const SkScalar TextureDomainEffect::kTestPad = 10.f;
154
155 DEF_GM(return new TextureDomainEffect;)
156 }
157
158 #endif