Home | History | Annotate | Download | only in sample_app
      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/c/system/main.h"
     10 #include "mojo/public/cpp/application/application_connection.h"
     11 #include "mojo/public/cpp/application/application_delegate.h"
     12 #include "mojo/public/cpp/application/application_impl.h"
     13 #include "mojo/public/cpp/application/application_runner.h"
     14 #include "mojo/public/cpp/system/core.h"
     15 #include "mojo/public/cpp/system/macros.h"
     16 #include "mojo/public/cpp/utility/run_loop.h"
     17 #include "mojo/services/public/interfaces/gpu/gpu.mojom.h"
     18 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
     19 
     20 namespace examples {
     21 
     22 class SampleApp : public mojo::ApplicationDelegate,
     23                   public mojo::NativeViewportClient {
     24  public:
     25   SampleApp() {}
     26 
     27   virtual ~SampleApp() {
     28     // TODO(darin): Fix shutdown so we don't need to leak this.
     29     MOJO_ALLOW_UNUSED GLES2ClientImpl* leaked = gles2_client_.release();
     30   }
     31 
     32   virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE {
     33     app->ConnectToService("mojo:mojo_native_viewport_service", &viewport_);
     34     viewport_.set_client(this);
     35 
     36     // TODO(jamesr): Should be mojo:mojo_gpu_service
     37     app->ConnectToService("mojo:mojo_native_viewport_service", &gpu_service_);
     38 
     39     mojo::SizePtr size(mojo::Size::New());
     40     size->width = 800;
     41     size->height = 600;
     42     viewport_->Create(size.Pass());
     43     viewport_->Show();
     44   }
     45 
     46   virtual void OnCreated(uint64_t native_viewport_id) MOJO_OVERRIDE {
     47     mojo::SizePtr size = mojo::Size::New();
     48     size->width = 800;
     49     size->height = 600;
     50     mojo::CommandBufferPtr command_buffer;
     51     // TODO(jamesr): Output to a surface instead.
     52     gpu_service_->CreateOnscreenGLES2Context(
     53         native_viewport_id, size.Pass(), Get(&command_buffer));
     54     gles2_client_.reset(new GLES2ClientImpl(command_buffer.Pass()));
     55   }
     56 
     57   virtual void OnDestroyed() MOJO_OVERRIDE { mojo::RunLoop::current()->Quit(); }
     58 
     59   virtual void OnBoundsChanged(mojo::SizePtr bounds) MOJO_OVERRIDE {
     60     assert(bounds);
     61     if (gles2_client_)
     62       gles2_client_->SetSize(*bounds);
     63   }
     64 
     65   virtual void OnEvent(mojo::EventPtr event,
     66                        const mojo::Callback<void()>& callback) MOJO_OVERRIDE {
     67     assert(event);
     68     if (event->location_data && event->location_data->in_view_location)
     69       gles2_client_->HandleInputEvent(*event);
     70     callback.Run();
     71   }
     72 
     73  private:
     74   scoped_ptr<GLES2ClientImpl> gles2_client_;
     75   mojo::NativeViewportPtr viewport_;
     76   mojo::GpuPtr gpu_service_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(SampleApp);
     79 };
     80 
     81 }  // namespace examples
     82 
     83 MojoResult MojoMain(MojoHandle shell_handle) {
     84   mojo::ApplicationRunner runner(new examples::SampleApp);
     85   return runner.Run(shell_handle);
     86 }
     87