1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AcceleratedSurfaceLibWPE.h"
28
29#if USE(WPE_RENDERER)
30
31#include "WebPage.h"
32#include <WebCore/PlatformDisplayLibWPE.h>
33#include <wpe/wpe-egl.h>
34#include <wtf/UniStdExtras.h>
35
36namespace WebKit {
37using namespace WebCore;
38
39std::unique_ptr<AcceleratedSurfaceLibWPE> AcceleratedSurfaceLibWPE::create(WebPage& webPage, Client& client)
40{
41 return std::unique_ptr<AcceleratedSurfaceLibWPE>(new AcceleratedSurfaceLibWPE(webPage, client));
42}
43
44AcceleratedSurfaceLibWPE::AcceleratedSurfaceLibWPE(WebPage& webPage, Client& client)
45 : AcceleratedSurface(webPage, client)
46{
47}
48
49AcceleratedSurfaceLibWPE::~AcceleratedSurfaceLibWPE()
50{
51 ASSERT(!m_backend);
52}
53
54void AcceleratedSurfaceLibWPE::initialize()
55{
56 m_backend = wpe_renderer_backend_egl_target_create(dupCloseOnExec(m_webPage.hostFileDescriptor()));
57 static struct wpe_renderer_backend_egl_target_client s_client = {
58 // frame_complete
59 [](void* data)
60 {
61 auto& surface = *reinterpret_cast<AcceleratedSurfaceLibWPE*>(data);
62 surface.m_client.frameComplete();
63 },
64 // padding
65 nullptr,
66 nullptr,
67 nullptr,
68 nullptr
69 };
70 wpe_renderer_backend_egl_target_set_client(m_backend, &s_client, this);
71 wpe_renderer_backend_egl_target_initialize(m_backend, downcast<PlatformDisplayLibWPE>(PlatformDisplay::sharedDisplayForCompositing()).backend(),
72 std::max(1, m_size.width()), std::max(1, m_size.height()));
73}
74
75void AcceleratedSurfaceLibWPE::finalize()
76{
77 wpe_renderer_backend_egl_target_destroy(m_backend);
78 m_backend = nullptr;
79}
80
81uint64_t AcceleratedSurfaceLibWPE::window() const
82{
83 ASSERT(m_backend);
84 // EGLNativeWindowType changes depending on the EGL implementation: reinterpret_cast works
85 // for pointers (only if they are 64-bit wide and not for other cases), and static_cast for
86 // numeric types (and when needed they get extended to 64-bit) but not for pointers. Using
87 // a plain C cast expression in this one instance works in all cases.
88 static_assert(sizeof(EGLNativeWindowType) <= sizeof(uint64_t), "EGLNativeWindowType must not be longer than 64 bits.");
89 return (uint64_t) wpe_renderer_backend_egl_target_get_native_window(m_backend);
90}
91
92uint64_t AcceleratedSurfaceLibWPE::surfaceID() const
93{
94 return m_webPage.pageID().toUInt64();
95}
96
97void AcceleratedSurfaceLibWPE::clientResize(const IntSize& size)
98{
99 ASSERT(m_backend);
100 wpe_renderer_backend_egl_target_resize(m_backend, std::max(1, m_size.width()), std::max(1, m_size.height()));
101}
102
103void AcceleratedSurfaceLibWPE::willRenderFrame()
104{
105 ASSERT(m_backend);
106 wpe_renderer_backend_egl_target_frame_will_render(m_backend);
107}
108
109void AcceleratedSurfaceLibWPE::didRenderFrame()
110{
111 ASSERT(m_backend);
112 wpe_renderer_backend_egl_target_frame_rendered(m_backend);
113}
114
115} // namespace WebKit
116
117#endif // USE(WPE_RENDERER)
118