// Copyright 2014 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 COMPONENTS_VIZ_SERVICE_DISPLAY_DISPLAY_H_ #define COMPONENTS_VIZ_SERVICE_DISPLAY_DISPLAY_H_ #include #include #include "base/macros.h" #include "base/observer_list.h" #include "cc/resources/display_resource_provider.h" #include "components/viz/common/frame_sinks/begin_frame_source.h" #include "components/viz/common/gpu/context_lost_observer.h" #include "components/viz/common/resources/returned_resource.h" #include "components/viz/common/surfaces/frame_sink_id.h" #include "components/viz/common/surfaces/surface_id.h" #include "components/viz/service/display/display_scheduler.h" #include "components/viz/service/display/output_surface_client.h" #include "components/viz/service/display/surface_aggregator.h" #include "components/viz/service/surfaces/latest_local_surface_id_lookup_delegate.h" #include "components/viz/service/surfaces/surface_manager.h" #include "components/viz/service/viz_service_export.h" #include "gpu/command_buffer/common/texture_in_use_response.h" #include "ui/gfx/color_space.h" #include "ui/latency/latency_info.h" namespace cc { class DisplayResourceProvider; class RendererSettings; } // namespace cc namespace gfx { class Size; } namespace viz { class DirectRenderer; class DisplayClient; class OutputSurface; class SharedBitmapManager; class SoftwareRenderer; class VIZ_SERVICE_EXPORT DisplayObserver { public: virtual ~DisplayObserver() {} virtual void OnDisplayDidFinishFrame(const BeginFrameAck& ack) = 0; }; // A Display produces a surface that can be used to draw to a physical display // (OutputSurface). The client is responsible for creating and sizing the // surface IDs used to draw into the display and deciding when to draw. class VIZ_SERVICE_EXPORT Display : public DisplaySchedulerClient, public OutputSurfaceClient, public ContextLostObserver, public LatestLocalSurfaceIdLookupDelegate { public: // The |begin_frame_source| and |scheduler| may be null (together). In that // case, DrawAndSwap must be called externally when needed. // The |current_task_runner| may be null if the Display is on a thread without // a MessageLoop. Display(SharedBitmapManager* bitmap_manager, const RendererSettings& settings, const FrameSinkId& frame_sink_id, std::unique_ptr output_surface, std::unique_ptr scheduler, scoped_refptr current_task_runner); ~Display() override; void Initialize(DisplayClient* client, SurfaceManager* surface_manager); void AddObserver(DisplayObserver* observer); void RemoveObserver(DisplayObserver* observer); // device_scale_factor is used to communicate to the external window system // what scale this was rendered at. void SetLocalSurfaceId(const LocalSurfaceId& id, float device_scale_factor); void SetVisible(bool visible); void Resize(const gfx::Size& new_size); // Sets the color matrix that will be used to transform the output of this // display. This is only supported for GPU compositing. void SetColorMatrix(const SkMatrix44& matrix); void SetColorSpace(const gfx::ColorSpace& blending_color_space, const gfx::ColorSpace& device_color_space); void SetOutputIsSecure(bool secure); const SurfaceId& CurrentSurfaceId(); // DisplaySchedulerClient implementation. bool DrawAndSwap() override; bool SurfaceHasUndrawnFrame(const SurfaceId& surface_id) const override; bool SurfaceDamaged(const SurfaceId& surface_id, const BeginFrameAck& ack) override; void SurfaceDiscarded(const SurfaceId& surface_id) override; void DidFinishFrame(const BeginFrameAck& ack) override; // OutputSurfaceClient implementation. void SetNeedsRedrawRect(const gfx::Rect& damage_rect) override; void DidReceiveSwapBuffersAck(uint64_t swap_id) override; void DidReceiveTextureInUseResponses( const gpu::TextureInUseResponses& responses) override; void DidReceiveCALayerParams( const gfx::CALayerParams& ca_layer_params) override; void DidReceivePresentationFeedback( uint64_t swap_id, const gfx::PresentationFeedback& feedback) override; // LatestLocalSurfaceIdLookupDelegate implementation. LocalSurfaceId GetSurfaceAtAggregation( const FrameSinkId& frame_sink_id) const override; bool has_scheduler() const { return !!scheduler_; } DirectRenderer* renderer_for_testing() const { return renderer_.get(); } void ForceImmediateDrawAndSwapIfPossible(); void SetNeedsOneBeginFrame(); void RemoveOverdrawQuads(CompositorFrame* frame); private: void InitializeRenderer(); void UpdateRootSurfaceResourcesLocked(); // ContextLostObserver implementation. void OnContextLost() override; SharedBitmapManager* const bitmap_manager_; const RendererSettings settings_; DisplayClient* client_ = nullptr; base::ObserverList observers_; SurfaceManager* surface_manager_ = nullptr; const FrameSinkId frame_sink_id_; SurfaceId current_surface_id_; gfx::Size current_surface_size_; float device_scale_factor_ = 1.f; gfx::ColorSpace blending_color_space_ = gfx::ColorSpace::CreateSRGB(); gfx::ColorSpace device_color_space_ = gfx::ColorSpace::CreateSRGB(); bool visible_ = false; bool swapped_since_resize_ = false; bool output_is_secure_ = false; std::unique_ptr output_surface_; std::unique_ptr scheduler_; std::unique_ptr resource_provider_; std::unique_ptr aggregator_; // This may be null if the Display is on a thread without a MessageLoop. scoped_refptr current_task_runner_; std::unique_ptr renderer_; SoftwareRenderer* software_renderer_ = nullptr; std::vector stored_latency_info_; using PresentedCallbacks = std::vector; PresentedCallbacks presented_callbacks_; PresentedCallbacks active_presented_callbacks_; // TODO(penghuang): Remove it when we can get accurate presentation time from // GPU for every SwapBuffers. https://crbug.com/776877 std::vector previous_presented_callbacks_; private: DISALLOW_COPY_AND_ASSIGN(Display); }; } // namespace viz #endif // COMPONENTS_VIZ_SERVICE_DISPLAY_DISPLAY_H_