blob: 5cf5e14ce80c0d05c85298be8290130307a44440 [file] [log] [blame]
commit-bot@chromium.org7842c812013-12-06 20:14:39 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "gm.h"
9
10#include "SkBitmap.h"
11#include "SkPaint.h"
12#include "SkShader.h"
13
14namespace skiagm {
15
16static void draw_bm(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000017 SkPaint bluePaint;
18 bluePaint.setColor(SK_ColorBLUE);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000019
reed@google.comeb9a46c2014-01-25 16:46:20 +000020 bm->allocN32Pixels(20, 20);
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000021 bm->eraseColor(SK_ColorRED);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000022
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000023 SkCanvas canvas(*bm);
24 canvas.drawCircle(10, 10, 5, bluePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000025}
26
27static void draw_mask(SkBitmap* bm) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000028 SkPaint circlePaint;
29 circlePaint.setColor(SK_ColorBLACK);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000030
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000031 bm->allocPixels(SkImageInfo::MakeA8(20, 20));
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000032 bm->eraseColor(SK_ColorTRANSPARENT);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000033
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000034 SkCanvas canvas(*bm);
35 canvas.drawCircle(10, 10, 10, circlePaint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000036}
37
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000038static void adopt_shader(SkPaint* paint, SkShader* shader) {
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000039 paint->setShader(shader);
40 SkSafeUnref(shader);
41}
42
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000043class BitmapShaderGM : public GM {
44public:
45
46 BitmapShaderGM() {
47 this->setBGColor(SK_ColorGRAY);
48 draw_bm(&fBitmap);
49 draw_mask(&fMask);
50 }
51
52protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000053 virtual uint32_t onGetFlags() const SK_OVERRIDE {
54 return kSkipTiled_Flag;
55 }
56
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000057 virtual SkString onShortName() {
58 return SkString("bitmapshaders");
59 }
60
61 virtual SkISize onISize() {
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000062 return SkISize::Make(150, 100);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000063 }
64
65 virtual void onDraw(SkCanvas* canvas) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000066 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000067
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000068 for (int i = 0; i < 2; i++) {
69 SkMatrix s;
70 s.reset();
71 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000072 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000073 s.postTranslate(2, 2);
74 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000075
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000076 canvas->save();
77 adopt_shader(&paint, SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000078 SkShader::kClamp_TileMode, &s));
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000079
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000080 // draw the shader with a bitmap mask
81 canvas->drawBitmap(fMask, 0, 0, &paint);
82 canvas->drawBitmap(fMask, 30, 0, &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000083
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000084 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000085
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000086 canvas->drawCircle(10, 10, 10, paint);
87 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000088
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000089 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000090
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000091 // clear the shader, colorized by a solid color with a bitmap mask
92 paint.setShader(NULL);
93 paint.setColor(SK_ColorGREEN);
94 canvas->drawBitmap(fMask, 0, 0, &paint);
95 canvas->drawBitmap(fMask, 30, 0, &paint);
96
97 canvas->translate(0, 25);
98
99 adopt_shader(&paint, SkShader::CreateBitmapShader(fMask, SkShader::kRepeat_TileMode,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000100 SkShader::kRepeat_TileMode, &s));
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +0000101 paint.setColor(SK_ColorRED);
102
103 // draw the mask using the shader and a color
104 canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
105 canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
106 canvas->restore();
107 canvas->translate(60, 0);
108 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000109 }
110
111private:
112 SkBitmap fBitmap;
113 SkBitmap fMask;
114
115 typedef GM INHERITED;
116};
117
118//////////////////////////////////////////////////////////////////////////////
119
120static GM* MyFactory(void*) { return new BitmapShaderGM; }
121static GMRegistry reg(MyFactory);
122
123}