blob: 6d7923fd8dd1361d13508980f809c7df012f19b2 [file] [log] [blame]
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +00001/*
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 */
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkRefCnt.h"
9#include "src/core/SkDiscardableMemory.h"
10#include "src/lazy/SkDiscardableMemoryPool.h"
11#include "tests/Test.h"
Ben Wagnerb5f28972018-04-17 17:42:08 -040012
Ben Wagnerd90cd3b2018-05-22 10:48:08 -040013#include <memory>
14
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000015DEF_TEST(DiscardableMemoryPool, reporter) {
Hal Canary788c3c42017-04-25 08:58:57 -040016 sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Make(1));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000017 pool->setRAMBudget(3);
18 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
19
Ben Wagner145dbcd2016-11-03 14:40:50 -040020 std::unique_ptr<SkDiscardableMemory> dm1(pool->create(100));
halcanary96fcdcc2015-08-27 07:41:13 -070021 REPORTER_ASSERT(reporter, dm1->data() != nullptr);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000022 REPORTER_ASSERT(reporter, 100 == pool->getRAMUsed());
23 dm1->unlock();
24 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
25 REPORTER_ASSERT(reporter, !dm1->lock());
26
27
Ben Wagner145dbcd2016-11-03 14:40:50 -040028 std::unique_ptr<SkDiscardableMemory> dm2(pool->create(200));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000029 REPORTER_ASSERT(reporter, 200 == pool->getRAMUsed());
30 pool->setRAMBudget(400);
31 dm2->unlock();
32 REPORTER_ASSERT(reporter, 200 == pool->getRAMUsed());
33 REPORTER_ASSERT(reporter, dm2->lock());
34 dm2->unlock();
35 pool->dumpPool();
36 REPORTER_ASSERT(reporter, !dm2->lock());
37 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
38}