blob: 5d2d6b9e1e96372fdfbd7bfb9db7bf2542a9405f [file] [log] [blame]
halcanary@google.combc55eec2013-12-10 18:33:07 +00001/*
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
halcanary51761d12016-09-08 08:58:37 -07008#include "SkDiscardableMemoryPool.h"
halcanary@google.combc55eec2013-12-10 18:33:07 +00009
10#include "Test.h"
halcanary@google.combc55eec2013-12-10 18:33:07 +000011
halcanary51761d12016-09-08 08:58:37 -070012namespace {
13constexpr char kTestString[] = "HELLO, WORLD!";
14constexpr size_t kTestStringLength = sizeof(kTestString);
15}
16
17static void test_dm(skiatest::Reporter* reporter,
18 SkDiscardableMemory* dm,
19 bool assertRelock) {
20 REPORTER_ASSERT(reporter, dm);
21 if (!dm) {
halcanary@google.combc55eec2013-12-10 18:33:07 +000022 return;
23 }
24 void* ptr = dm->data();
halcanary51761d12016-09-08 08:58:37 -070025 REPORTER_ASSERT(reporter, ptr);
26 if (!ptr) {
27 return;
28 }
29 memcpy(ptr, kTestString, sizeof(kTestString));
halcanary@google.combc55eec2013-12-10 18:33:07 +000030 dm->unlock();
halcanary51761d12016-09-08 08:58:37 -070031 bool relockSuccess = dm->lock();
32 if (assertRelock) {
33 REPORTER_ASSERT(reporter, relockSuccess);
34 }
35 if (!relockSuccess) {
halcanary@google.combc55eec2013-12-10 18:33:07 +000036 return;
37 }
38 ptr = dm->data();
halcanary51761d12016-09-08 08:58:37 -070039 REPORTER_ASSERT(reporter, ptr);
40 if (!ptr) {
41 return;
42 }
43 REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength));
halcanary@google.combc55eec2013-12-10 18:33:07 +000044 dm->unlock();
45}
halcanary51761d12016-09-08 08:58:37 -070046
47DEF_TEST(DiscardableMemory_global, reporter) {
48 std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStringLength));
49 // lock() test is allowed to fail, since other threads could be
50 // using global pool.
51 test_dm(reporter, dm.get(), false);
52}
53
54DEF_TEST(DiscardableMemory_nonglobal, reporter) {
55 std::unique_ptr<SkDiscardableMemoryPool> pool(
56 SkDiscardableMemoryPool::Create(1024, /* mutex = */ nullptr));
57 std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
58 test_dm(reporter, dm.get(), true);
59}
60