reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 9 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
| 11 | #include "SkGradientShader.h" |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 12 | #include "SkImageDecoder.h" |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkTypeface.h" |
reed@google.com | 7b06ba4 | 2013-05-28 16:39:52 +0000 | [diff] [blame] | 15 | |
| 16 | static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 17 | sk_tool_utils::set_portable_typeface(paint, name, style); |
reed@google.com | 7b06ba4 | 2013-05-28 16:39:52 +0000 | [diff] [blame] | 18 | } |
| 19 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 20 | static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { |
reed@google.com | 98447b0 | 2013-05-28 15:42:21 +0000 | [diff] [blame] | 21 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), |
| 22 | SkIntToScalar(bm.height())); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 23 | mat.mapRect(&bounds); |
reed@google.com | 98447b0 | 2013-05-28 15:42:21 +0000 | [diff] [blame] | 24 | return SkSize::Make(bounds.width(), bounds.height()); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 25 | } |
| 26 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 27 | static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 28 | SkPaint paint; |
| 29 | |
| 30 | SkAutoCanvasRestore acr(canvas, true); |
| 31 | |
| 32 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 33 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 34 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 35 | canvas->translate(dx, 0); |
| 36 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 37 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 38 | paint.setFilterLevel(SkPaint::kMedium_FilterLevel); |
| 39 | canvas->translate(dx, 0); |
| 40 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 41 | |
reed@google.com | 9cfc83c | 2013-07-22 17:18:18 +0000 | [diff] [blame] | 42 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 43 | canvas->translate(dx, 0); |
| 44 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 45 | } |
| 46 | |
| 47 | class FilterBitmapGM : public skiagm::GM { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 48 | void onOnceBeforeDraw() { |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 50 | this->makeBitmap(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 51 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 52 | SkScalar cx = SkScalarHalf(fBM.width()); |
| 53 | SkScalar cy = SkScalarHalf(fBM.height()); |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 54 | SkScalar scale = this->getScale(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 55 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 56 | // these two matrices use a scale factor configured by the subclass |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 57 | fMatrix[0].setScale(scale, scale); |
| 58 | fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 59 | |
| 60 | // up/down scaling mix |
| 61 | fMatrix[2].setScale(0.7f, 1.05f); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public: |
| 65 | SkBitmap fBM; |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 66 | SkMatrix fMatrix[3]; |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 67 | SkString fName; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 68 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 69 | FilterBitmapGM() |
| 70 | { |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 71 | this->setBGColor(0xFFDDDDDD); |
| 72 | } |
| 73 | |
| 74 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 75 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 76 | return kSkipTiled_Flag; |
| 77 | } |
| 78 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 79 | virtual SkString onShortName() SK_OVERRIDE { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 80 | return fName; |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | virtual SkISize onISize() SK_OVERRIDE { |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 84 | return SkISize::Make(1024, 768); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 85 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 86 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 87 | virtual void makeBitmap() = 0; |
| 88 | virtual SkScalar getScale() = 0; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 89 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 90 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 91 | |
| 92 | canvas->translate(10, 10); |
| 93 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { |
| 94 | SkSize size = computeSize(fBM, fMatrix[i]); |
| 95 | size.fWidth += 20; |
| 96 | size.fHeight += 20; |
| 97 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 98 | draw_row(canvas, fBM, fMatrix[i], size.fWidth); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 99 | canvas->translate(0, size.fHeight); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | typedef skiagm::GM INHERITED; |
| 105 | }; |
| 106 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 107 | class FilterBitmapTextGM: public FilterBitmapGM { |
| 108 | public: |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 109 | FilterBitmapTextGM(float textSize) |
| 110 | : fTextSize(textSize) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 111 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 112 | fName.printf("filterbitmap_text_%.2fpt", fTextSize); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 113 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 114 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 115 | protected: |
| 116 | float fTextSize; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 117 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 118 | SkScalar getScale() SK_OVERRIDE { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 119 | return 32.f/fTextSize; |
| 120 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 121 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 122 | void makeBitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 123 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 124 | SkCanvas canvas(fBM); |
| 125 | canvas.drawColor(SK_ColorWHITE); |
| 126 | |
| 127 | SkPaint paint; |
| 128 | paint.setAntiAlias(true); |
| 129 | paint.setSubpixelText(true); |
| 130 | paint.setTextSize(fTextSize); |
| 131 | |
| 132 | setTypeface(&paint, "Times", SkTypeface::kNormal); |
humper@google.com | 6dce113 | 2013-05-31 12:58:29 +0000 | [diff] [blame] | 133 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 134 | setTypeface(&paint, "Times", SkTypeface::kBold); |
humper@google.com | 6dce113 | 2013-05-31 12:58:29 +0000 | [diff] [blame] | 135 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 136 | setTypeface(&paint, "Times", SkTypeface::kItalic); |
humper@google.com | 6dce113 | 2013-05-31 12:58:29 +0000 | [diff] [blame] | 137 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 138 | setTypeface(&paint, "Times", SkTypeface::kBoldItalic); |
humper@google.com | 6dce113 | 2013-05-31 12:58:29 +0000 | [diff] [blame] | 139 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 140 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 141 | private: |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 142 | typedef FilterBitmapGM INHERITED; |
| 143 | }; |
| 144 | |
| 145 | class FilterBitmapCheckerboardGM: public FilterBitmapGM { |
| 146 | public: |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 147 | FilterBitmapCheckerboardGM(int size, int num_checks) |
| 148 | : fSize(size), fNumChecks(num_checks) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 149 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 150 | fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 151 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 152 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 153 | protected: |
| 154 | int fSize; |
| 155 | int fNumChecks; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 156 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 157 | SkScalar getScale() SK_OVERRIDE { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 158 | return 192.f/fSize; |
| 159 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 160 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 161 | void makeBitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 162 | fBM.allocN32Pixels(fSize, fSize); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 163 | for (int y = 0; y < fSize; y ++) { |
| 164 | for (int x = 0; x < fSize; x ++) { |
| 165 | SkPMColor* s = fBM.getAddr32(x, y); |
| 166 | int cx = (x * fNumChecks) / fSize; |
| 167 | int cy = (y * fNumChecks) / fSize; |
| 168 | if ((cx+cy)%2) { |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 169 | *s = 0xFFFFFFFF; |
| 170 | } else { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 171 | *s = 0xFF000000; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 176 | private: |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 177 | typedef FilterBitmapGM INHERITED; |
| 178 | }; |
| 179 | |
| 180 | class FilterBitmapImageGM: public FilterBitmapGM { |
| 181 | public: |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 182 | FilterBitmapImageGM(const char filename[]) |
| 183 | : fFilename(filename) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 184 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 185 | fName.printf("filterbitmap_image_%s", filename); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 186 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 187 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 188 | protected: |
| 189 | SkString fFilename; |
| 190 | int fSize; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 191 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 192 | SkScalar getScale() SK_OVERRIDE { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 193 | return 192.f/fSize; |
| 194 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 195 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 196 | void makeBitmap() SK_OVERRIDE { |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 197 | SkImageDecoder* codec = NULL; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 198 | SkString resourcePath = GetResourcePath(fFilename.c_str()); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 199 | SkFILEStream stream(resourcePath.c_str()); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 200 | if (stream.isValid()) { |
| 201 | codec = SkImageDecoder::Factory(&stream); |
| 202 | } |
| 203 | if (codec) { |
| 204 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 205 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 206 | SkDELETE(codec); |
| 207 | } else { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 208 | fBM.allocN32Pixels(1, 1); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 209 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 210 | } |
| 211 | fSize = fBM.height(); |
| 212 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 213 | private: |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 214 | typedef FilterBitmapGM INHERITED; |
| 215 | }; |
| 216 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 217 | ////////////////////////////////////////////////////////////////////////////// |
| 218 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 219 | DEF_GM( return new FilterBitmapTextGM(3); ) |
| 220 | DEF_GM( return new FilterBitmapTextGM(7); ) |
| 221 | DEF_GM( return new FilterBitmapTextGM(10); ) |
| 222 | DEF_GM( return new FilterBitmapCheckerboardGM(4,4); ) |
| 223 | DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) |
| 224 | DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) |
| 225 | DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) |
| 226 | DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) |
| 227 | DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) |
| 228 | DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) |
| 229 | DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) |
| 230 | DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) |
| 231 | DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) |
| 232 | DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) |
halcanary | dffa397 | 2014-10-27 10:32:18 -0700 | [diff] [blame] | 233 | DEF_GM( return new FilterBitmapImageGM("color_wheel.png"); ) |