blob: acb2ecb22f32cd227d8620cd4cfad4aaef8f27f2 [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.org08f6d862014-03-24 02:49:48 +000038static void adopt_shader(SkPaint* paint, SkShader* shader, SkMatrix s) {
39 shader->setLocalMatrix(s);
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000040 paint->setShader(shader);
41 SkSafeUnref(shader);
42}
43
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000044class BitmapShaderGM : public GM {
45public:
46
47 BitmapShaderGM() {
48 this->setBGColor(SK_ColorGRAY);
49 draw_bm(&fBitmap);
50 draw_mask(&fMask);
51 }
52
53protected:
54 virtual SkString onShortName() {
55 return SkString("bitmapshaders");
56 }
57
58 virtual SkISize onISize() {
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000059 return SkISize::Make(150, 100);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000060 }
61
62 virtual void onDraw(SkCanvas* canvas) {
skia.committer@gmail.com56710d12013-12-07 07:02:21 +000063 SkPaint paint;
commit-bot@chromium.orge5957f62014-03-18 16:28:12 +000064
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000065 for (int i = 0; i < 2; i++) {
66 SkMatrix s;
67 s.reset();
68 if (1 == i) {
commit-bot@chromium.orga723fee2014-03-24 14:54:04 +000069 s.setScale(1.5f, 1.5f);
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000070 s.postTranslate(2, 2);
71 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000072
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000073 canvas->save();
74 adopt_shader(&paint, SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode,
75 SkShader::kClamp_TileMode), s);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000076
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000077 // draw the shader with a bitmap mask
78 canvas->drawBitmap(fMask, 0, 0, &paint);
79 canvas->drawBitmap(fMask, 30, 0, &paint);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000080
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000081 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000082
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000083 canvas->drawCircle(10, 10, 10, paint);
84 canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000085
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000086 canvas->translate(0, 25);
commit-bot@chromium.org7842c812013-12-06 20:14:39 +000087
commit-bot@chromium.org08f6d862014-03-24 02:49:48 +000088 // clear the shader, colorized by a solid color with a bitmap mask
89 paint.setShader(NULL);
90 paint.setColor(SK_ColorGREEN);
91 canvas->drawBitmap(fMask, 0, 0, &paint);
92 canvas->drawBitmap(fMask, 30, 0, &paint);
93
94 canvas->translate(0, 25);
95
96 adopt_shader(&paint, SkShader::CreateBitmapShader(fMask, SkShader::kRepeat_TileMode,
97 SkShader::kRepeat_TileMode), s);
98 paint.setColor(SK_ColorRED);
99
100 // draw the mask using the shader and a color
101 canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
102 canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
103 canvas->restore();
104 canvas->translate(60, 0);
105 }
commit-bot@chromium.org7842c812013-12-06 20:14:39 +0000106 }
107
108private:
109 SkBitmap fBitmap;
110 SkBitmap fMask;
111
112 typedef GM INHERITED;
113};
114
115//////////////////////////////////////////////////////////////////////////////
116
117static GM* MyFactory(void*) { return new BitmapShaderGM; }
118static GMRegistry reg(MyFactory);
119
120}