2017-04-26 23:31:59 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "InlineTranslator.h"
|
2017-07-04 18:53:35 +03:00
|
|
|
#include "RecordedEventImpl.h"
|
|
|
|
#include "DrawEventRecorder.h"
|
2017-04-26 23:31:59 +03:00
|
|
|
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "nsDeviceContext.h"
|
|
|
|
#include "mozilla/gfx/RecordingTypes.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
namespace mozilla::gfx {
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2019-09-10 05:06:28 +03:00
|
|
|
InlineTranslator::InlineTranslator() : mFontContext(nullptr) {}
|
|
|
|
|
2017-05-18 04:56:58 +03:00
|
|
|
InlineTranslator::InlineTranslator(DrawTarget* aDT, void* aFontContext)
|
|
|
|
: mBaseDT(aDT), mFontContext(aFontContext) {}
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-07-04 22:36:05 +03:00
|
|
|
bool InlineTranslator::TranslateRecording(char* aData, size_t aLen) {
|
|
|
|
// an istream like class for reading from memory
|
|
|
|
struct MemReader {
|
|
|
|
MemReader(char* aData, size_t aLen) : mData(aData), mEnd(aData + aLen) {}
|
2017-07-17 21:54:50 +03:00
|
|
|
void read(char* s, std::streamsize n) {
|
2017-07-04 22:36:05 +03:00
|
|
|
if (n <= (mEnd - mData)) {
|
|
|
|
memcpy(s, mData, n);
|
|
|
|
mData += n;
|
|
|
|
} else {
|
|
|
|
// We've requested more data than is available
|
|
|
|
// set the Reader into an eof state
|
2019-08-05 16:08:30 +03:00
|
|
|
SetIsBad();
|
2017-07-04 22:36:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bool eof() { return mData > mEnd; }
|
|
|
|
bool good() { return !eof(); }
|
2019-08-05 16:08:30 +03:00
|
|
|
void SetIsBad() { mData = mEnd + 1; }
|
2017-07-04 22:36:05 +03:00
|
|
|
|
|
|
|
char* mData;
|
|
|
|
char* mEnd;
|
|
|
|
};
|
|
|
|
MemReader reader(aData, aLen);
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
uint32_t magicInt;
|
2017-07-04 22:36:05 +03:00
|
|
|
ReadElement(reader, magicInt);
|
2017-04-26 23:31:59 +03:00
|
|
|
if (magicInt != mozilla::gfx::kMagicInt) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = "Magic";
|
2017-04-26 23:31:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t majorRevision;
|
2017-07-04 22:36:05 +03:00
|
|
|
ReadElement(reader, majorRevision);
|
2017-04-26 23:31:59 +03:00
|
|
|
if (majorRevision != kMajorRevision) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = "Major";
|
2017-04-26 23:31:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t minorRevision;
|
2017-07-04 22:36:05 +03:00
|
|
|
ReadElement(reader, minorRevision);
|
2017-04-26 23:31:59 +03:00
|
|
|
if (minorRevision > kMinorRevision) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = "Minor";
|
2017-04-26 23:31:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t eventType;
|
2017-07-04 22:36:05 +03:00
|
|
|
ReadElement(reader, eventType);
|
|
|
|
while (reader.good()) {
|
2017-08-18 20:03:09 +03:00
|
|
|
bool success = RecordedEvent::DoWithEvent(
|
|
|
|
reader, static_cast<RecordedEvent::EventType>(eventType),
|
2018-12-02 17:13:56 +03:00
|
|
|
[&](RecordedEvent* recordedEvent) -> bool {
|
2017-08-18 20:03:09 +03:00
|
|
|
// Make sure that the whole event was read from the stream
|
|
|
|
// successfully.
|
|
|
|
if (!reader.good()) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = " READ";
|
2017-08-18 20:03:09 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-08-18 20:03:09 +03:00
|
|
|
if (!recordedEvent->PlayEvent(this)) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = " PLAY";
|
2017-08-18 20:03:09 +03:00
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-08-18 20:03:09 +03:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
if (!success) {
|
2018-11-13 13:38:24 +03:00
|
|
|
mError = RecordedEvent::GetEventName(
|
|
|
|
static_cast<RecordedEvent::EventType>(eventType)) +
|
|
|
|
mError;
|
2017-05-06 06:51:35 +03:00
|
|
|
return false;
|
2017-04-26 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-04 22:36:05 +03:00
|
|
|
ReadElement(reader, eventType);
|
2017-04-26 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DrawTarget> InlineTranslator::CreateDrawTarget(
|
|
|
|
ReferencePtr aRefPtr, const gfx::IntSize& aSize,
|
|
|
|
gfx::SurfaceFormat aFormat) {
|
2019-09-10 05:06:28 +03:00
|
|
|
MOZ_ASSERT(mBaseDT, "mBaseDT has not been initialized.");
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
RefPtr<DrawTarget> drawTarget = mBaseDT;
|
2017-05-05 04:33:10 +03:00
|
|
|
AddDrawTarget(aRefPtr, drawTarget);
|
2017-04-26 23:31:59 +03:00
|
|
|
return drawTarget.forget();
|
|
|
|
}
|
|
|
|
|
2020-10-07 23:29:48 +03:00
|
|
|
already_AddRefed<SourceSurface> InlineTranslator::LookupExternalSurface(
|
|
|
|
uint64_t aKey) {
|
|
|
|
if (mExternalSurfaces) {
|
|
|
|
RefPtr<SourceSurface> surface = mExternalSurfaces->Get(aKey);
|
|
|
|
if (surface) {
|
|
|
|
return surface.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDependentSurfaces) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<RecordedDependentSurface> recordedSurface =
|
|
|
|
mDependentSurfaces->Get(aKey);
|
|
|
|
if (!recordedSurface) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DrawTarget> newDT = GetReferenceDrawTarget()->CreateSimilarDrawTarget(
|
|
|
|
recordedSurface->mSize, SurfaceFormat::B8G8R8A8);
|
|
|
|
|
|
|
|
InlineTranslator translator(newDT, nullptr);
|
|
|
|
translator.SetDependentSurfaces(mDependentSurfaces);
|
|
|
|
if (!translator.TranslateRecording((char*)recordedSurface->mRecording.mData,
|
|
|
|
recordedSurface->mRecording.mLen)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SourceSurface> snapshot = newDT->Snapshot();
|
|
|
|
return snapshot.forget();
|
|
|
|
}
|
|
|
|
|
2020-01-18 16:48:34 +03:00
|
|
|
} // namespace mozilla::gfx
|