2017-10-27 20:33:53 +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
|
2017-10-03 19:29:15 +03: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_layout_printing_DrawEventRecorder_h
|
|
|
|
#define mozilla_layout_printing_DrawEventRecorder_h
|
|
|
|
|
2017-10-26 22:08:39 +03:00
|
|
|
#include <memory>
|
|
|
|
|
2017-10-03 19:29:15 +03:00
|
|
|
#include "mozilla/gfx/DrawEventRecorder.h"
|
|
|
|
#include "mozilla/gfx/RecordingTypes.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layout {
|
|
|
|
|
|
|
|
class PRFileDescStream : public mozilla::gfx::EventStream {
|
2017-10-26 22:08:39 +03:00
|
|
|
// Most writes, as seen in the print IPC use case, are very small (<32 bytes),
|
|
|
|
// with a small number of very large (>40KB) writes. Writes larger than this
|
|
|
|
// value are not buffered.
|
|
|
|
static const size_t kBufferSize = 1024;
|
2017-10-03 19:29:15 +03:00
|
|
|
public:
|
2017-10-26 22:08:39 +03:00
|
|
|
PRFileDescStream() : mFd(nullptr), mBuffer(nullptr), mBufferPos(0),
|
|
|
|
mGood(true) {}
|
2018-01-05 22:52:54 +03:00
|
|
|
PRFileDescStream(const PRFileDescStream& other) = delete;
|
|
|
|
~PRFileDescStream() { Close(); }
|
2017-10-03 19:29:15 +03:00
|
|
|
|
2017-11-03 21:37:15 +03:00
|
|
|
void OpenFD(PRFileDesc* aFd)
|
|
|
|
{
|
2017-10-03 19:29:15 +03:00
|
|
|
MOZ_ASSERT(!IsOpen());
|
2017-11-03 21:37:15 +03:00
|
|
|
mFd = aFd;
|
2018-01-05 22:52:54 +03:00
|
|
|
mGood = !!mFd;
|
2017-10-26 22:08:39 +03:00
|
|
|
mBuffer.reset(new uint8_t[kBufferSize]);
|
|
|
|
mBufferPos = 0;
|
2017-10-03 19:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Close() {
|
2017-11-16 21:38:33 +03:00
|
|
|
// We need to be API compatible with std::ostream, and so we silently handle
|
|
|
|
// closes on a closed FD.
|
|
|
|
if (IsOpen()) {
|
|
|
|
Flush();
|
|
|
|
PR_Close(mFd);
|
|
|
|
mFd = nullptr;
|
|
|
|
mBuffer.reset();
|
|
|
|
mBufferPos = 0;
|
|
|
|
}
|
2017-10-03 19:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOpen() {
|
|
|
|
return mFd != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flush() {
|
2017-11-16 21:38:33 +03:00
|
|
|
// See comment in Close().
|
2017-10-26 22:08:39 +03:00
|
|
|
if (IsOpen() && mBufferPos > 0) {
|
2018-01-05 22:52:54 +03:00
|
|
|
PRInt32 length =
|
|
|
|
PR_Write(mFd, static_cast<const void*>(mBuffer.get()), mBufferPos);
|
|
|
|
mGood = length >= 0 && static_cast<size_t>(length) == mBufferPos;
|
2017-10-26 22:08:39 +03:00
|
|
|
mBufferPos = 0;
|
|
|
|
}
|
2017-10-03 19:29:15 +03:00
|
|
|
}
|
|
|
|
|
2018-01-05 22:52:54 +03:00
|
|
|
void Seek(PRInt64 aOffset, PRSeekWhence aWhence)
|
|
|
|
{
|
2017-10-26 22:08:39 +03:00
|
|
|
Flush();
|
2018-01-05 22:52:54 +03:00
|
|
|
PRInt64 pos = PR_Seek64(mFd, aOffset, aWhence);
|
|
|
|
mGood = pos != -1;
|
2017-10-03 23:34:18 +03:00
|
|
|
}
|
|
|
|
|
2017-10-03 19:29:15 +03:00
|
|
|
void write(const char* aData, size_t aSize) {
|
2018-01-05 22:52:54 +03:00
|
|
|
if (!good()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-16 21:38:33 +03:00
|
|
|
// See comment in Close().
|
2017-10-03 19:29:15 +03:00
|
|
|
if (IsOpen()) {
|
2017-10-26 22:08:39 +03:00
|
|
|
// If we're writing more data than could ever fit in our buffer, flush the
|
|
|
|
// buffer and write directly.
|
|
|
|
if (aSize > kBufferSize) {
|
|
|
|
Flush();
|
2018-01-05 22:52:54 +03:00
|
|
|
PRInt32 length = PR_Write(mFd, static_cast<const void*>(aData), aSize);
|
|
|
|
mGood = length >= 0 && static_cast<size_t>(length) == aSize;
|
|
|
|
// If our write could fit in our buffer, but doesn't because the buffer
|
|
|
|
// is partially full, write to the buffer, flush the buffer, and then
|
|
|
|
// write the rest of the data to the buffer.
|
2017-10-26 22:08:39 +03:00
|
|
|
} else if (aSize > AvailableBufferSpace()) {
|
|
|
|
size_t length = AvailableBufferSpace();
|
|
|
|
WriteToBuffer(aData, length);
|
|
|
|
Flush();
|
|
|
|
|
|
|
|
WriteToBuffer(aData + length, aSize - length);
|
|
|
|
// Write fits in the buffer.
|
|
|
|
} else {
|
|
|
|
WriteToBuffer(aData, aSize);
|
|
|
|
}
|
2017-10-03 19:29:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:23:57 +03:00
|
|
|
void read(char* aOut, size_t aSize) {
|
2018-01-05 22:52:54 +03:00
|
|
|
if (!good()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-26 22:08:39 +03:00
|
|
|
Flush();
|
2017-10-03 20:23:57 +03:00
|
|
|
PRInt32 res = PR_Read(mFd, static_cast<void*>(aOut), aSize);
|
2018-01-05 22:52:54 +03:00
|
|
|
mGood = res >= 0 && (static_cast<size_t>(res) == aSize);
|
2017-10-03 20:23:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool good() {
|
|
|
|
return mGood;
|
|
|
|
}
|
|
|
|
|
2017-10-03 19:29:15 +03:00
|
|
|
private:
|
2017-10-26 22:08:39 +03:00
|
|
|
size_t AvailableBufferSpace() {
|
|
|
|
return kBufferSize - mBufferPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteToBuffer(const char* aData, size_t aSize) {
|
|
|
|
MOZ_ASSERT(aSize <= AvailableBufferSpace());
|
|
|
|
memcpy(mBuffer.get() + mBufferPos, aData, aSize);
|
|
|
|
mBufferPos += aSize;
|
|
|
|
}
|
|
|
|
|
2017-10-03 19:29:15 +03:00
|
|
|
PRFileDesc* mFd;
|
2017-10-26 22:08:39 +03:00
|
|
|
std::unique_ptr<uint8_t[]> mBuffer;
|
|
|
|
size_t mBufferPos;
|
2017-10-03 20:23:57 +03:00
|
|
|
bool mGood;
|
2017-10-03 19:29:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class DrawEventRecorderPRFileDesc : public gfx::DrawEventRecorderPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderPRFileDesc, override)
|
2017-11-03 21:37:15 +03:00
|
|
|
explicit DrawEventRecorderPRFileDesc(){};
|
2017-10-03 19:29:15 +03:00
|
|
|
~DrawEventRecorderPRFileDesc();
|
|
|
|
|
|
|
|
void RecordEvent(const gfx::RecordedEvent& aEvent) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a recording file is currently open.
|
|
|
|
*/
|
|
|
|
bool IsOpen();
|
|
|
|
|
|
|
|
/**
|
2017-11-03 21:37:15 +03:00
|
|
|
* Opens the recorder with the provided PRFileDesc *.
|
2017-10-03 19:29:15 +03:00
|
|
|
*/
|
2017-11-03 21:37:15 +03:00
|
|
|
void OpenFD(PRFileDesc* aFd);
|
2017-10-03 19:29:15 +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();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Flush() override;
|
|
|
|
|
|
|
|
PRFileDescStream mOutputStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2017-10-03 20:23:57 +03:00
|
|
|
|
2017-10-03 19:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* mozilla_layout_printing_DrawEventRecorder_h */
|