humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 9 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
| 11 | #include "SkGradientShader.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 12 | #include "SkTypeface.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkStream.h" |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 16 | |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 17 | static void make_checker(SkBitmap* bm, int size, int numChecks) { |
| 18 | bm->allocN32Pixels(size, size); |
| 19 | for (int y = 0; y < size; ++y) { |
| 20 | for (int x = 0; x < size; ++x) { |
| 21 | SkPMColor* s = bm->getAddr32(x, y); |
| 22 | int cx = (x * numChecks) / size; |
| 23 | int cy = (y * numChecks) / size; |
| 24 | if ((cx+cy)%2) { |
| 25 | *s = 0xFFFFFFFF; |
| 26 | } else { |
| 27 | *s = 0xFF000000; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 33 | static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 34 | sk_tool_utils::set_portable_typeface_always(paint, name, style); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | class DownsampleBitmapGM : public skiagm::GM { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 38 | public: |
| 39 | SkBitmap fBM; |
| 40 | SkString fName; |
| 41 | bool fBitmapMade; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 42 | SkFilterQuality fFilterQuality; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 43 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 44 | DownsampleBitmapGM(SkFilterQuality filterQuality) |
| 45 | : fFilterQuality(filterQuality) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 46 | { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 47 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 48 | fBitmapMade = false; |
| 49 | } |
| 50 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 51 | const char* filterQualityToString() { |
| 52 | static const char *filterQualityNames[] = { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 53 | "none", "low", "medium", "high" |
| 54 | }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 55 | return filterQualityNames[fFilterQuality]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 56 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 57 | |
| 58 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | SkString onShortName() override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 61 | return fName; |
| 62 | } |
| 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | SkISize onISize() override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 65 | make_bitmap_wrapper(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 66 | return SkISize::Make(fBM.width(), 4 * fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void make_bitmap_wrapper() { |
| 70 | if (!fBitmapMade) { |
| 71 | fBitmapMade = true; |
| 72 | make_bitmap(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | virtual void make_bitmap() = 0; |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 79 | make_bitmap_wrapper(); |
| 80 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 81 | int curY = 0; |
| 82 | int curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 83 | float curScale = 1; |
| 84 | do { |
| 85 | |
| 86 | SkMatrix matrix; |
| 87 | matrix.setScale( curScale, curScale ); |
| 88 | |
| 89 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 90 | paint.setFilterQuality(fFilterQuality); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 91 | |
| 92 | canvas->save(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 93 | canvas->translate(0, (SkScalar)curY); |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 94 | canvas->concat(matrix); |
| 95 | canvas->drawBitmap(fBM, 0, 0, &paint); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 96 | canvas->restore(); |
| 97 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 98 | curHeight = (int) (fBM.height() * curScale + 2); |
| 99 | curY += curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 100 | curScale *= 0.75f; |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 101 | } while (curHeight >= 2 && curY < 4*fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private: |
| 105 | typedef skiagm::GM INHERITED; |
| 106 | }; |
| 107 | |
| 108 | class DownsampleBitmapTextGM: public DownsampleBitmapGM { |
| 109 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 110 | DownsampleBitmapTextGM(float textSize, SkFilterQuality filterQuality) |
| 111 | : INHERITED(filterQuality), fTextSize(textSize) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 112 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 113 | fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterQualityToString(), fTextSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | protected: |
| 117 | float fTextSize; |
| 118 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 119 | void make_bitmap() override { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 120 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 121 | SkCanvas canvas(fBM); |
| 122 | canvas.drawColor(SK_ColorWHITE); |
| 123 | |
| 124 | SkPaint paint; |
| 125 | paint.setAntiAlias(true); |
| 126 | paint.setSubpixelText(true); |
| 127 | paint.setTextSize(fTextSize); |
| 128 | |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 129 | setTypeface(&paint, "serif", SkTypeface::kNormal); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 130 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 131 | setTypeface(&paint, "serif", SkTypeface::kBold); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 132 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 133 | setTypeface(&paint, "serif", SkTypeface::kItalic); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 134 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 135 | setTypeface(&paint, "serif", SkTypeface::kBoldItalic); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 136 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
| 137 | } |
| 138 | private: |
| 139 | typedef DownsampleBitmapGM INHERITED; |
| 140 | }; |
| 141 | |
| 142 | class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM { |
| 143 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 144 | DownsampleBitmapCheckerboardGM(int size, int numChecks, SkFilterQuality filterQuality) |
| 145 | : INHERITED(filterQuality), fSize(size), fNumChecks(numChecks) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 146 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 147 | fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterQualityToString(), fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | protected: |
| 151 | int fSize; |
| 152 | int fNumChecks; |
| 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | void make_bitmap() override { |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 155 | make_checker(&fBM, fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 156 | } |
| 157 | private: |
| 158 | typedef DownsampleBitmapGM INHERITED; |
| 159 | }; |
| 160 | |
| 161 | class DownsampleBitmapImageGM: public DownsampleBitmapGM { |
| 162 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 163 | DownsampleBitmapImageGM(const char filename[], SkFilterQuality filterQuality) |
| 164 | : INHERITED(filterQuality), fFilename(filename) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 165 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 166 | fName.printf("downsamplebitmap_image_%s_%s", this->filterQualityToString(), filename); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | protected: |
| 170 | SkString fFilename; |
| 171 | int fSize; |
| 172 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | void make_bitmap() override { |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 174 | SkImageDecoder* codec = NULL; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 175 | SkString resourcePath = GetResourcePath(fFilename.c_str()); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 176 | SkFILEStream stream(resourcePath.c_str()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 177 | if (stream.isValid()) { |
| 178 | codec = SkImageDecoder::Factory(&stream); |
| 179 | } |
| 180 | if (codec) { |
| 181 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 182 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 183 | SkDELETE(codec); |
| 184 | } else { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 185 | fBM.allocN32Pixels(1, 1); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 186 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 187 | } |
| 188 | fSize = fBM.height(); |
| 189 | } |
| 190 | private: |
| 191 | typedef DownsampleBitmapGM INHERITED; |
| 192 | }; |
| 193 | |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 194 | #include "SkMipMap.h" |
halcanary | a1cb378 | 2015-02-24 09:25:09 -0800 | [diff] [blame] | 195 | |
| 196 | static void release_mipmap(void*, void* context) { |
| 197 | ((SkMipMap*)context)->unref(); |
| 198 | } |
| 199 | |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 200 | class ShowMipLevels : public skiagm::GM { |
| 201 | public: |
| 202 | SkBitmap fBM; |
| 203 | |
| 204 | ShowMipLevels() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 205 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 206 | make_checker(&fBM, 512, 256); |
| 207 | } |
| 208 | |
| 209 | protected: |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 210 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 211 | SkString onShortName() override { |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 212 | return SkString("showmiplevels"); |
| 213 | } |
| 214 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 215 | SkISize onISize() override { |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 216 | return SkISize::Make(fBM.width() + 8, 2 * fBM.height() + 80); |
| 217 | } |
| 218 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 219 | void onDraw(SkCanvas* canvas) override { |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 220 | SkScalar x = 4; |
| 221 | SkScalar y = 4; |
| 222 | canvas->drawBitmap(fBM, x, y, NULL); |
| 223 | y += fBM.height() + 4; |
| 224 | |
| 225 | SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(fBM, NULL)); |
| 226 | |
| 227 | SkMipMap::Level level; |
| 228 | SkScalar scale = 0.5f; |
| 229 | while (mm->extractLevel(scale, &level)) { |
| 230 | SkImageInfo info = SkImageInfo::MakeN32Premul(level.fWidth, level.fHeight); |
| 231 | SkBitmap bm; |
halcanary | a1cb378 | 2015-02-24 09:25:09 -0800 | [diff] [blame] | 232 | bm.installPixels(info, level.fPixels, level.fRowBytes, NULL, |
| 233 | &release_mipmap, (void*)(SkRef(mm.get()))); |
| 234 | bm.setImmutable(); |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 235 | canvas->drawBitmap(bm, x, y, NULL); |
| 236 | y += bm.height() + 4; |
| 237 | scale /= 2; |
| 238 | if (info.width() == 1 || info.height() == 1) { |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
mtklein | 1c40292 | 2015-01-23 11:07:07 -0800 | [diff] [blame] | 243 | |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 244 | private: |
| 245 | typedef skiagm::GM INHERITED; |
| 246 | }; |
| 247 | DEF_GM( return new ShowMipLevels; ) |
| 248 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 249 | ////////////////////////////////////////////////////////////////////////////// |
| 250 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 251 | DEF_GM( return new DownsampleBitmapTextGM(72, kHigh_SkFilterQuality); ) |
| 252 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kHigh_SkFilterQuality); ) |
| 253 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kHigh_SkFilterQuality); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 254 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 255 | kHigh_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 256 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 257 | DEF_GM( return new DownsampleBitmapTextGM(72, kMedium_SkFilterQuality); ) |
| 258 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kMedium_SkFilterQuality); ) |
| 259 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kMedium_SkFilterQuality); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 260 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 261 | kMedium_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 262 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 263 | DEF_GM( return new DownsampleBitmapTextGM(72, kLow_SkFilterQuality); ) |
| 264 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kLow_SkFilterQuality); ) |
| 265 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kLow_SkFilterQuality); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 266 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 267 | kLow_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 268 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 269 | DEF_GM( return new DownsampleBitmapTextGM(72, kNone_SkFilterQuality); ) |
| 270 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kNone_SkFilterQuality); ) |
| 271 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kNone_SkFilterQuality); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 272 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 273 | kNone_SkFilterQuality); ) |