1 // Copyright 2013 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.
8 #include "base/message_loop/message_loop.h"
9 #include "mojo/examples/compositor_app/compositor_host.h"
10 #include "mojo/public/bindings/allocation_scope.h"
11 #include "mojo/public/bindings/remote_ptr.h"
12 #include "mojo/public/gles2/gles2_cpp.h"
13 #include "mojo/public/shell/application.h"
14 #include "mojo/public/system/core.h"
15 #include "mojo/public/system/macros.h"
16 #include "mojo/services/native_viewport/geometry_conversions.h"
17 #include "mojom/native_viewport.h"
18 #include "mojom/shell.h"
19 #include "ui/gfx/rect.h"
25 #define SAMPLE_APP_EXPORT __declspec(dllexport)
28 #define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
34 class SampleApp : public Application, public NativeViewportClient {
36 explicit SampleApp(MojoHandle shell_handle) : Application(shell_handle) {
37 InterfacePipe<NativeViewport, AnyInterface> viewport_pipe;
39 AllocationScope scope;
40 shell()->Connect("mojo:mojo_native_viewport_service",
41 viewport_pipe.handle_to_peer.Pass());
43 viewport_.reset(viewport_pipe.handle_to_self.Pass(), this);
44 viewport_->Create(gfx::Rect(10, 10, 800, 600));
46 ScopedMessagePipeHandle gles2_handle;
47 ScopedMessagePipeHandle gles2_client_handle;
48 CreateMessagePipe(&gles2_handle, &gles2_client_handle);
50 viewport_->CreateGLES2Context(gles2_client_handle.Pass());
51 host_.reset(new CompositorHost(gles2_handle.Pass()));
54 virtual void OnCreated() MOJO_OVERRIDE {
57 virtual void OnDestroyed() MOJO_OVERRIDE {
58 base::MessageLoop::current()->Quit();
61 virtual void OnBoundsChanged(const Rect& bounds) MOJO_OVERRIDE {
62 host_->SetSize(bounds.size());
65 virtual void OnEvent(const Event& event) MOJO_OVERRIDE {
66 if (!event.location().is_null()) {
67 viewport_->AckEvent(event);
72 RemotePtr<NativeViewport> viewport_;
73 scoped_ptr<CompositorHost> host_;
76 } // namespace examples
79 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
80 MojoHandle shell_handle) {
81 base::MessageLoop loop;
82 mojo::GLES2Initializer gles2;
84 mojo::examples::SampleApp app(shell_handle);
86 return MOJO_RESULT_OK;