simple_magnification.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2017 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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkColorPriv.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkSize.h"
  20. #include "include/core/SkString.h"
  21. #include "include/core/SkSurface.h"
  22. #include "include/core/SkTypes.h"
  23. #include "include/effects/SkImageSource.h"
  24. #include "include/effects/SkMagnifierImageFilter.h"
  25. #include "include/gpu/GrTypes.h"
  26. #include <utility>
  27. class GrContext;
  28. static sk_sp<SkImage> make_image(GrContext* context, int size, GrSurfaceOrigin origin) {
  29. if (context) {
  30. SkImageInfo ii = SkImageInfo::Make(size, size, kN32_SkColorType, kPremul_SkAlphaType);
  31. sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, ii, 0,
  32. origin, nullptr));
  33. if (surf) {
  34. SkCanvas* canvas = surf->getCanvas();
  35. canvas->clear(SK_ColorRED);
  36. const struct {
  37. SkPoint fPt;
  38. SkColor fColor;
  39. } rec[] = {
  40. { { 1.5f, 1.5f }, SK_ColorGREEN },
  41. { { 2.5f, 1.5f }, SK_ColorBLUE },
  42. { { 1.5f, 2.5f }, SK_ColorCYAN },
  43. { { 2.5f, 2.5f }, SK_ColorGRAY },
  44. };
  45. SkPaint paint;
  46. for (const auto& r : rec) {
  47. paint.setColor(r.fColor);
  48. canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &r.fPt, paint);
  49. }
  50. return surf->makeImageSnapshot();
  51. }
  52. }
  53. SkBitmap bm;
  54. bm.allocN32Pixels(size, size);
  55. bm.eraseColor(SK_ColorRED);
  56. *bm.getAddr32(1, 1) = SkPackARGB32(0xFF, 0x00, 0xFF, 0x00);
  57. *bm.getAddr32(2, 1) = SkPackARGB32(0xFF, 0x00, 0x00, 0xFF);
  58. *bm.getAddr32(1, 2) = SkPackARGB32(0xFF, 0x00, 0xFF, 0xFF);
  59. *bm.getAddr32(2, 2) = SkPackARGB32(0xFF, 0x88, 0x88, 0x88);
  60. return SkImage::MakeFromBitmap(bm);
  61. }
  62. /*
  63. * This GM creates an image with a 2x2:
  64. * Green | Blue
  65. * ------------
  66. * Cyan | Gray
  67. * block of pixels in one corner of a 33x33 field. The 'srcRect' feature of the
  68. * SkMagnifierImageFilter is then used to blow it up with different inset border widths.
  69. *
  70. * In GPU-mode we wind up drawing 4 rects:
  71. *
  72. * BottomLeft origin + 1-wide inset | TopLeft origin + 1-wide inset
  73. * ----------------------------------------------------------------
  74. * BottomLeft origin + 7-wide inset | TopLeft origin + 7-wide inset
  75. *
  76. * In Raster-mode the source origin isn't used.
  77. */
  78. class SimpleMagnificationGM : public skiagm::GM {
  79. public:
  80. SimpleMagnificationGM() {
  81. this->setBGColor(0xFFCCCCCC);
  82. }
  83. protected:
  84. SkString onShortName() override {
  85. return SkString("simple-magnification");
  86. }
  87. SkISize onISize() override {
  88. return SkISize::Make(3*kPad+2*kImgSize, 3*kPad+2*kImgSize);
  89. }
  90. void draw(SkCanvas* canvas, sk_sp<SkImage> image, const SkIPoint& offset, int inset) {
  91. sk_sp<SkImageFilter> imgSrc(SkImageSource::Make(std::move(image)));
  92. SkRect srcRect = SkRect::MakeXYWH(1.0f, 1.0f, 2.0f, 2.0f);
  93. sk_sp<SkImageFilter> magFilter(SkMagnifierImageFilter::Make(srcRect, inset, imgSrc));
  94. SkPaint paint;
  95. paint.setImageFilter(std::move(magFilter));
  96. canvas->save();
  97. canvas->translate(offset.fX, offset.fY);
  98. SkRect rect = SkRect::MakeWH(kImgSize, kImgSize);
  99. canvas->drawRect(rect, paint);
  100. canvas->restore();
  101. }
  102. DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
  103. GrContext* context = canvas->getGrContext();
  104. sk_sp<SkImage> bottomLImg = make_image(context, kImgSize, kBottomLeft_GrSurfaceOrigin);
  105. sk_sp<SkImage> topLImg = make_image(context, kImgSize, kTopLeft_GrSurfaceOrigin);
  106. if (!bottomLImg || !topLImg) {
  107. *errorMsg = "Could not load images. Did you forget to set the resourcePath?";
  108. return DrawResult::kFail;
  109. }
  110. int bigOffset = 2 * kPad + kImgSize;
  111. this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, kPad), 1);
  112. this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, kPad), 1);
  113. this->draw(canvas, bottomLImg, SkIPoint::Make(kPad, bigOffset), 7);
  114. this->draw(canvas, topLImg, SkIPoint::Make(bigOffset, bigOffset), 7);
  115. return DrawResult::kOk;
  116. }
  117. private:
  118. static const int kImgSize = 33;
  119. static const int kPad = 2;
  120. typedef skiagm::GM INHERITED;
  121. };
  122. //////////////////////////////////////////////////////////////////////////////
  123. DEF_GM(return new SimpleMagnificationGM;)