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"
|
|
|
|
#include "GrTest.h"
|
|
|
|
#include "effects/GrTextureDomain.h"
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkGr.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
|
|
|
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 };
|
|
|
|
paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1,
|
2015-08-27 14:41:13 +00:00
|
|
|
nullptr, SK_ARRAY_COUNT(colors1)))->unref();
|
2014-03-25 15:13:18 +00:00
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
|
|
|
|
fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
|
|
|
|
|
|
|
|
SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, nullptr,
|
2014-03-25 15:13:18 +00:00
|
|
|
SK_ARRAY_COUNT(colors2)))->unref();
|
|
|
|
paint.setXfermodeMode(SkXfermode::kDarken_Mode);
|
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
|
|
|
|
fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
|
|
|
|
|
|
|
|
SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, nullptr,
|
2014-03-25 15:13:18 +00:00
|
|
|
SK_ARRAY_COUNT(colors3)))->unref();
|
|
|
|
paint.setXfermodeMode(SkXfermode::kLighten_Mode);
|
|
|
|
canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
|
|
|
|
fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-03-25 15:13:18 +00:00
|
|
|
GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == rt) {
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
GrContext* context = rt->getContext();
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == context) {
|
2015-09-09 15:16:41 +00:00
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
2014-03-25 15:13:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTestTarget tt;
|
|
|
|
context->getTestTarget(&tt);
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tt.target()) {
|
2014-03-25 15:13:18 +00:00
|
|
|
SkDEBUGFAIL("Couldn't get Gr test target.");
|
|
|
|
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-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);
|
2015-01-22 18:16:09 +00:00
|
|
|
pipelineBuilder.setRenderTarget(rt);
|
2015-08-27 13:30:17 +00:00
|
|
|
pipelineBuilder.addColorFragmentProcessor(fp);
|
2014-03-25 15:13:18 +00:00
|
|
|
|
2015-08-21 17:53:34 +00:00
|
|
|
tt.target()->drawNonAARect(pipelineBuilder,
|
|
|
|
GrColor_WHITE,
|
|
|
|
viewMatrix,
|
|
|
|
renderRect);
|
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
|