PixelRefTest.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "tests/Test.h"
  8. #include "include/core/SkMallocPixelRef.h"
  9. #include "include/core/SkPixelRef.h"
  10. static void decrement_counter_proc(void* pixels, void* ctx) {
  11. int* counter = (int*)ctx;
  12. *counter -= 1;
  13. }
  14. static void test_dont_leak_install(skiatest::Reporter* reporter) {
  15. bool success;
  16. int release_counter;
  17. SkImageInfo info;
  18. SkBitmap bm;
  19. info = SkImageInfo::MakeN32Premul(0, 0);
  20. release_counter = 1;
  21. success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
  22. REPORTER_ASSERT(reporter, true == success);
  23. bm.reset();
  24. REPORTER_ASSERT(reporter, 0 == release_counter);
  25. info = SkImageInfo::MakeN32Premul(10, 10);
  26. release_counter = 1;
  27. success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
  28. REPORTER_ASSERT(reporter, true == success);
  29. bm.reset();
  30. REPORTER_ASSERT(reporter, 0 == release_counter);
  31. info = SkImageInfo::MakeN32Premul(-10, -10);
  32. release_counter = 1;
  33. success = bm.installPixels(info, nullptr, 0, decrement_counter_proc, &release_counter);
  34. REPORTER_ASSERT(reporter, false == success);
  35. bm.reset();
  36. REPORTER_ASSERT(reporter, 0 == release_counter);
  37. }
  38. static void test_install(skiatest::Reporter* reporter) {
  39. bool success;
  40. SkImageInfo info = SkImageInfo::MakeN32Premul(0, 0);
  41. SkBitmap bm;
  42. // make sure we don't assert on an empty install
  43. success = bm.installPixels(info, nullptr, 0);
  44. REPORTER_ASSERT(reporter, success);
  45. // no pixels should be the same as setInfo()
  46. info = SkImageInfo::MakeN32Premul(10, 10);
  47. success = bm.installPixels(info, nullptr, 0);
  48. REPORTER_ASSERT(reporter, success);
  49. }
  50. class TestListener : public SkPixelRef::GenIDChangeListener {
  51. public:
  52. explicit TestListener(int* ptr) : fPtr(ptr) {}
  53. void onChange() override { (*fPtr)++; }
  54. private:
  55. int* fPtr;
  56. };
  57. DEF_TEST(PixelRef_GenIDChange, r) {
  58. SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
  59. sk_sp<SkPixelRef> pixelRef = SkMallocPixelRef::MakeAllocate(info, 0);
  60. // Register a listener.
  61. int count = 0;
  62. pixelRef->addGenIDChangeListener(new TestListener(&count));
  63. REPORTER_ASSERT(r, 0 == count);
  64. // No one has looked at our pixelRef's generation ID, so invalidating it doesn't make sense.
  65. // (An SkPixelRef tree falls in the forest but there's nobody around to hear it. Do we care?)
  66. pixelRef->notifyPixelsChanged();
  67. REPORTER_ASSERT(r, 0 == count);
  68. // Force the generation ID to be calculated.
  69. REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
  70. // Our listener was dropped in the first call to notifyPixelsChanged(). This is a no-op.
  71. pixelRef->notifyPixelsChanged();
  72. REPORTER_ASSERT(r, 0 == count);
  73. // Force the generation ID to be recalculated, then add a listener.
  74. REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
  75. pixelRef->addGenIDChangeListener(new TestListener(&count));
  76. pixelRef->notifyPixelsChanged();
  77. REPORTER_ASSERT(r, 1 == count);
  78. // Quick check that nullptr is safe.
  79. REPORTER_ASSERT(r, 0 != pixelRef->getGenerationID());
  80. pixelRef->addGenIDChangeListener(nullptr);
  81. pixelRef->notifyPixelsChanged();
  82. test_install(r);
  83. test_dont_leak_install(r);
  84. }