• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkRecord_DEFINED
9 #define SkRecord_DEFINED
10 
11 #include "include/private/SkTLogic.h"
12 #include "include/private/SkTemplates.h"
13 #include "src/core/SkArenaAlloc.h"
14 #include "src/core/SkRecords.h"
15 
16 // SkRecord represents a sequence of SkCanvas calls, saved for future use.
17 // These future uses may include: replay, optimization, serialization, or combinations of those.
18 //
19 // Though an enterprising user may find calling alloc(), append(), visit(), and mutate() enough to
20 // work with SkRecord, you probably want to look at SkRecorder which presents an SkCanvas interface
21 // for creating an SkRecord, and SkRecordDraw which plays an SkRecord back into another SkCanvas.
22 //
23 // SkRecord often looks like it's compatible with any type T, but really it's compatible with any
24 // type T which has a static const SkRecords::Type kType.  That is to say, SkRecord is compatible
25 // only with SkRecords::* structs defined in SkRecords.h.  Your compiler will helpfully yell if you
26 // get this wrong.
27 
28 class SkRecord : public SkRefCnt {
29 public:
30     SkRecord() = default;
31     ~SkRecord();
32 
33     // Returns the number of canvas commands in this SkRecord.
count()34     int count() const { return fCount; }
35 
36     // Visit the i-th canvas command with a functor matching this interface:
37     //   template <typename T>
38     //   R operator()(const T& record) { ... }
39     // This operator() must be defined for at least all SkRecords::*.
40     template <typename F>
41     auto visit(int i, F&& f) const -> decltype(f(SkRecords::NoOp())) {
42         return fRecords[i].visit(f);
43     }
44 
45     // Mutate the i-th canvas command with a functor matching this interface:
46     //   template <typename T>
47     //   R operator()(T* record) { ... }
48     // This operator() must be defined for at least all SkRecords::*.
49     template <typename F>
50     auto mutate(int i, F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
51         return fRecords[i].mutate(f);
52     }
53 
54     // Allocate contiguous space for count Ts, to be freed when the SkRecord is destroyed.
55     // Here T can be any class, not just those from SkRecords.  Throws on failure.
56     template <typename T>
57     T* alloc(size_t count = 1) {
58         struct RawBytes {
59             alignas(T) char data[sizeof(T)];
60         };
61         fApproxBytesAllocated += count * sizeof(T) + alignof(T);
62         return (T*)fAlloc.makeArrayDefault<RawBytes>(count);
63     }
64 
65     // Add a new command of type T to the end of this SkRecord.
66     // You are expected to placement new an object of type T onto this pointer.
67     template <typename T>
append()68     T* append() {
69         if (fCount == fReserved) {
70             this->grow();
71         }
72         return fRecords[fCount++].set(this->allocCommand<T>());
73     }
74 
75     // Replace the i-th command with a new command of type T.
76     // You are expected to placement new an object of type T onto this pointer.
77     // References to the original command are invalidated.
78     template <typename T>
replace(int i)79     T* replace(int i) {
80         SkASSERT(i < this->count());
81 
82         Destroyer destroyer;
83         this->mutate(i, destroyer);
84 
85         return fRecords[i].set(this->allocCommand<T>());
86     }
87 
88     // Replace the i-th command with a new command of type T.
89     // You are expected to placement new an object of type T onto this pointer.
90     // You must show proof that you've already adopted the existing command.
91     template <typename T, typename Existing>
replace(int i,const SkRecords::Adopted<Existing> & proofOfAdoption)92     T* replace(int i, const SkRecords::Adopted<Existing>& proofOfAdoption) {
93         SkASSERT(i < this->count());
94 
95         SkASSERT(Existing::kType == fRecords[i].type());
96         SkASSERT(proofOfAdoption == fRecords[i].ptr());
97 
98         return fRecords[i].set(this->allocCommand<T>());
99     }
100 
101     // Does not return the bytes in any pointers embedded in the Records; callers
102     // need to iterate with a visitor to measure those they care for.
103     size_t bytesUsed() const;
104 
105     // Rearrange and resize this record to eliminate any NoOps.
106     // May change count() and the indices of ops, but preserves their order.
107     void defrag();
108 
109 private:
110     // An SkRecord is structured as an array of pointers into a big chunk of memory where
111     // records representing each canvas draw call are stored:
112     //
113     // fRecords:  [*][*][*]...
114     //             |  |  |
115     //             |  |  |
116     //             |  |  +---------------------------------------+
117     //             |  +-----------------+                        |
118     //             |                    |                        |
119     //             v                    v                        v
120     //   fAlloc:  [SkRecords::DrawRect][SkRecords::DrawPosTextH][SkRecords::DrawRect]...
121     //
122     // We store the types of each of the pointers alongside the pointer.
123     // The cost to append a T to this structure is 8 + sizeof(T) bytes.
124 
125     // A mutator that can be used with replace to destroy canvas commands.
126     struct Destroyer {
127         template <typename T>
operatorDestroyer128         void operator()(T* record) { record->~T(); }
129     };
130 
131     template <typename T>
SK_WHEN(std::is_empty<T>::value,T *)132     SK_WHEN(std::is_empty<T>::value, T*) allocCommand() {
133         static T singleton = {};
134         return &singleton;
135     }
136 
137     template <typename T>
allocCommand()138     SK_WHEN(!std::is_empty<T>::value, T*) allocCommand() { return this->alloc<T>(); }
139 
140     void grow();
141 
142     // A typed pointer to some bytes in fAlloc.  visit() and mutate() allow polymorphic dispatch.
143     struct Record {
144         SkRecords::Type fType;
145         void*           fPtr;
146 
147         // Point this record to its data in fAlloc.  Returns ptr for convenience.
148         template <typename T>
setRecord149         T* set(T* ptr) {
150             fType = T::kType;
151             fPtr  = ptr;
152             SkASSERT(this->ptr() == ptr && this->type() == T::kType);
153             return ptr;
154         }
155 
typeRecord156         SkRecords::Type type() const { return fType; }
ptrRecord157         void* ptr() const { return fPtr; }
158 
159         // Visit this record with functor F (see public API above).
160         template <typename F>
161         auto visit(F&& f) const -> decltype(f(SkRecords::NoOp())) {
162         #define CASE(T) case SkRecords::T##_Type: return f(*(const SkRecords::T*)this->ptr());
163             switch(this->type()) { SK_RECORD_TYPES(CASE) }
164         #undef CASE
165             SkDEBUGFAIL("Unreachable");
166             static const SkRecords::NoOp noop{};
167             return f(noop);
168         }
169 
170         // Mutate this record with functor F (see public API above).
171         template <typename F>
172         auto mutate(F&& f) -> decltype(f((SkRecords::NoOp*)nullptr)) {
173         #define CASE(T) case SkRecords::T##_Type: return f((SkRecords::T*)this->ptr());
174             switch(this->type()) { SK_RECORD_TYPES(CASE) }
175         #undef CASE
176             SkDEBUGFAIL("Unreachable");
177             static const SkRecords::NoOp noop{};
178             return f(const_cast<SkRecords::NoOp*>(&noop));
179         }
180     };
181 
182     // fRecords needs to be a data structure that can append fixed length data, and need to
183     // support efficient random access and forward iteration.  (It doesn't need to be contiguous.)
184     int fCount{0},
185         fReserved{0};
186     SkAutoTMalloc<Record> fRecords;
187 
188     // fAlloc needs to be a data structure which can append variable length data in contiguous
189     // chunks, returning a stable handle to that data for later retrieval.
190     SkArenaAlloc fAlloc{256};
191     size_t       fApproxBytesAllocated{0};
192 };
193 
194 #endif//SkRecord_DEFINED
195