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" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 15 | |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 16 | static void make_checker(SkBitmap* bm, int size, int numChecks) { |
| 17 | bm->allocN32Pixels(size, size); |
| 18 | for (int y = 0; y < size; ++y) { |
| 19 | for (int x = 0; x < size; ++x) { |
| 20 | SkPMColor* s = bm->getAddr32(x, y); |
| 21 | int cx = (x * numChecks) / size; |
| 22 | int cy = (y * numChecks) / size; |
| 23 | if ((cx+cy)%2) { |
| 24 | *s = 0xFFFFFFFF; |
| 25 | } else { |
| 26 | *s = 0xFF000000; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 32 | static void setTypeface(SkPaint* paint, const char name[], SkFontStyle style) { |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 33 | sk_tool_utils::set_portable_typeface(paint, name, style); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | class DownsampleBitmapGM : public skiagm::GM { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 37 | public: |
| 38 | SkBitmap fBM; |
| 39 | SkString fName; |
| 40 | bool fBitmapMade; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 41 | SkFilterQuality fFilterQuality; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 42 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 43 | DownsampleBitmapGM(SkFilterQuality filterQuality) |
| 44 | : fFilterQuality(filterQuality) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 45 | { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 46 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 47 | fBitmapMade = false; |
| 48 | } |
| 49 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 50 | const char* filterQualityToString() { |
| 51 | static const char *filterQualityNames[] = { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 52 | "none", "low", "medium", "high" |
| 53 | }; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 54 | return filterQualityNames[fFilterQuality]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 55 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 56 | |
| 57 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 58 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 59 | SkString onShortName() override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 60 | return fName; |
| 61 | } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | SkISize onISize() override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 64 | make_bitmap_wrapper(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 65 | return SkISize::Make(fBM.width(), 4 * fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void make_bitmap_wrapper() { |
| 69 | if (!fBitmapMade) { |
| 70 | fBitmapMade = true; |
| 71 | make_bitmap(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | virtual void make_bitmap() = 0; |
| 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | void onDraw(SkCanvas* canvas) override { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 78 | make_bitmap_wrapper(); |
| 79 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 80 | int curY = 0; |
| 81 | int curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 82 | float curScale = 1; |
| 83 | do { |
| 84 | |
| 85 | SkMatrix matrix; |
| 86 | matrix.setScale( curScale, curScale ); |
| 87 | |
| 88 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 89 | paint.setFilterQuality(fFilterQuality); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 90 | |
| 91 | canvas->save(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 92 | canvas->translate(0, (SkScalar)curY); |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 93 | canvas->concat(matrix); |
| 94 | canvas->drawBitmap(fBM, 0, 0, &paint); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 95 | canvas->restore(); |
| 96 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 97 | curHeight = (int) (fBM.height() * curScale + 2); |
| 98 | curY += curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 99 | curScale *= 0.75f; |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 100 | } while (curHeight >= 2 && curY < 4*fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
| 104 | typedef skiagm::GM INHERITED; |
| 105 | }; |
| 106 | |
| 107 | class DownsampleBitmapTextGM: public DownsampleBitmapGM { |
| 108 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 109 | DownsampleBitmapTextGM(float textSize, SkFilterQuality filterQuality) |
| 110 | : INHERITED(filterQuality), fTextSize(textSize) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 111 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 112 | fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterQualityToString(), fTextSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | protected: |
| 116 | float fTextSize; |
| 117 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 118 | void make_bitmap() override { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 119 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 120 | SkCanvas canvas(fBM); |
| 121 | canvas.drawColor(SK_ColorWHITE); |
| 122 | |
| 123 | SkPaint paint; |
| 124 | paint.setAntiAlias(true); |
| 125 | paint.setSubpixelText(true); |
| 126 | paint.setTextSize(fTextSize); |
| 127 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 128 | setTypeface(&paint, "serif", SkFontStyle()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 129 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 130 | setTypeface(&paint, "serif", SkFontStyle::FromOldStyle(SkTypeface::kBold)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 131 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 132 | setTypeface(&paint, "serif", SkFontStyle::FromOldStyle(SkTypeface::kItalic)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 133 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 134 | setTypeface(&paint, "serif", SkFontStyle::FromOldStyle(SkTypeface::kBoldItalic)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 135 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
| 136 | } |
| 137 | private: |
| 138 | typedef DownsampleBitmapGM INHERITED; |
| 139 | }; |
| 140 | |
| 141 | class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM { |
| 142 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 143 | DownsampleBitmapCheckerboardGM(int size, int numChecks, SkFilterQuality filterQuality) |
| 144 | : INHERITED(filterQuality), fSize(size), fNumChecks(numChecks) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 145 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 146 | fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterQualityToString(), fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | protected: |
| 150 | int fSize; |
| 151 | int fNumChecks; |
| 152 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 153 | void make_bitmap() override { |
reed | c695e95 | 2015-01-16 05:01:16 -0800 | [diff] [blame] | 154 | make_checker(&fBM, fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 155 | } |
| 156 | private: |
| 157 | typedef DownsampleBitmapGM INHERITED; |
| 158 | }; |
| 159 | |
| 160 | class DownsampleBitmapImageGM: public DownsampleBitmapGM { |
| 161 | public: |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 162 | DownsampleBitmapImageGM(const char filename[], SkFilterQuality filterQuality) |
| 163 | : INHERITED(filterQuality), fFilename(filename) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 164 | { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 165 | fName.printf("downsamplebitmap_image_%s_%s", this->filterQualityToString(), filename); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | protected: |
| 169 | SkString fFilename; |
| 170 | int fSize; |
| 171 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 172 | void make_bitmap() override { |
msarett | e820dfe | 2016-03-18 12:13:47 -0700 | [diff] [blame] | 173 | if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) { |
| 174 | fBM.allocN32Pixels(1, 1); |
| 175 | fBM.eraseARGB(255, 255, 0 , 0); // red == bad |
| 176 | } |
| 177 | fSize = fBM.height(); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 178 | } |
| 179 | private: |
| 180 | typedef DownsampleBitmapGM INHERITED; |
| 181 | }; |
| 182 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 183 | DEF_GM( return new DownsampleBitmapTextGM(72, kHigh_SkFilterQuality); ) |
| 184 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kHigh_SkFilterQuality); ) |
| 185 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kHigh_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 186 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 187 | DEF_GM( return new DownsampleBitmapTextGM(72, kMedium_SkFilterQuality); ) |
| 188 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kMedium_SkFilterQuality); ) |
| 189 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kMedium_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 190 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 191 | DEF_GM( return new DownsampleBitmapTextGM(72, kLow_SkFilterQuality); ) |
| 192 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kLow_SkFilterQuality); ) |
| 193 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kLow_SkFilterQuality); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 194 | |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 195 | DEF_GM( return new DownsampleBitmapTextGM(72, kNone_SkFilterQuality); ) |
| 196 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, kNone_SkFilterQuality); ) |
| 197 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", kNone_SkFilterQuality); ) |