Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
CachedDataTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 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
13#include "tests/Test.h"
14
15#include <cstring>
16
18
23
28
30 int refcnt, CachedState cacheState, LockedState lockedState) {
31 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
32 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
33 REPORTER_ASSERT(reporter, data->testing_only_isLocked() == (lockedState == kLocked));
34}
35
37 if (pool) {
38 SkDiscardableMemory* dm = pool->create(size);
39 // the pool "can" return null, but it shouldn't in these controlled conditions
41 return new SkCachedData(size, dm);
42 } else {
43 return new SkCachedData(sk_malloc_throw(size), size);
44 }
45}
46
47// returns with the data locked by client and cache
49 size_t size, SkDiscardableMemoryPool* pool) {
51
52 memset(data->writable_data(), 0x80, size); // just to use writable_data()
53
55
56 data->ref();
58 data->unref();
60
61 data->attachToCacheAndRef();
63
64 data->unref();
66
67 data->ref();
69
70 return data;
71}
72
73/*
74 * SkCachedData behaves differently (regarding its locked/unlocked state) depending on
75 * when it is in the cache or not. Being in the cache is signaled by calling attachToCacheAndRef()
76 * instead of ref(). (and balanced by detachFromCacheAndUnref).
77 *
78 * Thus, among other things, we test the end-of-life behavior when the client is the last owner
79 * and when the cache is.
80 */
81DEF_TEST(CachedData, reporter) {
83
84 for (int useDiscardable = 0; useDiscardable <= 1; ++useDiscardable) {
85 const size_t size = 100;
86
87 // test with client as last owner
88 SkCachedData* data = test_locking(reporter, size, useDiscardable ? pool.get() : nullptr);
90 data->detachFromCacheAndUnref();
92 data->unref();
93
94 // test with cache as last owner
95 data = test_locking(reporter, size, useDiscardable ? pool.get() : nullptr);
97 data->unref();
99 data->detachFromCacheAndUnref();
100 }
101}
AutoreleasePool pool
static void check_data(skiatest::Reporter *reporter, SkCachedData *data, int refcnt, CachedState cacheState, LockedState lockedState)
CachedState
@ kInCache
@ kNotInCache
LockedState
@ kUnlocked
@ kLocked
static SkCachedData * test_locking(skiatest::Reporter *reporter, size_t size, SkDiscardableMemoryPool *pool)
reporter
static std::unique_ptr< uint32_t[]> make_data()
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
static sk_sp< SkDiscardableMemoryPool > Make(size_t size)
virtual void * data()=0