2013-12-06 20:14:39 +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.
|
|
|
|
*/
|
|
|
|
#include "gm.h"
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkShader.h"
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
static void draw_bm(SkBitmap* bm) {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint bluePaint;
|
|
|
|
bluePaint.setColor(SK_ColorBLUE);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2014-01-25 16:46:20 +00:00
|
|
|
bm->allocN32Pixels(20, 20);
|
2013-12-07 07:02:21 +00:00
|
|
|
bm->eraseColor(SK_ColorRED);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2013-12-07 07:02:21 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(10, 10, 5, bluePaint);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_mask(SkBitmap* bm) {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint circlePaint;
|
|
|
|
circlePaint.setColor(SK_ColorBLACK);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
bm->allocPixels(SkImageInfo::MakeA8(20, 20));
|
2013-12-07 07:02:21 +00:00
|
|
|
bm->eraseColor(SK_ColorTRANSPARENT);
|
2013-12-06 20:14:39 +00:00
|
|
|
|
2013-12-07 07:02:21 +00:00
|
|
|
SkCanvas canvas(*bm);
|
|
|
|
canvas.drawCircle(10, 10, 10, circlePaint);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:55:39 +00:00
|
|
|
static void adopt_shader(SkPaint* paint, SkShader* shader) {
|
2014-03-18 16:28:12 +00:00
|
|
|
paint->setShader(shader);
|
|
|
|
SkSafeUnref(shader);
|
|
|
|
}
|
|
|
|
|
2013-12-06 20:14:39 +00:00
|
|
|
class BitmapShaderGM : public GM {
|
|
|
|
|
2015-02-25 17:04:04 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2015-07-28 17:37:53 +00:00
|
|
|
this->setBGColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
|
2013-12-06 20:14:39 +00:00
|
|
|
draw_bm(&fBitmap);
|
|
|
|
draw_mask(&fMask);
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkString onShortName() override {
|
2013-12-06 20:14:39 +00:00
|
|
|
return SkString("bitmapshaders");
|
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
SkISize onISize() override {
|
2014-03-24 02:49:48 +00:00
|
|
|
return SkISize::Make(150, 100);
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 13:18:39 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-12-07 07:02:21 +00:00
|
|
|
SkPaint paint;
|
2014-03-18 16:28:12 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
SkMatrix s;
|
|
|
|
s.reset();
|
|
|
|
if (1 == i) {
|
2014-03-24 14:54:04 +00:00
|
|
|
s.setScale(1.5f, 1.5f);
|
2014-03-24 02:49:48 +00:00
|
|
|
s.postTranslate(2, 2);
|
|
|
|
}
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->save();
|
|
|
|
adopt_shader(&paint, SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode,
|
2014-04-28 14:55:39 +00:00
|
|
|
SkShader::kClamp_TileMode, &s));
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
// draw the shader with a bitmap mask
|
|
|
|
canvas->drawBitmap(fMask, 0, 0, &paint);
|
|
|
|
canvas->drawBitmap(fMask, 30, 0, &paint);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->translate(0, 25);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->drawCircle(10, 10, 10, paint);
|
|
|
|
canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
canvas->translate(0, 25);
|
2013-12-07 07:02:21 +00:00
|
|
|
|
2014-03-24 02:49:48 +00:00
|
|
|
// clear the shader, colorized by a solid color with a bitmap mask
|
2015-08-27 14:41:13 +00:00
|
|
|
paint.setShader(nullptr);
|
2014-03-24 02:49:48 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
|
|
|
canvas->drawBitmap(fMask, 0, 0, &paint);
|
|
|
|
canvas->drawBitmap(fMask, 30, 0, &paint);
|
|
|
|
|
|
|
|
canvas->translate(0, 25);
|
|
|
|
|
|
|
|
adopt_shader(&paint, SkShader::CreateBitmapShader(fMask, SkShader::kRepeat_TileMode,
|
2014-04-28 14:55:39 +00:00
|
|
|
SkShader::kRepeat_TileMode, &s));
|
2014-03-24 02:49:48 +00:00
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// draw the mask using the shader and a color
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(60, 0);
|
|
|
|
}
|
2013-12-06 20:14:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkBitmap fBitmap;
|
|
|
|
SkBitmap fMask;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new BitmapShaderGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|