transfer_cache_entry.h 3.8 KB

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