text: /*
   * Copyright (C) 2015, 2016 Igalia S.L.
   * All rights reserved.
   *
   * 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   * "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 THE COPYRIGHT
   * HOLDER 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.
   */
  
  #include <wpe-mesa/view-backend-exportable-dma-buf.h>
  
  #include "ipc.h"
  #include "ipc-gbm.h"
  
  namespace ExportableDmaBuf {
  
  class ViewBackend;
  
  class ClientBundle {
  public:
      struct wpe_mesa_view_backend_exportable_dma_buf_client* client;
      void* data;
      ViewBackend* viewBackend;
  };
  
  class ViewBackend : public IPC::Host::Handler {
  public:
      ViewBackend(ClientBundle*, struct wpe_view_backend* backend);
      virtual ~ViewBackend();
  
      void initialize();
  
      IPC::Host& ipcHost() { return m_renderer.ipcHost; }
  
  private:
      // IPC::Host::Handler
      void handleFd(int) override;
      void handleMessage(char*, size_t) override;
  
      ClientBundle* m_clientBundle;
      struct wpe_view_backend* m_backend;
  
      struct {
          IPC::Host ipcHost;
          int pendingBufferFd { -1 };
      } m_renderer;
  };
  
  ViewBackend::ViewBackend(ClientBundle* clientBundle, struct wpe_view_backend* backend)
      : m_clientBundle(clientBundle)
      , m_backend(backend)
  {
      m_clientBundle->viewBackend = this;
      m_renderer.ipcHost.initialize(*this);
  }
  
  ViewBackend::~ViewBackend()
  {
      m_backend = nullptr;
  
      m_renderer.ipcHost.deinitialize();
  
      if (m_renderer.pendingBufferFd != -1)
          close(m_renderer.pendingBufferFd);
      m_renderer.pendingBufferFd = -1;
  }
  
  void ViewBackend::initialize()
  {
      wpe_view_backend_dispatch_set_size(m_backend, 800, 600);
  }
  
  void ViewBackend::handleFd(int fd)
  {
      if (m_renderer.pendingBufferFd != -1)
          close(m_renderer.pendingBufferFd);
      m_renderer.pendingBufferFd = fd;
  }
  
  void ViewBackend::handleMessage(char* data, size_t size)
  {
  
      if (size != IPC::Message::size)
          return;
  
      auto& message = IPC::Message::cast(data);
      if (message.messageCode != IPC::GBM::BufferCommit::code)
          return;
  
      auto& bufferCommit = IPC::GBM::BufferCommit::cast(message);
  
      struct wpe_mesa_view_backend_exportable_dma_buf_data imageData{
          m_renderer.pendingBufferFd, bufferCommit.handle,
          bufferCommit.width, bufferCommit.height,
          bufferCommit.stride, bufferCommit.format
      };
      m_clientBundle->client->export_dma_buf(m_clientBundle->data, &imageData);
  
      m_renderer.pendingBufferFd = -1;
  }
  
  } // namespace ExportableDmaBuf
  
  extern "C" {
  
  struct wpe_mesa_view_backend_exportable_dma_buf {
      ExportableDmaBuf::ClientBundle* clientBundle;
      struct wpe_view_backend* backend;
  };
  
  struct wpe_view_backend_interface exportable_dma_buf_view_backend_interface = {
      // create
      [](void* data, struct wpe_view_backend* backend) -> void*
      {
          auto* clientBundle = static_cast<ExportableDmaBuf::ClientBundle*>(data);
          return new ExportableDmaBuf::ViewBackend(clientBundle, backend);
      },
      // destroy
      [](void* data)
      {
          auto* backend = static_cast<ExportableDmaBuf::ViewBackend*>(data);
          delete backend;
      },
      // initialize
      [](void* data)
      {
          auto& backend = *static_cast<ExportableDmaBuf::ViewBackend*>(data);
          backend.initialize();
      },
      // get_renderer_host_fd
      [](void* data) -> int
      {
          auto& backend = *static_cast<ExportableDmaBuf::ViewBackend*>(data);
          return backend.ipcHost().releaseClientFD();
      },
  };
  
  __attribute__((visibility("default")))
  struct wpe_mesa_view_backend_exportable_dma_buf*
  wpe_mesa_view_backend_exportable_dma_buf_create(struct wpe_mesa_view_backend_exportable_dma_buf_client* client, void* client_data)
  {
      auto* clientBundle = new ExportableDmaBuf::ClientBundle{ client, client_data, nullptr };
      struct wpe_view_backend* backend = wpe_view_backend_create_with_backend_interface(&exportable_dma_buf_view_backend_interface, clientBundle);
  
      auto* exportable = new struct wpe_mesa_view_backend_exportable_dma_buf;
      exportable->clientBundle = clientBundle;
      exportable->backend = backend;
  
      return exportable;
  }
  
  __attribute__((visibility("default")))
  void
  wpe_mesa_view_backend_exportable_dma_buf_destroy(struct wpe_mesa_view_backend_exportable_dma_buf* exportable)
  {
      wpe_view_backend_destroy(exportable->backend);
      delete exportable->clientBundle;
      delete exportable;
  }
  
  __attribute__((visibility("default")))
  struct wpe_view_backend*
  wpe_mesa_view_backend_exportable_dma_buf_get_view_backend(struct wpe_mesa_view_backend_exportable_dma_buf* exportable)
  {
      return exportable->backend;
  }
  
  __attribute__((visibility("default")))
  void
  wpe_mesa_view_backend_exportable_dma_buf_dispatch_frame_complete(struct wpe_mesa_view_backend_exportable_dma_buf* exportable)
  {
      IPC::Message message;
      IPC::GBM::FrameComplete::construct(message);
      exportable->clientBundle->viewBackend->ipcHost().sendMessage(IPC::Message::data(message), IPC::Message::size);
  }
  
  __attribute__((visibility("default")))
  void
  wpe_mesa_view_backend_exportable_dma_buf_dispatch_release_buffer(struct wpe_mesa_view_backend_exportable_dma_buf* exportable, uint32_t handle)
  {
      IPC::Message message;
      IPC::GBM::ReleaseBuffer::construct(message, handle);
      exportable->clientBundle->viewBackend->ipcHost().sendMessage(IPC::Message::data(message), IPC::Message::size);
  }
  
  }
  