CopySurfaceTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2016 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/SkImageInfo.h"
  8. #include "include/core/SkPoint.h"
  9. #include "include/core/SkRect.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkTypes.h"
  12. #include "include/gpu/GrContext.h"
  13. #include "include/gpu/GrTypes.h"
  14. #include "include/private/GrTypesPriv.h"
  15. #include "include/private/SkTemplates.h"
  16. #include "src/core/SkUtils.h"
  17. #include "src/gpu/GrCaps.h"
  18. #include "src/gpu/GrContextPriv.h"
  19. #include "src/gpu/GrRenderTargetContext.h"
  20. #include "src/gpu/GrSurfaceContext.h"
  21. #include "src/gpu/GrSurfaceProxy.h"
  22. #include "src/gpu/GrTextureProxy.h"
  23. #include "src/gpu/SkGr.h"
  24. #include "tests/Test.h"
  25. #include "tools/gpu/GrContextFactory.h"
  26. #include "tools/gpu/ProxyUtils.h"
  27. #include <initializer_list>
  28. #include <utility>
  29. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(CopySurface, reporter, ctxInfo) {
  30. GrContext* context = ctxInfo.grContext();
  31. static const int kW = 10;
  32. static const int kH = 10;
  33. static const size_t kRowBytes = sizeof(uint32_t) * kW;
  34. SkAutoTMalloc<uint32_t> srcPixels(kW * kH);
  35. for (int i = 0; i < kW * kH; ++i) {
  36. srcPixels.get()[i] = i;
  37. }
  38. SkAutoTMalloc<uint32_t> dstPixels(kW * kH);
  39. for (int i = 0; i < kW * kH; ++i) {
  40. dstPixels.get()[i] = ~i;
  41. }
  42. static const SkIRect kSrcRects[] {
  43. { 0, 0, kW , kH },
  44. {-1, -1, kW+1, kH+1},
  45. { 1, 1, kW-1, kH-1},
  46. { 5, 5, 6 , 6 },
  47. };
  48. static const SkIPoint kDstPoints[] {
  49. { 0 , 0 },
  50. { 1 , 1 },
  51. { kW/2, kH/4},
  52. { kW-1, kH-1},
  53. { kW , kH },
  54. { kW+1, kH+2},
  55. {-1 , -1 },
  56. };
  57. static const SkImageInfo kImageInfos[] {
  58. SkImageInfo::Make(kW, kH, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
  59. SkImageInfo::Make(kW, kH, kBGRA_8888_SkColorType, kPremul_SkAlphaType)
  60. };
  61. SkAutoTMalloc<uint32_t> read(kW * kH);
  62. for (auto sOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
  63. for (auto dOrigin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
  64. for (auto sRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
  65. for (auto dRenderable : {GrRenderable::kYes, GrRenderable::kNo}) {
  66. for (auto srcRect : kSrcRects) {
  67. for (auto dstPoint : kDstPoints) {
  68. for (auto ii: kImageInfos) {
  69. auto src = sk_gpu_test::MakeTextureProxyFromData(
  70. context, sRenderable, kW, kH, ii.colorType(),
  71. ii.alphaType(), sOrigin, srcPixels.get(), kRowBytes);
  72. auto dst = sk_gpu_test::MakeTextureProxyFromData(
  73. context, dRenderable, kW, kH, ii.colorType(),
  74. ii.alphaType(), dOrigin, dstPixels.get(), kRowBytes);
  75. // Should always work if the color type is RGBA, but may not work
  76. // for BGRA
  77. if (ii.colorType() == kRGBA_8888_SkColorType) {
  78. if (!src || !dst) {
  79. ERRORF(reporter,
  80. "Could not create surfaces for copy surface test.");
  81. continue;
  82. }
  83. } else {
  84. GrPixelConfig config =
  85. SkColorType2GrPixelConfig(kBGRA_8888_SkColorType);
  86. if (!context->priv().caps()->isConfigTexturable(config)) {
  87. continue;
  88. }
  89. if (!src || !dst) {
  90. ERRORF(reporter,
  91. "Could not create surfaces for copy surface test.");
  92. continue;
  93. }
  94. }
  95. sk_sp<GrSurfaceContext> dstContext =
  96. context->priv().makeWrappedSurfaceContext(
  97. std::move(dst),
  98. SkColorTypeToGrColorType(ii.colorType()),
  99. ii.alphaType());
  100. bool result = false;
  101. if (sOrigin == dOrigin) {
  102. result = dstContext->testCopy(src.get(), srcRect, dstPoint);
  103. } else if (dRenderable == GrRenderable::kYes) {
  104. SkASSERT(dstContext->asRenderTargetContext());
  105. result = dstContext->asRenderTargetContext()->blitTexture(
  106. src.get(), srcRect, dstPoint);
  107. }
  108. bool expectedResult = true;
  109. SkIPoint dstOffset = { dstPoint.fX - srcRect.fLeft,
  110. dstPoint.fY - srcRect.fTop };
  111. SkIRect copiedDstRect = SkIRect::MakeXYWH(dstPoint.fX,
  112. dstPoint.fY,
  113. srcRect.width(),
  114. srcRect.height());
  115. SkIRect copiedSrcRect;
  116. if (!copiedSrcRect.intersect(srcRect, SkIRect::MakeWH(kW, kH))) {
  117. expectedResult = false;
  118. } else {
  119. // If the src rect was clipped, apply same clipping to each side
  120. // of copied dst rect.
  121. copiedDstRect.fLeft += copiedSrcRect.fLeft - srcRect.fLeft;
  122. copiedDstRect.fTop += copiedSrcRect.fTop - srcRect.fTop;
  123. copiedDstRect.fRight -= copiedSrcRect.fRight - srcRect.fRight;
  124. copiedDstRect.fBottom -= copiedSrcRect.fBottom -
  125. srcRect.fBottom;
  126. }
  127. if (copiedDstRect.isEmpty() ||
  128. !copiedDstRect.intersect(SkIRect::MakeWH(kW, kH))) {
  129. expectedResult = false;
  130. }
  131. if (sOrigin != dOrigin && dRenderable == GrRenderable::kNo) {
  132. expectedResult = false;
  133. }
  134. // To make the copied src rect correct we would apply any dst
  135. // clipping back to the src rect, but we don't use it again so
  136. // don't bother.
  137. if (expectedResult != result) {
  138. ERRORF(reporter, "Expected return value %d from copySurface, "
  139. "got %d.", expectedResult, result);
  140. continue;
  141. }
  142. if (!expectedResult || !result) {
  143. continue;
  144. }
  145. sk_memset32(read.get(), 0, kW * kH);
  146. if (!dstContext->readPixels(ii, read.get(), kRowBytes, {0, 0})) {
  147. ERRORF(reporter, "Error calling readPixels");
  148. continue;
  149. }
  150. bool abort = false;
  151. // Validate that pixels inside copiedDstRect received the correct
  152. // value from src and that those outside were not modified.
  153. for (int y = 0; y < kH && !abort; ++y) {
  154. for (int x = 0; x < kW; ++x) {
  155. uint32_t r = read.get()[y * kW + x];
  156. if (copiedDstRect.contains(x, y)) {
  157. int sx = x - dstOffset.fX;
  158. int sy = y - dstOffset.fY;
  159. uint32_t s = srcPixels.get()[sy * kW + sx];
  160. if (s != r) {
  161. ERRORF(reporter, "Expected dst %d,%d to contain "
  162. "0x%08x copied from src location %d,%d. Got "
  163. "0x%08x", x, y, s, sx, sy, r);
  164. abort = true;
  165. break;
  166. }
  167. } else {
  168. uint32_t d = dstPixels.get()[y * kW + x];
  169. if (d != r) {
  170. ERRORF(reporter, "Expected dst %d,%d to be "
  171. "unmodified (0x%08x). Got 0x%08x",
  172. x, y, d, r);
  173. abort = true;
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }