17842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
27842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org/*
37842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org * Copyright 2013 Google Inc.
47842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org *
57842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
67842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org * found in the LICENSE file.
77842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org */
87842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org#include "gm.h"
97842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
107842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org#include "SkBitmap.h"
117842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org#include "SkPaint.h"
127842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org#include "SkShader.h"
137842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
147842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgnamespace skiagm {
157842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
167842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgstatic void draw_bm(SkBitmap* bm) {
1756710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    SkPaint bluePaint;
1856710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    bluePaint.setColor(SK_ColorBLUE);
197842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
20eb9a46cbbb475e862a084aa2224ec18d4ac5e95breed@google.com    bm->allocN32Pixels(20, 20);
2156710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    bm->eraseColor(SK_ColorRED);
227842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
2356710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    SkCanvas canvas(*bm);
2456710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    canvas.drawCircle(10, 10, 5, bluePaint);
257842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org}
267842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
277842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgstatic void draw_mask(SkBitmap* bm) {
2856710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    SkPaint circlePaint;
2956710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    circlePaint.setColor(SK_ColorBLACK);
307842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
31dac522589e9395b4654a1a708f1bd971f37f95a5commit-bot@chromium.org    bm->allocPixels(SkImageInfo::MakeA8(20, 20));
3256710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    bm->eraseColor(SK_ColorTRANSPARENT);
337842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
3456710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    SkCanvas canvas(*bm);
3556710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com    canvas.drawCircle(10, 10, 10, circlePaint);
367842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org}
377842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
389c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.orgstatic void adopt_shader(SkPaint* paint, SkShader* shader) {
39e5957f67d00c04c0fbf88809df510426e74fe40acommit-bot@chromium.org    paint->setShader(shader);
40e5957f67d00c04c0fbf88809df510426e74fe40acommit-bot@chromium.org    SkSafeUnref(shader);
41e5957f67d00c04c0fbf88809df510426e74fe40acommit-bot@chromium.org}
42e5957f67d00c04c0fbf88809df510426e74fe40acommit-bot@chromium.org
437842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgclass BitmapShaderGM : public GM {
447842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgpublic:
457842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
467842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    BitmapShaderGM() {
477842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org        this->setBGColor(SK_ColorGRAY);
487842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org        draw_bm(&fBitmap);
497842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org        draw_mask(&fMask);
507842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    }
517842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
527842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgprotected:
537842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    virtual SkString onShortName() {
547842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org        return SkString("bitmapshaders");
557842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    }
567842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
577842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    virtual SkISize onISize() {
5808f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org        return SkISize::Make(150, 100);
597842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    }
607842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
617842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    virtual void onDraw(SkCanvas* canvas) {
6256710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com        SkPaint paint;
63e5957f67d00c04c0fbf88809df510426e74fe40acommit-bot@chromium.org
6408f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org        for (int i = 0; i < 2; i++) {
6508f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            SkMatrix s;
6608f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            s.reset();
6708f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            if (1 == i) {
68a723feec39b19a2e8718c7516fafd4ca1ffb3009commit-bot@chromium.org                s.setScale(1.5f, 1.5f);
6908f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org                s.postTranslate(2, 2);
7008f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            }
7156710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
7208f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->save();
7308f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            adopt_shader(&paint, SkShader::CreateBitmapShader(fBitmap, SkShader::kClamp_TileMode,
749c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                                                              SkShader::kClamp_TileMode, &s));
7556710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
7608f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            // draw the shader with a bitmap mask
7708f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawBitmap(fMask, 0, 0, &paint);
7808f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawBitmap(fMask, 30, 0, &paint);
7956710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
8008f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->translate(0, 25);
8156710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
8208f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawCircle(10, 10, 10, paint);
8308f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
8456710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
8508f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->translate(0, 25);
8656710d1905e2575ea91da400f0dd6b3fc6c346c3skia.committer@gmail.com
8708f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            // clear the shader, colorized by a solid color with a bitmap mask
8808f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            paint.setShader(NULL);
8908f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            paint.setColor(SK_ColorGREEN);
9008f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawBitmap(fMask, 0, 0, &paint);
9108f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawBitmap(fMask, 30, 0, &paint);
9208f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org
9308f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->translate(0, 25);
9408f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org
9508f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            adopt_shader(&paint, SkShader::CreateBitmapShader(fMask, SkShader::kRepeat_TileMode,
969c9005a347e9996f357bd79591bd34f74f8bbc66commit-bot@chromium.org                                                              SkShader::kRepeat_TileMode, &s));
9708f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            paint.setColor(SK_ColorRED);
9808f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org
9908f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            // draw the mask using the shader and a color
10008f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
10108f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
10208f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->restore();
10308f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org            canvas->translate(60, 0);
10408f6d86f8e6aa19f6f2702b106fcfaa89d760869commit-bot@chromium.org        }
1057842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    }
1067842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
1077842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgprivate:
1087842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    SkBitmap fBitmap;
1097842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    SkBitmap fMask;
1107842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
1117842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org    typedef GM INHERITED;
1127842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org};
1137842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
1147842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
1157842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
1167842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgstatic GM* MyFactory(void*) { return new BitmapShaderGM; }
1177842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.orgstatic GMRegistry reg(MyFactory);
1187842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org
1197842c817e920a0e0c83be339f8d7e19b016c3373commit-bot@chromium.org}
120