blob: 660748873158c095d4d546fc1d86b0cddf28eb14 [file] [log] [blame]
commit-bot@chromium.org26632632014-03-25 15:13:18 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8// This test only works with the GPU backend.
9
10#include "gm.h"
11
12#if SK_SUPPORT_GPU
13
robertphillips391395d2016-03-02 09:26:36 -080014#include "GrDrawContextPriv.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000015#include "GrContext.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000016#include "SkBitmap.h"
17#include "SkGr.h"
18#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080019#include "batches/GrDrawBatch.h"
20#include "batches/GrRectBatchFactory.h"
21#include "effects/GrTextureDomain.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000022
23namespace skiagm {
24/**
25 * This GM directly exercises GrTextureDomainEffect.
26 */
27class TextureDomainEffect : public GM {
28public:
29 TextureDomainEffect() {
30 this->setBGColor(0xFFFFFFFF);
31 }
32
33protected:
mtklein36352bf2015-03-25 18:17:31 -070034 SkString onShortName() override {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000035 return SkString("texture_domain_effect");
36 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 SkISize onISize() override {
joshualitt5ae5fc52014-07-29 12:59:27 -070039 const SkScalar canvasWidth = kDrawPad +
40 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
41 kTestPad * GrTextureDomain::kModeCount;
42 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000043 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onOnceBeforeDraw() override {
joshualitt5ae5fc52014-07-29 12:59:27 -070046 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000047 SkCanvas canvas(fBmp);
commit-bot@chromium.orgc5713e42014-04-07 21:18:46 +000048 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000049 SkPaint paint;
50
51 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
reed1a9b9642016-03-13 14:13:58 -070052 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),
55 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000056
57 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
reed1a9b9642016-03-13 14:13:58 -070058 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
59 SK_ARRAY_COUNT(colors2)));
commit-bot@chromium.org26632632014-03-25 15:13:18 +000060 paint.setXfermodeMode(SkXfermode::kDarken_Mode);
reed1a9b9642016-03-13 14:13:58 -070061 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
62 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000063
64 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
reed1a9b9642016-03-13 14:13:58 -070065 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
66 SK_ARRAY_COUNT(colors3)));
commit-bot@chromium.org26632632014-03-25 15:13:18 +000067 paint.setXfermodeMode(SkXfermode::kLighten_Mode);
reed1a9b9642016-03-13 14:13:58 -070068 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
69 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000070 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -070073 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
74 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -070075 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000076 return;
77 }
78
robertphillips175dd9b2016-04-28 14:32:04 -070079 GrContext* context = canvas->getGrContext();
80 if (!context) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000081 return;
82 }
83
bsalomonafa95e22015-10-12 10:39:46 -070084 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp,
85 GrTextureParams::ClampNoFilter()));
bsalomonbcf0a522014-10-08 08:40:09 -070086 if (!texture) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000087 return;
88 }
89
commit-bot@chromium.org26632632014-03-25 15:13:18 +000090 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);
96
97 const SkIRect texelDomains[] = {
halcanaryf622a6c2014-10-24 12:54:53 -070098 fBmp.bounds(),
commit-bot@chromium.org26632632014-03-25 15:13:18 +000099 SkIRect::MakeXYWH(fBmp.width() / 4,
100 fBmp.height() / 4,
101 fBmp.width() / 2,
102 fBmp.height() / 2),
103 };
104
halcanaryf622a6c2014-10-24 12:54:53 -0700105 SkRect renderRect = SkRect::Make(fBmp.bounds());
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000106 renderRect.outset(kDrawPad, kDrawPad);
107
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;
joshualitt5f10b5c2015-07-09 10:24:35 -0700114 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800115 pipelineBuilder.setXPFactory(
116 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomon0ba8c242015-10-07 09:20:28 -0700117 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -0700118 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000119 GrTextureDomain::MakeTexelDomain(texture,
120 texelDomains[d]),
121 mode, GrTextureParams::kNone_FilterMode));
122
joshualittb0a8a372014-09-23 09:50:21 -0700123 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000124 continue;
125 }
robertphillips1d24b8d2015-03-26 19:57:08 -0700126 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
robertphillips175dd9b2016-04-28 14:32:04 -0700127 pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
bsalomonac856c92015-08-27 06:30:17 -0700128 pipelineBuilder.addColorFragmentProcessor(fp);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000129
joshualitt04194f32016-01-13 10:08:27 -0800130 SkAutoTUnref<GrDrawBatch> batch(
131 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
132 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800133 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000134 x += renderRect.width() + kTestPad;
135 }
136 y += renderRect.height() + kTestPad;
137 }
138 }
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000139 }
140
141private:
joshualitt5ae5fc52014-07-29 12:59:27 -0700142 static const SkScalar kDrawPad;
143 static const SkScalar kTestPad;
144 static const int kTargetWidth = 100;
145 static const int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000146 SkBitmap fBmp;
147
148 typedef GM INHERITED;
149};
150
joshualitt5ae5fc52014-07-29 12:59:27 -0700151// Windows builds did not like SkScalar initialization in class :(
152const SkScalar TextureDomainEffect::kDrawPad = 10.f;
153const SkScalar TextureDomainEffect::kTestPad = 10.f;
154
halcanary385fe4d2015-08-26 13:07:48 -0700155DEF_GM(return new TextureDomainEffect;)
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000156}
157
158#endif