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