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" |
Mike Reed | e602f39 | 2018-02-06 10:17:08 -0500 | [diff] [blame^] | 12 | #include "SkColorPriv.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
| 14 | #include "SkTypeface.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkPaint.h" |
| 17 | #include "SkMipMap.h" |
| 18 | #include "Resources.h" |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 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 |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 96 | 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 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 107 | ShowMipLevels(int N) : fN(N) { } |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 108 | |
| 109 | protected: |
| 110 | |
| 111 | SkString onShortName() override { |
| 112 | SkString str; |
| 113 | str.printf("showmiplevels_%d", fN); |
| 114 | return str; |
| 115 | } |
| 116 | |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 117 | SkISize onISize() override { return { 150, 862 }; } |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 118 | |
| 119 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) { |
| 120 | SkBitmap bm; |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 121 | sk_tool_utils::copy_to(&bm, orig.colorType(), orig); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 122 | 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; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 136 | baseBM.peekPixels(&prevPM); |
| 137 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 138 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 139 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 140 | |
| 141 | int index = 0; |
| 142 | SkMipMap::Level level; |
| 143 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 144 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 145 | SkBitmap bm = func(prevPM, level.fPixmap); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 146 | DrawAndFrame(canvas, bm, x, y); |
| 147 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 148 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 152 | x += level.fPixmap.width() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 153 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 154 | y += level.fPixmap.height() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 155 | } |
| 156 | scale /= 2; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 157 | prevPM = level.fPixmap; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 158 | index += 1; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 163 | SkAutoCanvasRestore acr(canvas, true); |
| 164 | |
| 165 | drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) { |
| 166 | SkBitmap bm; |
| 167 | bm.installPixels(curr); |
| 168 | return bm; |
| 169 | }); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 170 | } |
| 171 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 172 | void onOnceBeforeDraw() override { |
| 173 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fN, fN, SK_ColorBLACK, SK_ColorWHITE, 2); |
| 174 | fBM[1] = make_bitmap(fN, fN); |
| 175 | fBM[2] = make_bitmap2(fN, fN); |
| 176 | fBM[3] = make_bitmap3(fN, fN); |
| 177 | } |
| 178 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 179 | void onDraw(SkCanvas* canvas) override { |
| 180 | canvas->translate(4, 4); |
| 181 | for (const auto& bm : fBM) { |
| 182 | this->drawSet(canvas, bm); |
Greg Daniel | 7785dd2 | 2017-06-07 10:01:48 -0400 | [diff] [blame] | 183 | // round so we always produce an integral translate, so the GOLD tool won't show |
| 184 | // unimportant diffs if this is drawn on a GPU with different rounding rules |
| 185 | // since we draw the bitmaps using nearest-neighbor |
| 186 | canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f)); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 189 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 190 | private: |
| 191 | typedef skiagm::GM INHERITED; |
| 192 | }; |
| 193 | DEF_GM( return new ShowMipLevels(255); ) |
| 194 | DEF_GM( return new ShowMipLevels(256); ) |
| 195 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 196 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 197 | |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 198 | void copy_to(SkBitmap* dst, SkColorType dstColorType, const SkBitmap& src) { |
| 199 | if (kGray_8_SkColorType == dstColorType) { |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 200 | return sk_tool_utils::copy_to_g8(dst, src); |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 203 | const SkBitmap* srcPtr = &src; |
| 204 | SkBitmap tmp(src); |
| 205 | if (kRGB_565_SkColorType == dstColorType) { |
| 206 | tmp.setAlphaType(kOpaque_SkAlphaType); |
| 207 | srcPtr = &tmp; |
| 208 | } |
| 209 | |
| 210 | sk_tool_utils::copy_to(dst, dstColorType, *srcPtr); |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 211 | } |
| 212 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Show mip levels that were built, for all supported colortypes |
| 215 | */ |
| 216 | class ShowMipLevels2 : public skiagm::GM { |
| 217 | const int fW, fH; |
| 218 | SkBitmap fBM[4]; |
| 219 | |
| 220 | public: |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 221 | ShowMipLevels2(int w, int h) : fW(w), fH(h) { } |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 222 | |
| 223 | protected: |
| 224 | |
| 225 | SkString onShortName() override { |
| 226 | SkString str; |
| 227 | str.printf("showmiplevels2_%dx%d", fW, fH); |
| 228 | return str; |
| 229 | } |
| 230 | |
| 231 | SkISize onISize() override { |
| 232 | return { 824, 862 }; |
| 233 | } |
| 234 | |
| 235 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) { |
| 236 | canvas->drawBitmap(bm, x, y, nullptr); |
| 237 | SkPaint paint; |
| 238 | paint.setStyle(SkPaint::kStroke_Style); |
| 239 | paint.setColor(0xFFFFCCCC); |
| 240 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 241 | } |
| 242 | |
| 243 | void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) { |
| 244 | SkScalar x = 4; |
| 245 | SkScalar y = 4; |
| 246 | |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame] | 247 | SkDestinationSurfaceColorMode colorMode = SkDestinationSurfaceColorMode::kLegacy; |
| 248 | sk_sp<SkMipMap> mm(SkMipMap::Build(baseBM, colorMode, nullptr)); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 249 | |
| 250 | int index = 0; |
| 251 | SkMipMap::Level level; |
| 252 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 253 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 254 | SkBitmap bm; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 255 | bm.installPixels(level.fPixmap); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 256 | DrawAndFrame(canvas, bm, x, y); |
| 257 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 258 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 262 | x += level.fPixmap.width() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 263 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 264 | y += level.fPixmap.height() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 265 | } |
| 266 | scale /= 2; |
| 267 | index += 1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 272 | const SkColorType ctypes[] = { |
| 273 | kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType |
| 274 | }; |
| 275 | |
| 276 | SkAutoCanvasRestore acr(canvas, true); |
| 277 | |
| 278 | for (auto ctype : ctypes) { |
| 279 | SkBitmap bm; |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 280 | copy_to(&bm, ctype, orig); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 281 | drawLevels(canvas, bm); |
| 282 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 283 | } |
| 284 | } |
| 285 | |
robertphillips | 55713af | 2016-08-26 10:04:26 -0700 | [diff] [blame] | 286 | void onOnceBeforeDraw() override { |
| 287 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(fW, fH, |
| 288 | SHOW_MIP_COLOR, SK_ColorWHITE, 2); |
| 289 | fBM[1] = make_bitmap(fW, fH); |
| 290 | fBM[2] = make_bitmap2(fW, fH); |
| 291 | fBM[3] = make_bitmap3(fW, fH); |
| 292 | } |
| 293 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 294 | void onDraw(SkCanvas* canvas) override { |
| 295 | canvas->translate(4, 4); |
| 296 | for (const auto& bm : fBM) { |
| 297 | this->drawSet(canvas, bm); |
| 298 | // round so we always produce an integral translate, so the GOLD tool won't show |
| 299 | // unimportant diffs if this is drawn on a GPU with different rounding rules |
| 300 | // since we draw the bitmaps using nearest-neighbor |
| 301 | canvas->translate(0, SkScalarRoundToScalar(bm.height() * 0.85f)); |
| 302 | } |
| 303 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 304 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 305 | private: |
| 306 | typedef skiagm::GM INHERITED; |
| 307 | }; |
| 308 | DEF_GM( return new ShowMipLevels2(255, 255); ) |
| 309 | DEF_GM( return new ShowMipLevels2(256, 255); ) |
| 310 | DEF_GM( return new ShowMipLevels2(255, 256); ) |
| 311 | DEF_GM( return new ShowMipLevels2(256, 256); ) |