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" |
| 9 | |
| 10 | #include "Resources.h" |
| 11 | #include "SkBitmapScaler.h" |
| 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" |
| 18 | #include "sk_tool_utils.h" |
| 19 | |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 20 | #define SHOW_MIP_COLOR 0xFF000000 |
| 21 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 22 | static SkBitmap make_bitmap(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 23 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 24 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 25 | SkCanvas canvas(bm); |
| 26 | canvas.clear(0xFFFFFFFF); |
| 27 | SkPaint paint; |
| 28 | paint.setStyle(SkPaint::kStroke_Style); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 29 | paint.setStrokeWidth(w / 16.0f); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 30 | paint.setColor(SHOW_MIP_COLOR); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 31 | canvas.drawCircle(w/2.0f, h/2.0f, w/3.0f, paint); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 32 | return bm; |
| 33 | } |
| 34 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 35 | static SkBitmap make_bitmap2(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 36 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 37 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 38 | SkCanvas canvas(bm); |
| 39 | canvas.clear(0xFFFFFFFF); |
| 40 | SkPaint paint; |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 41 | paint.setColor(SHOW_MIP_COLOR); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 42 | paint.setStyle(SkPaint::kStroke_Style); |
| 43 | |
| 44 | SkScalar inset = 2; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 45 | SkRect r = SkRect::MakeIWH(w, h).makeInset(0.5f, 0.5f); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 46 | 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" |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 55 | static SkBitmap make_bitmap3(int w, int h) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 56 | SkBitmap bm; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 57 | bm.allocN32Pixels(w, h); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 58 | SkCanvas canvas(bm); |
| 59 | canvas.clear(0xFFFFFFFF); |
| 60 | SkPaint paint; |
| 61 | paint.setStyle(SkPaint::kStroke_Style); |
| 62 | paint.setStrokeWidth(2.1f); |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 63 | paint.setColor(SHOW_MIP_COLOR); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 64 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 65 | SkScalar s = SkIntToScalar(w); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 66 | Sk4f p(s, -s, -s, s); |
| 67 | Sk4f d(5); |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 68 | while (p[1] < s) { |
| 69 | canvas.drawLine(p[0],p[1], p[2], p[3], paint); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 70 | p = p + d; |
| 71 | } |
| 72 | return bm; |
| 73 | } |
| 74 | |
| 75 | class ShowMipLevels : public skiagm::GM { |
| 76 | const int fN; |
| 77 | SkBitmap fBM[4]; |
| 78 | |
| 79 | public: |
| 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 |
| 96 | bm.lockPixels(); |
| 97 | for (int y = 0; y < bm.height(); ++y) { |
| 98 | for (int x = 0; x < bm.width(); ++x) { |
| 99 | SkPMColor c = *bm.getAddr32(x, y); |
| 100 | unsigned r = gamma(SkGetPackedR32(c)); |
| 101 | unsigned g = gamma(SkGetPackedG32(c)); |
| 102 | unsigned b = gamma(SkGetPackedB32(c)); |
| 103 | *bm.getAddr32(x, y) = SkPackARGB32(0xFF, r, g, b); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 108 | ShowMipLevels(int N) : fN(N) { } |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 109 | |
| 110 | protected: |
| 111 | |
| 112 | SkString onShortName() override { |
| 113 | SkString str; |
| 114 | str.printf("showmiplevels_%d", fN); |
| 115 | return str; |
| 116 | } |
| 117 | |
| 118 | SkISize onISize() override { |
| 119 | return { 824, 862 }; |
| 120 | } |
| 121 | |
| 122 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) { |
| 123 | SkBitmap bm; |
| 124 | orig.copyTo(&bm); |
| 125 | apply_gamma(bm); |
| 126 | |
| 127 | canvas->drawBitmap(bm, x, y, nullptr); |
| 128 | SkPaint paint; |
| 129 | paint.setStyle(SkPaint::kStroke_Style); |
| 130 | paint.setColor(0xFFFFCCCC); |
| 131 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 132 | } |
| 133 | |
| 134 | template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) { |
| 135 | SkScalar x = 4; |
| 136 | SkScalar y = 4; |
| 137 | |
| 138 | SkPixmap prevPM; |
| 139 | baseBM.lockPixels(); |
| 140 | baseBM.peekPixels(&prevPM); |
| 141 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 142 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 143 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 144 | |
| 145 | int index = 0; |
| 146 | SkMipMap::Level level; |
| 147 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 148 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 149 | SkBitmap bm = func(prevPM, level.fPixmap); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 150 | DrawAndFrame(canvas, bm, x, y); |
| 151 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 152 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 156 | x += level.fPixmap.width() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 157 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 158 | y += level.fPixmap.height() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 159 | } |
| 160 | scale /= 2; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 161 | prevPM = level.fPixmap; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 162 | index += 1; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 167 | SkAutoCanvasRestore acr(canvas, true); |
| 168 | |
| 169 | drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) { |
| 170 | SkBitmap bm; |
| 171 | bm.installPixels(curr); |
| 172 | return bm; |
| 173 | }); |
| 174 | |
| 175 | const SkBitmapScaler::ResizeMethod methods[] = { |
| 176 | SkBitmapScaler::RESIZE_BOX, |
| 177 | SkBitmapScaler::RESIZE_TRIANGLE, |
| 178 | SkBitmapScaler::RESIZE_LANCZOS3, |
| 179 | SkBitmapScaler::RESIZE_HAMMING, |
| 180 | SkBitmapScaler::RESIZE_MITCHELL, |
| 181 | }; |
| 182 | |
| 183 | SkPixmap basePM; |
| 184 | orig.lockPixels(); |
| 185 | orig.peekPixels(&basePM); |
| 186 | for (auto method : methods) { |
| 187 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 188 | drawLevels(canvas, orig, [basePM, method](const SkPixmap& prev, const SkPixmap& curr) { |
| 189 | SkBitmap bm; |
| 190 | SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.height()); |
| 191 | return bm; |
| 192 | }); |
| 193 | } |
| 194 | } |
| 195 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 196 | void onOnceBeforeDraw() override { |
| 197 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fN, fN, SK_ColorBLACK, SK_ColorWHITE, 2); |
| 198 | fBM[1] = make_bitmap(fN, fN); |
| 199 | fBM[2] = make_bitmap2(fN, fN); |
| 200 | fBM[3] = make_bitmap3(fN, fN); |
| 201 | } |
| 202 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 203 | void onDraw(SkCanvas* canvas) override { |
| 204 | canvas->translate(4, 4); |
| 205 | for (const auto& bm : fBM) { |
| 206 | this->drawSet(canvas, bm); |
| 207 | canvas->translate(0, bm.height() * 0.85f); |
| 208 | } |
| 209 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 210 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 211 | private: |
| 212 | typedef skiagm::GM INHERITED; |
| 213 | }; |
| 214 | DEF_GM( return new ShowMipLevels(255); ) |
| 215 | DEF_GM( return new ShowMipLevels(256); ) |
| 216 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 217 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 218 | |
Matt Sarett | cf5d6ca | 2017-01-13 12:29:08 -0500 | [diff] [blame] | 219 | static void copy_32_to_g8(void* dst, size_t dstRB, const void* src, const SkImageInfo& srcInfo, |
| 220 | size_t srcRB) { |
| 221 | uint8_t* dst8 = (uint8_t*)dst; |
| 222 | const uint32_t* src32 = (const uint32_t*)src; |
| 223 | |
| 224 | const int w = srcInfo.width(); |
| 225 | const int h = srcInfo.height(); |
| 226 | const bool isBGRA = (kBGRA_8888_SkColorType == srcInfo.colorType()); |
| 227 | |
| 228 | for (int y = 0; y < h; ++y) { |
| 229 | if (isBGRA) { |
| 230 | // BGRA |
| 231 | for (int x = 0; x < w; ++x) { |
| 232 | uint32_t s = src32[x]; |
| 233 | dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF); |
| 234 | } |
| 235 | } else { |
| 236 | // RGBA |
| 237 | for (int x = 0; x < w; ++x) { |
| 238 | uint32_t s = src32[x]; |
| 239 | dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF); |
| 240 | } |
| 241 | } |
| 242 | src32 = (const uint32_t*)((const char*)src32 + srcRB); |
| 243 | dst8 += dstRB; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 248 | if (kGray_8_SkColorType == dstColorType) { |
| 249 | SkImageInfo grayInfo = src.info().makeColorType(kGray_8_SkColorType); |
| 250 | dst->allocPixels(grayInfo); |
| 251 | copy_32_to_g8(dst->getPixels(), dst->rowBytes(), src.getPixels(), src.info(), |
| 252 | src.rowBytes()); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | src.copyTo(dst, dstColorType); |
| 257 | } |
| 258 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Show mip levels that were built, for all supported colortypes |
| 261 | */ |
| 262 | class ShowMipLevels2 : public skiagm::GM { |
| 263 | const int fW, fH; |
| 264 | SkBitmap fBM[4]; |
| 265 | |
| 266 | public: |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 267 | ShowMipLevels2(int w, int h) : fW(w), fH(h) { } |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 268 | |
| 269 | protected: |
| 270 | |
| 271 | SkString onShortName() override { |
| 272 | SkString str; |
| 273 | str.printf("showmiplevels2_%dx%d", fW, fH); |
| 274 | return str; |
| 275 | } |
| 276 | |
| 277 | SkISize onISize() override { |
| 278 | return { 824, 862 }; |
| 279 | } |
| 280 | |
| 281 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) { |
| 282 | canvas->drawBitmap(bm, x, y, nullptr); |
| 283 | SkPaint paint; |
| 284 | paint.setStyle(SkPaint::kStroke_Style); |
| 285 | paint.setColor(0xFFFFCCCC); |
| 286 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 287 | } |
| 288 | |
| 289 | void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) { |
| 290 | SkScalar x = 4; |
| 291 | SkScalar y = 4; |
| 292 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 293 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 294 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 295 | |
| 296 | int index = 0; |
| 297 | SkMipMap::Level level; |
| 298 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 299 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 300 | SkBitmap bm; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 301 | bm.installPixels(level.fPixmap); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 302 | DrawAndFrame(canvas, bm, x, y); |
| 303 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 304 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 308 | x += level.fPixmap.width() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 309 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 310 | y += level.fPixmap.height() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 311 | } |
| 312 | scale /= 2; |
| 313 | index += 1; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 318 | const SkColorType ctypes[] = { |
| 319 | kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType |
| 320 | }; |
| 321 | |
| 322 | SkAutoCanvasRestore acr(canvas, true); |
| 323 | |
| 324 | for (auto ctype : ctypes) { |
| 325 | SkBitmap bm; |
Matt Sarett | cf5d6ca | 2017-01-13 12:29:08 -0500 | [diff] [blame] | 326 | copy_to(&bm, ctype, orig); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 327 | drawLevels(canvas, bm); |
| 328 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 329 | } |
| 330 | } |
| 331 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 332 | void onOnceBeforeDraw() override { |
| 333 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fW, fH, |
| 334 | SHOW_MIP_COLOR, SK_ColorWHITE, 2); |
| 335 | fBM[1] = make_bitmap(fW, fH); |
| 336 | fBM[2] = make_bitmap2(fW, fH); |
| 337 | fBM[3] = make_bitmap3(fW, fH); |
| 338 | } |
| 339 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 340 | void onDraw(SkCanvas* canvas) override { |
| 341 | canvas->translate(4, 4); |
| 342 | for (const auto& bm : fBM) { |
| 343 | this->drawSet(canvas, bm); |
| 344 | // round so we always produce an integral translate, so the GOLD tool won't show |
| 345 | // unimportant diffs if this is drawn on a GPU with different rounding rules |
| 346 | // since we draw the bitmaps using nearest-neighbor |
| 347 | canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f)); |
| 348 | } |
| 349 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 350 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 351 | private: |
| 352 | typedef skiagm::GM INHERITED; |
| 353 | }; |
| 354 | DEF_GM( return new ShowMipLevels2(255, 255); ) |
| 355 | DEF_GM( return new ShowMipLevels2(256, 255); ) |
| 356 | DEF_GM( return new ShowMipLevels2(255, 256); ) |
| 357 | DEF_GM( return new ShowMipLevels2(256, 256); ) |