Skbug6653.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "include/core/SkTypes.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkSurface.h"
  10. #include "include/gpu/GrContext.h"
  11. #include "src/gpu/GrContextPriv.h"
  12. #include "tests/Test.h"
  13. static SkBitmap read_pixels(sk_sp<SkSurface> surface, SkColor initColor) {
  14. SkBitmap bmp;
  15. bmp.allocN32Pixels(surface->width(), surface->height());
  16. bmp.eraseColor(initColor);
  17. if (!surface->readPixels(bmp, 0, 0)) {
  18. SkDebugf("readPixels failed\n");
  19. }
  20. return bmp;
  21. }
  22. static sk_sp<SkSurface> make_surface(GrContext* context) {
  23. SkImageInfo info = SkImageInfo::Make(50, 50, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  24. return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 4,
  25. kBottomLeft_GrSurfaceOrigin, nullptr);
  26. }
  27. static void test_bug_6653(GrContext* ctx, skiatest::Reporter* reporter, const char* label) {
  28. SkRect rect = SkRect::MakeWH(50, 50);
  29. SkPaint paint;
  30. paint.setColor(SK_ColorWHITE);
  31. paint.setStrokeWidth(5);
  32. paint.setStyle(SkPaint::kStroke_Style);
  33. // The one device that fails this test (Galaxy S6) does so in a flaky fashion. Trying many
  34. // times makes it more likely to fail. Also, interacting with the phone (eg swiping between
  35. // different home screens) while the test is running makes it fail close to 100%.
  36. static const int kNumIterations = 50;
  37. for (int i = 0; i < kNumIterations; ++i) {
  38. auto s0 = make_surface(ctx);
  39. if (!s0) {
  40. // MSAA may not be supported
  41. return;
  42. }
  43. auto s1 = make_surface(ctx);
  44. s1->getCanvas()->clear(SK_ColorBLACK);
  45. s1->getCanvas()->drawOval(rect, paint);
  46. SkBitmap b1 = read_pixels(s1, SK_ColorBLACK);
  47. s1 = nullptr;
  48. // The bug requires that all three of the following surfaces are cleared to the same color
  49. auto s2 = make_surface(ctx);
  50. s2->getCanvas()->clear(SK_ColorBLUE);
  51. SkBitmap b2 = read_pixels(s2, SK_ColorBLACK);
  52. s2 = nullptr;
  53. auto s3 = make_surface(ctx);
  54. s3->getCanvas()->clear(SK_ColorBLUE);
  55. SkBitmap b3 = read_pixels(s3, SK_ColorBLACK);
  56. s0->getCanvas()->drawBitmap(b3, 0, 0);
  57. s3 = nullptr;
  58. auto s4 = make_surface(ctx);
  59. s4->getCanvas()->clear(SK_ColorBLUE);
  60. s4->getCanvas()->drawOval(rect, paint);
  61. // When this fails, b4 will "succeed", but return an empty bitmap (containing just the
  62. // clear color). Regardless, b5 will contain the oval that was just drawn, so diffing the
  63. // two bitmaps tests for the failure case. Initialize the bitmaps to different colors so
  64. // that if the readPixels doesn't work, this test will always fail.
  65. SkBitmap b4 = read_pixels(s4, SK_ColorRED);
  66. SkBitmap b5 = read_pixels(s4, SK_ColorGREEN);
  67. bool match = true;
  68. for (int y = 0; y < b4.height() && match; ++y) {
  69. for (int x = 0; x < b4.width() && match; ++x) {
  70. uint32_t pixelA = *b4.getAddr32(x, y);
  71. uint32_t pixelB = *b5.getAddr32(x, y);
  72. if (pixelA != pixelB) {
  73. match = false;
  74. }
  75. }
  76. }
  77. REPORTER_ASSERT(reporter, match, label);
  78. }
  79. }
  80. // Tests that readPixels returns up-to-date results. This has failed on several GPUs,
  81. // from multiple vendors, in MSAA mode.
  82. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(skbug6653, reporter, ctxInfo) {
  83. GrContext* ctx = ctxInfo.grContext();
  84. test_bug_6653(ctx, reporter, "Default");
  85. }