• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "SkDiscardableMemoryPool.h"
9 
10 #include "Test.h"
11 
12 namespace {
13 constexpr char kTestString[] = "HELLO, WORLD!";
14 constexpr size_t kTestStringLength = sizeof(kTestString);
15 }
16 
test_dm(skiatest::Reporter * reporter,SkDiscardableMemory * dm,bool assertRelock)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 DEF_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 
DEF_TEST(DiscardableMemory_nonglobal,reporter)54 DEF_TEST(DiscardableMemory_nonglobal, reporter) {
55     sk_sp<SkDiscardableMemoryPool> pool(
56         SkDiscardableMemoryPool::Make(1024));
57     std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
58     test_dm(reporter, dm.get(), true);
59 }
60 
61