summaryrefslogtreecommitdiffstats
path: root/mojo/examples/aura_demo/aura_demo.cc
blob: b2464847a9476d6b931f7ca77caf4e20f77dba88 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// 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 <stdio.h>
#include <string>

#include "base/bind.h"
#include "mojo/aura/context_factory_mojo.h"
#include "mojo/aura/screen_mojo.h"
#include "mojo/aura/window_tree_host_mojo.h"
#include "mojo/aura/window_tree_host_mojo_delegate.h"
#include "mojo/public/cpp/application/application.h"
#include "mojo/public/cpp/system/core.h"
#include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
#include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
#include "ui/aura/client/default_capture_client.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/base/hit_test.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/png_codec.h"

namespace mojo {
namespace examples {

void OnSetViewContentsDone(bool value) {
  VLOG(1) << "OnSetViewContentsDone " << value;
  DCHECK(value);
}

bool CreateMapAndDupSharedBuffer(size_t size,
                                 void** memory,
                                 ScopedSharedBufferHandle* handle,
                                 ScopedSharedBufferHandle* duped) {
  MojoResult result = CreateSharedBuffer(NULL, size, handle);
  if (result != MOJO_RESULT_OK)
    return false;
  DCHECK(handle->is_valid());

  result = DuplicateBuffer(handle->get(), NULL, duped);
  if (result != MOJO_RESULT_OK)
    return false;
  DCHECK(duped->is_valid());

  result = MapBuffer(
      handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE);
  if (result != MOJO_RESULT_OK)
    return false;
  DCHECK(*memory);

  return true;
}

// Trivial WindowDelegate implementation that draws a colored background.
class DemoWindowDelegate : public aura::WindowDelegate {
 public:
  explicit DemoWindowDelegate(SkColor color) : color_(color) {}

  // Overridden from WindowDelegate:
  virtual gfx::Size GetMinimumSize() const OVERRIDE {
    return gfx::Size();
  }

  virtual gfx::Size GetMaximumSize() const OVERRIDE {
    return gfx::Size();
  }

  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
                               const gfx::Rect& new_bounds) OVERRIDE {}
  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
    return gfx::kNullCursor;
  }
  virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
    return HTCAPTION;
  }
  virtual bool ShouldDescendIntoChildForEventHandling(
      aura::Window* child,
      const gfx::Point& location) OVERRIDE {
    return true;
  }
  virtual bool CanFocus() OVERRIDE { return true; }
  virtual void OnCaptureLost() OVERRIDE {}
  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
    canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
  }
  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
  virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
  virtual bool HasHitTestMask() const OVERRIDE { return false; }
  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}

 private:
  const SkColor color_;

  DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
};

class DemoWindowTreeClient : public aura::client::WindowTreeClient {
 public:
  explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
    aura::client::SetWindowTreeClient(window_, this);
  }

  virtual ~DemoWindowTreeClient() {
    aura::client::SetWindowTreeClient(window_, NULL);
  }

  // Overridden from aura::client::WindowTreeClient:
  virtual aura::Window* GetDefaultParent(aura::Window* context,
                                         aura::Window* window,
                                         const gfx::Rect& bounds) OVERRIDE {
    if (!capture_client_) {
      capture_client_.reset(
          new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
    }
    return window_;
  }

 private:
  aura::Window* window_;
  scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;

  DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
};

class AuraDemo;

// Trivial ViewManagerClient implementation. Forwards to AuraDemo when
// connection established.
class ViewManagerClientImpl
    : public InterfaceImpl<view_manager::ViewManagerClient> {
 public:
  explicit ViewManagerClientImpl(AuraDemo* aura_demo)
      : aura_demo_(aura_demo) {}
  virtual ~ViewManagerClientImpl() {}

 private:
  void OnResult(bool result) {
    VLOG(1) << "ViewManagerClientImpl::::OnResult result=" << result;
    DCHECK(result);
  }

  // ViewManagerClient:
  virtual void OnViewManagerConnectionEstablished(
      uint16_t connection_id,
      const String& creator_url,
      uint32_t next_server_change_id,
      mojo::Array<view_manager::NodeDataPtr> nodes) OVERRIDE;
  virtual void OnRootsAdded(Array<view_manager::NodeDataPtr> nodes) OVERRIDE {
    NOTREACHED();
  }
  virtual void OnServerChangeIdAdvanced(
      uint32_t next_server_change_id) OVERRIDE {
  }
  virtual void OnNodeBoundsChanged(uint32_t node,
                                   mojo::RectPtr old_bounds,
                                   mojo::RectPtr new_bounds) OVERRIDE {
  }
  virtual void OnNodeHierarchyChanged(
      uint32_t node,
      uint32_t new_parent,
      uint32_t old_parent,
      uint32_t server_change_id,
      mojo::Array<view_manager::NodeDataPtr> nodes) OVERRIDE {
  }
  virtual void OnNodeReordered(
      uint32_t node_id,
      uint32_t relative_node_id,
      view_manager::OrderDirection direction,
      uint32_t server_change_id) OVERRIDE {
  }
  virtual void OnNodeDeleted(uint32_t node, uint32_t server_change_id)
      OVERRIDE {
  }
  virtual void OnNodeViewReplaced(uint32_t node,
                                  uint32_t new_view_id,
                                  uint32_t old_view_id) OVERRIDE {
  }
  virtual void OnViewDeleted(uint32_t view) OVERRIDE {
  }
  virtual void OnViewInputEvent(uint32_t view_id,
                                EventPtr event,
                                const Callback<void()>& callback) OVERRIDE {
  }
  virtual void DispatchOnViewInputEvent(uint32_t view_id,
                                        EventPtr event) OVERRIDE {
  }

  AuraDemo* aura_demo_;

  DISALLOW_COPY_AND_ASSIGN(ViewManagerClientImpl);
};

class AuraDemo : public Application, public WindowTreeHostMojoDelegate {
 public:
  AuraDemo()
      : view_manager_(NULL),
        window1_(NULL),
        window2_(NULL),
        window21_(NULL),
        view_id_(0) {
    AddService<ViewManagerClientImpl>(this);
  }
  virtual ~AuraDemo() {}

  void SetRoot(view_manager::ViewManagerService* view_manager,
               uint32_t view_id) {
    aura::Env::CreateInstance(true);
    view_manager_ = view_manager;
    view_id_ = view_id;
    context_factory_.reset(new ContextFactoryMojo);
    aura::Env::GetInstance()->set_context_factory(context_factory_.get());
    screen_.reset(ScreenMojo::Create());
    gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());

    window_tree_host_.reset(new WindowTreeHostMojo(gfx::Rect(800, 600), this));
    window_tree_host_->InitHost();

    window_tree_client_.reset(
        new DemoWindowTreeClient(window_tree_host_->window()));

    delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
    window1_ = new aura::Window(delegate1_.get());
    window1_->Init(aura::WINDOW_LAYER_TEXTURED);
    window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
    window1_->Show();
    window_tree_host_->window()->AddChild(window1_);

    delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
    window2_ = new aura::Window(delegate2_.get());
    window2_->Init(aura::WINDOW_LAYER_TEXTURED);
    window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
    window2_->Show();
    window_tree_host_->window()->AddChild(window2_);

    delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
    window21_ = new aura::Window(delegate21_.get());
    window21_->Init(aura::WINDOW_LAYER_TEXTURED);
    window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
    window21_->Show();
    window2_->AddChild(window21_);

    window_tree_host_->Show();
  }

  // WindowTreeHostMojoDelegate:
  virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE {
    std::vector<unsigned char> data;
    gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data);

    void* memory = NULL;
    ScopedSharedBufferHandle duped;
    bool result = CreateMapAndDupSharedBuffer(data.size(),
                                              &memory,
                                              &shared_state_handle_,
                                              &duped);
    if (!result)
      return;

    memcpy(memory, &data[0], data.size());

    view_manager_->SetViewContents(
        view_id_, duped.Pass(), static_cast<uint32_t>(data.size()),
        base::Bind(&OnSetViewContentsDone));
  }

  virtual void Initialize() OVERRIDE {
  }

  scoped_ptr<DemoWindowTreeClient> window_tree_client_;

  scoped_ptr<ui::ContextFactory> context_factory_;

  scoped_ptr<ScreenMojo> screen_;

  scoped_ptr<DemoWindowDelegate> delegate1_;
  scoped_ptr<DemoWindowDelegate> delegate2_;
  scoped_ptr<DemoWindowDelegate> delegate21_;

  view_manager::ViewManagerService* view_manager_;

  aura::Window* window1_;
  aura::Window* window2_;
  aura::Window* window21_;

  uint32_t view_id_;

  scoped_ptr<aura::WindowTreeHost> window_tree_host_;

  ScopedSharedBufferHandle shared_state_handle_;

  DISALLOW_COPY_AND_ASSIGN(AuraDemo);
};

void ViewManagerClientImpl::OnViewManagerConnectionEstablished(
      uint16_t connection_id,
      const String& creator_url,
      uint32_t next_server_change_id,
      mojo::Array<view_manager::NodeDataPtr> nodes) {
  const uint32_t view_id = connection_id << 16 | 1;
  client()->CreateView(view_id, base::Bind(&ViewManagerClientImpl::OnResult,
                                           base::Unretained(this)));
  client()->SetView(nodes[0]->node_id, view_id,
                    base::Bind(&ViewManagerClientImpl::OnResult,
                               base::Unretained(this)));

  aura_demo_->SetRoot(client(), view_id);
}

}  // namespace examples

// static
Application* Application::Create() {
  return new examples::AuraDemo();
}

}  // namespace mojo