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