MallocPixelRefTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/SkData.h"
  8. #include "include/core/SkMallocPixelRef.h"
  9. #include "src/core/SkAutoMalloc.h"
  10. #include "tests/Test.h"
  11. static void delete_uint8_proc(void* ptr, void*) {
  12. delete[] static_cast<uint8_t*>(ptr);
  13. }
  14. static void set_to_one_proc(void*, void* context) {
  15. *(static_cast<int*>(context)) = 1;
  16. }
  17. /**
  18. * This test contains basic sanity checks concerning SkMallocPixelRef.
  19. */
  20. DEF_TEST(MallocPixelRef, reporter) {
  21. REPORTER_ASSERT(reporter, true);
  22. SkImageInfo info = SkImageInfo::MakeN32Premul(10, 13);
  23. {
  24. sk_sp<SkPixelRef> pr(
  25. SkMallocPixelRef::MakeAllocate(info, info.minRowBytes() - 1));
  26. // rowbytes too small.
  27. REPORTER_ASSERT(reporter, nullptr == pr.get());
  28. }
  29. {
  30. size_t rowBytes = info.minRowBytes() - 1;
  31. size_t size = info.computeByteSize(rowBytes);
  32. sk_sp<SkData> data(SkData::MakeUninitialized(size));
  33. sk_sp<SkPixelRef> pr(
  34. SkMallocPixelRef::MakeWithData(info, rowBytes, data));
  35. // rowbytes too small.
  36. REPORTER_ASSERT(reporter, nullptr == pr.get());
  37. }
  38. {
  39. size_t rowBytes = info.minRowBytes() + 2;
  40. size_t size = info.computeByteSize(rowBytes) - 1;
  41. sk_sp<SkData> data(SkData::MakeUninitialized(size));
  42. sk_sp<SkPixelRef> pr(
  43. SkMallocPixelRef::MakeWithData(info, rowBytes, data));
  44. // data too small.
  45. REPORTER_ASSERT(reporter, nullptr == pr.get());
  46. }
  47. size_t rowBytes = info.minRowBytes() + 7;
  48. size_t size = info.computeByteSize(rowBytes) + 9;
  49. {
  50. SkAutoMalloc memory(size);
  51. sk_sp<SkPixelRef> pr(
  52. SkMallocPixelRef::MakeDirect(info, memory.get(), rowBytes));
  53. REPORTER_ASSERT(reporter, pr.get() != nullptr);
  54. REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
  55. }
  56. {
  57. sk_sp<SkPixelRef> pr(
  58. SkMallocPixelRef::MakeAllocate(info, rowBytes));
  59. REPORTER_ASSERT(reporter, pr.get() != nullptr);
  60. REPORTER_ASSERT(reporter, pr->pixels());
  61. }
  62. {
  63. void* addr = static_cast<void*>(new uint8_t[size]);
  64. sk_sp<SkPixelRef> pr(
  65. SkMallocPixelRef::MakeWithProc(info, rowBytes, addr, delete_uint8_proc, nullptr));
  66. REPORTER_ASSERT(reporter, pr.get() != nullptr);
  67. REPORTER_ASSERT(reporter, addr == pr->pixels());
  68. }
  69. {
  70. int x = 0;
  71. SkAutoMalloc memory(size);
  72. sk_sp<SkPixelRef> pr(
  73. SkMallocPixelRef::MakeWithProc(info, rowBytes,
  74. memory.get(), set_to_one_proc,
  75. static_cast<void*>(&x)));
  76. REPORTER_ASSERT(reporter, pr.get() != nullptr);
  77. REPORTER_ASSERT(reporter, memory.get() == pr->pixels());
  78. REPORTER_ASSERT(reporter, 0 == x);
  79. pr.reset(nullptr);
  80. // make sure that set_to_one_proc was called.
  81. REPORTER_ASSERT(reporter, 1 == x);
  82. }
  83. {
  84. int x = 0;
  85. SkAutoMalloc memory(size);
  86. sk_sp<SkPixelRef> pr(
  87. SkMallocPixelRef::MakeWithProc(SkImageInfo::MakeN32Premul(-1, -1), rowBytes,
  88. memory.get(), set_to_one_proc,
  89. static_cast<void*>(&x)));
  90. REPORTER_ASSERT(reporter, pr.get() == nullptr);
  91. // make sure that set_to_one_proc was called.
  92. REPORTER_ASSERT(reporter, 1 == x);
  93. }
  94. {
  95. void* addr = static_cast<void*>(new uint8_t[size]);
  96. REPORTER_ASSERT(reporter, addr != nullptr);
  97. sk_sp<SkPixelRef> pr(
  98. SkMallocPixelRef::MakeWithProc(info, rowBytes, addr,
  99. delete_uint8_proc, nullptr));
  100. REPORTER_ASSERT(reporter, addr == pr->pixels());
  101. }
  102. {
  103. sk_sp<SkData> data(SkData::MakeUninitialized(size));
  104. SkData* dataPtr = data.get();
  105. REPORTER_ASSERT(reporter, dataPtr->unique());
  106. sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeWithData(info, rowBytes, data);
  107. REPORTER_ASSERT(reporter, !(dataPtr->unique()));
  108. data.reset(nullptr);
  109. REPORTER_ASSERT(reporter, dataPtr->unique());
  110. REPORTER_ASSERT(reporter, dataPtr->data() == pr->pixels());
  111. }
  112. }