Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: examples/ui/noodles/noodles_view.h

Issue 1558813002: Add a multi-threaded rendering example. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-15
Patch Set: address feedback Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « examples/ui/noodles/noodles_app.cc ('k') | examples/ui/noodles/noodles_view.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_
6 #define EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_
7
8 #include <memory>
9 #include <mutex>
10
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/threading/thread.h"
16 #include "mojo/ui/base_view.h"
17 #include "mojo/ui/choreographer.h"
18 #include "skia/ext/refptr.h"
19
20 class SkPicture;
21
22 namespace examples {
23
24 class Frame;
25 class Rasterizer;
26
27 class NoodlesView : public mojo::ui::BaseView,
28 public mojo::ui::ChoreographerDelegate {
29 public:
30 NoodlesView(
31 mojo::ApplicationImpl* app_impl,
32 const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback);
33
34 ~NoodlesView() override;
35
36 private:
37 // Frame queue, held by a std::shared_ptr.
38 // This object acts as a shared fifo between both threads.
39 class FrameQueue {
40 public:
41 FrameQueue();
42 ~FrameQueue();
43
44 // Puts a pending frame into the queue, drops existing frames if needed.
45 // Returns true if the queue was previously empty.
46 bool PutFrame(std::unique_ptr<Frame> frame);
47
48 // Takes a pending frame from the queue.
49 std::unique_ptr<Frame> TakeFrame();
50
51 private:
52 std::mutex mutex_;
53 std::unique_ptr<Frame> next_frame_; // guarded by |mutex_|
54
55 DISALLOW_COPY_AND_ASSIGN(FrameQueue);
56 };
57
58 // Wrapper around state which is only accessible by the rasterizer thread.
59 class RasterizerDelegate {
60 public:
61 explicit RasterizerDelegate(const std::shared_ptr<FrameQueue>& frame_queue);
62 ~RasterizerDelegate();
63
64 void CreateRasterizer(
65 mojo::InterfacePtrInfo<mojo::ApplicationConnector> connector_info,
66 mojo::InterfacePtrInfo<mojo::gfx::composition::Scene> scene_info);
67
68 void PublishNextFrame();
69
70 private:
71 std::shared_ptr<FrameQueue> frame_queue_;
72 std::unique_ptr<Rasterizer> rasterizer_;
73
74 DISALLOW_COPY_AND_ASSIGN(RasterizerDelegate);
75 };
76
77 // |BaseView|:
78 void OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params,
79 mojo::Array<uint32_t> children_needing_layout,
80 const OnLayoutCallback& callback) override;
81
82 // |ChoreographerDelegate|:
83 void OnDraw(const mojo::gfx::composition::FrameInfo& frame_info,
84 const base::TimeDelta& time_delta) override;
85
86 void UpdateFrame();
87 skia::RefPtr<SkPicture> CreatePicture();
88
89 mojo::ui::Choreographer choreographer_;
90
91 std::shared_ptr<FrameQueue> frame_queue_;
92
93 scoped_ptr<RasterizerDelegate> rasterizer_delegate_; // can't use unique_ptr
94 // here due to
95 // base::Bind (sadness)
96 std::unique_ptr<base::Thread> rasterizer_thread_;
97 scoped_refptr<base::SingleThreadTaskRunner> rasterizer_task_runner_;
98
99 mojo::Size size_;
100 double alpha_ = 0.0;
101 int wx_ = 2;
102 int wy_ = 3;
103
104 DISALLOW_COPY_AND_ASSIGN(NoodlesView);
105 };
106
107 } // namespace examples
108
109 #endif // EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_
OLDNEW
« no previous file with comments | « examples/ui/noodles/noodles_app.cc ('k') | examples/ui/noodles/noodles_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698