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/. */
|
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_DRAWEVENTRECORDER_H_
|
|
|
|
#define MOZILLA_GFX_DRAWEVENTRECORDER_H_
|
|
|
|
|
|
|
|
#include "2D.h"
|
|
|
|
#include "RecordedEvent.h"
|
2017-10-03 19:29:15 +03:00
|
|
|
#include "RecordingTypes.h"
|
2017-12-16 18:13:30 +03:00
|
|
|
#include "mozilla/FStream.h"
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2015-01-09 02:42:38 +03:00
|
|
|
#include <unordered_set>
|
2017-10-28 01:21:26 +03:00
|
|
|
#include <unordered_map>
|
2017-10-28 01:21:27 +03:00
|
|
|
#include <functional>
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2018-09-25 05:43:41 +03:00
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
class PathRecording;
|
|
|
|
|
|
|
|
class DrawEventRecorderPrivate : public DrawEventRecorder
|
|
|
|
{
|
|
|
|
public:
|
2017-11-06 06:37:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderPrivate, override)
|
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
DrawEventRecorderPrivate();
|
2012-09-24 19:02:50 +04:00
|
|
|
virtual ~DrawEventRecorderPrivate() { }
|
2018-02-20 23:59:31 +03:00
|
|
|
virtual bool Finish() override { ClearResources(); return true; }
|
2017-10-28 01:21:27 +03:00
|
|
|
virtual void FlushItem(IntRect) { }
|
2018-04-03 10:54:00 +03:00
|
|
|
void DetachResources() {
|
2017-06-07 17:28:28 +03:00
|
|
|
// The iteration is a bit awkward here because our iterator will
|
|
|
|
// be invalidated by the removal
|
|
|
|
for (auto font = mStoredFonts.begin(); font != mStoredFonts.end(); ) {
|
|
|
|
auto oldFont = font++;
|
|
|
|
(*oldFont)->RemoveUserData(reinterpret_cast<UserDataKey*>(this));
|
|
|
|
}
|
|
|
|
for (auto surface = mStoredSurfaces.begin(); surface != mStoredSurfaces.end(); ) {
|
|
|
|
auto oldSurface = surface++;
|
|
|
|
(*oldSurface)->RemoveUserData(reinterpret_cast<UserDataKey*>(this));
|
|
|
|
}
|
2017-10-28 01:21:27 +03:00
|
|
|
mStoredFonts.clear();
|
|
|
|
mStoredSurfaces.clear();
|
|
|
|
}
|
2017-06-07 17:28:28 +03:00
|
|
|
|
2017-10-28 01:21:27 +03:00
|
|
|
void ClearResources() {
|
|
|
|
mStoredObjects.clear();
|
|
|
|
mStoredFontData.clear();
|
2018-09-06 04:55:53 +03:00
|
|
|
mScaledFonts.clear();
|
2017-06-07 17:28:28 +03:00
|
|
|
}
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
template<class S>
|
2017-10-03 19:29:15 +03:00
|
|
|
void WriteHeader(S& aStream) {
|
|
|
|
WriteElement(aStream, kMagicInt);
|
|
|
|
WriteElement(aStream, kMajorRevision);
|
|
|
|
WriteElement(aStream, kMinorRevision);
|
|
|
|
}
|
2016-01-05 13:08:57 +03:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
virtual void RecordEvent(const RecordedEvent &aEvent) = 0;
|
2012-09-24 19:02:50 +04:00
|
|
|
void WritePath(const PathRecording *aPath);
|
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
void AddStoredObject(const ReferencePtr aObject) {
|
|
|
|
mStoredObjects.insert(aObject);
|
2012-09-24 19:02:50 +04:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
void RemoveStoredObject(const ReferencePtr aObject) {
|
|
|
|
mStoredObjects.erase(aObject);
|
2012-09-24 19:02:50 +04:00
|
|
|
}
|
|
|
|
|
2017-06-07 17:28:28 +03:00
|
|
|
void AddScaledFont(ScaledFont* aFont) {
|
2018-09-06 04:55:53 +03:00
|
|
|
if (mStoredFonts.insert(aFont).second &&
|
|
|
|
WantsExternalFonts()) {
|
|
|
|
mScaledFonts.push_back(aFont);
|
|
|
|
}
|
2017-06-07 17:28:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveScaledFont(ScaledFont* aFont) {
|
|
|
|
mStoredFonts.erase(aFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSourceSurface(SourceSurface* aSurface) {
|
|
|
|
mStoredSurfaces.insert(aSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveSourceSurface(SourceSurface* aSurface) {
|
|
|
|
mStoredSurfaces.erase(aSurface);
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
bool HasStoredObject(const ReferencePtr aObject) {
|
|
|
|
return mStoredObjects.find(aObject) != mStoredObjects.end();
|
2015-12-21 23:33:14 +03:00
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:58 +03:00
|
|
|
void AddStoredFontData(const uint64_t aFontDataKey) {
|
|
|
|
mStoredFontData.insert(aFontDataKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasStoredFontData(const uint64_t aFontDataKey) {
|
|
|
|
return mStoredFontData.find(aFontDataKey) != mStoredFontData.end();
|
|
|
|
}
|
|
|
|
|
2018-04-27 02:00:16 +03:00
|
|
|
bool WantsExternalFonts() const { return mExternalFonts; }
|
|
|
|
|
|
|
|
void TakeExternalSurfaces(std::vector<RefPtr<SourceSurface>>& aSurfaces)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
aSurfaces = std::move(mExternalSurfaces);
|
2018-04-27 02:00:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void StoreSourceSurfaceRecording(SourceSurface *aSurface,
|
|
|
|
const char *aReason);
|
2017-10-28 01:21:26 +03:00
|
|
|
|
2018-09-25 05:43:41 +03:00
|
|
|
virtual void AddDependentSurface(uint64_t aDependencyId)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("GFX: AddDependentSurface");
|
|
|
|
}
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
protected:
|
2018-04-27 02:00:16 +03:00
|
|
|
void StoreExternalSurfaceRecording(SourceSurface* aSurface,
|
|
|
|
uint64_t aKey);
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
virtual void Flush() = 0;
|
|
|
|
|
2017-06-09 22:39:14 +03:00
|
|
|
std::unordered_set<const void*> mStoredObjects;
|
|
|
|
std::unordered_set<uint64_t> mStoredFontData;
|
|
|
|
std::unordered_set<ScaledFont*> mStoredFonts;
|
2018-09-06 04:55:53 +03:00
|
|
|
std::vector<RefPtr<ScaledFont>> mScaledFonts;
|
2017-06-09 22:39:14 +03:00
|
|
|
std::unordered_set<SourceSurface*> mStoredSurfaces;
|
2018-04-27 02:00:16 +03:00
|
|
|
std::vector<RefPtr<SourceSurface>> mExternalSurfaces;
|
2017-10-28 01:21:26 +03:00
|
|
|
bool mExternalFonts;
|
2012-09-24 19:02:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class DrawEventRecorderFile : public DrawEventRecorderPrivate
|
|
|
|
{
|
2017-12-16 18:13:30 +03:00
|
|
|
using char_type = filesystem::Path::value_type;
|
2012-09-24 19:02:50 +04:00
|
|
|
public:
|
2017-06-22 20:39:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderFile, override)
|
2017-12-16 18:13:30 +03:00
|
|
|
explicit DrawEventRecorderFile(const char_type* aFilename);
|
2012-09-24 19:02:50 +04:00
|
|
|
~DrawEventRecorderFile();
|
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
void RecordEvent(const RecordedEvent &aEvent) override;
|
|
|
|
|
2016-11-22 17:06:46 +03:00
|
|
|
/**
|
|
|
|
* Returns whether a recording file is currently open.
|
|
|
|
*/
|
|
|
|
bool IsOpen();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens new file with the provided name. The recorder does NOT forget which
|
|
|
|
* objects it has recorded. This can be used with Close, so that a recording
|
|
|
|
* can be processed in chunks. The file must not already be open.
|
|
|
|
*/
|
2017-12-16 18:13:30 +03:00
|
|
|
void OpenNew(const char_type* aFilename);
|
2016-11-22 17:06:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the file so that it can be processed. The recorder does NOT forget
|
|
|
|
* which objects it has recorded. This can be used with OpenNew, so that a
|
|
|
|
* recording can be processed in chunks. The file must be open.
|
|
|
|
*/
|
|
|
|
void Close();
|
|
|
|
|
2012-09-24 19:02:50 +04:00
|
|
|
private:
|
2017-06-22 20:39:28 +03:00
|
|
|
void Flush() override;
|
2012-09-24 19:02:50 +04:00
|
|
|
|
2017-12-16 18:13:30 +03:00
|
|
|
mozilla::OFStream mOutputStream;
|
2012-09-24 19:02:50 +04:00
|
|
|
};
|
|
|
|
|
2018-09-06 04:55:53 +03:00
|
|
|
typedef std::function<void(MemStream &aStream, std::vector<RefPtr<ScaledFont>> &aScaledFonts)> SerializeResourcesFn;
|
2017-10-28 01:21:27 +03:00
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
// WARNING: This should not be used in its existing state because
|
|
|
|
// it is likely to OOM because of large continguous allocations.
|
2018-04-27 02:00:16 +03:00
|
|
|
class DrawEventRecorderMemory : public DrawEventRecorderPrivate
|
2017-04-26 23:31:59 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-06-22 20:39:28 +03:00
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderMemory, override)
|
2017-04-26 23:31:59 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a DrawEventRecorder that stores the recording in memory.
|
|
|
|
*/
|
|
|
|
DrawEventRecorderMemory();
|
2017-10-28 01:21:27 +03:00
|
|
|
explicit DrawEventRecorderMemory(const SerializeResourcesFn &aSerialize);
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
void RecordEvent(const RecordedEvent &aEvent) override;
|
|
|
|
|
2018-09-25 05:43:41 +03:00
|
|
|
void AddDependentSurface(uint64_t aDependencyId) override;
|
|
|
|
|
|
|
|
nsTHashtable<nsUint64HashKey>&& TakeDependentSurfaces();
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
/**
|
|
|
|
* @return the current size of the recording (in chars).
|
|
|
|
*/
|
|
|
|
size_t RecordingSize();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wipes the internal recording buffer, but the recorder does NOT forget which
|
|
|
|
* objects it has recorded. This can be used so that a recording can be copied
|
|
|
|
* and processed in chunks, releasing memory as it goes.
|
|
|
|
*/
|
|
|
|
void WipeRecording();
|
2018-02-20 23:59:31 +03:00
|
|
|
bool Finish() override;
|
2017-10-28 01:21:27 +03:00
|
|
|
void FlushItem(IntRect) override;
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-06-22 20:39:29 +03:00
|
|
|
MemStream mOutputStream;
|
2017-10-28 01:21:27 +03:00
|
|
|
/* The index stream is of the form:
|
|
|
|
* ItemIndex { size_t dataEnd; size_t extraDataEnd; }
|
|
|
|
* It gets concatenated to the end of mOutputStream in Finish()
|
|
|
|
* The last size_t in the stream is offset of the begining of the
|
|
|
|
* index.
|
|
|
|
*/
|
|
|
|
MemStream mIndex;
|
2018-04-27 02:00:16 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
~DrawEventRecorderMemory() {};
|
|
|
|
|
2017-04-26 23:31:59 +03:00
|
|
|
private:
|
2017-10-28 01:21:27 +03:00
|
|
|
SerializeResourcesFn mSerializeCallback;
|
2018-09-25 05:43:41 +03:00
|
|
|
nsTHashtable<nsUint64HashKey> mDependentSurfaces;
|
2017-04-26 23:31:59 +03:00
|
|
|
|
2017-06-22 20:39:28 +03:00
|
|
|
void Flush() override;
|
2017-04-26 23:31:59 +03:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2012-09-24 19:02:50 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_DRAWEVENTRECORDER_H_ */
|