commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 10 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 12 | #include "SkBitmapProcState.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkImageEncoder.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkTypeface.h" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 17 | |
| 18 | static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { |
| 19 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), |
| 20 | SkIntToScalar(bm.height())); |
| 21 | mat.mapRect(&bounds); |
| 22 | return SkSize::Make(bounds.width(), bounds.height()); |
| 23 | } |
| 24 | |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 25 | static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 26 | SkFilterQuality lvl) { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 27 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 28 | paint.setFilterQuality(lvl); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 29 | |
| 30 | SkAutoCanvasRestore acr(canvas, true); |
| 31 | |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 32 | canvas->translate(dx, 0); |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 33 | canvas->concat(mat); |
| 34 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 35 | } |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 36 | |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 37 | static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 38 | draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality); |
| 39 | draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality); |
| 40 | draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality); |
| 41 | draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | class FilterIndiaBoxGM : public skiagm::GM { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 46 | this->makeBitmap(); |
| 47 | |
| 48 | SkScalar cx = SkScalarHalf(fBM.width()); |
| 49 | SkScalar cy = SkScalarHalf(fBM.height()); |
| 50 | |
| 51 | float vertScale = 30.0f/55.0f; |
| 52 | float horizScale = 150.0f/200.0f; |
| 53 | |
| 54 | fMatrix[0].setScale(horizScale, vertScale); |
| 55 | fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale); |
| 56 | } |
| 57 | |
| 58 | public: |
| 59 | SkBitmap fBM; |
| 60 | SkMatrix fMatrix[2]; |
| 61 | SkString fName; |
| 62 | |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 63 | FilterIndiaBoxGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 64 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 65 | } |
| 66 | |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 67 | FilterIndiaBoxGM(const char filename[]) : fFilename(filename) { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 68 | fName.printf("filterindiabox"); |
| 69 | } |
| 70 | |
| 71 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | SkString onShortName() override { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 73 | return fName; |
| 74 | } |
| 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | SkISize onISize() override { |
Mike Reed | e32500f | 2017-07-19 17:20:37 -0400 | [diff] [blame] | 77 | return SkISize::Make(680, 130); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 78 | } |
| 79 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 81 | canvas->translate(10, 10); |
| 82 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { |
| 83 | SkSize size = computeSize(fBM, fMatrix[i]); |
| 84 | size.fWidth += 20; |
| 85 | size.fHeight += 20; |
| 86 | |
| 87 | draw_row(canvas, fBM, fMatrix[i], size.fWidth); |
| 88 | canvas->translate(0, size.fHeight); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | protected: |
| 93 | SkString fFilename; |
| 94 | int fSize; |
| 95 | |
| 96 | SkScalar getScale() { |
| 97 | return 192.f/fSize; |
| 98 | } |
| 99 | |
| 100 | void makeBitmap() { |
msarett | e820dfe | 2016-03-18 12:13:47 -0700 | [diff] [blame] | 101 | if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) { |
| 102 | fBM.allocN32Pixels(1, 1); |
| 103 | fBM.eraseARGB(255, 255, 0 , 0); // red == bad |
| 104 | } |
| 105 | fSize = fBM.height(); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 106 | } |
| 107 | private: |
| 108 | typedef skiagm::GM INHERITED; |
| 109 | }; |
| 110 | |
| 111 | ////////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | |
| 114 | DEF_GM( return new FilterIndiaBoxGM("box.gif"); ) |