OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/exo/shared_memory.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/trace_event/trace_event.h" |
| 9 #include "components/exo/buffer.h" |
| 10 #include "gpu/command_buffer/client/gpu_memory_buffer_manager.h" |
| 11 #include "ui/aura/env.h" |
| 12 #include "ui/compositor/compositor.h" |
| 13 #include "ui/gfx/buffer_format_util.h" |
| 14 #include "ui/gfx/geometry/size.h" |
| 15 #include "ui/gfx/gpu_memory_buffer.h" |
| 16 |
| 17 namespace exo { |
| 18 namespace { |
| 19 |
| 20 bool IsSupportedFormat(gfx::BufferFormat format) { |
| 21 return format == gfx::BufferFormat::RGBX_8888 || |
| 22 format == gfx::BufferFormat::RGBA_8888 || |
| 23 format == gfx::BufferFormat::BGRX_8888 || |
| 24 format == gfx::BufferFormat::BGRA_8888; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 //////////////////////////////////////////////////////////////////////////////// |
| 30 // SharedMemory, public: |
| 31 |
| 32 SharedMemory::SharedMemory(const base::SharedMemoryHandle& handle) |
| 33 : shared_memory_(handle, true /* read-only */) {} |
| 34 |
| 35 SharedMemory::~SharedMemory() {} |
| 36 |
| 37 scoped_ptr<Buffer> SharedMemory::CreateBuffer(const gfx::Size& size, |
| 38 gfx::BufferFormat format, |
| 39 int offset, |
| 40 int stride) { |
| 41 TRACE_EVENT2("exo", "SharedMemory::CreateBuffer", "size", size.ToString(), |
| 42 "format", static_cast<int>(format)); |
| 43 |
| 44 if (!IsSupportedFormat(format)) { |
| 45 DLOG(WARNING) << "Failed to create shm buffer. Unsupported format 0x" |
| 46 << static_cast<int>(format); |
| 47 return nullptr; |
| 48 } |
| 49 |
| 50 if (gfx::RowSizeForBufferFormat(size.width(), format, 0) != |
| 51 static_cast<size_t>(stride)) { |
| 52 DLOG(WARNING) << "Failed to create shm buffer. Unsupported stride " |
| 53 << stride; |
| 54 return nullptr; |
| 55 } |
| 56 |
| 57 if (offset < 0) { |
| 58 DLOG(WARNING) << "Failed to create shm buffer. Negative offset " << offset; |
| 59 return nullptr; |
| 60 } |
| 61 |
| 62 gfx::GpuMemoryBufferHandle handle; |
| 63 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 64 handle.handle = base::SharedMemory::DuplicateHandle(shared_memory_.handle()); |
| 65 handle.offset = offset; |
| 66 |
| 67 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer = |
| 68 aura::Env::GetInstance() |
| 69 ->context_factory() |
| 70 ->GetGpuMemoryBufferManager() |
| 71 ->CreateGpuMemoryBufferFromHandle(handle, size, format); |
| 72 if (!gpu_memory_buffer) { |
| 73 LOG(ERROR) << "Failed to create GpuMemoryBuffer from handle"; |
| 74 return nullptr; |
| 75 } |
| 76 |
| 77 return make_scoped_ptr(new Buffer(gpu_memory_buffer.Pass())); |
| 78 } |
| 79 |
| 80 } // namespace exo |
OLD | NEW |