SampleIdentityScale.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkColorPriv.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkPath.h"
  11. #include "include/core/SkStream.h"
  12. #include "include/core/SkTime.h"
  13. #include "include/effects/SkBlurMaskFilter.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. // Intended to exercise pixel snapping observed with scaled images (and
  20. // with non-scaled images, but for a different reason): Bug 1145
  21. class IdentityScaleView : public Sample {
  22. public:
  23. IdentityScaleView(const char imageFilename[]) {
  24. if (!DecodeDataToBitmap(GetResourceAsData(imageFilename), &fBM)) {
  25. fBM.allocN32Pixels(1, 1);
  26. *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
  27. }
  28. }
  29. protected:
  30. SkBitmap fBM;
  31. SkString name() override { return SkString("IdentityScale"); }
  32. void onDrawContent(SkCanvas* canvas) override {
  33. SkFont font(nullptr, 48);
  34. SkPaint paint;
  35. paint.setAntiAlias(true);
  36. paint.setFilterQuality(kHigh_SkFilterQuality);
  37. SkTime::DateTime time;
  38. SkTime::GetDateTime(&time);
  39. bool use_scale = (time.fSecond % 2 == 1);
  40. const char *text;
  41. canvas->save();
  42. if (use_scale) {
  43. text = "Scaled = 1";
  44. } else {
  45. SkRect r = { 100, 100, 356, 356 };
  46. SkPath clipPath;
  47. clipPath.addRoundRect(r, SkIntToScalar(5), SkIntToScalar(5));
  48. canvas->clipPath(clipPath, kIntersect_SkClipOp, true);
  49. text = "Scaled = 0";
  50. }
  51. canvas->drawBitmap( fBM, 100, 100, &paint );
  52. canvas->restore();
  53. canvas->drawString(text, 100, 400, font, paint);
  54. }
  55. private:
  56. typedef Sample INHERITED;
  57. };
  58. //////////////////////////////////////////////////////////////////////////////
  59. DEF_SAMPLE( return new IdentityScaleView("images/mandrill_256.png"); )