blob: e7d08cdfbd9e678f5962f97f3d22f8c4c4d6b018 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkCanvas.h"
13#include "include/core/SkColor.h"
14#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkGradientShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/private/GrTypesPriv.h"
21#include "include/private/SkTArray.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrContextPriv.h"
23#include "src/gpu/GrProxyProvider.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrRenderTargetContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040026#include "src/gpu/GrSamplerState.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040027#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/effects/GrTextureDomain.h"
Brian Salomone21af502019-11-22 16:56:36 -050029#include "tools/gpu/TestOps.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000030
Ben Wagner7fde8e12019-05-01 17:28:53 -040031#include <memory>
32#include <utility>
33
commit-bot@chromium.org26632632014-03-25 15:13:18 +000034namespace skiagm {
35/**
Brian Salomon7eabfe82019-12-02 14:20:20 -050036 * This GM directly exercises GrDomainEffect.
commit-bot@chromium.org26632632014-03-25 15:13:18 +000037 */
Chris Dalton3a778372019-02-07 15:23:36 -070038class TextureDomainEffect : public GpuGM {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000039public:
Brian Salomone21af502019-11-22 16:56:36 -050040 TextureDomainEffect(GrSamplerState::Filter filter) : fFilter(filter) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000041 this->setBGColor(0xFFFFFFFF);
42 }
43
44protected:
mtklein36352bf2015-03-25 18:17:31 -070045 SkString onShortName() override {
Michael Ludwigbe315a22018-12-17 09:50:51 -050046 SkString name("texture_domain_effect");
47 if (fFilter == GrSamplerState::Filter::kBilerp) {
48 name.append("_bilerp");
49 } else if (fFilter == GrSamplerState::Filter::kMipMap) {
50 name.append("_mipmap");
51 }
52 return name;
commit-bot@chromium.org26632632014-03-25 15:13:18 +000053 }
54
mtklein36352bf2015-03-25 18:17:31 -070055 SkISize onISize() override {
Brian Salomone21af502019-11-22 16:56:36 -050056 const SkScalar canvasWidth =
57 kDrawPad + 2 * ((kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
58 kTestPad * GrTextureDomain::kModeCount);
joshualitt5ae5fc52014-07-29 12:59:27 -070059 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000060 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 void onOnceBeforeDraw() override {
Brian Osman65f33442019-03-25 13:42:35 -040063 fBitmap.allocN32Pixels(kTargetWidth, kTargetHeight);
64 SkCanvas canvas(fBitmap);
65 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000066 SkPaint paint;
67
68 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
reed1a9b9642016-03-13 14:13:58 -070069 paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
70 SK_ARRAY_COUNT(colors1)));
Brian Osman65f33442019-03-25 13:42:35 -040071 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
72 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000073
74 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
reed1a9b9642016-03-13 14:13:58 -070075 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
76 SK_ARRAY_COUNT(colors2)));
reed374772b2016-10-05 17:33:02 -070077 paint.setBlendMode(SkBlendMode::kDarken);
Brian Osman65f33442019-03-25 13:42:35 -040078 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
79 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000080
81 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
reed1a9b9642016-03-13 14:13:58 -070082 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
83 SK_ARRAY_COUNT(colors3)));
reed374772b2016-10-05 17:33:02 -070084 paint.setBlendMode(SkBlendMode::kLighten);
Brian Osman65f33442019-03-25 13:42:35 -040085 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
86 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000087 }
88
Chris Dalton50e24d72019-02-07 16:20:09 -070089 DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
90 SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips9da87e02019-02-04 13:26:26 -050091 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Michael Ludwigbe315a22018-12-17 09:50:51 -050092 sk_sp<GrTextureProxy> proxy;
Brian Osman65f33442019-03-25 13:42:35 -040093 GrMipMapped mipMapped = fFilter == GrSamplerState::Filter::kMipMap &&
94 context->priv().caps()->mipMapSupport()
95 ? GrMipMapped::kYes : GrMipMapped::kNo;
96 proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped);
Robert Phillips2f493142017-03-02 18:18:38 -050097 if (!proxy) {
Chris Dalton50e24d72019-02-07 16:20:09 -070098 *errorMsg = "Failed to create proxy.";
99 return DrawResult::kFail;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000100 }
101
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000102 SkTArray<SkMatrix> textureMatrices;
Robert Phillips67c18d62017-01-20 12:44:06 -0500103 textureMatrices.push_back() = SkMatrix::I();
104 textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f);
105 textureMatrices.push_back();
Robert Phillips40fd7c92017-01-30 08:06:27 -0500106 textureMatrices.back().setRotate(45.f, proxy->width() / 2.f, proxy->height() / 2.f);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000107
108 const SkIRect texelDomains[] = {
Brian Osman65f33442019-03-25 13:42:35 -0400109 fBitmap.bounds(),
110 SkIRect::MakeXYWH(fBitmap.width() / 4 - 1, fBitmap.height() / 4 - 1,
111 fBitmap.width() / 2 + 2, fBitmap.height() / 2 + 2),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000112 };
113
Brian Salomone21af502019-11-22 16:56:36 -0500114 SkRect localRect = SkRect::Make(fBitmap.bounds()).makeOutset(kDrawPad, kDrawPad);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000115
116 SkScalar y = kDrawPad + kTestPad;
117 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
118 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
119 SkScalar x = kDrawPad + kTestPad;
120 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
121 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
Michael Ludwigbe315a22018-12-17 09:50:51 -0500122 if (fFilter != GrSamplerState::Filter::kNearest &&
123 mode == GrTextureDomain::kRepeat_Mode) {
124 // Repeat mode doesn't produce correct results with bilerp filtering
125 continue;
126 }
Brian Salomon7eabfe82019-12-02 14:20:20 -0500127 auto fp1 = GrSimpleTextureEffect::Make(proxy, fBitmap.alphaType(),
128 textureMatrices[tm], fFilter);
129 fp1 = GrDomainEffect::Make(
130 std::move(fp1), GrTextureDomain::MakeTexelDomain(texelDomains[d], mode),
131 mode, fFilter);
Brian Salomone21af502019-11-22 16:56:36 -0500132 if (!fp1) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000133 continue;
134 }
Brian Salomone21af502019-11-22 16:56:36 -0500135 auto fp2 = fp1->clone();
136 SkASSERT(fp2);
137 auto drawRect = localRect.makeOffset(x, y);
138 if (auto op = sk_gpu_test::test_ops::MakeRect(
139 context, std::move(fp1), drawRect, localRect)) {
140 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
141 }
142 x += localRect.width() + kTestPad;
143 // Draw again with a translated local rect and compensating translate matrix.
144 drawRect = localRect.makeOffset(x, y);
145 static constexpr SkVector kT = {-100, 300};
146 if (auto op = sk_gpu_test::test_ops::MakeRect(context,
147 std::move(fp2),
148 drawRect,
149 localRect.makeOffset(kT),
150 SkMatrix::MakeTrans(-kT))) {
151 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
152 }
153 x += localRect.width() + kTestPad;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000154 }
Brian Salomone21af502019-11-22 16:56:36 -0500155 y += localRect.height() + kTestPad;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000156 }
157 }
Chris Dalton50e24d72019-02-07 16:20:09 -0700158 return DrawResult::kOk;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000159 }
160
161private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 static constexpr SkScalar kDrawPad = 10.f;
Kevin Lubick48ce5432019-01-04 08:54:32 -0500163 static constexpr SkScalar kTestPad = 10.f;
mtkleindbfd7ab2016-09-01 11:24:54 -0700164 static constexpr int kTargetWidth = 100;
165 static constexpr int kTargetHeight = 100;
Brian Osman65f33442019-03-25 13:42:35 -0400166 SkBitmap fBitmap;
Michael Ludwigbe315a22018-12-17 09:50:51 -0500167 GrSamplerState::Filter fFilter;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000168
169 typedef GM INHERITED;
170};
171
Michael Ludwigbe315a22018-12-17 09:50:51 -0500172DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kNearest);)
173DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kBilerp);)
174DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kMipMap);)
175
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000176}