123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- // Copyright (c) 2012 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef UI_GL_GL_SURFACE_H_
- #define UI_GL_GL_SURFACE_H_
- #include <vector>
- #include "base/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/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"
- namespace gfx {
- namespace mojom {
- class DelegatedInkPointRenderer;
- } // namespace mojom
- class ColorSpace;
- struct OverlayPlaneData;
- class GpuFence;
- class VSyncProvider;
- } // namespace gfx
- namespace ui {
- struct CARendererLayerParams;
- struct DCRendererLayerParams;
- } // namespace ui
- namespace gl {
- class GLContext;
- class GLImage;
- class EGLTimestampClient;
- // Encapsulates a surface that can be rendered to with GL, hiding platform
- // specific management.
- class GL_EXPORT GLSurface : public base::RefCounted<GLSurface>,
- public base::SupportsWeakPtr<GLSurface> {
- public:
- GLSurface();
- GLSurface(const GLSurface&) = delete;
- GLSurface& operator=(const GLSurface&) = delete;
- // Non-virtual initialization, this always calls Initialize with a
- // default GLSurfaceFormat. Subclasses should override the format-
- // specific Initialize method below and interpret the default format
- // as appropriate.
- bool Initialize();
- // (Re)create the surface. TODO(apatrick): This is an ugly hack to allow the
- // EGL surface associated to be recreated without destroying the associated
- // context. The implementation of this function for other GLSurface derived
- // classes is in a pending changelist.
- virtual bool Initialize(GLSurfaceFormat format);
- // Destroys the surface.
- virtual void Destroy() = 0;
- // Some implementations (macOS), in Destroy, will need to delete GL objects
- // that exist in the current GL context. This method is called before the
- // context's decoder (and potentially context itself) are destroyed, giving an
- // opportunity for this cleanup.
- virtual void PrepareToDestroy(bool have_context);
- // Resizes the surface, returning success. If failed, it is possible that the
- // context is no longer current.
- virtual bool Resize(const gfx::Size& size,
- float scale_factor,
- const gfx::ColorSpace& color_space,
- bool has_alpha);
- // Recreate the surface without changing the size, returning success. If
- // failed, it is possible that the context is no longer current.
- virtual bool Recreate();
- // Unschedule the CommandExecutor and return true to abort the processing of
- // a GL draw call to this surface and defer it until the CommandExecutor is
- // rescheduled.
- virtual bool DeferDraws();
- // Returns true if this surface is offscreen.
- virtual bool IsOffscreen() = 0;
- // The callback is for receiving presentation feedback from |SwapBuffers|,
- // |PostSubBuffer|, |CommitOverlayPlanes|, etc.
- // See |PresentationFeedback| for detail.
- using PresentationCallback =
- base::OnceCallback<void(const gfx::PresentationFeedback& feedback)>;
- // Swaps front and back buffers. This has no effect for off-screen
- // contexts. If it returns SWAP_FAILED, it is possible that the context is no
- // longer current.
- virtual gfx::SwapResult SwapBuffers(PresentationCallback callback) = 0;
- // Get the size of the surface.
- virtual gfx::Size GetSize() = 0;
- // Get the underlying platform specific surface "handle".
- virtual void* GetHandle() = 0;
- // Android SurfaceControl specific, notifies that we should not detach child
- // surface controls during destruction.
- virtual void PreserveChildSurfaceControls() {}
- // Returns whether or not the surface supports SwapBuffersWithBounds
- virtual bool SupportsSwapBuffersWithBounds();
- // Returns whether or not the surface supports PostSubBuffer.
- virtual bool SupportsPostSubBuffer();
- // Returns whether or not the surface supports CommitOverlayPlanes.
- virtual bool SupportsCommitOverlayPlanes();
- // Returns whether SwapBuffersAsync() is supported.
- virtual bool SupportsAsyncSwap();
- // Returns the internal frame buffer object name if the surface is backed by
- // FBO. Otherwise returns 0.
- virtual unsigned int GetBackingFramebufferObject();
- // The SwapCompletionCallback is used to receive notification about the
- // completion of the swap operation from |SwapBuffersAsync|,
- // |PostSubBufferAsync|, |CommitOverlayPlanesAsync|, etc. If a null gpu fence
- // is returned, then the swap is guaranteed to have already completed. If a
- // non-null gpu fence is returned, then the swap operation may still be in
- // progress when this callback is invoked, and the signaling of the gpu fence
- // will mark the completion of the swap operation.
- using SwapCompletionCallback =
- base::OnceCallback<void(gfx::SwapCompletionResult)>;
- // Swaps front and back buffers. This has no effect for off-screen
- // contexts. On some platforms, we want to send SwapBufferAck only after the
- // surface is displayed on screen. The callback can be used to delay sending
- // SwapBufferAck till that data is available. The callback should be run on
- // the calling thread (i.e. same thread SwapBuffersAsync is called)
- virtual void SwapBuffersAsync(SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback);
- // Swap buffers with content bounds. If it returns SWAP_FAILED, it is possible
- // that the context is no longer current.
- virtual gfx::SwapResult SwapBuffersWithBounds(
- const std::vector<gfx::Rect>& rects,
- PresentationCallback callback);
- // Copy part of the backbuffer to the frontbuffer. If it returns SWAP_FAILED,
- // it is possible that the context is no longer current.
- virtual gfx::SwapResult PostSubBuffer(int x,
- int y,
- int width,
- int height,
- PresentationCallback callback);
- // Copy part of the backbuffer to the frontbuffer. On some platforms, we want
- // to send SwapBufferAck only after the surface is displayed on screen. The
- // callback can be used to delay sending SwapBufferAck till that data is
- // available. The callback should be run on the calling thread (i.e. same
- // thread PostSubBufferAsync is called)
- virtual void PostSubBufferAsync(int x,
- int y,
- int width,
- int height,
- SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback);
- // Show overlay planes but don't swap the front and back buffers. This acts
- // like SwapBuffers from the point of view of the client, but is cheaper when
- // overlays account for all the damage. If it returns SWAP_FAILED,
- // it is possible that the context is no longer current.
- virtual gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback);
- // Show overlay planes but don't swap the front and back buffers. On some
- // platforms, we want to send SwapBufferAck only after the overlays are
- // displayed on screen. The callback can be used to delay sending
- // SwapBufferAck till that data is available. The callback should be run on
- // the calling thread (i.e. same thread CommitOverlayPlanesAsync is called).
- virtual void CommitOverlayPlanesAsync(
- SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback);
- // Called after a context is made current with this surface. Returns false
- // on error.
- virtual bool OnMakeCurrent(GLContext* context);
- // Used for explicit buffer management.
- virtual bool SetBackbufferAllocation(bool allocated);
- virtual void SetFrontbufferAllocation(bool allocated);
- // Get a handle used to share the surface with another process. Returns null
- // if this is not possible.
- virtual void* GetShareHandle();
- // Get the platform specific display on which this surface resides, if
- // available.
- virtual GLDisplay* GetGLDisplay();
- // Get the platfrom specific configuration for this surface, if available.
- virtual void* GetConfig();
- // Get the GL pixel format of the surface. Must be implemented in a
- // subclass, though it's ok to just "return GLSurfaceFormat()" if
- // the default is appropriate.
- virtual GLSurfaceFormat GetFormat() = 0;
- // Get access to a helper providing time of recent refresh and period
- // of screen refresh. If unavailable, returns NULL.
- virtual gfx::VSyncProvider* GetVSyncProvider();
- // Set vsync to enabled or disabled. If supported, vsync is enabled by
- // default. Does nothing if vsync cannot be changed.
- virtual void SetVSyncEnabled(bool enabled);
- // Schedule an overlay plane to be shown at swap time, or on the next
- // CommitOverlayPlanes call.
- // |image| to be presented by the overlay.
- // |bounds_rect| specify where it is supposed to be on the screen in pixels.
- // |overlay_plane_data| specifies overlay data such as opacity, z_order, size,
- // etc.
- virtual bool ScheduleOverlayPlane(
- GLImage* image,
- std::unique_ptr<gfx::GpuFence> gpu_fence,
- const gfx::OverlayPlaneData& overlay_plane_data);
- // Schedule a CALayer to be shown at swap time.
- // All arguments correspond to their CALayer properties.
- virtual bool ScheduleCALayer(const ui::CARendererLayerParams& params);
- virtual bool ScheduleDCLayer(
- std::unique_ptr<ui::DCRendererLayerParams> params);
- // Enables or disables DC layers, returning success. If failed, it is possible
- // that the context is no longer current.
- virtual bool SetEnableDCLayers(bool enable);
- virtual bool IsSurfaceless() const;
- // Returns true if this surface permits scheduling an isothetic sub-rectangle
- // (i.e. viewport) of its contents for display.
- virtual bool SupportsViewporter() const;
- virtual gfx::SurfaceOrigin GetOrigin() const;
- // Returns true if SwapBuffers or PostSubBuffers causes a flip, such that
- // the next buffer may be 2 frames old.
- virtual bool BuffersFlipped() const;
- virtual bool SupportsDCLayers() const;
- virtual bool SupportsProtectedVideo() const;
- // Returns true if we are allowed to adopt a size different from the
- // platform's proposed surface size.
- virtual bool SupportsOverridePlatformSize() const;
- // Set the rectangle that will be drawn into on the surface, returning
- // success. If failed, it is possible that the context is no longer current.
- virtual bool SetDrawRectangle(const gfx::Rect& rect);
- // This is the amount by which the scissor and viewport rectangles should be
- // offset.
- virtual gfx::Vector2d GetDrawOffset() const;
- // Tells the surface to rely on implicit sync when swapping buffers.
- virtual void SetRelyOnImplicitSync();
- // Tells the surface to perform a glFlush() before swapping buffers.
- virtual void SetForceGlFlushOnSwapBuffers();
- // Support for eglGetFrameTimestamps.
- virtual bool SupportsSwapTimestamps() const;
- virtual void SetEnableSwapTimestamps();
- virtual bool SupportsPlaneGpuFences() const;
- // Returns the number of buffers the surface uses in the swap chain. For
- // example, most surfaces are double-buffered, so this would return 2. For
- // triple-buffered surfaces this would return 3, etc.
- virtual int GetBufferCount() const;
- // Return the interface used for querying EGL timestamps.
- virtual EGLTimestampClient* GetEGLTimestampClient();
- virtual bool SupportsGpuVSync() const;
- virtual void SetGpuVSyncEnabled(bool enabled);
- virtual void SetDisplayTransform(gfx::OverlayTransform transform) {}
- virtual void SetFrameRate(float frame_rate) {}
- virtual void SetChoreographerVsyncIdForNextFrame(
- absl::optional<int64_t> choreographer_vsync_id) {}
- 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);
- // This should be called at most once at GPU process startup time.
- static void SetForcedGpuPreference(GpuPreference gpu_preference);
- // If a gpu preference is forced (by GPU driver bug workaround, etc), return
- // it. Otherwise, return the original input preference.
- static GpuPreference AdjustGpuPreference(GpuPreference gpu_preference);
- virtual void SetCALayerErrorCode(gfx::CALayerResult ca_layer_error_code) {}
- protected:
- virtual ~GLSurface();
- static GpuPreference forced_gpu_preference_;
- private:
- static void ClearCurrent();
- friend class base::RefCounted<GLSurface>;
- friend class GLContext;
- };
- // Implementation of GLSurface that forwards all calls through to another
- // GLSurface.
- 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 PrepareToDestroy(bool have_context) 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) override;
- void SwapBuffersAsync(SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback) override;
- gfx::SwapResult SwapBuffersWithBounds(const std::vector<gfx::Rect>& rects,
- PresentationCallback callback) override;
- gfx::SwapResult PostSubBuffer(int x,
- int y,
- int width,
- int height,
- PresentationCallback callback) override;
- void PostSubBufferAsync(int x,
- int y,
- int width,
- int height,
- SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback) override;
- gfx::SwapResult CommitOverlayPlanes(PresentationCallback callback) override;
- void CommitOverlayPlanesAsync(
- SwapCompletionCallback completion_callback,
- PresentationCallback presentation_callback) override;
- bool SupportsSwapBuffersWithBounds() override;
- bool SupportsPostSubBuffer() override;
- bool SupportsCommitOverlayPlanes() override;
- bool SupportsAsyncSwap() override;
- gfx::Size GetSize() override;
- void* GetHandle() override;
- void PreserveChildSurfaceControls() 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 ScheduleOverlayPlane(
- GLImage* image,
- std::unique_ptr<gfx::GpuFence> gpu_fence,
- const gfx::OverlayPlaneData& overlay_plane_data) override;
- bool ScheduleDCLayer(
- std::unique_ptr<ui::DCRendererLayerParams> params) override;
- bool SetEnableDCLayers(bool enable) override;
- bool IsSurfaceless() const override;
- bool SupportsViewporter() 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;
- void SetRelyOnImplicitSync() override;
- void SetForceGlFlushOnSwapBuffers() 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 SetDisplayTransform(gfx::OverlayTransform transform) override;
- void SetFrameRate(float frame_rate) override;
- void SetChoreographerVsyncIdForNextFrame(
- absl::optional<int64_t> choreographer_vsync_id) 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_;
- };
- // Wraps GLSurface in scoped_refptr and tries to initializes it. Returns a
- // scoped_refptr containing the initialized GLSurface or nullptr if
- // initialization fails.
- GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurface(
- scoped_refptr<GLSurface> surface);
- GL_EXPORT scoped_refptr<GLSurface> InitializeGLSurfaceWithFormat(
- scoped_refptr<GLSurface> surface, GLSurfaceFormat format);
- } // namespace gl
- #endif // UI_GL_GL_SURFACE_H_
|