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

Unified 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: Created 4 years, 12 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 side-by-side diff with in-line comments
Download patch
Index: examples/ui/noodles/noodles_view.h
diff --git a/examples/ui/noodles/noodles_view.h b/examples/ui/noodles/noodles_view.h
new file mode 100644
index 0000000000000000000000000000000000000000..9a60128d08abb5b30ab4e28b5595df6e4dfc5086
--- /dev/null
+++ b/examples/ui/noodles/noodles_view.h
@@ -0,0 +1,109 @@
+// Copyright 2015 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 EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_
+#define EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_
+
+#include <memory>
+#include <mutex>
+
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/threading/thread.h"
+#include "mojo/ui/base_view.h"
+#include "mojo/ui/choreographer.h"
+#include "skia/ext/refptr.h"
+
+class SkPicture;
+
+namespace examples {
+
+class Frame;
+class Rasterizer;
+
+class NoodlesView : public mojo::ui::BaseView,
+ public mojo::ui::ChoreographerDelegate {
+ public:
+ NoodlesView(
+ mojo::ApplicationImpl* app_impl,
+ const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback);
+
+ ~NoodlesView() override;
+
+ private:
+ // Frame queue, held by a std::shared_ptr.
+ // This object acts as a shared fifo between both threads.
+ class FrameQueue {
+ public:
+ FrameQueue();
+ ~FrameQueue();
+
+ // Puts a pending frame into the queue, drops existing frames if needed.
+ // Returns true if the queue was previously empty.
+ bool PutFrame(std::unique_ptr<Frame> frame);
+
+ // Takes a pending frame from the queue.
+ std::unique_ptr<Frame> TakeFrame();
+
+ private:
+ std::mutex mutex_;
+ std::unique_ptr<Frame> next_frame_; // guarded by |mutex_|
+
+ DISALLOW_COPY_AND_ASSIGN(FrameQueue);
+ };
+
+ // Wrapper around state which is only accessible by the rasterizer thread.
+ class RasterizerDelegate {
+ public:
+ explicit RasterizerDelegate(const std::shared_ptr<FrameQueue>& frame_queue);
+ ~RasterizerDelegate();
+
+ void CreateRasterizer(
+ mojo::InterfacePtrInfo<mojo::ApplicationConnector> connector_info,
+ mojo::InterfacePtrInfo<mojo::gfx::composition::Scene> scene_info);
+
+ void PublishNextFrame();
+
+ private:
+ std::shared_ptr<FrameQueue> frame_queue_;
+ std::unique_ptr<Rasterizer> rasterizer_;
+
+ DISALLOW_COPY_AND_ASSIGN(RasterizerDelegate);
+ };
+
+ // |View|:
+ void OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params,
+ mojo::Array<uint32_t> children_needing_layout,
+ const OnLayoutCallback& callback) override;
+
+ // |ChoreographerDelegate|:
+ void OnDraw(const mojo::gfx::composition::FrameInfo& frame_info,
+ const base::TimeDelta& time_delta) override;
+
+ void UpdateFrame();
+ skia::RefPtr<SkPicture> CreatePicture();
+
+ mojo::ui::Choreographer choreographer_;
+
+ std::shared_ptr<FrameQueue> frame_queue_;
+
+ scoped_ptr<RasterizerDelegate> rasterizer_delegate_; // can't use unique_ptr
+ // here due to
+ // base::Bind (sadness)
+ std::unique_ptr<base::Thread> rasterizer_thread_;
+ scoped_refptr<base::SingleThreadTaskRunner> rasterizer_task_runner_;
+
+ mojo::Size size_;
+ double alpha_ = 0.0;
+ int wx_ = 2;
+ int wy_ = 3;
+
+ DISALLOW_COPY_AND_ASSIGN(NoodlesView);
+};
+
+} // namespace examples
+
+#endif // EXAMPLES_UI_NOODLES_NOODLES_VIEW_H_

Powered by Google App Engine
This is Rietveld 408576698