michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkDiscardableMemory_DEFINED michael@0: #define SkDiscardableMemory_DEFINED michael@0: michael@0: #include "SkRefCnt.h" michael@0: #include "SkTypes.h" michael@0: michael@0: /** michael@0: * Interface for discardable memory. Implementation is provided by the michael@0: * embedder. michael@0: */ michael@0: class SK_API SkDiscardableMemory { michael@0: public: michael@0: /** michael@0: * Factory method that creates, initializes and locks an SkDiscardableMemory michael@0: * object. If either of these steps fails, a NULL pointer will be returned. michael@0: */ michael@0: static SkDiscardableMemory* Create(size_t bytes); michael@0: michael@0: /** michael@0: * Factory class that creates, initializes and locks an SkDiscardableMemory michael@0: * object. If either of these steps fails, a NULL pointer will be returned. michael@0: */ michael@0: class Factory : public SkRefCnt { michael@0: public: michael@0: virtual SkDiscardableMemory* create(size_t bytes) = 0; michael@0: private: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: /** Must not be called while locked. michael@0: */ michael@0: virtual ~SkDiscardableMemory() {} michael@0: michael@0: /** michael@0: * Locks the memory, prevent it from being discarded. Once locked. you may michael@0: * obtain a pointer to that memory using the data() method. michael@0: * michael@0: * lock() may return false, indicating that the underlying memory was michael@0: * discarded and that the lock failed. michael@0: * michael@0: * Nested calls to lock are not allowed. michael@0: */ michael@0: virtual bool lock() = 0; michael@0: michael@0: /** michael@0: * Returns the current pointer for the discardable memory. This call is ONLY michael@0: * valid when the discardable memory object is locked. michael@0: */ michael@0: virtual void* data() = 0; michael@0: michael@0: /** michael@0: * Unlock the memory so that it can be purged by the system. Must be called michael@0: * after every successful lock call. michael@0: */ michael@0: virtual void unlock() = 0; michael@0: }; michael@0: michael@0: #endif