File: AcceleratedBackingStoreDMABuf.h

package info (click to toggle)
webkit2gtk 2.48.5-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 429,792 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: 295
file content (258 lines) | stat: -rw-r--r-- 10,084 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2023 Igalia S.L.
 *
 * 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 COMPUTER, INC. ``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 COMPUTER, INC. OR
 * 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

#include "AcceleratedBackingStore.h"
#include "DMABufRendererBufferFormat.h"
#include "FenceMonitor.h"
#include "MessageReceiver.h"
#include "RendererBufferFormat.h"
#include <WebCore/IntSize.h>
#include <WebCore/RefPtrCairo.h>
#include <WebCore/Region.h>
#include <gtk/gtk.h>
#include <wtf/OptionSet.h>
#include <wtf/RefCounted.h>
#include <wtf/TZoneMalloc.h>
#include <wtf/WeakPtr.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/unix/UnixFileDescriptor.h>

typedef void *EGLImage;

#if USE(GBM)
struct gbm_bo;
#endif

namespace WebCore {
class IntRect;
class Region;
class ShareableBitmap;
class ShareableBitmapHandle;
}

namespace WTF {
class UnixFileDescriptor;
}

namespace WebKit {

class WebPageProxy;
class WebProcessProxy;
enum class RendererBufferTransportMode : uint8_t;

class AcceleratedBackingStoreDMABuf final : public AcceleratedBackingStore, public IPC::MessageReceiver, public RefCounted<AcceleratedBackingStoreDMABuf> {
    WTF_MAKE_TZONE_ALLOCATED(AcceleratedBackingStoreDMABuf);
    WTF_MAKE_NONCOPYABLE(AcceleratedBackingStoreDMABuf);
public:
    static OptionSet<RendererBufferTransportMode> rendererBufferTransportMode();
    static bool checkRequirements();
#if USE(GBM)
    static Vector<DMABufRendererBufferFormat> preferredBufferFormats();
#endif
    static Ref<AcceleratedBackingStoreDMABuf> create(WebPageProxy&);
    ~AcceleratedBackingStoreDMABuf();

    void ref() const final { RefCounted::ref(); }
    void deref() const final { RefCounted::deref(); }

private:
    AcceleratedBackingStoreDMABuf(WebPageProxy&);

    // IPC::MessageReceiver.
    void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;

    void didCreateBuffer(uint64_t id, const WebCore::IntSize&, uint32_t format, Vector<WTF::UnixFileDescriptor>&&, Vector<uint32_t>&& offsets, Vector<uint32_t>&& strides, uint64_t modifier, DMABufRendererBufferFormat::Usage);
    void didCreateBufferSHM(uint64_t id, WebCore::ShareableBitmapHandle&&);
    void didDestroyBuffer(uint64_t id);
    void frame(uint64_t id, WebCore::Region&&, WTF::UnixFileDescriptor&&);
    void frameDone();
    void ensureGLContext();
    bool swapBuffersIfNeeded();

#if USE(GTK4)
    bool snapshot(GtkSnapshot*) override;
#else
    bool paint(cairo_t*, const WebCore::IntRect&) override;
#endif
    void unrealize() override;
    void update(const LayerTreeContext&) override;
    RendererBufferFormat bufferFormat() const override;
    RefPtr<WebCore::NativeImage> bufferAsNativeImageForTesting() const override;

    class Buffer : public RefCounted<Buffer> {
    public:
        virtual ~Buffer() = default;

        enum class Type {
#if GTK_CHECK_VERSION(4, 13, 4)
            DmaBuf,
#endif
            EglImage,
#if USE(GBM)
            Gbm,
#endif
            SharedMemory
        };

        virtual Type type() const = 0;
        virtual void didUpdateContents(Buffer*, const WebCore::Region&) = 0;
#if USE(GTK4)
        virtual GdkTexture* texture() const { return nullptr; }
#else
        virtual unsigned textureID() const { return 0; }
#endif
        virtual cairo_surface_t* surface() const { return nullptr; }

        virtual RendererBufferFormat format() const = 0;
        virtual RefPtr<WebCore::NativeImage> asNativeImageForTesting() const = 0;
        virtual void release() = 0;

        uint64_t id() const { return m_id; }
        float deviceScaleFactor() const;
        void setSurfaceID(uint64_t surfaceID) { m_surfaceID = surfaceID; }
#if USE(GTK4)
        void snapshot(GtkSnapshot*) const;
#else
        void paint(cairo_t*, const WebCore::IntRect&) const;
#endif
        void didRelease() const;

    protected:
        Buffer(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage);

        WeakPtr<WebPageProxy> m_webPage;
        uint64_t m_id { 0 };
        uint64_t m_surfaceID { 0 };
        WebCore::IntSize m_size;
        DMABufRendererBufferFormat::Usage m_usage { DMABufRendererBufferFormat::Usage::Rendering };
    };

#if GTK_CHECK_VERSION(4, 13, 4)
    class BufferDMABuf final : public Buffer {
    public:
        static RefPtr<Buffer> create(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, uint32_t format, Vector<WTF::UnixFileDescriptor>&&, Vector<uint32_t>&& offsets, Vector<uint32_t>&& strides, uint64_t modifier);
        ~BufferDMABuf() = default;

    private:
        BufferDMABuf(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, Vector<WTF::UnixFileDescriptor>&&, GRefPtr<GdkDmabufTextureBuilder>&&);

        Buffer::Type type() const override { return Buffer::Type::DmaBuf; }
        void didUpdateContents(Buffer*, const WebCore::Region&) override;
        GdkTexture* texture() const override { return m_texture.get(); }
        RendererBufferFormat format() const override;
        RefPtr<WebCore::NativeImage> asNativeImageForTesting() const override;
        void release() override;

        Vector<WTF::UnixFileDescriptor> m_fds;
        GRefPtr<GdkDmabufTextureBuilder> m_builder;
        GRefPtr<GdkTexture> m_texture;
    };
#endif

    class BufferEGLImage final : public Buffer {
    public:
        static RefPtr<Buffer> create(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, uint32_t format, Vector<WTF::UnixFileDescriptor>&&, Vector<uint32_t>&& offsets, Vector<uint32_t>&& strides, uint64_t modifier);
        ~BufferEGLImage();

    private:
        BufferEGLImage(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, uint32_t format, Vector<WTF::UnixFileDescriptor>&&, uint64_t modifier, EGLImage);

        Buffer::Type type() const override { return Buffer::Type::EglImage; }
        void didUpdateContents(Buffer*, const WebCore::Region&) override;
#if USE(GTK4)
        GdkTexture* texture() const override { return m_texture.get(); }
#else
        unsigned textureID() const override { return m_textureID; }
#endif
        RendererBufferFormat format() const override;
        RefPtr<WebCore::NativeImage> asNativeImageForTesting() const override;
        void release() override;

        Vector<WTF::UnixFileDescriptor> m_fds;
        EGLImage m_image { nullptr };
#if USE(GTK4)
        GRefPtr<GdkTexture> m_texture;
#else
        unsigned m_textureID { 0 };
#endif
        uint32_t m_fourcc { 0 };
        uint64_t m_modifier { 0 };
    };

#if USE(GBM)
    class BufferGBM final : public Buffer {
    public:
        static RefPtr<Buffer> create(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, uint32_t format, WTF::UnixFileDescriptor&&, uint32_t stride);
        ~BufferGBM();

    private:
        BufferGBM(WebPageProxy&, uint64_t id, uint64_t surfaceID, const WebCore::IntSize&, DMABufRendererBufferFormat::Usage, WTF::UnixFileDescriptor&&, struct gbm_bo*);

        Buffer::Type type() const override { return Buffer::Type::Gbm; }
        void didUpdateContents(Buffer*, const WebCore::Region&) override;
        cairo_surface_t* surface() const override { return m_surface.get(); }
        RendererBufferFormat format() const override;
        RefPtr<WebCore::NativeImage> asNativeImageForTesting() const override;
        void release() override;

        WTF::UnixFileDescriptor m_fd;
        struct gbm_bo* m_buffer { nullptr };
        RefPtr<cairo_surface_t> m_surface;
    };
#endif

    class BufferSHM final : public Buffer {
    public:
        static RefPtr<Buffer> create(WebPageProxy&, uint64_t id, uint64_t surfaceID, RefPtr<WebCore::ShareableBitmap>&&);
        ~BufferSHM() = default;

    private:
        BufferSHM(WebPageProxy&, uint64_t id, uint64_t surfaceID, RefPtr<WebCore::ShareableBitmap>&&);

        Buffer::Type type() const override { return Buffer::Type::SharedMemory; }
        void didUpdateContents(Buffer*, const WebCore::Region&) override;
        cairo_surface_t* surface() const override { return m_surface.get(); }
        RendererBufferFormat format() const override;
        RefPtr<WebCore::NativeImage> asNativeImageForTesting() const override;
        void release() override;

        RefPtr<WebCore::ShareableBitmap> m_bitmap;
        RefPtr<cairo_surface_t> m_surface;
    };

    FenceMonitor m_fenceMonitor;
    GRefPtr<GdkGLContext> m_gdkGLContext;
    bool m_glContextInitialized { false };
    uint64_t m_surfaceID { 0 };
    WeakPtr<WebProcessProxy> m_legacyMainFrameProcess;
    RefPtr<Buffer> m_pendingBuffer;
    RefPtr<Buffer> m_committedBuffer;
    WebCore::Region m_pendingDamageRegion;
    HashMap<uint64_t, RefPtr<Buffer>> m_buffers;
};

} // namespace WebKit