humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkFont.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/core/SkTime.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/utils/SkRandom.h" |
| 15 | #include "samplecode/DecodeFile.h" |
| 16 | #include "samplecode/Sample.h" |
| 17 | #include "src/core/SkClipOpPriv.h" |
| 18 | #include "tools/Resources.h" |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 19 | |
| 20 | // Intended to exercise pixel snapping observed with scaled images (and |
| 21 | // with non-scaled images, but for a different reason): Bug 1145 |
| 22 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 23 | class IdentityScaleView : public Sample { |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 24 | public: |
| 25 | IdentityScaleView(const char imageFilename[]) { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 26 | if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) { |
| 27 | fBM.allocN32Pixels(1, 1); |
| 28 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 29 | } |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | protected: |
| 33 | SkBitmap fBM; |
| 34 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 35 | SkString name() override { return SkString("IdentityScale"); } |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | void onDrawContent(SkCanvas* canvas) override { |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 38 | |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 39 | SkFont font(nullptr, 48); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 40 | SkPaint paint; |
| 41 | |
| 42 | paint.setAntiAlias(true); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 43 | paint.setFilterQuality(kHigh_SkFilterQuality); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 44 | |
| 45 | SkTime::DateTime time; |
| 46 | SkTime::GetDateTime(&time); |
| 47 | |
| 48 | bool use_scale = (time.fSecond % 2 == 1); |
| 49 | const char *text; |
| 50 | |
| 51 | canvas->save(); |
| 52 | if (use_scale) { |
| 53 | text = "Scaled = 1"; |
| 54 | } else { |
| 55 | |
| 56 | SkRect r = { 100, 100, 356, 356 }; |
| 57 | SkPath clipPath; |
Mike Reed | 4241f5e | 2019-09-14 19:13:23 +0000 | [diff] [blame] | 58 | clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 59 | canvas->clipPath(clipPath, kIntersect_SkClipOp, true); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 60 | text = "Scaled = 0"; |
| 61 | } |
| 62 | canvas->drawBitmap( fBM, 100, 100, &paint ); |
| 63 | canvas->restore(); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 64 | canvas->drawString(text, 100, 400, font, paint); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 68 | typedef Sample INHERITED; |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | ////////////////////////////////////////////////////////////////////////////// |
| 72 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 73 | DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); ) |