⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 278565 in webkit


Ignore:
Timestamp:
Jun 7, 2021, 11:53:54 AM (5 years ago)
Author:
Devin Rousso
Message:

Convert WebCore::SnapshotOptions into an enum class
https://bugs.webkit.org/show_bug.cgi?id=226730

Reviewed by Wenson Hsieh.

Convert SnapshotOptions into an enum class SnapshotFlags and create a container `struct
SnapshotOptions that also allows for changing the DestinationColorSpace` (defaults to sRGB)
and PixelFormat (defaults to BGRA8).

No behavior change.

Source/WebCore:

  • page/FrameSnapshotting.h:
  • page/FrameSnapshotting.cpp:

(WebCore::snapshotFrameRect):
(WebCore::snapshotFrameRectWithClip):
(WebCore::snapshotSelection):
(WebCore::snapshotNode):

  • inspector/agents/InspectorPageAgent.cpp:

(WebCore::InspectorPageAgent::snapshotRect):

  • page/PageColorSampler.cpp:

(WebCore::sampleColor):

  • page/PageConsoleClient.cpp:

(WebCore::PageConsoleClient::screenshot):

  • page/TextIndicator.cpp:

(WebCore::snapshotOptionsForTextIndicatorOptions):
(WebCore::takeSnapshot):
(WebCore::takeSnapshots):

  • platform/DragImage.cpp:

(WebCore::createDragImageForSelection):
(WebCore::createDragImageForRange):

Source/WebKit:

  • WebProcess/WebPage/WebFrame.cpp:

(WebKit::WebFrame::createSelectionSnapshot const):

Location:
trunk/Source
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r278563 r278565  
     12021-06-07  Devin Rousso  <drousso@apple.com>
     2
     3        Convert WebCore::SnapshotOptions into an enum class
     4        https://bugs.webkit.org/show_bug.cgi?id=226730
     5
     6        Reviewed by Wenson Hsieh.
     7
     8        Convert `SnapshotOptions` into an `enum class SnapshotFlags` and create a container `struct
     9        SnapshotOptions` that also allows for changing the `DestinationColorSpace` (defaults to sRGB)
     10        and `PixelFormat` (defaults to BGRA8).
     11
     12        No behavior change.
     13
     14        * page/FrameSnapshotting.h:
     15        * page/FrameSnapshotting.cpp:
     16        (WebCore::snapshotFrameRect):
     17        (WebCore::snapshotFrameRectWithClip):
     18        (WebCore::snapshotSelection):
     19        (WebCore::snapshotNode):
     20
     21        * inspector/agents/InspectorPageAgent.cpp:
     22        (WebCore::InspectorPageAgent::snapshotRect):
     23        * page/PageColorSampler.cpp:
     24        (WebCore::sampleColor):
     25        * page/PageConsoleClient.cpp:
     26        (WebCore::PageConsoleClient::screenshot):
     27        * page/TextIndicator.cpp:
     28        (WebCore::snapshotOptionsForTextIndicatorOptions):
     29        (WebCore::takeSnapshot):
     30        (WebCore::takeSnapshots):
     31        * platform/DragImage.cpp:
     32        (WebCore::createDragImageForSelection):
     33        (WebCore::createDragImageForRange):
     34
    1352021-06-07  Imanol Fernandez  <ifernandez@igalia.com>
    236
  • trunk/Source/WebCore/inspector/agents/InspectorPageAgent.cpp

    r278516 r278565  
    10821082Protocol::ErrorStringOr<String> InspectorPageAgent::snapshotRect(int x, int y, int width, int height, Protocol::Page::CoordinateSystem coordinateSystem)
    10831083{
    1084     SnapshotOptions options = SnapshotOptionsNone;
     1084    SnapshotOptions options;
    10851085    if (coordinateSystem == Protocol::Page::CoordinateSystem::Viewport)
    1086         options |= SnapshotOptionsInViewCoordinates;
     1086        options.flags.add(SnapshotFlags::InViewCoordinates);
    10871087
    10881088    IntRect rectangle(x, y, width, height);
    1089     auto snapshot = snapshotFrameRect(m_inspectedPage.mainFrame(), rectangle, options);
     1089    auto snapshot = snapshotFrameRect(m_inspectedPage.mainFrame(), rectangle, WTFMove(options));
    10901090
    10911091    if (!snapshot)
  • trunk/Source/WebCore/page/FrameSnapshotting.cpp

    r277986 r278565  
    3232#include "FrameSnapshotting.h"
    3333
     34#include "DestinationColorSpace.h"
    3435#include "Document.h"
    3536#include "FloatRect.h"
     
    4041#include "ImageBuffer.h"
    4142#include "Page.h"
     43#include "PixelFormat.h"
    4244#include "RenderObject.h"
    4345#include "Settings.h"
     46#include <wtf/OptionSet.h>
    4447
    4548namespace WebCore {
     
    6871};
    6972
    70 RefPtr<ImageBuffer> snapshotFrameRect(Frame& frame, const IntRect& imageRect, SnapshotOptions options)
     73RefPtr<ImageBuffer> snapshotFrameRect(Frame& frame, const IntRect& imageRect, SnapshotOptions&& options)
    7174{
    7275    Vector<FloatRect> clipRects;
    73     return snapshotFrameRectWithClip(frame, imageRect, clipRects, options);
     76    return snapshotFrameRectWithClip(frame, imageRect, clipRects, WTFMove(options));
    7477}
    7578
    76 RefPtr<ImageBuffer> snapshotFrameRectWithClip(Frame& frame, const IntRect& imageRect, const Vector<FloatRect>& clipRects, SnapshotOptions options)
     79RefPtr<ImageBuffer> snapshotFrameRectWithClip(Frame& frame, const IntRect& imageRect, const Vector<FloatRect>& clipRects, SnapshotOptions&& options)
    7780{
    7881    if (!frame.page())
     
    8285
    8386    FrameView::SelectionInSnapshot shouldIncludeSelection = FrameView::IncludeSelection;
    84     if (options & SnapshotOptionsExcludeSelectionHighlighting)
     87    if (options.flags.contains(SnapshotFlags::ExcludeSelectionHighlighting))
    8588        shouldIncludeSelection = FrameView::ExcludeSelection;
    8689
    8790    FrameView::CoordinateSpaceForSnapshot coordinateSpace = FrameView::DocumentCoordinates;
    88     if (options & SnapshotOptionsInViewCoordinates)
     91    if (options.flags.contains(SnapshotFlags::InViewCoordinates))
    8992        coordinateSpace = FrameView::ViewCoordinates;
    9093
     
    9295
    9396    auto paintBehavior = state.paintBehavior;
    94     if (options & SnapshotOptionsForceBlackText)
     97    if (options.flags.contains(SnapshotFlags::ForceBlackText))
    9598        paintBehavior.add(PaintBehavior::ForceBlackText);
    96     if (options & SnapshotOptionsPaintSelectionOnly)
     99    if (options.flags.contains(SnapshotFlags::PaintSelectionOnly))
    97100        paintBehavior.add(PaintBehavior::SelectionOnly);
    98     if (options & SnapshotOptionsPaintSelectionAndBackgroundsOnly)
     101    if (options.flags.contains(SnapshotFlags::PaintSelectionAndBackgroundsOnly))
    99102        paintBehavior.add(PaintBehavior::SelectionAndBackgroundsOnly);
    100     if (options & SnapshotOptionsPaintEverythingExcludingSelection)
     103    if (options.flags.contains(SnapshotFlags::PaintEverythingExcludingSelection))
    101104        paintBehavior.add(PaintBehavior::ExcludeSelection);
    102105
     
    109112        scaleFactor *= frame.page()->pageScaleFactor();
    110113
    111     if (options & SnapshotOptionsPaintWithIntegralScaleFactor)
     114    if (options.flags.contains(SnapshotFlags::PaintWithIntegralScaleFactor))
    112115        scaleFactor = ceilf(scaleFactor);
    113116
    114     auto buffer = ImageBuffer::create(imageRect.size(), RenderingMode::Unaccelerated, scaleFactor, DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
     117    auto buffer = ImageBuffer::create(imageRect.size(), RenderingMode::Unaccelerated, scaleFactor, options.colorSpace.value_or(DestinationColorSpace::SRGB()), options.pixelFormat.value_or(PixelFormat::BGRA8));
    115118    if (!buffer)
    116119        return nullptr;
     
    128131}
    129132
    130 RefPtr<ImageBuffer> snapshotSelection(Frame& frame, SnapshotOptions options)
     133RefPtr<ImageBuffer> snapshotSelection(Frame& frame, SnapshotOptions&& options)
    131134{
    132135    auto& selection = frame.selection();
     
    141144        return nullptr;
    142145
    143     options |= SnapshotOptionsPaintSelectionOnly;
    144     return snapshotFrameRect(frame, enclosingIntRect(selectionBounds), options);
     146    options.flags.add(SnapshotFlags::PaintSelectionOnly);
     147    return snapshotFrameRect(frame, enclosingIntRect(selectionBounds), WTFMove(options));
    145148}
    146149
    147 RefPtr<ImageBuffer> snapshotNode(Frame& frame, Node& node)
     150RefPtr<ImageBuffer> snapshotNode(Frame& frame, Node& node, SnapshotOptions&& options)
    148151{
    149152    if (!node.renderer())
     
    156159
    157160    LayoutRect topLevelRect;
    158     return snapshotFrameRect(frame, snappedIntRect(node.renderer()->paintingRootRect(topLevelRect)));
     161    return snapshotFrameRect(frame, snappedIntRect(node.renderer()->paintingRootRect(topLevelRect)), WTFMove(options));
    159162}
    160163
  • trunk/Source/WebCore/page/FrameSnapshotting.h

    r269323 r278565  
    3131
    3232#include <memory>
     33#include <optional>
    3334#include <wtf/Forward.h>
    3435
    3536namespace WebCore {
    3637
     38class DestinationColorSpace;
    3739class FloatRect;
    3840class Frame;
     
    4042class ImageBuffer;
    4143class Node;
     44enum class PixelFormat : uint8_t;
    4245
    43 enum {
    44     SnapshotOptionsNone = 0,
    45     SnapshotOptionsExcludeSelectionHighlighting = 1 << 0,
    46     SnapshotOptionsPaintSelectionOnly = 1 << 1,
    47     SnapshotOptionsInViewCoordinates = 1 << 2,
    48     SnapshotOptionsForceBlackText = 1 << 3,
    49     SnapshotOptionsPaintSelectionAndBackgroundsOnly = 1 << 4,
    50     SnapshotOptionsPaintEverythingExcludingSelection = 1 << 5,
    51     SnapshotOptionsPaintWithIntegralScaleFactor = 1 << 6,
     46enum class SnapshotFlags : uint8_t {
     47    ExcludeSelectionHighlighting = 1 << 0,
     48    PaintSelectionOnly = 1 << 1,
     49    InViewCoordinates = 1 << 2,
     50    ForceBlackText = 1 << 3,
     51    PaintSelectionAndBackgroundsOnly = 1 << 4,
     52    PaintEverythingExcludingSelection = 1 << 5,
     53    PaintWithIntegralScaleFactor = 1 << 6,
    5254};
    53 typedef unsigned SnapshotOptions;
    5455
    55 WEBCORE_EXPORT RefPtr<ImageBuffer> snapshotFrameRect(Frame&, const IntRect&, SnapshotOptions = SnapshotOptionsNone);
    56 RefPtr<ImageBuffer> snapshotFrameRectWithClip(Frame&, const IntRect&, const Vector<FloatRect>& clipRects, SnapshotOptions = SnapshotOptionsNone);
    57 RefPtr<ImageBuffer> snapshotNode(Frame&, Node&);
    58 WEBCORE_EXPORT RefPtr<ImageBuffer> snapshotSelection(Frame&, SnapshotOptions = SnapshotOptionsNone);
     56struct SnapshotOptions {
     57    OptionSet<SnapshotFlags> flags { };
     58    std::optional<PixelFormat> pixelFormat { };
     59    std::optional<DestinationColorSpace> colorSpace { };
     60};
     61
     62WEBCORE_EXPORT RefPtr<ImageBuffer> snapshotFrameRect(Frame&, const IntRect&, SnapshotOptions&& = { });
     63RefPtr<ImageBuffer> snapshotFrameRectWithClip(Frame&, const IntRect&, const Vector<FloatRect>& clipRects, SnapshotOptions&& = { });
     64RefPtr<ImageBuffer> snapshotNode(Frame&, Node&, SnapshotOptions&& = { });
     65WEBCORE_EXPORT RefPtr<ImageBuffer> snapshotSelection(Frame&, SnapshotOptions&& = { });
    5966
    6067} // namespace WebCore
  • trunk/Source/WebCore/page/PageColorSampler.cpp

    r278340 r278565  
    117117        return std::nullopt;
    118118
     119    // FIXME: <https://webkit.org/b/225942> (Sampled Page Top Color: support sampling non-RGB values like P3)
     120    auto colorSpace = DestinationColorSpace::SRGB();
     121
    119122    ASSERT(document.view());
    120     auto snapshot = snapshotFrameRect(document.view()->frame(), IntRect(location, IntSize(1, 1)), SnapshotOptionsExcludeSelectionHighlighting | SnapshotOptionsPaintEverythingExcludingSelection);
     123    auto snapshot = snapshotFrameRect(document.view()->frame(), IntRect(location, IntSize(1, 1)), { { SnapshotFlags::ExcludeSelectionHighlighting, SnapshotFlags::PaintEverythingExcludingSelection }, PixelFormat::BGRA8, colorSpace });
    121124    if (!snapshot)
    122125        return std::nullopt;
    123126
    124     auto pixelBuffer = snapshot->getPixelBuffer({ AlphaPremultiplication::Unpremultiplied, PixelFormat::BGRA8, DestinationColorSpace::SRGB() }, { { }, snapshot->logicalSize() });
     127    auto pixelBuffer = snapshot->getPixelBuffer({ AlphaPremultiplication::Unpremultiplied, PixelFormat::BGRA8, colorSpace }, { { }, snapshot->logicalSize() });
    125128    if (!pixelBuffer)
    126129        return std::nullopt;
  • trunk/Source/WebCore/page/PageConsoleClient.cpp

    r278253 r278565  
    405405            // If no target is provided, capture an image of the viewport.
    406406            IntRect imageRect(IntPoint::zero(), m_page.mainFrame().view()->sizeForVisibleContent());
    407             if (auto snapshot = WebCore::snapshotFrameRect(m_page.mainFrame(), imageRect, SnapshotOptionsInViewCoordinates))
     407            if (auto snapshot = WebCore::snapshotFrameRect(m_page.mainFrame(), imageRect, { { SnapshotFlags::InViewCoordinates } }))
    408408                dataURL = snapshot->toDataURL("image/png"_s, std::nullopt, PreserveResolution::Yes);
    409409        }
  • trunk/Source/WebCore/page/TextIndicator.cpp

    r276331 r278565  
    128128static SnapshotOptions snapshotOptionsForTextIndicatorOptions(OptionSet<TextIndicatorOption> options)
    129129{
    130     SnapshotOptions snapshotOptions = SnapshotOptionsPaintWithIntegralScaleFactor;
     130    SnapshotOptions snapshotOptions;
     131    snapshotOptions.flags.add(SnapshotFlags::PaintWithIntegralScaleFactor);
    131132
    132133    if (!options.contains(TextIndicatorOption::PaintAllContent)) {
    133134        if (options.contains(TextIndicatorOption::PaintBackgrounds))
    134             snapshotOptions |= SnapshotOptionsPaintSelectionAndBackgroundsOnly;
     135            snapshotOptions.flags.add(SnapshotFlags::PaintSelectionAndBackgroundsOnly);
    135136        else {
    136             snapshotOptions |= SnapshotOptionsPaintSelectionOnly;
     137            snapshotOptions.flags.add(SnapshotFlags::PaintSelectionOnly);
    137138
    138139            if (!options.contains(TextIndicatorOption::RespectTextColor))
    139                 snapshotOptions |= SnapshotOptionsForceBlackText;
     140                snapshotOptions.flags.add(SnapshotFlags::ForceBlackText);
    140141        }
    141142    } else
    142         snapshotOptions |= SnapshotOptionsExcludeSelectionHighlighting;
     143        snapshotOptions.flags.add(SnapshotFlags::ExcludeSelectionHighlighting);
    143144
    144145    return snapshotOptions;
    145146}
    146147
    147 static RefPtr<Image> takeSnapshot(Frame& frame, IntRect rect, SnapshotOptions options, float& scaleFactor, const Vector<FloatRect>& clipRectsInDocumentCoordinates)
    148 {
    149     auto buffer = snapshotFrameRectWithClip(frame, rect, clipRectsInDocumentCoordinates, options);
     148static RefPtr<Image> takeSnapshot(Frame& frame, IntRect rect, SnapshotOptions&& options, float& scaleFactor, const Vector<FloatRect>& clipRectsInDocumentCoordinates)
     149{
     150    auto buffer = snapshotFrameRectWithClip(frame, rect, clipRectsInDocumentCoordinates, WTFMove(options));
    150151    if (!buffer)
    151152        return nullptr;
     
    156157static bool takeSnapshots(TextIndicatorData& data, Frame& frame, IntRect snapshotRect, const Vector<FloatRect>& clipRectsInDocumentCoordinates)
    157158{
    158     SnapshotOptions snapshotOptions = snapshotOptionsForTextIndicatorOptions(data.options);
    159 
    160     data.contentImage = takeSnapshot(frame, snapshotRect, snapshotOptions, data.contentImageScaleFactor, clipRectsInDocumentCoordinates);
     159    data.contentImage = takeSnapshot(frame, snapshotRect, snapshotOptionsForTextIndicatorOptions(data.options), data.contentImageScaleFactor, clipRectsInDocumentCoordinates);
    161160    if (!data.contentImage)
    162161        return false;
     
    164163    if (data.options.contains(TextIndicatorOption::IncludeSnapshotWithSelectionHighlight)) {
    165164        float snapshotScaleFactor;
    166         data.contentImageWithHighlight = takeSnapshot(frame, snapshotRect, SnapshotOptionsNone, snapshotScaleFactor, clipRectsInDocumentCoordinates);
     165        data.contentImageWithHighlight = takeSnapshot(frame, snapshotRect, { }, snapshotScaleFactor, clipRectsInDocumentCoordinates);
    167166        ASSERT(!data.contentImageWithHighlight || data.contentImageScaleFactor >= snapshotScaleFactor);
    168167    }
     
    171170        float snapshotScaleFactor;
    172171        auto snapshotRect = frame.view()->visibleContentRect();
    173         data.contentImageWithoutSelection = takeSnapshot(frame, snapshotRect, SnapshotOptionsPaintEverythingExcludingSelection, snapshotScaleFactor, { });
     172        data.contentImageWithoutSelection = takeSnapshot(frame, snapshotRect, { { SnapshotFlags::PaintEverythingExcludingSelection } }, snapshotScaleFactor, { });
    174173        data.contentImageWithoutSelectionRectInRootViewCoordinates = frame.view()->contentsToRootView(snapshotRect);
    175174    }
  • trunk/Source/WebCore/platform/DragImage.cpp

    r278253 r278565  
    127127DragImageRef createDragImageForSelection(Frame& frame, TextIndicatorData&, bool forceBlackText)
    128128{
    129     SnapshotOptions options = forceBlackText ? SnapshotOptionsForceBlackText : SnapshotOptionsNone;
    130     return createDragImageFromSnapshot(snapshotSelection(frame, options), nullptr);
     129    SnapshotOptions options;
     130    if (forceBlackText)
     131        options.flags.add(SnapshotFlags::ForceBlackText);
     132    return createDragImageFromSnapshot(snapshotSelection(frame, WTFMove(options)), nullptr);
    131133}
    132134
     
    183185        return nullptr;
    184186
    185     SnapshotOptions options = SnapshotOptionsPaintSelectionOnly | (forceBlackText ? SnapshotOptionsForceBlackText : SnapshotOptionsNone);
     187    SnapshotOptions options;
     188    options.flags.add(SnapshotFlags::PaintSelectionOnly);
     189    if (forceBlackText)
     190        options.flags.add(SnapshotFlags::ForceBlackText);
     191
    186192    int startOffset = start.deprecatedEditingOffset();
    187193    int endOffset = end.deprecatedEditingOffset();
     
    190196    // We capture using snapshotFrameRect() because we fake up the selection using
    191197    // FrameView but snapshotSelection() uses the selection from the Frame itself.
    192     return createDragImageFromSnapshot(snapshotFrameRect(frame, view->selection().boundsClippedToVisibleContent(), options), nullptr);
     198    return createDragImageFromSnapshot(snapshotFrameRect(frame, view->selection().boundsClippedToVisibleContent(), WTFMove(options)), nullptr);
    193199}
    194200
  • trunk/Source/WebKit/ChangeLog

    r278560 r278565  
     12021-06-07  Devin Rousso  <drousso@apple.com>
     2
     3        Convert WebCore::SnapshotOptions into an enum class
     4        https://bugs.webkit.org/show_bug.cgi?id=226730
     5
     6        Reviewed by Wenson Hsieh.
     7
     8        Convert `SnapshotOptions` into an `enum class SnapshotFlags` and create a container `struct
     9        SnapshotOptions` that also allows for changing the `DestinationColorSpace` (defaults to sRGB)
     10        and `PixelFormat` (defaults to BGRA8).
     11
     12        No behavior change.
     13
     14        * WebProcess/WebPage/WebFrame.cpp:
     15        (WebKit::WebFrame::createSelectionSnapshot const):
     16
    1172021-06-07  Wenson Hsieh  <wenson_hsieh@apple.com>
    218
  • trunk/Source/WebKit/WebProcess/WebPage/WebFrame.cpp

    r278253 r278565  
    843843RefPtr<ShareableBitmap> WebFrame::createSelectionSnapshot() const
    844844{
    845     auto snapshot = snapshotSelection(*coreFrame(), WebCore::SnapshotOptionsForceBlackText);
     845    auto snapshot = snapshotSelection(*coreFrame(), { { WebCore::SnapshotFlags::ForceBlackText } });
    846846    if (!snapshot)
    847847        return nullptr;
Note: See TracChangeset for help on using the changeset viewer.