GrUploadPixelsTests.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // This is a GPU-backend specific test. It relies on static intializers to work
  8. #include "include/core/SkTypes.h"
  9. #include "src/gpu/GrContextPriv.h"
  10. #include "src/gpu/GrSurfaceProxy.h"
  11. #include "src/gpu/SkGr.h"
  12. #include "tests/Test.h"
  13. #include "tests/TestUtils.h"
  14. #include "tools/gpu/GrContextFactory.h"
  15. #include "tools/gpu/ProxyUtils.h"
  16. using sk_gpu_test::GrContextFactory;
  17. void basic_texture_test(skiatest::Reporter* reporter, GrContext* context, SkColorType ct,
  18. GrRenderable renderable) {
  19. const int kWidth = 16;
  20. const int kHeight = 16;
  21. SkAutoTMalloc<GrColor> srcBuffer(kWidth*kHeight);
  22. SkAutoTMalloc<GrColor> dstBuffer(kWidth*kHeight);
  23. fill_pixel_data(kWidth, kHeight, srcBuffer.get());
  24. auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderable, kWidth, kHeight, ct,
  25. kPremul_SkAlphaType,
  26. kTopLeft_GrSurfaceOrigin, srcBuffer, 0);
  27. REPORTER_ASSERT(reporter, proxy);
  28. if (proxy) {
  29. sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
  30. proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
  31. SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
  32. bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
  33. REPORTER_ASSERT(reporter, result);
  34. REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
  35. dstBuffer,
  36. kWidth,
  37. kHeight));
  38. dstInfo = SkImageInfo::Make(10, 2, ct, kPremul_SkAlphaType);
  39. result = sContext->writePixels(dstInfo, srcBuffer, 0, {2, 10});
  40. REPORTER_ASSERT(reporter, result);
  41. memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
  42. result = sContext->readPixels(dstInfo, dstBuffer, 0, {2, 10});
  43. REPORTER_ASSERT(reporter, result);
  44. REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
  45. dstBuffer,
  46. 10,
  47. 2));
  48. }
  49. proxy = sk_gpu_test::MakeTextureProxyFromData(context, renderable, kWidth, kHeight, ct,
  50. kPremul_SkAlphaType, kBottomLeft_GrSurfaceOrigin,
  51. srcBuffer, 0);
  52. REPORTER_ASSERT(reporter, proxy);
  53. if (proxy) {
  54. sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
  55. proxy, SkColorTypeToGrColorType(ct), kPremul_SkAlphaType);
  56. SkImageInfo dstInfo = SkImageInfo::Make(kWidth, kHeight, ct, kPremul_SkAlphaType);
  57. bool result = sContext->readPixels(dstInfo, dstBuffer, 0, {0, 0});
  58. REPORTER_ASSERT(reporter, result);
  59. REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
  60. dstBuffer,
  61. kWidth,
  62. kHeight));
  63. dstInfo = SkImageInfo::Make(4, 5, ct, kPremul_SkAlphaType);
  64. result = sContext->writePixels(dstInfo, srcBuffer, 0, {5, 4});
  65. REPORTER_ASSERT(reporter, result);
  66. memset(dstBuffer, 0, kWidth*kHeight*sizeof(GrColor));
  67. result = sContext->readPixels(dstInfo, dstBuffer, 0, {5, 4});
  68. REPORTER_ASSERT(reporter, result);
  69. REPORTER_ASSERT(reporter, does_full_buffer_contain_correct_color(srcBuffer,
  70. dstBuffer,
  71. 4,
  72. 5));
  73. }
  74. }
  75. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrUploadPixelsTests, reporter, ctxInfo) {
  76. // RGBA
  77. basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kNo);
  78. basic_texture_test(reporter, ctxInfo.grContext(), kRGBA_8888_SkColorType, GrRenderable::kYes);
  79. // BGRA
  80. basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kNo);
  81. basic_texture_test(reporter, ctxInfo.grContext(), kBGRA_8888_SkColorType, GrRenderable::kYes);
  82. }