scroggo@google.com | 8b71ef1 | 2013-08-19 18:38:08 +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 | |
| 8 | #ifndef SkDiscardableMemory_DEFINED |
| 9 | #define SkDiscardableMemory_DEFINED |
| 10 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 11 | #include "SkRefCnt.h" |
scroggo@google.com | 8b71ef1 | 2013-08-19 18:38:08 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
| 14 | /** |
| 15 | * Interface for discardable memory. Implementation is provided by the |
| 16 | * embedder. |
| 17 | */ |
commit-bot@chromium.org | a6aa0e3 | 2013-08-22 17:53:05 +0000 | [diff] [blame] | 18 | class SK_API SkDiscardableMemory { |
scroggo@google.com | 8b71ef1 | 2013-08-19 18:38:08 +0000 | [diff] [blame] | 19 | public: |
| 20 | /** |
| 21 | * Factory method that creates, initializes and locks an SkDiscardableMemory |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | * object. If either of these steps fails, a nullptr pointer will be returned. |
scroggo@google.com | 8b71ef1 | 2013-08-19 18:38:08 +0000 | [diff] [blame] | 23 | */ |
| 24 | static SkDiscardableMemory* Create(size_t bytes); |
| 25 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 26 | /** |
| 27 | * Factory class that creates, initializes and locks an SkDiscardableMemory |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | * object. If either of these steps fails, a nullptr pointer will be returned. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 29 | */ |
| 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.com | 8b71ef1 | 2013-08-19 18:38:08 +0000 | [diff] [blame] | 37 | /** 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 |