Sample/Shaders: use resources; cleanup code

Change-Id: Icd71734927ebb50c8db55730107deb29faf0c61a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/225716
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
This commit is contained in:
Hal Canary 2019-07-05 10:42:56 -04:00 committed by Skia Commit-Bot
parent a1e88e7fb3
commit 352f0917a3

View File

@ -6,94 +6,77 @@
*/
#include "include/core/SkCanvas.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkColorPriv.h"
#include "include/core/SkGraphics.h"
#include "include/core/SkPath.h"
#include "include/core/SkRegion.h"
#include "include/core/SkShader.h"
#include "include/core/SkTime.h"
#include "include/core/SkTypeface.h"
#include "include/effects/SkGradientShader.h"
#include "samplecode/DecodeFile.h"
#include "tools/Resources.h"
#include "samplecode/Sample.h"
#include "src/utils/SkUTF.h"
namespace {
static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
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() {
SkPoint pts[2];
SkColor colors[2];
pts[0].set(0, 0);
pts[1].set(0, SkIntToScalar(bm.height()));
colors[0] = SK_ColorBLACK;
colors[1] = SkColorSetARGB(0, 0, 0, 0);
pts[1].set(SkIntToScalar(100), 0);
colors[0] = SK_ColorRED;
colors[1] = SK_ColorBLUE;
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
auto shaderB = bm.makeShader();
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);
return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderB), std::move(shaderA));
return SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB));
}
class ShaderView : public Sample {
public:
struct ShaderView : public Sample {
sk_sp<SkShader> fShader;
sk_sp<SkShader> fShaderFade;
SkBitmap fBitmap;
ShaderView() {
decode_file("/skimages/logo.gif", &fBitmap);
SkPoint pts[2];
SkColor colors[2];
pts[0].set(0, 0);
pts[1].set(SkIntToScalar(100), 0);
colors[0] = SK_ColorRED;
colors[1] = SK_ColorBLUE;
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
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);
fShader = SkShaders::Blend(SkBlendMode::kDstIn, std::move(shaderA), std::move(shaderB));
void onOnceBeforeDraw() override {
decode_file(GetResourceAsData("images/dog.jpg"), &fBitmap);
fShader = make_blend_shader();
fShaderFade = make_bitmapfade(fBitmap);
}
protected:
SkString name() override { return SkString("Shaders"); }
void onDrawContent(SkCanvas* canvas) override {
canvas->drawBitmap(fBitmap, 0, 0);
canvas->translate(20, 120);
SkPaint paint;
SkRect r;
paint.setColor(SK_ColorGREEN);
canvas->drawRect(SkRect::MakeWH(100, 100), paint);
canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
paint.setShader(fShader);
canvas->drawRect(SkRect::MakeWH(100, 100), paint);
canvas->drawRect(SkRect{0, 0, 100, 100}, paint);
canvas->translate(SkIntToScalar(110), 0);
int w = fBitmap.width();
int h = fBitmap.height();
w = 120;
h = 80;
r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
paint.setShader(nullptr);
canvas->drawRect(r, paint);
paint.setShader(make_bitmapfade(fBitmap));
canvas->drawRect(r, paint);
canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
paint.setShader(fShaderFade);
canvas->drawRect(SkRect{0, 0, 120, 80}, paint);
}
private:
typedef Sample INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
} // namespace
DEF_SAMPLE( return new ShaderView(); )