DiscardableMemoryTest.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/SkRefCnt.h"
  8. #include "src/core/SkDiscardableMemory.h"
  9. #include "src/lazy/SkDiscardableMemoryPool.h"
  10. #include "tests/Test.h"
  11. #include <cstring>
  12. #include <memory>
  13. namespace {
  14. constexpr char kTestString[] = "HELLO, WORLD!";
  15. constexpr size_t kTestStringLength = sizeof(kTestString);
  16. }
  17. static void test_dm(skiatest::Reporter* reporter,
  18. SkDiscardableMemory* dm,
  19. bool assertRelock) {
  20. REPORTER_ASSERT(reporter, dm);
  21. if (!dm) {
  22. return;
  23. }
  24. void* ptr = dm->data();
  25. REPORTER_ASSERT(reporter, ptr);
  26. if (!ptr) {
  27. return;
  28. }
  29. memcpy(ptr, kTestString, sizeof(kTestString));
  30. dm->unlock();
  31. bool relockSuccess = dm->lock();
  32. if (assertRelock) {
  33. REPORTER_ASSERT(reporter, relockSuccess);
  34. }
  35. if (!relockSuccess) {
  36. return;
  37. }
  38. ptr = dm->data();
  39. REPORTER_ASSERT(reporter, ptr);
  40. if (!ptr) {
  41. return;
  42. }
  43. REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength));
  44. dm->unlock();
  45. }
  46. DEF_TEST(DiscardableMemory_global, reporter) {
  47. std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStringLength));
  48. // lock() test is allowed to fail, since other threads could be
  49. // using global pool.
  50. test_dm(reporter, dm.get(), false);
  51. }
  52. DEF_TEST(DiscardableMemory_nonglobal, reporter) {
  53. sk_sp<SkDiscardableMemoryPool> pool(
  54. SkDiscardableMemoryPool::Make(1024));
  55. std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
  56. test_dm(reporter, dm.get(), true);
  57. }