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" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 9 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 11 | #include "SkBitmapProcState.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 12 | #include "SkBitmapScaler.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkImageDecoder.h" |
| 15 | #include "SkImageEncoder.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 18 | |
| 19 | static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { |
| 20 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), |
| 21 | SkIntToScalar(bm.height())); |
| 22 | mat.mapRect(&bounds); |
| 23 | return SkSize::Make(bounds.width(), bounds.height()); |
| 24 | } |
| 25 | |
| 26 | static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { |
| 27 | SkPaint paint; |
| 28 | |
| 29 | SkAutoCanvasRestore acr(canvas, true); |
| 30 | |
| 31 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 32 | |
| 33 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
| 34 | canvas->translate(dx, 0); |
| 35 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 36 | |
| 37 | paint.setFilterLevel(SkPaint::kMedium_FilterLevel); |
| 38 | canvas->translate(dx, 0); |
| 39 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 40 | |
| 41 | paint.setFilterLevel(SkPaint::kHigh_FilterLevel); |
| 42 | canvas->translate(dx, 0); |
| 43 | canvas->drawBitmapMatrix(bm, mat, &paint); |
| 44 | } |
| 45 | |
| 46 | class FilterIndiaBoxGM : public skiagm::GM { |
| 47 | void onOnceBeforeDraw() { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 48 | this->makeBitmap(); |
| 49 | |
| 50 | SkScalar cx = SkScalarHalf(fBM.width()); |
| 51 | SkScalar cy = SkScalarHalf(fBM.height()); |
| 52 | |
| 53 | float vertScale = 30.0f/55.0f; |
| 54 | float horizScale = 150.0f/200.0f; |
| 55 | |
| 56 | fMatrix[0].setScale(horizScale, vertScale); |
| 57 | fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale); |
| 58 | } |
| 59 | |
| 60 | public: |
| 61 | SkBitmap fBM; |
| 62 | SkMatrix fMatrix[2]; |
| 63 | SkString fName; |
| 64 | |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 65 | FilterIndiaBoxGM() { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 66 | this->setBGColor(0xFFDDDDDD); |
| 67 | } |
| 68 | |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 69 | FilterIndiaBoxGM(const char filename[]) : fFilename(filename) { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 70 | fName.printf("filterindiabox"); |
| 71 | } |
| 72 | |
| 73 | protected: |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 74 | virtual SkString onShortName() SK_OVERRIDE { |
| 75 | return fName; |
| 76 | } |
| 77 | |
Derek Sollenberger | 5353bae | 2014-09-04 09:51:33 -0400 | [diff] [blame] | 78 | #ifdef SK_CPU_ARM64 |
| 79 | // Skip tiled drawing on 64-bit ARM until https://skbug.com/2908 is fixed. |
| 80 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 81 | return kSkipTiled_Flag; |
| 82 | } |
| 83 | #endif |
| 84 | |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 85 | virtual SkISize onISize() SK_OVERRIDE { |
| 86 | return SkISize::Make(1024, 768); |
| 87 | } |
| 88 | |
| 89 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 90 | canvas->translate(10, 10); |
| 91 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { |
| 92 | SkSize size = computeSize(fBM, fMatrix[i]); |
| 93 | size.fWidth += 20; |
| 94 | size.fHeight += 20; |
| 95 | |
| 96 | draw_row(canvas, fBM, fMatrix[i], size.fWidth); |
| 97 | canvas->translate(0, size.fHeight); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | protected: |
| 102 | SkString fFilename; |
| 103 | int fSize; |
| 104 | |
| 105 | SkScalar getScale() { |
| 106 | return 192.f/fSize; |
| 107 | } |
| 108 | |
| 109 | void makeBitmap() { |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 110 | SkImageDecoder* codec = NULL; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 111 | SkString resourcePath = GetResourcePath(fFilename.c_str()); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 112 | SkFILEStream stream(resourcePath.c_str()); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 113 | if (stream.isValid()) { |
| 114 | codec = SkImageDecoder::Factory(&stream); |
| 115 | } |
| 116 | if (codec) { |
| 117 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 118 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
commit-bot@chromium.org | f449156 | 2014-05-28 17:30:02 +0000 | [diff] [blame] | 119 | SkDELETE(codec); |
| 120 | } else { |
| 121 | fBM.allocN32Pixels(1, 1); |
| 122 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 123 | } |
| 124 | fSize = fBM.height(); |
| 125 | } |
| 126 | private: |
| 127 | typedef skiagm::GM INHERITED; |
| 128 | }; |
| 129 | |
| 130 | ////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | |
| 133 | DEF_GM( return new FilterIndiaBoxGM("box.gif"); ) |