| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/services/native_viewport/native_viewport_android.h" | 5 #include "mojo/services/native_viewport/native_viewport_android.h" |
| 6 | 6 |
| 7 #include <android/native_window_jni.h> | 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" | 8 #include "mojo/services/native_viewport/android/mojo_viewport.h" |
| 11 #include "mojo/shell/context.h" | 9 #include "mojo/shell/context.h" |
| 12 | 10 |
| 13 namespace mojo { | 11 namespace mojo { |
| 14 namespace services { | 12 namespace services { |
| 15 | 13 |
| 16 NativeViewportAndroid::NativeViewportAndroid(NativeViewportDelegate* delegate) | 14 NativeViewportAndroid::NativeViewportAndroid(NativeViewportDelegate* delegate) |
| 17 : delegate_(delegate), | 15 : delegate_(delegate), |
| 18 window_(NULL), | 16 window_(NULL), |
| 19 weak_factory_(this) { | 17 weak_factory_(this) { |
| 20 } | 18 } |
| 21 | 19 |
| 22 NativeViewportAndroid::~NativeViewportAndroid() { | 20 NativeViewportAndroid::~NativeViewportAndroid() { |
| 23 if (window_) | 21 if (window_) |
| 24 ReleaseWindow(); | 22 ReleaseWindow(); |
| 25 } | 23 } |
| 26 | 24 |
| 27 void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) { | 25 void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) { |
| 28 DCHECK(!window_); | 26 DCHECK(!window_); |
| 29 window_ = window; | 27 window_ = window; |
| 30 | 28 delegate_->OnAcceleratedWidgetAvailable(window_); |
| 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 } | 29 } |
| 44 | 30 |
| 45 void NativeViewportAndroid::OnNativeWindowDestroyed() { | 31 void NativeViewportAndroid::OnNativeWindowDestroyed() { |
| 46 DCHECK(window_); | 32 DCHECK(window_); |
| 47 ReleaseWindow(); | 33 ReleaseWindow(); |
| 48 } | 34 } |
| 49 | 35 |
| 50 void NativeViewportAndroid::OnResized(const gfx::Size& size) { | 36 void NativeViewportAndroid::OnResized(const gfx::Size& size) { |
| 51 size_ = size; | 37 size_ = size; |
| 52 delegate_->OnResized(size); | 38 delegate_->OnResized(size); |
| 53 } | 39 } |
| 54 | 40 |
| 55 void NativeViewportAndroid::ReleaseWindow() { | 41 void NativeViewportAndroid::ReleaseWindow() { |
| 56 gl_context_.reset(); | |
| 57 ANativeWindow_release(window_); | 42 ANativeWindow_release(window_); |
| 58 window_ = NULL; | 43 window_ = NULL; |
| 59 } | 44 } |
| 60 | 45 |
| 46 gfx::Size NativeViewportAndroid::GetSize() { |
| 47 return size_; |
| 48 } |
| 49 |
| 61 void NativeViewportAndroid::Close() { | 50 void NativeViewportAndroid::Close() { |
| 62 // TODO(beng): close activity containing MojoView? | 51 // TODO(beng): close activity containing MojoView? |
| 63 | 52 |
| 64 // TODO(beng): perform this in response to view destruction. | 53 // TODO(beng): perform this in response to view destruction. |
| 65 delegate_->OnDestroyed(); | 54 delegate_->OnDestroyed(); |
| 66 } | 55 } |
| 67 | 56 |
| 68 // static | 57 // static |
| 69 scoped_ptr<NativeViewport> NativeViewport::Create( | 58 scoped_ptr<NativeViewport> NativeViewport::Create( |
| 70 shell::Context* context, | 59 shell::Context* context, |
| 71 NativeViewportDelegate* delegate) { | 60 NativeViewportDelegate* delegate) { |
| 72 scoped_ptr<NativeViewportAndroid> native_viewport( | 61 scoped_ptr<NativeViewportAndroid> native_viewport( |
| 73 new NativeViewportAndroid(delegate)); | 62 new NativeViewportAndroid(delegate)); |
| 74 | 63 |
| 75 MojoViewportInit* init = new MojoViewportInit(); | 64 MojoViewportInit* init = new MojoViewportInit(); |
| 76 init->ui_runner = context->task_runners()->ui_runner(); | 65 init->ui_runner = context->task_runners()->ui_runner(); |
| 77 init->native_viewport = native_viewport->GetWeakPtr(); | 66 init->native_viewport = native_viewport->GetWeakPtr(); |
| 78 | 67 |
| 79 context->task_runners()->java_runner()->PostTask(FROM_HERE, | 68 context->task_runners()->java_runner()->PostTask(FROM_HERE, |
| 80 base::Bind(MojoViewport::CreateForActivity, | 69 base::Bind(MojoViewport::CreateForActivity, |
| 81 context->activity(), | 70 context->activity(), |
| 82 init)); | 71 init)); |
| 83 | 72 |
| 84 return scoped_ptr<NativeViewport>(native_viewport.Pass()); | 73 return scoped_ptr<NativeViewport>(native_viewport.Pass()); |
| 85 } | 74 } |
| 86 | 75 |
| 87 } // namespace services | 76 } // namespace services |
| 88 } // namespace mojo | 77 } // namespace mojo |
| OLD | NEW |