2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2016-03-09 02:50:00 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "samplecode/DecodeFile.h"
|
2019-07-05 14:42:56 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-07-05 14:42:56 +00:00
|
|
|
namespace {
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
|
2019-07-05 14:42:56 +00:00
|
|
|
SkPoint pts[2] = {
|
|
|
|
{0, 0},
|
|
|
|
{0, (float)bm.height()},
|
|
|
|
};
|
|
|
|
SkColor colors[2] = {
|
|
|
|
SkColorSetARGB(255, 0, 0, 0),
|
|
|
|
SkColorSetARGB(0, 0, 0, 0),
|
|
|
|
};
|
|
|
|
return SkShaders::Blend(SkBlendMode::kDstIn,
|
|
|
|
bm.makeShader(),
|
|
|
|
SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
|
|
|
|
SkTileMode::kClamp));
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkShader> make_blend_shader() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPoint pts[2];
|
|
|
|
SkColor colors[2];
|
|
|
|
|
|
|
|
pts[0].set(0, 0);
|
2019-07-05 14:42:56 +00:00
|
|
|
pts[1].set(SkIntToScalar(100), 0);
|
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorBLUE;
|
2019-04-03 14:27:45 +00:00
|
|
|
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-07-05 14:42:56 +00:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(0, SkIntToScalar(100));
|
|
|
|
colors[0] = SK_ColorBLACK;
|
|
|
|
colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
|
|
|
|
auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-07-05 14:42:56 +00:00
|
|
|
return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB));
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 14:42:56 +00:00
|
|
|
struct ShaderView : public Sample {
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
2019-07-05 14:42:56 +00:00
|
|
|
sk_sp<SkShader> fShaderFade;
|
2016-03-09 02:50:00 +00:00
|
|
|
SkBitmap fBitmap;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-07-05 14:42:56 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
decode_file(GetResourceAsData("images/dog.jpg"), &fBitmap);
|
|
|
|
fShader = make_blend_shader();
|
|
|
|
fShaderFade = make_bitmapfade(fBitmap);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return SkString("Shaders"); }
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 0, 0);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->translate(20, 120);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2019-07-05 14:42:56 +00:00
|
|
|
canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(fShader);
|
2019-07-05 14:42:56 +00:00
|
|
|
canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(110), 0);
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2019-07-05 14:42:56 +00:00
|
|
|
canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
|
|
|
|
paint.setShader(fShaderFade);
|
|
|
|
canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
2019-07-05 14:42:56 +00:00
|
|
|
} // namespace
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ShaderView(); )
|