2017-10-28 02:10:06 +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
|
2012-09-24 19:02:50 +04:00
|
|
|
* 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"
|
2020-07-07 22:26:13 +03:00
|
|
|
|
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2012-09-24 19:02:50 +04:00
|
|
|
#include "PathRecording.h"
|
2016-01-05 13:08:57 +03:00
|
|
|
#include "RecordingTypes.h"
|
2018-04-27 02:00:16 +03:00
|
|
|
#include "RecordedEventImpl.h"
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2017-10-28 01:21:26 +03:00
|
|
|
DrawEventRecorderPrivate::DrawEventRecorderPrivate() : mExternalFonts(false) {}
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2018-04-27 02:00:16 +03:00
|
|
|
void DrawEventRecorderPrivate::StoreExternalSurfaceRecording(
|
|
|
|
SourceSurface* aSurface, uint64_t aKey) {
|
|
|
|
RecordEvent(RecordedExternalSurfaceCreation(aSurface, aKey));
|
|
|
|
mExternalSurfaces.push_back(aSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawEventRecorderPrivate::StoreSourceSurfaceRecording(
|
|
|
|
SourceSurface* aSurface, const char* aReason) {
|
|
|
|
RefPtr<DataSourceSurface> dataSurf = aSurface->GetDataSurface();
|
2019-10-07 17:38:06 +03:00
|
|
|
IntSize surfaceSize = aSurface->GetSize();
|
2020-07-07 22:32:38 +03:00
|
|
|
Maybe<DataSourceSurface::ScopedMap> map;
|
|
|
|
if (dataSurf) {
|
|
|
|
map.emplace(dataSurf, DataSourceSurface::READ);
|
|
|
|
}
|
|
|
|
if (!dataSurf || !map->IsMapped() ||
|
|
|
|
!Factory::AllowedSurfaceSize(surfaceSize)) {
|
2019-10-07 17:38:06 +03:00
|
|
|
gfxWarning() << "Recording failed to record SourceSurface for " << aReason;
|
|
|
|
|
|
|
|
// If surface size is not allowed, replace with reasonable size.
|
|
|
|
if (!Factory::AllowedSurfaceSize(surfaceSize)) {
|
|
|
|
surfaceSize.width = std::min(surfaceSize.width, kReasonableSurfaceSize);
|
|
|
|
surfaceSize.height = std::min(surfaceSize.height, kReasonableSurfaceSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a dummy source surface.
|
|
|
|
int32_t stride = surfaceSize.width * BytesPerPixel(aSurface->GetFormat());
|
2020-07-07 22:26:13 +03:00
|
|
|
UniquePtr<uint8_t[]> sourceData =
|
|
|
|
MakeUniqueFallible<uint8_t[]>(stride * surfaceSize.height);
|
|
|
|
if (!sourceData) {
|
|
|
|
// If the surface is too big just create a 1 x 1 dummy.
|
|
|
|
surfaceSize.width = 1;
|
|
|
|
surfaceSize.height = 1;
|
|
|
|
stride = surfaceSize.width * BytesPerPixel(aSurface->GetFormat());
|
|
|
|
sourceData = MakeUnique<uint8_t[]>(stride * surfaceSize.height);
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:38:06 +03:00
|
|
|
RecordEvent(RecordedSourceSurfaceCreation(aSurface, sourceData.get(),
|
|
|
|
stride, surfaceSize,
|
|
|
|
aSurface->GetFormat()));
|
2018-04-27 02:00:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-07 17:38:06 +03:00
|
|
|
RecordEvent(RecordedSourceSurfaceCreation(
|
2020-07-07 22:32:38 +03:00
|
|
|
aSurface, map->GetData(), map->GetStride(), dataSurf->GetSize(),
|
2019-10-07 17:38:06 +03:00
|
|
|
dataSurf->GetFormat()));
|
2018-04-27 02:00:16 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 17:21:31 +03:00
|
|
|
void DrawEventRecorderPrivate::RecordSourceSurfaceDestruction(void* aSurface) {
|
|
|
|
RemoveSourceSurface(static_cast<SourceSurface*>(aSurface));
|
2020-04-16 20:13:16 +03:00
|
|
|
RemoveStoredObject(aSurface);
|
|
|
|
RecordEvent(RecordedSourceSurfaceDestruction(ReferencePtr(aSurface)));
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:09:43 +03:00
|
|
|
void DrawEventRecorderPrivate::DecrementUnscaledFontRefCount(
|
|
|
|
const ReferencePtr aUnscaledFont) {
|
|
|
|
auto element = mUnscaledFontRefs.find(aUnscaledFont);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(element != mUnscaledFontRefs.end(),
|
|
|
|
"DecrementUnscaledFontRefCount calls should balance "
|
|
|
|
"with IncrementUnscaledFontRefCount calls");
|
|
|
|
if (--(element->second) <= 0) {
|
|
|
|
RecordEvent(RecordedUnscaledFontDestruction(aUnscaledFont));
|
|
|
|
mUnscaledFontRefs.erase(aUnscaledFont);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
void DrawEventRecorderFile::RecordEvent(const RecordedEvent& aEvent) {
|
|
|
|
aEvent.RecordToStream(mOutputStream);
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
void DrawEventRecorderMemory::RecordEvent(const RecordedEvent& aEvent) {
|
|
|
|
aEvent.RecordToStream(mOutputStream);
|
|
|
|
}
|
|
|
|
|
2018-09-25 05:43:41 +03:00
|
|
|
void DrawEventRecorderMemory::AddDependentSurface(uint64_t aDependencyId) {
|
2021-03-23 13:36:37 +03:00
|
|
|
mDependentSurfaces.Insert(aDependencyId);
|
2018-09-25 05:43:41 +03:00
|
|
|
}
|
|
|
|
|
2021-03-23 13:36:37 +03:00
|
|
|
nsTHashSet<uint64_t>&& DrawEventRecorderMemory::TakeDependentSurfaces() {
|
2018-09-25 05:43:41 +03:00
|
|
|
return std::move(mDependentSurfaces);
|
|
|
|
}
|
|
|
|
|
2017-12-16 18:13:30 +03:00
|
|
|
DrawEventRecorderFile::DrawEventRecorderFile(const char_type* aFilename)
|
2019-09-01 21:32:06 +03:00
|
|
|
: mOutputStream(aFilename, std::ofstream::binary) {
|
2017-06-22 20:39:28 +03:00
|
|
|
WriteHeader(mOutputStream);
|
2012-09-24 19:02:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawEventRecorderFile::~DrawEventRecorderFile() { mOutputStream.close(); }
|
|
|
|
|
|
|
|
void DrawEventRecorderFile::Flush() { mOutputStream.flush(); }
|
|
|
|
|
2016-11-22 17:06:46 +03:00
|
|
|
bool DrawEventRecorderFile::IsOpen() { return mOutputStream.is_open(); }
|
|
|
|
|
2017-12-16 18:13:30 +03:00
|
|
|
void DrawEventRecorderFile::OpenNew(const char_type* aFilename) {
|
2017-06-22 20:39:28 +03:00
|
|
|
MOZ_ASSERT(!mOutputStream.is_open());
|
2016-11-22 17:06:46 +03:00
|
|
|
|
2019-09-01 21:32:06 +03:00
|
|
|
mOutputStream.open(aFilename, std::ofstream::binary);
|
2017-06-22 20:39:28 +03:00
|
|
|
WriteHeader(mOutputStream);
|
2016-11-22 17:06:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawEventRecorderFile::Close() {
|
2017-06-22 20:39:28 +03:00
|
|
|
MOZ_ASSERT(mOutputStream.is_open());
|
2016-11-22 17:06:46 +03:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
mOutputStream.close();
|
2016-11-22 17:06:46 +03:00
|
|
|
}
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
DrawEventRecorderMemory::DrawEventRecorderMemory() {
|
2017-06-22 20:39:28 +03:00
|
|
|
WriteHeader(mOutputStream);
|
2017-04-26 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
DrawEventRecorderMemory::DrawEventRecorderMemory(
|
2019-10-03 00:47:11 +03:00
|
|
|
const SerializeResourcesFn& aFn)
|
|
|
|
: mSerializeCallback(aFn) {
|
2018-09-25 05:33:14 +03:00
|
|
|
mExternalFonts = !!mSerializeCallback;
|
2017-10-28 01:21:27 +03:00
|
|
|
WriteHeader(mOutputStream);
|
|
|
|
}
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
void DrawEventRecorderMemory::Flush() {}
|
2017-10-28 01:21:27 +03:00
|
|
|
|
|
|
|
void DrawEventRecorderMemory::FlushItem(IntRect aRect) {
|
2018-03-17 02:20:26 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!aRect.IsEmpty());
|
2018-04-03 10:54:00 +03:00
|
|
|
// Detaching our existing resources will add some
|
2018-03-17 02:20:26 +03:00
|
|
|
// destruction events to our stream so we need to do that
|
|
|
|
// first.
|
2018-04-03 10:54:00 +03:00
|
|
|
DetachResources();
|
2018-03-17 02:20:26 +03:00
|
|
|
|
2018-03-27 18:51:29 +03:00
|
|
|
// See moz2d_renderer.rs for a description of the stream format
|
2017-10-28 01:21:27 +03:00
|
|
|
WriteElement(mIndex, mOutputStream.mLength);
|
2018-03-17 02:20:26 +03:00
|
|
|
|
|
|
|
// write out the fonts into the extra data section
|
2018-09-06 04:55:53 +03:00
|
|
|
mSerializeCallback(mOutputStream, mScaledFonts);
|
2017-10-28 01:21:27 +03:00
|
|
|
WriteElement(mIndex, mOutputStream.mLength);
|
2018-03-17 02:20:26 +03:00
|
|
|
|
|
|
|
WriteElement(mIndex, aRect.x);
|
|
|
|
WriteElement(mIndex, aRect.y);
|
|
|
|
WriteElement(mIndex, aRect.XMost());
|
|
|
|
WriteElement(mIndex, aRect.YMost());
|
2017-10-28 01:21:27 +03:00
|
|
|
ClearResources();
|
2018-03-27 18:51:29 +03:00
|
|
|
|
|
|
|
// write out a new header for the next recording in the stream
|
2018-03-17 02:20:26 +03:00
|
|
|
WriteHeader(mOutputStream);
|
2017-10-28 01:21:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DrawEventRecorderMemory::Finish() {
|
2018-02-20 23:59:31 +03:00
|
|
|
// this length might be 0, and things should still work.
|
|
|
|
// for example if there are no items in a particular area
|
2017-10-28 01:21:27 +03:00
|
|
|
size_t indexOffset = mOutputStream.mLength;
|
|
|
|
// write out the index
|
|
|
|
mOutputStream.write(mIndex.mData, mIndex.mLength);
|
2018-02-20 23:59:31 +03:00
|
|
|
bool hasItems = mIndex.mLength != 0;
|
2017-10-28 01:21:27 +03:00
|
|
|
mIndex = MemStream();
|
|
|
|
// write out the offset of the Index to the end of the output stream
|
|
|
|
WriteElement(mOutputStream, indexOffset);
|
|
|
|
ClearResources();
|
2018-02-20 23:59:31 +03:00
|
|
|
return hasItems;
|
2017-10-28 01:21:27 +03:00
|
|
|
}
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
size_t DrawEventRecorderMemory::RecordingSize() {
|
2017-06-22 20:39:28 +03:00
|
|
|
return mOutputStream.mLength;
|
2017-04-26 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawEventRecorderMemory::WipeRecording() {
|
2017-06-22 20:39:28 +03:00
|
|
|
mOutputStream = MemStream();
|
2017-10-28 01:21:27 +03:00
|
|
|
mIndex = MemStream();
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
WriteHeader(mOutputStream);
|
2017-04-26 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|