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"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
|
|
|
|
#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 "SkBitmap.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkGradientShader.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"
|
2017-06-15 13:59:23 +00:00
|
|
|
#include "ops/GrRectOpFactory.h"
|
2014-03-25 15:13:18 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
/**
|
|
|
|
* This GM directly exercises GrTextureDomainEffect.
|
|
|
|
*/
|
|
|
|
class TextureDomainEffect : public GM {
|
|
|
|
public:
|
|
|
|
TextureDomainEffect() {
|
|
|
|
this->setBGColor(0xFFFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-03-25 15:13:18 +00:00
|
|
|
return SkString("texture_domain_effect");
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-01-30 13:06:27 +00:00
|
|
|
// TODO: do this with surfaces & images and gpu backend
|
|
|
|
SkImageInfo ii = SkImageInfo::Make(kTargetWidth, kTargetHeight, kN32_SkColorType,
|
|
|
|
kPremul_SkAlphaType);
|
|
|
|
fBmp.allocPixels(ii);
|
2014-03-25 15:13:18 +00:00
|
|
|
SkCanvas canvas(fBmp);
|
2014-04-07 21:18:46 +00:00
|
|
|
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)));
|
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 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);
|
2016-03-13 21:13:58 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 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);
|
2016-03-13 21:13:58 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f, fBmp.width() + 10.f, fBmp.height() + 10.f),
|
|
|
|
paint);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:06:32 +00:00
|
|
|
GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
|
2017-01-30 13:06:27 +00:00
|
|
|
GrSurfaceDesc desc;
|
2017-07-21 19:37:19 +00:00
|
|
|
desc.fOrigin = kTopLeft_GrSurfaceOrigin;
|
2017-01-30 13:06:27 +00:00
|
|
|
desc.fWidth = fBmp.width();
|
|
|
|
desc.fHeight = fBmp.height();
|
|
|
|
desc.fConfig = SkImageInfo2GrPixelConfig(fBmp.info(), *context->caps());
|
2018-02-21 18:02:32 +00:00
|
|
|
SkASSERT(kUnknown_GrPixelConfig != desc.fConfig);
|
2017-01-30 13:06:27 +00:00
|
|
|
|
2018-01-16 13:06:32 +00:00
|
|
|
sk_sp<GrTextureProxy> proxy = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes,
|
|
|
|
fBmp.getPixels(),
|
|
|
|
fBmp.rowBytes());
|
2017-03-02 23:18:38 +00:00
|
|
|
if (!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[] = {
|
2014-10-24 19:54:53 +00:00
|
|
|
fBmp.bounds(),
|
2017-01-30 13:06:27 +00:00
|
|
|
SkIRect::MakeXYWH(fBmp.width() / 4, fBmp.height() / 4,
|
|
|
|
fBmp.width() / 2, fBmp.height() / 2),
|
2014-03-25 15:13:18 +00:00
|
|
|
};
|
|
|
|
|
2014-10-24 19:54:53 +00:00
|
|
|
SkRect renderRect = SkRect::Make(fBmp.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;
|
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],
|
2017-08-11 13:40:37 +00:00
|
|
|
GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode), mode,
|
2017-09-07 16:36:34 +00:00
|
|
|
GrSamplerState::Filter::kNearest);
|
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(
|
2017-06-15 13:59:23 +00:00
|
|
|
GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
|
|
|
|
renderRect, GrAAType::kNone));
|
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;
|
|
|
|
static constexpr SkScalar kTestPad = 10.f;;
|
|
|
|
static constexpr int kTargetWidth = 100;
|
|
|
|
static constexpr int kTargetHeight = 100;
|
2014-03-25 15:13:18 +00:00
|
|
|
SkBitmap fBmp;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextureDomainEffect;)
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|