blob: 5777b35f5ead79ceff965a257d53d776b772c6ce [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
commit-bot@chromium.org26632632014-03-25 15:13:18 +000012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050014#include "GrProxyProvider.h"
Brian Salomon6a639042016-12-14 11:08:17 -050015#include "GrRenderTargetContextPriv.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000016#include "SkGradientShader.h"
Brian Osman2700abc2018-09-12 10:19:41 -040017#include "SkImage.h"
18#include "SkSurface.h"
joshualitt04194f32016-01-13 10:08:27 -080019#include "effects/GrTextureDomain.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040021#include "ops/GrRectOpFactory.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 {
Brian Osman2700abc2018-09-12 10:19:41 -040046 // TODO: do this with gpu backend
Robert Phillips40fd7c92017-01-30 08:06:27 -050047 SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType,
48 kPremul_SkAlphaType);
Brian Osman2700abc2018-09-12 10:19:41 -040049 auto surface = SkSurface::MakeRaster(ii);
50 SkCanvas* canvas = surface->getCanvas();
51 canvas->clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000052 SkPaint paint;
53
54 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
reed1a9b9642016-03-13 14:13:58 -070055 paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
56 SK_ARRAY_COUNT(colors1)));
Brian Osman2700abc2018-09-12 10:19:41 -040057 canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
58 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000059
60 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
reed1a9b9642016-03-13 14:13:58 -070061 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
62 SK_ARRAY_COUNT(colors2)));
reed374772b2016-10-05 17:33:02 -070063 paint.setBlendMode(SkBlendMode::kDarken);
Brian Osman2700abc2018-09-12 10:19:41 -040064 canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
65 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000066
67 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
reed1a9b9642016-03-13 14:13:58 -070068 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
69 SK_ARRAY_COUNT(colors3)));
reed374772b2016-10-05 17:33:02 -070070 paint.setBlendMode(SkBlendMode::kLighten);
Brian Osman2700abc2018-09-12 10:19:41 -040071 canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
72 paint);
73
74 fImage = surface->makeImageSnapshot();
commit-bot@chromium.org26632632014-03-25 15:13:18 +000075 }
76
mtklein36352bf2015-03-25 18:17:31 -070077 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040078 GrRenderTargetContext* renderTargetContext =
79 canvas->internal_private_accessTopLayerRenderTargetContext();
80 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070081 skiagm::GM::DrawGpuOnlyMessage(canvas);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000082 return;
83 }
84
robertphillips175dd9b2016-04-28 14:32:04 -070085 GrContext* context = canvas->getGrContext();
86 if (!context) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000087 return;
88 }
89
Robert Phillips0bd24dc2018-01-16 08:06:32 -050090 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomon58389b92018-03-07 13:01:25 -050091 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(
Brian Osman2700abc2018-09-12 10:19:41 -040092 fImage, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips2f493142017-03-02 18:18:38 -050093 if (!proxy) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000094 return;
95 }
96
commit-bot@chromium.org26632632014-03-25 15:13:18 +000097 SkTArray<SkMatrix> textureMatrices;
Robert Phillips67c18d62017-01-20 12:44:06 -050098 textureMatrices.push_back() = SkMatrix::I();
99 textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f);
100 textureMatrices.push_back();
Robert Phillips40fd7c92017-01-30 08:06:27 -0500101 textureMatrices.back().setRotate(45.f, proxy->width() / 2.f, proxy->height() / 2.f);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000102
Brian Osman2700abc2018-09-12 10:19:41 -0400103
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000104 const SkIRect texelDomains[] = {
Brian Osman2700abc2018-09-12 10:19:41 -0400105 fImage->bounds(),
106 SkIRect::MakeXYWH(fImage->width() / 4, fImage->height() / 4,
107 fImage->width() / 2, fImage->height() / 2),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000108 };
109
Brian Osman2700abc2018-09-12 10:19:41 -0400110 SkRect renderRect = SkRect::Make(fImage->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;
robertphillips28a838e2016-06-23 14:07:00 -0700119 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500120 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400121 auto fp = GrTextureDomainEffect::Make(
Brian Osman2240be92017-10-18 13:15:13 -0400122 proxy, textureMatrices[tm],
Brian Salomonaff329b2017-08-11 09:40:37 -0400123 GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode), mode,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400124 GrSamplerState::Filter::kNearest);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000125
joshualittb0a8a372014-09-23 09:50:21 -0700126 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000127 continue;
128 }
robertphillips1d24b8d2015-03-26 19:57:08 -0700129 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700130 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -0400131 renderTargetContext->priv().testingOnly_addDrawOp(
Robert Phillips7c525e62018-06-12 10:11:12 -0400132 GrRectOpFactory::MakeNonAAFill(context, std::move(grPaint), viewMatrix,
Brian Salomonbaaf4392017-06-15 09:59:23 -0400133 renderRect, GrAAType::kNone));
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:
mtkleindbfd7ab2016-09-01 11:24:54 -0700142 static constexpr SkScalar kDrawPad = 10.f;
143 static constexpr SkScalar kTestPad = 10.f;;
144 static constexpr int kTargetWidth = 100;
145 static constexpr int kTargetHeight = 100;
Brian Osman2700abc2018-09-12 10:19:41 -0400146 sk_sp<SkImage> fImage;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000147
148 typedef GM INHERITED;
149};
150
halcanary385fe4d2015-08-26 13:07:48 -0700151DEF_GM(return new TextureDomainEffect;)
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000152}