blob: ef9c20d031dbcd14a532a10e76a08ed32209e390 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.com1d12b1f2011-03-03 13:23:35 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11
12static const int kILimit = 101;
13static const SkScalar kLimit = SK_Scalar1 * kILimit;
14
reed@google.com81e3d7f2011-06-01 12:42:36 +000015class OvalTestView : public SampleView {
reed@google.com1d12b1f2011-03-03 13:23:35 +000016public:
17 SkSize fSize;
18 SkPMColor fInsideColor; // signals an interior pixel that was not set
19 SkPMColor fOutsideColor; // signals an exterior pixels that was set
20 SkBitmap fBitmap;
21
rmistry@google.comae933ce2012-08-23 18:19:56 +000022 OvalTestView() {
reed@google.com1d12b1f2011-03-03 13:23:35 +000023 fSize.set(SK_Scalar1, SK_Scalar1);
24
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000025 fBitmap.allocN32Pixels(kILimit, kILimit);
reed@google.com1d12b1f2011-03-03 13:23:35 +000026
27 fInsideColor = SkPreMultiplyColor(SK_ColorRED);
28 fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN);
reed@google.com81e3d7f2011-06-01 12:42:36 +000029
30 this->setBGColor(0xFFDDDDDD);
reed@google.com1d12b1f2011-03-03 13:23:35 +000031 }
32
33protected:
34 // overrides from SkEventSink
35 virtual bool onQuery(SkEvent* evt) {
36 if (SampleCode::TitleQ(*evt)) {
37 SampleCode::TitleR(evt, "OvalTest");
38 return true;
39 }
40 return this->INHERITED::onQuery(evt);
41 }
42
reed@google.com1d12b1f2011-03-03 13:23:35 +000043 void drawOval() {
44 SkCanvas canvas(fBitmap);
45 SkPaint p;
46
junov@google.comdbfac8a2012-12-06 21:47:40 +000047 fBitmap.eraseColor(SK_ColorTRANSPARENT);
reed@google.com1d12b1f2011-03-03 13:23:35 +000048 canvas.drawOval(SkRect::MakeSize(fSize), p);
49 }
50
51 int checkOval(int* flatCount, int* buldgeCount) {
52 int flatc = 0;
53 int buldgec = 0;
54 const SkScalar rad = SkScalarHalf(fSize.width());
55 SkScalar cx = SkScalarHalf(fSize.width());
56 SkScalar cy = SkScalarHalf(fSize.height());
57 for (int y = 0; y < kILimit; y++) {
58 for (int x = 0; x < kILimit; x++) {
59 // measure from pixel centers
60 SkScalar px = SkIntToScalar(x) + SK_ScalarHalf;
61 SkScalar py = SkIntToScalar(y) + SK_ScalarHalf;
62
63 SkPMColor* ptr = fBitmap.getAddr32(x, y);
64 SkScalar dist = SkPoint::Length(px - cx, py - cy);
65 if (dist <= rad && !*ptr) {
66 flatc++;
67 *ptr = fInsideColor;
68 } else if (dist > rad && *ptr) {
69 buldgec++;
70 *ptr = fOutsideColor;
71 }
72 }
73 }
74 if (flatCount) *flatCount = flatc;
75 if (buldgeCount) *buldgeCount = buldgec;
76 return flatc + buldgec;
77 }
78
reed@google.com81e3d7f2011-06-01 12:42:36 +000079 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com1d12b1f2011-03-03 13:23:35 +000080 this->drawOval();
81 int flatCount, buldgeCount;
82 this->checkOval(&flatCount, &buldgeCount);
83 this->inval(NULL);
84
85 canvas->drawBitmap(fBitmap, SkIntToScalar(20), SkIntToScalar(20), NULL);
86
87
88 static int gFlatCount;
89 static int gBuldgeCount;
90 gFlatCount += flatCount;
91 gBuldgeCount += buldgeCount;
92
93 if (fSize.fWidth < kLimit) {
94 SkDebugf("--- width=%g, flat=%d buldge=%d total: flat=%d buldge=%d\n", fSize.fWidth,
95 flatCount, buldgeCount, gFlatCount, gBuldgeCount);
96 fSize.fWidth += SK_Scalar1;
97 fSize.fHeight += SK_Scalar1;
98 } else {
99 // fSize.set(SK_Scalar1, SK_Scalar1);
100 }
101 }
102
sugoi@google.com9c55f802013-03-07 20:52:59 +0000103 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@google.com1d12b1f2011-03-03 13:23:35 +0000104 this->inval(NULL);
105 return NULL;
106 }
107
108private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000109 typedef SampleView INHERITED;
reed@google.com1d12b1f2011-03-03 13:23:35 +0000110};
111
112///////////////////////////////////////////////////////////////////////////////
113
114static SkView* MyFactory() { return new OvalTestView; }
115static SkViewRegister reg(MyFactory);