GrTestingBackendTextureUploadTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2018 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 "src/core/SkAutoPixmapStorage.h"
  8. #include "src/gpu/GrContextPriv.h"
  9. #include "src/gpu/GrGpu.h"
  10. #include "src/gpu/GrSurfaceContext.h"
  11. #include "src/gpu/SkGr.h"
  12. #include "tests/Test.h"
  13. #include "tests/TestUtils.h"
  14. void testing_only_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
  15. GrRenderable renderable, bool doDataUpload, GrMipMapped mipMapped) {
  16. const int kWidth = 16;
  17. const int kHeight = 16;
  18. SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
  19. SkAutoPixmapStorage expectedPixels, actualPixels;
  20. expectedPixels.alloc(ii);
  21. actualPixels.alloc(ii);
  22. const GrCaps* caps = context->priv().caps();
  23. GrColorType grCT = SkColorTypeToGrColorType(ct);
  24. GrBackendFormat backendFormat = caps->getBackendFormatFromColorType(grCT);
  25. if (!caps->isFormatTexturable(grCT, backendFormat)) {
  26. return;
  27. }
  28. GrBackendTexture backendTex;
  29. if (doDataUpload) {
  30. SkASSERT(GrMipMapped::kNo == mipMapped);
  31. fill_pixel_data(kWidth, kHeight, expectedPixels.writable_addr32(0, 0));
  32. backendTex = context->priv().createBackendTexture(&expectedPixels, 1,
  33. renderable, GrProtected::kNo);
  34. } else {
  35. backendTex = context->createBackendTexture(kWidth, kHeight, ct, SkColors::kTransparent,
  36. mipMapped, renderable, GrProtected::kNo);
  37. size_t allocSize = SkAutoPixmapStorage::AllocSize(ii, nullptr);
  38. // createBackendTexture will fill the texture with 0's if no data is provided, so
  39. // we set the expected result likewise.
  40. memset(expectedPixels.writable_addr32(0, 0), 0, allocSize);
  41. }
  42. if (!backendTex.isValid()) {
  43. return;
  44. }
  45. // skbug.com/9165
  46. auto supportedRead =
  47. caps->supportedReadPixelsColorType(grCT, backendTex.getBackendFormat(), grCT);
  48. if (supportedRead.fColorType != grCT || supportedRead.fSwizzle != GrSwizzle("rgba")) {
  49. return;
  50. }
  51. sk_sp<GrTextureProxy> wrappedProxy;
  52. if (GrRenderable::kYes == renderable) {
  53. wrappedProxy = context->priv().proxyProvider()->wrapRenderableBackendTexture(
  54. backendTex, kTopLeft_GrSurfaceOrigin, 1, grCT, kAdopt_GrWrapOwnership,
  55. GrWrapCacheable::kNo);
  56. } else {
  57. wrappedProxy = context->priv().proxyProvider()->wrapBackendTexture(
  58. backendTex, grCT, kTopLeft_GrSurfaceOrigin, kAdopt_GrWrapOwnership,
  59. GrWrapCacheable::kNo, GrIOType::kRW_GrIOType);
  60. }
  61. REPORTER_ASSERT(reporter, wrappedProxy);
  62. auto surfaceContext = context->priv().makeWrappedSurfaceContext(std::move(wrappedProxy), grCT,
  63. kPremul_SkAlphaType);
  64. REPORTER_ASSERT(reporter, surfaceContext);
  65. bool result = surfaceContext->readPixels({grCT, kPremul_SkAlphaType, nullptr, kWidth, kHeight},
  66. actualPixels.writable_addr(), actualPixels.rowBytes(),
  67. {0, 0}, context);
  68. REPORTER_ASSERT(reporter, result);
  69. REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(expectedPixels.addr32(),
  70. actualPixels.addr32(),
  71. kWidth, kHeight));
  72. }
  73. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrTestingBackendTextureUploadTest, reporter, ctxInfo) {
  74. for (auto colorType: { kRGBA_8888_SkColorType, kBGRA_8888_SkColorType }) {
  75. for (auto renderable: { GrRenderable::kYes, GrRenderable::kNo }) {
  76. for (bool doDataUpload: {true, false}) {
  77. testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
  78. renderable, doDataUpload, GrMipMapped::kNo);
  79. if (!doDataUpload) {
  80. testing_only_texture_test(reporter, ctxInfo.grContext(), colorType,
  81. renderable, doDataUpload, GrMipMapped::kYes);
  82. }
  83. }
  84. }
  85. }
  86. }