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
|
|
|
|
2016-03-18 22:48:49 +00:00
|
|
|
#include "DecodeFile.h"
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
2018-08-21 15:45:46 +00:00
|
|
|
#include "SkUTF.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkColorPriv.h"
|
|
|
|
#include "SkColorFilter.h"
|
|
|
|
#include "SkTime.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
|
2008-12-17 15:59:43 +00:00
|
|
|
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);
|
2016-03-09 02:50:00 +00:00
|
|
|
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-03-09 02:50:00 +00:00
|
|
|
auto shaderB = SkShader::MakeBitmapShader(bm,
|
2016-03-29 18:32:50 +00:00
|
|
|
SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-10-28 19:42:34 +00:00
|
|
|
return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class ShaderView : public Sample {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2016-03-09 02:50:00 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
|
|
|
SkBitmap fBitmap;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-08-23 18:19:56 +00:00
|
|
|
ShaderView() {
|
2016-03-18 22:48:49 +00:00
|
|
|
decode_file("/skimages/logo.gif", &fBitmap);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkPoint pts[2];
|
|
|
|
SkColor colors[2];
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(SkIntToScalar(100), 0);
|
|
|
|
colors[0] = SK_ColorRED;
|
|
|
|
colors[1] = SK_ColorBLUE;
|
2016-03-09 02:50:00 +00:00
|
|
|
auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
pts[0].set(0, 0);
|
|
|
|
pts[1].set(0, SkIntToScalar(100));
|
|
|
|
colors[0] = SK_ColorBLACK;
|
|
|
|
colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
|
2016-03-09 02:50:00 +00:00
|
|
|
auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2016-03-29 18:32:50 +00:00
|
|
|
fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
|
2016-10-28 19:42:34 +00:00
|
|
|
SkBlendMode::kDstIn);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, "Shaders");
|
2009-05-30 02:40:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
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);
|
2011-02-07 15:30:46 +00:00
|
|
|
|
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;
|
|
|
|
SkRect r;
|
|
|
|
|
|
|
|
paint.setColor(SK_ColorGREEN);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(100, 100), paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
paint.setShader(fShader);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect(SkRect::MakeWH(100, 100), paint);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
canvas->translate(SkIntToScalar(110), 0);
|
|
|
|
|
2009-05-30 02:40:28 +00:00
|
|
|
int w = fBitmap.width();
|
|
|
|
int h = fBitmap.height();
|
|
|
|
w = 120;
|
|
|
|
h = 80;
|
|
|
|
r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawRect(r, paint);
|
2016-03-09 02:50:00 +00:00
|
|
|
paint.setShader(make_bitmapfade(fBitmap));
|
2008-12-17 15:59:43 +00:00
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
DEF_SAMPLE( return new ShaderView(); )
|