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 bool drawn = false; // set when DrawFrame is called |
mikejurka
2016/05/20 01:49:23
this works. but it wasn't my intention to force yo
jeffbrown
2016/05/20 19:31:35
It's fine. Technically this is better anyhow sinc
| |
45 int64_t draw_time = 0; // 0 if not drawn | |
46 int64_t wait_time = 0; // 0 if not drawn | |
53 | 47 |
54 private: | 48 private: |
55 void PostSubmit(); | 49 friend class base::RefCountedThreadSafe<FrameData>; |
56 | 50 |
57 // Called on rasterizer thread. | 51 ~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 | 52 |
64 // Called on rasterizer thread. | 53 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 }; | 54 }; |
82 | 55 |
83 scoped_refptr<VsyncScheduler> scheduler_; | 56 // |GpuRasterizer::Callbacks|: |
84 scoped_ptr<RasterizerDelegate> rasterizer_delegate_; // can't use unique_ptr | 57 void OnRasterizerReady(int64_t vsync_timebase, |
85 // here due to | 58 int64_t vsync_interval) override; |
86 // base::Bind (sadness) | 59 void OnRasterizerSuspended() override; |
60 void OnRasterizerFinishedDraw(bool presented) override; | |
61 void OnRasterizerError() override; | |
62 | |
63 void ScheduleDrawLocked(); | |
64 void OnDraw(); | |
65 | |
66 void InitializeRasterizer( | |
67 mojo::InterfaceHandle<mojo::ContextProvider> context_provider); | |
68 void DestroyRasterizer(); | |
69 void PostErrorCallback(); | |
70 | |
71 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | |
72 scoped_refptr<VsyncScheduler> vsync_scheduler_; | |
73 base::Closure error_callback_; | |
74 | |
75 // Maximum number of frames to hold in the drawing pipeline. | |
76 // Any more than this and we start dropping them. | |
77 uint32_t pipeline_depth_; | |
78 | |
79 // The rasterizer itself runs on its own thread. | |
80 std::unique_ptr<base::Thread> rasterizer_thread_; | |
81 scoped_refptr<base::SingleThreadTaskRunner> rasterizer_task_runner_; | |
82 base::WaitableEvent rasterizer_initialized_; | |
83 std::unique_ptr<GpuRasterizer> rasterizer_; | |
84 | |
85 // Holds state shared between the compositor and rasterizer threads. | |
86 struct { | |
87 std::mutex mutex; // guards all shared state | |
88 | |
89 // The most recently submitted frame. | |
90 // Only null until the first frame has been submitted. | |
91 scoped_refptr<FrameData> current_frame_data; | |
92 | |
93 // Frames drawn and awaiting completion by the rasterizer. | |
94 std::queue<scoped_refptr<FrameData>> drawn_frames_awaiting_finish; | |
95 | |
96 // Set to true when the rasterizer is ready to draw. | |
97 bool rasterizer_ready = false; | |
98 | |
99 // Set to true when a request to draw has been scheduled. | |
100 bool draw_scheduled = false; | |
101 } shared_state_; | |
87 | 102 |
88 DISALLOW_COPY_AND_ASSIGN(GpuOutput); | 103 DISALLOW_COPY_AND_ASSIGN(GpuOutput); |
89 }; | 104 }; |
90 | 105 |
91 } // namespace compositor | 106 } // namespace compositor |
92 | 107 |
93 #endif // SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ | 108 #endif // SERVICES_GFX_COMPOSITOR_BACKEND_GPU_OUTPUT_H_ |
OLD | NEW |