Skip to content

Commit

Permalink
osr: Implement shared texture support (fixes #1006, fixes #2575)
Browse files Browse the repository at this point in the history
Adds support for the OnAcceleratedPaint callback. Verified to work
on macOS and Windows. Linux support is present but not implemented
for cefclient, so it is not verified to work.

To test:
Run `cefclient --off-screen-rendering-enabled --shared-texture-enabled`
  • Loading branch information
reitowo authored and magreenblatt committed Apr 23, 2024
1 parent dca0435 commit 260dd0c
Show file tree
Hide file tree
Showing 43 changed files with 768 additions and 108 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,7 @@ if (is_mac) {
]
frameworks = [
"AppKit.framework",
"IOSurface.framework",
"OpenGL.framework",
]
defines = [
Expand Down
1 change: 1 addition & 0 deletions cef_paths2.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'include/internal/cef_types_content_settings.h',
'include/internal/cef_types_geometry.h',
'include/internal/cef_types_runtime.h',
'include/internal/cef_types_color.h',
],
'includes_capi': [
'include/capi/cef_base_capi.h',
Expand Down
34 changes: 22 additions & 12 deletions include/capi/cef_render_handler_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=5309b2f6da62526ed92c928c0918bc27898cf03b$
// $hash=5151b6ea3c06e46a75f2cd7679044a2891063d29$
//

#ifndef CEF_INCLUDE_CAPI_CEF_RENDER_HANDLER_CAPI_H_
Expand Down Expand Up @@ -149,17 +149,27 @@ typedef struct _cef_render_handler_t {
/// Called when an element has been rendered to the shared texture handle.
/// |type| indicates whether the element is the view or the popup widget.
/// |dirtyRects| contains the set of rectangles in pixel coordinates that need
/// to be repainted. |shared_handle| is the handle for a D3D11 Texture2D that
/// can be accessed via ID3D11Device using the OpenSharedResource function.
/// This function is only called when cef_window_tInfo::shared_texture_enabled
/// is set to true (1), and is currently only supported on Windows.
///
void(CEF_CALLBACK* on_accelerated_paint)(struct _cef_render_handler_t* self,
struct _cef_browser_t* browser,
cef_paint_element_type_t type,
size_t dirtyRectsCount,
cef_rect_t const* dirtyRects,
void* shared_handle);
/// to be repainted. |info| contains the shared handle; on Windows it is a
/// HANDLE to a texture that can be opened with D3D11 OpenSharedResource, on
/// macOS it is an IOSurface pointer that can be opened with Metal or OpenGL,
/// and on Linux it contains several planes, each with an fd to the underlying
/// system native buffer.
///
/// The underlying implementation uses a pool to deliver frames. As a result,
/// the handle may differ every frame depending on how many frames are in-
/// progress. The handle's resource cannot be cached and cannot be accessed
/// outside of this callback. It should be reopened each time this callback is
/// executed and the contents should be copied to a texture owned by the
/// client application. The contents of |info| will be released back to the
/// pool after this callback returns.
///
void(CEF_CALLBACK* on_accelerated_paint)(
struct _cef_render_handler_t* self,
struct _cef_browser_t* browser,
cef_paint_element_type_t type,
size_t dirtyRectsCount,
cef_rect_t const* dirtyRects,
const cef_accelerated_paint_info_t* info);

///
/// Called to retrieve the size of the touch handle for the specified
Expand Down
8 changes: 4 additions & 4 deletions include/cef_api_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
// way that may cause binary incompatibility with other builds. The universal
// hash value will change if any platform is affected whereas the platform hash
// values will change only if that particular platform is affected.
#define CEF_API_HASH_UNIVERSAL "faa2d7c5a95e129e5a746785cbbcb5f5f99cfe53"
#define CEF_API_HASH_UNIVERSAL "984fcdc779268524c848d94232e2d045d9a42852"
#if defined(OS_WIN)
#define CEF_API_HASH_PLATFORM "9123354f4395b59f275ee3389f21734aeeac7a5e"
#define CEF_API_HASH_PLATFORM "3975a9042b7124d15f39044972f879066f61e9dd"
#elif defined(OS_MAC)
#define CEF_API_HASH_PLATFORM "5ae2b1b7fbe4bdbd25dc6f707f6e3bfc163120ad"
#define CEF_API_HASH_PLATFORM "ffdd4259a0790d813627975952d0e714818c24b2"
#elif defined(OS_LINUX)
#define CEF_API_HASH_PLATFORM "41ddfb7023cda15e5b813b237b06111b6d4cabb8"
#define CEF_API_HASH_PLATFORM "f0dff25daca39032db1f882545b3ad880e731060"
#endif

#ifdef __cplusplus
Expand Down
19 changes: 14 additions & 5 deletions include/cef_render_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,25 @@ class CefRenderHandler : public virtual CefBaseRefCounted {
/// Called when an element has been rendered to the shared texture handle.
/// |type| indicates whether the element is the view or the popup widget.
/// |dirtyRects| contains the set of rectangles in pixel coordinates that need
/// to be repainted. |shared_handle| is the handle for a D3D11 Texture2D that
/// can be accessed via ID3D11Device using the OpenSharedResource method. This
/// method is only called when CefWindowInfo::shared_texture_enabled is set to
/// true, and is currently only supported on Windows.
/// to be repainted. |info| contains the shared handle; on Windows it is a
/// HANDLE to a texture that can be opened with D3D11 OpenSharedResource, on
/// macOS it is an IOSurface pointer that can be opened with Metal or OpenGL,
/// and on Linux it contains several planes, each with an fd to the underlying
/// system native buffer.
///
/// The underlying implementation uses a pool to deliver frames. As a result,
/// the handle may differ every frame depending on how many frames are
/// in-progress. The handle's resource cannot be cached and cannot be accessed
/// outside of this callback. It should be reopened each time this callback is
/// executed and the contents should be copied to a texture owned by the
/// client application. The contents of |info| will be released back to the
/// pool after this callback returns.
///
/*--cef()--*/
virtual void OnAcceleratedPaint(CefRefPtr<CefBrowser> browser,
PaintElementType type,
const RectList& dirtyRects,
void* shared_handle) {}
const CefAcceleratedPaintInfo& info) {}

///
/// Called to retrieve the size of the touch handle for the specified
Expand Down
15 changes: 0 additions & 15 deletions include/internal/cef_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2925,21 +2925,6 @@ typedef enum {
RESPONSE_FILTER_ERROR
} cef_response_filter_status_t;

///
/// Describes how to interpret the components of a pixel.
///
typedef enum {
///
/// RGBA with 8 bits per pixel (32bits total).
///
CEF_COLOR_TYPE_RGBA_8888,

///
/// BGRA with 8 bits per pixel (32bits total).
///
CEF_COLOR_TYPE_BGRA_8888,
} cef_color_type_t;

///
/// Describes how to interpret the alpha component of a pixel.
///
Expand Down
57 changes: 57 additions & 0 deletions include/internal/cef_types_color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2024 Marshall A. Greenblatt. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// 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
// OWNER 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.

#ifndef CEF_INCLUDE_INTERNAL_CEF_TYPES_COLOR_H_
#define CEF_INCLUDE_INTERNAL_CEF_TYPES_COLOR_H_
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

///
/// Describes how to interpret the components of a pixel.
///
typedef enum {
///
/// RGBA with 8 bits per pixel (32bits total).
///
CEF_COLOR_TYPE_RGBA_8888,

///
/// BGRA with 8 bits per pixel (32bits total).
///
CEF_COLOR_TYPE_BGRA_8888,
} cef_color_type_t;

#ifdef __cplusplus
}
#endif

#endif // CEF_INCLUDE_INTERNAL_CEF_TYPES_COLOR_H_
51 changes: 51 additions & 0 deletions include/internal/cef_types_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef struct _XDisplay XDisplay;

#include "include/internal/cef_export.h"
#include "include/internal/cef_string.h"
#include "include/internal/cef_types_color.h"
#include "include/internal/cef_types_geometry.h"
#include "include/internal/cef_types_runtime.h"

Expand Down Expand Up @@ -145,6 +146,56 @@ typedef struct _cef_window_info_t {
cef_runtime_style_t runtime_style;
} cef_window_info_t;

///
/// Structure containing the plane information of the shared texture.
/// Sync with native_pixmap_handle.h
///
typedef struct _cef_accelerated_paint_native_pixmap_plane_info_t {
///
/// The strides and offsets in bytes to be used when accessing the buffers via
/// a memory mapping. One per plane per entry. Size in bytes of the plane is
/// necessary to map the buffers.
///
uint32_t stride;
uint64_t offset;
uint64_t size;

///
/// File descriptor for the underlying memory object (usually dmabuf).
///
int fd;
} cef_accelerated_paint_native_pixmap_plane_t;

#define kAcceleratedPaintMaxPlanes 4

///
/// Structure containing shared texture information for the OnAcceleratedPaint
/// callback. Resources will be released to the underlying pool for reuse when
/// the callback returns from client code.
///
typedef struct _cef_accelerated_paint_info_t {
///
/// Planes of the shared texture, usually file descriptors of dmabufs.
///
cef_accelerated_paint_native_pixmap_plane_t
planes[kAcceleratedPaintMaxPlanes];

///
/// Plane count.
///
int plane_count;

///
/// Modifier could be used with EGL driver.
///
uint64_t modifier;

///
/// The pixel format of the texture.
///
cef_color_type_t format;
} cef_accelerated_paint_info_t;

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions include/internal/cef_types_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#if defined(OS_MAC)
#include "include/internal/cef_string.h"
#include "include/internal/cef_types_color.h"
#include "include/internal/cef_types_geometry.h"
#include "include/internal/cef_types_runtime.h"

Expand All @@ -45,6 +46,8 @@
#define cef_event_handle_t void*
// Actually NSView*
#define cef_window_handle_t void*
// Actually IOSurface*
#define cef_shared_texture_handle_t void*

#define kNullCursorHandle NULL
#define kNullEventHandle NULL
Expand Down Expand Up @@ -144,6 +147,23 @@ typedef struct _cef_window_info_t {
cef_runtime_style_t runtime_style;
} cef_window_info_t;

///
/// Structure containing shared texture information for the OnAcceleratedPaint
/// callback. Resources will be released to the underlying pool for reuse when
/// the callback returns from client code.
///
typedef struct _cef_accelerated_paint_info_t {
///
/// Handle for the shared texture IOSurface.
///
cef_shared_texture_handle_t shared_texture_io_surface;

///
/// The pixel format of the texture.
///
cef_color_type_t format;
} cef_accelerated_paint_info_t;

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions include/internal/cef_types_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
#include <windows.h>

#include "include/internal/cef_string.h"
#include "include/internal/cef_types_color.h"
#include "include/internal/cef_types_geometry.h"
#include "include/internal/cef_types_runtime.h"

// Handle types.
#define cef_cursor_handle_t HCURSOR
#define cef_event_handle_t MSG*
#define cef_window_handle_t HWND
#define cef_shared_texture_handle_t HANDLE

#define kNullCursorHandle NULL
#define kNullEventHandle NULL
Expand Down Expand Up @@ -112,6 +114,24 @@ typedef struct _cef_window_info_t {
cef_runtime_style_t runtime_style;
} cef_window_info_t;

///
/// Structure containing shared texture information for the OnAcceleratedPaint
/// callback. Resources will be released to the underlying pool for reuse when
/// the callback returns from client code.
///
typedef struct _cef_accelerated_paint_info_t {
///
/// Handle for the shared texture. The shared texture is instantiated
/// without a keyed mutex.
///
cef_shared_texture_handle_t shared_texture_handle;

///
/// The pixel format of the texture.
///
cef_color_type_t format;
} cef_accelerated_paint_info_t;

#ifdef __cplusplus
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions include/internal/cef_types_wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -750,4 +750,14 @@ struct CefMediaSinkDeviceInfoTraits {
///
using CefMediaSinkDeviceInfo = CefStructBase<CefMediaSinkDeviceInfoTraits>;

///
/// Class representing accelerated paint info.
///
class CefAcceleratedPaintInfo : public cef_accelerated_paint_info_t {
public:
CefAcceleratedPaintInfo() : cef_accelerated_paint_info_t{} {}
CefAcceleratedPaintInfo(const cef_accelerated_paint_info_t& r)
: cef_accelerated_paint_info_t(r) {}
};

#endif // CEF_INCLUDE_INTERNAL_CEF_TYPES_WRAPPERS_H_
4 changes: 2 additions & 2 deletions libcef/browser/browser_platform_delegate_create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ std::unique_ptr<CefBrowserPlatformDelegateOsr> CreateOSRDelegate(
std::move(native_delegate), use_shared_texture, use_external_begin_frame);
#elif BUILDFLAG(IS_MAC)
return std::make_unique<CefBrowserPlatformDelegateOsrMac>(
std::move(native_delegate));
std::move(native_delegate), use_shared_texture, use_external_begin_frame);
#elif BUILDFLAG(IS_LINUX)
return std::make_unique<CefBrowserPlatformDelegateOsrLinux>(
std::move(native_delegate), use_external_begin_frame);
std::move(native_delegate), use_shared_texture, use_external_begin_frame);
#endif
}

Expand Down
3 changes: 2 additions & 1 deletion libcef/browser/osr/browser_platform_delegate_osr_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

CefBrowserPlatformDelegateOsrLinux::CefBrowserPlatformDelegateOsrLinux(
std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate,
bool use_shared_texture,
bool use_external_begin_frame)
: CefBrowserPlatformDelegateOsr(std::move(native_delegate),
/*use_shared_texture=*/false,
use_shared_texture,
use_external_begin_frame) {}

CefWindowHandle CefBrowserPlatformDelegateOsrLinux::GetHostWindowHandle()
Expand Down
1 change: 1 addition & 0 deletions libcef/browser/osr/browser_platform_delegate_osr_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class CefBrowserPlatformDelegateOsrLinux
public:
CefBrowserPlatformDelegateOsrLinux(
std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate,
bool use_shared_texture,
bool use_external_begin_frame);

// CefBrowserPlatformDelegate methods:
Expand Down
4 changes: 3 additions & 1 deletion libcef/browser/osr/browser_platform_delegate_osr_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
class CefBrowserPlatformDelegateOsrMac : public CefBrowserPlatformDelegateOsr {
public:
explicit CefBrowserPlatformDelegateOsrMac(
std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate);
std::unique_ptr<CefBrowserPlatformDelegateNative> native_delegate,
bool use_shared_texture,
bool use_external_begin_frame);

// CefBrowserPlatformDelegate methods:
CefWindowHandle GetHostWindowHandle() const override;
Expand Down

0 comments on commit 260dd0c

Please sign in to comment.