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