Bug 1156742 Part 4: Add an in memory DrawEventRecorder. r=bas
authorBob Owen <bobowencode@gmail.com>
Tue, 05 Jan 2016 10:08:57 +0000
changeset 318974 00b3ae3147bc1eb7f68dc1c412b41069fe41f9ae
parent 318973 ef3078a358d8072dc4c04a9fb6880928884b5bae
child 318975 d265787bad2983eb224c132896f394a4f169e44d
push id8951
push userbenj@benj.me
push dateTue, 05 Jan 2016 13:08:54 +0000
reviewersbas
bugs1156742
milestone46.0a1
Bug 1156742 Part 4: Add an in memory DrawEventRecorder. r=bas
gfx/2d/DrawEventRecorder.cpp
gfx/2d/DrawEventRecorder.h
--- a/gfx/2d/DrawEventRecorder.cpp
+++ b/gfx/2d/DrawEventRecorder.cpp
@@ -14,41 +14,82 @@ using namespace std;
 const uint32_t kMagicInt = 0xc001feed;
 
 DrawEventRecorderPrivate::DrawEventRecorderPrivate(std::ostream *aStream)
   : mOutputStream(aStream)
 {
 }
 
 void
+DrawEventRecorderPrivate::WriteHeader()
+{
+  WriteElement(*mOutputStream, kMagicInt);
+  WriteElement(*mOutputStream, kMajorRevision);
+  WriteElement(*mOutputStream, kMinorRevision);
+}
+
+void
 DrawEventRecorderPrivate::RecordEvent(const RecordedEvent &aEvent)
 {
   WriteElement(*mOutputStream, aEvent.mType);
 
   aEvent.RecordToStream(*mOutputStream);
 
   Flush();
 }
 
 DrawEventRecorderFile::DrawEventRecorderFile(const char *aFilename)
   : DrawEventRecorderPrivate(nullptr) 
   , mOutputFile(aFilename, ofstream::binary)
 {
   mOutputStream = &mOutputFile;
 
-  WriteElement(*mOutputStream, kMagicInt);
-  WriteElement(*mOutputStream, kMajorRevision);
-  WriteElement(*mOutputStream, kMinorRevision);
+  WriteHeader();
 }
 
 DrawEventRecorderFile::~DrawEventRecorderFile()
 {
   mOutputFile.close();
 }
 
 void
 DrawEventRecorderFile::Flush()
 {
   mOutputFile.flush();
 }
 
+DrawEventRecorderMemory::DrawEventRecorderMemory()
+  : DrawEventRecorderPrivate(nullptr)
+{
+  mOutputStream = &mMemoryStream;
+
+  WriteHeader();
+}
+
+void
+DrawEventRecorderMemory::Flush()
+{
+   mOutputStream->flush();
+}
+
+size_t
+DrawEventRecorderMemory::RecordingSize()
+{
+  return mMemoryStream.tellp();
+}
+
+bool
+DrawEventRecorderMemory::CopyRecording(char* aBuffer, size_t aBufferLen)
+{
+  return !!mMemoryStream.read(aBuffer, aBufferLen);
+}
+
+void
+DrawEventRecorderMemory::WipeRecording()
+{
+  mMemoryStream.str(std::string());
+  mMemoryStream.clear();
+
+  WriteHeader();
+}
+
 } // namespace gfx
 } // namespace mozilla
--- a/gfx/2d/DrawEventRecorder.h
+++ b/gfx/2d/DrawEventRecorder.h
@@ -24,16 +24,18 @@ class PathRecording;
 
 class DrawEventRecorderPrivate : public DrawEventRecorder
 {
 public:
   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderPrivate)
   explicit DrawEventRecorderPrivate(std::ostream *aStream);
   virtual ~DrawEventRecorderPrivate() { }
 
+  void WriteHeader();
+
   void RecordEvent(const RecordedEvent &aEvent);
   void WritePath(const PathRecording *aPath);
 
   void AddStoredObject(const ReferencePtr aObject) {
     mStoredObjects.insert(aObject);
   }
 
   void RemoveStoredObject(const ReferencePtr aObject) {
@@ -66,12 +68,51 @@ public:
   ~DrawEventRecorderFile();
 
 private:
   virtual void Flush();
 
   std::ofstream mOutputFile;
 };
 
+class DrawEventRecorderMemory final : public DrawEventRecorderPrivate
+{
+public:
+  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderMemory)
+
+  /**
+   * Constructs a DrawEventRecorder that stores the recording in memory.
+   */
+  DrawEventRecorderMemory();
+
+  /**
+   * @return the current size of the recording (in chars).
+   */
+  size_t RecordingSize();
+
+  /**
+   * Copies at most aBufferLen chars of the recording into aBuffer.
+   *
+   * @param aBuffer buffer to receive the recording chars
+   * @param aBufferLen length of aBuffer
+   * @return true if copied successfully
+   */
+  bool CopyRecording(char* aBuffer, size_t aBufferLen);
+
+  /**
+   * Wipes the internal recording buffer, but the recorder does NOT forget which
+   * objects it has recorded. This can be used so that a recording can be copied
+   * and processed in chunks, releasing memory as it goes.
+   */
+  void WipeRecording();
+
+private:
+  ~DrawEventRecorderMemory() {};
+
+  void Flush() final;
+
+  std::stringstream mMemoryStream;
+};
+
 } // namespace gfx
 } // namespace mozilla
 
 #endif /* MOZILLA_GFX_DRAWEVENTRECORDER_H_ */