Skip to content

Commit

Permalink
Render overlay textures in browser sources, such as <select> menus
Browse files Browse the repository at this point in the history
  • Loading branch information
WizardCM committed Jan 28, 2023
1 parent c2aaaef commit 0e82928
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
90 changes: 84 additions & 6 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,47 @@ bool BrowserClient::OnTooltip(CefRefPtr<CefBrowser>, CefString &text)
#endif
}

void BrowserClient::OnPopupShow(CefRefPtr<CefBrowser>, bool show)
{
if (!show && bs->texture_overlay) {
obs_enter_graphics();
gs_texture_destroy(bs->texture_overlay);
bs->texture_overlay = nullptr;
obs_leave_graphics();
}
}

void BrowserClient::OnPopupSize(CefRefPtr<CefBrowser>, const CefRect &rect)
{
if (rect.width > 0) {
bs->overlay_x = rect.x;
bs->overlay_y = rect.y;
}
}

void BrowserClient::OnPaint(CefRefPtr<CefBrowser>, PaintElementType type,
const RectList &, const void *buffer, int width,
int height)
{
if (type != PET_VIEW) {
// TODO Overlay texture on top of bs->texture
if (type == PET_POPUP && !!bs->texture && valid()) {
obs_enter_graphics();
if (bs->texture_overlay) {
gs_texture_destroy(bs->texture_overlay);
bs->texture_overlay = nullptr;
}

if (!bs->texture_overlay) {
bs->texture_overlay = gs_texture_create(
width, height, GS_BGRA, 1,
(const uint8_t **)&buffer, GS_DYNAMIC);
} else {
gs_texture_set_image(bs->texture_overlay,
(const uint8_t *)buffer, width * 4,
false);
}
obs_leave_graphics();
return;
} else if (type != PET_VIEW) {
return;
}

Expand Down Expand Up @@ -385,8 +420,29 @@ void BrowserClient::OnAcceleratedPaint(CefRefPtr<CefBrowser>,
PaintElementType type, const RectList &,
void *shared_handle)
{
if (type != PET_VIEW) {
// TODO Overlay texture on top of bs->texture
if (type == PET_POPUP && !!bs->texture && valid()) {
obs_enter_graphics();
if (bs->texture_overlay) {
gs_texture_destroy(bs->texture_overlay);
bs->texture_overlay = nullptr;
}
if (!shared_handle) {
obs_leave_graphics();
return;
}
#if defined(__APPLE__) && CHROME_VERSION_BUILD > 4183
bs->texture_overlay = gs_texture_create_from_iosurface(
(IOSurfaceRef)(uintptr_t)shared_handle);
#elif defined(_WIN32) && CHROME_VERSION_BUILD > 4183
bs->texture_overlay = gs_texture_open_nt_shared(
(uint32_t)(uintptr_t)shared_handle);
#else
bs->texture_overlay = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
#endif
obs_leave_graphics();
return;
} else if (type != PET_VIEW) {
return;
}

Expand Down Expand Up @@ -433,8 +489,30 @@ void BrowserClient::OnAcceleratedPaint2(CefRefPtr<CefBrowser>,
PaintElementType type, const RectList &,
void *shared_handle, bool new_texture)
{
if (type != PET_VIEW) {
// TODO Overlay texture on top of bs->texture
if (type == PET_POPUP && !!bs->texture && valid()) {
obs_enter_graphics();
if (bs->texture_overlay) {
gs_texture_destroy(bs->texture_overlay);
bs->texture_overlay = nullptr;
}
if (!shared_handle) {
obs_leave_graphics();
return;
}

#if defined(__APPLE__) && CHROME_VERSION_BUILD > 4183
bs->texture_overlay = gs_texture_create_from_iosurface(
(IOSurfaceRef)(uintptr_t)shared_handle);
#elif defined(_WIN32) && CHROME_VERSION_BUILD > 4183
bs->texture_overlay = gs_texture_open_nt_shared(
(uint32_t)(uintptr_t)shared_handle);
#else
bs->texture_overlay = gs_texture_open_shared(
(uint32_t)(uintptr_t)shared_handle);
#endif
obs_leave_graphics();
return;
} else if (type != PET_VIEW) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions browser-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class BrowserClient : public CefClient,
/* CefRenderHandler */
virtual void GetViewRect(CefRefPtr<CefBrowser> browser,
CefRect &rect) override;
virtual void OnPopupShow(CefRefPtr<CefBrowser> browser,
bool show) override;
virtual void OnPopupSize(CefRefPtr<CefBrowser> browser,
const CefRect &rect) override;
virtual void OnPaint(CefRefPtr<CefBrowser> browser,
PaintElementType type, const RectList &dirtyRects,
const void *buffer, int width,
Expand Down
21 changes: 21 additions & 0 deletions obs-browser-source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ void BrowserSource::Render()
gs_blend_state_pop();

gs_enable_framebuffer_srgb(previous);

while (gs_effect_loop(effect, "Draw")) {
DrawOverlay(flip_flag);
}
}

#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED) && \
Expand All @@ -699,6 +703,23 @@ void BrowserSource::Render()
#endif
}

void BrowserSource::DrawOverlay(bool flip)
{
if (!!texture && !!texture_overlay) {
gs_blend_state_push();
gs_blend_function_separate(GS_BLEND_SRCALPHA,
GS_BLEND_INVSRCALPHA /*dest_color*/,
GS_BLEND_ONE /*src_alpha*/,
GS_BLEND_INVSRCALPHA /*dest_alpha*/);
gs_matrix_push();
obs_source_draw(texture_overlay, overlay_x, overlay_y, 0, 0,
flip);
gs_matrix_pop();

gs_blend_state_pop();
}
}

static void ExecuteOnBrowser(BrowserFunc func, BrowserSource *bs)
{
lock_guard<mutex> lock(browser_list_mutex);
Expand Down
4 changes: 4 additions & 0 deletions obs-browser-source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ struct BrowserSource {
std::string url;
std::string css;
gs_texture_t *texture = nullptr;
gs_texture_t *texture_overlay = nullptr;
gs_texture_t *extra_texture = nullptr;
uint32_t last_cx = 0;
uint32_t last_cy = 0;
int overlay_x = 0;
int overlay_y = 0;
gs_color_format last_format = GS_UNKNOWN;

#ifdef ENABLE_BROWSER_SHARED_TEXTURE
Expand Down Expand Up @@ -131,6 +134,7 @@ struct BrowserSource {
void Update(obs_data_t *settings = nullptr);
void Tick();
void Render();
void DrawOverlay(bool flip);
#if CHROME_VERSION_BUILD < 4103
void ClearAudioStreams();
void EnumAudioStreams(obs_source_enum_proc_t cb, void *param);
Expand Down

0 comments on commit 0e82928

Please sign in to comment.