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)
|
2019-07-18 17:35:49 +00:00
|
|
|
: fProc(proc), fName(name), fDither(dither) {}
|
2013-02-04 16:56:15 +00:00
|
|
|
|
2019-07-18 17:35:49 +00:00
|
|
|
private:
|
|
|
|
MakeShaderProc fProc;
|
|
|
|
const char* fName;
|
|
|
|
bool fDither;
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2019-07-18 17:35:49 +00:00
|
|
|
return SkStringPrintf("shallow_gradient_%s%s", fName, fDither ? "" : "_nodither");
|
2013-02-04 16:56:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 17:35:49 +00:00
|
|
|
SkISize onISize() override { return {800, 800}; }
|
2013-02-04 16:56:15 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-07-18 17:35:49 +00:00
|
|
|
#define M(PROC, DITHER) DEF_GM( return new ShallowGradientGM(shader_ ## PROC, #PROC, DITHER); )
|
|
|
|
M(linear, true)
|
|
|
|
M(radial, true)
|
|
|
|
M(conical, true)
|
|
|
|
M(sweep, true)
|
|
|
|
|
|
|
|
M(linear, false)
|
|
|
|
M(radial, false)
|
|
|
|
M(conical, false)
|
|
|
|
M(sweep, false)
|
|
|
|
#undef M
|