#ifndef UI_GL_GL_SURFACE_H_
#define UI_GL_GL_SURFACE_H_
#include <vector>
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/delegated_ink_metadata.h"
#include "ui/gfx/frame_data.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/overlay_priority_hint.h"
#include "ui/gfx/overlay_transform.h"
#include "ui/gfx/presentation_feedback.h"
#include "ui/gfx/surface_origin.h"
#include "ui/gfx/swap_result.h"
#include "ui/gl/gl_display.h"
#include "ui/gl/gl_export.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_format.h"
#include "ui/gl/gpu_preference.h"
#if BUILDFLAG(IS_OZONE)
#include "ui/gfx/native_pixmap.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "ui/gfx/mac/io_surface.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/scoped_hardware_buffer_fence_sync.h"
#endif
namespace gfx {
namespace mojom {
class DelegatedInkPointRenderer;
}
class ColorSpace;
class VSyncProvider;
}
namespace gl {
struct DCLayerOverlayParams;
class GLContext;
class EGLTimestampClient;
class GL_EXPORT GLSurface : public base::RefCounted<GLSurface>,
public base::SupportsWeakPtr<GLSurface> {
public:
GLSurface();
GLSurface(const GLSurface&) = delete;
GLSurface& operator=(const GLSurface&) = delete;
bool Initialize();
virtual bool Initialize(GLSurfaceFormat format);
virtual void Destroy() = 0;
virtual bool Resize(const gfx::Size& size,
float scale_factor,
const gfx::ColorSpace& color_space,
bool has_alpha);
virtual bool Recreate();
virtual bool DeferDraws();
virtual bool IsOffscreen() = 0;
using PresentationCallback =
base::OnceCallback<void(const gfx::PresentationFeedback& feedback)>;
virtual gfx::SwapResult SwapBuffers(PresentationCallback callback,
gfx::FrameData data) = 0;
virtual gfx::Size GetSize() = 0;
virtual void* GetHandle() = 0;
virtual bool SupportsSwapBuffersWithBounds();
#if BUILDFLAG(IS_OHOS)
virtual gfx::SwapResult SwapBuffersWithDamage(const std::vector<int>& rects,
PresentationCallback callback,
gfx::FrameData data);
virtual void SetNativeInnerWeb(bool isInnerWeb) {}
#endif
virtual bool SupportsPostSubBuffer();
virtual bool SupportsAsyncSwap();
virtual unsigned int GetBackingFramebufferObject();
using SwapCompletionCallback =
base::OnceCallback<void(gfx::SwapCompletionResult)>;
virtual void SwapBuffersAsync(SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data);
virtual gfx::SwapResult SwapBuffersWithBounds(
const std::vector<gfx::Rect>& rects,
PresentationCallback callback,
gfx::FrameData data);
virtual gfx::SwapResult PostSubBuffer(int x,
int y,
int width,
int height,
PresentationCallback callback,
gfx::FrameData data);
virtual void PostSubBufferAsync(int x,
int y,
int width,
int height,
SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data);
virtual bool OnMakeCurrent(GLContext* context);
virtual bool SetBackbufferAllocation(bool allocated);
virtual void SetFrontbufferAllocation(bool allocated);
virtual void* GetShareHandle();
virtual GLDisplay* GetGLDisplay();
virtual void* GetConfig();
virtual GLSurfaceFormat GetFormat() = 0;
virtual gfx::VSyncProvider* GetVSyncProvider();
virtual void SetVSyncEnabled(bool enabled);
virtual bool ScheduleDCLayer(std::unique_ptr<DCLayerOverlayParams> params);
virtual bool SetEnableDCLayers(bool enable);
virtual bool IsSurfaceless() const;
virtual gfx::SurfaceOrigin GetOrigin() const;
virtual bool BuffersFlipped() const;
virtual bool SupportsDCLayers() const;
virtual bool SupportsProtectedVideo() const;
virtual bool SupportsOverridePlatformSize() const;
virtual bool SetDrawRectangle(const gfx::Rect& rect);
virtual gfx::Vector2d GetDrawOffset() const;
virtual bool SupportsSwapTimestamps() const;
virtual void SetEnableSwapTimestamps();
virtual bool SupportsPlaneGpuFences() const;
virtual int GetBufferCount() const;
virtual EGLTimestampClient* GetEGLTimestampClient();
virtual bool SupportsGpuVSync() const;
virtual void SetGpuVSyncEnabled(bool enabled);
virtual void SetFrameRate(float frame_rate) {}
static GLSurface* GetCurrent();
virtual void SetCurrent();
virtual bool IsCurrent();
static bool ExtensionsContain(const char* extensions, const char* name);
virtual bool SupportsDelegatedInk();
virtual void SetDelegatedInkTrailStartPoint(
std::unique_ptr<gfx::DelegatedInkMetadata> metadata) {}
virtual void InitDelegatedInkPointRendererReceiver(
mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
pending_receiver);
static void SetForcedGpuPreference(GpuPreference gpu_preference);
static GpuPreference AdjustGpuPreference(GpuPreference gpu_preference);
protected:
virtual ~GLSurface();
static GpuPreference forced_gpu_preference_;
private:
static void ClearCurrent();
friend class base::RefCounted<GLSurface>;
friend class GLContext;
};
class GL_EXPORT GLSurfaceAdapter : public GLSurface {
public:
explicit GLSurfaceAdapter(GLSurface* surface);
GLSurfaceAdapter(const GLSurfaceAdapter&) = delete;
GLSurfaceAdapter& operator=(const GLSurfaceAdapter&) = delete;
bool Initialize(GLSurfaceFormat format) override;
void Destroy() override;
bool Resize(const gfx::Size& size,
float scale_factor,
const gfx::ColorSpace& color_space,
bool has_alpha) override;
bool Recreate() override;
bool DeferDraws() override;
bool IsOffscreen() override;
gfx::SwapResult SwapBuffers(PresentationCallback callback,
gfx::FrameData data) override;
#if BUILDFLAG(IS_OHOS)
gfx::SwapResult SwapBuffersWithDamage(const std::vector<int>& rects,
PresentationCallback callback,
gfx::FrameData data) override;
void SetNativeInnerWeb(bool isInnerWeb) override;
#endif
void SwapBuffersAsync(SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) override;
gfx::SwapResult SwapBuffersWithBounds(const std::vector<gfx::Rect>& rects,
PresentationCallback callback,
gfx::FrameData data) override;
gfx::SwapResult PostSubBuffer(int x,
int y,
int width,
int height,
PresentationCallback callback,
gfx::FrameData data) override;
void PostSubBufferAsync(int x,
int y,
int width,
int height,
SwapCompletionCallback completion_callback,
PresentationCallback presentation_callback,
gfx::FrameData data) override;
bool SupportsSwapBuffersWithBounds() override;
bool SupportsPostSubBuffer() override;
bool SupportsAsyncSwap() override;
gfx::Size GetSize() override;
void* GetHandle() override;
unsigned int GetBackingFramebufferObject() override;
bool OnMakeCurrent(GLContext* context) override;
bool SetBackbufferAllocation(bool allocated) override;
void SetFrontbufferAllocation(bool allocated) override;
void* GetShareHandle() override;
GLDisplay* GetGLDisplay() override;
void* GetConfig() override;
GLSurfaceFormat GetFormat() override;
gfx::VSyncProvider* GetVSyncProvider() override;
void SetVSyncEnabled(bool enabled) override;
bool ScheduleDCLayer(std::unique_ptr<DCLayerOverlayParams> params) override;
bool SetEnableDCLayers(bool enable) override;
bool IsSurfaceless() const override;
gfx::SurfaceOrigin GetOrigin() const override;
bool BuffersFlipped() const override;
bool SupportsDCLayers() const override;
bool SupportsProtectedVideo() const override;
bool SupportsOverridePlatformSize() const override;
bool SetDrawRectangle(const gfx::Rect& rect) override;
gfx::Vector2d GetDrawOffset() const override;
bool SupportsSwapTimestamps() const override;
void SetEnableSwapTimestamps() override;
bool SupportsPlaneGpuFences() const override;
int GetBufferCount() const override;
bool SupportsGpuVSync() const override;
void SetGpuVSyncEnabled(bool enabled) override;
void SetFrameRate(float frame_rate) override;
void SetCurrent() override;
bool IsCurrent() override;
bool SupportsDelegatedInk() override;
void SetDelegatedInkTrailStartPoint(
std::unique_ptr<gfx::DelegatedInkMetadata> metadata) override;
void InitDelegatedInkPointRendererReceiver(
mojo::PendingReceiver<gfx::mojom::DelegatedInkPointRenderer>
pending_receiver) override;
GLSurface* surface() const { return surface_.get(); }
protected:
~GLSurfaceAdapter() override;
private:
scoped_refptr<GLSurface> surface_;
};
GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurface(
scoped_refptr<GLSurface> surface);
GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurfaceWithFormat(
scoped_refptr<GLSurface> surface,
GLSurfaceFormat format);
}
#endif