blob: a7e31660023112685c7ba792ee329e0e6a99cdaf [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.comad687ce2009-06-16 01:50:25 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkPaint.h"
reed@android.com83f7bc32009-07-17 02:42:41 +000013#include "SkShader.h"
reed@android.comad687ce2009-06-16 01:50:25 +000014
15static 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.org5fd92432011-05-05 01:59:48 +000038class MipMapView : public SampleView {
reed@android.comad687ce2009-06-16 01:50:25 +000039 SkBitmap fBitmap;
40 enum {
reed@android.comf2b98d62010-12-20 18:26:13 +000041 N = 64
reed@android.comad687ce2009-06-16 01:50:25 +000042 };
reed@google.com7b185902012-03-12 21:13:48 +000043 bool fOnce;
reed@android.comad687ce2009-06-16 01:50:25 +000044public:
45 MipMapView() {
reed@google.com7b185902012-03-12 21:13:48 +000046 fOnce = false;
47 }
48
49 void init() {
50 if (fOnce) {
51 return;
52 }
53 fOnce = true;
54
reed@android.comad687ce2009-06-16 01:50:25 +000055 fBitmap = createBitmap(N);
56
57 fWidth = N;
reed@android.comad687ce2009-06-16 01:50:25 +000058 }
59
60protected:
61 // overrides from SkEventSink
62 virtual bool onQuery(SkEvent* evt) {
63 if (SampleCode::TitleQ(*evt)) {
reed@android.comf2b98d62010-12-20 18:26:13 +000064 SampleCode::TitleR(evt, "MipMaps");
reed@android.comad687ce2009-06-16 01:50:25 +000065 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.com0baf1932009-06-24 12:41:42 +000085 for (int i = 0; i < 6; i++) {
reed@android.comad687ce2009-06-16 01:50:25 +000086 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.org5fd92432011-05-05 01:59:48 +000099 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +0000100 this->init();
reed@android.comad687ce2009-06-16 01:50:25 +0000101 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com0baf1932009-06-24 12:41:42 +0000102
reed@google.com261b8e22011-04-14 17:53:24 +0000103 canvas->scale(1.00000001f, 0.9999999f);
reed@android.comad687ce2009-06-16 01:50:25 +0000104
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.com44177402009-11-23 21:07:51 +0000111
112 SkScalar time = SampleCode::GetAnimScalar(SkIntToScalar(1)/4,
113 SkIntToScalar(2));
114 if (time >= SK_Scalar1) {
115 time = SkIntToScalar(2) - time;
reed@android.comad687ce2009-06-16 01:50:25 +0000116 }
reed@android.com44177402009-11-23 21:07:51 +0000117 fWidth = 8 + SkScalarRound(N * time);
reed@android.comad687ce2009-06-16 01:50:25 +0000118
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.com83f7bc32009-07-17 02:42:41 +0000130 canvas->translate(-SkIntToScalar(N + 8), SkIntToScalar(N + 8));
reed@android.comad687ce2009-06-16 01:50:25 +0000131 canvas->drawBitmapRect(bitmap, NULL, dst, NULL);
132 canvas->translate(SkIntToScalar(N + 8), 0);
133 canvas->drawBitmapRect(bitmap, NULL, dst, &paint);
134
reed@android.com83f7bc32009-07-17 02:42:41 +0000135 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.comad687ce2009-06-16 01:50:25 +0000148 this->inval(NULL);
149 }
150
151private:
reed@android.com44177402009-11-23 21:07:51 +0000152 int fWidth;
reed@android.comad687ce2009-06-16 01:50:25 +0000153
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000154 typedef SampleView INHERITED;
reed@android.comad687ce2009-06-16 01:50:25 +0000155};
156
157//////////////////////////////////////////////////////////////////////////////
158
159static SkView* MyFactory() { return new MipMapView; }
160static SkViewRegister reg(MyFactory);
161