epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 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 | |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 15 | class OvalTestView : public SampleView { |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 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 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 22 | OvalTestView() { |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 23 | fSize.set(SK_Scalar1, SK_Scalar1); |
| 24 | |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 25 | fBitmap.allocN32Pixels(kILimit, kILimit); |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 26 | |
| 27 | fInsideColor = SkPreMultiplyColor(SK_ColorRED); |
| 28 | fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN); |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 29 | |
| 30 | this->setBGColor(0xFFDDDDDD); |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | protected: |
| 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.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 43 | void drawOval() { |
| 44 | SkCanvas canvas(fBitmap); |
| 45 | SkPaint p; |
| 46 | |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 47 | fBitmap.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 48 | 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.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 79 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 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 | |
sugoi@google.com | 9c55f80 | 2013-03-07 20:52:59 +0000 | [diff] [blame] | 103 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE { |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 104 | this->inval(NULL); |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 109 | typedef SampleView INHERITED; |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | static SkView* MyFactory() { return new OvalTestView; } |
| 115 | static SkViewRegister reg(MyFactory); |