blob: d39375b3a2bbcb78c2abc5d5f20e251825a4aa63 [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:
mtklein72c9faa2015-01-09 10:06:39 -080033 SkString onShortName() SK_OVERRIDE {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000034 return SkString("texture_domain_effect");
35 }
36
mtklein72c9faa2015-01-09 10:06:39 -080037 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
mtklein72c9faa2015-01-09 10:06:39 -080044 void onOnceBeforeDraw() SK_OVERRIDE {
joshualitt5ae5fc52014-07-29 12:59:27 -070045 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000046 SkCanvas canvas(fBmp);
commit-bot@chromium.orgc5713e42014-04-07 21:18:46 +000047 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000048 SkPaint paint;
49
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);
55
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);
62
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);
69 }
70
mtklein72c9faa2015-01-09 10:06:39 -080071 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000072 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
73 if (NULL == rt) {
74 return;
75 }
76 GrContext* context = rt->getContext();
77 if (NULL == context) {
bsalomonb62da802015-01-31 07:51:14 -080078 this->drawGpuOnlyMessage(canvas);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000079 return;
80 }
81
82 GrTestTarget tt;
83 context->getTestTarget(&tt);
84 if (NULL == tt.target()) {
85 SkDEBUGFAIL("Couldn't get Gr test target.");
86 return;
87 }
88
bsalomonbcf0a522014-10-08 08:40:09 -070089 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp, NULL));
90 if (!texture) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000091 return;
92 }
93
commit-bot@chromium.org26632632014-03-25 15:13:18 +000094 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);
100
101 const SkIRect texelDomains[] = {
halcanaryf622a6c2014-10-24 12:54:53 -0700102 fBmp.bounds(),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000103 SkIRect::MakeXYWH(fBmp.width() / 4,
104 fBmp.height() / 4,
105 fBmp.width() / 2,
106 fBmp.height() / 2),
107 };
108
halcanaryf622a6c2014-10-24 12:54:53 -0700109 SkRect renderRect = SkRect::Make(fBmp.bounds());
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000110 renderRect.outset(kDrawPad, kDrawPad);
111
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;
joshualittb0a8a372014-09-23 09:50:21 -0700118 SkAutoTUnref<GrFragmentProcessor> fp(
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000119 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
120 GrTextureDomain::MakeTexelDomain(texture,
121 texelDomains[d]),
122 mode, GrTextureParams::kNone_FilterMode));
123
joshualittb0a8a372014-09-23 09:50:21 -0700124 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000125 continue;
126 }
127 SkMatrix viewMatrix;
128 viewMatrix.setTranslate(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800129 GrPipelineBuilder pipelineBuilder;
130 pipelineBuilder.setRenderTarget(rt);
131 pipelineBuilder.addColorProcessor(fp);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000132
egdaniel8dd688b2015-01-22 10:16:09 -0800133 tt.target()->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, viewMatrix,
134 renderRect);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000135 x += renderRect.width() + kTestPad;
136 }
137 y += renderRect.height() + kTestPad;
138 }
139 }
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000140 }
141
142private:
joshualitt5ae5fc52014-07-29 12:59:27 -0700143 static const SkScalar kDrawPad;
144 static const SkScalar kTestPad;
145 static const int kTargetWidth = 100;
146 static const int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000147 SkBitmap fBmp;
148
149 typedef GM INHERITED;
150};
151
joshualitt5ae5fc52014-07-29 12:59:27 -0700152// Windows builds did not like SkScalar initialization in class :(
153const SkScalar TextureDomainEffect::kDrawPad = 10.f;
154const SkScalar TextureDomainEffect::kTestPad = 10.f;
155
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000156DEF_GM( return SkNEW(TextureDomainEffect); )
157}
158
159#endif