summaryrefslogtreecommitdiffstats
path: root/src/core/compositor/native_skia_output_device.h
blob: 2c35cef77b4be287fbc8483cbb65db9d138de10d (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef NATIVE_SKIA_OUTPUT_DEVICE_H
#define NATIVE_SKIA_OUTPUT_DEVICE_H

#include "compositor.h"

#include "base/task/single_thread_task_runner.h"
#include "components/viz/service/display_embedder/skia_output_device.h"
#include "gpu/command_buffer/service/shared_context_state.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/config/gpu_preferences.h"

#include <QMutex>

#if defined(Q_OS_WIN)
#include "ui/gl/dc_layer_overlay_image.h"
#endif

#if defined(Q_OS_MACOS)
#include "ui/gfx/mac/io_surface.h"
#endif

QT_BEGIN_NAMESPACE
class QQuickWindow;
QT_END_NAMESPACE

namespace gl {
class GLFence;
}

namespace gfx {
class GpuFence;
class NativePixmap;
}

namespace gpu {
class SharedImageFactory;
class SharedImageRepresentationFactory;
}

namespace viz {
class SkiaOutputSurfaceDependency;
}

namespace QtWebEngineCore {

class NativeSkiaOutputDevice : public viz::SkiaOutputDevice, public Compositor
{
public:
    NativeSkiaOutputDevice(scoped_refptr<gpu::SharedContextState> contextState,
                            bool requiresAlpha,
                            gpu::MemoryTracker *memoryTracker,
                            viz::SkiaOutputSurfaceDependency *dependency,
                            gpu::SharedImageFactory *shared_image_factory,
                            gpu::SharedImageRepresentationFactory *shared_image_representation_factory,
                            DidSwapBufferCompleteCallback didSwapBufferCompleteCallback);
    ~NativeSkiaOutputDevice() override;

    // Overridden from SkiaOutputDevice.
    void SetFrameSinkId(const viz::FrameSinkId &frame_sink_id) override;
    bool Reshape(const SkImageInfo &image_info,
                 const gfx::ColorSpace &color_space,
                 int sample_count,
                 float device_scale_factor,
                 gfx::OverlayTransform transform) override;
    void Present(const absl::optional<gfx::Rect>& update_rect,
                 BufferPresentedCallback feedback,
                 viz::OutputSurfaceFrame frame) override;
    void EnsureBackbuffer() override;
    void DiscardBackbuffer() override;
    SkSurface *BeginPaint(std::vector<GrBackendSemaphore> *semaphores) override;
    void EndPaint() override;

    // Overridden from Compositor.
    void swapFrame() override;
    void waitForTexture() override;
    void releaseTexture() override;
    void releaseResources() override;
    bool textureIsFlipped() override;
    QSize size() override;
    bool requiresAlphaChannel() override;
    float devicePixelRatio() override;

protected:
    struct Shape
    {
        SkImageInfo imageInfo;
        float devicePixelRatio;
        gfx::ColorSpace colorSpace;
        int sampleCount;

        bool operator==(const Shape &that) const
        {
            return (imageInfo == that.imageInfo &&
                    devicePixelRatio == that.devicePixelRatio &&
                    colorSpace == that.colorSpace &&
                    sampleCount == that.sampleCount);
        }
        bool operator!=(const Shape &that) const { return !(*this == that); }
    };

    class Buffer
    {
    public:
        Buffer(NativeSkiaOutputDevice *parent);
        ~Buffer();

        bool initialize();
        SkSurface *beginWriteSkia();
        void endWriteSkia(bool force_flush);
        std::vector<GrBackendSemaphore> takeEndWriteSkiaSemaphores();
        void beginPresent();
        void endPresent();
        void freeTexture();
        void createFence();
        void consumeFence();

        sk_sp<SkImage> skImage();
#if defined(USE_OZONE)
        scoped_refptr<gfx::NativePixmap> nativePixmap();
#elif defined(Q_OS_WIN)
        absl::optional<gl::DCLayerOverlayImage> overlayImage() const;
#elif defined(Q_OS_MACOS)
        gfx::ScopedIOSurface ioSurface() const;
#endif

        const Shape &shape() const { return m_shape; }
        viz::SharedImageFormat sharedImageFormat() const { return m_skiaRepresentation->format(); }

        std::function<void()> textureCleanupCallback;

    private:
        void createSkImageOnGPUThread();

        NativeSkiaOutputDevice *m_parent;
        Shape m_shape;
        uint64_t m_estimatedSize = 0; // FIXME: estimate size
        std::unique_ptr<gfx::GpuFence> m_acquireFence;
        std::unique_ptr<gl::GLFence> m_fence;
        gpu::Mailbox m_mailbox;
        std::unique_ptr<gpu::SkiaImageRepresentation> m_skiaRepresentation;
        std::unique_ptr<gpu::SkiaImageRepresentation::ScopedWriteAccess> m_scopedSkiaWriteAccess;
        std::unique_ptr<gpu::SkiaImageRepresentation::ScopedReadAccess> m_scopedSkiaReadAccess;
        std::unique_ptr<gpu::OverlayImageRepresentation> m_overlayRepresentation;
        std::unique_ptr<gpu::OverlayImageRepresentation::ScopedReadAccess>
                m_scopedOverlayReadAccess;
        std::vector<GrBackendSemaphore> m_endSemaphores;
        int m_presentCount = 0;

        mutable QMutex m_skImageMutex;
        sk_sp<SkImage> m_cachedSkImage;
    };

protected:
    scoped_refptr<gpu::SharedContextState> m_contextState;
    std::unique_ptr<Buffer> m_frontBuffer;
    bool m_readyWithTexture = false;
    bool m_isNativeBufferSupported = true;

private:
    friend class NativeSkiaOutputDevice::Buffer;

    void SwapBuffersFinished();

    mutable QMutex m_mutex;
    Shape m_shape;
    std::unique_ptr<Buffer> m_middleBuffer;
    std::unique_ptr<Buffer> m_backBuffer;
    viz::OutputSurfaceFrame m_frame;
    bool m_readyToUpdate = false;
    bool m_requiresAlpha;
    scoped_refptr<base::SingleThreadTaskRunner> m_gpuTaskRunner;

    const raw_ptr<gpu::SharedImageFactory> m_factory;
    const raw_ptr<gpu::SharedImageRepresentationFactory> m_representationFactory;
    const raw_ptr<viz::SkiaOutputSurfaceDependency> m_deps;
};

} // namespace QtWebEngineCore

#endif // !NATIVE_SKIA_OUTPUT_DEVICE_H