reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
| 13 | #include "SkTypeface.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkPaint.h" |
| 16 | #include "SkMipMap.h" |
| 17 | #include "Resources.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 18 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 19 | #define SHOW_MIP_COLOR 0xFF000000 |
| 20 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 21 | static SkBitmap make_bitmap(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 22 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 23 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 24 | SkCanvas canvas(bm); |
| 25 | canvas.clear(0xFFFFFFFF); |
| 26 | SkPaint paint; |
| 27 | paint.setStyle(SkPaint::kStroke_Style); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 28 | paint.setStrokeWidth(w / 16.0f); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 29 | paint.setColor(SHOW_MIP_COLOR); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 30 | canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 31 | return bm; |
| 32 | } |
| 33 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 34 | static SkBitmap make_bitmap2(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 35 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 36 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 37 | SkCanvas canvas(bm); |
| 38 | canvas.clear(0xFFFFFFFF); |
| 39 | SkPaint paint; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 40 | paint.setColor(SHOW_MIP_COLOR); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 41 | paint.setStyle(SkPaint::kStroke_Style); |
| 42 | |
| 43 | SkScalar inset = 2; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 44 | SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 45 | while (r.width() > 4) { |
| 46 | canvas.drawRect(r, paint); |
| 47 | r.inset(inset, inset); |
| 48 | inset += 1; |
| 49 | } |
| 50 | return bm; |
| 51 | } |
| 52 | |
| 53 | #include "SkNx.h" |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 54 | static SkBitmap make_bitmap3(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 55 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 56 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 57 | SkCanvas canvas(bm); |
| 58 | canvas.clear(0xFFFFFFFF); |
| 59 | SkPaint paint; |
| 60 | paint.setStyle(SkPaint::kStroke_Style); |
| 61 | paint.setStrokeWidth(2.1f); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 62 | paint.setColor(SHOW_MIP_COLOR); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 63 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 64 | SkScalar s = SkIntToScalar(w); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 65 | Sk4f p(s, -s, -s, s); |
| 66 | Sk4f d(5); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 67 | while (p[1] < s) { |
| 68 | canvas.drawLine(p[0],p[1], p[2], p[3], paint); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 69 | p = p + d; |
| 70 | } |
| 71 | return bm; |
| 72 | } |
| 73 | |
| 74 | class ShowMipLevels : public skiagm::GM { |
| 75 | const int fN; |
| 76 | SkBitmap fBM[4]; |
| 77 | |
| 78 | public: |
| 79 | static unsigned gamma(unsigned n) { |
| 80 | float x = n / 255.0f; |
| 81 | #if 0 |
| 82 | x = sqrtf(x); |
| 83 | #else |
| 84 | if (x > 0.0031308f) { |
| 85 | x = 1.055f * (powf(x, (1.0f / 2.4f))) - 0.055f; |
| 86 | } else { |
| 87 | x = 12.92f * x; |
| 88 | } |
| 89 | #endif |
| 90 | return (int)(x * 255); |
| 91 | } |
| 92 | |
| 93 | static void apply_gamma(const SkBitmap& bm) { |
| 94 | return; // below is our experiment for sRGB correction |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 95 | for (int y = 0; y < bm.height(); ++y) { |
| 96 | for (int x = 0; x < bm.width(); ++x) { |
| 97 | SkPMColor c = *bm.getAddr32(x, y); |
| 98 | unsigned r = gamma(SkGetPackedR32(c)); |
| 99 | unsigned g = gamma(SkGetPackedG32(c)); |
| 100 | unsigned b = gamma(SkGetPackedB32(c)); |
| 101 | *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 106 | ShowMipLevels(int N) : fN(N) { } |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | |
| 110 | SkString onShortName() override { |
| 111 | SkString str; |
| 112 | str.printf("showmiplevels_%d", fN); |
| 113 | return str; |
| 114 | } |
| 115 | |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 116 | SkISize onISize() override { return { 150, 862 }; } |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 117 | |
| 118 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) { |
| 119 | SkBitmap bm; |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 120 | sk_tool_utils::copy_to(&bm, orig.colorType(), orig); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 121 | apply_gamma(bm); |
| 122 | |
| 123 | canvas->drawBitmap(bm, x, y, nullptr); |
| 124 | SkPaint paint; |
| 125 | paint.setStyle(SkPaint::kStroke_Style); |
| 126 | paint.setColor(0xFFFFCCCC); |
| 127 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 128 | } |
| 129 | |
| 130 | template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) { |
| 131 | SkScalar x = 4; |
| 132 | SkScalar y = 4; |
| 133 | |
| 134 | SkPixmap prevPM; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 135 | baseBM.peekPixels(&prevPM); |
| 136 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 137 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 138 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 139 | |
| 140 | int index = 0; |
| 141 | SkMipMap::Level level; |
| 142 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 143 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 144 | SkBitmap bm = func(prevPM, level.fPixmap); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 145 | DrawAndFrame(canvas, bm, x, y); |
| 146 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 147 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 151 | x += level.fPixmap.width() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 152 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 153 | y += level.fPixmap.height() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 154 | } |
| 155 | scale /= 2; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 156 | prevPM = level.fPixmap; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 157 | 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 | }); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 169 | } |
| 170 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 171 | 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 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 178 | void onDraw(SkCanvas* canvas) override { |
| 179 | canvas->translate(4, 4); |
| 180 | for (const auto& bm : fBM) { |
| 181 | this->drawSet(canvas, bm); |
Greg Daniel | 7785dd2 | 2017-06-07 10:01:48 -0400 | [diff] [blame] | 182 | // 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)); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 188 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 189 | private: |
| 190 | typedef skiagm::GM INHERITED; |
| 191 | }; |
| 192 | DEF_GM( return new ShowMipLevels(255); ) |
| 193 | DEF_GM( return new ShowMipLevels(256); ) |
| 194 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 195 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 196 | |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 197 | void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 198 | if (kGray_8_SkColorType == dstColorType) { |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 199 | return sk_tool_utils::copy_to_g8(dst, src); |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 202 | 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 Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 210 | } |
| 211 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 212 | /** |
| 213 | * Show mip levels that were built, for all supported colortypes |
| 214 | */ |
| 215 | class ShowMipLevels2 : public skiagm::GM { |
| 216 | const int fW, fH; |
| 217 | SkBitmap fBM[4]; |
| 218 | |
| 219 | public: |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 220 | ShowMipLevels2(int w, int h) : fW(w), fH(h) { } |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 221 | |
| 222 | protected: |
| 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 Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 246 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 247 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 248 | |
| 249 | int index = 0; |
| 250 | SkMipMap::Level level; |
| 251 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 252 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 253 | SkBitmap bm; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 254 | bm.installPixels(level.fPixmap); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 255 | DrawAndFrame(canvas, bm, x, y); |
| 256 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 257 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 261 | x += level.fPixmap.width() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 262 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 263 | y += level.fPixmap.height() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 264 | } |
| 265 | scale /= 2; |
| 266 | index += 1; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 271 | const SkColorType ctypes[] = { |
| 272 | kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType |
| 273 | }; |
| 274 | |
| 275 | SkAutoCanvasRestore acr(canvas, true); |
| 276 | |
| 277 | for (auto ctype : ctypes) { |
| 278 | SkBitmap bm; |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 279 | copy_to(&bm, ctype, orig); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 280 | drawLevels(canvas, bm); |
| 281 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 282 | } |
| 283 | } |
| 284 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 285 | void onOnceBeforeDraw() override { |
| 286 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fW, fH, |
| 287 | SHOW_MIP_COLOR, SK_ColorWHITE, 2); |
| 288 | fBM[1] = make_bitmap(fW, fH); |
| 289 | fBM[2] = make_bitmap2(fW, fH); |
| 290 | fBM[3] = make_bitmap3(fW, fH); |
| 291 | } |
| 292 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 293 | void onDraw(SkCanvas* canvas) override { |
| 294 | canvas->translate(4, 4); |
| 295 | for (const auto& bm : fBM) { |
| 296 | this->drawSet(canvas, bm); |
| 297 | // round so we always produce an integral translate, so the GOLD tool won't show |
| 298 | // unimportant diffs if this is drawn on a GPU with different rounding rules |
| 299 | // since we draw the bitmaps using nearest-neighbor |
| 300 | canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f)); |
| 301 | } |
| 302 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 303 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 304 | private: |
| 305 | typedef skiagm::GM INHERITED; |
| 306 | }; |
| 307 | DEF_GM( return new ShowMipLevels2(255, 255); ) |
| 308 | DEF_GM( return new ShowMipLevels2(256, 255); ) |
| 309 | DEF_GM( return new ShowMipLevels2(255, 256); ) |
| 310 | DEF_GM( return new ShowMipLevels2(256, 256); ) |