blob: f44d4fe9cf42df52ac1bd0978bea80e7334cfb46 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkRefCnt.h"
9#include "src/core/SkDiscardableMemory.h"
10#include "src/lazy/SkDiscardableMemoryPool.h"
11#include "tests/Test.h"
Ben Wagnerb5f28972018-04-17 17:42:08 -040012
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040013#include <cstring>
14#include <memory>
15
halcanary51761d12016-09-08 08:58:37 -070016namespace {
17constexpr char kTestString[] = "HELLO, WORLD!";
18constexpr size_t kTestStringLength = sizeof(kTestString);
John Stilesa6841be2020-08-06 14:11:56 -040019} // namespace
halcanary51761d12016-09-08 08:58:37 -070020
21static void test_dm(skiatest::Reporter* reporter,
22 SkDiscardableMemory* dm,
23 bool assertRelock) {
24 REPORTER_ASSERT(reporter, dm);
25 if (!dm) {
halcanary@google.combc55eec2013-12-10 18:33:07 +000026 return;
27 }
28 void* ptr = dm->data();
halcanary51761d12016-09-08 08:58:37 -070029 REPORTER_ASSERT(reporter, ptr);
30 if (!ptr) {
31 return;
32 }
33 memcpy(ptr, kTestString, sizeof(kTestString));
halcanary@google.combc55eec2013-12-10 18:33:07 +000034 dm->unlock();
halcanary51761d12016-09-08 08:58:37 -070035 bool relockSuccess = dm->lock();
36 if (assertRelock) {
37 REPORTER_ASSERT(reporter, relockSuccess);
38 }
39 if (!relockSuccess) {
halcanary@google.combc55eec2013-12-10 18:33:07 +000040 return;
41 }
42 ptr = dm->data();
halcanary51761d12016-09-08 08:58:37 -070043 REPORTER_ASSERT(reporter, ptr);
44 if (!ptr) {
45 return;
46 }
47 REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength));
halcanary@google.combc55eec2013-12-10 18:33:07 +000048 dm->unlock();
49}
halcanary51761d12016-09-08 08:58:37 -070050
51DEF_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
54 // using global pool.
55 test_dm(reporter, dm.get(), false);
56}
57
58DEF_TEST(DiscardableMemory_nonglobal, reporter) {
Hal Canarya294be22017-04-19 13:17:59 -040059 sk_sp<SkDiscardableMemoryPool> pool(
Hal Canary788c3c42017-04-25 08:58:57 -040060 SkDiscardableMemoryPool::Make(1024));
halcanary51761d12016-09-08 08:58:37 -070061 std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
62 test_dm(reporter, dm.get(), true);
63}
Ben Wagner63fd7602017-10-09 15:45:33 -040064