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"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkRegion.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkUtils.h"
|
|
|
|
#include "SkXfermode.h"
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
2011-05-05 01:59:48 +00:00
|
|
|
class ShaderView : public SampleView {
|
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:
|
|
|
|
// overrides from SkEventSink
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
2009-05-30 02:40:28 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "Shaders");
|
|
|
|
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
|
|
|
|
2009-05-30 02:40:28 +00:00
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(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);
|
|
|
|
canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
|
|
|
|
paint.setShader(fShader);
|
|
|
|
canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint);
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-06-05 18:31:26 +00:00
|
|
|
SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
this->inval(nullptr);
|
2013-01-08 16:17:50 +00:00
|
|
|
return this->INHERITED::onFindClickHandler(x, y, modi);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onClick(Click* click) override {
|
2008-12-17 15:59:43 +00:00
|
|
|
return this->INHERITED::onClick(click);
|
|
|
|
}
|
2011-02-07 15:30:46 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2011-05-05 01:59:48 +00:00
|
|
|
typedef SampleView INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static SkView* MyFactory() { return new ShaderView; }
|
|
|
|
static SkViewRegister reg(MyFactory);
|