| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <blimp/client/feature/compositor/blimp_gpu_memory_buffer_manager.h> | |
| 6 #include <GLES2/gl2.h> | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/numerics/safe_conversions.h" | |
| 15 #include "ui/gfx/buffer_format_util.h" | |
| 16 #include "ui/gfx/gpu_memory_buffer.h" | |
| 17 | |
| 18 namespace blimp { | |
| 19 namespace client { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { | |
| 24 public: | |
| 25 GpuMemoryBufferImpl(const gfx::Size& size, | |
| 26 gfx::BufferFormat format, | |
| 27 std::unique_ptr<base::SharedMemory> shared_memory, | |
| 28 size_t offset, | |
| 29 size_t stride) | |
| 30 : size_(size), | |
| 31 format_(format), | |
| 32 shared_memory_(std::move(shared_memory)), | |
| 33 offset_(offset), | |
| 34 stride_(stride), | |
| 35 mapped_(false) {} | |
| 36 | |
| 37 // Overridden from gfx::GpuMemoryBuffer: | |
| 38 bool Map() override { | |
| 39 DCHECK(!mapped_); | |
| 40 DCHECK_EQ(stride_, gfx::RowSizeForBufferFormat(size_.width(), format_, 0)); | |
| 41 if (!shared_memory_->Map(offset_ + | |
| 42 gfx::BufferSizeForBufferFormat(size_, format_))) | |
| 43 return false; | |
| 44 mapped_ = true; | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 void* memory(size_t plane) override { | |
| 49 DCHECK(mapped_); | |
| 50 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
| 51 return reinterpret_cast<uint8_t*>(shared_memory_->memory()) + offset_ + | |
| 52 gfx::BufferOffsetForBufferFormat(size_, format_, plane); | |
| 53 } | |
| 54 | |
| 55 void Unmap() override { | |
| 56 DCHECK(mapped_); | |
| 57 shared_memory_->Unmap(); | |
| 58 mapped_ = false; | |
| 59 } | |
| 60 | |
| 61 gfx::Size GetSize() const override { return size_; } | |
| 62 | |
| 63 gfx::BufferFormat GetFormat() const override { return format_; } | |
| 64 | |
| 65 int stride(size_t plane) const override { | |
| 66 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
| 67 return base::checked_cast<int>(gfx::RowSizeForBufferFormat( | |
| 68 size_.width(), format_, static_cast<int>(plane))); | |
| 69 } | |
| 70 | |
| 71 gfx::GpuMemoryBufferId GetId() const override { | |
| 72 return gfx::GpuMemoryBufferId(0); | |
| 73 } | |
| 74 | |
| 75 gfx::GpuMemoryBufferHandle GetHandle() const override { | |
| 76 gfx::GpuMemoryBufferHandle handle; | |
| 77 handle.type = gfx::SHARED_MEMORY_BUFFER; | |
| 78 handle.handle = shared_memory_->handle(); | |
| 79 handle.offset = base::checked_cast<uint32_t>(offset_); | |
| 80 handle.stride = base::checked_cast<int32_t>(stride_); | |
| 81 return handle; | |
| 82 } | |
| 83 | |
| 84 ClientBuffer AsClientBuffer() override { | |
| 85 return reinterpret_cast<ClientBuffer>(this); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 const gfx::Size size_; | |
| 90 gfx::BufferFormat format_; | |
| 91 std::unique_ptr<base::SharedMemory> shared_memory_; | |
| 92 size_t offset_; | |
| 93 size_t stride_; | |
| 94 bool mapped_; | |
| 95 }; | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 BlimpGpuMemoryBufferManager::BlimpGpuMemoryBufferManager() {} | |
| 100 | |
| 101 BlimpGpuMemoryBufferManager::~BlimpGpuMemoryBufferManager() {} | |
| 102 | |
| 103 // static | |
| 104 cc::BufferToTextureTargetMap | |
| 105 BlimpGpuMemoryBufferManager::GetDefaultBufferToTextureTargetMap() { | |
| 106 cc::BufferToTextureTargetMap image_targets; | |
| 107 for (int usage_idx = 0; usage_idx <= static_cast<int>(gfx::BufferUsage::LAST); | |
| 108 ++usage_idx) { | |
| 109 gfx::BufferUsage usage = static_cast<gfx::BufferUsage>(usage_idx); | |
| 110 for (int format_idx = 0; | |
| 111 format_idx <= static_cast<int>(gfx::BufferFormat::LAST); | |
| 112 ++format_idx) { | |
| 113 gfx::BufferFormat format = static_cast<gfx::BufferFormat>(format_idx); | |
| 114 image_targets.insert(cc::BufferToTextureTargetMap::value_type( | |
| 115 cc::BufferToTextureTargetKey(usage, format), GL_TEXTURE_2D)); | |
| 116 } | |
| 117 } | |
| 118 return image_targets; | |
| 119 } | |
| 120 | |
| 121 std::unique_ptr<gfx::GpuMemoryBuffer> | |
| 122 BlimpGpuMemoryBufferManager::AllocateGpuMemoryBuffer( | |
| 123 const gfx::Size& size, | |
| 124 gfx::BufferFormat format, | |
| 125 gfx::BufferUsage usage, | |
| 126 gpu::SurfaceHandle surface_handle) { | |
| 127 DCHECK_EQ(gpu::kNullSurfaceHandle, surface_handle) | |
| 128 << "Blimp should not be allocating scanout buffers"; | |
| 129 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | |
| 130 const size_t buffer_size = gfx::BufferSizeForBufferFormat(size, format); | |
| 131 if (!shared_memory->CreateAnonymous(buffer_size)) | |
| 132 return nullptr; | |
| 133 return base::WrapUnique<gfx::GpuMemoryBuffer>(new GpuMemoryBufferImpl( | |
| 134 size, format, std::move(shared_memory), 0, | |
| 135 base::checked_cast<int>( | |
| 136 gfx::RowSizeForBufferFormat(size.width(), format, 0)))); | |
| 137 } | |
| 138 | |
| 139 std::unique_ptr<gfx::GpuMemoryBuffer> | |
| 140 BlimpGpuMemoryBufferManager::CreateGpuMemoryBufferFromHandle( | |
| 141 const gfx::GpuMemoryBufferHandle& handle, | |
| 142 const gfx::Size& size, | |
| 143 gfx::BufferFormat format) { | |
| 144 if (handle.type != gfx::SHARED_MEMORY_BUFFER) | |
| 145 return nullptr; | |
| 146 | |
| 147 return base::WrapUnique<gfx::GpuMemoryBuffer>(new GpuMemoryBufferImpl( | |
| 148 size, format, base::MakeUnique<base::SharedMemory>(handle.handle, false), | |
| 149 handle.offset, handle.stride)); | |
| 150 } | |
| 151 | |
| 152 gfx::GpuMemoryBuffer* | |
| 153 BlimpGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer( | |
| 154 ClientBuffer buffer) { | |
| 155 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer); | |
| 156 } | |
| 157 | |
| 158 void BlimpGpuMemoryBufferManager::SetDestructionSyncToken( | |
| 159 gfx::GpuMemoryBuffer* buffer, | |
| 160 const gpu::SyncToken& sync_token) { | |
| 161 NOTIMPLEMENTED(); | |
| 162 } | |
| 163 | |
| 164 } // namespace client | |
| 165 } // namespace blimp | |
| OLD | NEW |