Commit c18e91b3 authored by Kartikaya Gupta's avatar Kartikaya Gupta
Browse files

Bug 1385003 - Update webrender bindings for API changes in WR cset f6d81d9. r=kvark

In theory the upstream API change should allow us to share the same WR renderer
instance across multiple WebRenderAPI instances. For now however I retain the
existing behaviour of one WR instance for each WebRenderAPI instance, but keep
track of the document id in a new DocumentHandle struct. The C++ side keeps
a pointer to this DocumentHandle struct instead of the raw RenderApi.

MozReview-Commit-ID: I9pCKOY1OYx

--HG--
extra : rebase_source : 7b0ae2ccb2692a76045371ac165468c7f7539b40
parent b43eb147
Loading
Loading
Loading
Loading
+29 −29
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ using layers::Stringify;
class NewRenderer : public RendererEvent
{
public:
  NewRenderer(wr::RenderApi** aApi, layers::CompositorBridgeParentBase* aBridge,
  NewRenderer(wr::DocumentHandle** aDocHandle, layers::CompositorBridgeParentBase* aBridge,
              GLint* aMaxTextureSize,
              bool* aUseANGLE,
              RefPtr<widget::CompositorWidget>&& aWidget,
@@ -31,7 +31,7 @@ public:
              bool aEnableProfiler,
              LayoutDeviceIntSize aSize,
              layers::SyncHandle* aHandle)
    : mRenderApi(aApi)
    : mDocHandle(aDocHandle)
    , mMaxTextureSize(aMaxTextureSize)
    , mUseANGLE(aUseANGLE)
    , mBridge(aBridge)
@@ -75,7 +75,7 @@ public:
    wr::Renderer* wrRenderer = nullptr;
    if (!wr_window_new(aWindowId, mSize.width, mSize.height, gl.get(),
                       aRenderThread.ThreadPool().Raw(),
                       this->mEnableProfiler, mRenderApi, &wrRenderer)) {
                       this->mEnableProfiler, mDocHandle, &wrRenderer)) {
      // wr_window_new puts a message into gfxCriticalNote if it returns false
      return;
    }
@@ -104,7 +104,7 @@ public:
  }

private:
  wr::RenderApi** mRenderApi;
  wr::DocumentHandle** mDocHandle;
  GLint* mMaxTextureSize;
  bool* mUseANGLE;
  layers::CompositorBridgeParentBase* mBridge;
@@ -153,32 +153,32 @@ WebRenderAPI::Create(bool aEnableProfiler,
  static uint64_t sNextId = 1;
  auto id = NewWindowId(sNextId++);

  wr::RenderApi* renderApi = nullptr;
  wr::DocumentHandle* docHandle = nullptr;
  GLint maxTextureSize = 0;
  bool useANGLE = false;
  layers::SyncHandle syncHandle = 0;

  // Dispatch a synchronous task because the RenderApi object needs to be created
  // Dispatch a synchronous task because the DocumentHandle object needs to be created
  // on the render thread. If need be we could delay waiting on this task until
  // the next time we need to access the RenderApi object.
  // the next time we need to access the DocumentHandle object.
  layers::SynchronousTask task("Create Renderer");
  auto event = MakeUnique<NewRenderer>(&renderApi, aBridge, &maxTextureSize, &useANGLE,
  auto event = MakeUnique<NewRenderer>(&docHandle, aBridge, &maxTextureSize, &useANGLE,
                                       Move(aWidget), &task, aEnableProfiler, aSize,
                                       &syncHandle);
  RenderThread::Get()->RunEvent(id, Move(event));

  task.Wait();

  if (!renderApi) {
  if (!docHandle) {
    return nullptr;
  }

  return RefPtr<WebRenderAPI>(new WebRenderAPI(renderApi, id, maxTextureSize, useANGLE, syncHandle)).forget();
  return RefPtr<WebRenderAPI>(new WebRenderAPI(docHandle, id, maxTextureSize, useANGLE, syncHandle)).forget();
}

wr::WrIdNamespace
WebRenderAPI::GetNamespace() {
  return wr_api_get_namespace(mRenderApi);
  return wr_api_get_namespace(mDocHandle);
}

WebRenderAPI::~WebRenderAPI()
@@ -188,7 +188,7 @@ WebRenderAPI::~WebRenderAPI()
  RunOnRenderThread(Move(event));
  task.Wait();

  wr_api_delete(mRenderApi);
  wr_api_delete(mDocHandle);
}

void
@@ -196,20 +196,20 @@ WebRenderAPI::UpdateScrollPosition(const wr::WrPipelineId& aPipelineId,
                                   const layers::FrameMetrics::ViewID& aScrollId,
                                   const wr::LayoutPoint& aScrollPosition)
{
  wr_scroll_layer_with_id(mRenderApi, aPipelineId, aScrollId, aScrollPosition);
  wr_scroll_layer_with_id(mDocHandle, aPipelineId, aScrollId, aScrollPosition);
}

void
WebRenderAPI::GenerateFrame()
{
  wr_api_generate_frame(mRenderApi);
  wr_api_generate_frame(mDocHandle);
}

void
WebRenderAPI::GenerateFrame(const nsTArray<wr::WrOpacityProperty>& aOpacityArray,
                            const nsTArray<wr::WrTransformProperty>& aTransformArray)
{
  wr_api_generate_frame_with_properties(mRenderApi,
  wr_api_generate_frame_with_properties(mDocHandle,
                                        aOpacityArray.IsEmpty() ?
                                          nullptr : aOpacityArray.Elements(),
                                        aOpacityArray.Length(),
@@ -228,7 +228,7 @@ WebRenderAPI::SetRootDisplayList(gfx::Color aBgColor,
                                 uint8_t *dl_data,
                                 size_t dl_size)
{
    wr_api_set_root_display_list(mRenderApi,
    wr_api_set_root_display_list(mDocHandle,
                                 ToColorF(aBgColor),
                                 aEpoch,
                                 aViewportSize.width, aViewportSize.height,
@@ -243,13 +243,13 @@ void
WebRenderAPI::ClearRootDisplayList(Epoch aEpoch,
                                   wr::WrPipelineId pipeline_id)
{
  wr_api_clear_root_display_list(mRenderApi, aEpoch, pipeline_id);
  wr_api_clear_root_display_list(mDocHandle, aEpoch, pipeline_id);
}

void
WebRenderAPI::SetWindowParameters(LayoutDeviceIntSize size)
{
  wr_api_set_window_parameters(mRenderApi, size.width, size.height);
  wr_api_set_window_parameters(mDocHandle, size.width, size.height);
}

void
@@ -411,14 +411,14 @@ WebRenderAPI::WaitFlushed()
void
WebRenderAPI::SetRootPipeline(PipelineId aPipeline)
{
  wr_api_set_root_pipeline(mRenderApi, aPipeline);
  wr_api_set_root_pipeline(mDocHandle, aPipeline);
}

void
WebRenderAPI::AddImage(ImageKey key, const ImageDescriptor& aDescriptor,
                       Range<uint8_t> aBytes)
{
  wr_api_add_image(mRenderApi,
  wr_api_add_image(mDocHandle,
                   key,
                   &aDescriptor,
                   RangeToByteSlice(aBytes));
@@ -428,7 +428,7 @@ void
WebRenderAPI::AddBlobImage(ImageKey key, const ImageDescriptor& aDescriptor,
                           Range<uint8_t> aBytes)
{
  wr_api_add_blob_image(mRenderApi,
  wr_api_add_blob_image(mDocHandle,
                        key,
                        &aDescriptor,
                        RangeToByteSlice(aBytes));
@@ -441,7 +441,7 @@ WebRenderAPI::AddExternalImage(ImageKey key,
                               wr::WrExternalImageBufferType aBufferType,
                               uint8_t aChannelIndex)
{
  wr_api_add_external_image(mRenderApi,
  wr_api_add_external_image(mDocHandle,
                            key,
                            &aDescriptor,
                            aExtID,
@@ -465,7 +465,7 @@ WebRenderAPI::UpdateImageBuffer(ImageKey aKey,
                                const ImageDescriptor& aDescriptor,
                                Range<uint8_t> aBytes)
{
  wr_api_update_image(mRenderApi,
  wr_api_update_image(mDocHandle,
                      aKey,
                      &aDescriptor,
                      RangeToByteSlice(aBytes));
@@ -476,7 +476,7 @@ WebRenderAPI::UpdateBlobImage(ImageKey aKey,
                              const ImageDescriptor& aDescriptor,
                              Range<uint8_t> aBytes)
{
  wr_api_update_blob_image(mRenderApi,
  wr_api_update_blob_image(mDocHandle,
                           aKey,
                           &aDescriptor,
                           RangeToByteSlice(aBytes));
@@ -489,7 +489,7 @@ WebRenderAPI::UpdateExternalImage(ImageKey aKey,
                                  wr::WrExternalImageBufferType aBufferType,
                                  uint8_t aChannelIndex)
{
  wr_api_update_external_image(mRenderApi,
  wr_api_update_external_image(mDocHandle,
                               aKey,
                               &aDescriptor,
                               aExtID,
@@ -500,19 +500,19 @@ WebRenderAPI::UpdateExternalImage(ImageKey aKey,
void
WebRenderAPI::DeleteImage(ImageKey aKey)
{
  wr_api_delete_image(mRenderApi, aKey);
  wr_api_delete_image(mDocHandle, aKey);
}

void
WebRenderAPI::AddRawFont(wr::FontKey aKey, Range<uint8_t> aBytes, uint32_t aIndex)
{
  wr_api_add_raw_font(mRenderApi, aKey, &aBytes[0], aBytes.length(), aIndex);
  wr_api_add_raw_font(mDocHandle, aKey, &aBytes[0], aBytes.length(), aIndex);
}

void
WebRenderAPI::DeleteFont(wr::FontKey aKey)
{
  wr_api_delete_font(mRenderApi, aKey);
  wr_api_delete_font(mDocHandle, aKey);
}

class EnableProfiler : public RendererEvent
@@ -585,7 +585,7 @@ void
WebRenderAPI::RunOnRenderThread(UniquePtr<RendererEvent> aEvent)
{
  auto event = reinterpret_cast<uintptr_t>(aEvent.release());
  wr_api_send_external_event(mRenderApi, event);
  wr_api_send_external_event(mDocHandle, event);
}

DisplayListBuilder::DisplayListBuilder(PipelineId aId,
+3 −3
Original line number Diff line number Diff line
@@ -126,8 +126,8 @@ public:
  layers::SyncHandle GetSyncHandle() const { return mSyncHandle; }

protected:
  WebRenderAPI(wr::RenderApi* aRawApi, wr::WindowId aId, GLint aMaxTextureSize, bool aUseANGLE, layers::SyncHandle aSyncHandle)
    : mRenderApi(aRawApi)
  WebRenderAPI(wr::DocumentHandle* aHandle, wr::WindowId aId, GLint aMaxTextureSize, bool aUseANGLE, layers::SyncHandle aSyncHandle)
    : mDocHandle(aHandle)
    , mId(aId)
    , mMaxTextureSize(aMaxTextureSize)
    , mUseANGLE(aUseANGLE)
@@ -138,7 +138,7 @@ protected:
  // Should be used only for shutdown handling
  void WaitFlushed();

  wr::RenderApi* mRenderApi;
  wr::DocumentHandle* mDocHandle;
  wr::WindowId mId;
  GLint mMaxTextureSize;
  bool mUseANGLE;
+89 −68
Original line number Diff line number Diff line
@@ -56,6 +56,21 @@ fn make_slice_mut<'a, T>(ptr: *mut T, len: usize) -> &'a mut [T] {
    }
}

pub struct DocumentHandle {
    api: RenderApi,
    document_id: DocumentId,
}

impl DocumentHandle {
    pub fn new(api: RenderApi, size: DeviceUintSize) -> DocumentHandle {
        let doc = api.add_document(size);
        DocumentHandle {
            api: api,
            document_id: doc
        }
    }
}

#[repr(C)]
pub struct WrVecU8 {
    data: *mut u8,
@@ -531,7 +546,7 @@ pub extern "C" fn wr_window_new(window_id: WrWindowId,
                                gl_context: *mut c_void,
                                thread_pool: *mut WrThreadPool,
                                enable_profiler: bool,
                                out_api: &mut *mut RenderApi,
                                out_handle: &mut *mut DocumentHandle,
                                out_renderer: &mut *mut Renderer)
                                -> bool {
    assert!(unsafe { is_in_render_thread() });
@@ -571,8 +586,7 @@ pub extern "C" fn wr_window_new(window_id: WrWindowId,
        ..Default::default()
    };

    let window_size = DeviceUintSize::new(window_width, window_height);
    let (renderer, sender) = match Renderer::new(gl, opts, window_size) {
    let (renderer, sender) = match Renderer::new(gl, opts) {
        Ok((renderer, sender)) => (renderer, sender),
        Err(e) => {
            println!(" Failed to create a Renderer: {:?}", e);
@@ -588,7 +602,9 @@ pub extern "C" fn wr_window_new(window_id: WrWindowId,
                                              window_id: window_id,
                                          }));

    *out_api = Box::into_raw(Box::new(sender.create_api()));
    let window_size = DeviceUintSize::new(window_width, window_height);
    *out_handle = Box::into_raw(Box::new(
            DocumentHandle::new(sender.create_api(), window_size)));
    *out_renderer = Box::into_raw(Box::new(renderer));

    return true;
@@ -596,46 +612,47 @@ pub extern "C" fn wr_window_new(window_id: WrWindowId,

/// cbindgen:postfix=WR_DESTRUCTOR_SAFE_FUNC
#[no_mangle]
pub unsafe extern "C" fn wr_api_delete(api: *mut RenderApi) {
    let api = Box::from_raw(api);
    api.shut_down();
pub unsafe extern "C" fn wr_api_delete(dh: *mut DocumentHandle) {
    let handle = Box::from_raw(dh);
    handle.api.delete_document(handle.document_id);
    handle.api.shut_down();
}

#[no_mangle]
pub extern "C" fn wr_api_add_image(api: &mut RenderApi,
pub extern "C" fn wr_api_add_image(dh: &mut DocumentHandle,
                                   image_key: WrImageKey,
                                   descriptor: &WrImageDescriptor,
                                   bytes: ByteSlice) {
    assert!(unsafe { is_in_compositor_thread() });
    let copied_bytes = bytes.as_slice().to_owned();
    api.add_image(image_key,
    dh.api.add_image(image_key,
                     descriptor.into(),
                     ImageData::new(copied_bytes),
                     None);
}

#[no_mangle]
pub extern "C" fn wr_api_add_blob_image(api: &mut RenderApi,
pub extern "C" fn wr_api_add_blob_image(dh: &mut DocumentHandle,
                                        image_key: WrImageKey,
                                        descriptor: &WrImageDescriptor,
                                        bytes: ByteSlice) {
    assert!(unsafe { is_in_compositor_thread() });
    let copied_bytes = bytes.as_slice().to_owned();
    api.add_image(image_key,
    dh.api.add_image(image_key,
                     descriptor.into(),
                     ImageData::new_blob_image(copied_bytes),
                     None);
}

#[no_mangle]
pub extern "C" fn wr_api_add_external_image(api: &mut RenderApi,
pub extern "C" fn wr_api_add_external_image(dh: &mut DocumentHandle,
                                            image_key: WrImageKey,
                                            descriptor: &WrImageDescriptor,
                                            external_image_id: WrExternalImageId,
                                            buffer_type: WrExternalImageBufferType,
                                            channel_index: u8) {
    assert!(unsafe { is_in_compositor_thread() });
    api.add_image(image_key,
    dh.api.add_image(image_key,
                     descriptor.into(),
                     ImageData::External(ExternalImageData {
                                             id: external_image_id.into(),
@@ -646,19 +663,19 @@ pub extern "C" fn wr_api_add_external_image(api: &mut RenderApi,
}

#[no_mangle]
pub extern "C" fn wr_api_update_image(api: &mut RenderApi,
pub extern "C" fn wr_api_update_image(dh: &mut DocumentHandle,
                                      key: WrImageKey,
                                      descriptor: &WrImageDescriptor,
                                      bytes: ByteSlice) {
    assert!(unsafe { is_in_compositor_thread() });
    let copied_bytes = bytes.as_slice().to_owned();

    api.update_image(key, descriptor.into(), ImageData::new(copied_bytes), None);
    dh.api.update_image(key, descriptor.into(), ImageData::new(copied_bytes), None);
}

#[no_mangle]
pub extern "C" fn wr_api_update_external_image(
    api: &mut RenderApi,
    dh: &mut DocumentHandle,
    key: WrImageKey,
    descriptor: &WrImageDescriptor,
    external_image_id: WrExternalImageId,
@@ -675,17 +692,17 @@ pub extern "C" fn wr_api_update_external_image(
        }
    );

    api.update_image(key, descriptor.into(), data, None);
    dh.api.update_image(key, descriptor.into(), data, None);
}

#[no_mangle]
pub extern "C" fn wr_api_update_blob_image(api: &mut RenderApi,
pub extern "C" fn wr_api_update_blob_image(dh: &mut DocumentHandle,
                                           image_key: WrImageKey,
                                           descriptor: &WrImageDescriptor,
                                           bytes: ByteSlice) {
    assert!(unsafe { is_in_compositor_thread() });
    let copied_bytes = bytes.as_slice().to_owned();
    api.update_image(
    dh.api.update_image(
           image_key,
           descriptor.into(),
           ImageData::new_blob_image(copied_bytes),
@@ -694,28 +711,30 @@ pub extern "C" fn wr_api_update_blob_image(api: &mut RenderApi,
}

#[no_mangle]
pub extern "C" fn wr_api_delete_image(api: &mut RenderApi,
pub extern "C" fn wr_api_delete_image(dh: &mut DocumentHandle,
                                      key: WrImageKey) {
    assert!(unsafe { is_in_compositor_thread() });
    api.delete_image(key)
    dh.api.delete_image(key)
}

#[no_mangle]
pub extern "C" fn wr_api_set_root_pipeline(api: &mut RenderApi,
pub extern "C" fn wr_api_set_root_pipeline(dh: &mut DocumentHandle,
                                           pipeline_id: WrPipelineId) {
    api.set_root_pipeline(pipeline_id);
    dh.api.set_root_pipeline(dh.document_id, pipeline_id);
}

#[no_mangle]
pub extern "C" fn wr_api_set_window_parameters(api: &mut RenderApi,
pub extern "C" fn wr_api_set_window_parameters(dh: &mut DocumentHandle,
                                               width: i32,
                                               height: i32) {
    let size = DeviceUintSize::new(width as u32, height as u32);
    api.set_window_parameters(size, DeviceUintRect::new(DeviceUintPoint::new(0, 0), size));
    dh.api.set_window_parameters(dh.document_id,
                                 size,
                                 DeviceUintRect::new(DeviceUintPoint::new(0, 0), size));
}

#[no_mangle]
pub unsafe extern "C" fn wr_api_set_root_display_list(api: &mut RenderApi,
pub unsafe extern "C" fn wr_api_set_root_display_list(dh: &mut DocumentHandle,
                                                      color: ColorF,
                                                      epoch: WrEpoch,
                                                      viewport_width: f32,
@@ -741,34 +760,36 @@ pub unsafe extern "C" fn wr_api_set_root_display_list(api: &mut RenderApi,
    dl_vec.extend_from_slice(dl_slice);
    let dl = BuiltDisplayList::from_data(dl_vec, dl_descriptor);

    api.set_display_list(color,
    dh.api.set_display_list(dh.document_id,
                            epoch,
                            color,
                            LayoutSize::new(viewport_width, viewport_height),
                            (pipeline_id, content_size.into(), dl),
                            preserve_frame_state);
}

#[no_mangle]
pub unsafe extern "C" fn wr_api_clear_root_display_list(api: &mut RenderApi,
pub unsafe extern "C" fn wr_api_clear_root_display_list(dh: &mut DocumentHandle,
                                                        epoch: WrEpoch,
                                                        pipeline_id: WrPipelineId) {
    let preserve_frame_state = true;
    let frame_builder = WebRenderFrameBuilder::new(pipeline_id, LayoutSize::zero());

    api.set_display_list(None,
    dh.api.set_display_list(dh.document_id,
                            epoch,
                            None,
                            LayoutSize::new(0.0, 0.0),
                            frame_builder.dl_builder.finalize(),
                            preserve_frame_state);
}

#[no_mangle]
pub extern "C" fn wr_api_generate_frame(api: &mut RenderApi) {
    api.generate_frame(None);
pub extern "C" fn wr_api_generate_frame(dh: &mut DocumentHandle) {
    dh.api.generate_frame(dh.document_id, None);
}

#[no_mangle]
pub extern "C" fn wr_api_generate_frame_with_properties(api: &mut RenderApi,
pub extern "C" fn wr_api_generate_frame_with_properties(dh: &mut DocumentHandle,
                                                        opacity_array: *const WrOpacityProperty,
                                                        opacity_count: usize,
                                                        transform_array: *const WrTransformProperty,
@@ -803,20 +824,20 @@ pub extern "C" fn wr_api_generate_frame_with_properties(api: &mut RenderApi,
        }
    }

    api.generate_frame(Some(properties));
    dh.api.generate_frame(dh.document_id, Some(properties));
}

/// cbindgen:postfix=WR_DESTRUCTOR_SAFE_FUNC
#[no_mangle]
pub extern "C" fn wr_api_send_external_event(api: &mut RenderApi,
pub extern "C" fn wr_api_send_external_event(dh: &mut DocumentHandle,
                                             evt: usize) {
    assert!(unsafe { !is_in_render_thread() });

    api.send_external_event(ExternalEvent::from_raw(evt));
    dh.api.send_external_event(ExternalEvent::from_raw(evt));
}

#[no_mangle]
pub extern "C" fn wr_api_add_raw_font(api: &mut RenderApi,
pub extern "C" fn wr_api_add_raw_font(dh: &mut DocumentHandle,
                                      key: WrFontKey,
                                      font_buffer: *mut u8,
                                      buffer_size: usize,
@@ -827,19 +848,19 @@ pub extern "C" fn wr_api_add_raw_font(api: &mut RenderApi,
    let mut font_vector = Vec::new();
    font_vector.extend_from_slice(font_slice);

    api.add_raw_font(key, font_vector, index);
    dh.api.add_raw_font(key, font_vector, index);
}

#[no_mangle]
pub extern "C" fn wr_api_delete_font(api: &mut RenderApi,
pub extern "C" fn wr_api_delete_font(dh: &mut DocumentHandle,
                                     key: WrFontKey) {
    assert!(unsafe { is_in_compositor_thread() });
    api.delete_font(key);
    dh.api.delete_font(key);
}

#[no_mangle]
pub unsafe extern "C" fn wr_api_get_namespace(api: &mut RenderApi) -> WrIdNamespace {
    api.id_namespace
pub unsafe extern "C" fn wr_api_get_namespace(dh: &mut DocumentHandle) -> WrIdNamespace {
    dh.document_id.0
}

// RenderThread WIP notes:
@@ -1050,13 +1071,13 @@ pub extern "C" fn wr_dp_pop_scroll_layer(state: &mut WrState) {
}

#[no_mangle]
pub extern "C" fn wr_scroll_layer_with_id(api: &mut RenderApi,
pub extern "C" fn wr_scroll_layer_with_id(dh: &mut DocumentHandle,
                                          pipeline_id: WrPipelineId,
                                          scroll_id: u64,
                                          new_scroll_origin: LayoutPoint) {
    assert!(unsafe { is_in_compositor_thread() });
    let clip_id = ClipId::new(scroll_id, pipeline_id);
    api.scroll_node_with_id(new_scroll_origin.into(), clip_id, ScrollClamping::NoClamping);
    dh.api.scroll_node_with_id(dh.document_id, new_scroll_origin.into(), clip_id, ScrollClamping::NoClamping);
}

#[no_mangle]