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.
|
|
|
|
*/
|
|
|
|
class TextureDomainEffect : public GM {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-10-27 18:47:55 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext =
|
|
|
|
canvas->internal_private_accessTopLayerRenderTargetContext();
|
|
|
|
if (!renderTargetContext) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:32:04 +00:00
|
|
|
GrContext* context = canvas->getGrContext();
|
|
|
|
if (!context) {
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (fFilter == GrSamplerState::Filter::kMipMap) {
|
|
|
|
SkBitmap copy;
|
|
|
|
SkImageInfo info = as_IB(fImage)->onImageInfo().makeColorType(kN32_SkColorType);
|
|
|
|
if (!copy.tryAllocPixels(info) || !fImage->readPixels(copy.pixmap(), 0, 0)) {
|
2019-02-04 08:20:52 +00:00
|
|
|
DrawFailureMessage(canvas, "Failed to read pixels.");
|
2018-12-17 14:50:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
proxy = proxyProvider->createMipMapProxyFromBitmap(copy);
|
|
|
|
} 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-04 08:20:52 +00:00
|
|
|
DrawFailureMessage(canvas, "Failed to create proxy.");
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|