blob: de5aac524deed0830a516c9e0e77abece1cf573b [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 };
43public:
44 MipMapView() {
45 fBitmap = createBitmap(N);
46
47 fWidth = N;
reed@android.comad687ce2009-06-16 01:50:25 +000048 }
49
50protected:
51 // overrides from SkEventSink
52 virtual bool onQuery(SkEvent* evt) {
53 if (SampleCode::TitleQ(*evt)) {
reed@android.comf2b98d62010-12-20 18:26:13 +000054 SampleCode::TitleR(evt, "MipMaps");
reed@android.comad687ce2009-06-16 01:50:25 +000055 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.com0baf1932009-06-24 12:41:42 +000075 for (int i = 0; i < 6; i++) {
reed@android.comad687ce2009-06-16 01:50:25 +000076 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.org5fd92432011-05-05 01:59:48 +000089 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.comad687ce2009-06-16 01:50:25 +000090 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
reed@android.com0baf1932009-06-24 12:41:42 +000091
reed@google.com261b8e22011-04-14 17:53:24 +000092 canvas->scale(1.00000001f, 0.9999999f);
reed@android.comad687ce2009-06-16 01:50:25 +000093
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.com44177402009-11-23 21:07:51 +0000100
101 SkScalar time = SampleCode::GetAnimScalar(SkIntToScalar(1)/4,
102 SkIntToScalar(2));
103 if (time >= SK_Scalar1) {
104 time = SkIntToScalar(2) - time;
reed@android.comad687ce2009-06-16 01:50:25 +0000105 }
reed@android.com44177402009-11-23 21:07:51 +0000106 fWidth = 8 + SkScalarRound(N * time);
reed@android.comad687ce2009-06-16 01:50:25 +0000107
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.com83f7bc32009-07-17 02:42:41 +0000119 canvas->translate(-SkIntToScalar(N + 8), SkIntToScalar(N + 8));
reed@android.comad687ce2009-06-16 01:50:25 +0000120 canvas->drawBitmapRect(bitmap, NULL, dst, NULL);
121 canvas->translate(SkIntToScalar(N + 8), 0);
122 canvas->drawBitmapRect(bitmap, NULL, dst, &paint);
123
reed@android.com83f7bc32009-07-17 02:42:41 +0000124 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.comad687ce2009-06-16 01:50:25 +0000137 this->inval(NULL);
138 }
139
140private:
reed@android.com44177402009-11-23 21:07:51 +0000141 int fWidth;
reed@android.comad687ce2009-06-16 01:50:25 +0000142
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000143 typedef SampleView INHERITED;
reed@android.comad687ce2009-06-16 01:50:25 +0000144};
145
146//////////////////////////////////////////////////////////////////////////////
147
148static SkView* MyFactory() { return new MipMapView; }
149static SkViewRegister reg(MyFactory);
150