diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.h b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.h index 27023d395bb26..ff6eec91b7e68 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.h +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.h @@ -155,7 +155,7 @@ class AsyncPDFRenderer final : public WebCore::TiledBackingClient, private: AsyncPDFRenderer(PDFPresentationController&); - WebCore::GraphicsLayer* layerForTileGrid(WebCore::TileGridIdentifier) const; + RefPtr layerForTileGrid(WebCore::TileGridIdentifier) const; TileRenderInfo renderInfoForFullTile(const WebCore::TiledBacking&, const TileForGrid& tileInfo, const WebCore::FloatRect& tileRect) const; TileRenderInfo renderInfoForTile(const WebCore::TiledBacking&, const TileForGrid& tileInfo, const WebCore::FloatRect& tileRect, const WebCore::FloatRect& renderRect, RefPtr&& background) const; diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.mm b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.mm index af51b1dd7e02e..93c2d5fa8131d 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.mm +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.mm @@ -84,10 +84,10 @@ if (!presentationController) return; - for (auto& [layerID, layer] : m_layerIDtoLayerMap) { + for (Ref layer : m_layerIDtoLayerMap.values()) { // Ideally we'd be able to make the ImageBuffer memory volatile which would eliminate the need for this callback: webkit.org/b/274878 if (auto* tiledBacking = layer->tiledBacking()) - removePagePreviewsOutsideCoverageRect(tiledBacking->coverageRect(), presentationController->rowForLayerID(layerID)); + removePagePreviewsOutsideCoverageRect(tiledBacking->coverageRect(), presentationController->rowForLayer(layer.ptr())); } LOG_WITH_STREAM(PDFAsyncRendering, stream << "AsyncPDFRenderer::releaseMemory - reduced page preview count from " << oldPagePreviewCount << " to " << m_pagePreviews.size()); @@ -95,28 +95,27 @@ void AsyncPDFRenderer::startTrackingLayer(GraphicsLayer& layer) { - Ref layerRef = layer; - if (auto* tiledBacking = layerRef->tiledBacking()) { - tiledBacking->setClient(this); - m_tileGridToLayerIDMap.set(tiledBacking->primaryGridIdentifier(), *layer.primaryLayerID()); - } - - m_layerIDtoLayerMap.set(*layer.primaryLayerID(), WTFMove(layerRef)); + auto* tiledBacking = layer.tiledBacking(); + if (!tiledBacking) + return; + tiledBacking->setClient(this); + m_tileGridToLayerIDMap.set(tiledBacking->primaryGridIdentifier(), *layer.primaryLayerID()); + m_layerIDtoLayerMap.set(*layer.primaryLayerID(), layer); } void AsyncPDFRenderer::stopTrackingLayer(GraphicsLayer& layer) { - if (auto* tiledBacking = layer.tiledBacking()) { - auto gridIdentifier = tiledBacking->primaryGridIdentifier(); - m_tileGridToLayerIDMap.remove(gridIdentifier); - m_gridRevalidationState.remove(gridIdentifier); - tiledBacking->setClient(nullptr); - } - + auto* tiledBacking = layer.tiledBacking(); + if (!tiledBacking) + return; + auto gridIdentifier = tiledBacking->primaryGridIdentifier(); + m_tileGridToLayerIDMap.remove(gridIdentifier); + m_gridRevalidationState.remove(gridIdentifier); + tiledBacking->setClient(nullptr); m_layerIDtoLayerMap.remove(*layer.primaryLayerID()); } -GraphicsLayer* AsyncPDFRenderer::layerForTileGrid(TileGridIdentifier identifier) const +RefPtr AsyncPDFRenderer::layerForTileGrid(TileGridIdentifier identifier) const { auto layerID = m_tileGridToLayerIDMap.getOptional(identifier); if (!layerID) @@ -286,12 +285,9 @@ return; std::optional layoutRow; - RefPtr layer; - auto layerID = m_tileGridToLayerIDMap.getOptional(tiledBacking.primaryGridIdentifier()); - if (layerID) { - layoutRow = presentationController->rowForLayerID(*layerID); - layer = m_layerIDtoLayerMap.get(*layerID); - } + RefPtr layer = layerForTileGrid(tiledBacking.primaryGridIdentifier()); + if (layer) + layoutRow = presentationController->rowForLayer(layer.get()); auto pageCoverage = presentationController->pageCoverageForContentsRect(coverageRect, layoutRow); @@ -506,9 +502,8 @@ auto paintingClipRect = convertTileRectToPaintingCoords(tileRect, tilingScaleFactor); std::optional layoutRow; - auto layerID = m_tileGridToLayerIDMap.getOptional(tileInfo.gridIdentifier); - if (layerID) - layoutRow = presentationController->rowForLayerID(*layerID); + if (RefPtr layer = layerForTileGrid(tileInfo.gridIdentifier)) + layoutRow = presentationController->rowForLayer(layer.get()); auto pageCoverage = presentationController->pageCoverageAndScalesForContentsRect(paintingClipRect, layoutRow, tilingScaleFactor); diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.h b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.h index da79ca0a66dd5..7cb85c59e5f8b 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.h +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.h @@ -85,7 +85,7 @@ class PDFDiscretePresentationController final : public PDFPresentationController WebCore::GraphicsLayerClient& graphicsLayerClient() override { return *this; } std::optional visibleRow() const override; - std::optional rowForLayerID(WebCore::PlatformLayerIdentifier) const override; + std::optional rowForLayer(const WebCore::GraphicsLayer*) const override; WebCore::FloatSize contentsOffsetForPage(PDFDocumentLayout::PageIndex) const; @@ -216,7 +216,7 @@ class PDFDiscretePresentationController final : public PDFPresentationController #endif }; - const RowData* rowDataForLayerID(WebCore::PlatformLayerIdentifier) const; + const RowData* rowDataForLayer(const WebCore::GraphicsLayer*) const; WebCore::FloatPoint positionForRowContainerLayer(const PDFLayoutRow&) const; WebCore::FloatSize rowContainerSize(const PDFLayoutRow&) const; @@ -225,7 +225,7 @@ class PDFDiscretePresentationController final : public PDFPresentationController RefPtr m_rowsContainerLayer; Vector m_rows; - HashMap m_layerIDToRowIndexMap; + HashMap m_layerToRowIndexMap; std::optional m_displayModeAtLastLayerSetup; unsigned m_visibleRowIndex { 0 }; diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.mm b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.mm index fb6082c37c193..cc1fc6fa3b33b 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.mm +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDiscretePresentationController.mm @@ -1069,7 +1069,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci row.leftPageContainerLayer = makePageContainerLayer(leftPageIndex); RefPtr pageBackgroundLayer = pageBackgroundLayerForPageContainerLayer(*row.protectedLeftPageContainerLayer()); - m_layerIDToRowIndexMap.add(*pageBackgroundLayer->primaryLayerID(), rowIndex); + m_layerToRowIndexMap.add(pageBackgroundLayer.get(), rowIndex); if (row.pages.numPages() == 1) { ASSERT(!row.rightPageContainerLayer); @@ -1079,7 +1079,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci auto rightPageIndex = layoutRow.pages[1]; row.rightPageContainerLayer = makePageContainerLayer(rightPageIndex); RefPtr rightPageBackgroundLayer = pageBackgroundLayerForPageContainerLayer(*row.protectedRightPageContainerLayer()); - m_layerIDToRowIndexMap.add(*rightPageBackgroundLayer->primaryLayerID(), rowIndex); + m_layerToRowIndexMap.add(rightPageBackgroundLayer.get(), rowIndex); }; auto parentRowLayers = [](RowData& row) { @@ -1112,7 +1112,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci // This is the call that enables async rendering. asyncRenderer()->startTrackingLayer(*rowContentsLayer); - m_layerIDToRowIndexMap.set(*rowContentsLayer->primaryLayerID(), rowIndex); + m_layerToRowIndexMap.set(rowContentsLayer.get(), rowIndex); #if ENABLE(UNIFIED_PDF_SELECTION_LAYER) RefPtr rowSelectionLayer = row.selectionLayer = createGraphicsLayer(makeString("Row selection "_s, rowIndex), GraphicsLayer::Type::TiledBacking); @@ -1120,7 +1120,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci rowSelectionLayer->setDrawsContent(true); rowSelectionLayer->setAcceleratesDrawing(true); rowSelectionLayer->setBlendMode(BlendMode::Multiply); - m_layerIDToRowIndexMap.set(*rowSelectionLayer->primaryLayerID(), rowIndex); + m_layerToRowIndexMap.set(rowSelectionLayer.get(), rowIndex); #endif parentRowLayers(row); @@ -1446,7 +1446,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci std::optional PDFDiscretePresentationController::customContentsScale(const GraphicsLayer* layer) const { - auto* rowData = rowDataForLayerID(*layer->primaryLayerID()); + auto* rowData = rowDataForLayer(layer); if (!rowData) return { }; @@ -1464,7 +1464,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci // We need a platform context if the plugin can not paint selections into its own layer, // since we would then have to vend a platform context that PDFKit can paint into. // However, this constraint only applies for the contents layer. No other layer needs to be WP-backed. - auto* rowData = rowDataForLayerID(*layer->primaryLayerID()); + auto* rowData = rowDataForLayer(layer); if (!rowData) return false; @@ -1503,9 +1503,9 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci } } -auto PDFDiscretePresentationController::rowDataForLayerID(PlatformLayerIdentifier layerID) const -> const RowData* +auto PDFDiscretePresentationController::rowDataForLayer(const GraphicsLayer* layer) const -> const RowData* { - auto rowIndex = m_layerIDToRowIndexMap.getOptional(layerID); + auto rowIndex = m_layerToRowIndexMap.getOptional(layer); if (!rowIndex) return nullptr; @@ -1523,9 +1523,9 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci return m_rows[m_visibleRowIndex].pages; } -std::optional PDFDiscretePresentationController::rowForLayerID(PlatformLayerIdentifier layerID) const +std::optional PDFDiscretePresentationController::rowForLayer(const GraphicsLayer* layer) const { - auto* rowData = rowDataForLayerID(layerID); + auto* rowData = rowDataForLayer(layer); if (rowData) return rowData->pages; @@ -1534,7 +1534,7 @@ static float elasticDeltaForTimeDelta(float initialPosition, float initialVeloci void PDFDiscretePresentationController::paintContents(const GraphicsLayer* layer, GraphicsContext& context, const FloatRect& clipRect, OptionSet) { - auto rowIndex = m_layerIDToRowIndexMap.getOptional(*layer->primaryLayerID()); + auto rowIndex = m_layerToRowIndexMap.getOptional(layer); if (!rowIndex) return; diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.h b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.h index 011c509c69321..54a7fb3b56879 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.h +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.h @@ -92,7 +92,7 @@ class PDFPresentationController : public ThreadSafeRefCountedAndCanMakeThreadSaf virtual void setNeedsRepaintInDocumentRect(OptionSet, const WebCore::FloatRect& rectInDocumentCoordinates, std::optional) = 0; virtual std::optional visibleRow() const { return { }; } - virtual std::optional rowForLayerID(WebCore::PlatformLayerIdentifier) const { return { }; } + virtual std::optional rowForLayer(const WebCore::GraphicsLayer*) const { return { }; } struct VisiblePDFPosition { PDFDocumentLayout::PageIndex pageIndex { 0 }; diff --git a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.mm b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.mm index 337bff0b92b75..f18aad11dca2b 100644 --- a/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.mm +++ b/Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPresentationController.mm @@ -124,6 +124,7 @@ pageBackgroundLayer->setDrawsContent(true); pageBackgroundLayer->setAcceleratesDrawing(true); pageBackgroundLayer->setShouldUpdateRootRelativeScaleFactor(false); + pageBackgroundLayer->setAllowsTiling(false); pageBackgroundLayer->setNeedsDisplay(); // We only need to paint this layer once when page backgrounds change. // FIXME: Need to add a 1px black border with alpha 0.0586.