blob: 8952b8df7f2df882495f0300aba4d3a897fa6886 [file] [log] [blame]
scroggo@google.com8b71ef12013-08-19 18:38:08 +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 */
7
8#ifndef SkDiscardableMemory_DEFINED
9#define SkDiscardableMemory_DEFINED
10
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkRefCnt.h"
scroggo@google.com8b71ef12013-08-19 18:38:08 +000012#include "SkTypes.h"
13
14/**
15 * Interface for discardable memory. Implementation is provided by the
16 * embedder.
17 */
commit-bot@chromium.orga6aa0e32013-08-22 17:53:05 +000018class SK_API SkDiscardableMemory {
scroggo@google.com8b71ef12013-08-19 18:38:08 +000019public:
20 /**
21 * Factory method that creates, initializes and locks an SkDiscardableMemory
halcanary96fcdcc2015-08-27 07:41:13 -070022 * object. If either of these steps fails, a nullptr pointer will be returned.
scroggo@google.com8b71ef12013-08-19 18:38:08 +000023 */
24 static SkDiscardableMemory* Create(size_t bytes);
25
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000026 /**
27 * Factory class that creates, initializes and locks an SkDiscardableMemory
halcanary96fcdcc2015-08-27 07:41:13 -070028 * object. If either of these steps fails, a nullptr pointer will be returned.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000029 */
30 class Factory : public SkRefCnt {
31 public:
32 virtual SkDiscardableMemory* create(size_t bytes) = 0;
33 private:
34 typedef SkRefCnt INHERITED;
35 };
36
scroggo@google.com8b71ef12013-08-19 18:38:08 +000037 /** Must not be called while locked.
38 */
39 virtual ~SkDiscardableMemory() {}
40
41 /**
42 * Locks the memory, prevent it from being discarded. Once locked. you may
43 * obtain a pointer to that memory using the data() method.
44 *
45 * lock() may return false, indicating that the underlying memory was
46 * discarded and that the lock failed.
47 *
48 * Nested calls to lock are not allowed.
49 */
50 virtual bool lock() = 0;
51
52 /**
53 * Returns the current pointer for the discardable memory. This call is ONLY
54 * valid when the discardable memory object is locked.
55 */
56 virtual void* data() = 0;
57
58 /**
59 * Unlock the memory so that it can be purged by the system. Must be called
60 * after every successful lock call.
61 */
62 virtual void unlock() = 0;
63};
64
65#endif