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 | |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 8 | #include "DecodeFile.h" |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 9 | #include "gm.h" |
| 10 | |
| 11 | #include "Resources.h" |
| 12 | #include "SampleCode.h" |
| 13 | #include "SkBlurMaskFilter.h" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkColorPriv.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkPath.h" |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 17 | #include "SkRandom.h" |
| 18 | #include "SkStream.h" |
| 19 | #include "SkTime.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 20 | #include "SkClipOpPriv.h" |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 21 | |
| 22 | // Intended to exercise pixel snapping observed with scaled images (and |
| 23 | // with non-scaled images, but for a different reason): Bug 1145 |
| 24 | |
| 25 | class IdentityScaleView : public SampleView { |
| 26 | public: |
| 27 | IdentityScaleView(const char imageFilename[]) { |
| 28 | SkString resourcePath = GetResourcePath(imageFilename); |
msarett | d15750c | 2016-03-18 15:48:49 -0700 | [diff] [blame] | 29 | if (!decode_file(resourcePath.c_str(), &fBM)) { |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 30 | fBM.allocN32Pixels(1, 1); |
| 31 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | protected: |
| 36 | SkBitmap fBM; |
| 37 | |
| 38 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | bool onQuery(SkEvent* evt) override { |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 40 | if (SampleCode::TitleQ(*evt)) { |
| 41 | SampleCode::TitleR(evt, "IdentityScale"); |
| 42 | return true; |
| 43 | } |
| 44 | return this->INHERITED::onQuery(evt); |
| 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDrawContent(SkCanvas* canvas) override { |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 48 | |
| 49 | SkPaint paint; |
| 50 | |
| 51 | paint.setAntiAlias(true); |
| 52 | paint.setTextSize(48); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 53 | paint.setFilterQuality(kHigh_SkFilterQuality); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 54 | |
| 55 | SkTime::DateTime time; |
| 56 | SkTime::GetDateTime(&time); |
| 57 | |
| 58 | bool use_scale = (time.fSecond % 2 == 1); |
| 59 | const char *text; |
| 60 | |
| 61 | canvas->save(); |
| 62 | if (use_scale) { |
| 63 | text = "Scaled = 1"; |
| 64 | } else { |
| 65 | |
| 66 | SkRect r = { 100, 100, 356, 356 }; |
| 67 | SkPath clipPath; |
| 68 | clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 69 | canvas->clipPath(clipPath, kIntersect_SkClipOp, true); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 70 | text = "Scaled = 0"; |
| 71 | } |
| 72 | canvas->drawBitmap( fBM, 100, 100, &paint ); |
| 73 | canvas->restore(); |
| 74 | canvas->drawText( text, strlen(text), 100, 400, paint ); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | this->inval(nullptr); |
humper | d1d3bae | 2014-08-08 12:11:33 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
| 79 | typedef SampleView INHERITED; |
| 80 | }; |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | static SkView* MyFactory() { return new IdentityScaleView("mandrill_256.png"); } |
| 85 | static SkViewRegister reg(MyFactory); |