1b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com/* 2b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Copyright 2013 Google Inc. 3b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * 4b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Use of this source code is governed by a BSD-style license that can be 5b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * found in the LICENSE file. 6b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 7b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 8b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com#ifndef SkDiscardableMemory_DEFINED 9b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com#define SkDiscardableMemory_DEFINED 10b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 119ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com#include "SkRefCnt.h" 12b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com#include "SkTypes.h" 13b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 14b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com/** 15b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Interface for discardable memory. Implementation is provided by the 16b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * embedder. 17b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 18bd6c40994d5ba83ca198bd3c0924e651a23f88accommit-bot@chromium.orgclass SK_API SkDiscardableMemory { 19b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.compublic: 20b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com /** 21b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Factory method that creates, initializes and locks an SkDiscardableMemory 22b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * object. If either of these steps fails, a NULL pointer will be returned. 23b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 24b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com static SkDiscardableMemory* Create(size_t bytes); 25b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 269ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com /** 279ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com * Factory class that creates, initializes and locks an SkDiscardableMemory 289ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com * object. If either of these steps fails, a NULL pointer will be returned. 299ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com */ 309ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com class Factory : public SkRefCnt { 319ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com public: 329ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com virtual SkDiscardableMemory* create(size_t bytes) = 0; 339ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com private: 349ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com typedef SkRefCnt INHERITED; 359ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com }; 369ab9c97883d0a0b7084cbb91eaf1ba6fb4c46affhalcanary@google.com 37b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com /** Must not be called while locked. 38b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 39b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com virtual ~SkDiscardableMemory() {} 40b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 41b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com /** 42b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Locks the memory, prevent it from being discarded. Once locked. you may 43b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * obtain a pointer to that memory using the data() method. 44b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * 45b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * lock() may return false, indicating that the underlying memory was 46b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * discarded and that the lock failed. 47b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * 48b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Nested calls to lock are not allowed. 49b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 50b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com virtual bool lock() = 0; 51b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 52b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com /** 53b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Returns the current pointer for the discardable memory. This call is ONLY 54b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * valid when the discardable memory object is locked. 55b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 56b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com virtual void* data() = 0; 57b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 58b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com /** 59b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * Unlock the memory so that it can be purged by the system. Must be called 60b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com * after every successful lock call. 61b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com */ 62b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com virtual void unlock() = 0; 63b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com}; 64b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com 65b419b275dd209a9de49be8018d52aab9af61cb4cscroggo@google.com#endif 66