tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 6e9c5b0b54efaed8a2926887c61c1a0ea11f99f3
parent 69ce430d442444e6492ca81c67d0a5ce289e86c7
Author: Lee Salzman <lsalzman@mozilla.com>
Date:   Wed, 15 Oct 2025 22:06:59 +0000

Bug 1994357 - Use RecordedEventArray for more RecordedEvents. r=aosmond

Differential Revision: https://phabricator.services.mozilla.com/D268656

Diffstat:
Mgfx/2d/2D.h | 2+-
Mgfx/2d/DrawEventRecorder.cpp | 43+++++++++++++++++--------------------------
Mgfx/2d/Factory.cpp | 3++-
Mgfx/2d/NativeFontResourceDWrite.cpp | 6+++---
Mgfx/2d/NativeFontResourceDWrite.h | 2+-
Mgfx/2d/NativeFontResourceFreeType.cpp | 7++++---
Mgfx/2d/NativeFontResourceFreeType.h | 6+++---
Mgfx/2d/NativeFontResourceGDI.cpp | 6+++---
Mgfx/2d/NativeFontResourceGDI.h | 4++--
Mgfx/2d/NativeFontResourceMac.cpp | 2+-
Mgfx/2d/NativeFontResourceMac.h | 4++--
Mgfx/2d/RecordedEvent.h | 21++++++++++++++-------
Mgfx/2d/RecordedEventImpl.h | 211+++++++++++++++++++++++++++++++++++--------------------------------------------
13 files changed, 145 insertions(+), 172 deletions(-)

diff --git a/gfx/2d/2D.h b/gfx/2d/2D.h @@ -2243,7 +2243,7 @@ class GFX2D_API Factory { * @return a NativeFontResource of nullptr if failed. */ static already_AddRefed<NativeFontResource> CreateNativeFontResource( - uint8_t* aData, uint32_t aSize, FontType aFontType, + const uint8_t* aData, uint32_t aSize, FontType aFontType, void* aFontContext = nullptr); /** diff --git a/gfx/2d/DrawEventRecorder.cpp b/gfx/2d/DrawEventRecorder.cpp @@ -44,43 +44,34 @@ void DrawEventRecorderPrivate::StoreSourceSurfaceRecording( SourceSurface* aSurface, const char* aReason) { NS_ASSERT_OWNINGTHREAD(DrawEventRecorderPrivate); - RefPtr<DataSourceSurface> dataSurf = aSurface->GetDataSurface(); + RefPtr<DataSourceSurface> dataSurf; IntSize surfaceSize = aSurface->GetSize(); - Maybe<DataSourceSurface::ScopedMap> map; - if (dataSurf) { - map.emplace(dataSurf, DataSourceSurface::READ); + if (Factory::AllowedSurfaceSize(surfaceSize)) { + dataSurf = aSurface->GetDataSurface(); } - if (!dataSurf || !map->IsMapped() || - !Factory::AllowedSurfaceSize(surfaceSize)) { + if (!dataSurf) { 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); - } + 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()); - UniquePtr<uint8_t[]> sourceData = - MakeUniqueFallible<uint8_t[]>(stride * surfaceSize.height); - if (!sourceData) { + if (Factory::AllowedSurfaceSize(surfaceSize)) { + dataSurf = Factory::CreateDataSourceSurface(surfaceSize, + aSurface->GetFormat(), true); + } + if (!dataSurf) { // 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); + dataSurf = Factory::CreateDataSourceSurface(IntSize(1, 1), + aSurface->GetFormat(), true); + if (!dataSurf) { + return; + } } - - RecordEvent(RecordedSourceSurfaceCreation(aSurface, sourceData.get(), - stride, surfaceSize, - aSurface->GetFormat())); - return; } - RecordEvent(RecordedSourceSurfaceCreation( - aSurface, map->GetData(), map->GetStride(), dataSurf->GetSize(), - dataSurf->GetFormat())); + RecordEvent(RecordedSourceSurfaceCreation(aSurface, dataSurf)); } void DrawEventRecorderPrivate::RecordSourceSurfaceDestruction(void* aSurface) { diff --git a/gfx/2d/Factory.cpp b/gfx/2d/Factory.cpp @@ -542,7 +542,8 @@ uint32_t Factory::GetMaxSurfaceSize(BackendType aType) { } already_AddRefed<NativeFontResource> Factory::CreateNativeFontResource( - uint8_t* aData, uint32_t aSize, FontType aFontType, void* aFontContext) { + const uint8_t* aData, uint32_t aSize, FontType aFontType, + void* aFontContext) { switch (aFontType) { #ifdef WIN32 case FontType::DWRITE: diff --git a/gfx/2d/NativeFontResourceDWrite.cpp b/gfx/2d/NativeFontResourceDWrite.cpp @@ -82,7 +82,7 @@ class DWriteFontFileStream final : public IDWriteFontFileStream { * * @param aData Font data */ - bool Initialize(uint8_t* aData, uint32_t aSize); + bool Initialize(const uint8_t* aData, uint32_t aSize); // IUnknown interface IFACEMETHOD(QueryInterface)(IID const& iid, OUT void** ppObject) { @@ -161,7 +161,7 @@ DWriteFontFileStream::~DWriteFontFileStream() { sFontFileStreams.erase(mFontFileKey); } -bool DWriteFontFileStream::Initialize(uint8_t* aData, uint32_t aSize) { +bool DWriteFontFileStream::Initialize(const uint8_t* aData, uint32_t aSize) { if (!mData.SetLength(aSize, fallible)) { return false; } @@ -201,7 +201,7 @@ DWriteFontFileStream::ReleaseFileFragment(void* fragmentContext) {} /* static */ already_AddRefed<NativeFontResourceDWrite> NativeFontResourceDWrite::Create( - uint8_t* aFontData, uint32_t aDataLength) { + const uint8_t* aFontData, uint32_t aDataLength) { RefPtr<IDWriteFactory> factory = Factory::GetDWriteFactory(); if (!factory) { gfxWarning() << "Failed to get DWrite Factory."; diff --git a/gfx/2d/NativeFontResourceDWrite.h b/gfx/2d/NativeFontResourceDWrite.h @@ -28,7 +28,7 @@ class NativeFontResourceDWrite final : public NativeFontResource { * @return Referenced NativeFontResourceDWrite or nullptr if invalid. */ static already_AddRefed<NativeFontResourceDWrite> Create( - uint8_t* aFontData, uint32_t aDataLength); + const uint8_t* aFontData, uint32_t aDataLength); already_AddRefed<UnscaledFont> CreateUnscaledFont( uint32_t aIndex, const uint8_t* aInstanceData, diff --git a/gfx/2d/NativeFontResourceFreeType.cpp b/gfx/2d/NativeFontResourceFreeType.cpp @@ -23,7 +23,7 @@ NativeFontResourceFreeType::~NativeFontResourceFreeType() = default; template <class T> already_AddRefed<T> NativeFontResourceFreeType::CreateInternal( - uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary) { + const uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary) { if (!aFontData || !aDataLength) { return nullptr; } @@ -39,7 +39,7 @@ already_AddRefed<T> NativeFontResourceFreeType::CreateInternal( #ifdef MOZ_WIDGET_ANDROID already_AddRefed<NativeFontResourceFreeType> NativeFontResourceFreeType::Create( - uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary) { + const uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary) { return CreateInternal<NativeFontResourceFreeType>(aFontData, aDataLength, aFTLibrary); } @@ -84,7 +84,8 @@ already_AddRefed<UnscaledFont> NativeFontResourceFontconfig::CreateUnscaledFont( } already_AddRefed<NativeFontResourceFontconfig> -NativeFontResourceFontconfig::Create(uint8_t* aFontData, uint32_t aDataLength, +NativeFontResourceFontconfig::Create(const uint8_t* aFontData, + uint32_t aDataLength, FT_Library aFTLibrary) { return CreateInternal<NativeFontResourceFontconfig>(aFontData, aDataLength, aFTLibrary); diff --git a/gfx/2d/NativeFontResourceFreeType.h b/gfx/2d/NativeFontResourceFreeType.h @@ -23,7 +23,7 @@ class NativeFontResourceFreeType #ifdef MOZ_WIDGET_ANDROID static already_AddRefed<NativeFontResourceFreeType> Create( - uint8_t* aFontData, uint32_t aDataLength, + const uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary = nullptr); already_AddRefed<UnscaledFont> CreateUnscaledFont( @@ -41,7 +41,7 @@ class NativeFontResourceFreeType FT_Library aFTLibrary = nullptr); template <class T> - static already_AddRefed<T> CreateInternal(uint8_t* aFontData, + static already_AddRefed<T> CreateInternal(const uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary); @@ -57,7 +57,7 @@ class NativeFontResourceFontconfig final : public NativeFontResourceFreeType { override) static already_AddRefed<NativeFontResourceFontconfig> Create( - uint8_t* aFontData, uint32_t aDataLength, + const uint8_t* aFontData, uint32_t aDataLength, FT_Library aFTLibrary = nullptr); already_AddRefed<UnscaledFont> CreateUnscaledFont( diff --git a/gfx/2d/NativeFontResourceGDI.cpp b/gfx/2d/NativeFontResourceGDI.cpp @@ -16,10 +16,10 @@ namespace gfx { /* static */ already_AddRefed<NativeFontResourceGDI> NativeFontResourceGDI::Create( - uint8_t* aFontData, uint32_t aDataLength) { + const uint8_t* aFontData, uint32_t aDataLength) { DWORD numberOfFontsAdded; - HANDLE fontResourceHandle = - ::AddFontMemResourceEx(aFontData, aDataLength, 0, &numberOfFontsAdded); + HANDLE fontResourceHandle = ::AddFontMemResourceEx( + (PVOID)aFontData, aDataLength, 0, &numberOfFontsAdded); if (!fontResourceHandle) { gfxWarning() << "Failed to add memory font resource."; return nullptr; diff --git a/gfx/2d/NativeFontResourceGDI.h b/gfx/2d/NativeFontResourceGDI.h @@ -28,8 +28,8 @@ class NativeFontResourceGDI final : public NativeFontResource { * @param aDataLength length of data. * @return Referenced NativeFontResourceGDI or nullptr if invalid. */ - static already_AddRefed<NativeFontResourceGDI> Create(uint8_t* aFontData, - uint32_t aDataLength); + static already_AddRefed<NativeFontResourceGDI> Create( + const uint8_t* aFontData, uint32_t aDataLength); virtual ~NativeFontResourceGDI(); diff --git a/gfx/2d/NativeFontResourceMac.cpp b/gfx/2d/NativeFontResourceMac.cpp @@ -82,7 +82,7 @@ void NativeFontResourceMac::RegisterMemoryReporter() { /* static */ already_AddRefed<NativeFontResourceMac> NativeFontResourceMac::Create( - uint8_t* aFontData, uint32_t aDataLength) { + const uint8_t* aFontData, uint32_t aDataLength) { uint8_t* fontData = (uint8_t*)malloc(aDataLength); if (!fontData) { return nullptr; diff --git a/gfx/2d/NativeFontResourceMac.h b/gfx/2d/NativeFontResourceMac.h @@ -18,8 +18,8 @@ class NativeFontResourceMac final : public NativeFontResource { public: MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(NativeFontResourceMac, override) - static already_AddRefed<NativeFontResourceMac> Create(uint8_t* aFontData, - uint32_t aDataLength); + static already_AddRefed<NativeFontResourceMac> Create( + const uint8_t* aFontData, uint32_t aDataLength); already_AddRefed<UnscaledFont> CreateUnscaledFont( uint32_t aIndex, const uint8_t* aInstanceData, diff --git a/gfx/2d/RecordedEvent.h b/gfx/2d/RecordedEvent.h @@ -87,7 +87,6 @@ struct ReferencePtr { struct RecordedFontDetails { uint64_t fontDataKey = 0; - uint32_t size = 0; uint32_t index = 0; }; @@ -541,21 +540,21 @@ class RecordedStrokeOptionsMixin { UniquePtr<Float[]> mDashPatternStorage; }; -template <typename T> +template <typename T, typename Z = size_t> class RecordedEventArray { public: T* data() { return mData.get(); } const T* data() const { return mData.get(); } - size_t size() const { return mSize; } + Z size() const { return mSize; } bool empty() const { return !mSize; } - void Assign(const T* aData, size_t aSize) { + void Assign(const T* aData, Z aSize) { mSize = aSize; mData = MakeUnique<T[]>(aSize); PodCopy(mData.get(), aData, aSize); } - bool TryAlloc(size_t aSize) { + bool TryAlloc(Z aSize) { if (mSize > 0) { MOZ_ASSERT_UNREACHABLE(); return false; @@ -568,6 +567,14 @@ class RecordedEventArray { return true; } + bool TryAssign(const T* aData, Z aSize) { + if (!TryAlloc(aSize)) { + return false; + } + PodCopy(mData.get(), aData, aSize); + return true; + } + template <typename S> void Write(S& aStream) const { if (mSize) { @@ -577,7 +584,7 @@ class RecordedEventArray { } template <typename S> - bool Read(S& aStream, size_t aSize) { + bool Read(S& aStream, Z aSize) { if (!aStream.good() || !TryAlloc(aSize)) { return false; } @@ -595,7 +602,7 @@ class RecordedEventArray { } protected: - size_t mSize = 0; + Z mSize = 0; UniquePtr<T[]> mData; }; diff --git a/gfx/2d/RecordedEventImpl.h b/gfx/2d/RecordedEventImpl.h @@ -1,4 +1,4 @@ -/* -*- 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 @@ -424,11 +424,8 @@ class RecordedDrawGlyphs : public RecordedEventDerived<Derived> { mPattern(), mOptions(aOptions) { this->StorePattern(mPattern, aPattern); - mNumGlyphs = aNumGlyphs; - mGlyphs = new Glyph[aNumGlyphs]; - memcpy(mGlyphs, aGlyphs, sizeof(Glyph) * aNumGlyphs); + mGlyphs.Assign(aGlyphs, aNumGlyphs); } - virtual ~RecordedDrawGlyphs(); bool PlayEvent(Translator* aTranslator) const override; @@ -449,8 +446,7 @@ class RecordedDrawGlyphs : public RecordedEventDerived<Derived> { ReferencePtr mScaledFont; PatternStorage mPattern; DrawOptions mOptions; - Glyph* mGlyphs = nullptr; - uint32_t mNumGlyphs = 0; + RecordedEventArray<Glyph, uint32_t> mGlyphs; }; class RecordedFillGlyphs : public RecordedDrawGlyphs<RecordedFillGlyphs> { @@ -1093,18 +1089,11 @@ class RecordedPathDestruction class RecordedSourceSurfaceCreation : public RecordedEventDerived<RecordedSourceSurfaceCreation> { public: - RecordedSourceSurfaceCreation(ReferencePtr aRefPtr, uint8_t* aData, - int32_t aStride, const IntSize& aSize, - SurfaceFormat aFormat) + RecordedSourceSurfaceCreation(ReferencePtr aRefPtr, + const RefPtr<DataSourceSurface>& aDataSurface) : RecordedEventDerived(SOURCESURFACECREATION), mRefPtr(aRefPtr), - mData(aData), - mStride(aStride), - mSize(aSize), - mFormat(aFormat), - mDataOwned(false) {} - - ~RecordedSourceSurfaceCreation(); + mDataSurface(aDataSurface) {} bool PlayEvent(Translator* aTranslator) const override; @@ -1118,11 +1107,7 @@ class RecordedSourceSurfaceCreation friend class RecordedEvent; ReferencePtr mRefPtr; - uint8_t* mData = nullptr; - int32_t mStride; - IntSize mSize; - SurfaceFormat mFormat; - mutable bool mDataOwned; + RefPtr<DataSourceSurface> mDataSurface; template <class S> MOZ_IMPLICIT RecordedSourceSurfaceCreation(S& aStream); @@ -1496,12 +1481,11 @@ class RecordedFontData : public RecordedEventDerived<RecordedFontData> { : RecordedEventDerived(FONTDATA), mType(aUnscaledFont->GetType()), mFontDetails() { - mGetFontFileDataSucceeded = - aUnscaledFont->GetFontFileData(&FontDataProc, this) && mData; + if (!aUnscaledFont->GetFontFileData(&FontDataProc, this)) { + mGetFontFileDataSucceeded = false; + } } - virtual ~RecordedFontData(); - bool IsValid() const { return mGetFontFileDataSucceeded; } bool PlayEvent(Translator* aTranslator) const override; @@ -1520,10 +1504,10 @@ class RecordedFontData : public RecordedEventDerived<RecordedFontData> { friend class RecordedEvent; FontType mType; - uint8_t* mData = nullptr; + RecordedEventArray<uint8_t, uint32_t> mData; RecordedFontDetails mFontDetails; - bool mGetFontFileDataSucceeded; + bool mGetFontFileDataSucceeded = true; template <class S> MOZ_IMPLICIT RecordedFontData(S& aStream); @@ -2775,17 +2759,7 @@ inline void RecordedFillCircle::OutputSimpleEventInfo( } template <class T> -inline RecordedDrawGlyphs<T>::~RecordedDrawGlyphs() { - delete[] mGlyphs; -} - -template <class T> inline bool RecordedDrawGlyphs<T>::PlayEvent(Translator* aTranslator) const { - if (mNumGlyphs > 0 && !mGlyphs) { - // Glyph allocation failed - return false; - } - DrawTarget* dt = aTranslator->GetCurrentDrawTarget(); if (!dt) { return false; @@ -2797,8 +2771,8 @@ inline bool RecordedDrawGlyphs<T>::PlayEvent(Translator* aTranslator) const { } GlyphBuffer buffer; - buffer.mGlyphs = mGlyphs; - buffer.mNumGlyphs = mNumGlyphs; + buffer.mGlyphs = mGlyphs.data(); + buffer.mNumGlyphs = mGlyphs.size(); DrawGlyphs(dt, scaledFont, buffer, *GenericPattern(mPattern, aTranslator)); return true; } @@ -2811,18 +2785,16 @@ RecordedDrawGlyphs<T>::RecordedDrawGlyphs(RecordedEvent::EventType aType, ReadElement(aStream, mScaledFont); ReadDrawOptions(aStream, mOptions); this->ReadPatternData(aStream, mPattern); - ReadElement(aStream, mNumGlyphs); - if (!aStream.good() || mNumGlyphs <= 0) { + uint32_t numGlyphs = 0; + ReadElement(aStream, numGlyphs); + if (!aStream.good() || numGlyphs <= 0) { return; } - mGlyphs = new (fallible) Glyph[mNumGlyphs]; - if (!mGlyphs) { + if (!mGlyphs.Read(aStream, numGlyphs)) { gfxCriticalNote << "RecordedDrawGlyphs failed to allocate glyphs of size " - << mNumGlyphs; + << numGlyphs; aStream.SetIsBad(); - } else { - aStream.read((char*)mGlyphs, sizeof(Glyph) * mNumGlyphs); } } @@ -2832,8 +2804,8 @@ void RecordedDrawGlyphs<T>::Record(S& aStream) const { WriteElement(aStream, mScaledFont); WriteElement(aStream, mOptions); this->RecordPatternData(aStream, mPattern); - WriteElement(aStream, mNumGlyphs); - aStream.write((char*)mGlyphs, sizeof(Glyph) * mNumGlyphs); + WriteElement(aStream, mGlyphs.size()); + mGlyphs.Write(aStream); } template <class T> @@ -3603,58 +3575,59 @@ inline void RecordedPathDestruction::OutputSimpleEventInfo( aStringStream << "[" << mRefPtr << "] Path Destroyed"; } -inline RecordedSourceSurfaceCreation::~RecordedSourceSurfaceCreation() { - if (mDataOwned) { - delete[] mData; - } -} - inline bool RecordedSourceSurfaceCreation::PlayEvent( Translator* aTranslator) const { - if (!mData) { + if (!mDataSurface) { return false; } - CheckedInt32 stride = CheckedInt32(mSize.width) * BytesPerPixel(mFormat); - RefPtr<SourceSurface> src; - if (!mSize.IsEmpty() && stride.isValid() && stride.value() > 0) { - src = Factory::CreateWrappingDataSourceSurface( - mData, stride.value(), mSize, mFormat, - [](void* aClosure) { delete[] static_cast<uint8_t*>(aClosure); }, - mData); - } - if (src) { - mDataOwned = false; - } - - aTranslator->AddSourceSurface(mRefPtr, src); + aTranslator->AddSourceSurface(mRefPtr, mDataSurface); return true; } template <class S> void RecordedSourceSurfaceCreation::Record(S& aStream) const { + MOZ_ASSERT(mDataSurface); + IntSize size = mDataSurface->GetSize(); + SurfaceFormat format = mDataSurface->GetFormat(); WriteElement(aStream, mRefPtr); - WriteElement(aStream, mSize); - WriteElement(aStream, mFormat); - MOZ_ASSERT(mData); - size_t dataFormatWidth = BytesPerPixel(mFormat) * mSize.width; - const char* endSrc = (const char*)(mData + (mSize.height * mStride)); - for (const char* src = (const char*)mData; src < endSrc; src += mStride) { - aStream.write(src, dataFormatWidth); + DataSourceSurface::ScopedMap map(mDataSurface, DataSourceSurface::READ); + if (map.IsMapped()) { + WriteElement(aStream, size); + WriteElement(aStream, format); + + size_t dataFormatWidth = BytesPerPixel(format) * size.width; + size_t stride = map.GetStride(); + const char* src = (const char*)map.GetData(); + const char* endSrc = src + stride * size.height; + while (src < endSrc) { + aStream.write(src, dataFormatWidth); + src += stride; + } + } else { + gfxWarning() + << "RecordedSourceSurfaceCreation::Record failed to map surface"; + WriteElement(aStream, IntSize(1, 1)); + int bpp = BytesPerPixel(format); + WriteElement(aStream, bpp <= 4 ? format : SurfaceFormat::B8G8R8A8); + static const char zeroData[4] = {0, 0, 0, 0}; + aStream.write(zeroData, BytesPerPixel(format)); } } template <class S> RecordedSourceSurfaceCreation::RecordedSourceSurfaceCreation(S& aStream) - : RecordedEventDerived(SOURCESURFACECREATION), mDataOwned(true) { + : RecordedEventDerived(SOURCESURFACECREATION) { ReadElement(aStream, mRefPtr); - ReadElement(aStream, mSize); - ReadElementConstrained(aStream, mFormat, SurfaceFormat::B8G8R8A8, + IntSize size; + ReadElement(aStream, size); + SurfaceFormat format = SurfaceFormat::UNKNOWN; + ReadElementConstrained(aStream, format, SurfaceFormat::B8G8R8A8, SurfaceFormat::UNKNOWN); - if (!Factory::AllowedSurfaceSize(mSize)) { + if (!Factory::AllowedSurfaceSize(size)) { gfxCriticalNote << "RecordedSourceSurfaceCreation read invalid size " - << mSize; + << size; aStream.SetIsBad(); } @@ -3662,31 +3635,43 @@ RecordedSourceSurfaceCreation::RecordedSourceSurfaceCreation(S& aStream) return; } - CheckedInt<size_t> size; - if (mSize.width >= 0 && mSize.height >= 0) { - CheckedInt32 stride = CheckedInt32(mSize.width) * BytesPerPixel(mFormat); - if (stride.isValid() && stride.value() >= 0) { - size = CheckedInt<size_t>(stride.value()) * size_t(mSize.height); - if (size.isValid()) { - mData = new (fallible) uint8_t[size.value()]; + mDataSurface = Factory::CreateDataSourceSurface(size, format); + if (mDataSurface) { + DataSourceSurface::ScopedMap map(mDataSurface, DataSourceSurface::WRITE); + if (!map.IsMapped()) { + mDataSurface = nullptr; + } else { + char* data = (char*)map.GetData(); + size_t stride = map.GetStride(); + size_t dataFormatWidth = BytesPerPixel(format) * size.width; + if (stride == dataFormatWidth) { + aStream.read(data, stride * size.height); + } else { + for (int y = 0; y < size.height; y++) { + aStream.read(data, dataFormatWidth); + data += stride; + } } } } - if (!mData) { + if (!mDataSurface) { gfxCriticalNote << "RecordedSourceSurfaceCreation failed to allocate data of size " - << (size.isValid() ? size.value() : 0); + << size.width << " x " << size.height; aStream.SetIsBad(); - } else { - aStream.read((char*)mData, size.value()); } } inline void RecordedSourceSurfaceCreation::OutputSimpleEventInfo( std::stringstream& aStringStream) const { - aStringStream << "[" << mRefPtr - << "] SourceSurface created (Size: " << mSize.width << "x" - << mSize.height << ")"; + if (mDataSurface) { + IntSize size = mDataSurface->GetSize(); + aStringStream << "[" << mRefPtr + << "] SourceSurface created (Size: " << size.width << "x" + << size.height << ")"; + } else { + aStringStream << "[" << mRefPtr << "] SourceSurface created (no data)"; + } } inline bool RecordedSourceSurfaceDestruction::PlayEvent( @@ -4115,15 +4100,13 @@ inline void RecordedSnapshot::OutputSimpleEventInfo( aStringStream << "[" << mRefPtr << "] Snapshot Created"; } -inline RecordedFontData::~RecordedFontData() { delete[] mData; } - inline bool RecordedFontData::PlayEvent(Translator* aTranslator) const { - if (!mData) { + if (mData.empty()) { return false; } RefPtr<NativeFontResource> fontResource = Factory::CreateNativeFontResource( - mData, mFontDetails.size, mType, aTranslator->GetFontContext()); + mData.data(), mData.size(), mType, aTranslator->GetFontContext()); if (!fontResource) { return false; } @@ -4138,31 +4121,24 @@ void RecordedFontData::Record(S& aStream) const { WriteElement(aStream, mType); WriteElement(aStream, mFontDetails.fontDataKey); - if (!mData) { - WriteElement(aStream, 0); - } else { - WriteElement(aStream, mFontDetails.size); - aStream.write((const char*)mData, mFontDetails.size); - } + WriteElement(aStream, mData.size()); + mData.Write(aStream); } inline void RecordedFontData::OutputSimpleEventInfo( std::stringstream& aStringStream) const { - aStringStream << "Font Data of size " << mFontDetails.size; + aStringStream << "Font Data of size " << mData.size(); } inline void RecordedFontData::SetFontData(const uint8_t* aData, uint32_t aSize, uint32_t aIndex) { - mData = new (fallible) uint8_t[aSize]; - if (!mData) { + if (!mData.TryAssign(aData, aSize)) { + mGetFontFileDataSucceeded = false; gfxCriticalNote << "RecordedFontData failed to allocate data for recording of size " << aSize; - } else { - memcpy(mData, aData, aSize); } mFontDetails.fontDataKey = SFNTData::GetUniqueKey(aData, aSize, 0, nullptr); - mFontDetails.size = aSize; mFontDetails.index = aIndex; } @@ -4172,7 +4148,6 @@ inline bool RecordedFontData::GetFontDetails(RecordedFontDetails& fontDetails) { } fontDetails.fontDataKey = mFontDetails.fontDataKey; - fontDetails.size = mFontDetails.size; fontDetails.index = mFontDetails.index; return true; } @@ -4182,19 +4157,17 @@ RecordedFontData::RecordedFontData(S& aStream) : RecordedEventDerived(FONTDATA), mType(FontType::UNKNOWN) { ReadElementConstrained(aStream, mType, FontType::DWRITE, FontType::UNKNOWN); ReadElement(aStream, mFontDetails.fontDataKey); - ReadElement(aStream, mFontDetails.size); - if (!mFontDetails.size || !aStream.good()) { + uint32_t dataSize = 0; + ReadElement(aStream, dataSize); + if (!dataSize || !aStream.good()) { return; } - mData = new (fallible) uint8_t[mFontDetails.size]; - if (!mData) { + if (!mData.Read(aStream, dataSize)) { gfxCriticalNote << "RecordedFontData failed to allocate data for playback of size " - << mFontDetails.size; + << dataSize; aStream.SetIsBad(); - } else { - aStream.read((char*)mData, mFontDetails.size); } }