Bug 1641886 [wpt PR 23851] - Support interpolating contain-intrinsic-size, a=testonly
[gecko.git] / gfx / 2d / InlineTranslator.cpp
blob9ace1d8d31d132c9dc725b353120a16c6e3fbe1b
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/. */
7 #include "InlineTranslator.h"
8 #include "RecordedEventImpl.h"
9 #include "DrawEventRecorder.h"
11 #include "gfxContext.h"
12 #include "nsDeviceContext.h"
13 #include "mozilla/gfx/RecordingTypes.h"
14 #include "mozilla/UniquePtr.h"
16 using namespace mozilla::gfx;
18 namespace mozilla::gfx {
20 InlineTranslator::InlineTranslator() : mFontContext(nullptr) {}
22 InlineTranslator::InlineTranslator(DrawTarget* aDT, void* aFontContext)
23 : mBaseDT(aDT), mFontContext(aFontContext) {}
25 bool InlineTranslator::TranslateRecording(char* aData, size_t aLen) {
26 // an istream like class for reading from memory
27 struct MemReader {
28 MemReader(char* aData, size_t aLen) : mData(aData), mEnd(aData + aLen) {}
29 void read(char* s, std::streamsize n) {
30 if (n <= (mEnd - mData)) {
31 memcpy(s, mData, n);
32 mData += n;
33 } else {
34 // We've requested more data than is available
35 // set the Reader into an eof state
36 SetIsBad();
39 bool eof() { return mData > mEnd; }
40 bool good() { return !eof(); }
41 void SetIsBad() { mData = mEnd + 1; }
43 char* mData;
44 char* mEnd;
46 MemReader reader(aData, aLen);
48 uint32_t magicInt;
49 ReadElement(reader, magicInt);
50 if (magicInt != mozilla::gfx::kMagicInt) {
51 mError = "Magic";
52 return false;
55 uint16_t majorRevision;
56 ReadElement(reader, majorRevision);
57 if (majorRevision != kMajorRevision) {
58 mError = "Major";
59 return false;
62 uint16_t minorRevision;
63 ReadElement(reader, minorRevision);
64 if (minorRevision > kMinorRevision) {
65 mError = "Minor";
66 return false;
69 int32_t eventType;
70 ReadElement(reader, eventType);
71 while (reader.good()) {
72 bool success = RecordedEvent::DoWithEvent(
73 reader, static_cast<RecordedEvent::EventType>(eventType),
74 [&](RecordedEvent* recordedEvent) -> bool {
75 // Make sure that the whole event was read from the stream
76 // successfully.
77 if (!reader.good()) {
78 mError = " READ";
79 return false;
82 if (!recordedEvent->PlayEvent(this)) {
83 mError = " PLAY";
84 return false;
87 return true;
88 });
89 if (!success) {
90 mError = RecordedEvent::GetEventName(
91 static_cast<RecordedEvent::EventType>(eventType)) +
92 mError;
93 return false;
96 ReadElement(reader, eventType);
99 return true;
102 already_AddRefed<DrawTarget> InlineTranslator::CreateDrawTarget(
103 ReferencePtr aRefPtr, const gfx::IntSize& aSize,
104 gfx::SurfaceFormat aFormat) {
105 MOZ_ASSERT(mBaseDT, "mBaseDT has not been initialized.");
107 RefPtr<DrawTarget> drawTarget = mBaseDT;
108 AddDrawTarget(aRefPtr, drawTarget);
109 return drawTarget.forget();
112 } // namespace mozilla::gfx