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 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 
14 /**
15  *  Interface for discardable memory. Implementation is provided by the
16  *  embedder.
17  */
18 class SK_API SkDiscardableMemory {
19 public:
20     /**
21      *  Factory method that creates, initializes and locks an SkDiscardableMemory
22      *  object. If either of these steps fails, a nullptr pointer will be returned.
23      */
24     static SkDiscardableMemory* Create(size_t bytes);
25 
26     /**
27      *  Factory class that creates, initializes and locks an SkDiscardableMemory
28      *  object. If either of these steps fails, a nullptr pointer will be returned.
29      */
30     class Factory : public SkRefCnt {
31     public:
32         virtual SkDiscardableMemory* create(size_t bytes) = 0;
33     private:
34         typedef SkRefCnt INHERITED;
35     };
36 
37     /** Must not be called while locked.
38      */
~SkDiscardableMemory()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 SK_WARN_UNUSED_RESULT 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
66