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.
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "GrContext.h"
|
2018-01-08 18:40:32 +00:00
|
|
|
#include "GrContextPriv.h"
|
2018-01-16 13:06:32 +00:00
|
|
|
#include "GrProxyProvider.h"
|
2016-12-14 16:08:17 +00:00
|
|
|
#include "GrRenderTargetContextPriv.h"
|
2014-03-25 15:13:18 +00:00
|
|
|
#include "SkGradientShader.h"
|
2018-09-12 14:19:41 +00:00
|
|
|
#include "SkImage.h"
|
2018-12-17 14:50:51 +00:00
|
|
|
#include "SkImage_Base.h"
|
2018-09-12 14:19:41 +00:00
|
|
|
#include "SkSurface.h"
|
2016-01-13 18:08:27 +00:00
|
|
|
#include "effects/GrTextureDomain.h"
|
2016-12-16 14:52:16 +00:00
|
|
|
#include "ops/GrDrawOp.h"
|
2018-12-10 17:43:36 +00:00
|
|
|
#include "ops/GrFillRectOp.h"
|
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 {
|
2018-09-12 14:19:41 +00:00
|
|
|
// TODO: do this with gpu backend
|
2017-01-30 13:06:27 +00:00
|
|
|
SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
2018-09-12 14:19:41 +00:00
|
|
|
auto surface = SkSurface::MakeRaster(ii);
|
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
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)));
|
2018-09-12 14:19:41 +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);
|
2018-09-12 14:19:41 +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);
|
2018-09-12 14:19:41 +00:00
|
|
|
canvas->drawOval(SkRect::MakeXYWH(-5.f, -5.f, kTargetWidth + 10.f, kTargetHeight + 10.f),
|
|
|
|
paint);
|
|
|
|
|
|
|
|
fImage = surface->makeImageSnapshot();
|
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 16:20:55 +00:00
|
|
|
if (fFilter == GrSamplerState::Filter::kMipMap && context->priv().caps()->mipMapSupport()) {
|
2018-12-17 14:50:51 +00:00
|
|
|
SkBitmap copy;
|
2019-03-21 19:30:08 +00:00
|
|
|
SkImageInfo info = fImage->imageInfo().makeColorType(kN32_SkColorType);
|
2018-12-17 14:50:51 +00:00
|
|
|
if (!copy.tryAllocPixels(info) || !fImage->readPixels(copy.pixmap(), 0, 0)) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Failed to read pixels.";
|
|
|
|
return DrawResult::kFail;
|
2018-12-17 14:50:51 +00:00
|
|
|
}
|
2019-03-22 17:42:33 +00:00
|
|
|
proxy = proxyProvider->createProxyFromBitmap(copy, GrMipMapped::kYes);
|
2018-12-17 14:50:51 +00:00
|
|
|
} else {
|
|
|
|
proxy = proxyProvider->createTextureProxy(
|
2018-09-12 14:19:41 +00:00
|
|
|
fImage, kNone_GrSurfaceFlags, 1, SkBudgeted::kYes, SkBackingFit::kExact);
|
2018-12-17 14:50:51 +00:00
|
|
|
}
|
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[] = {
|
2018-09-12 14:19:41 +00:00
|
|
|
fImage->bounds(),
|
2018-12-17 14:50:51 +00:00
|
|
|
SkIRect::MakeXYWH(fImage->width() / 4 - 1, fImage->height() / 4 - 1,
|
|
|
|
fImage->width() / 2 + 2, fImage->height() / 2 + 2),
|
2014-03-25 15:13:18 +00:00
|
|
|
};
|
|
|
|
|
2018-09-12 14:19:41 +00:00
|
|
|
SkRect renderRect = SkRect::Make(fImage->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(
|
2018-12-10 17:43:36 +00:00
|
|
|
GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone,
|
|
|
|
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;
|
2018-09-12 14:19:41 +00:00
|
|
|
sk_sp<SkImage> fImage;
|
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
|
|
|
}
|