2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkRefCnt.h"
9 #include "src/core/SkDiscardableMemory.h"
10 #include "src/lazy/SkDiscardableMemoryPool.h"
11 #include "tests/Test.h"
17 constexpr char kTestString[] = "HELLO, WORLD!";
18 constexpr size_t kTestStringLength = sizeof(kTestString);
21 static void test_dm(skiatest::Reporter* reporter,
22 SkDiscardableMemory* dm,
24 REPORTER_ASSERT(reporter, dm);
28 void* ptr = dm->data();
29 REPORTER_ASSERT(reporter, ptr);
33 memcpy(ptr, kTestString, sizeof(kTestString));
35 bool relockSuccess = dm->lock();
37 REPORTER_ASSERT(reporter, relockSuccess);
43 REPORTER_ASSERT(reporter, ptr);
47 REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength));
51 DEF_TEST(DiscardableMemory_global, reporter) {
52 std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStringLength));
53 // lock() test is allowed to fail, since other threads could be
55 test_dm(reporter, dm.get(), false);
58 DEF_TEST(DiscardableMemory_nonglobal, reporter) {
59 sk_sp<SkDiscardableMemoryPool> pool(
60 SkDiscardableMemoryPool::Make(1024));
61 std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
62 test_dm(reporter, dm.get(), true);