blob: 21bf0f0fe5572b4572118c245533285af8391b03 [file] [log] [blame]
reed@android.comad687ce2009-06-16 01:50:25 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkDevice.h"
5#include "SkPaint.h"
6
7static SkBitmap createBitmap(int n) {
8 SkBitmap bitmap;
9 bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n);
10 bitmap.allocPixels();
11 bitmap.eraseColor(0);
12
13 SkCanvas canvas(bitmap);
14 SkRect r;
15 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
16 SkPaint paint;
17 paint.setAntiAlias(true);
18
19 paint.setColor(SK_ColorRED);
20 canvas.drawOval(r, paint);
21 paint.setColor(SK_ColorBLUE);
22 paint.setStrokeWidth(SkIntToScalar(n)/15);
23 paint.setStyle(SkPaint::kStroke_Style);
24 canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
25 canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
26
27 return bitmap;
28}
29
30class MipMapView : public SkView {
31 SkBitmap fBitmap;
32 enum {
33 N = 128
34 };
35public:
36 MipMapView() {
37 fBitmap = createBitmap(N);
38
39 fWidth = N;
40 fDW = -1;
41 }
42
43protected:
44 // overrides from SkEventSink
45 virtual bool onQuery(SkEvent* evt) {
46 if (SampleCode::TitleQ(*evt)) {
47 SampleCode::TitleR(evt, "MapMaps");
48 return true;
49 }
50 return this->INHERITED::onQuery(evt);
51 }
52
53 void drawN(SkCanvas* canvas, const SkBitmap& bitmap) {
54 SkAutoCanvasRestore acr(canvas, true);
55 for (int i = N; i > 1; i >>= 1) {
56 canvas->drawBitmap(bitmap, 0, 0, NULL);
57 canvas->translate(SkIntToScalar(N + 8), 0);
58 canvas->scale(SK_ScalarHalf, SK_ScalarHalf);
59 }
60 }
61
62 void drawN2(SkCanvas* canvas, const SkBitmap& bitmap) {
63 SkBitmap bg;
64 bg.setConfig(SkBitmap::kARGB_8888_Config, N, N);
65 bg.allocPixels();
66
67 SkAutoCanvasRestore acr(canvas, true);
reed@android.com0baf1932009-06-24 12:41:42 +000068 for (int i = 0; i < 6; i++) {
reed@android.comad687ce2009-06-16 01:50:25 +000069 bg.eraseColor(0);
70 SkCanvas c(bg);
71 c.scale(SK_Scalar1 / (1 << i), SK_Scalar1 / (1 << i));
72 c.drawBitmap(bitmap, 0, 0, NULL);
73
74 canvas->save();
75 canvas->scale(SkIntToScalar(1 << i), SkIntToScalar(1 << i));
76 canvas->drawBitmap(bg, 0, 0, NULL);
77 canvas->restore();
78 canvas->translate(SkIntToScalar(N + 8), 0);
79 }
80 }
81
82 void drawBG(SkCanvas* canvas) {
83 canvas->drawColor(SK_ColorWHITE);
84 }
85
86 virtual void onDraw(SkCanvas* canvas) {
87 this->drawBG(canvas);
88
89 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com0baf1932009-06-24 12:41:42 +000090
91 canvas->scale(1.00000001, 0.9999999);
reed@android.comad687ce2009-06-16 01:50:25 +000092
93 drawN2(canvas, fBitmap);
94
95 canvas->translate(0, SkIntToScalar(N + 8));
96 SkBitmap bitmap(fBitmap);
97 bitmap.buildMipMap();
98 drawN2(canvas, bitmap);
99
100 fWidth += fDW;
101 if (fDW > 0 && fWidth > N) {
102 fDW = -fDW;
103 fWidth = N;
104 } else if (fDW < 0 && fWidth < 8) {
105 fDW = -fDW;
106 fWidth = 8;
107 }
108
109 SkRect dst;
110 dst.set(0, 0, SkIntToScalar(fWidth), SkIntToScalar(fWidth));
111
112 SkPaint paint;
113 paint.setFilterBitmap(true);
114 paint.setAntiAlias(true);
115
116 canvas->translate(0, SkIntToScalar(N + 8));
117 canvas->drawBitmapRect(fBitmap, NULL, dst, NULL);
118 canvas->translate(SkIntToScalar(N + 8), 0);
119 canvas->drawBitmapRect(fBitmap, NULL, dst, &paint);
120 canvas->translate(SkIntToScalar(N + 8), 0);
121 canvas->drawBitmapRect(bitmap, NULL, dst, NULL);
122 canvas->translate(SkIntToScalar(N + 8), 0);
123 canvas->drawBitmapRect(bitmap, NULL, dst, &paint);
124
125 this->inval(NULL);
126 }
127
128private:
129 int fWidth, fDW;
130
131 typedef SkView INHERITED;
132};
133
134//////////////////////////////////////////////////////////////////////////////
135
136static SkView* MyFactory() { return new MipMapView; }
137static SkViewRegister reg(MyFactory);
138