blob: 6cc979991002748ef20be74afc86e69f6870d96f [file] [log] [blame]
commit-bot@chromium.org26632632014-03-25 15:13:18 +00001
2/*
3 * Copyright 2014 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9// This test only works with the GPU backend.
10
11#include "gm.h"
12
13#if SK_SUPPORT_GPU
14
15#include "GrContext.h"
16#include "GrTest.h"
17#include "effects/GrTextureDomain.h"
18#include "SkBitmap.h"
19#include "SkGr.h"
20#include "SkGradientShader.h"
21
22namespace skiagm {
23/**
24 * This GM directly exercises GrTextureDomainEffect.
25 */
26class TextureDomainEffect : public GM {
27public:
28 TextureDomainEffect() {
29 this->setBGColor(0xFFFFFFFF);
30 }
31
32protected:
33 virtual SkString onShortName() SK_OVERRIDE {
34 return SkString("texture_domain_effect");
35 }
36
37 virtual SkISize onISize() SK_OVERRIDE {
joshualitt5ae5fc52014-07-29 12:59:27 -070038 const SkScalar canvasWidth = kDrawPad +
39 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
40 kTestPad * GrTextureDomain::kModeCount;
41 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000042 }
43
44 virtual uint32_t onGetFlags() const SK_OVERRIDE {
45 // This is a GPU-specific GM.
46 return kGPUOnly_Flag;
47 }
48
49 virtual void onOnceBeforeDraw() SK_OVERRIDE {
joshualitt5ae5fc52014-07-29 12:59:27 -070050 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000051 SkCanvas canvas(fBmp);
commit-bot@chromium.orgc5713e42014-04-07 21:18:46 +000052 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000053 SkPaint paint;
54
55 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
56 paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1,
57 NULL, SK_ARRAY_COUNT(colors1)))->unref();
58 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
59 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
60
61 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
62 paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, NULL,
63 SK_ARRAY_COUNT(colors2)))->unref();
64 paint.setXfermodeMode(SkXfermode::kDarken_Mode);
65 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
66 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
67
68 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
69 paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, NULL,
70 SK_ARRAY_COUNT(colors3)))->unref();
71 paint.setXfermodeMode(SkXfermode::kLighten_Mode);
72 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
73 fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
74 }
75
76 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
77 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
78 if (NULL == rt) {
79 return;
80 }
81 GrContext* context = rt->getContext();
82 if (NULL == context) {
83 return;
84 }
85
86 GrTestTarget tt;
87 context->getTestTarget(&tt);
88 if (NULL == tt.target()) {
89 SkDEBUGFAIL("Couldn't get Gr test target.");
90 return;
91 }
92
93 GrDrawState* drawState = tt.target()->drawState();
94
95 GrTexture* texture = GrLockAndRefCachedBitmapTexture(context, fBmp, NULL);
96 if (NULL == texture) {
97 return;
98 }
99
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000100 SkTArray<SkMatrix> textureMatrices;
101 textureMatrices.push_back().setIDiv(texture->width(), texture->height());
102 textureMatrices.push_back() = textureMatrices[0];
103 textureMatrices.back().postScale(1.5f, 0.85f);
104 textureMatrices.push_back() = textureMatrices[0];
105 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
106
107 const SkIRect texelDomains[] = {
108 SkIRect::MakeWH(fBmp.width(), fBmp.height()),
109 SkIRect::MakeXYWH(fBmp.width() / 4,
110 fBmp.height() / 4,
111 fBmp.width() / 2,
112 fBmp.height() / 2),
113 };
114
115 SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp.width()),
116 SkIntToScalar(fBmp.height()));
117 renderRect.outset(kDrawPad, kDrawPad);
118
119 SkScalar y = kDrawPad + kTestPad;
120 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
121 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
122 SkScalar x = kDrawPad + kTestPad;
123 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
124 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
bsalomon83d081a2014-07-08 09:56:10 -0700125 SkAutoTUnref<GrEffect> effect(
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000126 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
127 GrTextureDomain::MakeTexelDomain(texture,
128 texelDomains[d]),
129 mode, GrTextureParams::kNone_FilterMode));
130
131 if (!effect) {
132 continue;
133 }
134 SkMatrix viewMatrix;
135 viewMatrix.setTranslate(x, y);
136 drawState->reset(viewMatrix);
137 drawState->setRenderTarget(rt);
138 drawState->setColor(0xffffffff);
139 drawState->addColorEffect(effect, 1);
140
141 tt.target()->drawSimpleRect(renderRect);
142 x += renderRect.width() + kTestPad;
143 }
144 y += renderRect.height() + kTestPad;
145 }
146 }
147 GrUnlockAndUnrefCachedBitmapTexture(texture);
148 }
149
150private:
joshualitt5ae5fc52014-07-29 12:59:27 -0700151 static const SkScalar kDrawPad;
152 static const SkScalar kTestPad;
153 static const int kTargetWidth = 100;
154 static const int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000155 SkBitmap fBmp;
156
157 typedef GM INHERITED;
158};
159
joshualitt5ae5fc52014-07-29 12:59:27 -0700160// Windows builds did not like SkScalar initialization in class :(
161const SkScalar TextureDomainEffect::kDrawPad = 10.f;
162const SkScalar TextureDomainEffect::kTestPad = 10.f;
163
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000164DEF_GM( return SkNEW(TextureDomainEffect); )
165}
166
167#endif