- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / native_viewport_controller.cc
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 "mojo/services/native_viewport/native_viewport_controller.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "gpu/command_buffer/client/gles2_interface.h"
10 #include "mojo/services/native_viewport/native_viewport.h"
11 #include "ui/events/event.h"
12
13 namespace mojo {
14 namespace services {
15
16 NativeViewportController::NativeViewportController(
17     shell::Context* context, Handle pipe)
18     : pipe_(pipe) {
19   native_viewport_ = NativeViewport::Create(context, this);
20 }
21 NativeViewportController::~NativeViewportController() {
22 }
23
24 void NativeViewportController::Close() {
25   DCHECK(native_viewport_);
26   native_viewport_->Close();
27 }
28
29 bool NativeViewportController::OnEvent(ui::Event* event) {
30   ui::LocatedEvent* located = static_cast<ui::LocatedEvent*>(event);
31   SendString(base::StringPrintf("Event @ %d,%d",
32                                 located->location().x(),
33                                 located->location().y()));
34   return false;
35 }
36
37 void NativeViewportController::OnGLContextAvailable(
38     gpu::gles2::GLES2Interface* gl) {
39   // TODO(abarth): Instead of drawing green, we want to send the context over
40   // pipe_ somehow.
41   gl->ClearColor(0, 1, 0, 0);
42   gl->Clear(GL_COLOR_BUFFER_BIT);
43   gl->SwapBuffers();
44 }
45
46 void NativeViewportController::OnGLContextLost() {
47   SendString("GL context lost");
48 }
49
50 void NativeViewportController::OnResized(const gfx::Size& size) {
51   SendString(base::StringPrintf("Sized to: %d x %d",
52                                 size.width(),
53                                 size.height()));
54 }
55
56 void NativeViewportController::OnDestroyed() {
57   base::MessageLoop::current()->Quit();
58 }
59
60 void NativeViewportController::SendString(const std::string& string) {
61   WriteMessage(pipe_, string.c_str(), string.size()+1, NULL, 0,
62                 MOJO_WRITE_MESSAGE_FLAG_NONE);
63 }
64
65 }  // namespace services
66 }  // namespace mojo