2014-03-25 15:13:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This test only works with the GPU backend.
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/gpu/GrTypes.h"
|
|
|
|
#include "include/private/GrTypesPriv.h"
|
|
|
|
#include "include/private/SkTArray.h"
|
|
|
|
#include "src/gpu/GrCaps.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrContextPriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrFragmentProcessor.h"
|
|
|
|
#include "src/gpu/GrPaint.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrProxyProvider.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/GrRenderTargetContextPriv.h"
|
2019-08-14 21:00:30 +00:00
|
|
|
#include "src/gpu/GrSamplerState.h"
|
2019-06-18 13:58:02 +00:00
|
|
|
#include "src/gpu/GrTextureProxy.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/effects/GrTextureDomain.h"
|
|
|
|
#include "src/gpu/ops/GrDrawOp.h"
|
|
|
|
#include "src/gpu/ops/GrFillRectOp.h"
|
2014-03-25 15:13:18 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
|
2014-03-25 15:13:18 +00:00
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrTextureDomainEffect.
|
|
|
|
*/
|
2019-02-07 22:23:36 +00:00
|
|
|
class TextureDomainEffect : public GpuGM {
|
2014-03-25 15:13:18 +00:00
|
|
|
public:
|
2018-12-17 14:50:51 +00:00
|
|
|
TextureDomainEffect(GrSamplerState::Filter filter)
|
|
|
|
: fFilter(filter) {
|
2014-03-25 15:13:18 +00:00
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2018-12-17 14:50:51 +00:00
|
|
|
SkString name("texture_domain_effect");
|
|
|
|
if (fFilter == GrSamplerState::Filter::kBilerp) {
|
|
|
|
name.append("_bilerp");
|
|
|
|
} else if (fFilter == GrSamplerState::Filter::kMipMap) {
|
|
|
|
name.append("_mipmap");
|
|
|
|
}
|
|
|
|
return name;
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-07-29 19:59:27 +00:00
|
|
|
const SkScalar canvasWidth = kDrawPad +
|
|
|
|
(kTargetWidth + 2 * kDrawPad) * GrTextureDomain::kModeCount +
|
|
|
|
kTestPad * GrTextureDomain::kModeCount;
|
|
|
|
return SkISize::Make(SkScalarCeilToInt(canvasWidth), 800);
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-25 17:42:35 +00:00
|
|
|
fBitmap.allocN32Pixels(kTargetWidth, kTargetHeight);
|
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
canvas.clear(0x00000000);
|
2014-03-25 15:13:18 +00:00
|
|
|
SkPaint paint;
|
|
|
|
|
|
|
|
SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeSweep(65.f, 75.f, colors1, nullptr,
|
|
|
|
SK_ARRAY_COUNT(colors1)));
|
2019-03-25 17:42:35 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
|
|
|
|
paint);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
|
|
|
SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeSweep(45.f, 55.f, colors2, nullptr,
|
|
|
|
SK_ARRAY_COUNT(colors2)));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kDarken);
|
2019-03-25 17:42:35 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
|
|
|
|
paint);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
|
|
|
SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeSweep(25.f, 35.f, colors3, nullptr,
|
|
|
|
SK_ARRAY_COUNT(colors3)));
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kLighten);
|
2019-03-25 17:42:35 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
|
|
|
|
paint);
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
|
|
|
|
SkCanvas* canvas, SkString* errorMsg) override {
|
2019-02-04 18:26:26 +00:00
|
|
|
GrProxyProvider* proxyProvider = context->priv().proxyProvider();
|
2018-12-17 14:50:51 +00:00
|
|
|
sk_sp<GrTextureProxy> proxy;
|
2019-03-25 17:42:35 +00:00
|
|
|
GrMipMapped mipMapped = fFilter == GrSamplerState::Filter::kMipMap &&
|
|
|
|
context->priv().caps()->mipMapSupport()
|
|
|
|
? GrMipMapped::kYes : GrMipMapped::kNo;
|
|
|
|
proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped);
|
2017-03-02 23:18:38 +00:00
|
|
|
if (!proxy) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Failed to create proxy.";
|
|
|
|
return DrawResult::kFail;
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<SkMatrix> textureMatrices;
|
2017-01-20 17:44:06 +00:00
|
|
|
textureMatrices.push_back() = SkMatrix::I();
|
|
|
|
textureMatrices.push_back() = SkMatrix::MakeScale(1.5f, 0.85f);
|
|
|
|
textureMatrices.push_back();
|
2017-01-30 13:06:27 +00:00
|
|
|
textureMatrices.back().setRotate(45.f, proxy->width() / 2.f, proxy->height() / 2.f);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
|
|
|
const SkIRect texelDomains[] = {
|
2019-03-25 17:42:35 +00:00
|
|
|
fBitmap.bounds(),
|
|
|
|
SkIRect::MakeXYWH(fBitmap.width() / 4 - 1, fBitmap.height() / 4 - 1,
|
|
|
|
fBitmap.width() / 2 + 2, fBitmap.height() / 2 + 2),
|
2014-03-25 15:13:18 +00:00
|
|
|
};
|
|
|
|
|
2019-03-25 17:42:35 +00:00
|
|
|
SkRect renderRect = SkRect::Make(fBitmap.bounds());
|
2014-03-25 15:13:18 +00:00
|
|
|
renderRect.outset(kDrawPad, kDrawPad);
|
|
|
|
|
|
|
|
SkScalar y = kDrawPad + kTestPad;
|
|
|
|
for (int tm = 0; tm < textureMatrices.count(); ++tm) {
|
|
|
|
for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
|
|
|
|
SkScalar x = kDrawPad + kTestPad;
|
|
|
|
for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
|
|
|
|
GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
|
2018-12-17 14:50:51 +00:00
|
|
|
if (fFilter != GrSamplerState::Filter::kNearest &&
|
|
|
|
mode == GrTextureDomain::kRepeat_Mode) {
|
|
|
|
// Repeat mode doesn't produce correct results with bilerp filtering
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:07:00 +00:00
|
|
|
GrPaint grPaint;
|
2017-01-09 16:46:10 +00:00
|
|
|
grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
|
2017-08-11 13:40:37 +00:00
|
|
|
auto fp = GrTextureDomainEffect::Make(
|
2017-10-18 17:15:13 +00:00
|
|
|
proxy, textureMatrices[tm],
|
2018-12-17 14:50:51 +00:00
|
|
|
GrTextureDomain::MakeTexelDomain(texelDomains[d], mode),
|
|
|
|
mode, fFilter);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
2014-09-23 16:50:21 +00:00
|
|
|
if (!fp) {
|
2014-03-25 15:13:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-27 02:57:08 +00:00
|
|
|
const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
|
2016-06-23 21:07:00 +00:00
|
|
|
grPaint.addColorFragmentProcessor(std::move(fp));
|
2017-05-08 14:43:33 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(
|
2019-05-29 18:43:13 +00:00
|
|
|
GrFillRectOp::MakeNonAARect(context, std::move(grPaint),
|
|
|
|
viewMatrix, renderRect));
|
2014-03-25 15:13:18 +00:00
|
|
|
x += renderRect.width() + kTestPad;
|
|
|
|
}
|
|
|
|
y += renderRect.height() + kTestPad;
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr SkScalar kDrawPad = 10.f;
|
2019-01-04 13:54:32 +00:00
|
|
|
static constexpr SkScalar kTestPad = 10.f;
|
2016-09-01 18:24:54 +00:00
|
|
|
static constexpr int kTargetWidth = 100;
|
|
|
|
static constexpr int kTargetHeight = 100;
|
2019-03-25 17:42:35 +00:00
|
|
|
SkBitmap fBitmap;
|
2018-12-17 14:50:51 +00:00
|
|
|
GrSamplerState::Filter fFilter;
|
2014-03-25 15:13:18 +00:00
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2018-12-17 14:50:51 +00:00
|
|
|
DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kNearest);)
|
|
|
|
DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kBilerp);)
|
|
|
|
DEF_GM(return new TextureDomainEffect(GrSamplerState::Filter::kMipMap);)
|
|
|
|
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|