perspimages.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2018 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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFilterQuality.h"
  10. #include "include/core/SkImage.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkSize.h"
  16. #include "include/core/SkString.h"
  17. #include "include/private/SkTArray.h"
  18. #include "include/private/SkTDArray.h"
  19. #include "tools/Resources.h"
  20. #include <initializer_list>
  21. static sk_sp<SkImage> make_image1() { return GetResourceAsImage("images/mandrill_128.png"); }
  22. static sk_sp<SkImage> make_image2() {
  23. return GetResourceAsImage("images/brickwork-texture.jpg")->makeSubset({0, 0, 128, 128});
  24. }
  25. namespace skiagm {
  26. class PerspImages : public GM {
  27. public:
  28. PerspImages() = default;
  29. protected:
  30. SkString onShortName() override { return SkString("persp_images"); }
  31. SkISize onISize() override { return SkISize::Make(1150, 1280); }
  32. void onOnceBeforeDraw() override {
  33. fImages.push_back(make_image1());
  34. fImages.push_back(make_image2());
  35. }
  36. void onDraw(SkCanvas* canvas) override {
  37. SkTDArray<SkMatrix> matrices;
  38. matrices.push()->setAll(1.f, 0.f, 0.f,
  39. 0.f, 1.f, 0.f,
  40. 0.f, 0.005f, 1.f);
  41. matrices.push()->setAll(1.f, 0.f, 0.f,
  42. 0.f, 1.f, 0.f,
  43. 0.007f, -0.005f, 1.f);
  44. matrices[1].preSkew(0.2f, -0.1f);
  45. matrices[1].preRotate(-65.f);
  46. matrices[1].preScale(1.2f, .8f);
  47. matrices[1].postTranslate(0.f, 60.f);
  48. SkPaint paint;
  49. int n = 0;
  50. SkRect bounds = SkRect::MakeEmpty();
  51. for (const auto& img : fImages) {
  52. SkRect imgB = SkRect::MakeWH(img->width(), img->height());
  53. for (const auto& m : matrices) {
  54. SkRect temp;
  55. m.mapRect(&temp, imgB);
  56. bounds.join(temp);
  57. }
  58. }
  59. canvas->translate(-bounds.fLeft + 10.f, -bounds.fTop + 10.f);
  60. canvas->save();
  61. enum class DrawType {
  62. kDrawImage,
  63. kDrawImageRectStrict,
  64. kDrawImageRectFast,
  65. };
  66. for (auto type :
  67. {DrawType::kDrawImage, DrawType::kDrawImageRectStrict, DrawType::kDrawImageRectFast}) {
  68. for (const auto& m : matrices) {
  69. for (auto aa : {false, true}) {
  70. paint.setAntiAlias(aa);
  71. for (auto filter : {kNone_SkFilterQuality, kLow_SkFilterQuality,
  72. kMedium_SkFilterQuality, kHigh_SkFilterQuality}) {
  73. for (const auto& img : fImages) {
  74. paint.setFilterQuality(filter);
  75. canvas->save();
  76. canvas->concat(m);
  77. SkRect src = {img->width() / 4.f, img->height() / 4.f,
  78. 3.f * img->width() / 4.f, 3.f * img->height() / 4};
  79. SkRect dst = {0, 0,
  80. 3.f / 4.f * img->width(), 3.f / 4.f * img->height()};
  81. switch (type) {
  82. case DrawType::kDrawImage:
  83. canvas->drawImage(img, 0, 0, &paint);
  84. break;
  85. case DrawType::kDrawImageRectStrict:
  86. canvas->drawImageRect(img, src, dst, &paint,
  87. SkCanvas::kStrict_SrcRectConstraint);
  88. break;
  89. case DrawType::kDrawImageRectFast:
  90. canvas->drawImageRect(img, src, dst, &paint,
  91. SkCanvas::kFast_SrcRectConstraint);
  92. break;
  93. }
  94. canvas->restore();
  95. ++n;
  96. if (n < 8) {
  97. canvas->translate(bounds.width() + 10.f, 0);
  98. } else {
  99. canvas->restore();
  100. canvas->translate(0, bounds.height() + 10.f);
  101. canvas->save();
  102. n = 0;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. canvas->restore();
  110. }
  111. private:
  112. static constexpr int kNumImages = 4;
  113. SkTArray<sk_sp<SkImage>> fImages;
  114. typedef GM INHERITED;
  115. };
  116. //////////////////////////////////////////////////////////////////////////////
  117. DEF_GM(return new PerspImages();)
  118. } // namespace skiagm