BlendTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2015 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/SkBitmap.h"
  8. #include "include/core/SkBlendMode.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkColorSpace.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkRefCnt.h"
  17. #include "include/core/SkSurface.h"
  18. #include "include/core/SkTypes.h"
  19. #include "include/gpu/GrBackendSurface.h"
  20. #include "include/gpu/GrContext.h"
  21. #include "include/gpu/GrTexture.h"
  22. #include "include/gpu/GrTypes.h"
  23. #include "include/private/GrTypesPriv.h"
  24. #include "src/gpu/GrContextPriv.h"
  25. #include "src/gpu/GrResourceProvider.h"
  26. #include "tests/Test.h"
  27. #include "tools/gpu/GrContextFactory.h"
  28. #include <initializer_list>
  29. #include <vector>
  30. struct Results { int diffs, diffs_0x00, diffs_0xff, diffs_by_1; };
  31. static bool acceptable(const Results& r) {
  32. #if 0
  33. SkDebugf("%d diffs, %d at 0x00, %d at 0xff, %d off by 1, all out of 65536\n",
  34. r.diffs, r.diffs_0x00, r.diffs_0xff, r.diffs_by_1);
  35. #endif
  36. return r.diffs_by_1 == r.diffs // never off by more than 1
  37. && r.diffs_0x00 == 0 // transparent must stay transparent
  38. && r.diffs_0xff == 0; // opaque must stay opaque
  39. }
  40. template <typename Fn>
  41. static Results test(Fn&& multiply) {
  42. Results r = { 0,0,0,0 };
  43. for (int x = 0; x < 256; x++) {
  44. for (int y = 0; y < 256; y++) {
  45. int p = multiply(x, y),
  46. ideal = (x*y+127)/255;
  47. if (p != ideal) {
  48. r.diffs++;
  49. if (x == 0x00 || y == 0x00) { r.diffs_0x00++; }
  50. if (x == 0xff || y == 0xff) { r.diffs_0xff++; }
  51. if (SkTAbs(ideal - p) == 1) { r.diffs_by_1++; }
  52. }
  53. }}
  54. return r;
  55. }
  56. DEF_TEST(Blend_byte_multiply, r) {
  57. // These are all temptingly close but fundamentally broken.
  58. int (*broken[])(int, int) = {
  59. [](int x, int y) { return (x*y)>>8; },
  60. [](int x, int y) { return (x*y+128)>>8; },
  61. [](int x, int y) { y += y>>7; return (x*y)>>8; },
  62. };
  63. for (auto multiply : broken) { REPORTER_ASSERT(r, !acceptable(test(multiply))); }
  64. // These are fine to use, but not perfect.
  65. int (*fine[])(int, int) = {
  66. [](int x, int y) { return (x*y+x)>>8; },
  67. [](int x, int y) { return (x*y+y)>>8; },
  68. [](int x, int y) { return (x*y+255)>>8; },
  69. [](int x, int y) { y += y>>7; return (x*y+128)>>8; },
  70. };
  71. for (auto multiply : fine) { REPORTER_ASSERT(r, acceptable(test(multiply))); }
  72. // These are pefect.
  73. int (*perfect[])(int, int) = {
  74. [](int x, int y) { return (x*y+127)/255; }, // Duh.
  75. [](int x, int y) { int p = (x*y+128); return (p+(p>>8))>>8; },
  76. [](int x, int y) { return ((x*y+128)*257)>>16; },
  77. };
  78. for (auto multiply : perfect) { REPORTER_ASSERT(r, test(multiply).diffs == 0); }
  79. }
  80. namespace {
  81. static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
  82. GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
  83. GrPixelConfig config, GrSurfaceOrigin origin,
  84. sk_sp<GrTexture>* backingSurface) {
  85. GrSurfaceDesc backingDesc;
  86. backingDesc.fWidth = width;
  87. backingDesc.fHeight = height;
  88. backingDesc.fConfig = config;
  89. auto resourceProvider = context->priv().resourceProvider();
  90. *backingSurface = resourceProvider->createTexture(backingDesc, GrRenderable::kYes, sampleCnt,
  91. SkBudgeted::kNo, GrProtected::kNo,
  92. GrResourceProvider::Flags::kNoPendingIO);
  93. if (!(*backingSurface)) {
  94. return nullptr;
  95. }
  96. GrBackendTexture backendTex = (*backingSurface)->getBackendTexture();
  97. sk_sp<SkSurface> surface =
  98. SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
  99. sampleCnt, colorType, nullptr, nullptr);
  100. return surface;
  101. }
  102. }
  103. // Tests blending to a surface with no texture available.
  104. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo) {
  105. GrContext* context = ctxInfo.grContext();
  106. const int kWidth = 10;
  107. const int kHeight = 10;
  108. const GrPixelConfig kConfig = kRGBA_8888_GrPixelConfig;
  109. const SkColorType kColorType = kRGBA_8888_SkColorType;
  110. // Build our test cases:
  111. struct RectAndSamplePoint {
  112. SkRect rect;
  113. SkIPoint outPoint;
  114. SkIPoint inPoint;
  115. } allRectsAndPoints[3] = {
  116. {SkRect::MakeXYWH(0, 0, 5, 5), SkIPoint::Make(7, 7), SkIPoint::Make(2, 2)},
  117. {SkRect::MakeXYWH(2, 2, 5, 5), SkIPoint::Make(1, 1), SkIPoint::Make(4, 4)},
  118. {SkRect::MakeXYWH(5, 5, 5, 5), SkIPoint::Make(2, 2), SkIPoint::Make(7, 7)},
  119. };
  120. struct TestCase {
  121. RectAndSamplePoint fRectAndPoints;
  122. SkRect fClip;
  123. int fSampleCnt;
  124. GrSurfaceOrigin fOrigin;
  125. };
  126. std::vector<TestCase> testCases;
  127. for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
  128. for (int sampleCnt : {1, 4}) {
  129. for (auto rectAndPoints : allRectsAndPoints) {
  130. for (auto clip : {SkRect::MakeXYWH(0, 0, 10, 10), SkRect::MakeXYWH(1, 1, 8, 8)}) {
  131. testCases.push_back({rectAndPoints, clip, sampleCnt, origin});
  132. }
  133. }
  134. }
  135. }
  136. // Run each test case:
  137. for (auto testCase : testCases) {
  138. int sampleCnt = testCase.fSampleCnt;
  139. SkRect paintRect = testCase.fRectAndPoints.rect;
  140. SkIPoint outPoint = testCase.fRectAndPoints.outPoint;
  141. SkIPoint inPoint = testCase.fRectAndPoints.inPoint;
  142. GrSurfaceOrigin origin = testCase.fOrigin;
  143. sk_sp<GrTexture> backingSurface;
  144. // BGRA forces a framebuffer blit on ES2.
  145. sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
  146. context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
  147. if (!surface && sampleCnt > 1) {
  148. // Some platforms don't support MSAA.
  149. continue;
  150. }
  151. REPORTER_ASSERT(reporter, !!surface);
  152. // Fill our canvas with 0xFFFF80
  153. SkCanvas* canvas = surface->getCanvas();
  154. canvas->clipRect(testCase.fClip, false);
  155. SkPaint black_paint;
  156. black_paint.setColor(SkColorSetRGB(0xFF, 0xFF, 0x80));
  157. canvas->drawRect(SkRect::MakeXYWH(0, 0, kWidth, kHeight), black_paint);
  158. // Blend 2x2 pixels at 5,5 with 0x80FFFF. Use multiply blend mode as this will trigger
  159. // a copy of the destination.
  160. SkPaint white_paint;
  161. white_paint.setColor(SkColorSetRGB(0x80, 0xFF, 0xFF));
  162. white_paint.setBlendMode(SkBlendMode::kMultiply);
  163. canvas->drawRect(paintRect, white_paint);
  164. // Read the result into a bitmap.
  165. SkBitmap bitmap;
  166. REPORTER_ASSERT(reporter, bitmap.tryAllocPixels(SkImageInfo::Make(
  167. kWidth, kHeight, kColorType, kPremul_SkAlphaType)));
  168. REPORTER_ASSERT(
  169. reporter,
  170. surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), 0, 0));
  171. // Check the in/out pixels.
  172. REPORTER_ASSERT(reporter, bitmap.getColor(outPoint.x(), outPoint.y()) ==
  173. SkColorSetRGB(0xFF, 0xFF, 0x80));
  174. REPORTER_ASSERT(reporter, bitmap.getColor(inPoint.x(), inPoint.y()) ==
  175. SkColorSetRGB(0x80, 0xFF, 0x80));
  176. // Clean up - surface depends on backingSurface and must be released first.
  177. surface.reset();
  178. backingSurface.reset();
  179. }
  180. }