blob: d235f7f86bc10a427d1fb473a17d103b48c0c0fc [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
bsalomonbcf0a522014-10-08 08:40:09 -070095 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp, NULL));
96 if (!texture) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000097 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[] = {
halcanaryf622a6c2014-10-24 12:54:53 -0700108 fBmp.bounds(),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000109 SkIRect::MakeXYWH(fBmp.width() / 4,
110 fBmp.height() / 4,
111 fBmp.width() / 2,
112 fBmp.height() / 2),
113 };
114
halcanaryf622a6c2014-10-24 12:54:53 -0700115 SkRect renderRect = SkRect::Make(fBmp.bounds());
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000116 renderRect.outset(kDrawPad, kDrawPad);
117
118 SkScalar y = kDrawPad + kTestPad;
119 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
120 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
121 SkScalar x = kDrawPad + kTestPad;
122 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
123 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
joshualittb0a8a372014-09-23 09:50:21 -0700124 SkAutoTUnref<GrFragmentProcessor> fp(
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000125 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
126 GrTextureDomain::MakeTexelDomain(texture,
127 texelDomains[d]),
128 mode, GrTextureParams::kNone_FilterMode));
129
joshualittb0a8a372014-09-23 09:50:21 -0700130 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000131 continue;
132 }
133 SkMatrix viewMatrix;
134 viewMatrix.setTranslate(x, y);
135 drawState->reset(viewMatrix);
136 drawState->setRenderTarget(rt);
137 drawState->setColor(0xffffffff);
joshualittb0a8a372014-09-23 09:50:21 -0700138 drawState->addColorProcessor(fp);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000139
140 tt.target()->drawSimpleRect(renderRect);
141 x += renderRect.width() + kTestPad;
142 }
143 y += renderRect.height() + kTestPad;
144 }
145 }
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000146 }
147
148private:
joshualitt5ae5fc52014-07-29 12:59:27 -0700149 static const SkScalar kDrawPad;
150 static const SkScalar kTestPad;
151 static const int kTargetWidth = 100;
152 static const int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000153 SkBitmap fBmp;
154
155 typedef GM INHERITED;
156};
157
joshualitt5ae5fc52014-07-29 12:59:27 -0700158// Windows builds did not like SkScalar initialization in class :(
159const SkScalar TextureDomainEffect::kDrawPad = 10.f;
160const SkScalar TextureDomainEffect::kTestPad = 10.f;
161
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000162DEF_GM( return SkNEW(TextureDomainEffect); )
163}
164
165#endif