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 | |
| 17 | static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 18 | sk_tool_utils::set_portable_typeface(paint, name, style); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | class DownsampleBitmapGM : public skiagm::GM { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 22 | public: |
| 23 | SkBitmap fBM; |
| 24 | SkString fName; |
| 25 | bool fBitmapMade; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 26 | SkPaint::FilterLevel fFilterLevel; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 27 | |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 28 | DownsampleBitmapGM(SkPaint::FilterLevel filterLevel) |
| 29 | : fFilterLevel(filterLevel) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 30 | { |
| 31 | this->setBGColor(0xFFDDDDDD); |
| 32 | fBitmapMade = false; |
| 33 | } |
| 34 | |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 35 | const char* filterLevelToString() { |
| 36 | static const char *filterLevelNames[] = { |
| 37 | "none", "low", "medium", "high" |
| 38 | }; |
| 39 | return filterLevelNames[fFilterLevel]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 40 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 41 | |
| 42 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 43 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
humper | d73c169 | 2014-08-28 14:27:42 -0700 | [diff] [blame] | 44 | return kSkipTiled_Flag; |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 45 | } |
| 46 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 47 | virtual SkString onShortName() SK_OVERRIDE { |
| 48 | return fName; |
| 49 | } |
| 50 | |
| 51 | virtual SkISize onISize() SK_OVERRIDE { |
| 52 | make_bitmap_wrapper(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 53 | return SkISize::Make(fBM.width(), 4 * fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void make_bitmap_wrapper() { |
| 57 | if (!fBitmapMade) { |
| 58 | fBitmapMade = true; |
| 59 | make_bitmap(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | virtual void make_bitmap() = 0; |
| 64 | |
| 65 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 66 | make_bitmap_wrapper(); |
| 67 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 68 | int curY = 0; |
| 69 | int curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 70 | float curScale = 1; |
| 71 | do { |
| 72 | |
| 73 | SkMatrix matrix; |
| 74 | matrix.setScale( curScale, curScale ); |
| 75 | |
| 76 | SkPaint paint; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 77 | paint.setFilterLevel(fFilterLevel); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 78 | |
| 79 | canvas->save(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 80 | canvas->translate(0, (SkScalar)curY); |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 81 | canvas->concat(matrix); |
| 82 | canvas->drawBitmap(fBM, 0, 0, &paint); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 83 | canvas->restore(); |
| 84 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 85 | curHeight = (int) (fBM.height() * curScale + 2); |
| 86 | curY += curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 87 | curScale *= 0.75f; |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 88 | } while (curHeight >= 2 && curY < 4*fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
| 92 | typedef skiagm::GM INHERITED; |
| 93 | }; |
| 94 | |
| 95 | class DownsampleBitmapTextGM: public DownsampleBitmapGM { |
| 96 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 97 | DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel) |
| 98 | : INHERITED(filterLevel), fTextSize(textSize) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 99 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 100 | fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | protected: |
| 104 | float fTextSize; |
| 105 | |
| 106 | virtual void make_bitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 107 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 108 | SkCanvas canvas(fBM); |
| 109 | canvas.drawColor(SK_ColorWHITE); |
| 110 | |
| 111 | SkPaint paint; |
| 112 | paint.setAntiAlias(true); |
| 113 | paint.setSubpixelText(true); |
| 114 | paint.setTextSize(fTextSize); |
| 115 | |
| 116 | setTypeface(&paint, "Times", SkTypeface::kNormal); |
| 117 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
| 118 | setTypeface(&paint, "Times", SkTypeface::kBold); |
| 119 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
| 120 | setTypeface(&paint, "Times", SkTypeface::kItalic); |
| 121 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
| 122 | setTypeface(&paint, "Times", SkTypeface::kBoldItalic); |
| 123 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
| 124 | } |
| 125 | private: |
| 126 | typedef DownsampleBitmapGM INHERITED; |
| 127 | }; |
| 128 | |
| 129 | class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM { |
| 130 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 131 | DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel) |
| 132 | : INHERITED(filterLevel), fSize(size), fNumChecks(numChecks) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 133 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 134 | fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | protected: |
| 138 | int fSize; |
| 139 | int fNumChecks; |
| 140 | |
| 141 | virtual void make_bitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 142 | fBM.allocN32Pixels(fSize, fSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 143 | for (int y = 0; y < fSize; ++y) { |
| 144 | for (int x = 0; x < fSize; ++x) { |
| 145 | SkPMColor* s = fBM.getAddr32(x, y); |
| 146 | int cx = (x * fNumChecks) / fSize; |
| 147 | int cy = (y * fNumChecks) / fSize; |
| 148 | if ((cx+cy)%2) { |
| 149 | *s = 0xFFFFFFFF; |
| 150 | } else { |
| 151 | *s = 0xFF000000; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | private: |
| 157 | typedef DownsampleBitmapGM INHERITED; |
| 158 | }; |
| 159 | |
| 160 | class DownsampleBitmapImageGM: public DownsampleBitmapGM { |
| 161 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 162 | DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel) |
| 163 | : INHERITED(filterLevel), fFilename(filename) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 164 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 165 | fName.printf("downsamplebitmap_image_%s_%s", this->filterLevelToString(), 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 | |
| 172 | virtual void make_bitmap() SK_OVERRIDE { |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 173 | SkImageDecoder* codec = NULL; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 174 | SkString resourcePath = GetResourcePath(fFilename.c_str()); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 175 | SkFILEStream stream(resourcePath.c_str()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 176 | if (stream.isValid()) { |
| 177 | codec = SkImageDecoder::Factory(&stream); |
| 178 | } |
| 179 | if (codec) { |
| 180 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 181 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 182 | SkDELETE(codec); |
| 183 | } else { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 184 | fBM.allocN32Pixels(1, 1); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 185 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 186 | } |
| 187 | fSize = fBM.height(); |
| 188 | } |
| 189 | private: |
| 190 | typedef DownsampleBitmapGM INHERITED; |
| 191 | }; |
| 192 | |
| 193 | ////////////////////////////////////////////////////////////////////////////// |
| 194 | |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 195 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); ) |
| 196 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); ) |
| 197 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 198 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
| 199 | SkPaint::kHigh_FilterLevel); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 200 | |
| 201 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); ) |
| 202 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); ) |
| 203 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 204 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
| 205 | SkPaint::kMedium_FilterLevel); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 206 | |
| 207 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); ) |
| 208 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); ) |
| 209 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 210 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
| 211 | SkPaint::kLow_FilterLevel); ) |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 212 | |
| 213 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); ) |
| 214 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); ) |
| 215 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); ) |
bsalomon | efd7d45 | 2014-10-23 14:17:46 -0700 | [diff] [blame] | 216 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_132x132_12x12.astc", |
| 217 | SkPaint::kNone_FilterLevel); ) |