| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/examples/aura_demo/context_factory_view_manager.h" | 5 #include "mojo/aura/context_factory_mojo.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "cc/output/output_surface.h" | 8 #include "cc/output/output_surface.h" |
| 9 #include "cc/output/software_output_device.h" | 9 #include "cc/output/software_output_device.h" |
| 10 #include "cc/resources/shared_bitmap_manager.h" | 10 #include "cc/resources/shared_bitmap_manager.h" |
| 11 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | 11 #include "mojo/aura/window_tree_host_mojo.h" |
| 12 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
| 13 #include "ui/compositor/reflector.h" | 13 #include "ui/compositor/reflector.h" |
| 14 #include "ui/gfx/codec/png_codec.h" | |
| 15 | 14 |
| 16 namespace mojo { | 15 namespace mojo { |
| 17 namespace examples { | |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 void FreeSharedBitmap(cc::SharedBitmap* shared_bitmap) { | 18 void FreeSharedBitmap(cc::SharedBitmap* shared_bitmap) { |
| 21 delete shared_bitmap->memory(); | 19 delete shared_bitmap->memory(); |
| 22 } | 20 } |
| 23 | 21 |
| 24 void IgnoreSharedBitmap(cc::SharedBitmap* shared_bitmap) {} | 22 void IgnoreSharedBitmap(cc::SharedBitmap* shared_bitmap) {} |
| 25 | 23 |
| 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 { | 24 class SoftwareOutputDeviceViewManager : public cc::SoftwareOutputDevice { |
| 50 public: | 25 public: |
| 51 explicit SoftwareOutputDeviceViewManager( | 26 explicit SoftwareOutputDeviceViewManager(ui::Compositor* compositor) |
| 52 view_manager::IViewManager* view_manager, | 27 : compositor_(compositor) { |
| 53 uint32_t view_id) | |
| 54 : view_manager_(view_manager), | |
| 55 view_id_(view_id) { | |
| 56 } | 28 } |
| 57 virtual ~SoftwareOutputDeviceViewManager() {} | 29 virtual ~SoftwareOutputDeviceViewManager() {} |
| 58 | 30 |
| 59 // cc::SoftwareOutputDevice: | 31 // cc::SoftwareOutputDevice: |
| 60 virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE { | 32 virtual void EndPaint(cc::SoftwareFrameData* frame_data) OVERRIDE { |
| 61 SetViewContents(); | 33 WindowTreeHostMojo* window_tree_host = |
| 34 WindowTreeHostMojo::ForCompositor(compositor_); |
| 35 DCHECK(window_tree_host); |
| 36 window_tree_host->SetContents( |
| 37 skia::GetTopDevice(*canvas_)->accessBitmap(true)); |
| 62 | 38 |
| 63 SoftwareOutputDevice::EndPaint(frame_data); | 39 SoftwareOutputDevice::EndPaint(frame_data); |
| 64 } | 40 } |
| 65 | 41 |
| 66 private: | 42 private: |
| 67 void OnSetViewContentsDone(bool value) { | 43 ui::Compositor* compositor_; |
| 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 | 44 |
| 98 DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceViewManager); | 45 DISALLOW_COPY_AND_ASSIGN(SoftwareOutputDeviceViewManager); |
| 99 }; | 46 }; |
| 100 | 47 |
| 101 // TODO(sky): this is a copy from cc/test. Copy to a common place. | 48 // TODO(sky): this is a copy from cc/test. Copy to a common place. |
| 102 class TestSharedBitmapManager : public cc::SharedBitmapManager { | 49 class TestSharedBitmapManager : public cc::SharedBitmapManager { |
| 103 public: | 50 public: |
| 104 TestSharedBitmapManager() {} | 51 TestSharedBitmapManager() {} |
| 105 virtual ~TestSharedBitmapManager() {} | 52 virtual ~TestSharedBitmapManager() {} |
| 106 | 53 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 85 |
| 139 private: | 86 private: |
| 140 base::Lock lock_; | 87 base::Lock lock_; |
| 141 std::map<cc::SharedBitmapId, base::SharedMemory*> bitmap_map_; | 88 std::map<cc::SharedBitmapId, base::SharedMemory*> bitmap_map_; |
| 142 | 89 |
| 143 DISALLOW_COPY_AND_ASSIGN(TestSharedBitmapManager); | 90 DISALLOW_COPY_AND_ASSIGN(TestSharedBitmapManager); |
| 144 }; | 91 }; |
| 145 | 92 |
| 146 } // namespace | 93 } // namespace |
| 147 | 94 |
| 148 ContextFactoryViewManager::ContextFactoryViewManager( | 95 ContextFactoryMojo::ContextFactoryMojo() |
| 149 view_manager::IViewManager* view_manager, | 96 : shared_bitmap_manager_(new TestSharedBitmapManager()) { |
| 150 uint32_t view_id) | |
| 151 : view_manager_(view_manager), | |
| 152 view_id_(view_id), | |
| 153 shared_bitmap_manager_(new TestSharedBitmapManager()) { | |
| 154 } | 97 } |
| 155 | 98 |
| 156 ContextFactoryViewManager::~ContextFactoryViewManager() {} | 99 ContextFactoryMojo::~ContextFactoryMojo() {} |
| 157 | 100 |
| 158 scoped_ptr<cc::OutputSurface> ContextFactoryViewManager::CreateOutputSurface( | 101 scoped_ptr<cc::OutputSurface> ContextFactoryMojo::CreateOutputSurface( |
| 159 ui::Compositor* compositor, | 102 ui::Compositor* compositor, |
| 160 bool software_fallback) { | 103 bool software_fallback) { |
| 161 scoped_ptr<cc::SoftwareOutputDevice> output_device( | 104 scoped_ptr<cc::SoftwareOutputDevice> output_device( |
| 162 new SoftwareOutputDeviceViewManager(view_manager_, view_id_)); | 105 new SoftwareOutputDeviceViewManager(compositor)); |
| 163 return make_scoped_ptr(new cc::OutputSurface(output_device.Pass())); | 106 return make_scoped_ptr(new cc::OutputSurface(output_device.Pass())); |
| 164 } | 107 } |
| 165 | 108 |
| 166 scoped_refptr<ui::Reflector> ContextFactoryViewManager::CreateReflector( | 109 scoped_refptr<ui::Reflector> ContextFactoryMojo::CreateReflector( |
| 167 ui::Compositor* mirroed_compositor, | 110 ui::Compositor* mirroed_compositor, |
| 168 ui::Layer* mirroring_layer) { | 111 ui::Layer* mirroring_layer) { |
| 169 return new ui::Reflector(); | 112 return new ui::Reflector(); |
| 170 } | 113 } |
| 171 | 114 |
| 172 void ContextFactoryViewManager::RemoveReflector( | 115 void ContextFactoryMojo::RemoveReflector( |
| 173 scoped_refptr<ui::Reflector> reflector) { | 116 scoped_refptr<ui::Reflector> reflector) { |
| 174 } | 117 } |
| 175 | 118 |
| 176 scoped_refptr<cc::ContextProvider> | 119 scoped_refptr<cc::ContextProvider> |
| 177 ContextFactoryViewManager::SharedMainThreadContextProvider() { | 120 ContextFactoryMojo::SharedMainThreadContextProvider() { |
| 178 return scoped_refptr<cc::ContextProvider>(NULL); | 121 return scoped_refptr<cc::ContextProvider>(NULL); |
| 179 } | 122 } |
| 180 | 123 |
| 181 void ContextFactoryViewManager::RemoveCompositor(ui::Compositor* compositor) {} | 124 void ContextFactoryMojo::RemoveCompositor(ui::Compositor* compositor) {} |
| 182 | 125 |
| 183 bool ContextFactoryViewManager::DoesCreateTestContexts() { return false; } | 126 bool ContextFactoryMojo::DoesCreateTestContexts() { return false; } |
| 184 | 127 |
| 185 cc::SharedBitmapManager* ContextFactoryViewManager::GetSharedBitmapManager() { | 128 cc::SharedBitmapManager* ContextFactoryMojo::GetSharedBitmapManager() { |
| 186 return shared_bitmap_manager_.get(); | 129 return shared_bitmap_manager_.get(); |
| 187 } | 130 } |
| 188 | 131 |
| 189 base::MessageLoopProxy* ContextFactoryViewManager::GetCompositorMessageLoop() { | 132 base::MessageLoopProxy* ContextFactoryMojo::GetCompositorMessageLoop() { |
| 190 return NULL; | 133 return NULL; |
| 191 } | 134 } |
| 192 | 135 |
| 193 } // namespace examples | |
| 194 } // namespace mojo | 136 } // namespace mojo |
| OLD | NEW |