1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/lazy/SkPurgeableMemoryBlock.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkPurgeableMemoryBlock_DEFINED 1.12 +#define SkPurgeableMemoryBlock_DEFINED 1.13 + 1.14 +#include "SkTypes.h" 1.15 + 1.16 +class SkPurgeableMemoryBlock : public SkNoncopyable { 1.17 + 1.18 +public: 1.19 + /** 1.20 + * Whether or not this platform has an implementation for purgeable memory. 1.21 + */ 1.22 + static bool IsSupported(); 1.23 + 1.24 + /** 1.25 + * Create a new purgeable memory block of 'size' bytes. Returns NULL if not supported on this 1.26 + * platform or on failure. 1.27 + * @param size Number of bytes requested. 1.28 + * @return A new block, or NULL on failure. 1.29 + */ 1.30 + static SkPurgeableMemoryBlock* Create(size_t size); 1.31 + 1.32 +#ifdef SK_DEBUG 1.33 + /** 1.34 + * Whether the platform supports one shot purge of all unpinned blocks. If so, 1.35 + * PurgeAllUnpinnedBlocks will be used to test a purge. Otherwise, purge will be called on 1.36 + * individual blocks. 1.37 + */ 1.38 + static bool PlatformSupportsPurgingAllUnpinnedBlocks(); 1.39 + 1.40 + /** 1.41 + * Purge all unpinned blocks at once, if the platform supports it. 1.42 + */ 1.43 + static bool PurgeAllUnpinnedBlocks(); 1.44 + 1.45 + // If PlatformSupportsPurgingAllUnpinnedBlocks returns true, this will not be called, so it can 1.46 + // simply return false. 1.47 + bool purge(); 1.48 + 1.49 + bool isPinned() const { return fPinned; } 1.50 +#endif 1.51 + 1.52 + ~SkPurgeableMemoryBlock(); 1.53 + 1.54 + /** 1.55 + * Output parameter for pin(), stating whether the data has been retained. 1.56 + */ 1.57 + enum PinResult { 1.58 + /** 1.59 + * The data has been purged, or this is the first call to pin. 1.60 + */ 1.61 + kUninitialized_PinResult, 1.62 + 1.63 + /** 1.64 + * The data has been retained. The memory contains the same data it held when unpin() was 1.65 + * called. 1.66 + */ 1.67 + kRetained_PinResult, 1.68 + }; 1.69 + 1.70 + /** 1.71 + * Pin the memory for use. Must not be called while already pinned. 1.72 + * @param PinResult Whether the data was retained. Ignored on failure. 1.73 + * @return Pointer to the pinned data on success. NULL on failure. 1.74 + */ 1.75 + void* pin(PinResult*); 1.76 + 1.77 + /** 1.78 + * Unpin the data so it can be purged if necessary. 1.79 + */ 1.80 + void unpin(); 1.81 + 1.82 +private: 1.83 + void* fAddr; 1.84 + size_t fSize; 1.85 + bool fPinned; 1.86 +#ifdef SK_BUILD_FOR_ANDROID 1.87 + int fFD; 1.88 +#endif 1.89 + 1.90 + // Unimplemented default constructor is private, to prevent manual creation. 1.91 + SkPurgeableMemoryBlock(); 1.92 + 1.93 + // The correct way to create a new one is from the static Create. 1.94 + SkPurgeableMemoryBlock(size_t); 1.95 +}; 1.96 + 1.97 +#endif // SkPurgeableMemoryBlock_DEFINED