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"
|
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"
|
|
|
|
#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 {
|
2014-07-29 19:59:27 +00:00
|
|
|
fBmp.allocN32Pixels(kTargetWidth, kTargetHeight);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-08 22:08:54 +00:00
|
|
|
sk_sp<GrTexture> texture(
|
2016-12-09 19:51:59 +00:00
|
|
|
GrRefCachedBitmapTexture(context, fBmp, GrSamplerParams::ClampNoFilter()));
|
2014-10-08 15:40:09 +00:00
|
|
|
if (!texture) {
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTArray<SkMatrix> textureMatrices;
|
|
|
|
textureMatrices.push_back().setIDiv(texture->width(), texture->height());
|
|
|
|
textureMatrices.push_back() = textureMatrices[0];
|
|
|
|
textureMatrices.back().postScale(1.5f, 0.85f);
|
|
|
|
textureMatrices.push_back() = textureMatrices[0];
|
|
|
|
textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
|
|
|
|
|
|
|
|
const SkIRect texelDomains[] = {
|
2014-10-24 19:54:53 +00:00
|
|
|
fBmp.bounds(),
|
2014-03-25 15:13:18 +00:00
|
|
|
SkIRect::MakeXYWH(fBmp.width() / 4,
|
|
|
|
fBmp.height() / 4,
|
|
|
|
fBmp.width() / 2,
|
|
|
|
fBmp.height() / 2),
|
|
|
|
};
|
|
|
|
|
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));
|
2016-06-09 15:01:03 +00:00
|
|
|
sk_sp<GrFragmentProcessor> fp(
|
2017-01-09 19:23:59 +00:00
|
|
|
GrTextureDomainEffect::Make(
|
|
|
|
texture.get(), nullptr, textureMatrices[tm],
|
|
|
|
GrTextureDomain::MakeTexelDomainForMode(texelDomains[d], mode),
|
|
|
|
mode, GrSamplerParams::kNone_FilterMode));
|
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));
|
2014-03-25 15:13:18 +00:00
|
|
|
|
2017-01-03 14:42:58 +00:00
|
|
|
std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
|
2016-12-14 16:08:17 +00:00
|
|
|
GrColor_WHITE, viewMatrix, renderRect, nullptr, nullptr));
|
2017-01-11 18:42:54 +00:00
|
|
|
renderTargetContext->priv().testingOnly_addDrawOp(
|
|
|
|
std::move(grPaint), GrAAType::kNone, std::move(op));
|
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
|