2013-02-04 16:56:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2013-02-04 16:56:15 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
|
2013-02-04 16:56:15 +00:00
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
|
2013-02-04 16:56:15 +00:00
|
|
|
SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
|
2019-04-03 14:27:45 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkTileMode::kClamp);
|
2013-02-04 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
|
2013-02-04 16:56:15 +00:00
|
|
|
SkPoint center = { size.width()/2, size.height()/2 };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kClamp);
|
2013-02-04 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
|
2013-02-04 16:56:15 +00:00
|
|
|
SkPoint center = { size.width()/2, size.height()/2 };
|
2016-03-13 21:13:58 +00:00
|
|
|
return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
|
2019-04-03 14:27:45 +00:00
|
|
|
colors, nullptr, count, SkTileMode::kClamp);
|
2013-02-04 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-13 21:13:58 +00:00
|
|
|
static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
|
|
|
|
return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
|
2013-02-04 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ShallowGradientGM : public skiagm::GM {
|
|
|
|
public:
|
2015-10-12 17:41:48 +00:00
|
|
|
ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
|
|
|
|
: fProc(proc)
|
|
|
|
, fDither(dither) {
|
2013-02-04 16:56:15 +00:00
|
|
|
fName.printf("shallow_gradient_%s", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-02-04 16:56:15 +00:00
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-02-04 16:56:15 +00:00
|
|
|
return SkISize::Make(800, 800);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-08-16 13:32:46 +00:00
|
|
|
const SkColor colors[] = { 0xFF555555, 0xFF444444 };
|
2013-02-04 16:56:15 +00:00
|
|
|
const int colorCount = SK_ARRAY_COUNT(colors);
|
|
|
|
|
|
|
|
SkRect r = { 0, 0, this->width(), this->height() };
|
|
|
|
SkSize size = SkSize::Make(r.width(), r.height());
|
|
|
|
|
|
|
|
SkPaint paint;
|
2016-03-13 21:13:58 +00:00
|
|
|
paint.setShader(fProc(colors, colorCount, size));
|
2015-10-12 17:41:48 +00:00
|
|
|
paint.setDither(fDither);
|
2013-02-04 16:56:15 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
MakeShaderProc fProc;
|
|
|
|
SkString fName;
|
2015-10-12 17:41:48 +00:00
|
|
|
bool fDither;
|
2013-02-04 16:56:15 +00:00
|
|
|
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-10-12 17:41:48 +00:00
|
|
|
DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
|
|
|
|
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
|
|
|
|
DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )
|