blob: 6bd6490c660ba7064bff0a873b632474210832b0 [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
robertphillips391395d2016-03-02 09:26:36 -080015#include "GrDrawContextPriv.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000016#include "GrContext.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000017#include "SkBitmap.h"
18#include "SkGr.h"
19#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080020#include "batches/GrDrawBatch.h"
21#include "batches/GrRectBatchFactory.h"
22#include "effects/GrTextureDomain.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000023
24namespace skiagm {
25/**
26 * This GM directly exercises GrTextureDomainEffect.
27 */
28class TextureDomainEffect : public GM {
29public:
30 TextureDomainEffect() {
31 this->setBGColor(0xFFFFFFFF);
32 }
33
34protected:
mtklein36352bf2015-03-25 18:17:31 -070035 SkString onShortName() override {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000036 return SkString("texture_domain_effect");
37 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 SkISize onISize() override {
joshualitt5ae5fc52014-07-29 12:59:27 -070040 const SkScalar canvasWidth = kDrawPad +
41 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
42 kTestPad * GrTextureDomain::kModeCount;
43 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000044 }
45
mtklein36352bf2015-03-25 18:17:31 -070046 void onOnceBeforeDraw() override {
joshualitt5ae5fc52014-07-29 12:59:27 -070047 fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000048 SkCanvas canvas(fBmp);
commit-bot@chromium.orgc5713e42014-04-07 21:18:46 +000049 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000050 SkPaint paint;
51
52 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
reed1a9b9642016-03-13 14:13:58 -070053 paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
54 SK_ARRAY_COUNT(colors1)));
55 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
56 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000057
58 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
reed1a9b9642016-03-13 14:13:58 -070059 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
60 SK_ARRAY_COUNT(colors2)));
commit-bot@chromium.org26632632014-03-25 15:13:18 +000061 paint.setXfermodeMode(SkXfermode::kDarken_Mode);
reed1a9b9642016-03-13 14:13:58 -070062 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
63 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000064
65 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
reed1a9b9642016-03-13 14:13:58 -070066 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
67 SK_ARRAY_COUNT(colors3)));
commit-bot@chromium.org26632632014-03-25 15:13:18 +000068 paint.setXfermodeMode(SkXfermode::kLighten_Mode);
reed1a9b9642016-03-13 14:13:58 -070069 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
70 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000071 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000074 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070075 if (nullptr == rt) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000076 return;
77 }
78 GrContext* context = rt->getContext();
halcanary96fcdcc2015-08-27 07:41:13 -070079 if (nullptr == context) {
halcanary2a243382015-09-09 08:16:41 -070080 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000081 return;
82 }
83
joshualitt04194f32016-01-13 10:08:27 -080084 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
85 if (!drawContext) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000086 return;
87 }
88
bsalomonafa95e22015-10-12 10:39:46 -070089 SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp,
90 GrTextureParams::ClampNoFilter()));
bsalomonbcf0a522014-10-08 08:40:09 -070091 if (!texture) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000092 return;
93 }
94
commit-bot@chromium.org26632632014-03-25 15:13:18 +000095 SkTArray<SkMatrix> textureMatrices;
96 textureMatrices.push_back().setIDiv(texture->width(), texture->height());
97 textureMatrices.push_back() = textureMatrices[0];
98 textureMatrices.back().postScale(1.5f, 0.85f);
99 textureMatrices.push_back() = textureMatrices[0];
100 textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
101
102 const SkIRect texelDomains[] = {
halcanaryf622a6c2014-10-24 12:54:53 -0700103 fBmp.bounds(),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000104 SkIRect::MakeXYWH(fBmp.width() / 4,
105 fBmp.height() / 4,
106 fBmp.width() / 2,
107 fBmp.height() / 2),
108 };
109
halcanaryf622a6c2014-10-24 12:54:53 -0700110 SkRect renderRect = SkRect::Make(fBmp.bounds());
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000111 renderRect.outset(kDrawPad, kDrawPad);
112
113 SkScalar y = kDrawPad + kTestPad;
114 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
115 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
116 SkScalar x = kDrawPad + kTestPad;
117 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
118 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
joshualitt5f10b5c2015-07-09 10:24:35 -0700119 GrPipelineBuilder pipelineBuilder;
egdanielc4b72722015-11-23 13:20:41 -0800120 pipelineBuilder.setXPFactory(
121 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomon0ba8c242015-10-07 09:20:28 -0700122 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomon4a339522015-10-06 08:40:50 -0700123 GrTextureDomainEffect::Create(texture, textureMatrices[tm],
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000124 GrTextureDomain::MakeTexelDomain(texture,
125 texelDomains[d]),
126 mode, GrTextureParams::kNone_FilterMode));
127
joshualittb0a8a372014-09-23 09:50:21 -0700128 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000129 continue;
130 }
robertphillips1d24b8d2015-03-26 19:57:08 -0700131 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
egdaniel8dd688b2015-01-22 10:16:09 -0800132 pipelineBuilder.setRenderTarget(rt);
bsalomonac856c92015-08-27 06:30:17 -0700133 pipelineBuilder.addColorFragmentProcessor(fp);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000134
joshualitt04194f32016-01-13 10:08:27 -0800135 SkAutoTUnref<GrDrawBatch> batch(
136 GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
137 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800138 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000139 x += renderRect.width() + kTestPad;
140 }
141 y += renderRect.height() + kTestPad;
142 }
143 }
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000144 }
145
146private:
joshualitt5ae5fc52014-07-29 12:59:27 -0700147 static const SkScalar kDrawPad;
148 static const SkScalar kTestPad;
149 static const int kTargetWidth = 100;
150 static const int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000151 SkBitmap fBmp;
152
153 typedef GM INHERITED;
154};
155
joshualitt5ae5fc52014-07-29 12:59:27 -0700156// Windows builds did not like SkScalar initialization in class :(
157const SkScalar TextureDomainEffect::kDrawPad = 10.f;
158const SkScalar TextureDomainEffect::kTestPad = 10.f;
159
halcanary385fe4d2015-08-26 13:07:48 -0700160DEF_GM(return new TextureDomainEffect;)
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000161}
162
163#endif