native_viewport_impl.cc revision f2477e01787aa58f445919b809d89e252beef54f
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_impl.h"
6
7#include <limits>
8
9#include "base/bind.h"
10#include "base/message_loop/message_loop.h"
11#include "base/strings/stringprintf.h"
12#include "gpu/command_buffer/client/gl_in_process_context.h"
13#include "gpu/command_buffer/client/gles2_implementation.h"
14#include "mojo/services/native_viewport/native_viewport.h"
15#include "ui/events/event.h"
16
17namespace mojo {
18namespace services {
19
20NativeViewportImpl::NativeViewportImpl(shell::Context* context,
21                                       ScopedMessagePipeHandle pipe)
22    : context_(context),
23      client_(pipe.Pass()) {
24  client_.SetPeer(this);
25}
26
27NativeViewportImpl::~NativeViewportImpl() {
28}
29
30void NativeViewportImpl::Open() {
31  native_viewport_ = services::NativeViewport::Create(context_, this);
32  native_viewport_->Init();
33  client_->DidOpen();
34}
35
36void NativeViewportImpl::Close() {
37  DCHECK(native_viewport_);
38  native_viewport_->Close();
39}
40
41bool NativeViewportImpl::OnEvent(ui::Event* event) {
42  return false;
43}
44
45void NativeViewportImpl::OnAcceleratedWidgetAvailable(
46    gfx::AcceleratedWidget widget) {
47  gfx::Size size = native_viewport_->GetSize();
48  gpu::GLInProcessContextAttribs attribs;
49  gl_context_.reset(gpu::GLInProcessContext::CreateContext(
50      false, widget, size, false, attribs, gfx::PreferDiscreteGpu));
51  gl_context_->SetContextLostCallback(base::Bind(
52      &NativeViewportImpl::OnGLContextLost, base::Unretained(this)));
53
54  gpu::gles2::GLES2Interface* gl = gl_context_->GetImplementation();
55  // TODO(abarth): Instead of drawing green, we want to send the context over
56  // pipe_ somehow.
57  uint64_t encoded_gl = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(gl));
58  client_->DidCreateGLContext(encoded_gl);
59}
60
61void NativeViewportImpl::OnGLContextLost() {
62}
63
64void NativeViewportImpl::OnResized(const gfx::Size& size) {
65}
66
67void NativeViewportImpl::OnDestroyed() {
68  base::MessageLoop::current()->Quit();
69}
70
71}  // namespace services
72}  // namespace mojo
73