blob: a105b82801b3d4bf038afcd995bf069dd4a8ef1d [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
25 fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kILimit, kILimit);
26 fBitmap.allocPixels();
27
28 fInsideColor = SkPreMultiplyColor(SK_ColorRED);
29 fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN);
reed@google.com81e3d7f2011-06-01 12:42:36 +000030
31 this->setBGColor(0xFFDDDDDD);
reed@google.com1d12b1f2011-03-03 13:23:35 +000032 }
33
34protected:
35 // overrides from SkEventSink
36 virtual bool onQuery(SkEvent* evt) {
37 if (SampleCode::TitleQ(*evt)) {
38 SampleCode::TitleR(evt, "OvalTest");
39 return true;
40 }
41 return this->INHERITED::onQuery(evt);
42 }
43
reed@google.com1d12b1f2011-03-03 13:23:35 +000044 void drawOval() {
45 SkCanvas canvas(fBitmap);
46 SkPaint p;
47
junov@google.comdbfac8a2012-12-06 21:47:40 +000048 fBitmap.eraseColor(SK_ColorTRANSPARENT);
reed@google.com1d12b1f2011-03-03 13:23:35 +000049 canvas.drawOval(SkRect::MakeSize(fSize), p);
50 }
51
52 int checkOval(int* flatCount, int* buldgeCount) {
53 int flatc = 0;
54 int buldgec = 0;
55 const SkScalar rad = SkScalarHalf(fSize.width());
56 SkScalar cx = SkScalarHalf(fSize.width());
57 SkScalar cy = SkScalarHalf(fSize.height());
58 for (int y = 0; y < kILimit; y++) {
59 for (int x = 0; x < kILimit; x++) {
60 // measure from pixel centers
61 SkScalar px = SkIntToScalar(x) + SK_ScalarHalf;
62 SkScalar py = SkIntToScalar(y) + SK_ScalarHalf;
63
64 SkPMColor* ptr = fBitmap.getAddr32(x, y);
65 SkScalar dist = SkPoint::Length(px - cx, py - cy);
66 if (dist <= rad && !*ptr) {
67 flatc++;
68 *ptr = fInsideColor;
69 } else if (dist > rad && *ptr) {
70 buldgec++;
71 *ptr = fOutsideColor;
72 }
73 }
74 }
75 if (flatCount) *flatCount = flatc;
76 if (buldgeCount) *buldgeCount = buldgec;
77 return flatc + buldgec;
78 }
79
reed@google.com81e3d7f2011-06-01 12:42:36 +000080 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com1d12b1f2011-03-03 13:23:35 +000081 this->drawOval();
82 int flatCount, buldgeCount;
83 this->checkOval(&flatCount, &buldgeCount);
84 this->inval(NULL);
85
86 canvas->drawBitmap(fBitmap, SkIntToScalar(20), SkIntToScalar(20), NULL);
87
88
89 static int gFlatCount;
90 static int gBuldgeCount;
91 gFlatCount += flatCount;
92 gBuldgeCount += buldgeCount;
93
94 if (fSize.fWidth < kLimit) {
95 SkDebugf("--- width=%g, flat=%d buldge=%d total: flat=%d buldge=%d\n", fSize.fWidth,
96 flatCount, buldgeCount, gFlatCount, gBuldgeCount);
97 fSize.fWidth += SK_Scalar1;
98 fSize.fHeight += SK_Scalar1;
99 } else {
100 // fSize.set(SK_Scalar1, SK_Scalar1);
101 }
102 }
103
sugoi@google.com9c55f802013-03-07 20:52:59 +0000104 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@google.com1d12b1f2011-03-03 13:23:35 +0000105 this->inval(NULL);
106 return NULL;
107 }
108
109private:
reed@google.com81e3d7f2011-06-01 12:42:36 +0000110 typedef SampleView INHERITED;
reed@google.com1d12b1f2011-03-03 13:23:35 +0000111};
112
113///////////////////////////////////////////////////////////////////////////////
114
115static SkView* MyFactory() { return new OvalTestView; }
116static SkViewRegister reg(MyFactory);