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 "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" 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" 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 23 class IdentityScaleView : public Sample { 24 public: IdentityScaleView(const char imageFilename[])25 IdentityScaleView(const char imageFilename[]) { 26 if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) { 27 fBM.allocN32Pixels(1, 1); 28 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 29 } 30 } 31 32 protected: 33 SkBitmap fBM; 34 name()35 SkString name() override { return SkString("IdentityScale"); } 36 onDrawContent(SkCanvas * canvas)37 void onDrawContent(SkCanvas* canvas) override { 38 39 SkFont font(nullptr, 48); 40 SkPaint paint; 41 42 paint.setAntiAlias(true); 43 paint.setFilterQuality(kHigh_SkFilterQuality); 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; 58 clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5)); 59 canvas->clipPath(clipPath, kIntersect_SkClipOp, true); 60 text = "Scaled = 0"; 61 } 62 canvas->drawBitmap( fBM, 100, 100, &paint ); 63 canvas->restore(); 64 canvas->drawString(text, 100, 400, font, paint); 65 } 66 67 private: 68 typedef Sample INHERITED; 69 }; 70 71 ////////////////////////////////////////////////////////////////////////////// 72 73 DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); ) 74