Bug 1319423 - Part 1 - Switch away from std::ofstream to PRFileDesc in DrawEventRecorderFile; r?jrmuizel draft
authorAlex Gaynor <agaynor@mozilla.com>
Tue, 03 Oct 2017 12:29:15 -0400
changeset 676146 99960a4ca55dd66302560c375bb32f7f2d0576aa
parent 675689 19b32a138d08f73961df878a29de6f0aad441683
child 676147 030da713f425e49aefd939426371c78a57c0ce44
push id83399
push userbmo:agaynor@mozilla.com
push dateFri, 06 Oct 2017 17:20:48 +0000
reviewersjrmuizel
bugs1319423
milestone58.0a1
Bug 1319423 - Part 1 - Switch away from std::ofstream to PRFileDesc in DrawEventRecorderFile; r?jrmuizel MozReview-Commit-ID: 1gn8J2fhejS
gfx/2d/DrawEventRecorder.cpp
gfx/2d/DrawEventRecorder.h
gfx/2d/RecordedEvent.h
gfx/2d/RecordedEventImpl.h
--- a/gfx/2d/DrawEventRecorder.cpp
+++ b/gfx/2d/DrawEventRecorder.cpp
@@ -39,53 +39,53 @@ void
 DrawEventRecorderMemory::RecordEvent(const RecordedEvent &aEvent)
 {
   WriteElement(mOutputStream, aEvent.mType);
 
   aEvent.RecordToStream(mOutputStream);
 }
 
 DrawEventRecorderFile::DrawEventRecorderFile(const char *aFilename)
-  : mOutputStream(aFilename, ofstream::binary)
 {
+  mOutputStream.Open(aFilename);
   WriteHeader(mOutputStream);
 }
 
 DrawEventRecorderFile::~DrawEventRecorderFile()
 {
-  mOutputStream.close();
+  Close();
 }
 
 void
 DrawEventRecorderFile::Flush()
 {
-  mOutputStream.flush();
+  mOutputStream.Flush();
 }
 
 bool
 DrawEventRecorderFile::IsOpen()
 {
-  return mOutputStream.is_open();
+  return mOutputStream.IsOpen();
 }
 
 void
 DrawEventRecorderFile::OpenNew(const char *aFilename)
 {
-  MOZ_ASSERT(!mOutputStream.is_open());
+  MOZ_ASSERT(!IsOpen());
 
-  mOutputStream.open(aFilename, ofstream::binary);
+  mOutputStream.Open(aFilename);
   WriteHeader(mOutputStream);
 }
 
 void
 DrawEventRecorderFile::Close()
 {
-  MOZ_ASSERT(mOutputStream.is_open());
+  MOZ_ASSERT(IsOpen());
 
-  mOutputStream.close();
+  mOutputStream.Close();
 }
 
 DrawEventRecorderMemory::DrawEventRecorderMemory()
 {
   WriteHeader(mOutputStream);
 }
 
 void
--- a/gfx/2d/DrawEventRecorder.h
+++ b/gfx/2d/DrawEventRecorder.h
@@ -3,18 +3,16 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef MOZILLA_GFX_DRAWEVENTRECORDER_H_
 #define MOZILLA_GFX_DRAWEVENTRECORDER_H_
 
 #include "2D.h"
 #include "RecordedEvent.h"
-#include <ostream>
-#include <fstream>
 
 #include <unordered_set>
 
 namespace mozilla {
 namespace gfx {
 
 class PathRecording;
 
@@ -115,17 +113,17 @@ public:
    * which objects it has recorded. This can be used with OpenNew, so that a
    * recording can be processed in chunks. The file must be open.
    */
   void Close();
 
 private:
   void Flush() override;
 
-  std::ofstream mOutputStream;
+  PRFileDescStream mOutputStream;
 };
 
 // WARNING: This should not be used in its existing state because
 // it is likely to OOM because of large continguous allocations.
 class DrawEventRecorderMemory final : public DrawEventRecorderPrivate
 {
 public:
   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderMemory, override)
--- a/gfx/2d/RecordedEvent.h
+++ b/gfx/2d/RecordedEvent.h
@@ -2,16 +2,17 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef MOZILLA_GFX_RECORDEDEVENT_H_
 #define MOZILLA_GFX_RECORDEDEVENT_H_
 
 #include "2D.h"
+#include "prio.h"
 #include <ostream>
 #include <sstream>
 #include <cstring>
 #include <vector>
 
 namespace mozilla {
 namespace gfx {
 
@@ -202,16 +203,46 @@ struct MemStream {
     Resize(mLength + aSize);
     memcpy(mData + mLength - aSize, aData, aSize);
   }
 
   MemStream() : mData(nullptr), mLength(0), mCapacity(0) {}
   ~MemStream() { free(mData); }
 };
 
+class PRFileDescStream {
+public:
+  PRFileDescStream() : mFd(nullptr) {}
+
+  void Open(const char *aFilename) {
+    MOZ_ASSERT(!IsOpen());
+    mFd = PR_Open(aFilename, PR_RDWR | PR_CREATE_FILE, PR_IRUSR | PR_IWUSR);
+  }
+
+  void Close() {
+    PR_Close(mFd);
+    mFd = nullptr;
+  }
+
+  bool IsOpen() {
+    return mFd != nullptr;
+  }
+
+  void Flush() {
+    PR_Sync(mFd);
+  }
+
+  void write(const char* aData, size_t aSize) {
+    PR_Write(mFd, static_cast<const void*>(aData), aSize);
+  }
+
+private:
+  PRFileDesc *mFd;
+};
+
 class RecordedEvent {
 public:
   enum EventType {
     DRAWTARGETCREATION = 0,
     DRAWTARGETDESTRUCTION,
     FILLRECT,
     STROKERECT,
     STROKELINE,
@@ -261,18 +292,18 @@ public:
    * only return false when there is a fatal error, as it will probably mean the
    * translation will abort.
    * @param aTranslator Translator to be used for retrieving other referenced
    *                    objects and making playback decisions.
    * @return true unless a fatal problem has occurred and playback should abort.
    */
   virtual bool PlayEvent(Translator *aTranslator) const { return true; }
 
-  virtual void RecordToStream(std::ostream &aStream) const {}
-  virtual void RecordToStream(MemStream &aStream) const  = 0;
+  virtual void RecordToStream(PRFileDescStream &aStream) const = 0;
+  virtual void RecordToStream(MemStream &aStream) const = 0;
 
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const { }
 
   template<class S>
   void RecordPatternData(S &aStream, const PatternStorage &aPatternStorage) const;
   template<class S>
   void ReadPatternData(S &aStream, PatternStorage &aPatternStorage) const;
   void StorePattern(PatternStorage &aDestination, const Pattern &aSource) const;
@@ -310,9 +341,8 @@ protected:
   int32_t mType;
   std::vector<Float> mDashPatternStorage;
 };
 
 } // namespace gfx
 } // namespace mozilla
 
 #endif
-
--- a/gfx/2d/RecordedEventImpl.h
+++ b/gfx/2d/RecordedEventImpl.h
@@ -17,17 +17,17 @@
 #include "SFNTData.h"
 
 namespace mozilla {
 namespace gfx {
 
 template<class Derived>
 class RecordedEventDerived : public RecordedEvent {
   using RecordedEvent::RecordedEvent;
-  void RecordToStream(std::ostream &aStream) const {
+  void RecordToStream(PRFileDescStream &aStream) const {
     static_cast<const Derived*>(this)->Record(aStream);
   }
   void RecordToStream(MemStream &aStream) const {
     SizeCollector size;
     static_cast<const Derived*>(this)->Record(size);
     aStream.Resize(aStream.mLength + size.mTotalSize);
     MemWriter writer(aStream.mData + aStream.mLength - size.mTotalSize);
     static_cast<const Derived*>(this)->Record(writer);
@@ -74,17 +74,17 @@ public:
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 
   ReferencePtr mRefPtr;
   BackendType mBackendType;
   IntSize mSize;
   SurfaceFormat mFormat;
   bool mHasExistingData;
   RefPtr<SourceSurface> mExistingData;
-  
+
 private:
   friend class RecordedEvent;
 
   template<class S>
   MOZ_IMPLICIT RecordedDrawTargetCreation(S &aStream);
 };
 
 class RecordedDrawTargetDestruction : public RecordedEventDerived<RecordedDrawTargetDestruction> {
@@ -506,17 +506,17 @@ public:
     : RecordedDrawingEvent(SETTRANSFORM, aDT), mTransform(aTransform)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "SetTransform"; }
 
   Matrix mTransform;
 private:
   friend class RecordedEvent;
 
    template<class S>
   MOZ_IMPLICIT RecordedSetTransform(S &aStream);
@@ -531,17 +531,17 @@ public:
     , mSource(aSource), mDSOptions(aDSOptions), mOptions(aOptions)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "DrawSurface"; }
 private:
   friend class RecordedEvent;
 
    template<class S>
   MOZ_IMPLICIT RecordedDrawSurface(S &aStream);
 
   ReferencePtr mRefSource;
@@ -560,17 +560,17 @@ public:
     , mColor(aColor), mOffset(aOffset), mSigma(aSigma), mOp(aOp)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "DrawSurfaceWithShadow"; }
 private:
   friend class RecordedEvent;
 
   template<class S>
   MOZ_IMPLICIT RecordedDrawSurfaceWithShadow(S &aStream);
 
   ReferencePtr mRefSource;
@@ -609,22 +609,22 @@ private:
   Point mDestPoint;
   DrawOptions mOptions;
 };
 
 class RecordedPathCreation : public RecordedEventDerived<RecordedPathCreation> {
 public:
   MOZ_IMPLICIT RecordedPathCreation(PathRecording *aPath);
   ~RecordedPathCreation();
-  
+
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "Path Creation"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
   FillRule mFillRule;
   std::vector<PathOp> mPathOps;
@@ -634,22 +634,22 @@ private:
 };
 
 class RecordedPathDestruction : public RecordedEventDerived<RecordedPathDestruction> {
 public:
   MOZ_IMPLICIT RecordedPathDestruction(PathRecording *aPath)
     : RecordedEventDerived(PATHDESTRUCTION), mRefPtr(aPath)
   {
   }
-  
+
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "Path Destruction"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
 
   template<class S>
@@ -666,17 +666,17 @@ public:
   }
 
   ~RecordedSourceSurfaceCreation();
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "SourceSurface Creation"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
   uint8_t *mData;
   int32_t mStride;
@@ -694,17 +694,17 @@ public:
     : RecordedEventDerived(SOURCESURFACEDESTRUCTION), mRefPtr(aRefPtr)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "SourceSurface Destruction"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
 
   template<class S>
@@ -770,17 +770,17 @@ public:
   }
 
   ~RecordedGradientStopsCreation();
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "GradientStops Creation"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
   GradientStop *mStops;
   uint32_t mNumStops;
@@ -797,17 +797,17 @@ public:
     : RecordedEventDerived(GRADIENTSTOPSDESTRUCTION), mRefPtr(aRefPtr)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "GradientStops Destruction"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
 
   template<class S>
@@ -820,17 +820,17 @@ public:
     : RecordedEventDerived(SNAPSHOT), mRefPtr(aRefPtr), mDT(aDT)
   {
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "Snapshot"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 private:
   friend class RecordedEvent;
 
   ReferencePtr mRefPtr;
   ReferencePtr mDT;
 
@@ -1040,17 +1040,17 @@ public:
   {
     aScaledFont->GetFontInstanceData(FontInstanceDataProc, this);
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "ScaledFont Creation"; }
   virtual ReferencePtr GetObjectRef() const { return mRefPtr; }
 
   void SetFontInstanceData(const uint8_t *aData, uint32_t aSize,
                            const FontVariation* aVariations, uint32_t aNumVariations);
 
 private:
   friend class RecordedEvent;
@@ -1098,17 +1098,17 @@ public:
   {
     StorePattern(mPattern, aPattern);
   }
 
   virtual bool PlayEvent(Translator *aTranslator) const;
 
   template<class S> void Record(S &aStream) const;
   virtual void OutputSimpleEventInfo(std::stringstream &aStringStream) const;
-  
+
   virtual std::string GetName() const { return "MaskSurface"; }
 private:
   friend class RecordedEvent;
 
   template<class S>
   MOZ_IMPLICIT RecordedMaskSurface(S &aStream);
 
   PatternStorage mPattern;
@@ -1283,17 +1283,17 @@ RecordedEvent::ReadPatternData(S &aStrea
     return;
   }
 }
 
 inline void
 RecordedEvent::StorePattern(PatternStorage &aDestination, const Pattern &aSource) const
 {
   aDestination.mType = aSource.GetType();
-  
+
   switch (aSource.GetType()) {
   case PatternType::COLOR:
     {
       reinterpret_cast<ColorPatternStorage*>(&aDestination.mStorage)->mColor =
         static_cast<const ColorPattern*>(&aSource)->mColor;
       return;
     }
   case PatternType::LINEAR_GRADIENT:
@@ -2278,17 +2278,17 @@ RecordedPathCreation::RecordedPathCreati
 inline
 RecordedPathCreation::~RecordedPathCreation()
 {
 }
 
 inline bool
 RecordedPathCreation::PlayEvent(Translator *aTranslator) const
 {
-  RefPtr<PathBuilder> builder = 
+  RefPtr<PathBuilder> builder =
     aTranslator->GetReferenceDrawTarget()->CreatePathBuilder(mFillRule);
 
   for (size_t i = 0; i < mPathOps.size(); i++) {
     const PathOp &op = mPathOps[i];
     switch (op.mType) {
     case PathOp::OP_MOVETO:
       builder->MoveTo(op.mP1);
       break;