blob: 84d0b55445447bb70955bbdbaaa634117dbd43ca [file] [log] [blame]
commit-bot@chromium.org7842c812013-12-06 20:14:39 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "gm.h"
8
9#include "SkBitmap.h"
10#include "SkPaint.h"
11#include "SkShader.h"
12
13namespace skiagm {
14
15static void draw_bm(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000016 SkPaint bluePaint;
17 bluePaint.setColor(SK_ColorBLUE);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000018
reed@google.comeb9a46c2014-01-25 16:46:20 +000019 bm->allocN32Pixels(20, 20);
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000020 bm->eraseColor(SK_ColorRED);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000021
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000022 SkCanvas canvas(*bm);
23 canvas.drawCircle(10, 10, 5, bluePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000024}
25
26static void draw_mask(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000027 SkPaint circlePaint;
28 circlePaint.setColor(SK_ColorBLACK);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000029
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000030 bm->allocPixels(SkImageInfo::MakeA8(20, 20));
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000031 bm->eraseColor(SK_ColorTRANSPARENT);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000032
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000033 SkCanvas canvas(*bm);
34 canvas.drawCircle(10, 10, 10, circlePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000035}
36
37class BitmapShaderGM : public GM {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000038
caryclark63c684a2015-02-25 09:04:04 -080039protected:
mtklein36352bf2015-03-25 18:17:31 -070040 void onOnceBeforeDraw() override {
caryclarkf597c422015-07-28 10:37:53 -070041 this->setBGColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000042 draw_bm(&fBitmap);
43 draw_mask(&fMask);
44 }
45
mtkleinf0599002015-07-13 06:18:39 -070046 SkString onShortName() override {
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000047 return SkString("bitmapshaders");
48 }
49
mtkleinf0599002015-07-13 06:18:39 -070050 SkISize onISize() override {
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000051 return SkISize::Make(150, 100);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000052 }
53
mtkleinf0599002015-07-13 06:18:39 -070054 void onDraw(SkCanvas* canvas) override {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000055 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000056
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000057 for (int i = 0; i < 2; i++) {
58 SkMatrix s;
59 s.reset();
60 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000061 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000062 s.postTranslate(2, 2);
63 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000064
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000065 canvas->save();
reed8a21c9f2016-03-08 18:50:00 -080066 paint.setShader(SkShader::MakeBitmapShader(fBitmap, SkShader::kClamp_TileMode,
67 SkShader::kClamp_TileMode, &s));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000068
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000069 // draw the shader with a bitmap mask
70 canvas->drawBitmap(fMask, 0, 0, &paint);
71 canvas->drawBitmap(fMask, 30, 0, &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000072
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000073 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000074
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000075 canvas->drawCircle(10, 10, 10, paint);
76 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000077
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000078 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000079
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000080 // clear the shader, colorized by a solid color with a bitmap mask
halcanary96fcdcc2015-08-27 07:41:13 -070081 paint.setShader(nullptr);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000082 paint.setColor(SK_ColorGREEN);
83 canvas->drawBitmap(fMask, 0, 0, &paint);
84 canvas->drawBitmap(fMask, 30, 0, &paint);
85
86 canvas->translate(0, 25);
87
reed8a21c9f2016-03-08 18:50:00 -080088 paint.setShader(SkShader::MakeBitmapShader(fMask, SkShader::kRepeat_TileMode,
89 SkShader::kRepeat_TileMode, &s));
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000090 paint.setColor(SK_ColorRED);
91
92 // draw the mask using the shader and a color
93 canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
94 canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
95 canvas->restore();
96 canvas->translate(60, 0);
97 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000098 }
99
100private:
101 SkBitmap fBitmap;
102 SkBitmap fMask;
103
104 typedef GM INHERITED;
105};
106
107//////////////////////////////////////////////////////////////////////////////
108
109static GM* MyFactory(void*) { return new BitmapShaderGM; }
110static GMRegistry reg(MyFactory);
111
112}