Bug 1464032 Part 2: Remove LoadEvent and replace with DoWithEvent. r=jrmuizel
authorBob Owen <bobowencode@gmail.com>
Sun, 02 Dec 2018 14:13:56 +0000
changeset 477753 274a9d9596a2b7a2d046e198ebcddaa4c45f82e0
parent 477752 be11539bd8d89d2490e30bdb5cf242850b283e44
child 477754 b2e0e341bb82f5f6f04a02c631fd835c506e96e2
push id113373
push userbobowencode@gmail.com
push dateFri, 07 Jun 2019 11:10:59 +0000
treeherdermozilla-inbound@2195b79ea888 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel
bugs1464032
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1464032 Part 2: Remove LoadEvent and replace with DoWithEvent. r=jrmuizel This patch modifies DoWithEvent so that we can more easily have a DoWithEventFromStream callable from outside of Moz2D similar to LoadEventFromStream. We will add that in a later patch for the new EventRingBuffer. It also changes the only user of LoadEventFromStream over to it, so we can can get rid of it and LoadEvent entirely.
gfx/2d/InlineTranslator.cpp
gfx/2d/RecordedEvent.cpp
gfx/2d/RecordedEvent.h
gfx/2d/RecordedEventImpl.h
layout/printing/DrawEventRecorder.h
layout/printing/PrintTranslator.cpp
--- a/gfx/2d/InlineTranslator.cpp
+++ b/gfx/2d/InlineTranslator.cpp
@@ -64,17 +64,17 @@ bool InlineTranslator::TranslateRecordin
     return false;
   }
 
   int32_t eventType;
   ReadElement(reader, eventType);
   while (reader.good()) {
     bool success = RecordedEvent::DoWithEvent(
         reader, static_cast<RecordedEvent::EventType>(eventType),
-        [&](RecordedEvent* recordedEvent) {
+        [&](RecordedEvent* recordedEvent) -> bool {
           // Make sure that the whole event was read from the stream
           // successfully.
           if (!reader.good()) {
             mError = " READ";
             return false;
           }
 
           if (!recordedEvent->PlayEvent(this)) {
--- a/gfx/2d/RecordedEvent.cpp
+++ b/gfx/2d/RecordedEvent.cpp
@@ -14,24 +14,21 @@
 #include "ScaledFontBase.h"
 #include "SFNTData.h"
 
 namespace mozilla {
 namespace gfx {
 
 using namespace std;
 
-RecordedEvent* RecordedEvent::LoadEventFromStream(std::istream& aStream,
-                                                  EventType aType) {
-  return LoadEvent(aStream, aType);
-}
-
-RecordedEvent* RecordedEvent::LoadEventFromStream(EventStream& aStream,
-                                                  EventType aType) {
-  return LoadEvent(aStream, aType);
+/* static */
+bool RecordedEvent::DoWithEventFromStream(
+    EventStream& aStream, EventType aType,
+    const std::function<bool(RecordedEvent*)>& aAction) {
+  return DoWithEvent(aStream, aType, aAction);
 }
 
 string RecordedEvent::GetEventName(EventType aType) {
   switch (aType) {
     case DRAWTARGETCREATION:
       return "DrawTarget Creation";
     case DRAWTARGETDESTRUCTION:
       return "DrawTarget Destruction";
--- a/gfx/2d/RecordedEvent.h
+++ b/gfx/2d/RecordedEvent.h
@@ -194,16 +194,17 @@ struct MemStream {
   MemStream() : mData(nullptr), mLength(0), mCapacity(0) {}
   ~MemStream() { free(mData); }
 };
 
 class EventStream {
  public:
   virtual void write(const char* aData, size_t aSize) = 0;
   virtual void read(char* aOut, size_t aSize) = 0;
+  virtual bool good() = 0;
 };
 
 class RecordedEvent {
  public:
   enum EventType {
     DRAWTARGETCREATION = 0,
     DRAWTARGETDESTRUCTION,
     FILLRECT,
@@ -293,27 +294,21 @@ class RecordedEvent {
   virtual ReferencePtr GetObjectRef() const = 0;
 
   virtual ReferencePtr GetDestinedDT() { return nullptr; }
 
   void OutputSimplePatternInfo(const PatternStorage& aStorage,
                                std::stringstream& aOutput) const;
 
   template <class S>
-  static RecordedEvent* LoadEvent(S& aStream, EventType aType);
-  static RecordedEvent* LoadEventFromStream(std::istream& aStream,
-                                            EventType aType);
-  static RecordedEvent* LoadEventFromStream(EventStream& aStream,
-                                            EventType aType);
-
-  // An alternative to LoadEvent that avoids a heap allocation for the event.
-  // This accepts a callable `f' that will take a RecordedEvent* as a single
-  // parameter
-  template <class S, class F>
-  static bool DoWithEvent(S& aStream, EventType aType, F f);
+  static bool DoWithEvent(S& aStream, EventType aType,
+                          const std::function<bool(RecordedEvent*)>& aAction);
+  static bool DoWithEventFromStream(
+      EventStream& aStream, EventType aType,
+      const std::function<bool(RecordedEvent*)>& aAction);
 
   EventType GetType() const { return (EventType)mType; }
 
  protected:
   friend class DrawEventRecorderPrivate;
   friend class DrawEventRecorderFile;
   friend class DrawEventRecorderMemory;
   static void RecordUnscaledFont(UnscaledFont* aUnscaledFont,
--- a/gfx/2d/RecordedEventImpl.h
+++ b/gfx/2d/RecordedEventImpl.h
@@ -3481,20 +3481,16 @@ inline void RecordedFilterNodeSetInput::
     aStringStream << "Filter: " << mInputFilter;
   } else {
     aStringStream << "Surface: " << mInputSurface;
   }
 
   aStringStream << ")";
 }
 
-#define LOAD_EVENT_TYPE(_typeenum, _class) \
-  case _typeenum:                          \
-    return new _class(aStream)
-
 #define FOR_EACH_EVENT(f)                                          \
   f(DRAWTARGETCREATION, RecordedDrawTargetCreation);               \
   f(DRAWTARGETDESTRUCTION, RecordedDrawTargetDestruction);         \
   f(FILLRECT, RecordedFillRect);                                   \
   f(STROKERECT, RecordedStrokeRect);                               \
   f(STROKELINE, RecordedStrokeLine);                               \
   f(CLEARRECT, RecordedClearRect);                                 \
   f(COPYSURFACE, RecordedCopySurface);                             \
@@ -3532,33 +3528,26 @@ inline void RecordedFilterNodeSetInput::
   f(PUSHLAYER, RecordedPushLayer);                                 \
   f(PUSHLAYERWITHBLEND, RecordedPushLayerWithBlend);               \
   f(POPLAYER, RecordedPopLayer);                                   \
   f(UNSCALEDFONTCREATION, RecordedUnscaledFontCreation);           \
   f(UNSCALEDFONTDESTRUCTION, RecordedUnscaledFontDestruction);     \
   f(INTOLUMINANCE, RecordedIntoLuminanceSource);                   \
   f(EXTERNALSURFACECREATION, RecordedExternalSurfaceCreation);
 
-template <class S>
-RecordedEvent* RecordedEvent::LoadEvent(S& aStream, EventType aType) {
-  switch (aType) {
-    FOR_EACH_EVENT(LOAD_EVENT_TYPE)
-    default:
-      return nullptr;
-  }
-}
-
 #define DO_WITH_EVENT_TYPE(_typeenum, _class) \
   case _typeenum: {                           \
     auto e = _class(aStream);                 \
-    return f(&e);                             \
+    return aAction(&e);                       \
   }
 
-template <class S, class F>
-bool RecordedEvent::DoWithEvent(S& aStream, EventType aType, F f) {
+template <class S>
+bool RecordedEvent::DoWithEvent(
+    S& aStream, EventType aType,
+    const std::function<bool(RecordedEvent*)>& aAction) {
   switch (aType) {
     FOR_EACH_EVENT(DO_WITH_EVENT_TYPE)
     default:
       return false;
   }
 }
 
 }  // namespace gfx
--- a/layout/printing/DrawEventRecorder.h
+++ b/layout/printing/DrawEventRecorder.h
@@ -100,17 +100,17 @@ class PRFileDescStream final : public mo
       return;
     }
 
     Flush();
     PRInt32 res = PR_Read(mFd, static_cast<void*>(aOut), aSize);
     mGood = res >= 0 && (static_cast<size_t>(res) == aSize);
   }
 
-  bool good() { return mGood; }
+  bool good() final { return mGood; }
 
  private:
   size_t AvailableBufferSpace() { return kBufferSize - mBufferPos; }
 
   void WriteToBuffer(const char* aData, size_t aSize) {
     MOZ_ASSERT(aSize <= AvailableBufferSpace());
     memcpy(mBuffer.get() + mBufferPos, aData, aSize);
     mBufferPos += aSize;
--- a/layout/printing/PrintTranslator.cpp
+++ b/layout/printing/PrintTranslator.cpp
@@ -41,25 +41,28 @@ bool PrintTranslator::TranslateRecording
   ReadElement(aRecording, minorRevision);
   if (minorRevision > kMinorRevision) {
     return false;
   }
 
   int32_t eventType;
   ReadElement(aRecording, eventType);
   while (aRecording.good()) {
-    UniquePtr<RecordedEvent> recordedEvent(RecordedEvent::LoadEventFromStream(
-        aRecording, static_cast<RecordedEvent::EventType>(eventType)));
+    bool success = RecordedEvent::DoWithEventFromStream(
+        aRecording, static_cast<RecordedEvent::EventType>(eventType),
+        [&](RecordedEvent* recordedEvent) -> bool {
+          // Make sure that the whole event was read from the stream.
+          if (!aRecording.good()) {
+            return false;
+          }
 
-    // Make sure that the whole event was read from the stream successfully.
-    if (!aRecording.good() || !recordedEvent) {
-      return false;
-    }
+          return recordedEvent->PlayEvent(this);
+        });
 
-    if (!recordedEvent->PlayEvent(this)) {
+    if (!success) {
       return false;
     }
 
     ReadElement(aRecording, eventType);
   }
 
   return true;
 }