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 "GrDrawContextPriv.h"
15 #include "GrContext.h"
18 #include "SkGradientShader.h"
19 #include "batches/GrDrawBatch.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.setXfermodeMode(SkXfermode::kDarken_Mode);
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.setXfermodeMode(SkXfermode::kLighten_Mode);
68 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
72 void onDraw(SkCanvas* canvas) override {
73 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
75 skiagm::GM::DrawGpuOnlyMessage(canvas);
79 GrContext* context = canvas->getGrContext();
84 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp,
85 GrTextureParams::ClampNoFilter()));
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);
97 const SkIRect texelDomains[] = {
99 SkIRect::MakeXYWH(fBmp.width() / 4,
105 SkRect renderRect = SkRect::Make(fBmp.bounds());
106 renderRect.outset(kDrawPad, kDrawPad);
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,
121 mode, GrTextureParams::kNone_FilterMode));
126 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
127 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
128 pipelineBuilder.addColorFragmentProcessor(fp);
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;
136 y += renderRect.height() + kTestPad;
142 static const SkScalar kDrawPad;
143 static const SkScalar kTestPad;
144 static const int kTargetWidth = 100;
145 static const int kTargetHeight = 100;
148 typedef GM INHERITED;
151 // Windows builds did not like SkScalar initialization in class :(
152 const SkScalar TextureDomainEffect::kDrawPad = 10.f;
153 const SkScalar TextureDomainEffect::kTestPad = 10.f;
155 DEF_GM(return new TextureDomainEffect;)