summaryrefslogtreecommitdiff
path: root/chromium/cc/paint/transfer_cache_entry.h
blob: 474bd970344a857f2e36da7972fd61f752981b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_PAINT_TRANSFER_CACHE_ENTRY_H_
#define CC_PAINT_TRANSFER_CACHE_ENTRY_H_

#include <memory>

#include "base/containers/span.h"
#include "cc/paint/paint_export.h"

class GrContext;

namespace cc {

// To add a new transfer cache entry type:
//  - Add a type name to the TransferCacheEntryType enum.
//  - Implement a ClientTransferCacheEntry and ServiceTransferCacheEntry for
//    your new type.
//  - Update ServiceTransferCacheEntry::Create in transfer_cache_entry.cc.
enum class TransferCacheEntryType : uint32_t {
  kRawMemory,
  kImage,
  kPaintTypeface,
  kColorSpace,
  kPath,
  kShader,
  // Add new entries above this line, make sure to update kLast.
  kLast = kShader,
};

// An interface used on the client to serialize a transfer cache entry
// into raw bytes that can be sent to the service.
class CC_PAINT_EXPORT ClientTransferCacheEntry {
 public:
  virtual ~ClientTransferCacheEntry() {}

  // Returns the type of this entry. Combined with id, it should form a unique
  // identifier.
  virtual TransferCacheEntryType Type() const = 0;

  // Returns the id of this entry. Combined with type, it should form a unique
  // identifier.
  virtual uint32_t Id() const = 0;

  // Returns the serialized sized of this entry in bytes. This function will be
  // used to determine how much memory is going to be allocated and passed to
  // the Serialize() call.
  virtual size_t SerializedSize() const = 0;

  // Serializes the entry into the given span of memory. The size of the span is
  // guaranteed to be at least SerializedSize() bytes. Returns true on success
  // and false otherwise.
  virtual bool Serialize(base::span<uint8_t> data) const = 0;

  // Returns the same value as Type() but as a uint32_t to use via
  // ContextSupport.
  uint32_t UnsafeType() const { return static_cast<uint32_t>(Type()); }
};

// An interface which receives the raw data sent by the client and
// deserializes it into the appropriate service-side object.
class CC_PAINT_EXPORT ServiceTransferCacheEntry {
 public:
  static std::unique_ptr<ServiceTransferCacheEntry> Create(
      TransferCacheEntryType type);

  // Checks that |raw_type| represents a valid TransferCacheEntryType and
  // populates |type|. If |raw_type| is not valid, the function returns false
  // and |type| is not modified.
  static bool SafeConvertToType(uint32_t raw_type,
                                TransferCacheEntryType* type);

  virtual ~ServiceTransferCacheEntry() {}

  // Returns the type of this entry.
  virtual TransferCacheEntryType Type() const = 0;

  // Returns the cached size of this entry. This value is used for memory
  // bookkeeping and to determine whether an unlocked cache entry will be
  // evicted.
  virtual size_t CachedSize() const = 0;

  // Deserialize the cache entry from the given span of memory with the given
  // context.
  virtual bool Deserialize(GrContext* context,
                           base::span<const uint8_t> data) = 0;
};

// Helpers to simplify subclassing.
template <class Base, TransferCacheEntryType EntryType>
class TransferCacheEntryBase : public Base {
 public:
  static constexpr TransferCacheEntryType kType = EntryType;
  TransferCacheEntryType Type() const final { return kType; }
};

template <TransferCacheEntryType EntryType>
using ClientTransferCacheEntryBase =
    TransferCacheEntryBase<ClientTransferCacheEntry, EntryType>;

template <TransferCacheEntryType EntryType>
using ServiceTransferCacheEntryBase =
    TransferCacheEntryBase<ServiceTransferCacheEntry, EntryType>;

};  // namespace cc

#endif  // CC_PAINT_TRANSFER_CACHE_ENTRY_H_