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