SwizzlerTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "include/core/SkSwizzle.h"
  8. #include "include/private/SkImageInfoPriv.h"
  9. #include "src/codec/SkSwizzler.h"
  10. #include "src/core/SkOpts.h"
  11. #include "tests/Test.h"
  12. static void check_fill(skiatest::Reporter* r,
  13. const SkImageInfo& imageInfo,
  14. uint32_t startRow,
  15. uint32_t endRow,
  16. size_t rowBytes,
  17. uint32_t offset) {
  18. // Calculate the total size of the image in bytes. Use the smallest possible size.
  19. // The offset value tells us to adjust the pointer from the memory we allocate in order
  20. // to test on different memory alignments. If offset is nonzero, we need to increase the
  21. // size of the memory we allocate in order to make sure that we have enough. We are
  22. // still allocating the smallest possible size.
  23. const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset;
  24. // Create fake image data where every byte has a value of 0
  25. std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]);
  26. memset(storage.get(), 0, totalBytes);
  27. // Adjust the pointer in order to test on different memory alignments
  28. uint8_t* imageData = storage.get() + offset;
  29. uint8_t* imageStart = imageData + rowBytes * startRow;
  30. const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
  31. SkSampler::Fill(fillInfo, imageStart, rowBytes, SkCodec::kNo_ZeroInitialized);
  32. // Ensure that the pixels are filled properly
  33. // The bots should catch any memory corruption
  34. uint8_t* indexPtr = imageData + startRow * rowBytes;
  35. uint8_t* grayPtr = indexPtr;
  36. uint32_t* colorPtr = (uint32_t*) indexPtr;
  37. uint16_t* color565Ptr = (uint16_t*) indexPtr;
  38. for (uint32_t y = startRow; y <= endRow; y++) {
  39. for (int32_t x = 0; x < imageInfo.width(); x++) {
  40. switch (imageInfo.colorType()) {
  41. case kN32_SkColorType:
  42. REPORTER_ASSERT(r, 0 == colorPtr[x]);
  43. break;
  44. case kGray_8_SkColorType:
  45. REPORTER_ASSERT(r, 0 == grayPtr[x]);
  46. break;
  47. case kRGB_565_SkColorType:
  48. REPORTER_ASSERT(r, 0 == color565Ptr[x]);
  49. break;
  50. default:
  51. REPORTER_ASSERT(r, false);
  52. break;
  53. }
  54. }
  55. indexPtr += rowBytes;
  56. colorPtr = (uint32_t*) indexPtr;
  57. }
  58. }
  59. // Test Fill() with different combinations of dimensions, alignment, and padding
  60. DEF_TEST(SwizzlerFill, r) {
  61. // Test on an invalid width and representative widths
  62. const uint32_t widths[] = { 0, 10, 50 };
  63. // In order to call Fill(), there must be at least one row to fill
  64. // Test on the smallest possible height and representative heights
  65. const uint32_t heights[] = { 1, 5, 10 };
  66. // Test on interesting possibilities for row padding
  67. const uint32_t paddings[] = { 0, 4 };
  68. // Iterate over test dimensions
  69. for (uint32_t width : widths) {
  70. for (uint32_t height : heights) {
  71. // Create image info objects
  72. const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType);
  73. const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType);
  74. const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType);
  75. for (uint32_t padding : paddings) {
  76. // Calculate row bytes
  77. const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width
  78. + padding;
  79. const size_t indexRowBytes = width + padding;
  80. const size_t grayRowBytes = indexRowBytes;
  81. const size_t color565RowBytes =
  82. SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding;
  83. // If there is padding, we can invent an offset to change the memory alignment
  84. for (uint32_t offset = 0; offset <= padding; offset += 4) {
  85. // Test all possible start rows with all possible end rows
  86. for (uint32_t startRow = 0; startRow < height; startRow++) {
  87. for (uint32_t endRow = startRow; endRow < height; endRow++) {
  88. // Test fill with each color type
  89. check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset);
  90. check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset);
  91. check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. DEF_TEST(SwizzleOpts, r) {
  100. uint32_t dst, src;
  101. // forall c, c*255 == c, c*0 == 0
  102. for (int c = 0; c <= 255; c++) {
  103. src = (255<<24) | c;
  104. SkOpts::RGBA_to_rgbA(&dst, &src, 1);
  105. REPORTER_ASSERT(r, dst == src);
  106. SkOpts::RGBA_to_bgrA(&dst, &src, 1);
  107. REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16)));
  108. src = (0<<24) | c;
  109. SkOpts::RGBA_to_rgbA(&dst, &src, 1);
  110. REPORTER_ASSERT(r, dst == 0);
  111. SkOpts::RGBA_to_bgrA(&dst, &src, 1);
  112. REPORTER_ASSERT(r, dst == 0);
  113. }
  114. // check a totally arbitrary color
  115. src = 0xFACEB004;
  116. SkOpts::RGBA_to_rgbA(&dst, &src, 1);
  117. REPORTER_ASSERT(r, dst == 0xFACAAD04);
  118. // swap red and blue
  119. SkOpts::RGBA_to_BGRA(&dst, &src, 1);
  120. REPORTER_ASSERT(r, dst == 0xFA04B0CE);
  121. // all together now
  122. SkOpts::RGBA_to_bgrA(&dst, &src, 1);
  123. REPORTER_ASSERT(r, dst == 0xFA04ADCA);
  124. }
  125. DEF_TEST(PublicSwizzleOpts, r) {
  126. uint32_t dst, src;
  127. // check a totally arbitrary color
  128. src = 0xFACEB004;
  129. SkSwapRB(&dst, &src, 1);
  130. REPORTER_ASSERT(r, dst == 0xFA04B0CE);
  131. }