1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CC_PAINT_TRANSFER_CACHE_ENTRY_H_ 6 #define CC_PAINT_TRANSFER_CACHE_ENTRY_H_ 7 8 #include <memory> 9 10 #include "base/containers/span.h" 11 #include "cc/paint/paint_export.h" 12 13 class GrDirectContext; 14 15 namespace cc { 16 17 // To add a new transfer cache entry type: 18 // - Add a type name to the TransferCacheEntryType enum. 19 // - Implement a ClientTransferCacheEntry and ServiceTransferCacheEntry for 20 // your new type. 21 // - Update ServiceTransferCacheEntry::Create in transfer_cache_entry.cc. 22 enum class TransferCacheEntryType : uint32_t { 23 kRawMemory, 24 kImage, 25 kShader, 26 kSkottie, 27 // Add new entries above this line, make sure to update kLast. 28 kLast = kSkottie, 29 }; 30 31 // An interface used on the client to serialize a transfer cache entry 32 // into raw bytes that can be sent to the service. 33 class CC_PAINT_EXPORT ClientTransferCacheEntry { 34 public: ~ClientTransferCacheEntry()35 virtual ~ClientTransferCacheEntry() {} 36 37 // Returns the type of this entry. Combined with id, it should form a unique 38 // identifier. 39 virtual TransferCacheEntryType Type() const = 0; 40 41 // Returns the id of this entry. Combined with type, it should form a unique 42 // identifier. 43 virtual uint32_t Id() const = 0; 44 45 // Returns the serialized sized of this entry in bytes. This function will be 46 // used to determine how much memory is going to be allocated and passed to 47 // the Serialize() call. 48 virtual uint32_t SerializedSize() const = 0; 49 50 // Serializes the entry into the given span of memory. The size of the span is 51 // guaranteed to be at least SerializedSize() bytes. Returns true on success 52 // and false otherwise. 53 virtual bool Serialize(base::span<uint8_t> data) const = 0; 54 55 // Returns the same value as Type() but as a uint32_t to use via 56 // ContextSupport. UnsafeType()57 uint32_t UnsafeType() const { return static_cast<uint32_t>(Type()); } 58 }; 59 60 // An interface which receives the raw data sent by the client and 61 // deserializes it into the appropriate service-side object. 62 class CC_PAINT_EXPORT ServiceTransferCacheEntry { 63 public: 64 static std::unique_ptr<ServiceTransferCacheEntry> Create( 65 TransferCacheEntryType type); 66 67 // Checks that |raw_type| represents a valid TransferCacheEntryType and 68 // populates |type|. If |raw_type| is not valid, the function returns false 69 // and |type| is not modified. 70 static bool SafeConvertToType(uint32_t raw_type, 71 TransferCacheEntryType* type); 72 73 // Returns true if the entry needs a GrContext during deserialization. 74 static bool UsesGrContext(TransferCacheEntryType type); 75 ~ServiceTransferCacheEntry()76 virtual ~ServiceTransferCacheEntry() {} 77 78 // Returns the type of this entry. 79 virtual TransferCacheEntryType Type() const = 0; 80 81 // Returns the cached size of this entry. This value is used for memory 82 // bookkeeping and to determine whether an unlocked cache entry will be 83 // evicted. 84 virtual size_t CachedSize() const = 0; 85 86 // Deserialize the cache entry from the given span of memory with the given 87 // context. 88 virtual bool Deserialize(GrDirectContext* context, 89 base::span<const uint8_t> data) = 0; 90 }; 91 92 // Helpers to simplify subclassing. 93 template <class Base, TransferCacheEntryType EntryType> 94 class TransferCacheEntryBase : public Base { 95 public: 96 static constexpr TransferCacheEntryType kType = EntryType; Type()97 TransferCacheEntryType Type() const final { return kType; } 98 }; 99 100 template <TransferCacheEntryType EntryType> 101 using ClientTransferCacheEntryBase = 102 TransferCacheEntryBase<ClientTransferCacheEntry, EntryType>; 103 104 template <TransferCacheEntryType EntryType> 105 using ServiceTransferCacheEntryBase = 106 TransferCacheEntryBase<ServiceTransferCacheEntry, EntryType>; 107 108 } // namespace cc 109 110 #endif // CC_PAINT_TRANSFER_CACHE_ENTRY_H_ 111