SamplePathEffects.cpp revision e0e7cfe44bb9d66d76120a79e5275c294bacaa22
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SampleCode.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkView.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h"
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGradientShader.h"
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h"
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h"
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h"
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "Sk1DPathEffect.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCornerPathEffect.h"
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPathMeasure.h"
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRandom.h"
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPixelXorXfermode.h"
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define CORNER_RADIUS   12
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkScalar gPhase;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic const int gXY[] = {
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    4, 0, 0, -4, 8, -4, 12, 0, 8, 4, 0, 4
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
30f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic SkPathEffect* make_pe(int flags) {
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (flags == 1)
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath  path;
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.close();
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.offset(SkIntToScalar(-6), 0);
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kRotate_Style);
42ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (flags == 2)
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return outer;
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* pe = new SkComposePathEffect(outer, inner);
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    outer->unref();
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inner->unref();
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return pe;
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
54f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.comstatic SkPathEffect* make_warp_pe() {
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath  path;
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.moveTo(SkIntToScalar(gXY[0]), SkIntToScalar(gXY[1]));
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    for (unsigned i = 2; i < SK_ARRAY_COUNT(gXY); i += 2)
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        path.lineTo(SkIntToScalar(gXY[i]), SkIntToScalar(gXY[i+1]));
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.close();
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    path.offset(SkIntToScalar(-6), 0);
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* outer = new SkPath1DPathEffect(path, SkIntToScalar(12), gPhase, SkPath1DPathEffect::kMorph_Style);
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* inner = new SkCornerPathEffect(SkIntToScalar(CORNER_RADIUS));
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPathEffect* pe = new SkComposePathEffect(outer, inner);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    outer->unref();
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    inner->unref();
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return pe;
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com///////////////////////////////////////////////////////////
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorFilter.h"
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkLayerRasterizer.h"
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass testrast : public SkLayerRasterizer {
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
78f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    testrast() {
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint paint;
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setAntiAlias(true);
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
82ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com#if 0
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStyle(SkPaint::kStroke_Style);
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStrokeWidth(SK_Scalar1*4);
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->addLayer(paint);
86ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStrokeWidth(SK_Scalar1*1);
88048522dd2aa45d1b4bf52944527f877b30ea45fdreed@android.com        paint.setXfermode(SkXfermode::kClear_Mode);
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->addLayer(paint);
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#else
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setAlpha(0x66);
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->addLayer(paint, SkIntToScalar(4), SkIntToScalar(4));
93ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setAlpha(0xFF);
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->addLayer(paint);
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
100e5ff43906603dff33e14086cf1e5d73c157d2e8ereed@google.comclass PathEffectView : public SampleView {
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPath  fPath;
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkPoint fClickPt;
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
104ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com    PathEffectView() {
105e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.org        SkRandom    rand;
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        int         steps = 20;
10792a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com        SkScalar    dist = SkIntToScalar(400);
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    x = SkIntToScalar(20);
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkScalar    y = SkIntToScalar(50);
110ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fPath.moveTo(x, y);
112f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        for (int i = 0; i < steps; i++) {
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            x += dist/steps;
11492a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            SkScalar tmpY = y + SkIntToScalar(rand.nextS() % 25);
11592a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            if (i == steps/2) {
11692a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com                fPath.moveTo(x, tmpY);
11792a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            } else {
11892a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com                fPath.lineTo(x, tmpY);
11992a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            }
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12292a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com        {
12392a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            SkRect  oval;
12492a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            oval.set(SkIntToScalar(20), SkIntToScalar(30),
12592a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com                     SkIntToScalar(100), SkIntToScalar(60));
12692a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            oval.offset(x, 0);
12792a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com            fPath.addRoundRect(oval, SkIntToScalar(8), SkIntToScalar(8));
12892a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com        }
129ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fClickPt.set(SkIntToScalar(200), SkIntToScalar(200));
131ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
132e5ff43906603dff33e14086cf1e5d73c157d2e8ereed@google.com        this->setBGColor(0xFFDDDDDD);
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
134ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected:
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // overrides from SkEventSink
137f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    virtual bool onQuery(SkEvent* evt) {
138f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        if (SampleCode::TitleQ(*evt)) {
139f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            SampleCode::TitleR(evt, "PathEffects");
140f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com            return true;
141f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        }
142f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        return this->INHERITED::onQuery(evt);
1438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
144ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
145e5ff43906603dff33e14086cf1e5d73c157d2e8ereed@google.com    virtual void onDrawContent(SkCanvas* canvas) {
146f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com        gPhase -= SampleCode::GetAnimSecondsDelta() * 40;
147671cd656785de5e84564b6ffe4831625d7016dedreed@android.com        this->inval(NULL);
148ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkPaint paint;
150ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
151f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com#if 0
1528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setAntiAlias(true);
1538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStyle(SkPaint::kStroke_Style);
1548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStrokeWidth(SkIntToScalar(5));
1558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPath(fPath, paint);
1568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setStrokeWidth(0);
157ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
15892a50eaab977543031f12a1b0b1d4ce07f80ba75reed@android.com        paint.setColor(SK_ColorWHITE);
1598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setPathEffect(make_pe(1))->unref();
1608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPath(fPath, paint);
161f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com#endif
162ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->translate(0, SkIntToScalar(50));
164ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setColor(SK_ColorBLUE);
1668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setPathEffect(make_pe(2))->unref();
1678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPath(fPath, paint);
168ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->translate(0, SkIntToScalar(50));
170ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setARGB(0xFF, 0, 0xBB, 0);
1728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setPathEffect(make_pe(3))->unref();
1738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPath(fPath, paint);
174ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->translate(0, SkIntToScalar(50));
1768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setARGB(0xFF, 0, 0, 0);
1788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setPathEffect(make_warp_pe())->unref();
1798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        paint.setRasterizer(new testrast)->unref();
1808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        canvas->drawPath(fPath, paint);
1818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
182ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com
1838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
184e5ff43906603dff33e14086cf1e5d73c157d2e8ereed@google.com    typedef SampleView INHERITED;
1858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
1868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com//////////////////////////////////////////////////////////////////////////////
1888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new PathEffectView; }
1908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory);
191