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 "mojo/examples/sample_app/gles2_client_impl.h"
9 #include "mojo/public/bindings/allocation_scope.h"
10 #include "mojo/public/bindings/remote_ptr.h"
11 #include "mojo/public/environment/environment.h"
12 #include "mojo/public/gles2/gles2_cpp.h"
13 #include "mojo/public/shell/application.h"
14 #include "mojo/public/shell/shell.mojom.h"
15 #include "mojo/public/system/core.h"
16 #include "mojo/public/system/macros.h"
17 #include "mojo/public/utility/run_loop.h"
18 #include "mojo/services/native_viewport/native_viewport.mojom.h"
24 #define SAMPLE_APP_EXPORT __declspec(dllexport)
27 #define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
33 class SampleApp
: public Application
, public mojo::NativeViewportClient
{
35 explicit SampleApp(MojoHandle shell_handle
) : Application(shell_handle
) {
36 InterfacePipe
<NativeViewport
, AnyInterface
> viewport_pipe
;
37 mojo::AllocationScope scope
;
38 shell()->Connect("mojo:mojo_native_viewport_service",
39 viewport_pipe
.handle_to_peer
.Pass());
40 viewport_
.reset(viewport_pipe
.handle_to_self
.Pass(), this);
45 rect
.set_position(point
.Finish());
49 rect
.set_size(size
.Finish());
50 viewport_
->Create(rect
.Finish());
53 MessagePipe gles2_pipe
;
54 viewport_
->CreateGLES2Context(gles2_pipe
.handle1
.Pass());
55 gles2_client_
.reset(new GLES2ClientImpl(gles2_pipe
.handle0
.Pass()));
58 virtual ~SampleApp() {
59 // TODO(darin): Fix shutdown so we don't need to leak this.
60 MOJO_ALLOW_UNUSED GLES2ClientImpl
* leaked
= gles2_client_
.release();
63 virtual void OnCreated() MOJO_OVERRIDE
{
66 virtual void OnDestroyed() MOJO_OVERRIDE
{
67 RunLoop::current()->Quit();
70 virtual void OnBoundsChanged(const Rect
& bounds
) MOJO_OVERRIDE
{
71 gles2_client_
->SetSize(bounds
.size());
74 virtual void OnEvent(const Event
& event
) MOJO_OVERRIDE
{
75 if (!event
.location().is_null()) {
76 gles2_client_
->HandleInputEvent(event
);
77 viewport_
->AckEvent(event
);
82 scoped_ptr
<GLES2ClientImpl
> gles2_client_
;
83 RemotePtr
<NativeViewport
> viewport_
;
86 } // namespace examples
89 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL
MojoMain(
90 MojoHandle shell_handle
) {
91 mojo::Environment env
;
93 mojo::GLES2Initializer gles2
;
95 mojo::examples::SampleApp
app(shell_handle
);
97 return MOJO_RESULT_OK
;