2012-03-21 17:34:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2014-04-28 14:55:39 +00:00
|
|
|
static SkShader* MakeLinear(SkScalar width, SkScalar height, bool alternate,
|
|
|
|
const SkMatrix& localMatrix) {
|
2012-03-21 17:34:30 +00:00
|
|
|
SkPoint pts[2] = { {0, 0}, {width, height}};
|
|
|
|
SkColor colors[2] = {SK_ColorRED, SK_ColorGREEN};
|
|
|
|
if (alternate) {
|
|
|
|
pts[1].fY = 0;
|
|
|
|
colors[0] = SK_ColorBLUE;
|
|
|
|
colors[1] = SK_ColorYELLOW;
|
|
|
|
}
|
|
|
|
return SkGradientShader::CreateLinear(pts, colors, NULL, 2,
|
2014-05-22 12:27:41 +00:00
|
|
|
SkShader::kClamp_TileMode, 0, &localMatrix);
|
2012-03-21 17:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class ShaderBoundsGM : public GM {
|
|
|
|
public:
|
|
|
|
typedef SkShader* (*ShaderGenFunc)(SkScalar width, SkScalar height,
|
2014-04-28 14:55:39 +00:00
|
|
|
bool alternate, const SkMatrix& localMatrix);
|
2012-03-21 17:34:30 +00:00
|
|
|
ShaderBoundsGM(ShaderGenFunc maker, const SkString& name)
|
|
|
|
: fShaderMaker(maker),
|
|
|
|
fName(name) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-03-21 17:34:30 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(320, 240); }
|
2012-03-21 17:34:30 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkMatrix onGetInitialTransform() const override {
|
2012-03-21 17:34:30 +00:00
|
|
|
SkMatrix result;
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar scale = 0.8f;
|
2012-03-21 17:34:30 +00:00
|
|
|
result.setScale(scale, scale);
|
|
|
|
result.postTranslate(SkIntToScalar(7), SkIntToScalar(23));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-03-21 17:34:30 +00:00
|
|
|
// The PDF device has already clipped to the content area, but we
|
|
|
|
// do it again here so that the raster and pdf results are consistent.
|
|
|
|
canvas->clipRect(SkRect::MakeWH(SkIntToScalar(320),
|
|
|
|
SkIntToScalar(240)));
|
|
|
|
|
|
|
|
SkMatrix canvasScale;
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar scale = 0.7f;
|
2012-03-21 17:34:30 +00:00
|
|
|
canvasScale.setScale(scale, scale);
|
|
|
|
canvas->concat(canvasScale);
|
|
|
|
|
|
|
|
// Background shader.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(MakeShader(559, 387, false))->unref();
|
|
|
|
SkRect r = SkRect::MakeXYWH(SkIntToScalar(-12), SkIntToScalar(-41),
|
|
|
|
SkIntToScalar(571), SkIntToScalar(428));
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
|
|
|
|
// Constrained shader.
|
|
|
|
paint.setShader(MakeShader(101, 151, true))->unref();
|
|
|
|
r = SkRect::MakeXYWH(SkIntToScalar(43), SkIntToScalar(71),
|
|
|
|
SkIntToScalar(101), SkIntToScalar(151));
|
|
|
|
canvas->clipRect(r);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkShader* MakeShader(int width, int height, bool background) {
|
2013-11-25 19:44:07 +00:00
|
|
|
SkScalar scale = 0.5f;
|
2012-03-21 17:34:30 +00:00
|
|
|
if (background) {
|
2013-11-25 19:44:07 +00:00
|
|
|
scale = 0.6f;
|
2012-03-21 17:34:30 +00:00
|
|
|
}
|
2012-04-17 21:46:18 +00:00
|
|
|
SkScalar shaderWidth = SkScalarDiv(SkIntToScalar(width), scale);
|
|
|
|
SkScalar shaderHeight = SkScalarDiv(SkIntToScalar(height), scale);
|
2012-03-21 17:34:30 +00:00
|
|
|
SkMatrix shaderScale;
|
|
|
|
shaderScale.setScale(scale, scale);
|
2014-04-28 14:55:39 +00:00
|
|
|
SkShader* shader = fShaderMaker(shaderWidth, shaderHeight, background, shaderScale);
|
2012-03-21 17:34:30 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
|
|
|
|
ShaderGenFunc fShaderMaker;
|
|
|
|
SkString fName;
|
|
|
|
|
|
|
|
SkShader* MakeShader(bool background);
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) {
|
|
|
|
return new ShaderBoundsGM(MakeLinear, SkString("shaderbounds_linear"));
|
|
|
|
}
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|