Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef MOZILLA_GFX_DRAWEVENTRECORDER_H_
7 : #define MOZILLA_GFX_DRAWEVENTRECORDER_H_
8 :
9 : #include "2D.h"
10 : #include "RecordedEvent.h"
11 : #include <ostream>
12 : #include <fstream>
13 :
14 : #include <unordered_set>
15 :
16 : namespace mozilla {
17 : namespace gfx {
18 :
19 : class PathRecording;
20 :
21 : class DrawEventRecorderPrivate : public DrawEventRecorder
22 : {
23 : public:
24 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderPrivate)
25 : DrawEventRecorderPrivate();
26 0 : virtual ~DrawEventRecorderPrivate() { }
27 0 : virtual void Finish() {
28 : // The iteration is a bit awkward here because our iterator will
29 : // be invalidated by the removal
30 0 : for (auto font = mStoredFonts.begin(); font != mStoredFonts.end(); ) {
31 0 : auto oldFont = font++;
32 0 : (*oldFont)->RemoveUserData(reinterpret_cast<UserDataKey*>(this));
33 : }
34 0 : for (auto surface = mStoredSurfaces.begin(); surface != mStoredSurfaces.end(); ) {
35 0 : auto oldSurface = surface++;
36 0 : (*oldSurface)->RemoveUserData(reinterpret_cast<UserDataKey*>(this));
37 : }
38 :
39 0 : }
40 :
41 : template<class S>
42 : void WriteHeader(S &aStream);
43 :
44 : virtual void RecordEvent(const RecordedEvent &aEvent) = 0;
45 : void WritePath(const PathRecording *aPath);
46 :
47 0 : void AddStoredObject(const ReferencePtr aObject) {
48 0 : mStoredObjects.insert(aObject);
49 0 : }
50 :
51 0 : void RemoveStoredObject(const ReferencePtr aObject) {
52 0 : mStoredObjects.erase(aObject);
53 0 : }
54 :
55 0 : void AddScaledFont(ScaledFont* aFont) {
56 0 : mStoredFonts.insert(aFont);
57 0 : }
58 :
59 0 : void RemoveScaledFont(ScaledFont* aFont) {
60 0 : mStoredFonts.erase(aFont);
61 0 : }
62 :
63 0 : void AddSourceSurface(SourceSurface* aSurface) {
64 0 : mStoredSurfaces.insert(aSurface);
65 0 : }
66 :
67 0 : void RemoveSourceSurface(SourceSurface* aSurface) {
68 0 : mStoredSurfaces.erase(aSurface);
69 0 : }
70 :
71 0 : bool HasStoredObject(const ReferencePtr aObject) {
72 0 : return mStoredObjects.find(aObject) != mStoredObjects.end();
73 : }
74 :
75 0 : void AddStoredFontData(const uint64_t aFontDataKey) {
76 0 : mStoredFontData.insert(aFontDataKey);
77 0 : }
78 :
79 0 : bool HasStoredFontData(const uint64_t aFontDataKey) {
80 0 : return mStoredFontData.find(aFontDataKey) != mStoredFontData.end();
81 : }
82 :
83 : protected:
84 : virtual void Flush() = 0;
85 :
86 : std::unordered_set<const void*> mStoredObjects;
87 : std::unordered_set<uint64_t> mStoredFontData;
88 : std::unordered_set<ScaledFont*> mStoredFonts;
89 : std::unordered_set<SourceSurface*> mStoredSurfaces;
90 : };
91 :
92 : class DrawEventRecorderFile : public DrawEventRecorderPrivate
93 : {
94 : public:
95 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderFile, override)
96 : explicit DrawEventRecorderFile(const char *aFilename);
97 : ~DrawEventRecorderFile();
98 :
99 : void RecordEvent(const RecordedEvent &aEvent) override;
100 :
101 : /**
102 : * Returns whether a recording file is currently open.
103 : */
104 : bool IsOpen();
105 :
106 : /**
107 : * Opens new file with the provided name. The recorder does NOT forget which
108 : * objects it has recorded. This can be used with Close, so that a recording
109 : * can be processed in chunks. The file must not already be open.
110 : */
111 : void OpenNew(const char *aFilename);
112 :
113 : /**
114 : * Closes the file so that it can be processed. The recorder does NOT forget
115 : * which objects it has recorded. This can be used with OpenNew, so that a
116 : * recording can be processed in chunks. The file must be open.
117 : */
118 : void Close();
119 :
120 : private:
121 : void Flush() override;
122 :
123 : std::ofstream mOutputStream;
124 : };
125 :
126 : // WARNING: This should not be used in its existing state because
127 : // it is likely to OOM because of large continguous allocations.
128 : class DrawEventRecorderMemory final : public DrawEventRecorderPrivate
129 : {
130 : public:
131 0 : MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderMemory, override)
132 :
133 : /**
134 : * Constructs a DrawEventRecorder that stores the recording in memory.
135 : */
136 : DrawEventRecorderMemory();
137 :
138 : void RecordEvent(const RecordedEvent &aEvent) override;
139 :
140 : /**
141 : * @return the current size of the recording (in chars).
142 : */
143 : size_t RecordingSize();
144 :
145 : /**
146 : * Wipes the internal recording buffer, but the recorder does NOT forget which
147 : * objects it has recorded. This can be used so that a recording can be copied
148 : * and processed in chunks, releasing memory as it goes.
149 : */
150 : void WipeRecording();
151 :
152 : MemStream mOutputStream;
153 : private:
154 0 : ~DrawEventRecorderMemory() {};
155 :
156 : void Flush() override;
157 : };
158 :
159 : } // namespace gfx
160 : } // namespace mozilla
161 :
162 : #endif /* MOZILLA_GFX_DRAWEVENTRECORDER_H_ */
|