PackedConfigsTextureTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /*
  8. * This is a straightforward test of using packed pixel configs (4444, 565).
  9. * This test will make sure that these RGBA_4444 and RGB_565 are always supported
  10. * as valid texturing configs.
  11. */
  12. #include "tests/Test.h"
  13. #include "include/gpu/GrContext.h"
  14. #include "src/gpu/GrContextPriv.h"
  15. #include "src/gpu/GrProxyProvider.h"
  16. #include "src/gpu/GrTextureProxy.h"
  17. #include "tools/gpu/ProxyUtils.h"
  18. static const int DEV_W = 10, DEV_H = 10;
  19. static const uint8_t TOL = 0x4;
  20. static void check_component(skiatest::Reporter* reporter, uint8_t control, uint8_t test) {
  21. uint8_t diff = 0;
  22. if (control >= test) {
  23. diff = control - test;
  24. } else {
  25. diff = test - control;
  26. }
  27. REPORTER_ASSERT(reporter, diff < TOL);
  28. }
  29. static uint8_t expand_value(uint8_t original, int sigBits) {
  30. SkASSERT(sigBits >= 4);
  31. uint8_t inSigBitShift = 8 - sigBits;
  32. uint8_t duplBitShift = sigBits - inSigBitShift;
  33. return (original << inSigBitShift) + (original >> duplBitShift);
  34. }
  35. static void check_4444(skiatest::Reporter* reporter,
  36. const SkTDArray<uint16_t>& controlData,
  37. const SkTDArray<uint32_t>& readBuffer) {
  38. for (int j = 0; j < DEV_H; ++j) {
  39. for (int i = 0; i < DEV_W; ++i) {
  40. uint16_t control = controlData[i + j * DEV_H];
  41. uint32_t test = readBuffer[i + j * DEV_H];
  42. // Test alpha component
  43. uint8_t ctrlComp = expand_value(control & 0xF, 4);
  44. uint8_t testComp = GrColorUnpackA(test);
  45. check_component(reporter, ctrlComp, testComp);
  46. // Test blue component
  47. ctrlComp = expand_value((control >> 4) & 0xF, 4);
  48. testComp = GrColorUnpackB(test);
  49. check_component(reporter, ctrlComp, testComp);
  50. // Test green component
  51. ctrlComp = expand_value((control >> 8) & 0xF, 4);
  52. testComp = GrColorUnpackG(test);
  53. check_component(reporter, ctrlComp, testComp);
  54. // Test red component
  55. ctrlComp = expand_value((control >> 12) & 0xF, 4);
  56. testComp = GrColorUnpackR(test);
  57. check_component(reporter, ctrlComp, testComp);
  58. }
  59. }
  60. }
  61. static void check_565(skiatest::Reporter* reporter,
  62. const SkTDArray<uint16_t>& controlData,
  63. const SkTDArray<GrColor>& readBuffer) {
  64. for (int j = 0; j < DEV_H; ++j) {
  65. for (int i = 0; i < DEV_W; ++i) {
  66. uint16_t control = controlData[i + j * DEV_H];
  67. GrColor test = readBuffer[i + j * DEV_H];
  68. // Test blue component (5 bit control)
  69. uint8_t ctrlComp = expand_value(control & 0x1F, 5);
  70. uint8_t testComp = GrColorUnpackB(test);
  71. check_component(reporter, ctrlComp, testComp);
  72. // Test green component (6 bit control)
  73. ctrlComp = expand_value((control >> 5) & 0x3F, 6);
  74. testComp = GrColorUnpackG(test);
  75. check_component(reporter, ctrlComp, testComp);
  76. // Test red component (5 bit control)
  77. ctrlComp = expand_value((control >> 11) & 0x1F, 5);
  78. testComp = GrColorUnpackR(test);
  79. check_component(reporter, ctrlComp, testComp);
  80. }
  81. }
  82. }
  83. static void run_test(skiatest::Reporter* reporter, GrContext* context, int arraySize,
  84. SkColorType colorType) {
  85. SkTDArray<uint16_t> controlPixelData;
  86. // We will read back into an 8888 buffer since 565/4444 read backs aren't supported
  87. SkTDArray<GrColor> readBuffer;
  88. controlPixelData.setCount(arraySize);
  89. readBuffer.setCount(arraySize);
  90. for (int i = 0; i < arraySize; i += 2) {
  91. controlPixelData[i] = 0xF00F;
  92. controlPixelData[i + 1] = 0xA62F;
  93. }
  94. const SkImageInfo dstInfo =
  95. SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  96. for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
  97. auto proxy = sk_gpu_test::MakeTextureProxyFromData(context, GrRenderable::kNo, DEV_W, DEV_H,
  98. colorType, kPremul_SkAlphaType, origin,
  99. controlPixelData.begin(), 0);
  100. SkASSERT(proxy);
  101. sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
  102. std::move(proxy), SkColorTypeToGrColorType(colorType), kPremul_SkAlphaType);
  103. if (!sContext->readPixels(dstInfo, readBuffer.begin(), 0, {0, 0})) {
  104. // We only require this to succeed if the format is renderable.
  105. REPORTER_ASSERT(reporter, !context->colorTypeSupportedAsSurface(colorType));
  106. return;
  107. }
  108. if (kARGB_4444_SkColorType == colorType) {
  109. check_4444(reporter, controlPixelData, readBuffer);
  110. } else {
  111. SkASSERT(kRGB_565_SkColorType == colorType);
  112. check_565(reporter, controlPixelData, readBuffer);
  113. }
  114. }
  115. }
  116. static const int CONTROL_ARRAY_SIZE = DEV_W * DEV_H;
  117. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGBA4444TextureTest, reporter, ctxInfo) {
  118. if (ctxInfo.grContext()->colorTypeSupportedAsImage(kARGB_4444_SkColorType)) {
  119. run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kARGB_4444_SkColorType);
  120. }
  121. }
  122. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RGB565TextureTest, reporter, ctxInfo) {
  123. if (ctxInfo.grContext()->colorTypeSupportedAsImage(kRGB_565_SkColorType)) {
  124. run_test(reporter, ctxInfo.grContext(), CONTROL_ARRAY_SIZE, kRGB_565_SkColorType);
  125. }
  126. }