aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleOvalTest.cpp
blob: ef9c20d031dbcd14a532a10e76a08ed32209e390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"

static const int kILimit = 101;
static const SkScalar kLimit = SK_Scalar1 * kILimit;

class OvalTestView : public SampleView {
public:
    SkSize      fSize;
    SkPMColor   fInsideColor;   // signals an interior pixel that was not set
    SkPMColor   fOutsideColor;  // signals an exterior pixels that was set
    SkBitmap    fBitmap;

    OvalTestView() {
        fSize.set(SK_Scalar1, SK_Scalar1);

        fBitmap.allocN32Pixels(kILimit, kILimit);

        fInsideColor = SkPreMultiplyColor(SK_ColorRED);
        fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN);

        this->setBGColor(0xFFDDDDDD);
    }

protected:
    // overrides from SkEventSink
    virtual bool onQuery(SkEvent* evt) {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, "OvalTest");
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }

    void drawOval() {
        SkCanvas canvas(fBitmap);
        SkPaint p;

        fBitmap.eraseColor(SK_ColorTRANSPARENT);
        canvas.drawOval(SkRect::MakeSize(fSize), p);
    }

    int checkOval(int* flatCount, int* buldgeCount) {
        int flatc = 0;
        int buldgec = 0;
        const SkScalar rad = SkScalarHalf(fSize.width());
        SkScalar cx = SkScalarHalf(fSize.width());
        SkScalar cy = SkScalarHalf(fSize.height());
        for (int y = 0; y < kILimit; y++) {
            for (int x = 0; x < kILimit; x++) {
                // measure from pixel centers
                SkScalar px = SkIntToScalar(x) + SK_ScalarHalf;
                SkScalar py = SkIntToScalar(y) + SK_ScalarHalf;

                SkPMColor* ptr = fBitmap.getAddr32(x, y);
                SkScalar dist = SkPoint::Length(px - cx, py - cy);
                if (dist <= rad && !*ptr) {
                    flatc++;
                    *ptr = fInsideColor;
                } else if (dist > rad && *ptr) {
                    buldgec++;
                    *ptr = fOutsideColor;
                }
            }
        }
        if (flatCount) *flatCount = flatc;
        if (buldgeCount) *buldgeCount = buldgec;
        return flatc + buldgec;
    }

    virtual void onDrawContent(SkCanvas* canvas) {
        this->drawOval();
        int flatCount, buldgeCount;
        this->checkOval(&flatCount, &buldgeCount);
        this->inval(NULL);

        canvas->drawBitmap(fBitmap, SkIntToScalar(20), SkIntToScalar(20), NULL);


        static int gFlatCount;
        static int gBuldgeCount;
        gFlatCount += flatCount;
        gBuldgeCount += buldgeCount;

        if (fSize.fWidth < kLimit) {
            SkDebugf("--- width=%g, flat=%d buldge=%d total: flat=%d buldge=%d\n", fSize.fWidth,
                     flatCount, buldgeCount, gFlatCount, gBuldgeCount);
            fSize.fWidth += SK_Scalar1;
            fSize.fHeight += SK_Scalar1;
        } else {
         //   fSize.set(SK_Scalar1, SK_Scalar1);
        }
    }

    virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
        this->inval(NULL);
        return NULL;
    }

private:
    typedef SampleView INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

static SkView* MyFactory() { return new OvalTestView; }
static SkViewRegister reg(MyFactory);