Skip to content

Commit

Permalink
[UnifiedPDF] Generalize PDFPageCoverage
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=274036
rdar://127937268

Reviewed by Abrar Rahman Protyasha.

The concept of "a set of page and page-relative rectangles" will be used to
represent selection rects, among other things, so define `PDFPageCoverage` to
just be a `Vector<PerPageInfo>`, and use `PDFPageCoverageAndScales` for
the struct that's used for painting-related things, that has the additional
scales in it.

* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.h:
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/AsyncPDFRenderer.mm:
(WebKit::AsyncPDFRenderer::coverageRectDidChange):
(WebKit::AsyncPDFRenderer::renderInfoForTile const):
(WebKit::AsyncPDFRenderer::pdfContentChangedInRect):
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPageCoverage.h:
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFPageCoverage.mm:
(WebKit::operator<<):
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.h:
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm:
(WebKit::UnifiedPDFPlugin::pageCoverageForRect const):
(WebKit::UnifiedPDFPlugin::pageCoverageAndScalesForRect const):
(WebKit::UnifiedPDFPlugin::paintPDFContent):
(WebKit::UnifiedPDFPlugin::paintPDFSelection):

Canonical link: https://commits.webkit.org/278702@main
  • Loading branch information
smfr committed May 13, 2024
1 parent da243b5 commit 6f439da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class AsyncPDFRenderer : public WebCore::TiledBackingClient,
struct TileRenderInfo {
WebCore::FloatRect tileRect;
std::optional<WebCore::FloatRect> clipRect; // If set, represents the portion of the tile that needs repaint (in the same coordinate system as tileRect).
PDFPageCoverage pageCoverage;
PDFPageCoverageAndScales pageCoverage;
PDFContentsVersionIdentifier contentsVersion;

bool equivalentForPainting(const TileRenderInfo& other) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#if ENABLE(UNIFIED_PDF)

#include "Logging.h"
#include "PDFPageCoverage.h"
#include "UnifiedPDFPlugin.h"
#include <CoreGraphics/CoreGraphics.h>
#include <PDFKit/PDFKit.h>
Expand Down Expand Up @@ -228,7 +227,7 @@
for (auto pageIndex : m_pagePreviews.keys())
unwantedPageIndices.add(pageIndex);

for (auto& pageInfo : pageCoverage.pages) {
for (auto& pageInfo : pageCoverage) {
auto it = unwantedPageIndices.find(pageInfo.pageIndex);
if (it != unwantedPageIndices.end()) {
unwantedPageIndices.remove(it);
Expand Down Expand Up @@ -321,7 +320,7 @@
tilingScaleFactor = tiledBacking->tilingScaleFactor();

auto paintingClipRect = convertTileRectToPaintingCoords(tileRect, tilingScaleFactor);
auto pageCoverage = plugin->pageCoverageForRect(paintingClipRect);
auto pageCoverage = plugin->pageCoverageAndScalesForRect(paintingClipRect);

return TileRenderInfo { tileRect, clipRect, pageCoverage, m_contentsVersion };
}
Expand Down Expand Up @@ -626,7 +625,7 @@
return;

auto pageCoverage = plugin->pageCoverageForRect(paintingRect);
if (pageCoverage.pages.isEmpty())
if (pageCoverage.isEmpty())
return;

RetainPtr pdfDocument = plugin->pdfDocument();
Expand All @@ -652,7 +651,7 @@
}

auto pagePreviewScale = plugin->scaleForPagePreviews();
for (auto& pageInfo : pageCoverage.pages)
for (auto& pageInfo : pageCoverage)
generatePreviewImageForPage(pageInfo.pageIndex, pagePreviewScale);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ struct PerPageInfo {
bool operator==(const PerPageInfo&) const = default;
};

struct PDFPageCoverage {
Vector<PerPageInfo> pages;
using PDFPageCoverage = Vector<PerPageInfo>;

struct PDFPageCoverageAndScales {
PDFPageCoverage pages;
float deviceScaleFactor { 1 };
float pdfDocumentScale { 1 };
float tilingScaleFactor { 1 };

bool operator==(const PDFPageCoverage&) const = default;
bool operator==(const PDFPageCoverageAndScales&) const = default;
};

WTF::TextStream& operator<<(WTF::TextStream&, const PerPageInfo&);
WTF::TextStream& operator<<(WTF::TextStream&, const PDFPageCoverage&);
WTF::TextStream& operator<<(WTF::TextStream&, const PDFPageCoverageAndScales&);

} // namespace WebKit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
return ts;
}

TextStream& operator<<(TextStream& ts, const PDFPageCoverage& coverage)
TextStream& operator<<(TextStream& ts, const PDFPageCoverageAndScales& coverage)
{
ts << "PDFPageCoverage " << coverage.pages << " pdfDocumentScale " << coverage.pdfDocumentScale << " " << " tiling scale " << coverage.tilingScaleFactor;
return ts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#if ENABLE(UNIFIED_PDF)

#include "PDFDocumentLayout.h"
#include "PDFPageCoverage.h"
#include "PDFPluginBase.h"
#include <WebCore/ElementIdentifier.h>
#include <WebCore/GraphicsLayer.h>
Expand Down Expand Up @@ -63,7 +64,6 @@ class WebFrame;
class WebMouseEvent;
struct PDFContextMenu;
struct PDFContextMenuItem;
struct PDFPageCoverage;

enum class WebEventType : uint8_t;
enum class WebMouseEventButton : int8_t;
Expand Down Expand Up @@ -415,6 +415,7 @@ class UnifiedPDFPlugin final : public PDFPluginBase, public WebCore::GraphicsLay

// Package up the data needed to paint a set of pages for the given clip, for use by UnifiedPDFPlugin::paintPDFContent and async rendering.
PDFPageCoverage pageCoverageForRect(const WebCore::FloatRect& clipRect) const;
PDFPageCoverageAndScales pageCoverageAndScalesForRect(const WebCore::FloatRect& clipRect) const;

enum class PaintingBehavior : bool { All, PageContentsOnly };
enum class AllowsAsyncRendering : bool { No, Yes };
Expand Down
28 changes: 18 additions & 10 deletions Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -950,16 +950,9 @@ static String mutationObserverNotificationString()
if (m_size.isEmpty() || documentSize().isEmpty())
return { };

auto tilingScaleFactor = 1.0f;
if (auto* tiledBacking = m_contentsLayer->tiledBacking())
tilingScaleFactor = tiledBacking->tilingScaleFactor();

auto documentLayoutScale = m_documentLayout.scale();

auto pageCoverage = PDFPageCoverage { };
pageCoverage.deviceScaleFactor = deviceScaleFactor();
pageCoverage.pdfDocumentScale = documentLayoutScale;
pageCoverage.tilingScaleFactor = tilingScaleFactor;

auto drawingRect = IntRect { { }, documentSize() };
drawingRect.intersect(enclosingIntRect(clipRect));
Expand All @@ -977,12 +970,27 @@ static String mutationObserverNotificationString()
if (!pageBounds.intersects(drawingRectInPDFLayoutCoordinates))
continue;

pageCoverage.pages.append(PerPageInfo { i, pageBounds });
pageCoverage.append(PerPageInfo { i, pageBounds });
}

return pageCoverage;
}

PDFPageCoverageAndScales UnifiedPDFPlugin::pageCoverageAndScalesForRect(const WebCore::FloatRect& clipRect) const
{
auto pageCoverageAndScales = PDFPageCoverageAndScales { pageCoverageForRect(clipRect) };

auto tilingScaleFactor = 1.0f;
if (auto* tiledBacking = m_contentsLayer->tiledBacking())
tilingScaleFactor = tiledBacking->tilingScaleFactor();

pageCoverageAndScales.deviceScaleFactor = deviceScaleFactor();
pageCoverageAndScales.pdfDocumentScale = m_documentLayout.scale();
pageCoverageAndScales.tilingScaleFactor = tilingScaleFactor;

return pageCoverageAndScales;
}

void UnifiedPDFPlugin::paintPDFContent(GraphicsContext& context, const FloatRect& clipRect, PaintingBehavior behavior, AllowsAsyncRendering allowsAsyncRendering)
{
if (m_size.isEmpty() || documentSize().isEmpty())
Expand All @@ -1002,7 +1010,7 @@ static String mutationObserverNotificationString()
}

auto pageWithAnnotation = pageIndexWithHoveredAnnotation();
auto pageCoverage = pageCoverageForRect(clipRect);
auto pageCoverage = pageCoverageAndScalesForRect(clipRect);
auto documentScale = pageCoverage.pdfDocumentScale;

LOG_WITH_STREAM(PDF, stream << "UnifiedPDFPlugin: paintPDFContent " << pageCoverage);
Expand Down Expand Up @@ -1088,7 +1096,7 @@ static String mutationObserverNotificationString()
return blendSourceOver(Color::white, selectionColor);
}();

auto pageCoverage = pageCoverageForRect(clipRect);
auto pageCoverage = pageCoverageAndScalesForRect(clipRect);
auto documentScale = pageCoverage.pdfDocumentScale;
for (auto& pageInfo : pageCoverage.pages) {
auto page = m_documentLayout.pageAtIndex(pageInfo.pageIndex);
Expand Down

0 comments on commit 6f439da

Please sign in to comment.