1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "RecordedEventImpl.h"
8 
9 #include "PathRecording.h"
10 #include "RecordingTypes.h"
11 #include "Tools.h"
12 #include "Filters.h"
13 #include "Logging.h"
14 #include "ScaledFontBase.h"
15 #include "SFNTData.h"
16 
17 namespace mozilla {
18 namespace gfx {
19 
20 /* static */
DoWithEventFromStream(EventStream & aStream,EventType aType,const std::function<bool (RecordedEvent *)> & aAction)21 bool RecordedEvent::DoWithEventFromStream(
22     EventStream& aStream, EventType aType,
23     const std::function<bool(RecordedEvent*)>& aAction) {
24   return DoWithEvent(aStream, aType, aAction);
25 }
26 
27 /* static */
DoWithEventFromStream(EventRingBuffer & aStream,EventType aType,const std::function<bool (RecordedEvent *)> & aAction)28 bool RecordedEvent::DoWithEventFromStream(
29     EventRingBuffer& aStream, EventType aType,
30     const std::function<bool(RecordedEvent*)>& aAction) {
31   return DoWithEvent(aStream, aType, aAction);
32 }
33 
GetEventName(EventType aType)34 std::string RecordedEvent::GetEventName(EventType aType) {
35   switch (aType) {
36     case DRAWTARGETCREATION:
37       return "DrawTarget Creation";
38     case DRAWTARGETDESTRUCTION:
39       return "DrawTarget Destruction";
40     case FILLRECT:
41       return "FillRect";
42     case STROKERECT:
43       return "StrokeRect";
44     case STROKELINE:
45       return "StrokeLine";
46     case CLEARRECT:
47       return "ClearRect";
48     case COPYSURFACE:
49       return "CopySurface";
50     case SETTRANSFORM:
51       return "SetTransform";
52     case PUSHCLIP:
53       return "PushClip";
54     case PUSHCLIPRECT:
55       return "PushClipRect";
56     case POPCLIP:
57       return "PopClip";
58     case FILL:
59       return "Fill";
60     case FILLGLYPHS:
61       return "FillGlyphs";
62     case MASK:
63       return "Mask";
64     case STROKE:
65       return "Stroke";
66     case DRAWSURFACE:
67       return "DrawSurface";
68     case DRAWDEPENDENTSURFACE:
69       return "DrawDependentSurface";
70     case DRAWSURFACEWITHSHADOW:
71       return "DrawSurfaceWithShadow";
72     case DRAWFILTER:
73       return "DrawFilter";
74     case PATHCREATION:
75       return "PathCreation";
76     case PATHDESTRUCTION:
77       return "PathDestruction";
78     case SOURCESURFACECREATION:
79       return "SourceSurfaceCreation";
80     case SOURCESURFACEDESTRUCTION:
81       return "SourceSurfaceDestruction";
82     case FILTERNODECREATION:
83       return "FilterNodeCreation";
84     case FILTERNODEDESTRUCTION:
85       return "FilterNodeDestruction";
86     case GRADIENTSTOPSCREATION:
87       return "GradientStopsCreation";
88     case GRADIENTSTOPSDESTRUCTION:
89       return "GradientStopsDestruction";
90     case SNAPSHOT:
91       return "Snapshot";
92     case SCALEDFONTCREATION:
93       return "ScaledFontCreation";
94     case SCALEDFONTDESTRUCTION:
95       return "ScaledFontDestruction";
96     case MASKSURFACE:
97       return "MaskSurface";
98     case FILTERNODESETATTRIBUTE:
99       return "SetAttribute";
100     case FILTERNODESETINPUT:
101       return "SetInput";
102     case CREATESIMILARDRAWTARGET:
103       return "CreateSimilarDrawTarget";
104     case FONTDATA:
105       return "FontData";
106     case FONTDESC:
107       return "FontDescriptor";
108     case PUSHLAYER:
109       return "PushLayer";
110     case POPLAYER:
111       return "PopLayer";
112     case UNSCALEDFONTCREATION:
113       return "UnscaledFontCreation";
114     case UNSCALEDFONTDESTRUCTION:
115       return "UnscaledFontDestruction";
116     case EXTERNALSURFACECREATION:
117       return "ExternalSourceSurfaceCreation";
118     case LINK:
119       return "Link";
120     default:
121       return "Unknown";
122   }
123 }
124 
125 template <class S>
RecordUnscaledFontImpl(UnscaledFont * aUnscaledFont,S & aOutput)126 void RecordedEvent::RecordUnscaledFontImpl(UnscaledFont* aUnscaledFont,
127                                            S& aOutput) {
128   RecordedFontData fontData(aUnscaledFont);
129   RecordedFontDetails fontDetails;
130   if (fontData.GetFontDetails(fontDetails)) {
131     // Try to serialise the whole font, just in case this is a web font that
132     // is not present on the system.
133     WriteElement(aOutput, fontData.mType);
134     fontData.RecordToStream(aOutput);
135 
136     auto r = RecordedUnscaledFontCreation(aUnscaledFont, fontDetails);
137     WriteElement(aOutput, r.mType);
138     r.RecordToStream(aOutput);
139   } else {
140     // If that fails, record just the font description and try to load it from
141     // the system on the other side.
142     RecordedFontDescriptor fontDesc(aUnscaledFont);
143     if (fontDesc.IsValid()) {
144       WriteElement(aOutput, fontDesc.RecordedEvent::mType);
145       fontDesc.RecordToStream(aOutput);
146     } else {
147       gfxWarning()
148           << "DrawTargetRecording::FillGlyphs failed to serialise UnscaledFont";
149     }
150   }
151 }
152 
RecordUnscaledFont(UnscaledFont * aUnscaledFont,std::ostream * aOutput)153 void RecordedEvent::RecordUnscaledFont(UnscaledFont* aUnscaledFont,
154                                        std::ostream* aOutput) {
155   RecordUnscaledFontImpl(aUnscaledFont, *aOutput);
156 }
157 
RecordUnscaledFont(UnscaledFont * aUnscaledFont,MemStream & aOutput)158 void RecordedEvent::RecordUnscaledFont(UnscaledFont* aUnscaledFont,
159                                        MemStream& aOutput) {
160   RecordUnscaledFontImpl(aUnscaledFont, aOutput);
161 }
162 
CreateDrawTarget(ReferencePtr aRefPtr,const IntSize & aSize,SurfaceFormat aFormat)163 already_AddRefed<DrawTarget> Translator::CreateDrawTarget(
164     ReferencePtr aRefPtr, const IntSize& aSize, SurfaceFormat aFormat) {
165   RefPtr<DrawTarget> newDT =
166       GetReferenceDrawTarget()->CreateSimilarDrawTarget(aSize, aFormat);
167   AddDrawTarget(aRefPtr, newDT);
168   return newDT.forget();
169 }
170 
171 }  // namespace gfx
172 }  // namespace mozilla
173