File: AsyncPDFRenderer.h

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (275 lines) | stat: -rw-r--r-- 12,590 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (C) 2024 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#if ENABLE(UNIFIED_PDF)

#include "PDFDocumentLayout.h"
#include "PDFPageCoverage.h"
#include <WebCore/FloatRect.h>
#include <WebCore/GraphicsLayer.h>
#include <WebCore/IntPoint.h>
#include <WebCore/TiledBacking.h>
#include <limits>
#include <wtf/HashMap.h>
#include <wtf/ObjectIdentifier.h>
#include <wtf/TZoneMalloc.h>
#include <wtf/ThreadSafeRefCounted.h>
#include <wtf/ThreadSafeWeakPtr.h>
#include <wtf/WorkQueue.h>

OBJC_CLASS PDFDocument;

namespace WebKit {

struct TileForGrid {
    WebCore::TileGridIdentifier gridIdentifier;
    WebCore::TileIndex tileIndex;

    bool operator==(const TileForGrid&) const = default;

    unsigned computeHash() const
    {
        return WTF::computeHash(gridIdentifier.toUInt64(), tileIndex.x(), tileIndex.y());
    }
};

WTF::TextStream& operator<<(WTF::TextStream&, const TileForGrid&);

struct PDFTileRenderType;
using PDFTileRenderIdentifier = ObjectIdentifier<PDFTileRenderType>;

struct TileRenderInfo {
    WebCore::FloatRect tileRect;
    WebCore::FloatRect renderRect; // Represents the portion of the tile that needs rendering (in the same coordinate system as tileRect).
    RefPtr<WebCore::NativeImage> background; // Optional existing content around renderRect, will be rendered to tileRect.
    PDFPageCoverageAndScales pageCoverage;
    bool showDebugIndicators { false };

    bool operator==(const TileRenderInfo&) const = default;
    bool equivalentForPainting(const TileRenderInfo& other) const
    {
        return tileRect == other.tileRect && pageCoverage == other.pageCoverage;
    }
};

WTF::TextStream& operator<<(WTF::TextStream&, const TileRenderInfo&);

struct TileRenderData {
    PDFTileRenderIdentifier renderIdentifier;
    TileRenderInfo renderInfo;
};

WTF::TextStream& operator<<(WTF::TextStream&, const TileRenderData&);

struct PDFPagePreviewRenderKey {
    PDFDocumentLayout::PageIndex pageIndex { std::numeric_limits<PDFDocumentLayout::PageIndex>::max() };
    bool operator==(const PDFPagePreviewRenderKey&) const = default;
};

struct PDFPagePreviewRenderRequest {
    PDFDocumentLayout::PageIndex pageIndex;
    WebCore::FloatRect normalizedPageBounds;
    float scale { 1.0f };
    bool showDebugIndicators { false };
};

struct PDFPagePreviewRenderKeyHash {
    static unsigned hash(const PDFPagePreviewRenderKey& value)
    {
        return WTF::computeHash(value.pageIndex);
    }
    static bool equal(const PDFPagePreviewRenderKey& a, const PDFPagePreviewRenderKey& b)
    {
        return a == b;
    }
    static const bool safeToCompareToEmptyOrDeleted = true;
};

} // namespace WebKit

namespace WTF {

struct TileForGridHash {
    static unsigned hash(const WebKit::TileForGrid& key)
    {
        return key.computeHash();
    }
    static bool equal(const WebKit::TileForGrid& a, const WebKit::TileForGrid& b)
    {
        return a == b;
    }
    static const bool safeToCompareToEmptyOrDeleted = true;
};

template<> struct HashTraits<WebKit::TileForGrid> : GenericHashTraits<WebKit::TileForGrid> {
    static constexpr bool emptyValueIsZero = true;
    static WebKit::TileForGrid emptyValue() { return { HashTraits<WebCore::TileGridIdentifier>::emptyValue(), { 0, 0 } }; }
    static bool isEmptyValue(const WebKit::TileForGrid& value) { return value.gridIdentifier.isHashTableEmptyValue(); }
    static void constructDeletedValue(WebKit::TileForGrid& tileForGrid) { HashTraits<WebCore::TileGridIdentifier>::constructDeletedValue(tileForGrid.gridIdentifier); }
    static bool isDeletedValue(const WebKit::TileForGrid& tileForGrid) { return tileForGrid.gridIdentifier.isHashTableDeletedValue(); }
};
template<> struct DefaultHash<WebKit::TileForGrid> : TileForGridHash { };

template<> struct HashTraits<WebKit::TileRenderData> : SimpleClassHashTraits<WebKit::TileRenderData> {
    static constexpr bool emptyValueIsZero = false;
    static constexpr bool hasIsEmptyValueFunction = true;
    static WebKit::TileRenderData emptyValue() { return { HashTraits<WebKit::PDFTileRenderIdentifier>::emptyValue(), { } }; }
    static bool isEmptyValue(const WebKit::TileRenderData& data) { return HashTraits<WebKit::PDFTileRenderIdentifier>::isEmptyValue(data.renderIdentifier); }
};

template<> struct HashTraits<WebKit::PDFPagePreviewRenderKey> : GenericHashTraits<WebKit::PDFPagePreviewRenderKey> {
    static constexpr bool emptyValueIsZero = false;
    static WebKit::PDFPagePreviewRenderKey emptyValue() { return { std::numeric_limits<WebKit::PDFDocumentLayout::PageIndex>::max() }; }
    static void constructDeletedValue(WebKit::PDFPagePreviewRenderKey& slot) { slot = { std::numeric_limits<WebKit::PDFDocumentLayout::PageIndex>::max() - 1 }; }
    static bool isDeletedValue(WebKit::PDFPagePreviewRenderKey value) { return value.pageIndex == std::numeric_limits<WebKit::PDFDocumentLayout::PageIndex>::max() - 1; }
};

template<> struct DefaultHash<WebKit::PDFPagePreviewRenderKey> : WebKit::PDFPagePreviewRenderKeyHash {
};

} // namespace WTF

namespace WebKit {

class PDFPresentationController;

class AsyncPDFRenderer final : public WebCore::TiledBackingClient,
    public ThreadSafeRefCountedAndCanMakeThreadSafeWeakPtr<AsyncPDFRenderer> {
    WTF_MAKE_TZONE_ALLOCATED(AsyncPDFRenderer);
    WTF_MAKE_NONCOPYABLE(AsyncPDFRenderer);
public:
    static Ref<AsyncPDFRenderer> create(PDFPresentationController&);

    virtual ~AsyncPDFRenderer();

    void startTrackingLayer(WebCore::GraphicsLayer&);
    void stopTrackingLayer(WebCore::GraphicsLayer&);
    void teardown();

    void releaseMemory();

    bool paintTilesForPage(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, float documentScale, const WebCore::FloatRect& clipRect, const WebCore::FloatRect& clipRectInPageCoordinates, const WebCore::FloatRect& pageBoundsInPaintingCoordinates, PDFDocumentLayout::PageIndex);
    void paintPagePreview(WebCore::GraphicsContext&, const WebCore::FloatRect& clipRect, const WebCore::FloatRect& pageBoundsInPaintingCoordinates, PDFDocumentLayout::PageIndex);

    // Throws away existing tiles. Can result in flashing.
    void invalidateTilesForPaintingRect(float pageScaleFactor, const WebCore::FloatRect& paintingRect);

    // Updates existing tiles. Can result in temporarily stale content.
    void setNeedsRenderForRect(WebCore::GraphicsLayer&, const WebCore::FloatRect& bounds);
    void setNeedsPagePreviewRenderForPageCoverage(const PDFPageCoverage&);

    void generatePreviewImageForPage(PDFDocumentLayout::PageIndex, float scale);
    void removePreviewForPage(PDFDocumentLayout::PageIndex);

    void setShowDebugBorders(bool);

private:
    AsyncPDFRenderer(PDFPresentationController&);

    RefPtr<WebCore::GraphicsLayer> 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<WebCore::NativeImage>&& background) const;

    bool renderInfoIsValidForTile(WebCore::TiledBacking&, const TileForGrid&, const TileRenderInfo&) const;

    // TiledBackingClient
    void willRepaintTile(WebCore::TiledBacking&, WebCore::TileGridIdentifier, WebCore::TileIndex, const WebCore::FloatRect& tileRect, const WebCore::FloatRect& tileDirtyRect) final;
    void willRemoveTile(WebCore::TiledBacking&, WebCore::TileGridIdentifier, WebCore::TileIndex) final;
    void willRepaintAllTiles(WebCore::TiledBacking&, WebCore::TileGridIdentifier) final;

    void coverageRectDidChange(WebCore::TiledBacking&, const WebCore::FloatRect&) final;

    void willRevalidateTiles(WebCore::TiledBacking&, WebCore::TileGridIdentifier, WebCore::TileRevalidationType) final;
    void didRevalidateTiles(WebCore::TiledBacking&, WebCore::TileGridIdentifier, WebCore::TileRevalidationType, const HashSet<WebCore::TileIndex>& tilesNeedingDisplay) final;

    void willRepaintTilesAfterScaleFactorChange(WebCore::TiledBacking&, WebCore::TileGridIdentifier) final;
    void didRepaintTilesAfterScaleFactorChange(WebCore::TiledBacking&, WebCore::TileGridIdentifier) final;

    void didAddGrid(WebCore::TiledBacking&, WebCore::TileGridIdentifier) final;
    void willRemoveGrid(WebCore::TiledBacking&, WebCore::TileGridIdentifier) final;

    std::optional<PDFTileRenderIdentifier> enqueueTileRenderForTileGridRepaint(WebCore::TiledBacking&, WebCore::TileGridIdentifier, WebCore::TileIndex, const WebCore::FloatRect& tileRect, const WebCore::FloatRect& tileDirtyRect);
    std::optional<PDFTileRenderIdentifier> enqueueTileRenderIfNecessary(const TileForGrid&, TileRenderInfo&&);

    void serviceRequestQueues();

    void didCompleteTileRender(RefPtr<WebCore::NativeImage>&&, const TileForGrid&, const TileRenderData&);

    struct RevalidationStateForGrid {
        WTF_MAKE_STRUCT_FAST_ALLOCATED;
        bool inFullTileRevalidation { false };
        bool inScaleChangeRepaint { false };
        HashSet<PDFTileRenderIdentifier> renderIdentifiersForCurrentRevalidation;
    };

    RevalidationStateForGrid& revalidationStateForGrid(WebCore::TileGridIdentifier);
    void trackRendersForStaleTileMaintenance(WebCore::TileGridIdentifier, HashSet<PDFTileRenderIdentifier>&&);
    void trackRenderCompletionForStaleTileMaintenance(WebCore::TileGridIdentifier, PDFTileRenderIdentifier);

    void clearRequestsAndCachedTiles();

    void didCompletePagePreviewRender(RefPtr<WebCore::NativeImage>&&, const PDFPagePreviewRenderRequest&);
    void removePagePreviewsOutsideCoverageRect(const WebCore::FloatRect&, const std::optional<PDFLayoutRow>& = { });

    static WebCore::FloatRect convertTileRectToPaintingCoords(const WebCore::FloatRect&, float pageScaleFactor);
    static WebCore::AffineTransform tileToPaintingTransform(float tilingScaleFactor);
    static WebCore::AffineTransform paintingToTileTransform(float tilingScaleFactor);

    ThreadSafeWeakPtr<PDFPresentationController> m_presentationController;

    HashMap<WebCore::PlatformLayerIdentifier, Ref<WebCore::GraphicsLayer>> m_layerIDtoLayerMap;
    HashMap<WebCore::TileGridIdentifier, WebCore::PlatformLayerIdentifier> m_tileGridToLayerIDMap;

    const Ref<ConcurrentWorkQueue> m_workQueue;
    int m_workQueueSlots { 4 };

    ListHashSet<TileForGrid> m_pendingTileRenderOrder;
    HashMap<TileForGrid, TileRenderData> m_pendingTileRenders;
    struct RenderedTile {
        RefPtr<WebCore::NativeImage> image;
        TileRenderInfo tileInfo;
    };
    HashMap<TileForGrid, RenderedTile> m_rendereredTiles;
    HashMap<TileForGrid, RenderedTile> m_rendereredTilesForOldState;

    HashMap<WebCore::TileGridIdentifier, std::unique_ptr<RevalidationStateForGrid>> m_gridRevalidationState;

    struct RenderedPDFPagePreview {
        RefPtr<WebCore::NativeImage> image;
        float scale { 1.0f };
    };
    ListHashSet<PDFPagePreviewRenderKey> m_pendingPagePreviewOrder;
    HashMap<PDFPagePreviewRenderKey, PDFPagePreviewRenderRequest> m_pendingPagePreviews;
    HashMap<PDFPagePreviewRenderKey, RenderedPDFPagePreview> m_pagePreviews;

    bool m_showDebugBorders { false };
};

} // namespace WebKit

#endif // ENABLE(UNIFIED_PDF)