summaryrefslogtreecommitdiffstats
path: root/components/native_viewport/native_viewport_impl.cc
blob: 2f85500088553deddbbbaca9b657e83e1ddf9dfe (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2014 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 "components/native_viewport/native_viewport_impl.h"

#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "components/gles2/gpu_state.h"
#include "components/native_viewport/platform_viewport_headless.h"
#include "mojo/converters/geometry/geometry_type_converters.h"
#include "third_party/mojo/src/mojo/public/cpp/application/interface_factory.h"
#include "ui/events/event.h"

namespace native_viewport {

NativeViewportImpl::NativeViewportImpl(
    bool is_headless,
    const scoped_refptr<gles2::GpuState>& gpu_state,
    mojo::InterfaceRequest<mojo::NativeViewport> request)
    : is_headless_(is_headless),
      context_provider_(gpu_state),
      sent_metrics_(false),
      metrics_(mojo::ViewportMetrics::New()),
      binding_(this, request.Pass()),
      weak_factory_(this) {
  binding_.set_error_handler(this);
}

NativeViewportImpl::~NativeViewportImpl() {
  // Destroy the NativeViewport early on as it may call us back during
  // destruction and we want to be in a known state.
  platform_viewport_.reset();
}

void NativeViewportImpl::Create(mojo::SizePtr size,
                                const CreateCallback& callback) {
  create_callback_ = callback;
  metrics_->size = size.Clone();
  if (is_headless_)
    platform_viewport_ = PlatformViewportHeadless::Create(this);
  else
    platform_viewport_ = PlatformViewport::Create(this);
  platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>()));
}

void NativeViewportImpl::RequestMetrics(
    const RequestMetricsCallback& callback) {
  if (!sent_metrics_) {
    callback.Run(metrics_.Clone());
    sent_metrics_ = true;
    return;
  }
  metrics_callback_ = callback;
}

void NativeViewportImpl::Show() {
  platform_viewport_->Show();
}

void NativeViewportImpl::Hide() {
  platform_viewport_->Hide();
}

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

void NativeViewportImpl::SetSize(mojo::SizePtr size) {
  platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>()));
}

void NativeViewportImpl::GetContextProvider(
    mojo::InterfaceRequest<mojo::ContextProvider> request) {
  context_provider_.Bind(request.Pass());
}

void NativeViewportImpl::SetEventDispatcher(
    mojo::NativeViewportEventDispatcherPtr dispatcher) {
  event_dispatcher_ = dispatcher.Pass();
}

void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) {
  if (metrics_->Equals(*metrics))
    return;

  metrics_ = metrics.Pass();
  sent_metrics_ = false;

  if (!metrics_callback_.is_null()) {
    metrics_callback_.Run(metrics_.Clone());
    metrics_callback_.reset();
    sent_metrics_ = true;
  }
}

void NativeViewportImpl::OnAcceleratedWidgetAvailable(
    gfx::AcceleratedWidget widget,
    float device_pixel_ratio) {
  metrics_->device_pixel_ratio = device_pixel_ratio;
  context_provider_.SetAcceleratedWidget(widget);
  // TODO: The metrics here might not match the actual window size on android
  // where we don't know the actual size until the first OnMetricsChanged call.
  create_callback_.Run(metrics_.Clone());
  sent_metrics_ = true;
  create_callback_.reset();
}

void NativeViewportImpl::OnAcceleratedWidgetDestroyed() {
  context_provider_.SetAcceleratedWidget(gfx::kNullAcceleratedWidget);
}

bool NativeViewportImpl::OnEvent(mojo::EventPtr event) {
  if (event.is_null() || !event_dispatcher_.get())
    return false;

  mojo::NativeViewportEventDispatcher::OnEventCallback callback;
  switch (event->action) {
    case mojo::EVENT_TYPE_POINTER_MOVE: {
      // TODO(sky): add logic to remember last event location and not send if
      // the same.
      if (pointers_waiting_on_ack_.count(event->pointer_data->pointer_id))
        return false;

      pointers_waiting_on_ack_.insert(event->pointer_data->pointer_id);
      callback =
          base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr(),
                     event->pointer_data->pointer_id);
      break;
    }

    case mojo::EVENT_TYPE_POINTER_CANCEL:
      pointers_waiting_on_ack_.clear();
      break;

    case mojo::EVENT_TYPE_POINTER_UP:
      pointers_waiting_on_ack_.erase(event->pointer_data->pointer_id);
      break;

    default:
      break;
  }

  event_dispatcher_->OnEvent(event.Pass(), callback);
  return false;
}

void NativeViewportImpl::OnDestroyed() {
  // This will signal a connection error and cause us to delete |this|.
  binding_.Close();
}

void NativeViewportImpl::OnConnectionError() {
  binding_.set_error_handler(nullptr);
  delete this;
}

void NativeViewportImpl::AckEvent(int32 pointer_id) {
  pointers_waiting_on_ack_.erase(pointer_id);
}

}  // namespace native_viewport