| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/ui/ws/gpu_service_proxy.h" | 5 #include "services/ui/ws/gpu_service_proxy.h" |
| 6 | 6 |
| 7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 gfx::BufferFormat format, | 103 gfx::BufferFormat format, |
| 104 gfx::BufferUsage usage, | 104 gfx::BufferUsage usage, |
| 105 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) { | 105 const mojom::GpuService::CreateGpuMemoryBufferCallback& callback) { |
| 106 // TODO(sad): Check to see if native gpu memory buffer can be used first. | 106 // TODO(sad): Check to see if native gpu memory buffer can be used first. |
| 107 if (!gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage) || | 107 if (!gpu::GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage) || |
| 108 !gpu::GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(size, | 108 !gpu::GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(size, |
| 109 format)) { | 109 format)) { |
| 110 callback.Run(gfx::GpuMemoryBufferHandle()); | 110 callback.Run(gfx::GpuMemoryBufferHandle()); |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 | 113 callback.Run(gpu::GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( |
| 114 size_t bytes = 0; | 114 id, size, format, base::kNullProcessHandle)); |
| 115 if (!gfx::BufferSizeForBufferFormatChecked(size, format, &bytes)) { | |
| 116 callback.Run(gfx::GpuMemoryBufferHandle()); | |
| 117 return; | |
| 118 } | |
| 119 | |
| 120 mojo::ScopedSharedBufferHandle mojo_handle = | |
| 121 mojo::SharedBufferHandle::Create(bytes); | |
| 122 if (!mojo_handle.is_valid()) { | |
| 123 callback.Run(gfx::GpuMemoryBufferHandle()); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 base::SharedMemoryHandle shm_handle; | |
| 128 size_t shm_size; | |
| 129 bool readonly; | |
| 130 MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
| 131 std::move(mojo_handle), &shm_handle, &shm_size, &readonly); | |
| 132 if (result != MOJO_RESULT_OK) { | |
| 133 callback.Run(gfx::GpuMemoryBufferHandle()); | |
| 134 return; | |
| 135 } | |
| 136 DCHECK_EQ(shm_size, bytes); | |
| 137 DCHECK(!readonly); | |
| 138 const int stride = base::checked_cast<int>( | |
| 139 gfx::RowSizeForBufferFormat(size.width(), format, 0)); | |
| 140 | |
| 141 gfx::GpuMemoryBufferHandle gmb_handle; | |
| 142 gmb_handle.type = gfx::SHARED_MEMORY_BUFFER; | |
| 143 gmb_handle.id = id; | |
| 144 gmb_handle.handle = shm_handle; | |
| 145 gmb_handle.offset = 0; | |
| 146 gmb_handle.stride = stride; | |
| 147 callback.Run(gmb_handle); | |
| 148 } | 115 } |
| 149 | 116 |
| 150 void GpuServiceProxy::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, | 117 void GpuServiceProxy::DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, |
| 151 const gpu::SyncToken& sync_token) { | 118 const gpu::SyncToken& sync_token) { |
| 152 // NOTIMPLEMENTED(); | 119 // NOTIMPLEMENTED(); |
| 153 } | 120 } |
| 154 | 121 |
| 155 bool GpuServiceProxy::IsMainThread() { | 122 bool GpuServiceProxy::IsMainThread() { |
| 156 return main_thread_task_runner_->BelongsToCurrentThread(); | 123 return main_thread_task_runner_->BelongsToCurrentThread(); |
| 157 } | 124 } |
| 158 | 125 |
| 159 scoped_refptr<base::SingleThreadTaskRunner> | 126 scoped_refptr<base::SingleThreadTaskRunner> |
| 160 GpuServiceProxy::GetIOThreadTaskRunner() { | 127 GpuServiceProxy::GetIOThreadTaskRunner() { |
| 161 return io_thread_->task_runner(); | 128 return io_thread_->task_runner(); |
| 162 } | 129 } |
| 163 | 130 |
| 164 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( | 131 std::unique_ptr<base::SharedMemory> GpuServiceProxy::AllocateSharedMemory( |
| 165 size_t size) { | 132 size_t size) { |
| 166 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 133 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 167 if (!shm->CreateAnonymous(size)) | 134 if (!shm->CreateAnonymous(size)) |
| 168 shm.reset(); | 135 shm.reset(); |
| 169 return shm; | 136 return shm; |
| 170 } | 137 } |
| 171 | 138 |
| 172 } // namespace ws | 139 } // namespace ws |
| 173 } // namespace ui | 140 } // namespace ui |
| OLD | NEW |