summaryrefslogtreecommitdiffstats
path: root/chromium/mojo/aura/context_factory_mojo.cc
blob: 6b2b709cdcd75a7aa9c3c7c0020a6aaaef7e5a04 (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
// 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 "mojo/aura/context_factory_mojo.h"

#include "base/bind.h"
#include "cc/output/output_surface.h"
#include "cc/output/software_output_device.h"
#include "cc/resources/shared_bitmap_manager.h"
#include "mojo/aura/window_tree_host_mojo.h"
#include "skia/ext/platform_canvas.h"
#include "ui/compositor/reflector.h"

namespace mojo {
namespace {

void FreeSharedBitmap(cc::SharedBitmap* shared_bitmap) {
  delete shared_bitmap->memory();
}

void IgnoreSharedBitmap(cc::SharedBitmap* shared_bitmap) {}

class SoftwareOutputDeviceViewManager : public cc::SoftwareOutputDevice {
 public:
  explicit SoftwareOutputDeviceViewManager(ui::Compositor* compositor)
      : compositor_(compositor) {
  }
  virtual ~SoftwareOutputDeviceViewManager() {}

  // cc::SoftwareOutputDevice:
  virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE {
    WindowTreeHostMojo* window_tree_host =
        WindowTreeHostMojo::ForCompositor(compositor_);
    DCHECK(window_tree_host);
    window_tree_host->SetContents(
        skia::GetTopDevice(*canvas_)->accessBitmap(true));

    SoftwareOutputDevice::EndPaint(frame_data);
  }

 private:
  ui::Compositor* compositor_;

  DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceViewManager);
};

// TODO(sky): this is a copy from cc/test. Copy to a common place.
class TestSharedBitmapManager : public cc::SharedBitmapManager {
 public:
  TestSharedBitmapManager() {}
  virtual ~TestSharedBitmapManager() {}

  virtual scoped_ptr<cc::SharedBitmap> AllocateSharedBitmap(
      const gfx::Size& size) OVERRIDE {
    base::AutoLock lock(lock_);
    scoped_ptr<base::SharedMemory> memory(new base::SharedMemory);
    memory->CreateAndMapAnonymous(size.GetArea() * 4);
    cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
    bitmap_map_[id] = memory.get();
    return scoped_ptr<cc::SharedBitmap>(
        new cc::SharedBitmap(memory.release(), id,
                             base::Bind(&FreeSharedBitmap)));
  }

  virtual scoped_ptr<cc::SharedBitmap> GetSharedBitmapFromId(
      const gfx::Size&,
      const cc::SharedBitmapId& id) OVERRIDE {
    base::AutoLock lock(lock_);
    if (bitmap_map_.find(id) == bitmap_map_.end())
      return scoped_ptr<cc::SharedBitmap>();
    return scoped_ptr<cc::SharedBitmap>(
        new cc::SharedBitmap(bitmap_map_[id], id,
                             base::Bind(&IgnoreSharedBitmap)));
  }

  virtual scoped_ptr<cc::SharedBitmap> GetBitmapForSharedMemory(
      base::SharedMemory* memory) OVERRIDE {
    base::AutoLock lock(lock_);
    cc::SharedBitmapId id = cc::SharedBitmap::GenerateId();
    bitmap_map_[id] = memory;
    return scoped_ptr<cc::SharedBitmap>(
        new cc::SharedBitmap(memory, id, base::Bind(&IgnoreSharedBitmap)));
  }

 private:
  base::Lock lock_;
  std::map<cc::SharedBitmapId, base::SharedMemory*> bitmap_map_;

  DISALLOW_COPY_AND_ASSIGN(TestSharedBitmapManager);
};

}  // namespace

ContextFactoryMojo::ContextFactoryMojo()
    : shared_bitmap_manager_(new TestSharedBitmapManager()) {
}

ContextFactoryMojo::~ContextFactoryMojo() {}

scoped_ptr<cc::OutputSurface> ContextFactoryMojo::CreateOutputSurface(
    ui::Compositor* compositor,
    bool software_fallback) {
  scoped_ptr<cc::SoftwareOutputDevice> output_device(
      new SoftwareOutputDeviceViewManager(compositor));
  return make_scoped_ptr(new cc::OutputSurface(output_device.Pass()));
}

scoped_refptr<ui::Reflector> ContextFactoryMojo::CreateReflector(
    ui::Compositor* mirroed_compositor,
    ui::Layer* mirroring_layer) {
  return new ui::Reflector();
}

void ContextFactoryMojo::RemoveReflector(
    scoped_refptr<ui::Reflector> reflector) {
}

scoped_refptr<cc::ContextProvider>
ContextFactoryMojo::SharedMainThreadContextProvider() {
  return scoped_refptr<cc::ContextProvider>(NULL);
}

void ContextFactoryMojo::RemoveCompositor(ui::Compositor* compositor) {}

bool ContextFactoryMojo::DoesCreateTestContexts() { return false; }

cc::SharedBitmapManager* ContextFactoryMojo::GetSharedBitmapManager() {
  return shared_bitmap_manager_.get();
}

base::MessageLoopProxy* ContextFactoryMojo::GetCompositorMessageLoop() {
  return NULL;
}

}  // namespace mojo