2013-12-10 18:33:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "src/core/SkDiscardableMemory.h"
|
|
|
|
#include "src/lazy/SkDiscardableMemoryPool.h"
|
|
|
|
#include "tests/Test.h"
|
2018-04-17 21:42:08 +00:00
|
|
|
|
2018-05-22 14:48:08 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-09-08 15:58:37 +00:00
|
|
|
namespace {
|
|
|
|
constexpr char kTestString[] = "HELLO, WORLD!";
|
|
|
|
constexpr size_t kTestStringLength = sizeof(kTestString);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_dm(skiatest::Reporter* reporter,
|
|
|
|
SkDiscardableMemory* dm,
|
|
|
|
bool assertRelock) {
|
|
|
|
REPORTER_ASSERT(reporter, dm);
|
|
|
|
if (!dm) {
|
2013-12-10 18:33:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
void* ptr = dm->data();
|
2016-09-08 15:58:37 +00:00
|
|
|
REPORTER_ASSERT(reporter, ptr);
|
|
|
|
if (!ptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(ptr, kTestString, sizeof(kTestString));
|
2013-12-10 18:33:07 +00:00
|
|
|
dm->unlock();
|
2016-09-08 15:58:37 +00:00
|
|
|
bool relockSuccess = dm->lock();
|
|
|
|
if (assertRelock) {
|
|
|
|
REPORTER_ASSERT(reporter, relockSuccess);
|
|
|
|
}
|
|
|
|
if (!relockSuccess) {
|
2013-12-10 18:33:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr = dm->data();
|
2016-09-08 15:58:37 +00:00
|
|
|
REPORTER_ASSERT(reporter, ptr);
|
|
|
|
if (!ptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength));
|
2013-12-10 18:33:07 +00:00
|
|
|
dm->unlock();
|
|
|
|
}
|
2016-09-08 15:58:37 +00:00
|
|
|
|
|
|
|
DEF_TEST(DiscardableMemory_global, reporter) {
|
|
|
|
std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStringLength));
|
|
|
|
// lock() test is allowed to fail, since other threads could be
|
|
|
|
// using global pool.
|
|
|
|
test_dm(reporter, dm.get(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(DiscardableMemory_nonglobal, reporter) {
|
2017-04-19 17:17:59 +00:00
|
|
|
sk_sp<SkDiscardableMemoryPool> pool(
|
2017-04-25 12:58:57 +00:00
|
|
|
SkDiscardableMemoryPool::Make(1024));
|
2016-09-08 15:58:37 +00:00
|
|
|
std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
|
|
|
|
test_dm(reporter, dm.get(), true);
|
|
|
|
}
|
2017-10-09 19:45:33 +00:00
|
|
|
|