• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include <stdio.h>
6 #include <string>
7 
8 #include "mojo/examples/sample_app/gles2_client_impl.h"
9 #include "mojo/public/cpp/application/application.h"
10 #include "mojo/public/cpp/gles2/gles2.h"
11 #include "mojo/public/cpp/system/core.h"
12 #include "mojo/public/cpp/system/macros.h"
13 #include "mojo/public/cpp/utility/run_loop.h"
14 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
15 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
16 
17 namespace mojo {
18 namespace examples {
19 
20 class SampleApp : public Application, public NativeViewportClient {
21  public:
SampleApp()22   SampleApp() {}
23 
~SampleApp()24   virtual ~SampleApp() {
25     // TODO(darin): Fix shutdown so we don't need to leak this.
26     MOJO_ALLOW_UNUSED GLES2ClientImpl* leaked = gles2_client_.release();
27   }
28 
Initialize()29   virtual void Initialize() MOJO_OVERRIDE {
30     ConnectTo("mojo:mojo_native_viewport_service", &viewport_);
31     viewport_.set_client(this);
32 
33     RectPtr rect(Rect::New());
34     rect->x = 10;
35     rect->y = 10;
36     rect->width = 800;
37     rect->height = 600;
38     viewport_->Create(rect.Pass());
39     viewport_->Show();
40 
41     CommandBufferPtr command_buffer;
42     viewport_->CreateGLES2Context(Get(&command_buffer));
43     gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass()));
44   }
45 
OnCreated()46   virtual void OnCreated() MOJO_OVERRIDE {
47   }
48 
OnDestroyed()49   virtual void OnDestroyed() MOJO_OVERRIDE {
50     RunLoop::current()->Quit();
51   }
52 
OnBoundsChanged(RectPtr bounds)53   virtual void OnBoundsChanged(RectPtr bounds) MOJO_OVERRIDE {
54     assert(bounds);
55     SizePtr size(Size::New());
56     size->width = bounds->width;
57     size->height = bounds->height;
58     gles2_client_->SetSize(*size);
59   }
60 
OnEvent(EventPtr event,const Callback<void ()> & callback)61   virtual void OnEvent(EventPtr event,
62                        const Callback<void()>& callback) MOJO_OVERRIDE {
63     assert(event);
64     if (event->location)
65       gles2_client_->HandleInputEvent(*event);
66     callback.Run();
67   }
68 
69  private:
70   mojo::GLES2Initializer gles2;
71   scoped_ptr<GLES2ClientImpl> gles2_client_;
72   NativeViewportPtr viewport_;
73 
74   DISALLOW_COPY_AND_ASSIGN(SampleApp);
75 };
76 
77 }  // namespace examples
78 
79 // static
Create()80 Application* Application::Create() {
81   return new examples::SampleApp();
82 }
83 
84 }  // namespace mojo
85