blob: 3e2091fd6b159077fc9e86e4c67c25fca72c7f44 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reeda40be092016-01-14 09:11:51 -080010
11#include "Resources.h"
12#include "SkBitmapScaler.h"
13#include "SkGradientShader.h"
14#include "SkTypeface.h"
reeda40be092016-01-14 09:11:51 -080015#include "SkStream.h"
16#include "SkPaint.h"
17#include "SkMipMap.h"
18#include "Resources.h"
reeda40be092016-01-14 09:11:51 -080019
reed32e0b4a2016-01-15 13:17:08 -080020#define SHOW_MIP_COLOR 0xFF000000
21
reed01dc44a2016-01-15 10:51:08 -080022static SkBitmap make_bitmap(int w, int h) {
reeda40be092016-01-14 09:11:51 -080023 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080024 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080025 SkCanvas canvas(bm);
26 canvas.clear(0xFFFFFFFF);
27 SkPaint paint;
28 paint.setStyle(SkPaint::kStroke_Style);
reed01dc44a2016-01-15 10:51:08 -080029 paint.setStrokeWidth(w / 16.0f);
reed32e0b4a2016-01-15 13:17:08 -080030 paint.setColor(SHOW_MIP_COLOR);
reed01dc44a2016-01-15 10:51:08 -080031 canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint);
reeda40be092016-01-14 09:11:51 -080032 return bm;
33}
34
reed01dc44a2016-01-15 10:51:08 -080035static SkBitmap make_bitmap2(int w, int h) {
reeda40be092016-01-14 09:11:51 -080036 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080037 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080038 SkCanvas canvas(bm);
39 canvas.clear(0xFFFFFFFF);
40 SkPaint paint;
reed32e0b4a2016-01-15 13:17:08 -080041 paint.setColor(SHOW_MIP_COLOR);
reeda40be092016-01-14 09:11:51 -080042 paint.setStyle(SkPaint::kStroke_Style);
43
44 SkScalar inset = 2;
reed01dc44a2016-01-15 10:51:08 -080045 SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f);
reeda40be092016-01-14 09:11:51 -080046 while (r.width() > 4) {
47 canvas.drawRect(r, paint);
48 r.inset(inset, inset);
49 inset += 1;
50 }
51 return bm;
52}
53
54#include "SkNx.h"
reed01dc44a2016-01-15 10:51:08 -080055static SkBitmap make_bitmap3(int w, int h) {
reeda40be092016-01-14 09:11:51 -080056 SkBitmap bm;
reed01dc44a2016-01-15 10:51:08 -080057 bm.allocN32Pixels(w, h);
reeda40be092016-01-14 09:11:51 -080058 SkCanvas canvas(bm);
59 canvas.clear(0xFFFFFFFF);
60 SkPaint paint;
61 paint.setStyle(SkPaint::kStroke_Style);
62 paint.setStrokeWidth(2.1f);
reed32e0b4a2016-01-15 13:17:08 -080063 paint.setColor(SHOW_MIP_COLOR);
reeda40be092016-01-14 09:11:51 -080064
reed01dc44a2016-01-15 10:51:08 -080065 SkScalar s = SkIntToScalar(w);
reeda40be092016-01-14 09:11:51 -080066 Sk4f p(s, -s, -s, s);
67 Sk4f d(5);
mtklein7c249e52016-02-21 10:54:19 -080068 while (p[1] < s) {
69 canvas.drawLine(p[0],p[1], p[2], p[3], paint);
reeda40be092016-01-14 09:11:51 -080070 p = p + d;
71 }
72 return bm;
73}
74
75class ShowMipLevels : public skiagm::GM {
76 const int fN;
77 SkBitmap fBM[4];
78
79public:
80 static unsigned gamma(unsigned n) {
81 float x = n / 255.0f;
82#if 0
83 x = sqrtf(x);
84#else
85 if (x > 0.0031308f) {
86 x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f;
87 } else {
88 x = 12.92f * x;
89 }
90#endif
91 return (int)(x * 255);
92 }
93
94 static void apply_gamma(const SkBitmap& bm) {
95 return; // below is our experiment for sRGB correction
reeda40be092016-01-14 09:11:51 -080096 for (int y = 0; y < bm.height(); ++y) {
97 for (int x = 0; x < bm.width(); ++x) {
98 SkPMColor c = *bm.getAddr32(x, y);
99 unsigned r = gamma(SkGetPackedR32(c));
100 unsigned g = gamma(SkGetPackedG32(c));
101 unsigned b = gamma(SkGetPackedB32(c));
102 *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b);
103 }
104 }
105 }
106
robertphillips55713af2016-08-26 10:04:26 -0700107 ShowMipLevels(int N) : fN(N) { }
reeda40be092016-01-14 09:11:51 -0800108
109protected:
110
111 SkString onShortName() override {
112 SkString str;
113 str.printf("showmiplevels_%d", fN);
114 return str;
115 }
116
117 SkISize onISize() override {
118 return { 824, 862 };
119 }
120
121 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
122 SkBitmap bm;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400123 sk_tool_utils::copy_to(&bm, orig.colorType(), orig);
reeda40be092016-01-14 09:11:51 -0800124 apply_gamma(bm);
125
126 canvas->drawBitmap(bm, x, y, nullptr);
127 SkPaint paint;
128 paint.setStyle(SkPaint::kStroke_Style);
129 paint.setColor(0xFFFFCCCC);
130 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
131 }
132
133 template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) {
134 SkScalar x = 4;
135 SkScalar y = 4;
136
137 SkPixmap prevPM;
reeda40be092016-01-14 09:11:51 -0800138 baseBM.peekPixels(&prevPM);
139
Brian Osman7b8400d2016-11-08 17:08:54 -0500140 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
141 sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr));
reeda40be092016-01-14 09:11:51 -0800142
143 int index = 0;
144 SkMipMap::Level level;
145 SkScalar scale = 0.5f;
fmalita33ed3ad2016-02-09 08:20:18 -0800146 while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed67b09bf2016-01-16 18:50:35 -0800147 SkBitmap bm = func(prevPM, level.fPixmap);
reeda40be092016-01-14 09:11:51 -0800148 DrawAndFrame(canvas, bm, x, y);
149
reed67b09bf2016-01-16 18:50:35 -0800150 if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
reeda40be092016-01-14 09:11:51 -0800151 break;
152 }
153 if (index & 1) {
reed67b09bf2016-01-16 18:50:35 -0800154 x += level.fPixmap.width() + 4;
reeda40be092016-01-14 09:11:51 -0800155 } else {
reed67b09bf2016-01-16 18:50:35 -0800156 y += level.fPixmap.height() + 4;
reeda40be092016-01-14 09:11:51 -0800157 }
158 scale /= 2;
reed67b09bf2016-01-16 18:50:35 -0800159 prevPM = level.fPixmap;
reeda40be092016-01-14 09:11:51 -0800160 index += 1;
161 }
162 }
163
164 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
165 SkAutoCanvasRestore acr(canvas, true);
166
167 drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
168 SkBitmap bm;
169 bm.installPixels(curr);
170 return bm;
171 });
172
173 const SkBitmapScaler::ResizeMethod methods[] = {
174 SkBitmapScaler::RESIZE_BOX,
175 SkBitmapScaler::RESIZE_TRIANGLE,
176 SkBitmapScaler::RESIZE_LANCZOS3,
177 SkBitmapScaler::RESIZE_HAMMING,
178 SkBitmapScaler::RESIZE_MITCHELL,
179 };
180
181 SkPixmap basePM;
reeda40be092016-01-14 09:11:51 -0800182 orig.peekPixels(&basePM);
183 for (auto method : methods) {
184 canvas->translate(orig.width()/2 + 8.0f, 0);
Brian Salomon65a17532017-01-25 19:26:59 -0500185 drawLevels(canvas, orig, [method](const SkPixmap& prev, const SkPixmap& curr) {
reeda40be092016-01-14 09:11:51 -0800186 SkBitmap bm;
187 SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.height());
188 return bm;
189 });
190 }
191 }
192
robertphillips55713af2016-08-26 10:04:26 -0700193 void onOnceBeforeDraw() override {
194 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fN, fN, SK_ColorBLACK, SK_ColorWHITE, 2);
195 fBM[1] = make_bitmap(fN, fN);
196 fBM[2] = make_bitmap2(fN, fN);
197 fBM[3] = make_bitmap3(fN, fN);
198 }
199
reeda40be092016-01-14 09:11:51 -0800200 void onDraw(SkCanvas* canvas) override {
201 canvas->translate(4, 4);
202 for (const auto& bm : fBM) {
203 this->drawSet(canvas, bm);
204 canvas->translate(0, bm.height() * 0.85f);
205 }
206 }
halcanary9d524f22016-03-29 09:03:52 -0700207
reeda40be092016-01-14 09:11:51 -0800208private:
209 typedef skiagm::GM INHERITED;
210};
211DEF_GM( return new ShowMipLevels(255); )
212DEF_GM( return new ShowMipLevels(256); )
213
reed01dc44a2016-01-15 10:51:08 -0800214///////////////////////////////////////////////////////////////////////////////////////////////////
215
Matt Sarettcb6266b2017-01-17 10:48:53 -0500216void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
217 if (kGray_8_SkColorType == dstColorType) {
Matt Sarett4897fb82017-01-18 11:49:33 -0500218 return sk_tool_utils::copy_to_g8(dst, src);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500219 }
220
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400221 const SkBitmap* srcPtr = &src;
222 SkBitmap tmp(src);
223 if (kRGB_565_SkColorType == dstColorType) {
224 tmp.setAlphaType(kOpaque_SkAlphaType);
225 srcPtr = &tmp;
226 }
227
228 sk_tool_utils::copy_to(dst, dstColorType, *srcPtr);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500229}
230
reed01dc44a2016-01-15 10:51:08 -0800231/**
232 * Show mip levels that were built, for all supported colortypes
233 */
234class ShowMipLevels2 : public skiagm::GM {
235 const int fW, fH;
236 SkBitmap fBM[4];
237
238public:
robertphillips55713af2016-08-26 10:04:26 -0700239 ShowMipLevels2(int w, int h) : fW(w), fH(h) { }
reed01dc44a2016-01-15 10:51:08 -0800240
241protected:
242
243 SkString onShortName() override {
244 SkString str;
245 str.printf("showmiplevels2_%dx%d", fW, fH);
246 return str;
247 }
248
249 SkISize onISize() override {
250 return { 824, 862 };
251 }
252
253 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) {
254 canvas->drawBitmap(bm, x, y, nullptr);
255 SkPaint paint;
256 paint.setStyle(SkPaint::kStroke_Style);
257 paint.setColor(0xFFFFCCCC);
258 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
259 }
260
261 void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) {
262 SkScalar x = 4;
263 SkScalar y = 4;
264
Brian Osman7b8400d2016-11-08 17:08:54 -0500265 SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy;
266 sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr));
reed01dc44a2016-01-15 10:51:08 -0800267
268 int index = 0;
269 SkMipMap::Level level;
270 SkScalar scale = 0.5f;
fmalita33ed3ad2016-02-09 08:20:18 -0800271 while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed01dc44a2016-01-15 10:51:08 -0800272 SkBitmap bm;
reed67b09bf2016-01-16 18:50:35 -0800273 bm.installPixels(level.fPixmap);
reed01dc44a2016-01-15 10:51:08 -0800274 DrawAndFrame(canvas, bm, x, y);
275
reed67b09bf2016-01-16 18:50:35 -0800276 if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
reed01dc44a2016-01-15 10:51:08 -0800277 break;
278 }
279 if (index & 1) {
reed67b09bf2016-01-16 18:50:35 -0800280 x += level.fPixmap.width() + 4;
reed01dc44a2016-01-15 10:51:08 -0800281 } else {
reed67b09bf2016-01-16 18:50:35 -0800282 y += level.fPixmap.height() + 4;
reed01dc44a2016-01-15 10:51:08 -0800283 }
284 scale /= 2;
285 index += 1;
286 }
287 }
288
289 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
290 const SkColorType ctypes[] = {
291 kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType
292 };
293
294 SkAutoCanvasRestore acr(canvas, true);
295
296 for (auto ctype : ctypes) {
297 SkBitmap bm;
Matt Sarettcb6266b2017-01-17 10:48:53 -0500298 copy_to(&bm, ctype, orig);
reed01dc44a2016-01-15 10:51:08 -0800299 drawLevels(canvas, bm);
300 canvas->translate(orig.width()/2 + 8.0f, 0);
301 }
302 }
303
robertphillips55713af2016-08-26 10:04:26 -0700304 void onOnceBeforeDraw() override {
305 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fW, fH,
306 SHOW_MIP_COLOR, SK_ColorWHITE, 2);
307 fBM[1] = make_bitmap(fW, fH);
308 fBM[2] = make_bitmap2(fW, fH);
309 fBM[3] = make_bitmap3(fW, fH);
310 }
311
reed01dc44a2016-01-15 10:51:08 -0800312 void onDraw(SkCanvas* canvas) override {
313 canvas->translate(4, 4);
314 for (const auto& bm : fBM) {
315 this->drawSet(canvas, bm);
316 // round so we always produce an integral translate, so the GOLD tool won't show
317 // unimportant diffs if this is drawn on a GPU with different rounding rules
318 // since we draw the bitmaps using nearest-neighbor
319 canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
320 }
321 }
halcanary9d524f22016-03-29 09:03:52 -0700322
reed01dc44a2016-01-15 10:51:08 -0800323private:
324 typedef skiagm::GM INHERITED;
325};
326DEF_GM( return new ShowMipLevels2(255, 255); )
327DEF_GM( return new ShowMipLevels2(256, 255); )
328DEF_GM( return new ShowMipLevels2(255, 256); )
329DEF_GM( return new ShowMipLevels2(256, 256); )