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
|
|
|
|
|
2016-03-02 17:26:36 +00:00
|
|
|
#include "GrDrawContextPriv.h"
|
2014-03-25 15:13:18 +00:00
|
|
|
#include "GrContext.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkGradientShader.h"
|
2016-01-13 18:08:27 +00:00
|
|
|
#include "batches/GrDrawBatch.h"
|
|
|
|
#include "batches/GrRectBatchFactory.h"
|
|
|
|
#include "effects/GrTextureDomain.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)));
|
2014-03-25 15:13:18 +00:00
|
|
|
paint.setXfermodeMode(SkXfermode::kDarken_Mode);
|
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)));
|
2014-03-25 15:13:18 +00:00
|
|
|
paint.setXfermodeMode(SkXfermode::kLighten_Mode);
|
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-04-28 21:32:04 +00:00
|
|
|
GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
|
|
|
|
if (!drawContext) {
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-12 17:39:46 +00:00
|
|
|
SkAutoTUnref<GrTexture> texture(GrRefCachedBitmapTexture(context, fBmp,
|
|
|
|
GrTextureParams::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;
|
2015-07-09 17:24:35 +00:00
|
|
|
GrPipelineBuilder pipelineBuilder;
|
2015-11-23 21:20:41 +00:00
|
|
|
pipelineBuilder.setXPFactory(
|
|
|
|
GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
|
2015-10-07 16:20:28 +00:00
|
|
|
SkAutoTUnref<const GrFragmentProcessor> fp(
|
2015-10-06 15:40:50 +00:00
|
|
|
GrTextureDomainEffect::Create(texture, textureMatrices[tm],
|
2014-03-25 15:13:18 +00:00
|
|
|
GrTextureDomain::MakeTexelDomain(texture,
|
|
|
|
texelDomains[d]),
|
|
|
|
mode, GrTextureParams::kNone_FilterMode));
|
|
|
|
|
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-04-28 21:32:04 +00:00
|
|
|
pipelineBuilder.setRenderTarget(drawContext->accessRenderTarget());
|
2015-08-27 13:30:17 +00:00
|
|
|
pipelineBuilder.addColorFragmentProcessor(fp);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
2016-01-13 18:08:27 +00:00
|
|
|
SkAutoTUnref<GrDrawBatch> batch(
|
|
|
|
GrRectBatchFactory::CreateNonAAFill(GrColor_WHITE, viewMatrix,
|
|
|
|
renderRect, nullptr, nullptr));
|
2016-03-02 17:26:36 +00:00
|
|
|
drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
|
2014-03-25 15:13:18 +00:00
|
|
|
x += renderRect.width() + kTestPad;
|
|
|
|
}
|
|
|
|
y += renderRect.height() + kTestPad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-29 19:59:27 +00:00
|
|
|
static const SkScalar kDrawPad;
|
|
|
|
static const SkScalar kTestPad;
|
|
|
|
static const int kTargetWidth = 100;
|
|
|
|
static const int kTargetHeight = 100;
|
2014-03-25 15:13:18 +00:00
|
|
|
SkBitmap fBmp;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2014-07-29 19:59:27 +00:00
|
|
|
// Windows builds did not like SkScalar initialization in class :(
|
|
|
|
const SkScalar TextureDomainEffect::kDrawPad = 10.f;
|
|
|
|
const SkScalar TextureDomainEffect::kTestPad = 10.f;
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_GM(return new TextureDomainEffect;)
|
2014-03-25 15:13:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|