1064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org/*
2064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org * Copyright 2013 Google Inc.
3064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org *
4064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org * found in the LICENSE file.
6064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org */
7064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
8064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org#include "SampleCode.h"
9d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
10064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org#include "SkCanvas.h"
11d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org#include "SkCornerPathEffect.h"
12d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org#include "SkDashPathEffect.h"
13064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org#include "SkPathUtils.h"
14064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org#include "SkRandom.h"
15d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org#include "SkView.h"
16d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
17d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.orgtypedef void (*BitsToPath)(SkPath*, const char*, int, int, int);
18d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
19d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.orgstatic const BitsToPath gBitsToPath_fns[] = {
20d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    SkPathUtils::BitsToPath_Path,
21d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    SkPathUtils::BitsToPath_Region,
22d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org};
23064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
24d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org// hardcoded bitmap patterns
25d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.orgstatic const uint8_t gBits[][16] = {
26d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    { 0x18, 0x00, 0x3c, 0x00, 0x7e, 0x00, 0xdb, 0x00,
27d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org      0xff, 0x00, 0x24, 0x00, 0x5a, 0x00, 0xa5, 0x00 },
289e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com
29d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    { 0x20, 0x80, 0x91, 0x20, 0xbf, 0xa0, 0xee, 0xe0,
30d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org      0xff, 0xe0, 0x7f, 0xc0, 0x20, 0x80, 0x40, 0x40 },
319e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com
32d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    { 0x0f, 0x00, 0x7f, 0xe0, 0xff, 0xf0, 0xe6, 0x70,
33d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org      0xff, 0xf0, 0x19, 0x80, 0x36, 0xc0, 0xc0, 0x30 }
34d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org};
35d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
36d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
37d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.orgclass SamplePathUtils : public SampleView {
38064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.orgpublic:
39d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    static const int fNumBits = 3;
40d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    static const int fH = 8, fW = 12;
419e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com    static const size_t fRowBytes = 2;
429e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com    static const int fNumChars = fH * fRowBytes;
43d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
44d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    SkPaint fBmpPaint;
45d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    SkScalar fPhase;
46d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
47d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org    SamplePathUtils() {
48d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        fBmpPaint.setAntiAlias(true);  // Black paint for bitmap
49d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        fBmpPaint.setStyle(SkPaint::kFill_Style);
509e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com
51d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        fPhase = 0.0f; // to animate the dashed path
52064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    }
53064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
54064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.orgprotected:
55064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    // overrides from SkEventSink
56064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    virtual bool onQuery(SkEvent* evt) {
57064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org        if (SampleCode::TitleQ(*evt)) {
58064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org            SampleCode::TitleR(evt, "PathUtils");
59064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org            return true;
60064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org        }
61064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org        return this->INHERITED::onQuery(evt);
62064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    }
63064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
64064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    /////////////////////////////////////////////////////////////
650d55dd7d2c07a77b22478bed9b30ff44ceecad68skia.committer@gmail.com
66064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    virtual void onDrawContent(SkCanvas* canvas) {
6764527e9caa4d38c0c113ce3de3ccf3c6e40eef05robertphillips@google.com        SkScalar intervals[8] = { .5f, .3f, .5f, .3f, .5f, .3f, .5f, .3f };
680a2bf90dccba3bde188e0386a7f0c60e6dde1ae9commit-bot@chromium.org        SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, 2, fPhase));
690a2bf90dccba3bde188e0386a7f0c60e6dde1ae9commit-bot@chromium.org        SkAutoTUnref<SkCornerPathEffect> corner(SkCornerPathEffect::Create(.25f));
700a2bf90dccba3bde188e0386a7f0c60e6dde1ae9commit-bot@chromium.org        SkAutoTUnref<SkComposePathEffect> compose(SkComposePathEffect::Create(dash, corner));
71d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
72d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        SkPaint outlinePaint;
73d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        outlinePaint.setAntiAlias(true);  // dashed paint for bitmap
74d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        outlinePaint.setStyle(SkPaint::kStroke_Style);
750a2bf90dccba3bde188e0386a7f0c60e6dde1ae9commit-bot@chromium.org        outlinePaint.setPathEffect(compose);
76d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
77d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        canvas->scale(10.0f, 10.0f);  // scales up
78d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
79d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        for (int i = 0; i < fNumBits; ++i) {
809e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com            canvas->save();
81d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org            for (size_t j = 0; j < SK_ARRAY_COUNT(gBitsToPath_fns); ++j) {
82d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                SkPath path;
83d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                gBitsToPath_fns[j](&path, (char*) &gBits[i], fW, fH, fRowBytes);
84d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
85d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                //draw skPath and outline
869e1ec1a52985cce9db3a0d0e8d448b82a32e70cbskia.committer@gmail.com                canvas->drawPath(path, fBmpPaint);
87d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                canvas->translate(1.5f * fW, 0); // translates past previous bitmap
88d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                canvas->drawPath(path, outlinePaint);
89d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org                canvas->translate(1.5f * fW, 0); // translates past previous bitmap
90d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org            }
91064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org            canvas->restore();
92d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org            canvas->translate(0, 1.5f * fH); //translate to next row
93064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org        }
94d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org
95d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        // for animated pathEffect
9664527e9caa4d38c0c113ce3de3ccf3c6e40eef05robertphillips@google.com        fPhase += .01f;
97d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.org        this->inval(NULL);
98064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    }
990d55dd7d2c07a77b22478bed9b30ff44ceecad68skia.committer@gmail.com
100064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.orgprivate:
101064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org    typedef SkView INHERITED;
102064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org};
103064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
104064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
105064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org
106d43f6449129f11c9823bdeb0fb8ac38ab6d76a28commit-bot@chromium.orgstatic SkView* MyFactory() { return new SamplePathUtils; }
107064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.orgstatic SkViewRegister reg(MyFactory)
108064779aa18694b68536c113f7d5b74ccbe38d3bacommit-bot@chromium.org;
109