1/* 2 * Copyright 2011 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "gm/gm.h" 9#include "include/core/SkCanvas.h" 10#include "include/core/SkColor.h" 11#include "include/core/SkPaint.h" 12#include "include/core/SkPathBuilder.h" 13#include "include/core/SkRect.h" 14#include "include/core/SkScalar.h" 15#include "include/core/SkTypes.h" 16#include "include/utils/SkRandom.h" 17 18static void test_hittest(SkCanvas* canvas, const SkPath& path) { 19 SkPaint paint; 20 SkRect r = path.getBounds(); 21 22 paint.setColor(SK_ColorRED); 23 canvas->drawPath(path, paint); 24 25 const SkScalar MARGIN = SkIntToScalar(4); 26 27 paint.setColor(0x800000FF); 28 for (SkScalar y = r.fTop + SK_ScalarHalf - MARGIN; y < r.fBottom + MARGIN; y += SK_Scalar1) { 29 for (SkScalar x = r.fLeft + SK_ScalarHalf - MARGIN; x < r.fRight + MARGIN; x += SK_Scalar1) { 30 if (path.contains(x, y)) { 31 canvas->drawPoint(x, y, paint); 32 } 33 } 34 } 35} 36 37DEF_SIMPLE_GM_CAN_FAIL(hittestpath, canvas, errorMsg, 700, 460) { 38 if (canvas->recordingContext()) { 39 // GPU rasterization results vary greatly from platform to platform. We can't use them as 40 // an expected result for our internal SkPath::contains(). 41 *errorMsg = "This test is for CPU configs only."; 42 return skiagm::DrawResult::kSkip; 43 } 44 45 SkPathBuilder b; 46 SkRandom rand; 47 48 int scale = 300; 49 for (int i = 0; i < 4; ++i) { 50 // get the random values deterministically 51 SkScalar randoms[12]; 52 for (int index = 0; index < (int) SK_ARRAY_COUNT(randoms); ++index) { 53 randoms[index] = rand.nextUScalar1(); 54 } 55 b.lineTo(randoms[0] * scale, randoms[1] * scale) 56 .quadTo(randoms[2] * scale, randoms[3] * scale, 57 randoms[4] * scale, randoms[5] * scale) 58 .cubicTo(randoms[6] * scale, randoms[7] * scale, 59 randoms[8] * scale, randoms[9] * scale, 60 randoms[10] * scale, randoms[11] * scale); 61 } 62 63 b.setFillType(SkPathFillType::kEvenOdd); 64 b.offset(SkIntToScalar(20), SkIntToScalar(20)); 65 66 SkPath path = b.detach(); 67 68 test_hittest(canvas, path); 69 70 canvas->translate(SkIntToScalar(scale), 0); 71 path.setFillType(SkPathFillType::kWinding); 72 73 test_hittest(canvas, path); 74 return skiagm::DrawResult::kOk; 75} 76