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; |
| 17 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n); |
| 18 | bitmap.allocPixels(); |
| 19 | bitmap.eraseColor(0); |
| 20 | |
| 21 | SkCanvas canvas(bitmap); |
| 22 | SkRect r; |
| 23 | r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n)); |
| 24 | SkPaint paint; |
| 25 | paint.setAntiAlias(true); |
| 26 | |
| 27 | paint.setColor(SK_ColorRED); |
| 28 | canvas.drawOval(r, paint); |
| 29 | paint.setColor(SK_ColorBLUE); |
| 30 | paint.setStrokeWidth(SkIntToScalar(n)/15); |
| 31 | paint.setStyle(SkPaint::kStroke_Style); |
| 32 | canvas.drawLine(0, 0, r.fRight, r.fBottom, paint); |
| 33 | canvas.drawLine(0, r.fBottom, r.fRight, 0, paint); |
| 34 | |
| 35 | return bitmap; |
| 36 | } |
| 37 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 38 | class MipMapView : public SampleView { |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 39 | SkBitmap fBitmap; |
| 40 | enum { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 41 | N = 64 |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 42 | }; |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 43 | bool fOnce; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 44 | public: |
| 45 | MipMapView() { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 46 | fOnce = false; |
| 47 | } |
| 48 | |
| 49 | void init() { |
| 50 | if (fOnce) { |
| 51 | return; |
| 52 | } |
| 53 | fOnce = true; |
| 54 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 55 | fBitmap = createBitmap(N); |
| 56 | |
| 57 | fWidth = N; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
| 61 | // overrides from SkEventSink |
| 62 | virtual bool onQuery(SkEvent* evt) { |
| 63 | if (SampleCode::TitleQ(*evt)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 64 | SampleCode::TitleR(evt, "MipMaps"); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | return this->INHERITED::onQuery(evt); |
| 68 | } |
| 69 | |
| 70 | void drawN(SkCanvas* canvas, const SkBitmap& bitmap) { |
| 71 | SkAutoCanvasRestore acr(canvas, true); |
| 72 | for (int i = N; i > 1; i >>= 1) { |
| 73 | canvas->drawBitmap(bitmap, 0, 0, NULL); |
| 74 | canvas->translate(SkIntToScalar(N + 8), 0); |
| 75 | canvas->scale(SK_ScalarHalf, SK_ScalarHalf); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void drawN2(SkCanvas* canvas, const SkBitmap& bitmap) { |
| 80 | SkBitmap bg; |
| 81 | bg.setConfig(SkBitmap::kARGB_8888_Config, N, N); |
| 82 | bg.allocPixels(); |
| 83 | |
| 84 | SkAutoCanvasRestore acr(canvas, true); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 85 | for (int i = 0; i < 6; i++) { |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 86 | bg.eraseColor(0); |
| 87 | SkCanvas c(bg); |
| 88 | c.scale(SK_Scalar1 / (1 << i), SK_Scalar1 / (1 << i)); |
| 89 | c.drawBitmap(bitmap, 0, 0, NULL); |
| 90 | |
| 91 | canvas->save(); |
| 92 | canvas->scale(SkIntToScalar(1 << i), SkIntToScalar(1 << i)); |
| 93 | canvas->drawBitmap(bg, 0, 0, NULL); |
| 94 | canvas->restore(); |
| 95 | canvas->translate(SkIntToScalar(N + 8), 0); |
| 96 | } |
| 97 | } |
| 98 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 99 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | 7b18590 | 2012-03-12 21:13:48 +0000 | [diff] [blame] | 100 | this->init(); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 101 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 102 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 103 | canvas->scale(1.00000001f, 0.9999999f); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 104 | |
| 105 | drawN2(canvas, fBitmap); |
| 106 | |
| 107 | canvas->translate(0, SkIntToScalar(N + 8)); |
| 108 | SkBitmap bitmap(fBitmap); |
| 109 | bitmap.buildMipMap(); |
| 110 | drawN2(canvas, bitmap); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 111 | |
| 112 | SkScalar time = SampleCode::GetAnimScalar(SkIntToScalar(1)/4, |
| 113 | SkIntToScalar(2)); |
| 114 | if (time >= SK_Scalar1) { |
| 115 | time = SkIntToScalar(2) - time; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 116 | } |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 117 | fWidth = 8 + SkScalarRound(N * time); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 118 | |
| 119 | SkRect dst; |
| 120 | dst.set(0, 0, SkIntToScalar(fWidth), SkIntToScalar(fWidth)); |
| 121 | |
| 122 | SkPaint paint; |
| 123 | paint.setFilterBitmap(true); |
| 124 | paint.setAntiAlias(true); |
| 125 | |
| 126 | canvas->translate(0, SkIntToScalar(N + 8)); |
| 127 | canvas->drawBitmapRect(fBitmap, NULL, dst, NULL); |
| 128 | canvas->translate(SkIntToScalar(N + 8), 0); |
| 129 | canvas->drawBitmapRect(fBitmap, NULL, dst, &paint); |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 130 | canvas->translate(-SkIntToScalar(N + 8), SkIntToScalar(N + 8)); |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 131 | canvas->drawBitmapRect(bitmap, NULL, dst, NULL); |
| 132 | canvas->translate(SkIntToScalar(N + 8), 0); |
| 133 | canvas->drawBitmapRect(bitmap, NULL, dst, &paint); |
| 134 | |
reed@android.com | 83f7bc3 | 2009-07-17 02:42:41 +0000 | [diff] [blame] | 135 | SkShader* s = SkShader::CreateBitmapShader(bitmap, |
| 136 | SkShader::kRepeat_TileMode, |
| 137 | SkShader::kRepeat_TileMode); |
| 138 | paint.setShader(s)->unref(); |
| 139 | SkMatrix m; |
| 140 | m.setScale(SkIntToScalar(fWidth) / N, |
| 141 | SkIntToScalar(fWidth) / N); |
| 142 | s->setLocalMatrix(m); |
| 143 | SkRect r; |
| 144 | r.set(0, 0, SkIntToScalar(4*N), SkIntToScalar(5*N/2)); |
| 145 | r.offset(SkIntToScalar(N + 12), -SkIntToScalar(N + 4)); |
| 146 | canvas->drawRect(r, paint); |
| 147 | |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 148 | this->inval(NULL); |
| 149 | } |
| 150 | |
| 151 | private: |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 152 | int fWidth; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 153 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 154 | typedef SampleView INHERITED; |
reed@android.com | ad687ce | 2009-06-16 01:50:25 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | static SkView* MyFactory() { return new MipMapView; } |
| 160 | static SkViewRegister reg(MyFactory); |
| 161 | |