3 * Copyright 2014 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 // This test only works with the GPU backend.
15 #include "GrContext.h"
17 #include "effects/GrTextureDomain.h"
20 #include "SkGradientShader.h"
24 * This GM directly exercises GrTextureDomainEffect.
26 class TextureDomainEffect : public GM {
28 TextureDomainEffect() {
29 this->setBGColor(0xFFFFFFFF);
33 SkString onShortName() SK_OVERRIDE {
34 return SkString("texture_domain_effect");
37 SkISize onISize() SK_OVERRIDE {
38 const SkScalar canvasWidth = kDrawPad +
39 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
40 kTestPad * GrTextureDomain::kModeCount;
41 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
44 void onOnceBeforeDraw() SK_OVERRIDE {
45 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
46 SkCanvas canvas(fBmp);
47 canvas.clear(0x00000000);
50 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
51 paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1,
52 NULL, SK_ARRAY_COUNT(colors1)))->unref();
53 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
54 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
56 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
57 paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, NULL,
58 SK_ARRAY_COUNT(colors2)))->unref();
59 paint.setXfermodeMode(SkXfermode::kDarken_Mode);
60 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
61 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
63 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
64 paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, NULL,
65 SK_ARRAY_COUNT(colors3)))->unref();
66 paint.setXfermodeMode(SkXfermode::kLighten_Mode);
67 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
68 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
71 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
72 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
76 GrContext* context = rt->getContext();
77 if (NULL == context) {
78 this->drawGpuOnlyMessage(canvas);
83 context->getTestTarget(&tt);
84 if (NULL == tt.target()) {
85 SkDEBUGFAIL("Couldn't get Gr test target.");
89 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp, NULL));
94 SkTArray<SkMatrix> textureMatrices;
95 textureMatrices.push_back().setIDiv(texture->width(), texture->height());
96 textureMatrices.push_back() = textureMatrices[0];
97 textureMatrices.back().postScale(1.5f, 0.85f);
98 textureMatrices.push_back() = textureMatrices[0];
99 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
101 const SkIRect texelDomains[] = {
103 SkIRect::MakeXYWH(fBmp.width() / 4,
109 SkRect renderRect = SkRect::Make(fBmp.bounds());
110 renderRect.outset(kDrawPad, kDrawPad);
112 SkScalar y = kDrawPad + kTestPad;
113 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
114 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
115 SkScalar x = kDrawPad + kTestPad;
116 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
117 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
118 SkAutoTUnref<GrFragmentProcessor> fp(
119 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
120 GrTextureDomain::MakeTexelDomain(texture,
122 mode, GrTextureParams::kNone_FilterMode));
128 viewMatrix.setTranslate(x, y);
129 GrPipelineBuilder pipelineBuilder;
130 pipelineBuilder.setRenderTarget(rt);
131 pipelineBuilder.addColorProcessor(fp);
133 tt.target()->drawSimpleRect(&pipelineBuilder,
137 x += renderRect.width() + kTestPad;
139 y += renderRect.height() + kTestPad;
145 static const SkScalar kDrawPad;
146 static const SkScalar kTestPad;
147 static const int kTargetWidth = 100;
148 static const int kTargetHeight = 100;
151 typedef GM INHERITED;
154 // Windows builds did not like SkScalar initialization in class :(
155 const SkScalar TextureDomainEffect::kDrawPad = 10.f;
156 const SkScalar TextureDomainEffect::kTestPad = 10.f;
158 DEF_GM( return SkNEW(TextureDomainEffect); )