• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
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  */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 
12 static const int kILimit = 101;
13 static const SkScalar kLimit = SK_Scalar1 * kILimit;
14 
15 class OvalTestView : public SampleView {
16 public:
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 
OvalTestView()22     OvalTestView() {
23         fSize.set(SK_Scalar1, SK_Scalar1);
24 
25         fBitmap.allocN32Pixels(kILimit, kILimit);
26 
27         fInsideColor = SkPreMultiplyColor(SK_ColorRED);
28         fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN);
29 
30         this->setBGColor(0xFFDDDDDD);
31     }
32 
33 protected:
34     // overrides from SkEventSink
onQuery(SkEvent * evt)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 
drawOval()43     void drawOval() {
44         SkCanvas canvas(fBitmap);
45         SkPaint p;
46 
47         fBitmap.eraseColor(SK_ColorTRANSPARENT);
48         canvas.drawOval(SkRect::MakeSize(fSize), p);
49     }
50 
checkOval(int * flatCount,int * buldgeCount)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 
onDrawContent(SkCanvas * canvas)79     virtual void onDrawContent(SkCanvas* canvas) {
80         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 
onFindClickHandler(SkScalar x,SkScalar y,unsigned)103     virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
104         this->inval(NULL);
105         return NULL;
106     }
107 
108 private:
109     typedef SampleView INHERITED;
110 };
111 
112 ///////////////////////////////////////////////////////////////////////////////
113 
MyFactory()114 static SkView* MyFactory() { return new OvalTestView; }
115 static SkViewRegister reg(MyFactory);
116