OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/examples/aura_demo/context_factory_view_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "cc/output/output_surface.h" | |
9 #include "cc/output/software_output_device.h" | |
10 #include "cc/resources/shared_bitmap_manager.h" | |
11 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | |
12 #include "skia/ext/platform_canvas.h" | |
13 #include "ui/compositor/reflector.h" | |
14 #include "ui/gfx/codec/png_codec.h" | |
15 | |
16 namespace mojo { | |
17 namespace examples { | |
18 namespace { | |
19 | |
20 void FreeSharedBitmap(cc::SharedBitmap* shared_bitmap) { | |
21 delete shared_bitmap->memory(); | |
22 } | |
23 | |
24 void IgnoreSharedBitmap(cc::SharedBitmap* shared_bitmap) {} | |
25 | |
26 bool CreateMapAndDupSharedBuffer(size_t size, | |
27 void** memory, | |
28 ScopedSharedBufferHandle* handle, | |
29 ScopedSharedBufferHandle* duped) { | |
30 MojoResult result = CreateSharedBuffer(NULL, size, handle); | |
31 if (result != MOJO_RESULT_OK) | |
32 return false; | |
33 DCHECK(handle->is_valid()); | |
34 | |
35 result = DuplicateBuffer(handle->get(), NULL, duped); | |
36 if (result != MOJO_RESULT_OK) | |
37 return false; | |
38 DCHECK(duped->is_valid()); | |
39 | |
40 result = MapBuffer( | |
41 handle->get(), 0, size, memory, MOJO_MAP_BUFFER_FLAG_NONE); | |
42 if (result != MOJO_RESULT_OK) | |
43 return false; | |
44 DCHECK(*memory); | |
45 | |
46 return true; | |
47 } | |
48 | |
49 class SoftwareOutputDeviceViewManager : public cc::SoftwareOutputDevice { | |
50 public: | |
51 explicit SoftwareOutputDeviceViewManager( | |
52 view_manager::IViewManager* view_manager, | |
53 uint32_t view_id) | |
54 : view_manager_(view_manager), | |
55 view_id_(view_id) { | |
56 } | |
57 virtual ~SoftwareOutputDeviceViewManager() {} | |
58 | |
59 // cc::SoftwareOutputDevice: | |
60 virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE { | |
61 SetViewContents(); | |
62 | |
63 SoftwareOutputDevice::EndPaint(frame_data); | |
64 } | |
65 | |
66 private: | |
67 void OnSetViewContentsDone(bool value) { | |
68 VLOG(1) << "SoftwareOutputDeviceManager::OnSetViewContentsDone " << value; | |
69 DCHECK(value); | |
70 } | |
71 | |
72 void SetViewContents() { | |
73 std::vector<unsigned char> data; | |
74 gfx::PNGCodec::EncodeBGRASkBitmap( | |
75 skia::GetTopDevice(*canvas_)->accessBitmap(true), false, &data); | |
76 | |
77 void* memory = NULL; | |
78 ScopedSharedBufferHandle duped; | |
79 bool result = CreateMapAndDupSharedBuffer(data.size(), | |
80 &memory, | |
81 &shared_state_handle_, | |
82 &duped); | |
83 if (!result) | |
84 return; | |
85 | |
86 memcpy(memory, &data[0], data.size()); | |
87 | |
88 view_manager_->SetViewContents( | |
89 view_id_, duped.Pass(), static_cast<uint32_t>(data.size()), | |
90 base::Bind(&SoftwareOutputDeviceViewManager::OnSetViewContentsDone, | |
91 base::Unretained(this))); | |
92 } | |
93 | |
94 view_manager::IViewManager* view_manager_; | |
95 const uint32_t view_id_; | |
96 ScopedSharedBufferHandle shared_state_handle_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceViewManager); | |
99 }; | |
100 | |
101 // TODO(sky): this is a copy from cc/test. Copy to a common place. | |
102 class TestSharedBitmapManager : public cc::SharedBitmapManager { | |
103 public: | |
104 TestSharedBitmapManager() {} | |
105 virtual ~TestSharedBitmapManager() {} | |
106 | |
107 virtual scoped_ptr<cc::SharedBitmap> AllocateSharedBitmap( | |
108 const gfx::Size& size) OVERRIDE { | |
109 base::AutoLock lock(lock_); | |
110 scoped_ptr<base::SharedMemory> memory(new base::SharedMemory); | |
111 memory->CreateAndMapAnonymous(size.GetArea() * 4); | |
112 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
113 bitmap_map_[id] = memory.get(); | |
114 return scoped_ptr<cc::SharedBitmap>( | |
115 new cc::SharedBitmap(memory.release(), id, | |
116 base::Bind(&FreeSharedBitmap))); | |
117 } | |
118 | |
119 virtual scoped_ptr<cc::SharedBitmap> GetSharedBitmapFromId( | |
120 const gfx::Size&, | |
121 const cc::SharedBitmapId& id) OVERRIDE { | |
122 base::AutoLock lock(lock_); | |
123 if (bitmap_map_.find(id) == bitmap_map_.end()) | |
124 return scoped_ptr<cc::SharedBitmap>(); | |
125 return scoped_ptr<cc::SharedBitmap>( | |
126 new cc::SharedBitmap(bitmap_map_[id], id, | |
127 base::Bind(&IgnoreSharedBitmap))); | |
128 } | |
129 | |
130 virtual scoped_ptr<cc::SharedBitmap> GetBitmapForSharedMemory( | |
131 base::SharedMemory* memory) OVERRIDE { | |
132 base::AutoLock lock(lock_); | |
133 cc::SharedBitmapId id = cc::SharedBitmap::GenerateId(); | |
134 bitmap_map_[id] = memory; | |
135 return scoped_ptr<cc::SharedBitmap>( | |
136 new cc::SharedBitmap(memory, id, base::Bind(&IgnoreSharedBitmap))); | |
137 } | |
138 | |
139 private: | |
140 base::Lock lock_; | |
141 std::map<cc::SharedBitmapId, base::SharedMemory*> bitmap_map_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(TestSharedBitmapManager); | |
144 }; | |
145 | |
146 } // namespace | |
147 | |
148 ContextFactoryViewManager::ContextFactoryViewManager( | |
149 view_manager::IViewManager* view_manager, | |
150 uint32_t view_id) | |
151 : view_manager_(view_manager), | |
152 view_id_(view_id), | |
153 shared_bitmap_manager_(new TestSharedBitmapManager()) { | |
154 } | |
155 | |
156 ContextFactoryViewManager::~ContextFactoryViewManager() {} | |
157 | |
158 scoped_ptr<cc::OutputSurface> ContextFactoryViewManager::CreateOutputSurface( | |
159 ui::Compositor* compositor, | |
160 bool software_fallback) { | |
161 scoped_ptr<cc::SoftwareOutputDevice> output_device( | |
162 new SoftwareOutputDeviceViewManager(view_manager_, view_id_)); | |
163 return make_scoped_ptr(new cc::OutputSurface(output_device.Pass())); | |
164 } | |
165 | |
166 scoped_refptr<ui::Reflector> ContextFactoryViewManager::CreateReflector( | |
167 ui::Compositor* mirroed_compositor, | |
168 ui::Layer* mirroring_layer) { | |
169 return new ui::Reflector(); | |
170 } | |
171 | |
172 void ContextFactoryViewManager::RemoveReflector( | |
173 scoped_refptr<ui::Reflector> reflector) { | |
174 } | |
175 | |
176 scoped_refptr<cc::ContextProvider> | |
177 ContextFactoryViewManager::SharedMainThreadContextProvider() { | |
178 return scoped_refptr<cc::ContextProvider>(NULL); | |
179 } | |
180 | |
181 void ContextFactoryViewManager::RemoveCompositor(ui::Compositor* compositor) {} | |
182 | |
183 bool ContextFactoryViewManager::DoesCreateTestContexts() { return false; } | |
184 | |
185 cc::SharedBitmapManager* ContextFactoryViewManager::GetSharedBitmapManager() { | |
186 return shared_bitmap_manager_.get(); | |
187 } | |
188 | |
189 base::MessageLoopProxy* ContextFactoryViewManager::GetCompositorMessageLoop() { | |
190 return NULL; | |
191 } | |
192 | |
193 } // namespace examples | |
194 } // namespace mojo | |
OLD | NEW |