Line data Source code
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 "InlineTranslator.h"
8 : #include "RecordedEventImpl.h"
9 : #include "DrawEventRecorder.h"
10 :
11 : #include "gfxContext.h"
12 : #include "nsDeviceContext.h"
13 : #include "mozilla/gfx/RecordingTypes.h"
14 : #include "mozilla/UniquePtr.h"
15 :
16 : using namespace mozilla::gfx;
17 :
18 : namespace mozilla {
19 : namespace gfx {
20 :
21 0 : InlineTranslator::InlineTranslator(DrawTarget* aDT, void* aFontContext)
22 : : mBaseDT(aDT)
23 0 : , mFontContext(aFontContext)
24 : {
25 0 : }
26 :
27 : bool
28 0 : InlineTranslator::TranslateRecording(char *aData, size_t aLen)
29 : {
30 : // an istream like class for reading from memory
31 : struct MemReader {
32 0 : MemReader(char *aData, size_t aLen) : mData(aData), mEnd(aData + aLen) {}
33 0 : void read(char* s, streamsize n) {
34 0 : if (n <= (mEnd - mData)) {
35 0 : memcpy(s, mData, n);
36 0 : mData += n;
37 : } else {
38 : // We've requested more data than is available
39 : // set the Reader into an eof state
40 0 : mData = mEnd + 1;
41 : }
42 0 : }
43 0 : bool eof() {
44 0 : return mData > mEnd;
45 : }
46 0 : bool good() {
47 0 : return !eof();
48 : }
49 :
50 : char *mData;
51 : char *mEnd;
52 : };
53 0 : MemReader reader(aData, aLen);
54 :
55 : uint32_t magicInt;
56 0 : ReadElement(reader, magicInt);
57 0 : if (magicInt != mozilla::gfx::kMagicInt) {
58 0 : return false;
59 : }
60 :
61 : uint16_t majorRevision;
62 0 : ReadElement(reader, majorRevision);
63 0 : if (majorRevision != kMajorRevision) {
64 0 : return false;
65 : }
66 :
67 : uint16_t minorRevision;
68 0 : ReadElement(reader, minorRevision);
69 0 : if (minorRevision > kMinorRevision) {
70 0 : return false;
71 : }
72 :
73 : int32_t eventType;
74 0 : ReadElement(reader, eventType);
75 0 : while (reader.good()) {
76 : UniquePtr<RecordedEvent> recordedEvent(
77 : RecordedEvent::LoadEvent(reader,
78 0 : static_cast<RecordedEvent::EventType>(eventType)));
79 :
80 : // Make sure that the whole event was read from the stream successfully.
81 0 : if (!reader.good() || !recordedEvent) {
82 0 : return false;
83 : }
84 :
85 0 : if (!recordedEvent->PlayEvent(this)) {
86 0 : return false;
87 : }
88 :
89 0 : ReadElement(reader, eventType);
90 : }
91 :
92 0 : return true;
93 : }
94 :
95 : already_AddRefed<DrawTarget>
96 0 : InlineTranslator::CreateDrawTarget(ReferencePtr aRefPtr,
97 : const gfx::IntSize &aSize,
98 : gfx::SurfaceFormat aFormat)
99 : {
100 0 : RefPtr<DrawTarget> drawTarget = mBaseDT;
101 0 : AddDrawTarget(aRefPtr, drawTarget);
102 0 : return drawTarget.forget();
103 : }
104 :
105 : } // namespace gfx
106 : } // namespace mozilla
|