SampleTextureDomain.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2011 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/SkSurface.h"
  9. #include "include/effects/SkBlurMaskFilter.h"
  10. #include "samplecode/Sample.h"
  11. #include "src/core/SkBlurMask.h"
  12. static SkBitmap make_bitmap() {
  13. SkBitmap bm;
  14. bm.allocN32Pixels(5, 5);
  15. for (int y = 0; y < bm.height(); y++) {
  16. uint32_t* p = bm.getAddr32(0, y);
  17. for (int x = 0; x < bm.width(); x++) {
  18. p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
  19. }
  20. }
  21. return bm;
  22. }
  23. class TextureDomainView : public Sample {
  24. SkBitmap fBM;
  25. public:
  26. TextureDomainView(){
  27. fBM = make_bitmap();
  28. }
  29. protected:
  30. virtual SkString name() { return SkString("Texture Domain"); }
  31. virtual void onDrawContent(SkCanvas* canvas) {
  32. SkRect srcRect;
  33. SkRect dstRect;
  34. SkPaint paint;
  35. paint.setFilterQuality(kLow_SkFilterQuality);
  36. // Test that bitmap draws from malloc-backed bitmaps respect
  37. // the constrained texture domain.
  38. srcRect.setXYWH(1, 1, 3, 3);
  39. dstRect.setXYWH(5, 5, 305, 305);
  40. canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
  41. // Test that bitmap draws across separate devices also respect
  42. // the constrainted texture domain.
  43. // Note: GPU-backed bitmaps follow a different rendering path
  44. // when copying from one GPU device to another.
  45. SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
  46. auto surface(canvas->makeSurface(info));
  47. srcRect.setXYWH(1, 1, 3, 3);
  48. dstRect.setXYWH(1, 1, 3, 3);
  49. surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
  50. SkCanvas::kStrict_SrcRectConstraint);
  51. sk_sp<SkImage> image(surface->makeImageSnapshot());
  52. srcRect.setXYWH(1, 1, 3, 3);
  53. dstRect.setXYWH(405, 5, 305, 305);
  54. canvas->drawImageRect(image, srcRect, dstRect, &paint);
  55. // Test that bitmap blurring using a subrect
  56. // renders correctly
  57. srcRect.setXYWH(1, 1, 3, 3);
  58. dstRect.setXYWH(5, 405, 305, 305);
  59. paint.setMaskFilter(SkMaskFilter::MakeBlur(
  60. kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false));
  61. canvas->drawImageRect(image, srcRect, dstRect, &paint);
  62. // Blur and a rotation + nullptr src rect
  63. // This should not trigger the texture domain code
  64. // but it will test a code path in SkGpuDevice::drawBitmap
  65. // that handles blurs with rects transformed to non-
  66. // orthogonal rects. It also tests the nullptr src rect handling
  67. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
  68. SkBlurMask::ConvertRadiusToSigma(5)));
  69. dstRect.setXYWH(-150, -150, 300, 300);
  70. canvas->translate(550, 550);
  71. canvas->rotate(45);
  72. canvas->drawBitmapRect(fBM, dstRect, &paint);
  73. }
  74. private:
  75. typedef Sample INHERITED;
  76. };
  77. //////////////////////////////////////////////////////////////////////////////
  78. DEF_SAMPLE( return new TextureDomainView(); )