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 | |
| 108 | ShowMipLevels(int N) : fN(N) { |
| 109 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(N, N, SK_ColorBLACK, SK_ColorWHITE, 2); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 110 | fBM[1] = make_bitmap(N, N); |
| 111 | fBM[2] = make_bitmap2(N, N); |
| 112 | fBM[3] = make_bitmap3(N, N); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | protected: |
| 116 | |
| 117 | SkString onShortName() override { |
| 118 | SkString str; |
| 119 | str.printf("showmiplevels_%d", fN); |
| 120 | return str; |
| 121 | } |
| 122 | |
| 123 | SkISize onISize() override { |
| 124 | return { 824, 862 }; |
| 125 | } |
| 126 | |
| 127 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& orig, SkScalar x, SkScalar y) { |
| 128 | SkBitmap bm; |
| 129 | orig.copyTo(&bm); |
| 130 | apply_gamma(bm); |
| 131 | |
| 132 | canvas->drawBitmap(bm, x, y, nullptr); |
| 133 | SkPaint paint; |
| 134 | paint.setStyle(SkPaint::kStroke_Style); |
| 135 | paint.setColor(0xFFFFCCCC); |
| 136 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 137 | } |
| 138 | |
| 139 | template <typename F> void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM, F func) { |
| 140 | SkScalar x = 4; |
| 141 | SkScalar y = 4; |
| 142 | |
| 143 | SkPixmap prevPM; |
| 144 | baseBM.lockPixels(); |
| 145 | baseBM.peekPixels(&prevPM); |
| 146 | |
| 147 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr)); |
| 148 | |
| 149 | int index = 0; |
| 150 | SkMipMap::Level level; |
| 151 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 152 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 153 | SkBitmap bm = func(prevPM, level.fPixmap); |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 154 | DrawAndFrame(canvas, bm, x, y); |
| 155 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 156 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 160 | x += level.fPixmap.width() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 161 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 162 | y += level.fPixmap.height() + 4; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 163 | } |
| 164 | scale /= 2; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 165 | prevPM = level.fPixmap; |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 166 | index += 1; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 171 | SkAutoCanvasRestore acr(canvas, true); |
| 172 | |
| 173 | drawLevels(canvas, orig, [](const SkPixmap& prev, const SkPixmap& curr) { |
| 174 | SkBitmap bm; |
| 175 | bm.installPixels(curr); |
| 176 | return bm; |
| 177 | }); |
| 178 | |
| 179 | const SkBitmapScaler::ResizeMethod methods[] = { |
| 180 | SkBitmapScaler::RESIZE_BOX, |
| 181 | SkBitmapScaler::RESIZE_TRIANGLE, |
| 182 | SkBitmapScaler::RESIZE_LANCZOS3, |
| 183 | SkBitmapScaler::RESIZE_HAMMING, |
| 184 | SkBitmapScaler::RESIZE_MITCHELL, |
| 185 | }; |
| 186 | |
| 187 | SkPixmap basePM; |
| 188 | orig.lockPixels(); |
| 189 | orig.peekPixels(&basePM); |
| 190 | for (auto method : methods) { |
| 191 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 192 | drawLevels(canvas, orig, [basePM, method](const SkPixmap& prev, const SkPixmap& curr) { |
| 193 | SkBitmap bm; |
| 194 | SkBitmapScaler::Resize(&bm, prev, method, curr.width(), curr.height()); |
| 195 | return bm; |
| 196 | }); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 207 | |
reed | a40be09 | 2016-01-14 09:11:51 -0800 | [diff] [blame] | 208 | private: |
| 209 | typedef skiagm::GM INHERITED; |
| 210 | }; |
| 211 | DEF_GM( return new ShowMipLevels(255); ) |
| 212 | DEF_GM( return new ShowMipLevels(256); ) |
| 213 | |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 214 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 215 | |
| 216 | /** |
| 217 | * Show mip levels that were built, for all supported colortypes |
| 218 | */ |
| 219 | class ShowMipLevels2 : public skiagm::GM { |
| 220 | const int fW, fH; |
| 221 | SkBitmap fBM[4]; |
| 222 | |
| 223 | public: |
| 224 | ShowMipLevels2(int w, int h) : fW(w), fH(h) { |
reed | 32e0b4a | 2016-01-15 13:17:08 -0800 | [diff] [blame] | 225 | fBM[0] = sk_tool_utils::create_checkerboard_bitmap(w, h, SHOW_MIP_COLOR, SK_ColorWHITE, 2); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 226 | fBM[1] = make_bitmap(w, h); |
| 227 | fBM[2] = make_bitmap2(w, h); |
| 228 | fBM[3] = make_bitmap3(w, h); |
| 229 | } |
| 230 | |
| 231 | protected: |
| 232 | |
| 233 | SkString onShortName() override { |
| 234 | SkString str; |
| 235 | str.printf("showmiplevels2_%dx%d", fW, fH); |
| 236 | return str; |
| 237 | } |
| 238 | |
| 239 | SkISize onISize() override { |
| 240 | return { 824, 862 }; |
| 241 | } |
| 242 | |
| 243 | static void DrawAndFrame(SkCanvas* canvas, const SkBitmap& bm, SkScalar x, SkScalar y) { |
| 244 | canvas->drawBitmap(bm, x, y, nullptr); |
| 245 | SkPaint paint; |
| 246 | paint.setStyle(SkPaint::kStroke_Style); |
| 247 | paint.setColor(0xFFFFCCCC); |
| 248 | canvas->drawRect(SkRect::MakeIWH(bm.width(), bm.height()).makeOffset(x, y).makeOutset(0.5f, 0.5f), paint); |
| 249 | } |
| 250 | |
| 251 | void drawLevels(SkCanvas* canvas, const SkBitmap& baseBM) { |
| 252 | SkScalar x = 4; |
| 253 | SkScalar y = 4; |
| 254 | |
| 255 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(baseBM, nullptr)); |
| 256 | |
| 257 | int index = 0; |
| 258 | SkMipMap::Level level; |
| 259 | SkScalar scale = 0.5f; |
fmalita | 33ed3ad | 2016-02-09 08:20:18 -0800 | [diff] [blame] | 260 | while (mm->extractLevel(SkSize::Make(scale, scale), &level)) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 261 | SkBitmap bm; |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 262 | bm.installPixels(level.fPixmap); |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 263 | DrawAndFrame(canvas, bm, x, y); |
| 264 | |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 265 | if (level.fPixmap.width() <= 2 || level.fPixmap.height() <= 2) { |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | if (index & 1) { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 269 | x += level.fPixmap.width() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 270 | } else { |
reed | 67b09bf | 2016-01-16 18:50:35 -0800 | [diff] [blame] | 271 | y += level.fPixmap.height() + 4; |
reed | 01dc44a | 2016-01-15 10:51:08 -0800 | [diff] [blame] | 272 | } |
| 273 | scale /= 2; |
| 274 | index += 1; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void drawSet(SkCanvas* canvas, const SkBitmap& orig) { |
| 279 | const SkColorType ctypes[] = { |
| 280 | kN32_SkColorType, kRGB_565_SkColorType, kARGB_4444_SkColorType, kGray_8_SkColorType |
| 281 | }; |
| 282 | |
| 283 | SkAutoCanvasRestore acr(canvas, true); |
| 284 | |
| 285 | for (auto ctype : ctypes) { |
| 286 | SkBitmap bm; |
| 287 | orig.copyTo(&bm, ctype); |
| 288 | drawLevels(canvas, bm); |
| 289 | canvas->translate(orig.width()/2 + 8.0f, 0); |
| 290 | } |
| 291 | } |
| 292 | |
| 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); ) |