summaryrefslogtreecommitdiffstats
path: root/gfx/2d/DrawEventRecorder.cpp
blob: 0e20b8b5ac5ddebebbecc907a756929dcefa7605 (plain)
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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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/. */

#include "DrawEventRecorder.h"
#include "PathRecording.h"
#include "RecordingTypes.h"

namespace mozilla {
namespace gfx {

using namespace std;

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;

  WriteHeader();
}

DrawEventRecorderFile::~DrawEventRecorderFile()
{
  mOutputFile.close();
}

void
DrawEventRecorderFile::Flush()
{
  mOutputFile.flush();
}

bool
DrawEventRecorderFile::IsOpen()
{
  return mOutputFile.is_open();
}

void
DrawEventRecorderFile::OpenNew(const char *aFilename)
{
  MOZ_ASSERT(!mOutputFile.is_open());

  mOutputFile.open(aFilename, ofstream::binary);
  WriteHeader();
}

void
DrawEventRecorderFile::Close()
{
  MOZ_ASSERT(mOutputFile.is_open());

  mOutputFile.close();
}

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