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