CachedDecodingPixelRefTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2013 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/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkImage.h"
  11. #include "include/core/SkImageGenerator.h"
  12. #include "include/core/SkImageInfo.h"
  13. #include "include/core/SkRefCnt.h"
  14. #include "include/core/SkTypes.h"
  15. #include "include/private/SkColorData.h"
  16. #include "src/core/SkMakeUnique.h"
  17. #include "src/core/SkUtils.h"
  18. #include "tests/Test.h"
  19. #include "tools/ToolUtils.h"
  20. #include <utility>
  21. class TestImageGenerator : public SkImageGenerator {
  22. public:
  23. enum TestType {
  24. kFailGetPixels_TestType,
  25. kSucceedGetPixels_TestType,
  26. kLast_TestType = kSucceedGetPixels_TestType
  27. };
  28. static int Width() { return 10; }
  29. static int Height() { return 10; }
  30. // value choosen so that there is no loss when converting to to RGB565 and back
  31. static SkColor Color() { return ToolUtils::color_to_565(0xffaabbcc); }
  32. static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
  33. TestImageGenerator(TestType type, skiatest::Reporter* reporter,
  34. SkColorType colorType = kN32_SkColorType)
  35. : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
  36. SkASSERT((fType <= kLast_TestType) && (fType >= 0));
  37. }
  38. ~TestImageGenerator() override {}
  39. protected:
  40. static SkImageInfo GetMyInfo(SkColorType colorType) {
  41. return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
  42. colorType, kOpaque_SkAlphaType);
  43. }
  44. bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
  45. const Options& options) override {
  46. REPORTER_ASSERT(fReporter, pixels != nullptr);
  47. REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
  48. if (fType != kSucceedGetPixels_TestType) {
  49. return false;
  50. }
  51. if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
  52. return false;
  53. }
  54. char* bytePtr = static_cast<char*>(pixels);
  55. switch (info.colorType()) {
  56. case kN32_SkColorType:
  57. for (int y = 0; y < info.height(); ++y) {
  58. sk_memset32((uint32_t*)bytePtr,
  59. TestImageGenerator::PMColor(), info.width());
  60. bytePtr += rowBytes;
  61. }
  62. break;
  63. case kRGB_565_SkColorType:
  64. for (int y = 0; y < info.height(); ++y) {
  65. sk_memset16((uint16_t*)bytePtr,
  66. SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
  67. bytePtr += rowBytes;
  68. }
  69. break;
  70. default:
  71. return false;
  72. }
  73. return true;
  74. }
  75. private:
  76. const TestType fType;
  77. skiatest::Reporter* const fReporter;
  78. typedef SkImageGenerator INHERITED;
  79. };
  80. ////////////////////////////////////////////////////////////////////////////////
  81. DEF_TEST(Image_NewFromGenerator, r) {
  82. const TestImageGenerator::TestType testTypes[] = {
  83. TestImageGenerator::kFailGetPixels_TestType,
  84. TestImageGenerator::kSucceedGetPixels_TestType,
  85. };
  86. const SkColorType testColorTypes[] = {
  87. kN32_SkColorType,
  88. kRGB_565_SkColorType
  89. };
  90. for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
  91. TestImageGenerator::TestType test = testTypes[i];
  92. for (const SkColorType testColorType : testColorTypes) {
  93. auto gen = skstd::make_unique<TestImageGenerator>(test, r, testColorType);
  94. sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
  95. if (nullptr == image) {
  96. ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
  97. SK_SIZE_T_SPECIFIER "]", i);
  98. continue;
  99. }
  100. REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
  101. REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
  102. REPORTER_ASSERT(r, image->isLazyGenerated());
  103. SkBitmap bitmap;
  104. bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
  105. SkCanvas canvas(bitmap);
  106. const SkColor kDefaultColor = 0xffabcdef;
  107. canvas.clear(kDefaultColor);
  108. canvas.drawImage(image, 0, 0, nullptr);
  109. if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
  110. REPORTER_ASSERT(
  111. r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
  112. }
  113. else {
  114. REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
  115. }
  116. }
  117. }
  118. }