| 1 | /* |
| 2 | * Copyright (C) 2016 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 COMPUTER, 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 COMPUTER, 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 | #pragma once |
| 27 | |
| 28 | #if PLATFORM(WAYLAND) && USE(EGL) |
| 29 | |
| 30 | #include "WebPageProxy.h" |
| 31 | #include <WebCore/RefPtrCairo.h> |
| 32 | #include <WebCore/WlUniquePtr.h> |
| 33 | #include <wayland-server.h> |
| 34 | #include <wtf/HashMap.h> |
| 35 | #include <wtf/NeverDestroyed.h> |
| 36 | #include <wtf/Noncopyable.h> |
| 37 | #include <wtf/WeakPtr.h> |
| 38 | #include <wtf/glib/GRefPtr.h> |
| 39 | #include <wtf/text/WTFString.h> |
| 40 | |
| 41 | typedef void *EGLImageKHR; |
| 42 | |
| 43 | namespace WebKit { |
| 44 | |
| 45 | class WebPageProxy; |
| 46 | |
| 47 | class WaylandCompositor { |
| 48 | WTF_MAKE_NONCOPYABLE(WaylandCompositor); |
| 49 | friend NeverDestroyed<WaylandCompositor>; |
| 50 | public: |
| 51 | static WaylandCompositor& singleton(); |
| 52 | |
| 53 | class Buffer : public CanMakeWeakPtr<Buffer> { |
| 54 | WTF_MAKE_NONCOPYABLE(Buffer); WTF_MAKE_FAST_ALLOCATED; |
| 55 | public: |
| 56 | static Buffer* getOrCreate(struct wl_resource*); |
| 57 | ~Buffer(); |
| 58 | |
| 59 | void use(); |
| 60 | void unuse(); |
| 61 | |
| 62 | EGLImageKHR createImage() const; |
| 63 | WebCore::IntSize size() const; |
| 64 | |
| 65 | private: |
| 66 | Buffer(struct wl_resource*); |
| 67 | static void destroyListenerCallback(struct wl_listener*, void*); |
| 68 | |
| 69 | struct wl_resource* m_resource { nullptr }; |
| 70 | struct wl_listener m_destroyListener; |
| 71 | uint32_t m_busyCount { 0 }; |
| 72 | }; |
| 73 | |
| 74 | class Surface : public CanMakeWeakPtr<Surface> { |
| 75 | WTF_MAKE_NONCOPYABLE(Surface); WTF_MAKE_FAST_ALLOCATED; |
| 76 | public: |
| 77 | Surface(); |
| 78 | ~Surface(); |
| 79 | |
| 80 | void attachBuffer(struct wl_resource*); |
| 81 | void requestFrame(struct wl_resource*); |
| 82 | void commit(); |
| 83 | |
| 84 | void setWebPage(WebPageProxy*); |
| 85 | bool prepareTextureForPainting(unsigned&, WebCore::IntSize&); |
| 86 | |
| 87 | private: |
| 88 | void flushFrameCallbacks(); |
| 89 | void flushPendingFrameCallbacks(); |
| 90 | void makePendingBufferCurrent(); |
| 91 | |
| 92 | WeakPtr<Buffer> m_buffer; |
| 93 | WeakPtr<Buffer> m_pendingBuffer; |
| 94 | unsigned m_texture { 0 }; |
| 95 | EGLImageKHR m_image; |
| 96 | WebCore::IntSize m_imageSize; |
| 97 | Vector<wl_resource*> m_pendingFrameCallbackList; |
| 98 | Vector<wl_resource*> m_frameCallbackList; |
| 99 | WebPageProxy* m_webPage { nullptr }; |
| 100 | unsigned m_tickCallbackID { 0 }; |
| 101 | }; |
| 102 | |
| 103 | bool isRunning() const { return !!m_display; } |
| 104 | String displayName() const { return m_displayName; } |
| 105 | |
| 106 | void bindSurfaceToWebPage(Surface*, uint64_t pageID); |
| 107 | void registerWebPage(WebPageProxy&); |
| 108 | void unregisterWebPage(WebPageProxy&); |
| 109 | |
| 110 | bool getTexture(WebPageProxy&, unsigned&, WebCore::IntSize&); |
| 111 | |
| 112 | private: |
| 113 | WaylandCompositor(); |
| 114 | |
| 115 | bool initializeEGL(); |
| 116 | |
| 117 | String m_displayName; |
| 118 | |
| 119 | struct DisplayDeleter { |
| 120 | void operator() (struct wl_display* display) { wl_display_destroy(display); } |
| 121 | }; |
| 122 | std::unique_ptr<struct wl_display, DisplayDeleter> m_display; |
| 123 | |
| 124 | WebCore::WlUniquePtr<struct wl_global> m_compositorGlobal; |
| 125 | WebCore::WlUniquePtr<struct wl_global> m_webkitgtkGlobal; |
| 126 | GRefPtr<GSource> m_eventSource; |
| 127 | HashMap<WebPageProxy*, WeakPtr<Surface>> m_pageMap; |
| 128 | }; |
| 129 | |
| 130 | } // namespace WebKit |
| 131 | |
| 132 | #endif // PLATFORM(WAYLAND) && USE(EGL) |
| 133 | |