summaryrefslogtreecommitdiffstats
path: root/mojo/services/native_viewport/native_viewport_impl.cc
blob: 5b980c82d443a321a558163a4733b2af375303ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/services/native_viewport/native_viewport_impl.h"

#include "base/message_loop/message_loop.h"
#include "mojo/services/gles2/gles2_impl.h"
#include "mojo/services/native_viewport/native_viewport.h"
#include "ui/events/event.h"

namespace mojo {
namespace services {

NativeViewportImpl::NativeViewportImpl(shell::Context* context,
                                       ScopedMessagePipeHandle pipe)
    : context_(context),
      widget_(gfx::kNullAcceleratedWidget),
      client_(pipe.Pass()) {
  client_.SetPeer(this);
}

NativeViewportImpl::~NativeViewportImpl() {
}

void NativeViewportImpl::Open() {
  native_viewport_ = services::NativeViewport::Create(context_, this);
  native_viewport_->Init();
  client_->OnCreated();
}

void NativeViewportImpl::Close() {
  DCHECK(native_viewport_);
  native_viewport_->Close();
}

void NativeViewportImpl::CreateGLES2Context(
    ScopedMessagePipeHandle gles2_client) {
  gles2_.reset(new GLES2Impl(gles2_client.Pass()));
  CreateGLES2ContextIfNeeded();
}

void NativeViewportImpl::CreateGLES2ContextIfNeeded() {
  if (widget_ == gfx::kNullAcceleratedWidget || !gles2_)
    return;
  gles2_->CreateContext(widget_, native_viewport_->GetSize());
}

bool NativeViewportImpl::OnEvent(ui::Event* ui_event) {
  AllocationScope scope;

  Event::Builder event;
  event.set_action(ui_event->type());
  event.set_time_stamp(ui_event->time_stamp().ToInternalValue());

  if (ui_event->IsMouseEvent() || ui_event->IsTouchEvent()) {
    ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(ui_event);
    Point::Builder location;
    location.set_x(located_event->location().x());
    location.set_y(located_event->location().y());
    event.set_location(location.Finish());
  }

  if (ui_event->IsTouchEvent()) {
    ui::TouchEvent* touch_event = static_cast<ui::TouchEvent*>(ui_event);
    TouchData::Builder touch_data;
    touch_data.set_pointer_id(touch_event->touch_id());
    event.set_touch_data(touch_data.Finish());
  }

  client_->OnEvent(event.Finish());
  return false;
}

void NativeViewportImpl::OnAcceleratedWidgetAvailable(
    gfx::AcceleratedWidget widget) {
  widget_ = widget;
  CreateGLES2ContextIfNeeded();
}

void NativeViewportImpl::OnResized(const gfx::Size& size) {
}

void NativeViewportImpl::OnDestroyed() {
  // TODO(beng):
  // Destroying |gles2_| on the shell thread here hits thread checker asserts.
  // All code must stop touching the AcceleratedWidget at this point as it is
  // dead after this call stack. jamesr said we probably should make our own
  // GLSurface and simply tell it to stop touching the AcceleratedWidget
  // via Destroy() but we have no good way of doing that right now given our
  // current threading model so james' recommendation was just to wait until
  // after we move the gl service out of process.
  // gles2_.reset();
  client_->OnDestroyed();
}

}  // namespace services
}  // namespace mojo