VkWrapTests.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // This is a GPU-backend specific test. It relies on static intializers to work
  8. #include "include/core/SkTypes.h"
  9. #if defined(SK_VULKAN)
  10. #include "include/gpu/vk/GrVkVulkan.h"
  11. #include "include/gpu/GrBackendSurface.h"
  12. #include "include/gpu/GrRenderTarget.h"
  13. #include "include/gpu/GrTexture.h"
  14. #include "src/gpu/GrContextPriv.h"
  15. #include "tools/gpu/GrContextFactory.h"
  16. #include "include/gpu/vk/GrVkTypes.h"
  17. #include "src/gpu/vk/GrVkCaps.h"
  18. #include "src/gpu/vk/GrVkGpu.h"
  19. #include "src/gpu/vk/GrVkMemory.h"
  20. #include "tests/Test.h"
  21. using sk_gpu_test::GrContextFactory;
  22. const int kW = 1024;
  23. const int kH = 1024;
  24. const GrPixelConfig kPixelConfig = kRGBA_8888_GrPixelConfig;
  25. const SkColorType kColorType = SkColorType::kRGBA_8888_SkColorType;
  26. const GrColorType kGrColorType = GrColorType::kRGBA_8888;
  27. void wrap_tex_test(skiatest::Reporter* reporter, GrContext* context) {
  28. GrGpu* gpu = context->priv().getGpu();
  29. GrBackendTexture origBackendTex = context->createBackendTexture(kW, kH,
  30. kColorType,
  31. SkColors::kTransparent,
  32. GrMipMapped::kNo,
  33. GrRenderable::kNo,
  34. GrProtected::kNo);
  35. GrVkImageInfo imageInfo;
  36. SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
  37. sk_sp<GrTexture> tex = gpu->wrapBackendTexture(origBackendTex, kGrColorType,
  38. kBorrow_GrWrapOwnership, GrWrapCacheable::kNo,
  39. kRead_GrIOType);
  40. REPORTER_ASSERT(reporter, tex);
  41. // image is null
  42. {
  43. GrVkImageInfo backendCopy = imageInfo;
  44. backendCopy.fImage = VK_NULL_HANDLE;
  45. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  46. backendTex.setPixelConfig(kPixelConfig);
  47. tex = gpu->wrapBackendTexture(backendTex, kGrColorType, kBorrow_GrWrapOwnership,
  48. GrWrapCacheable::kNo, kRead_GrIOType);
  49. REPORTER_ASSERT(reporter, !tex);
  50. tex = gpu->wrapBackendTexture(backendTex, kGrColorType, kAdopt_GrWrapOwnership,
  51. GrWrapCacheable::kNo, kRead_GrIOType);
  52. REPORTER_ASSERT(reporter, !tex);
  53. }
  54. // alloc is null
  55. {
  56. GrVkImageInfo backendCopy = imageInfo;
  57. backendCopy.fAlloc = GrVkAlloc();
  58. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  59. backendTex.setPixelConfig(kPixelConfig);
  60. tex = gpu->wrapBackendTexture(backendTex, kGrColorType, kBorrow_GrWrapOwnership,
  61. GrWrapCacheable::kNo, kRead_GrIOType);
  62. REPORTER_ASSERT(reporter, tex);
  63. tex = gpu->wrapBackendTexture(backendTex, kGrColorType, kAdopt_GrWrapOwnership,
  64. GrWrapCacheable::kNo, kRead_GrIOType);
  65. REPORTER_ASSERT(reporter, !tex);
  66. }
  67. // check adopt creation
  68. {
  69. GrVkImageInfo backendCopy = imageInfo;
  70. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  71. backendTex.setPixelConfig(kPixelConfig);
  72. tex = gpu->wrapBackendTexture(backendTex, kGrColorType, kAdopt_GrWrapOwnership,
  73. GrWrapCacheable::kNo, kRead_GrIOType);
  74. REPORTER_ASSERT(reporter, tex);
  75. }
  76. }
  77. void wrap_rt_test(skiatest::Reporter* reporter, GrContext* context) {
  78. GrGpu* gpu = context->priv().getGpu();
  79. GrBackendTexture origBackendTex = context->createBackendTexture(kW, kH,
  80. kColorType,
  81. SkColors::kTransparent,
  82. GrMipMapped::kNo,
  83. GrRenderable::kYes,
  84. GrProtected::kNo);
  85. GrVkImageInfo imageInfo;
  86. SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
  87. GrBackendRenderTarget origBackendRT(kW, kH, 1, 0, imageInfo);
  88. origBackendRT.setPixelConfig(kPixelConfig);
  89. sk_sp<GrRenderTarget> rt = gpu->wrapBackendRenderTarget(origBackendRT, kGrColorType);
  90. REPORTER_ASSERT(reporter, rt);
  91. // image is null
  92. {
  93. GrVkImageInfo backendCopy = imageInfo;
  94. backendCopy.fImage = VK_NULL_HANDLE;
  95. GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
  96. backendRT.setPixelConfig(kPixelConfig);
  97. rt = gpu->wrapBackendRenderTarget(backendRT, kGrColorType);
  98. REPORTER_ASSERT(reporter, !rt);
  99. }
  100. // alloc is null
  101. {
  102. GrVkImageInfo backendCopy = imageInfo;
  103. backendCopy.fAlloc = GrVkAlloc();
  104. // can wrap null alloc
  105. GrBackendRenderTarget backendRT(kW, kH, 1, 0, backendCopy);
  106. backendRT.setPixelConfig(kPixelConfig);
  107. rt = gpu->wrapBackendRenderTarget(backendRT, kGrColorType);
  108. REPORTER_ASSERT(reporter, rt);
  109. }
  110. // When we wrapBackendRenderTarget it is always borrowed, so we must make sure to free the
  111. // resource when we're done.
  112. context->deleteBackendTexture(origBackendTex);
  113. }
  114. void wrap_trt_test(skiatest::Reporter* reporter, GrContext* context) {
  115. GrGpu* gpu = context->priv().getGpu();
  116. GrBackendTexture origBackendTex = context->createBackendTexture(kW, kH,
  117. kColorType,
  118. SkColors::kTransparent,
  119. GrMipMapped::kNo,
  120. GrRenderable::kYes,
  121. GrProtected::kNo);
  122. GrVkImageInfo imageInfo;
  123. SkAssertResult(origBackendTex.getVkImageInfo(&imageInfo));
  124. sk_sp<GrTexture> tex = gpu->wrapRenderableBackendTexture(
  125. origBackendTex, 1, kGrColorType, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
  126. REPORTER_ASSERT(reporter, tex);
  127. // image is null
  128. {
  129. GrVkImageInfo backendCopy = imageInfo;
  130. backendCopy.fImage = VK_NULL_HANDLE;
  131. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  132. backendTex.setPixelConfig(kPixelConfig);
  133. tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kGrColorType,
  134. kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
  135. REPORTER_ASSERT(reporter, !tex);
  136. tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kGrColorType,
  137. kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
  138. REPORTER_ASSERT(reporter, !tex);
  139. }
  140. // alloc is null
  141. {
  142. GrVkImageInfo backendCopy = imageInfo;
  143. backendCopy.fAlloc = GrVkAlloc();
  144. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  145. backendTex.setPixelConfig(kPixelConfig);
  146. tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kGrColorType,
  147. kBorrow_GrWrapOwnership, GrWrapCacheable::kNo);
  148. REPORTER_ASSERT(reporter, tex);
  149. tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kGrColorType,
  150. kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
  151. REPORTER_ASSERT(reporter, !tex);
  152. }
  153. // check adopt creation
  154. {
  155. GrVkImageInfo backendCopy = imageInfo;
  156. GrBackendTexture backendTex = GrBackendTexture(kW, kH, backendCopy);
  157. backendTex.setPixelConfig(kPixelConfig);
  158. tex = gpu->wrapRenderableBackendTexture(backendTex, 1, kGrColorType,
  159. kAdopt_GrWrapOwnership, GrWrapCacheable::kNo);
  160. REPORTER_ASSERT(reporter, tex);
  161. }
  162. }
  163. DEF_GPUTEST_FOR_VULKAN_CONTEXT(VkWrapTests, reporter, ctxInfo) {
  164. wrap_tex_test(reporter, ctxInfo.grContext());
  165. wrap_rt_test(reporter, ctxInfo.grContext());
  166. wrap_trt_test(reporter, ctxInfo.grContext());
  167. }
  168. #endif