blob: b6d7cbaa02d8a36baf50935af958935dde59c22e [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
14#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.h"
Brian Salomon6a639042016-12-14 11:08:17 -050017#include "GrRenderTargetContextPriv.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000018#include "SkBitmap.h"
19#include "SkGr.h"
20#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080021#include "effects/GrTextureDomain.h"
Brian Salomon89527432016-12-16 09:52:16 -050022#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040023#include "ops/GrRectOpFactory.h"
commit-bot@chromium.org26632632014-03-25 15:13:18 +000024
25namespace skiagm {
26/**
27 * This GM directly exercises GrTextureDomainEffect.
28 */
29class TextureDomainEffect : public GM {
30public:
31 TextureDomainEffect() {
32 this->setBGColor(0xFFFFFFFF);
33 }
34
35protected:
mtklein36352bf2015-03-25 18:17:31 -070036 SkString onShortName() override {
commit-bot@chromium.org26632632014-03-25 15:13:18 +000037 return SkString("texture_domain_effect");
38 }
39
mtklein36352bf2015-03-25 18:17:31 -070040 SkISize onISize() override {
joshualitt5ae5fc52014-07-29 12:59:27 -070041 const SkScalar canvasWidth = kDrawPad +
42 (kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
43 kTestPad * GrTextureDomain::kModeCount;
44 return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000045 }
46
mtklein36352bf2015-03-25 18:17:31 -070047 void onOnceBeforeDraw() override {
Robert Phillips40fd7c92017-01-30 08:06:27 -050048 // TODO: do this with surfaces & images and gpu backend
49 SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType,
50 kPremul_SkAlphaType);
51 fBmp.allocPixels(ii);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000052 SkCanvas canvas(fBmp);
commit-bot@chromium.orgc5713e42014-04-07 21:18:46 +000053 canvas.clear(0x00000000);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000054 SkPaint paint;
55
56 SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
reed1a9b9642016-03-13 14:13:58 -070057 paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
58 SK_ARRAY_COUNT(colors1)));
59 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
60 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000061
62 SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
reed1a9b9642016-03-13 14:13:58 -070063 paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
64 SK_ARRAY_COUNT(colors2)));
reed374772b2016-10-05 17:33:02 -070065 paint.setBlendMode(SkBlendMode::kDarken);
reed1a9b9642016-03-13 14:13:58 -070066 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
67 paint);
commit-bot@chromium.org26632632014-03-25 15:13:18 +000068
69 SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
reed1a9b9642016-03-13 14:13:58 -070070 paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
71 SK_ARRAY_COUNT(colors3)));
reed374772b2016-10-05 17:33:02 -070072 paint.setBlendMode(SkBlendMode::kLighten);
reed1a9b9642016-03-13 14:13:58 -070073 canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
74 paint);
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();
Robert Phillips40fd7c92017-01-30 08:06:27 -050091 GrSurfaceDesc desc;
92 desc.fWidth = fBmp.width();
93 desc.fHeight = fBmp.height();
94 desc.fConfig = SkImageInfo2GrPixelConfig(fBmp.info(), *context->caps());
Greg Daniel0a7aa142018-02-21 13:02:32 -050095 SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
Robert Phillips40fd7c92017-01-30 08:06:27 -050096
Brian Salomon58389b92018-03-07 13:01:25 -050097 sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(
98 desc, SkBudgeted::kYes, fBmp.getPixels(), fBmp.rowBytes());
Robert Phillips2f493142017-03-02 18:18:38 -050099 if (!proxy) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000100 return;
101 }
102
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000103 SkTArray<SkMatrix> textureMatrices;
Robert Phillips67c18d62017-01-20 12:44:06 -0500104 textureMatrices.push_back() = SkMatrix::I();
105 textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f);
106 textureMatrices.push_back();
Robert Phillips40fd7c92017-01-30 08:06:27 -0500107 textureMatrices.back().setRotate(45.f, proxy->width() / 2.f, proxy->height() / 2.f);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000108
109 const SkIRect texelDomains[] = {
halcanaryf622a6c2014-10-24 12:54:53 -0700110 fBmp.bounds(),
Robert Phillips40fd7c92017-01-30 08:06:27 -0500111 SkIRect::MakeXYWH(fBmp.width() / 4, fBmp.height() / 4,
112 fBmp.width() / 2, fBmp.height() / 2),
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000113 };
114
halcanaryf622a6c2014-10-24 12:54:53 -0700115 SkRect renderRect = SkRect::Make(fBmp.bounds());
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000116 renderRect.outset(kDrawPad, kDrawPad);
117
118 SkScalar y = kDrawPad + kTestPad;
119 for (int tm = 0; tm < textureMatrices.count(); ++tm) {
120 for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
121 SkScalar x = kDrawPad + kTestPad;
122 for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
123 GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
robertphillips28a838e2016-06-23 14:07:00 -0700124 GrPaint grPaint;
Brian Salomona1633922017-01-09 11:46:10 -0500125 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400126 auto fp = GrTextureDomainEffect::Make(
Brian Osman2240be92017-10-18 13:15:13 -0400127 proxy, textureMatrices[tm],
Brian Salomonaff329b2017-08-11 09:40:37 -0400128 GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode), mode,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400129 GrSamplerState::Filter::kNearest);
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000130
joshualittb0a8a372014-09-23 09:50:21 -0700131 if (!fp) {
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000132 continue;
133 }
robertphillips1d24b8d2015-03-26 19:57:08 -0700134 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
robertphillips28a838e2016-06-23 14:07:00 -0700135 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -0400136 renderTargetContext->priv().testingOnly_addDrawOp(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400137 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
138 renderRect, GrAAType::kNone));
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:
mtkleindbfd7ab2016-09-01 11:24:54 -0700147 static constexpr SkScalar kDrawPad = 10.f;
148 static constexpr SkScalar kTestPad = 10.f;;
149 static constexpr int kTargetWidth = 100;
150 static constexpr int kTargetHeight = 100;
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000151 SkBitmap fBmp;
152
153 typedef GM INHERITED;
154};
155
halcanary385fe4d2015-08-26 13:07:48 -0700156DEF_GM(return new TextureDomainEffect;)
commit-bot@chromium.org26632632014-03-25 15:13:18 +0000157}
158
159#endif