/
swf.h
369 lines (333 loc) · 9.97 KB
/
swf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**************************************************************************
Lightspark, a free flash player implementation
Copyright (C) 2009-2011 Alessandro Pignotti (a.pignotti@sssup.it)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#ifndef SWF_H
#define SWF_H
#include "compat.h"
#include <iostream>
#include <fstream>
#include <list>
#include <map>
#include <semaphore.h>
#include <string>
#include "swftypes.h"
#include "frame.h"
#include "scripting/flashdisplay.h"
#include "scripting/flashnet.h"
#include "scripting/flashsystem.h"
#include "timer.h"
#include "backends/audio.h"
#include "backends/config.h"
#include "backends/graphics.h"
#include "backends/pluginmanager.h"
#include "backends/security.h"
#include "backends/urlutils.h"
#include "backends/extscriptobject.h"
#include "platforms/pluginutils.h"
#include <GL/glew.h>
#ifndef WIN32
#include <GL/glx.h>
#else
//#include <windows.h>
#endif
namespace lightspark
{
class DownloadManager;
class DisplayListTag;
class DictionaryTag;
class ABCVm;
class InputThread;
class RenderThread;
class ParseThread;
class Tag;
//RootMovieClip is used as a ThreadJob for timed rendering purpose
class RootMovieClip: public MovieClip, public ITickJob
{
friend class ParseThread;
protected:
Mutex mutex;
bool initialized;
URLInfo origin;
void tick();
private:
//Semaphore to wait for new frames to be available
sem_t new_frame;
bool parsingIsFailed;
RGB Background;
Spinlock dictSpinlock;
std::list < DictionaryTag* > dictionary;
//frameSize and frameRate are valid only after the header has been parsed
RECT frameSize;
float frameRate;
//Frames mutex (shared with drawing thread)
Mutex mutexFrames;
bool toBind;
tiny_string bindName;
Mutex mutexChildrenClips;
std::set<MovieClip*> childrenClips;
public:
RootMovieClip(LoaderInfo* li, bool isSys=false);
~RootMovieClip();
uint32_t version;
uint32_t fileLength;
RGB getBackground();
void setBackground(const RGB& bg);
void setFrameSize(const RECT& f);
RECT getFrameSize() const;
float getFrameRate() const;
void setFrameRate(float f);
void setFrameCount(int f);
void setOnStage(bool staged);
void addToDictionary(DictionaryTag* r);
DictionaryTag* dictionaryLookup(int id);
void addToFrame(DisplayListTag* t);
void addToFrame(ControlTag* t);
void labelCurrentFrame(const STRING& name);
void commitFrame(bool another);
void revertFrame();
void Render(bool maskEnabled);
void parsingFailed();
bool getBounds(number_t& xmin, number_t& xmax, number_t& ymin, number_t& ymax) const;
void bindToName(const tiny_string& n);
void initialize();
void DLL_PUBLIC setOrigin(const tiny_string& u, const tiny_string& filename="");
URLInfo& getOrigin() DLL_PUBLIC { return origin; };
/* ASObject* getVariableByQName(const tiny_string& name, const tiny_string& ns);
void setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o);
void setVariableByMultiname(multiname& name, ASObject* o);
void setVariableByString(const std::string& s, ASObject* o);*/
void registerChildClip(MovieClip* clip);
void unregisterChildClip(MovieClip* clip);
static RootMovieClip* getInstance(LoaderInfo* li);
};
class ThreadProfile
{
private:
Mutex mutex;
class ProfilingData
{
public:
uint32_t index;
uint32_t timing;
std::string tag;
ProfilingData(uint32_t i, uint32_t t):index(i),timing(t){}
};
std::deque<ProfilingData> data;
RGB color;
int32_t len;
uint32_t tickCount;
public:
ThreadProfile(const RGB& c,uint32_t l):mutex("ThreadProfile"),color(c),len(l),tickCount(0){}
void accountTime(uint32_t time);
void setTag(const std::string& tag);
void tick();
void plot(uint32_t max, FTFont* font);
};
class SystemState: public RootMovieClip
{
private:
class EngineCreator: public IThreadJob
{
public:
EngineCreator()
{
destroyMe=true;
}
void execute();
void threadAbort();
void jobFence(){}
};
friend class SystemState::EngineCreator;
ThreadPool* threadPool;
TimerThread* timerThread;
sem_t terminated;
float renderRate;
bool error;
bool shutdown;
RenderThread* renderThread;
InputThread* inputThread;
NPAPI_params npapiParams;
ENGINE engine;
void startRenderTicks();
/**
Create the rendering and input engines
@pre engine and useAVM2 are known
*/
void createEngines();
/**
Destroys all the engines used in lightspark: timer, thread pool, vm...
*/
#ifdef COMPILE_PLUGIN
static void delayedCreation(SystemState* th);
static void delayedStopping(SystemState* th);
#endif
void stopEngines();
//Useful to wait for complete download of the SWF
Semaphore fileDumpAvailable;
tiny_string dumpedSWFPath;
bool waitingForDump;
//Data for handling Gnash fallback
enum VMVERSION { VMNONE=0, AVM1, AVM2 };
VMVERSION vmVersion;
pid_t childPid;
bool useGnashFallback;
void setParameters(ASObject* p);
/*
Used to keep a copy of the FlashVars, it's useful when gnash fallback is used
*/
std::string rawParameters;
std::string rawCookies;
char cookiesFileName[32]; // "/tmp/lightsparkcookiesXXXXXX"
URLInfo url;
Spinlock profileDataSpinlock;
Mutex mutexEnterFrameListeners;
std::set<DisplayObject*> enterFrameListeners;
/*
The head of the invalidate queue
*/
_NR<DisplayObject> invalidateQueueHead;
/*
The tail of the invalidate queue
*/
_NR<DisplayObject> invalidateQueueTail;
/*
The lock for the invalidate queue
*/
Spinlock invalidateQueueLock;
/*
Vector to keep track of the created tags
*/
std::vector<Tag*> tagsStorage;
/*
The lock for the tags storage
*/
Spinlock tagsStorageLock;
#ifdef PROFILING_SUPPORT
/*
Output file for the profiling data
*/
tiny_string profOut;
#endif
public:
void setURL(const tiny_string& url) DLL_PUBLIC;
ENGINE getEngine() DLL_PUBLIC { return engine; };
//Interative analysis flags
bool showProfilingData;
std::string errorCause;
void setError(const std::string& c);
bool hasError() { return error; }
std::string& getErrorCause() { return errorCause; }
bool shouldTerminate() const;
bool isShuttingDown() const DLL_PUBLIC;
bool isOnError() const;
void setShutdownFlag() DLL_PUBLIC;
void tick();
void wait() DLL_PUBLIC;
RenderThread* getRenderThread() const { return renderThread; }
InputThread* getInputThread() const { return inputThread; }
void setParamsAndEngine(ENGINE e, NPAPI_params* p) DLL_PUBLIC;
void setDownloadedPath(const tiny_string& p) DLL_PUBLIC;
void enableGnashFallback() DLL_PUBLIC;
void needsAVM2(bool n);
//Be careful, SystemState constructor does some global initialization that must be done
//before any other thread gets started
SystemState(ParseThread* p, uint32_t fileSize) DLL_PUBLIC;
void finalize();
~SystemState();
//Performance profiling
ThreadProfile* allocateProfiler(const RGB& color);
std::list<ThreadProfile> profilingData;
Stage* stage;
ABCVm* currentVm;
Config* config;
PluginManager* pluginManager;
AudioManager* audioManager;
//Application starting time in milliseconds
uint64_t startTime;
//Class map
std::map<QName, Class_base*> classes;
//Flags for command line options
bool useInterpreter;
bool useJit;
void parseParametersFromFile(const char* f) DLL_PUBLIC;
void parseParametersFromFlashvars(const char* vars) DLL_PUBLIC;
void setCookies(const char* c) DLL_PUBLIC;
void addJob(IThreadJob* j) DLL_PUBLIC;
void addTick(uint32_t tickTime, ITickJob* job);
void addWait(uint32_t waitTime, ITickJob* job);
bool removeJob(ITickJob* job);
void setRenderRate(float rate);
float getRenderRate();
/*
This is not supposed to be used in the VM, it's only useful to create the Downloader when plugin is being used
So don't create a smart reference
*/
LoaderInfo* getLoaderInfo() const { return loaderInfo.getPtr(); }
//Stuff to be done once for process and not for plugin instance
static void staticInit() DLL_PUBLIC;
static void staticDeinit() DLL_PUBLIC;
DownloadManager* downloadManager;
IntervalManager* intervalManager;
SecurityManager* securityManager;
ExtScriptObject* extScriptObject;
enum SCALE_MODE { EXACT_FIT=0, NO_BORDER=1, NO_SCALE=2, SHOW_ALL=3 };
SCALE_MODE scaleMode;
//Static AS class properties
//NAMING: static$CLASSNAME$$PROPERTYNAME$
// NetConnection
ObjectEncoding::ENCODING staticNetConnectionDefaultObjectEncoding;
ObjectEncoding::ENCODING staticByteArrayDefaultObjectEncoding;
//enterFrame event management
void registerEnterFrameListener(DisplayObject* clip);
void unregisterEnterFrameListener(DisplayObject* clip);
//tags management
void registerTag(Tag* t);
//Invalidation queue management
void addToInvalidateQueue(_R<DisplayObject> d);
void flushInvalidationQueue();
#ifdef PROFILING_SUPPORT
void setProfilingOutput(const tiny_string& t) DLL_PUBLIC;
const tiny_string& getProfilingOutput() const;
std::vector<ABCContext*> contextes;
void saveProfilingInformation();
#endif
};
class ParseThread: public IThreadJob
{
private:
std::istream& f;
std::streambuf* zlibFilter;
std::streambuf* backend;
sem_t ended;
bool isEnded;
void execute();
void threadAbort();
void jobFence() {};
/*
parseHeader takes the first four characters as argument
*/
enum FILE_TYPE { NONE=0, SWF, COMPRESSED_SWF, PNG, JPEG };
void parseSWFHeader();
void checkType(uint8_t c1, uint8_t c2, uint8_t c3, uint8_t c4);
FILE_TYPE fileType;
public:
RootMovieClip* root;
int version;
bool useAVM2;
ParseThread(RootMovieClip* r,std::istream& in) DLL_PUBLIC;
~ParseThread();
};
};
extern TLSDATA lightspark::SystemState* sys DLL_PUBLIC;
#endif