- add sources.
[platform/framework/web/crosswalk.git] / src / mojo / services / native_viewport / native_viewport_android.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_android.h"
6
7 #include <android/native_window_jni.h>
8 #include "gpu/command_buffer/client/gl_in_process_context.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "mojo/services/native_viewport/android/mojo_viewport.h"
11 #include "mojo/shell/context.h"
12
13 namespace mojo {
14 namespace services {
15
16 NativeViewportAndroid::NativeViewportAndroid(NativeViewportDelegate* delegate)
17     : delegate_(delegate),
18       window_(NULL),
19       weak_factory_(this) {
20 }
21
22 NativeViewportAndroid::~NativeViewportAndroid() {
23   if (window_)
24     ReleaseWindow();
25 }
26
27 void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) {
28   DCHECK(!window_);
29   window_ = window;
30
31   gpu::GLInProcessContextAttribs attribs;
32   gl_context_.reset(gpu::GLInProcessContext::CreateContext(
33       false, window_, size_, false, attribs, gfx::PreferDiscreteGpu));
34   gl_context_->SetContextLostCallback(base::Bind(
35       &NativeViewportAndroid::OnGLContextLost, base::Unretained(this)));
36
37   delegate_->OnGLContextAvailable(gl_context_->GetImplementation());
38 }
39
40 void NativeViewportAndroid::OnGLContextLost() {
41   gl_context_.reset();
42   delegate_->OnGLContextLost();
43 }
44
45 void NativeViewportAndroid::OnNativeWindowDestroyed() {
46   DCHECK(window_);
47   ReleaseWindow();
48 }
49
50 void NativeViewportAndroid::OnResized(const gfx::Size& size) {
51   size_ = size;
52   delegate_->OnResized(size);
53 }
54
55 void NativeViewportAndroid::ReleaseWindow() {
56   gl_context_.reset();
57   ANativeWindow_release(window_);
58   window_ = NULL;
59 }
60
61 void NativeViewportAndroid::Close() {
62   // TODO(beng): close activity containing MojoView?
63
64   // TODO(beng): perform this in response to view destruction.
65   delegate_->OnDestroyed();
66 }
67
68 // static
69 scoped_ptr<NativeViewport> NativeViewport::Create(
70     shell::Context* context,
71     NativeViewportDelegate* delegate) {
72   scoped_ptr<NativeViewportAndroid> native_viewport(
73       new NativeViewportAndroid(delegate));
74
75   MojoViewportInit* init = new MojoViewportInit();
76   init->ui_runner = context->task_runners()->ui_runner();
77   init->native_viewport = native_viewport->GetWeakPtr();
78
79   context->task_runners()->java_runner()->PostTask(FROM_HERE,
80       base::Bind(MojoViewport::CreateForActivity,
81                  context->activity(),
82                  init));
83
84    return scoped_ptr<NativeViewport>(native_viewport.Pass());
85 }
86
87 }  // namespace services
88 }  // namespace mojo