2018-12-02 17:19:11 +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 "CanvasChild.h"
|
|
|
|
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/gfx/DrawTargetRecording.h"
|
|
|
|
#include "mozilla/gfx/Tools.h"
|
2019-07-10 16:49:08 +03:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
|
|
|
#include "mozilla/gfx/Point.h"
|
2018-12-02 17:19:11 +03:00
|
|
|
#include "mozilla/layers/CanvasDrawEventRecorder.h"
|
|
|
|
#include "RecordedCanvasEventImpl.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2019-07-22 16:12:07 +03:00
|
|
|
class RingBufferWriterServices final
|
|
|
|
: public CanvasEventRingBuffer::WriterServices {
|
|
|
|
public:
|
|
|
|
explicit RingBufferWriterServices(RefPtr<CanvasChild> aCanvasChild)
|
|
|
|
: mCanvasChild(std::move(aCanvasChild)) {}
|
|
|
|
|
|
|
|
~RingBufferWriterServices() final = default;
|
|
|
|
|
|
|
|
bool ReaderClosed() final {
|
|
|
|
return mCanvasChild->GetIPCChannel()->Unsound_IsClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeReader() final { mCanvasChild->ResumeTranslation(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<CanvasChild> mCanvasChild;
|
|
|
|
};
|
2018-12-02 17:19:11 +03:00
|
|
|
|
2019-07-22 16:07:59 +03:00
|
|
|
class SourceSurfaceCanvasRecording final : public gfx::SourceSurface {
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCanvasRecording, final)
|
|
|
|
|
|
|
|
SourceSurfaceCanvasRecording(
|
|
|
|
const RefPtr<gfx::SourceSurface>& aRecordedSuface,
|
|
|
|
CanvasChild* aCanvasChild,
|
|
|
|
const RefPtr<CanvasDrawEventRecorder>& aRecorder)
|
|
|
|
: mRecordedSurface(aRecordedSuface),
|
|
|
|
mCanvasChild(aCanvasChild),
|
|
|
|
mRecorder(aRecorder) {
|
|
|
|
mRecorder->RecordEvent(RecordedAddSurfaceAlias(this, aRecordedSuface));
|
|
|
|
mRecorder->AddStoredObject(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SourceSurfaceCanvasRecording() {
|
2019-09-10 05:06:46 +03:00
|
|
|
ReleaseOnMainThread(std::move(mRecorder), this, std::move(mRecordedSurface),
|
|
|
|
std::move(mCanvasChild));
|
2019-07-22 16:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::SurfaceType GetType() const final { return mRecordedSurface->GetType(); }
|
|
|
|
|
|
|
|
gfx::IntSize GetSize() const final { return mRecordedSurface->GetSize(); }
|
|
|
|
|
|
|
|
gfx::SurfaceFormat GetFormat() const final {
|
|
|
|
return mRecordedSurface->GetFormat();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::DataSourceSurface> GetDataSurface() final {
|
2019-09-10 05:06:46 +03:00
|
|
|
EnsureDataSurfaceOnMainThread();
|
|
|
|
return do_AddRef(mDataSourceSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void GuaranteePersistance() final { EnsureDataSurfaceOnMainThread(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void EnsureDataSurfaceOnMainThread() {
|
|
|
|
// The data can only be retrieved on the main thread.
|
|
|
|
if (!mDataSourceSurface && NS_IsMainThread()) {
|
2019-07-22 16:07:59 +03:00
|
|
|
mDataSourceSurface = mCanvasChild->GetDataSurface(mRecordedSurface);
|
|
|
|
}
|
2019-09-10 05:06:46 +03:00
|
|
|
}
|
2019-07-22 16:07:59 +03:00
|
|
|
|
2019-09-10 05:06:46 +03:00
|
|
|
// Used to ensure that clean-up that requires it is done on the main thread.
|
|
|
|
static void ReleaseOnMainThread(RefPtr<CanvasDrawEventRecorder> aRecorder,
|
|
|
|
ReferencePtr aSurfaceAlias,
|
|
|
|
RefPtr<gfx::SourceSurface> aAliasedSurface,
|
|
|
|
RefPtr<CanvasChild> aCanvasChild) {
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(NewRunnableFunction(
|
|
|
|
"SourceSurfaceCanvasRecording::ReleaseOnMainThread",
|
|
|
|
SourceSurfaceCanvasRecording::ReleaseOnMainThread,
|
|
|
|
std::move(aRecorder), aSurfaceAlias, std::move(aAliasedSurface),
|
|
|
|
std::move(aCanvasChild)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRecorder->RemoveStoredObject(aSurfaceAlias);
|
|
|
|
aRecorder->RecordEvent(RecordedRemoveSurfaceAlias(aSurfaceAlias));
|
|
|
|
aAliasedSurface = nullptr;
|
|
|
|
aCanvasChild = nullptr;
|
|
|
|
aRecorder = nullptr;
|
2019-07-22 16:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<gfx::SourceSurface> mRecordedSurface;
|
|
|
|
RefPtr<CanvasChild> mCanvasChild;
|
|
|
|
RefPtr<CanvasDrawEventRecorder> mRecorder;
|
|
|
|
RefPtr<gfx::DataSourceSurface> mDataSourceSurface;
|
|
|
|
};
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
CanvasChild::CanvasChild(Endpoint<PCanvasChild>&& aEndpoint) {
|
|
|
|
aEndpoint.Bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasChild::~CanvasChild() {}
|
|
|
|
|
|
|
|
void CanvasChild::EnsureRecorder(TextureType aTextureType) {
|
|
|
|
if (!mRecorder) {
|
|
|
|
MOZ_ASSERT(mTextureType == TextureType::Unknown);
|
|
|
|
mTextureType = aTextureType;
|
|
|
|
mRecorder = MakeAndAddRef<CanvasDrawEventRecorder>();
|
|
|
|
SharedMemoryBasic::Handle handle;
|
|
|
|
CrossProcessSemaphoreHandle readerSem;
|
|
|
|
CrossProcessSemaphoreHandle writerSem;
|
|
|
|
mRecorder->Init(OtherPid(), &handle, &readerSem, &writerSem,
|
2019-07-22 16:12:07 +03:00
|
|
|
MakeUnique<RingBufferWriterServices>(this));
|
2018-12-02 17:19:11 +03:00
|
|
|
|
2019-09-06 21:38:25 +03:00
|
|
|
if (CanSend()) {
|
2019-09-10 05:06:28 +03:00
|
|
|
Unused << SendInitTranslator(mTextureType, handle, readerSem, writerSem);
|
2018-12-02 17:19:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(mTextureType == aTextureType,
|
|
|
|
"We only support one remote TextureType currently.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::ActorDestroy(ActorDestroyReason aWhy) {
|
|
|
|
// Explicitly drop our reference to the recorder, because it holds a reference
|
|
|
|
// to us via the ResumeTranslation callback.
|
|
|
|
mRecorder = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::ResumeTranslation() {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (CanSend()) {
|
2018-12-02 17:19:11 +03:00
|
|
|
SendResumeTranslation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:38:25 +03:00
|
|
|
void CanvasChild::Destroy() {
|
|
|
|
if (CanSend()) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
}
|
2018-12-02 17:19:11 +03:00
|
|
|
|
|
|
|
void CanvasChild::OnTextureWriteLock() {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
mHasOutstandingWriteLock = true;
|
|
|
|
mLastWriteLockCheckpoint = mRecorder->CreateCheckpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::OnTextureForwarded() {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
if (mHasOutstandingWriteLock) {
|
|
|
|
mRecorder->RecordEvent(RecordedCanvasFlush());
|
2019-07-22 16:12:07 +03:00
|
|
|
if (!mRecorder->WaitForCheckpoint(mLastWriteLockCheckpoint)) {
|
2018-12-02 17:19:11 +03:00
|
|
|
gfxWarning() << "Timed out waiting for last write lock to be processed.";
|
|
|
|
}
|
|
|
|
|
|
|
|
mHasOutstandingWriteLock = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::EnsureBeginTransaction() {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
if (!mIsInTransaction) {
|
|
|
|
mRecorder->RecordEvent(RecordedCanvasBeginTransaction());
|
|
|
|
mIsInTransaction = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::EndTransaction() {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
if (mIsInTransaction) {
|
|
|
|
mRecorder->RecordEvent(RecordedCanvasEndTransaction());
|
|
|
|
mIsInTransaction = false;
|
2019-09-17 03:15:59 +03:00
|
|
|
mLastNonEmptyTransaction = TimeStamp::NowLoRes();
|
2018-12-02 17:19:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
++mTransactionsSinceGetDataSurface;
|
|
|
|
}
|
|
|
|
|
2019-09-17 03:15:59 +03:00
|
|
|
bool CanvasChild::ShouldBeCleanedUp() const {
|
|
|
|
static const TimeDuration kCleanUpCanvasThreshold =
|
|
|
|
TimeDuration::FromSeconds(10);
|
|
|
|
return TimeStamp::NowLoRes() - mLastNonEmptyTransaction >
|
|
|
|
kCleanUpCanvasThreshold;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
already_AddRefed<gfx::DrawTarget> CanvasChild::CreateDrawTarget(
|
|
|
|
gfx::IntSize aSize, gfx::SurfaceFormat aFormat) {
|
|
|
|
MOZ_ASSERT(mRecorder);
|
|
|
|
|
|
|
|
RefPtr<gfx::DrawTarget> dummyDt = gfx::Factory::CreateDrawTarget(
|
|
|
|
gfx::BackendType::SKIA, gfx::IntSize(1, 1), aFormat);
|
2019-07-12 01:36:44 +03:00
|
|
|
RefPtr<gfx::DrawTarget> dt = MakeAndAddRef<gfx::DrawTargetRecording>(
|
|
|
|
mRecorder, dummyDt, gfx::IntRect(gfx::IntPoint(0, 0), aSize));
|
2018-12-02 17:19:11 +03:00
|
|
|
return dt.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasChild::RecordEvent(const gfx::RecordedEvent& aEvent) {
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
mRecorder->RecordEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfx::DataSourceSurface> CanvasChild::GetDataSurface(
|
|
|
|
const gfx::SourceSurface* aSurface) {
|
2019-09-10 05:06:46 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
|
2018-12-02 17:19:11 +03:00
|
|
|
MOZ_ASSERT(aSurface);
|
|
|
|
|
2019-09-06 21:38:25 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
mTransactionsSinceGetDataSurface = 0;
|
|
|
|
EnsureBeginTransaction();
|
|
|
|
mRecorder->RecordEvent(RecordedPrepareDataForSurface(aSurface));
|
|
|
|
uint32_t checkpoint = mRecorder->CreateCheckpoint();
|
|
|
|
|
|
|
|
gfx::IntSize ssSize = aSurface->GetSize();
|
|
|
|
gfx::SurfaceFormat ssFormat = aSurface->GetFormat();
|
|
|
|
size_t dataFormatWidth = ssSize.width * BytesPerPixel(ssFormat);
|
|
|
|
RefPtr<gfx::DataSourceSurface> dataSurface =
|
|
|
|
gfx::Factory::CreateDataSourceSurfaceWithStride(ssSize, ssFormat,
|
|
|
|
dataFormatWidth);
|
|
|
|
if (!dataSurface) {
|
|
|
|
gfxWarning() << "Failed to create DataSourceSurface.";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
gfx::DataSourceSurface::ScopedMap map(dataSurface,
|
|
|
|
gfx::DataSourceSurface::READ_WRITE);
|
|
|
|
char* dest = reinterpret_cast<char*>(map.GetData());
|
2019-07-22 16:12:07 +03:00
|
|
|
if (!mRecorder->WaitForCheckpoint(checkpoint)) {
|
2018-12-02 17:19:11 +03:00
|
|
|
gfxWarning() << "Timed out preparing data for DataSourceSurface.";
|
|
|
|
return dataSurface.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mRecorder->RecordEvent(RecordedGetDataForSurface(aSurface));
|
|
|
|
mRecorder->ReturnRead(dest, ssSize.height * dataFormatWidth);
|
|
|
|
|
|
|
|
return dataSurface.forget();
|
|
|
|
}
|
|
|
|
|
2019-07-22 16:07:59 +03:00
|
|
|
already_AddRefed<gfx::SourceSurface> CanvasChild::WrapSurface(
|
|
|
|
const RefPtr<gfx::SourceSurface>& aSurface) {
|
|
|
|
MOZ_ASSERT(aSurface);
|
2019-10-07 17:37:13 +03:00
|
|
|
if (!mRecorder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-22 16:07:59 +03:00
|
|
|
return MakeAndAddRef<SourceSurfaceCanvasRecording>(aSurface, this, mRecorder);
|
|
|
|
}
|
|
|
|
|
2018-12-02 17:19:11 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|