12b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips/*
22b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips * Copyright 2014 Google Inc.
32b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips *
42b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips * Use of this source code is governed by a BSD-style license that can be
52b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips * found in the LICENSE file.
62b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips */
72b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
8af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org#include "Test.h"
9af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org
10af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org#include "SkDashPathEffect.h"
11af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org#include "SkWriteBuffer.h"
12af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org
13af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
14af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org// fInitialDashLength < 0 (a signal the effect is nonsense).  Here we test that it flattens.
15af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org
16af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.orgDEF_TEST(DashPathEffectTest_crbug_348821, r) {
17af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f };  // Values from bug.
18af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    const int count = 2;
19af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    SkScalar phase = SK_ScalarInfinity;  // Used to force the bad fInitialDashLength = -1 path.
20af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, count, phase));
21af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org
22af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    // NULL -> refuses to work with flattening framework.
23af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    REPORTER_ASSERT(r, dash->getFactory() != NULL);
24af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org
25af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    SkWriteBuffer buffer;
26af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    buffer.writeFlattenable(dash);
27af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org    REPORTER_ASSERT(r, buffer.bytesWritten() > 12);  // We'd write 12 if broken, >=40 if not.
28af5346a39cf2412eeb2da7339a51e44ae68dd677commit-bot@chromium.org}
292b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
302b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips// Test out the asPoint culling behavior.
312b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillipsDEF_TEST(DashPathEffectTest_asPoints, r) {
322b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
332b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    const SkScalar intervals[] = { 1.0f, 1.0f };
342b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    const int count = 2;
352b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, count, 0.0f));
362b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
372b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
382b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
392b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    const struct {
402b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        SkPoint fPts[2];
412b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        bool    fExpectedResult;
422b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    } testCases[] = {
432b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { { -5.0f,  0.5f }, { -4.0f,  0.5f } }, false },   // off to the left
442b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  4.0f,  0.5f }, {  5.0f,  0.5f } }, false },   // off to the right
452b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f,  4.0f }, {  0.5f,  5.0f } }, false },   // off the bottom
462b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f, -5.0f }, {  0.5f, -4.0f } }, false },   // off the top
472b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f,  0.2f }, {  0.5f,  0.8f } }, true  },   // entirely inside vertical
482b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.2f,  0.5f }, {  0.8f,  0.5f } }, true  },   // entirely inside horizontal
492b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f, -5.0f }, {  0.5f,  5.0f } }, true  },   // straddles both sides vertically
502b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { { -5.0f,  0.5f }, {  5.0f,  0.5f } }, true  },   // straddles both sides horizontally
512b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f, -5.0f }, {  0.5f,  0.5f } }, true  },   // straddles top
522b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f,  5.0f }, {  0.5f,  0.5f } }, true  },   // straddles bottom
532b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { { -5.0f,  0.5f }, {  0.5f,  0.5f } }, true  },   // straddles left
542b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  5.0f,  0.5f }, {  0.5f,  0.5f } }, true  },   // straddles right
552b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        { { {  0.5f,  0.5f }, {  0.5f,  0.5f } }, false },   // zero length
562b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    };
572b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
582b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    SkPaint paint;
592b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    paint.setStyle(SkPaint::kStroke_Style);
602b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    paint.setStrokeWidth(1.0f);
612b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    SkStrokeRec rec(paint);
622b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
632b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    static const int kNumMats = 3;
642b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    SkMatrix mats[kNumMats];
652b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    mats[0].reset();
662b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    mats[1].setRotate(90, 0.5f, 0.5f);
672b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    mats[2].setTranslate(10.0f, 10.0f);
682b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
692b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    for (int i = 0; i < kNumMats; ++i) {
702b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
712b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips            for (int k = 0; k < 2; ++k) {  // exercise alternating endpoints
722b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                SkPathEffect::PointData results;
732b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                SkPath src;
742b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
752b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                src.moveTo(testCases[j].fPts[k]);
762b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                src.lineTo(testCases[j].fPts[(k+1)%2]);
772b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips
782b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull);
792b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                if (i < 2) {
802b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                    REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
812b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                } else {
822b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                    // On the third pass all the lines should be outside the translated cull rect
832b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                    REPORTER_ASSERT(r, !actualResult);
842b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips                }
852b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips            }
862b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips        }
872b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips    }
882b9ee63c2423167cf39d7a5027db944057c74cfdrobertphillips}
89