OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ | 5 #ifndef SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ |
6 #define SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ | 6 #define SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <mutex> | 9 #include <mutex> |
10 #include <queue> | 10 #include <queue> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "mojo/services/gpu/interfaces/context_provider.mojom.h" | 18 #include "mojo/services/gpu/interfaces/context_provider.mojom.h" |
19 #include "services/gfx/compositor/backend/gpu_rasterizer.h" | |
19 #include "services/gfx/compositor/backend/output.h" | 20 #include "services/gfx/compositor/backend/output.h" |
20 #include "services/gfx/compositor/backend/vsync_scheduler.h" | 21 #include "services/gfx/compositor/backend/vsync_scheduler.h" |
21 | 22 |
22 namespace compositor { | 23 namespace compositor { |
23 | 24 |
24 class GpuRasterizer; | |
25 | |
26 // Renderer backed by a ContextProvider. | 25 // Renderer backed by a ContextProvider. |
27 class GpuOutput : public Output { | 26 class GpuOutput : public Output, private GpuRasterizer::Callbacks { |
28 public: | 27 public: |
29 GpuOutput(mojo::InterfaceHandle<mojo::ContextProvider> context_provider, | 28 GpuOutput(mojo::InterfaceHandle<mojo::ContextProvider> context_provider, |
30 const SchedulerCallbacks& scheduler_callbacks, | 29 const SchedulerCallbacks& scheduler_callbacks, |
31 const base::Closure& error_callback); | 30 const base::Closure& error_callback); |
32 ~GpuOutput() override; | 31 ~GpuOutput() override; |
33 | 32 |
34 Scheduler* GetScheduler() override; | 33 Scheduler* GetScheduler() override; |
35 void SubmitFrame(const scoped_refptr<RenderFrame>& frame) override; | 34 void SubmitFrame(const scoped_refptr<RenderFrame>& frame) override; |
36 | 35 |
37 private: | 36 private: |
38 // Wrapper around state which is made available to the rasterizer thread. | 37 struct FrameData : public base::RefCountedThreadSafe<FrameData> { |
39 class RasterizerDelegate { | 38 FrameData(const scoped_refptr<RenderFrame>& frame, int64_t submit_time); |
40 public: | |
41 RasterizerDelegate(); | |
42 ~RasterizerDelegate(); | |
43 | 39 |
44 void PostInitialize( | 40 void Recycle(); |
45 mojo::InterfaceHandle<mojo::ContextProvider> context_provider, | |
46 const scoped_refptr<VsyncScheduler>& scheduler, | |
47 const scoped_refptr<base::TaskRunner>& task_runner, | |
48 const base::Closure& error_callback); | |
49 | 41 |
50 void PostDestroy(scoped_ptr<RasterizerDelegate> self); | 42 const scoped_refptr<RenderFrame> frame; |
51 | 43 const int64_t submit_time; |
52 void PostFrame(const scoped_refptr<RenderFrame>& frame); | 44 int64_t draw_time; |
45 int64_t wait_time; | |
abarth
2016/05/19 00:49:56
Should these be base::TimeTicks?
jeffbrown
2016/05/20 01:09:47
Perhaps. Technically they are MojoTimeTicks but i
| |
53 | 46 |
54 private: | 47 private: |
55 void PostSubmit(); | 48 friend class base::RefCountedThreadSafe<FrameData>; |
56 | 49 |
57 // Called on rasterizer thread. | 50 ~FrameData(); |
58 void InitializeTask( | |
59 mojo::InterfaceHandle<mojo::ContextProvider> context_provider, | |
60 const scoped_refptr<VsyncScheduler>& scheduler, | |
61 const scoped_refptr<base::TaskRunner>& task_runner, | |
62 const base::Closure& error_callback); | |
63 | 51 |
64 // Called on rasterizer thread. | 52 DISALLOW_COPY_AND_ASSIGN(FrameData); |
65 void SubmitTask(); | |
66 | |
67 // Called on rasterizer thread. | |
68 void OnFrameSubmitted(int64_t frame_time, | |
69 int64_t presentation_time, | |
70 int64_t submit_time, | |
71 bool presented); | |
72 | |
73 std::unique_ptr<base::Thread> thread_; | |
74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
75 std::unique_ptr<GpuRasterizer> rasterizer_; | |
76 | |
77 std::mutex mutex_; | |
78 std::queue<scoped_refptr<RenderFrame>> frames_; // guarded by |mutex_| | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(RasterizerDelegate); | |
81 }; | 53 }; |
82 | 54 |
83 scoped_refptr<VsyncScheduler> scheduler_; | 55 // |GpuRasterizer::Callbacks|: |
84 scoped_ptr<RasterizerDelegate> rasterizer_delegate_; // can't use unique_ptr | 56 void OnRasterizerReady(int64_t vsync_timebase, |
85 // here due to | 57 int64_t vsync_interval) override; |
86 // base::Bind (sadness) | 58 void OnRasterizerSuspended() override; |
59 void OnRasterizerFinishedDraw(bool presented) override; | |
60 void OnRasterizerError() override; | |
61 | |
62 void ScheduleDrawLocked(); | |
63 void OnDraw(); | |
64 | |
65 void InitializeRasterizer( | |
66 mojo::InterfaceHandle<mojo::ContextProvider> context_provider); | |
67 void DestroyRasterizer(); | |
68 void PostErrorCallback(); | |
69 | |
70 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | |
mikejurka
2016/05/19 23:26:19
should we be using scoped_refptr or std::unique_pt
jeffbrown
2016/05/20 01:09:47
A better substitute for scoped_refptr is std::shar
| |
71 scoped_refptr<VsyncScheduler> vsync_scheduler_; | |
72 base::Closure error_callback_; | |
73 | |
74 // Maximum number of frames to hold in the drawing pipeline. | |
75 // Any more than this and we start dropping them. | |
76 uint32_t pipeline_depth_; | |
77 | |
78 // The rasterizer itself runs on its own thread. | |
79 std::unique_ptr<base::Thread> rasterizer_thread_; | |
80 scoped_refptr<base::SingleThreadTaskRunner> rasterizer_task_runner_; | |
81 base::WaitableEvent rasterizer_initialized_; | |
82 std::unique_ptr<GpuRasterizer> rasterizer_; | |
83 | |
84 // Holds state shared between the compositor and rasterizer threads. | |
85 struct { | |
86 std::mutex mutex; // guards all shared state | |
87 | |
88 // The most recently submitted frame. | |
89 // Only null until the first frame has been submitted. | |
90 scoped_refptr<FrameData> current_frame_data; | |
mikejurka
2016/05/19 23:26:18
If FrameData already inherits from RefCountedThrea
jeffbrown
2016/05/20 01:09:47
scoped_refptr is a smart pointer which holds a ref
| |
91 | |
92 // Frames drawn and awaiting completion by the rasterizer. | |
93 std::queue<scoped_refptr<FrameData>> drawn_frames_awaiting_finish; | |
94 | |
95 // Set to true when the rasterizer is ready to draw. | |
96 bool rasterizer_ready = false; | |
97 | |
98 // Set to true when a request to draw has been scheduled. | |
99 bool draw_scheduled = false; | |
100 } shared_state_; | |
87 | 101 |
88 DISALLOW_COPY_AND_ASSIGN(GpuOutput); | 102 DISALLOW_COPY_AND_ASSIGN(GpuOutput); |
89 }; | 103 }; |
90 | 104 |
91 } // namespace compositor | 105 } // namespace compositor |
92 | 106 |
93 #endif // SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ | 107 #endif // SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ |
OLD | NEW |