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 | |
| 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.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 30 | |
| 31 | this->setBGColor(0xFFDDDDDD); |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | protected: |
| 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.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 44 | void drawOval() { |
| 45 | SkCanvas canvas(fBitmap); |
| 46 | SkPaint p; |
| 47 | |
| 48 | fBitmap.eraseColor(0); |
| 49 | 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.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 80 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 81 | 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 | |
| 104 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 105 | this->inval(NULL); |
| 106 | return NULL; |
| 107 | } |
| 108 | |
| 109 | private: |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 110 | typedef SampleView INHERITED; |
reed@google.com | 1d12b1f | 2011-03-03 13:23:35 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
| 115 | static SkView* MyFactory() { return new OvalTestView; } |
| 116 | static SkViewRegister reg(MyFactory); |
| 117 | |