halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkRefCnt.h" |
| 9 | #include "src/core/SkDiscardableMemory.h" |
| 10 | #include "src/lazy/SkDiscardableMemoryPool.h" |
| 11 | #include "tests/Test.h" |
Ben Wagner | b5f2897 | 2018-04-17 17:42:08 -0400 | [diff] [blame] | 12 | |
Ben Wagner | d90cd3b | 2018-05-22 10:48:08 -0400 | [diff] [blame] | 13 | #include <cstring> |
| 14 | #include <memory> |
| 15 | |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | constexpr char kTestString[] = "HELLO, WORLD!"; |
| 18 | constexpr size_t kTestStringLength = sizeof(kTestString); |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 19 | } // namespace |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 20 | |
| 21 | static void test_dm(skiatest::Reporter* reporter, |
| 22 | SkDiscardableMemory* dm, |
| 23 | bool assertRelock) { |
| 24 | REPORTER_ASSERT(reporter, dm); |
| 25 | if (!dm) { |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | void* ptr = dm->data(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 29 | REPORTER_ASSERT(reporter, ptr); |
| 30 | if (!ptr) { |
| 31 | return; |
| 32 | } |
| 33 | memcpy(ptr, kTestString, sizeof(kTestString)); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 34 | dm->unlock(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 35 | bool relockSuccess = dm->lock(); |
| 36 | if (assertRelock) { |
| 37 | REPORTER_ASSERT(reporter, relockSuccess); |
| 38 | } |
| 39 | if (!relockSuccess) { |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | ptr = dm->data(); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 43 | REPORTER_ASSERT(reporter, ptr); |
| 44 | if (!ptr) { |
| 45 | return; |
| 46 | } |
| 47 | REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength)); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 48 | dm->unlock(); |
| 49 | } |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 50 | |
| 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 | |
| 58 | DEF_TEST(DiscardableMemory_nonglobal, reporter) { |
Hal Canary | a294be2 | 2017-04-19 13:17:59 -0400 | [diff] [blame] | 59 | sk_sp<SkDiscardableMemoryPool> pool( |
Hal Canary | 788c3c4 | 2017-04-25 08:58:57 -0400 | [diff] [blame] | 60 | SkDiscardableMemoryPool::Make(1024)); |
halcanary | 51761d1 | 2016-09-08 08:58:37 -0700 | [diff] [blame] | 61 | std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength)); |
| 62 | test_dm(reporter, dm.get(), true); |
| 63 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 64 | |