blob: 23359f046ea2861bc43c73ef83c63f505e6c3df5 [file] [log] [blame]
reeda40be092016-01-14 09:11:51 -08001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9
10#include "Resources.h"
11#include "SkBitmapScaler.h"
12#include "SkGradientShader.h"
13#include "SkTypeface.h"
14#include "SkImageDecoder.h"
15#include "SkStream.h"
16#include "SkPaint.h"
17#include "SkMipMap.h"
18#include "Resources.h"
19#include "sk_tool_utils.h"
20
reed32e0b4a2016-01-15 13:17:08 -080021#define SHOW_MIP_COLOR 0xFF000000
22
reed01dc44a2016-01-15 10:51:08 -080023static SkBitmap make_bitmap(int w, int h) {
reeda40be092016-01-14 09:11:51 -080024 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080025 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080026 SkCanvas canvas(bm);
27 canvas.clear(0xFFFFFFFF);
28 SkPaint paint;
29 paint.setStyle(SkPaint::kStroke_Style);
reed01dc44a2016-01-15 10:51:08 -080030 paint.setStrokeWidth(w / 16.0f);
reed32e0b4a2016-01-15 13:17:08 -080031 paint.setColor(SHOW_MIP_COLOR);
reed01dc44a2016-01-15 10:51:08 -080032 canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint);
reeda40be092016-01-14 09:11:51 -080033 return bm;
34}
35
reed01dc44a2016-01-15 10:51:08 -080036static SkBitmap make_bitmap2(int w, int h) {
reeda40be092016-01-14 09:11:51 -080037 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080038 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080039 SkCanvas canvas(bm);
40 canvas.clear(0xFFFFFFFF);
41 SkPaint paint;
reed32e0b4a2016-01-15 13:17:08 -080042 paint.setColor(SHOW_MIP_COLOR);
reeda40be092016-01-14 09:11:51 -080043 paint.setStyle(SkPaint::kStroke_Style);
44
45 SkScalar inset = 2;
reed01dc44a2016-01-15 10:51:08 -080046 SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f);
reeda40be092016-01-14 09:11:51 -080047 while (r.width() > 4) {
48 canvas.drawRect(r, paint);
49 r.inset(inset, inset);
50 inset += 1;
51 }
52 return bm;
53}
54
55#include "SkNx.h"
reed01dc44a2016-01-15 10:51:08 -080056static SkBitmap make_bitmap3(int w, int h) {
reeda40be092016-01-14 09:11:51 -080057 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080058 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080059 SkCanvas canvas(bm);
60 canvas.clear(0xFFFFFFFF);
61 SkPaint paint;
62 paint.setStyle(SkPaint::kStroke_Style);
63 paint.setStrokeWidth(2.1f);
reed32e0b4a2016-01-15 13:17:08 -080064 paint.setColor(SHOW_MIP_COLOR);
reeda40be092016-01-14 09:11:51 -080065
reed01dc44a2016-01-15 10:51:08 -080066 SkScalar s = SkIntToScalar(w);
reeda40be092016-01-14 09:11:51 -080067 Sk4f p(s, -s, -s, s);
68 Sk4f d(5);
69 while (p.kth<1>() < s) {
70 canvas.drawLine(p.kth<0>(),p.kth<1>(), p.kth<2>(), p.kth<3>(), paint);
71 p = p + d;
72 }
73 return bm;
74}
75
76class ShowMipLevels : public skiagm::GM {
77 const int fN;
78 SkBitmap fBM[4];
79
80public:
81 static unsigned gamma(unsigned n) {
82 float x = n / 255.0f;
83#if 0
84 x = sqrtf(x);
85#else
86 if (x > 0.0031308f) {
87 x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f;
88 } else {
89 x = 12.92f * x;
90 }
91#endif
92 return (int)(x * 255);
93 }
94
95 static void apply_gamma(const SkBitmap& bm) {
96 return; // below is our experiment for sRGB correction
97 bm.lockPixels();
98 for (int y = 0; y < bm.height(); ++y) {
99 for (int x = 0; x < bm.width(); ++x) {
100 SkPMColor c = *bm.getAddr32(x, y);
101 unsigned r = gamma(SkGetPackedR32(c));
102 unsigned g = gamma(SkGetPackedG32(c));
103 unsigned b = gamma(SkGetPackedB32(c));
104 *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
105 }
106 }
107 }
108
109 ShowMipLevels(int N) : fN(N) {
110 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(N, N, SK_ColorBLACK, SK_ColorWHITE, 2);
reed01dc44a2016-01-15 10:51:08 -0800111 fBM[1] = make_bitmap(N, N);
112 fBM[2] = make_bitmap2(N, N);
113 fBM[3] = make_bitmap3(N, N);
reeda40be092016-01-14 09:11:51 -0800114 }
115
116protected:
117
118 SkString onShortName() override {
119 SkString str;
120 str.printf("showmiplevels_%d", fN);
121 return str;
122 }
123
124 SkISize onISize() override {
125 return { 824, 862 };
126 }
127
128 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
129 SkBitmap bm;
130 orig.copyTo(&bm);
131 apply_gamma(bm);
132
133 canvas->drawBitmap(bm, x, y, nullptr);
134 SkPaint paint;
135 paint.setStyle(SkPaint::kStroke_Style);
136 paint.setColor(0xFFFFCCCC);
137 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
138 }
139
140 template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) {
141 SkScalar x = 4;
142 SkScalar y = 4;
143
144 SkPixmap prevPM;
145 baseBM.lockPixels();
146 baseBM.peekPixels(&prevPM);
147
148 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
149
150 int index = 0;
151 SkMipMap::Level level;
152 SkScalar scale = 0.5f;
153 while (mm->extractLevel(scale, &level)) {
reed01dc44a2016-01-15 10:51:08 -0800154 SkImageInfo info = SkImageInfo::Make(level.fWidth, level.fHeight,
155 baseBM.colorType(), baseBM.alphaType());
reeda40be092016-01-14 09:11:51 -0800156 SkPixmap levelPM{ info, level.fPixels, level.fRowBytes };
157
158 SkBitmap bm = func(prevPM, levelPM);
159 DrawAndFrame(canvas, bm, x, y);
160
161 if (info.width() <= 2 || info.height() <= 2) {
162 break;
163 }
164 if (index & 1) {
165 x += info.width() + 4;
166 } else {
167 y += info.height() + 4;
168 }
169 scale /= 2;
170 prevPM = levelPM;
171 index += 1;
172 }
173 }
174
175 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
176 SkAutoCanvasRestore acr(canvas, true);
177
178 drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
179 SkBitmap bm;
180 bm.installPixels(curr);
181 return bm;
182 });
183
184 const SkBitmapScaler::ResizeMethod methods[] = {
185 SkBitmapScaler::RESIZE_BOX,
186 SkBitmapScaler::RESIZE_TRIANGLE,
187 SkBitmapScaler::RESIZE_LANCZOS3,
188 SkBitmapScaler::RESIZE_HAMMING,
189 SkBitmapScaler::RESIZE_MITCHELL,
190 };
191
192 SkPixmap basePM;
193 orig.lockPixels();
194 orig.peekPixels(&basePM);
195 for (auto method : methods) {
196 canvas->translate(orig.width()/2 + 8.0f, 0);
197 drawLevels(canvas, orig, [basePM, method](const SkPixmap& prev, const SkPixmap& curr) {
198 SkBitmap bm;
199 SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.height());
200 return bm;
201 });
202 }
203 }
204
205 void onDraw(SkCanvas* canvas) override {
206 canvas->translate(4, 4);
207 for (const auto& bm : fBM) {
208 this->drawSet(canvas, bm);
209 canvas->translate(0, bm.height() * 0.85f);
210 }
211 }
212
213private:
214 typedef skiagm::GM INHERITED;
215};
216DEF_GM( return new ShowMipLevels(255); )
217DEF_GM( return new ShowMipLevels(256); )
218
reed01dc44a2016-01-15 10:51:08 -0800219///////////////////////////////////////////////////////////////////////////////////////////////////
220
221/**
222 * Show mip levels that were built, for all supported colortypes
223 */
224class ShowMipLevels2 : public skiagm::GM {
225 const int fW, fH;
226 SkBitmap fBM[4];
227
228public:
229 ShowMipLevels2(int w, int h) : fW(w), fH(h) {
reed32e0b4a2016-01-15 13:17:08 -0800230 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(w, h, SHOW_MIP_COLOR, SK_ColorWHITE, 2);
reed01dc44a2016-01-15 10:51:08 -0800231 fBM[1] = make_bitmap(w, h);
232 fBM[2] = make_bitmap2(w, h);
233 fBM[3] = make_bitmap3(w, h);
234 }
235
236protected:
237
238 SkString onShortName() override {
239 SkString str;
240 str.printf("showmiplevels2_%dx%d", fW, fH);
241 return str;
242 }
243
244 SkISize onISize() override {
245 return { 824, 862 };
246 }
247
248 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) {
249 canvas->drawBitmap(bm, x, y, nullptr);
250 SkPaint paint;
251 paint.setStyle(SkPaint::kStroke_Style);
252 paint.setColor(0xFFFFCCCC);
253 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
254 }
255
256 void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) {
257 SkScalar x = 4;
258 SkScalar y = 4;
259
260 SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
261
262 int index = 0;
263 SkMipMap::Level level;
264 SkScalar scale = 0.5f;
265 while (mm->extractLevel(scale, &level)) {
266 SkImageInfo info = SkImageInfo::Make(level.fWidth, level.fHeight,
267 baseBM.colorType(), baseBM.alphaType());
268
269 SkBitmap bm;
270 bm.installPixels(info, level.fPixels, level.fRowBytes);
271 DrawAndFrame(canvas, bm, x, y);
272
273 if (info.width() <= 2 || info.height() <= 2) {
274 break;
275 }
276 if (index & 1) {
277 x += info.width() + 4;
278 } else {
279 y += info.height() + 4;
280 }
281 scale /= 2;
282 index += 1;
283 }
284 }
285
286 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
287 const SkColorType ctypes[] = {
288 kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType
289 };
290
291 SkAutoCanvasRestore acr(canvas, true);
292
293 for (auto ctype : ctypes) {
294 SkBitmap bm;
295 orig.copyTo(&bm, ctype);
296 drawLevels(canvas, bm);
297 canvas->translate(orig.width()/2 + 8.0f, 0);
298 }
299 }
300
301 void onDraw(SkCanvas* canvas) override {
302 canvas->translate(4, 4);
303 for (const auto& bm : fBM) {
304 this->drawSet(canvas, bm);
305 // round so we always produce an integral translate, so the GOLD tool won't show
306 // unimportant diffs if this is drawn on a GPU with different rounding rules
307 // since we draw the bitmaps using nearest-neighbor
308 canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
309 }
310 }
311
312private:
313 typedef skiagm::GM INHERITED;
314};
315DEF_GM( return new ShowMipLevels2(255, 255); )
316DEF_GM( return new ShowMipLevels2(256, 255); )
317DEF_GM( return new ShowMipLevels2(255, 256); )
318DEF_GM( return new ShowMipLevels2(256, 256); )