2021-05-13 16:24:06 +00: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/. */
|
|
|
|
|
2021-11-27 11:47:38 +00:00
|
|
|
#include "BlobSurfaceProvider.h"
|
2021-05-13 16:24:06 +00:00
|
|
|
#include "AutoRestoreSVGState.h"
|
|
|
|
#include "ImageRegion.h"
|
|
|
|
#include "SVGDocumentWrapper.h"
|
2021-08-09 20:39:31 +00:00
|
|
|
#include "mozilla/PresShell.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
2021-05-13 16:24:06 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "mozilla/layers/IpcResourceUpdateQueue.h"
|
|
|
|
#include "mozilla/layers/WebRenderBridgeChild.h"
|
|
|
|
#include "mozilla/layers/WebRenderDrawEventRecorder.h"
|
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
using namespace mozilla::layers;
|
|
|
|
|
|
|
|
namespace mozilla::image {
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
BlobSurfaceProvider::BlobSurfaceProvider(
|
|
|
|
const ImageKey aImageKey, const SurfaceKey& aSurfaceKey,
|
|
|
|
image::SVGDocumentWrapper* aSVGDocumentWrapper, uint32_t aImageFlags)
|
|
|
|
: ISurfaceProvider(aImageKey, aSurfaceKey,
|
|
|
|
AvailabilityState::StartAvailable()),
|
|
|
|
mSVGDocumentWrapper(aSVGDocumentWrapper),
|
2021-05-13 16:24:06 +00:00
|
|
|
mImageFlags(aImageFlags) {
|
|
|
|
MOZ_ASSERT(mSVGDocumentWrapper);
|
|
|
|
MOZ_ASSERT(aImageFlags & imgIContainer::FLAG_RECORD_BLOB);
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
BlobSurfaceProvider::~BlobSurfaceProvider() {
|
2021-05-13 16:24:06 +00:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
DestroyKeys(mKeys);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ReleaseOnMainThread("SourceSurfaceBlobImage::mSVGDocumentWrapper",
|
|
|
|
mSVGDocumentWrapper.forget());
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableFunction("SourceSurfaceBlobImage::DestroyKeys",
|
|
|
|
[keys = std::move(mKeys)] { DestroyKeys(keys); }));
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
/* static */ void BlobSurfaceProvider::DestroyKeys(
|
2021-05-13 16:24:06 +00:00
|
|
|
const AutoTArray<BlobImageKeyData, 1>& aKeys) {
|
|
|
|
for (const auto& entry : aKeys) {
|
2022-04-07 16:07:36 +00:00
|
|
|
if (entry.mManager->IsDestroyed()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebRenderBridgeChild* wrBridge = entry.mManager->WrBridge();
|
|
|
|
if (!wrBridge || !wrBridge->MatchesNamespace(entry.mBlobKey)) {
|
|
|
|
continue;
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
2022-04-07 16:07:36 +00:00
|
|
|
|
|
|
|
entry.mManager->GetRenderRootStateManager()->AddBlobImageKeyForDiscard(
|
|
|
|
entry.mBlobKey);
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
nsresult BlobSurfaceProvider::UpdateKey(
|
|
|
|
layers::RenderRootStateManager* aManager,
|
|
|
|
wr::IpcResourceUpdateQueue& aResources, wr::ImageKey& aKey) {
|
2021-05-13 16:24:06 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
layers::WebRenderLayerManager* manager = aManager->LayerManager();
|
|
|
|
MOZ_ASSERT(manager);
|
|
|
|
|
2021-05-13 16:24:06 +00:00
|
|
|
Maybe<wr::BlobImageKey> key;
|
|
|
|
auto i = mKeys.Length();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
BlobImageKeyData& entry = mKeys[i];
|
|
|
|
if (entry.mManager->IsDestroyed()) {
|
|
|
|
mKeys.RemoveElementAt(i);
|
2021-11-27 11:47:39 +00:00
|
|
|
} else if (entry.mManager == manager) {
|
|
|
|
WebRenderBridgeChild* wrBridge = manager->WrBridge();
|
2021-05-13 16:24:06 +00:00
|
|
|
MOZ_ASSERT(wrBridge);
|
|
|
|
|
|
|
|
bool ownsKey = wrBridge->MatchesNamespace(entry.mBlobKey);
|
|
|
|
if (ownsKey && !entry.mDirty) {
|
|
|
|
key.emplace(entry.mBlobKey);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if the manager is the same, its underlying WebRenderBridgeChild
|
|
|
|
// can change state. Either our namespace differs, and our old key has
|
|
|
|
// already been discarded, or the blob has changed. Either way, we need
|
|
|
|
// to rerecord it.
|
2021-11-27 11:47:39 +00:00
|
|
|
auto newEntry = RecordDrawing(manager, aResources,
|
2021-05-13 16:24:06 +00:00
|
|
|
ownsKey ? Some(entry.mBlobKey) : Nothing());
|
|
|
|
if (!newEntry) {
|
|
|
|
if (ownsKey) {
|
2021-11-27 11:47:39 +00:00
|
|
|
aManager->AddBlobImageKeyForDiscard(entry.mBlobKey);
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
|
|
|
mKeys.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
key.emplace(newEntry.ref().mBlobKey);
|
|
|
|
entry = std::move(newEntry.ref());
|
2021-05-28 17:07:13 +00:00
|
|
|
MOZ_ASSERT(!entry.mDirty);
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find an entry. Attempt to record the blob with a new key.
|
|
|
|
if (!key) {
|
2021-11-27 11:47:39 +00:00
|
|
|
auto newEntry = RecordDrawing(manager, aResources, Nothing());
|
2021-05-13 16:24:06 +00:00
|
|
|
if (newEntry) {
|
|
|
|
key.emplace(newEntry.ref().mBlobKey);
|
|
|
|
mKeys.AppendElement(std::move(newEntry.ref()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
if (key) {
|
|
|
|
aKey = wr::AsImageKey(key.value());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 01:45:32 +00:00
|
|
|
void BlobSurfaceProvider::InvalidateSurface() {
|
2021-05-13 16:24:06 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
auto i = mKeys.Length();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
BlobImageKeyData& entry = mKeys[i];
|
|
|
|
if (entry.mManager->IsDestroyed()) {
|
|
|
|
mKeys.RemoveElementAt(i);
|
|
|
|
} else {
|
|
|
|
entry.mDirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
Maybe<BlobImageKeyData> BlobSurfaceProvider::RecordDrawing(
|
2021-05-13 16:24:06 +00:00
|
|
|
WebRenderLayerManager* aManager, wr::IpcResourceUpdateQueue& aResources,
|
|
|
|
Maybe<wr::BlobImageKey> aBlobKey) {
|
|
|
|
MOZ_ASSERT(!aManager->IsDestroyed());
|
|
|
|
|
|
|
|
if (mSVGDocumentWrapper->IsDrawing()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is either our first pass, or we have a stale key requiring us to
|
|
|
|
// re-record the SVG image draw commands.
|
|
|
|
auto* rootManager = aManager->GetRenderRootStateManager();
|
|
|
|
auto* wrBridge = aManager->WrBridge();
|
2021-05-13 16:24:08 +00:00
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
const auto& size = GetSurfaceKey().Size();
|
|
|
|
const auto& region = GetSurfaceKey().Region();
|
|
|
|
const auto& svgContext = GetSurfaceKey().SVGContext();
|
|
|
|
|
|
|
|
IntRect imageRect = region ? region->Rect() : IntRect(IntPoint(0, 0), size);
|
2021-05-13 16:24:08 +00:00
|
|
|
IntRect imageRectOrigin = imageRect - imageRect.TopLeft();
|
2021-05-13 16:24:06 +00:00
|
|
|
|
|
|
|
std::vector<RefPtr<ScaledFont>> fonts;
|
|
|
|
bool validFonts = true;
|
|
|
|
RefPtr<WebRenderDrawEventRecorder> recorder =
|
|
|
|
MakeAndAddRef<WebRenderDrawEventRecorder>(
|
|
|
|
[&](MemStream& aStream,
|
|
|
|
std::vector<RefPtr<ScaledFont>>& aScaledFonts) {
|
|
|
|
auto count = aScaledFonts.size();
|
|
|
|
aStream.write((const char*)&count, sizeof(count));
|
|
|
|
|
|
|
|
for (auto& scaled : aScaledFonts) {
|
|
|
|
Maybe<wr::FontInstanceKey> key =
|
2022-03-05 23:35:16 +00:00
|
|
|
wrBridge->GetFontKeyForScaledFont(scaled, aResources);
|
2021-05-13 16:24:06 +00:00
|
|
|
if (key.isNothing()) {
|
|
|
|
validFonts = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
BlobFont font = {key.value(), scaled};
|
|
|
|
aStream.write((const char*)&font, sizeof(font));
|
|
|
|
}
|
|
|
|
|
|
|
|
fonts = std::move(aScaledFonts);
|
|
|
|
});
|
|
|
|
|
2021-10-20 16:45:59 +00:00
|
|
|
RefPtr<DrawTarget> dummyDt =
|
|
|
|
gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget();
|
2021-05-13 16:24:06 +00:00
|
|
|
RefPtr<DrawTarget> dt =
|
2021-05-13 16:24:08 +00:00
|
|
|
Factory::CreateRecordingDrawTarget(recorder, dummyDt, imageRectOrigin);
|
2021-05-13 16:24:06 +00:00
|
|
|
|
|
|
|
if (!dt || !dt->IsValid()) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:03:25 +00:00
|
|
|
bool contextPaint = svgContext.GetContextPaint();
|
2021-05-13 16:24:08 +00:00
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
float animTime = (GetSurfaceKey().Playback() == PlaybackType::eStatic)
|
2021-08-09 20:39:31 +00:00
|
|
|
? 0.0f
|
|
|
|
: mSVGDocumentWrapper->GetCurrentTimeAsFloat();
|
2021-05-13 16:24:06 +00:00
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
IntSize viewportSize = size;
|
2022-07-14 16:03:25 +00:00
|
|
|
if (auto cssViewportSize = svgContext.GetViewportSize()) {
|
|
|
|
// XXX losing unit
|
|
|
|
viewportSize.SizeTo(cssViewportSize->width, cssViewportSize->height);
|
2021-08-09 20:39:31 +00:00
|
|
|
}
|
2021-05-13 16:24:06 +00:00
|
|
|
|
2021-08-09 20:39:31 +00:00
|
|
|
{
|
|
|
|
// Get (& sanity-check) the helper-doc's presShell
|
|
|
|
RefPtr<PresShell> presShell = mSVGDocumentWrapper->GetPresShell();
|
|
|
|
MOZ_ASSERT(presShell, "GetPresShell returned null for an SVG image?");
|
|
|
|
|
|
|
|
nsPresContext* presContext = presShell->GetPresContext();
|
|
|
|
MOZ_ASSERT(presContext, "pres shell w/out pres context");
|
|
|
|
|
|
|
|
auto* doc = presShell->GetDocument();
|
|
|
|
[[maybe_unused]] nsIURI* uri = doc ? doc->GetDocumentURI() : nullptr;
|
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING(
|
|
|
|
"SVG Image recording", GRAPHICS,
|
|
|
|
nsPrintfCString("(%d,%d) %dx%d from %dx%d %s", imageRect.x, imageRect.y,
|
2021-11-27 11:47:39 +00:00
|
|
|
imageRect.width, imageRect.height, size.width,
|
|
|
|
size.height,
|
2021-08-09 20:39:31 +00:00
|
|
|
uri ? uri->GetSpecOrDefault().get() : "N/A"));
|
|
|
|
|
2021-11-27 11:47:39 +00:00
|
|
|
AutoRestoreSVGState autoRestore(svgContext, animTime, mSVGDocumentWrapper,
|
2021-08-09 20:39:31 +00:00
|
|
|
contextPaint);
|
|
|
|
|
|
|
|
mSVGDocumentWrapper->UpdateViewportBounds(viewportSize);
|
2021-05-13 16:24:06 +00:00
|
|
|
mSVGDocumentWrapper->FlushImageTransformInvalidation();
|
|
|
|
|
2023-02-21 07:28:25 +00:00
|
|
|
gfxContext ctx(dt);
|
2021-05-13 16:24:08 +00:00
|
|
|
|
2021-08-09 20:39:31 +00:00
|
|
|
nsRect svgRect;
|
|
|
|
auto auPerDevPixel = presContext->AppUnitsPerDevPixel();
|
2021-11-27 11:47:39 +00:00
|
|
|
if (size != viewportSize) {
|
|
|
|
auto scaleX = double(size.width) / viewportSize.width;
|
|
|
|
auto scaleY = double(size.height) / viewportSize.height;
|
2023-02-21 07:28:25 +00:00
|
|
|
ctx.SetMatrix(Matrix::Scaling(float(scaleX), float(scaleY)));
|
2021-08-09 20:39:31 +00:00
|
|
|
|
|
|
|
auto scaledVisibleRect = IntRectToRect(imageRect);
|
|
|
|
scaledVisibleRect.Scale(float(auPerDevPixel / scaleX),
|
|
|
|
float(auPerDevPixel / scaleY));
|
|
|
|
scaledVisibleRect.Round();
|
|
|
|
svgRect.SetRect(
|
|
|
|
int32_t(scaledVisibleRect.x), int32_t(scaledVisibleRect.y),
|
|
|
|
int32_t(scaledVisibleRect.width), int32_t(scaledVisibleRect.height));
|
|
|
|
} else {
|
|
|
|
auto scaledVisibleRect(imageRect);
|
|
|
|
scaledVisibleRect.Scale(auPerDevPixel);
|
|
|
|
svgRect.SetRect(scaledVisibleRect.x, scaledVisibleRect.y,
|
|
|
|
scaledVisibleRect.width, scaledVisibleRect.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderDocumentFlags renderDocFlags =
|
|
|
|
RenderDocumentFlags::IgnoreViewportScrolling;
|
|
|
|
if (!(mImageFlags & imgIContainer::FLAG_SYNC_DECODE)) {
|
|
|
|
renderDocFlags |= RenderDocumentFlags::AsyncDecodeImages;
|
|
|
|
}
|
|
|
|
if (mImageFlags & imgIContainer::FLAG_HIGH_QUALITY_SCALING) {
|
|
|
|
renderDocFlags |= RenderDocumentFlags::UseHighQualityScaling;
|
|
|
|
}
|
2021-05-13 16:24:08 +00:00
|
|
|
|
2021-08-09 20:39:31 +00:00
|
|
|
presShell->RenderDocument(svgRect, renderDocFlags,
|
|
|
|
NS_RGBA(0, 0, 0, 0), // transparent
|
2023-02-21 07:28:25 +00:00
|
|
|
&ctx);
|
2021-05-13 16:24:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 16:24:08 +00:00
|
|
|
recorder->FlushItem(imageRectOrigin);
|
2021-05-13 16:24:06 +00:00
|
|
|
recorder->Finish();
|
|
|
|
|
|
|
|
if (!validFonts) {
|
|
|
|
gfxCriticalNote << "Failed serializing fonts for blob vector image";
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
Range<uint8_t> bytes((uint8_t*)recorder->mOutputStream.mData,
|
|
|
|
recorder->mOutputStream.mLength);
|
|
|
|
wr::BlobImageKey key = aBlobKey
|
|
|
|
? aBlobKey.value()
|
|
|
|
: wr::BlobImageKey{wrBridge->GetNextImageKey()};
|
2021-05-13 16:24:08 +00:00
|
|
|
wr::ImageDescriptor descriptor(imageRect.Size(), 0, SurfaceFormat::OS_RGBA,
|
2021-05-13 16:24:06 +00:00
|
|
|
wr::OpacityType::HasAlphaChannel);
|
|
|
|
|
2021-05-13 16:24:08 +00:00
|
|
|
auto visibleRect = ImageIntRect::FromUnknownRect(imageRectOrigin);
|
2021-05-13 16:24:06 +00:00
|
|
|
if (aBlobKey) {
|
|
|
|
if (!aResources.UpdateBlobImage(key, descriptor, bytes, visibleRect,
|
|
|
|
visibleRect)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
} else if (!aResources.AddBlobImage(key, descriptor, bytes, visibleRect)) {
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2023-12-23 01:34:31 +00:00
|
|
|
DrawEventRecorderPrivate::ExternalSurfacesHolder externalSurfaces;
|
2021-05-13 16:24:06 +00:00
|
|
|
recorder->TakeExternalSurfaces(externalSurfaces);
|
|
|
|
|
2023-12-23 01:34:31 +00:00
|
|
|
for (auto& entry : externalSurfaces) {
|
2021-05-13 16:24:06 +00:00
|
|
|
// While we don't use the image key with the surface, because the blob image
|
|
|
|
// renderer doesn't have easy access to the resource set, we still want to
|
|
|
|
// ensure one is generated. That will ensure the surface remains alive until
|
|
|
|
// at least the last epoch which the blob image could be used in.
|
|
|
|
wr::ImageKey key = {};
|
2023-12-23 01:34:31 +00:00
|
|
|
DebugOnly<nsresult> rv = SharedSurfacesChild::Share(
|
|
|
|
entry.mSurface, rootManager, aResources, key);
|
2021-05-13 16:24:06 +00:00
|
|
|
MOZ_ASSERT(rv.value != NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(BlobImageKeyData(aManager, key, std::move(fonts),
|
|
|
|
std::move(externalSurfaces)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla::image
|