blob: 4a3a83615cb91e07f0de107d91dde6470b66ffab [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"
Mike Reed5dd202d2018-02-06 23:05:36 +000012#include "SkColorPriv.h"
reeda40be092016-01-14 09:11:51 -080013#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
Mike Reede32500f2017-07-19 17:20:37 -0400117 SkISize onISize() override { return { 150, 862 }; }
reeda40be092016-01-14 09:11:51 -0800118
119 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) {
120 SkBitmap bm;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400121 sk_tool_utils::copy_to(&bm, orig.colorType(), orig);
reeda40be092016-01-14 09:11:51 -0800122 apply_gamma(bm);
123
124 canvas->drawBitmap(bm, x, y, nullptr);
125 SkPaint paint;
126 paint.setStyle(SkPaint::kStroke_Style);
127 paint.setColor(0xFFFFCCCC);
128 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
129 }
130
131 template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) {
132 SkScalar x = 4;
133 SkScalar y = 4;
134
135 SkPixmap prevPM;
reeda40be092016-01-14 09:11:51 -0800136 baseBM.peekPixels(&prevPM);
137
Brian Osman2b23c4b2018-06-01 12:25:08 -0400138 sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
reeda40be092016-01-14 09:11:51 -0800139
140 int index = 0;
141 SkMipMap::Level level;
142 SkScalar scale = 0.5f;
fmalita33ed3ad2016-02-09 08:20:18 -0800143 while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed67b09bf2016-01-16 18:50:35 -0800144 SkBitmap bm = func(prevPM, level.fPixmap);
reeda40be092016-01-14 09:11:51 -0800145 DrawAndFrame(canvas, bm, x, y);
146
reed67b09bf2016-01-16 18:50:35 -0800147 if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
reeda40be092016-01-14 09:11:51 -0800148 break;
149 }
150 if (index & 1) {
reed67b09bf2016-01-16 18:50:35 -0800151 x += level.fPixmap.width() + 4;
reeda40be092016-01-14 09:11:51 -0800152 } else {
reed67b09bf2016-01-16 18:50:35 -0800153 y += level.fPixmap.height() + 4;
reeda40be092016-01-14 09:11:51 -0800154 }
155 scale /= 2;
reed67b09bf2016-01-16 18:50:35 -0800156 prevPM = level.fPixmap;
reeda40be092016-01-14 09:11:51 -0800157 index += 1;
158 }
159 }
160
161 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
162 SkAutoCanvasRestore acr(canvas, true);
163
164 drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) {
165 SkBitmap bm;
166 bm.installPixels(curr);
167 return bm;
168 });
reeda40be092016-01-14 09:11:51 -0800169 }
170
robertphillips55713af2016-08-26 10:04:26 -0700171 void onOnceBeforeDraw() override {
172 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fN, fN, SK_ColorBLACK, SK_ColorWHITE, 2);
173 fBM[1] = make_bitmap(fN, fN);
174 fBM[2] = make_bitmap2(fN, fN);
175 fBM[3] = make_bitmap3(fN, fN);
176 }
177
reeda40be092016-01-14 09:11:51 -0800178 void onDraw(SkCanvas* canvas) override {
179 canvas->translate(4, 4);
180 for (const auto& bm : fBM) {
181 this->drawSet(canvas, bm);
Greg Daniel7785dd22017-06-07 10:01:48 -0400182 // round so we always produce an integral translate, so the GOLD tool won't show
183 // unimportant diffs if this is drawn on a GPU with different rounding rules
184 // since we draw the bitmaps using nearest-neighbor
185 canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
reeda40be092016-01-14 09:11:51 -0800186 }
187 }
halcanary9d524f22016-03-29 09:03:52 -0700188
reeda40be092016-01-14 09:11:51 -0800189private:
190 typedef skiagm::GM INHERITED;
191};
192DEF_GM( return new ShowMipLevels(255); )
193DEF_GM( return new ShowMipLevels(256); )
194
reed01dc44a2016-01-15 10:51:08 -0800195///////////////////////////////////////////////////////////////////////////////////////////////////
196
Matt Sarettcb6266b2017-01-17 10:48:53 -0500197void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) {
198 if (kGray_8_SkColorType == dstColorType) {
Matt Sarett4897fb82017-01-18 11:49:33 -0500199 return sk_tool_utils::copy_to_g8(dst, src);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500200 }
201
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400202 const SkBitmap* srcPtr = &src;
203 SkBitmap tmp(src);
204 if (kRGB_565_SkColorType == dstColorType) {
205 tmp.setAlphaType(kOpaque_SkAlphaType);
206 srcPtr = &tmp;
207 }
208
209 sk_tool_utils::copy_to(dst, dstColorType, *srcPtr);
Matt Sarettcb6266b2017-01-17 10:48:53 -0500210}
211
reed01dc44a2016-01-15 10:51:08 -0800212/**
213 * Show mip levels that were built, for all supported colortypes
214 */
215class ShowMipLevels2 : public skiagm::GM {
216 const int fW, fH;
217 SkBitmap fBM[4];
218
219public:
robertphillips55713af2016-08-26 10:04:26 -0700220 ShowMipLevels2(int w, int h) : fW(w), fH(h) { }
reed01dc44a2016-01-15 10:51:08 -0800221
222protected:
223
224 SkString onShortName() override {
225 SkString str;
226 str.printf("showmiplevels2_%dx%d", fW, fH);
227 return str;
228 }
229
230 SkISize onISize() override {
231 return { 824, 862 };
232 }
233
234 static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) {
235 canvas->drawBitmap(bm, x, y, nullptr);
236 SkPaint paint;
237 paint.setStyle(SkPaint::kStroke_Style);
238 paint.setColor(0xFFFFCCCC);
239 canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint);
240 }
241
242 void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) {
243 SkScalar x = 4;
244 SkScalar y = 4;
245
Brian Osman2b23c4b2018-06-01 12:25:08 -0400246 sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr));
reed01dc44a2016-01-15 10:51:08 -0800247
248 int index = 0;
249 SkMipMap::Level level;
250 SkScalar scale = 0.5f;
fmalita33ed3ad2016-02-09 08:20:18 -0800251 while (mm->extractLevel(SkSize::Make(scale, scale), &level)) {
reed01dc44a2016-01-15 10:51:08 -0800252 SkBitmap bm;
reed67b09bf2016-01-16 18:50:35 -0800253 bm.installPixels(level.fPixmap);
reed01dc44a2016-01-15 10:51:08 -0800254 DrawAndFrame(canvas, bm, x, y);
255
reed67b09bf2016-01-16 18:50:35 -0800256 if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) {
reed01dc44a2016-01-15 10:51:08 -0800257 break;
258 }
259 if (index & 1) {
reed67b09bf2016-01-16 18:50:35 -0800260 x += level.fPixmap.width() + 4;
reed01dc44a2016-01-15 10:51:08 -0800261 } else {
reed67b09bf2016-01-16 18:50:35 -0800262 y += level.fPixmap.height() + 4;
reed01dc44a2016-01-15 10:51:08 -0800263 }
264 scale /= 2;
265 index += 1;
266 }
267 }
268
269 void drawSet(SkCanvas* canvas, const SkBitmap& orig) {
270 const SkColorType ctypes[] = {
271 kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType
272 };
273
274 SkAutoCanvasRestore acr(canvas, true);
275
276 for (auto ctype : ctypes) {
277 SkBitmap bm;
Matt Sarettcb6266b2017-01-17 10:48:53 -0500278 copy_to(&bm, ctype, orig);
reed01dc44a2016-01-15 10:51:08 -0800279 drawLevels(canvas, bm);
280 canvas->translate(orig.width()/2 + 8.0f, 0);
281 }
282 }
283
robertphillips55713af2016-08-26 10:04:26 -0700284 void onOnceBeforeDraw() override {
285 fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fW, fH,
286 SHOW_MIP_COLOR, SK_ColorWHITE, 2);
287 fBM[1] = make_bitmap(fW, fH);
288 fBM[2] = make_bitmap2(fW, fH);
289 fBM[3] = make_bitmap3(fW, fH);
290 }
291
reed01dc44a2016-01-15 10:51:08 -0800292 void onDraw(SkCanvas* canvas) override {
293 canvas->translate(4, 4);
294 for (const auto& bm : fBM) {
295 this->drawSet(canvas, bm);
296 // round so we always produce an integral translate, so the GOLD tool won't show
297 // unimportant diffs if this is drawn on a GPU with different rounding rules
298 // since we draw the bitmaps using nearest-neighbor
299 canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f));
300 }
301 }
halcanary9d524f22016-03-29 09:03:52 -0700302
reed01dc44a2016-01-15 10:51:08 -0800303private:
304 typedef skiagm::GM INHERITED;
305};
306DEF_GM( return new ShowMipLevels2(255, 255); )
307DEF_GM( return new ShowMipLevels2(256, 255); )
308DEF_GM( return new ShowMipLevels2(255, 256); )
309DEF_GM( return new ShowMipLevels2(256, 256); )