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@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkDevice.h" |
| 12 | #include "SkPaint.h" |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 13 | #include "SkShader.h" |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 14 | |
| 15 | static SkBitmap createBitmap(int n) { |
| 16 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 17 | bitmap.allocN32Pixels(n, n); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 18 | bitmap.eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 19 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 20 | SkCanvas canvas(bitmap); |
| 21 | SkRect r; |
| 22 | r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); |
| 23 | SkPaint paint; |
| 24 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 25 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 26 | paint.setColor(SK_ColorRED); |
| 27 | canvas.drawOval(r, paint); |
| 28 | paint.setColor(SK_ColorBLUE); |
| 29 | paint.setStrokeWidth(SkIntToScalar(n)/15); |
| 30 | paint.setStyle(SkPaint::kStroke_Style); |
| 31 | canvas.drawLine(0, 0, r.fRight, r.fBottom, paint); |
| 32 | canvas.drawLine(0, r.fBottom, r.fRight, 0, paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 34 | return bitmap; |
| 35 | } |
| 36 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 37 | class MipMapView : public SampleView { |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 38 | SkBitmap fBitmap; |
| 39 | enum { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 40 | N = 64 |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 41 | }; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 42 | bool fOnce; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 43 | public: |
| 44 | MipMapView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 45 | fOnce = false; |
| 46 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 48 | void init() { |
| 49 | if (fOnce) { |
| 50 | return; |
| 51 | } |
| 52 | fOnce = true; |
| 53 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 54 | fBitmap = createBitmap(N); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 56 | fWidth = N; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 57 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 59 | protected: |
| 60 | // overrides from SkEventSink |
| 61 | virtual bool onQuery(SkEvent* evt) { |
| 62 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 63 | SampleCode::TitleR(evt, "MipMaps"); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | return this->INHERITED::onQuery(evt); |
| 67 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 69 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 70 | this->init(); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 71 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 72 | static const SkPaint::FilterLevel gLevel[] = { |
| 73 | SkPaint::kNone_FilterLevel, |
| 74 | SkPaint::kLow_FilterLevel, |
| 75 | SkPaint::kMedium_FilterLevel, |
| 76 | SkPaint::kHigh_FilterLevel, |
| 77 | }; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 78 | |
| 79 | SkPaint paint; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 80 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 81 | for (size_t i = 0; i < SK_ARRAY_COUNT(gLevel); ++i) { |
reed@google.com | d9763a5 | 2013-07-24 17:10:05 +0000 | [diff] [blame] | 82 | SkScalar x = 10.0f + i * 100; |
| 83 | SkScalar y = 10.0f; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 84 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 85 | paint.setFilterLevel(gLevel[i]); |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 86 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 87 | canvas->drawBitmap(fBitmap, x, y, &paint); |
| 88 | } |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 89 | this->inval(NULL); |
| 90 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 91 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 92 | private: |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 93 | int fWidth; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 94 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 95 | typedef SampleView INHERITED; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | ////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | static SkView* MyFactory() { return new MipMapView; } |
| 101 | static SkViewRegister reg(MyFactory); |