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 "media/video/gpu_memory_buffer_video_frame_pool.h" |
| 6 |
| 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> |
| 9 |
| 10 #include <list> |
| 11 #include <utility> |
| 12 |
| 13 #include "base/bind.h" |
| 14 #include "base/containers/stack_container.h" |
| 15 #include "base/location.h" |
| 16 #include "base/memory/linked_ptr.h" |
| 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/trace_event/trace_event.h" |
| 19 #include "gpu/command_buffer/client/gles2_interface.h" |
| 20 #include "media/renderers/gpu_video_accelerator_factories.h" |
| 21 |
| 22 namespace media { |
| 23 |
| 24 // Implementation of a pool of GpuMemoryBuffers used to back VideoFrames. |
| 25 class GpuMemoryBufferVideoFramePool::PoolImpl |
| 26 : public base::RefCountedThreadSafe< |
| 27 GpuMemoryBufferVideoFramePool::PoolImpl> { |
| 28 public: |
| 29 // |task_runner| is associated to the thread where the context of |
| 30 // GLES2Interface returned by |gpu_factories| lives. |
| 31 // |gpu_factories| is an interface to GPU related operation and can be |
| 32 // null. |
| 33 PoolImpl(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 34 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories) |
| 35 : task_runner_(task_runner), gpu_factories_(gpu_factories) {} |
| 36 |
| 37 // Takes a software VideoFrame and returns a VideoFrame backed by native |
| 38 // textures if possible. |
| 39 // The data contained in video_frame is copied into the returned frame. |
| 40 scoped_refptr<VideoFrame> CreateHardwareFrame( |
| 41 const scoped_refptr<VideoFrame>& video_frame); |
| 42 |
| 43 private: |
| 44 friend class base::RefCountedThreadSafe< |
| 45 GpuMemoryBufferVideoFramePool::PoolImpl>; |
| 46 ~PoolImpl(); |
| 47 |
| 48 // Resource to represent a plane. |
| 49 struct PlaneResource { |
| 50 gfx::Size size; |
| 51 scoped_ptr<gfx::GpuMemoryBuffer> gpu_memory_buffer; |
| 52 unsigned texture_id = 0u; |
| 53 unsigned image_id = 0u; |
| 54 gpu::Mailbox mailbox; |
| 55 }; |
| 56 |
| 57 // All the resources needed to compose a frame. |
| 58 struct FrameResources { |
| 59 FrameResources(VideoFrame::Format format, const gfx::Size& size) |
| 60 : format(format), size(size) {} |
| 61 bool in_use = true; |
| 62 VideoFrame::Format format; |
| 63 gfx::Size size; |
| 64 PlaneResource plane_resources[VideoFrame::kMaxPlanes]; |
| 65 }; |
| 66 |
| 67 // Return true if |resources| can be used to represent a frame for |
| 68 // specific |format| and |size|. |
| 69 static bool AreFrameResourcesCompatible(const FrameResources* resources, |
| 70 const gfx::Size& size, |
| 71 VideoFrame::Format format) { |
| 72 return size == resources->size && format == resources->format; |
| 73 } |
| 74 |
| 75 // Get the resources needed for a frame out of the pool, or create them if |
| 76 // necessary. |
| 77 // This also drops the LRU resources that can't be reuse for this frame. |
| 78 FrameResources* GetOrCreateFrameResources(const gfx::Size& size, |
| 79 VideoFrame::Format format); |
| 80 |
| 81 // Callback called when a VideoFrame generated with GetFrameResources is no |
| 82 // longer referenced. |
| 83 // This could be called by any thread. |
| 84 void MailboxHoldersReleased(FrameResources* frame_resources, |
| 85 uint32 sync_point); |
| 86 |
| 87 // Return frame resources to the pool. This has to be called on the thread |
| 88 // where |task_runner| is current. |
| 89 void ReturnFrameResources(FrameResources* frame_resources); |
| 90 |
| 91 // Delete resources. This has to be called on the thread where |task_runner| |
| 92 // is current. |
| 93 static void DeleteFrameResources( |
| 94 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, |
| 95 FrameResources* frame_resources); |
| 96 |
| 97 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 98 scoped_refptr<GpuVideoAcceleratorFactories> gpu_factories_; |
| 99 |
| 100 // Pool of resources. |
| 101 std::list<FrameResources*> resources_pool_; |
| 102 |
| 103 unsigned texture_target_ = GL_TEXTURE_2D; |
| 104 DISALLOW_COPY_AND_ASSIGN(PoolImpl); |
| 105 }; |
| 106 |
| 107 namespace { |
| 108 |
| 109 // Copy a buffer info a GpuMemoryBuffer. |
| 110 // |bytes_per_row| is expected to be less or equal than the strides of the two |
| 111 // buffers. |
| 112 void CopyPlaneToGpuMemoryBuffer(int rows, |
| 113 int bytes_per_row, |
| 114 const uint8* source, |
| 115 int source_stride, |
| 116 gfx::GpuMemoryBuffer* buffer) { |
| 117 TRACE_EVENT2("media", "CopyPlaneToGpuMemoryBuffer", "bytes_per_row", |
| 118 bytes_per_row, "rows", rows); |
| 119 |
| 120 DCHECK(buffer); |
| 121 DCHECK(source); |
| 122 void* data = nullptr; |
| 123 CHECK(buffer->Map(&data)); |
| 124 uint8* mapped_buffer = static_cast<uint8*>(data); |
| 125 int dest_stride = 0; |
| 126 buffer->GetStride(&dest_stride); |
| 127 DCHECK_NE(dest_stride, 0); |
| 128 DCHECK_LE(bytes_per_row, std::abs(dest_stride)); |
| 129 DCHECK_LE(bytes_per_row, source_stride); |
| 130 for (int row = 0; row < rows; ++row) { |
| 131 memcpy(mapped_buffer + dest_stride * row, source + source_stride * row, |
| 132 bytes_per_row); |
| 133 } |
| 134 buffer->Unmap(); |
| 135 } |
| 136 |
| 137 } // unnamed namespace |
| 138 |
| 139 // Creates a VideoFrame backed by native textures starting from a software |
| 140 // VideoFrame. |
| 141 // The data contained in video_frame is copied into the returned VideoFrame. |
| 142 scoped_refptr<VideoFrame> |
| 143 GpuMemoryBufferVideoFramePool::PoolImpl::CreateHardwareFrame( |
| 144 const scoped_refptr<VideoFrame>& video_frame) { |
| 145 if (!gpu_factories_) |
| 146 return video_frame; |
| 147 |
| 148 if (!gpu_factories_->IsTextureRGSupported()) |
| 149 return video_frame; |
| 150 |
| 151 gpu::gles2::GLES2Interface* gles2 = gpu_factories_->GetGLES2Interface(); |
| 152 if (!gles2) |
| 153 return video_frame; |
| 154 |
| 155 VideoFrame::Format format = video_frame->format(); |
| 156 size_t planes = VideoFrame::NumPlanes(format); |
| 157 DCHECK(video_frame->visible_rect().origin().IsOrigin()); |
| 158 gfx::Size size = video_frame->visible_rect().size(); |
| 159 gpu::MailboxHolder mailbox_holders[VideoFrame::kMaxPlanes]; |
| 160 |
| 161 // Acquire resources. Incompatible ones will be dropped from the pool. |
| 162 FrameResources* frame_resources = GetOrCreateFrameResources(size, format); |
| 163 |
| 164 // Set up the planes copying data into it and creating the mailboxes needed |
| 165 // to refer to the textures. |
| 166 for (size_t i = 0; i < planes; ++i) { |
| 167 PlaneResource& plane_resource = frame_resources->plane_resources[i]; |
| 168 CopyPlaneToGpuMemoryBuffer(VideoFrame::Rows(i, format, size.height()), |
| 169 VideoFrame::RowBytes(i, format, size.width()), |
| 170 video_frame->data(i), video_frame->stride(i), |
| 171 plane_resource.gpu_memory_buffer.get()); |
| 172 |
| 173 // Bind the texture and create or rebind the image. |
| 174 gles2->BindTexture(texture_target_, plane_resource.texture_id); |
| 175 |
| 176 if (plane_resource.gpu_memory_buffer && !plane_resource.image_id) { |
| 177 const size_t width = VideoFrame::Columns(i, format, size.width()); |
| 178 const size_t height = VideoFrame::Rows(i, format, size.height()); |
| 179 plane_resource.image_id = gles2->CreateImageCHROMIUM( |
| 180 plane_resource.gpu_memory_buffer->AsClientBuffer(), width, height, |
| 181 GL_R8_EXT); |
| 182 } else { |
| 183 gles2->ReleaseTexImage2DCHROMIUM(texture_target_, |
| 184 plane_resource.image_id); |
| 185 } |
| 186 gles2->BindTexImage2DCHROMIUM(texture_target_, plane_resource.image_id); |
| 187 mailbox_holders[i] = |
| 188 gpu::MailboxHolder(plane_resource.mailbox, texture_target_, 0); |
| 189 } |
| 190 |
| 191 // Insert a sync_point, this is needed to make sure that the textures the |
| 192 // mailboxes refer to will be used only after all the previous commands posted |
| 193 // in the command buffer have been processed. |
| 194 unsigned sync_point = gles2->InsertSyncPointCHROMIUM(); |
| 195 for (size_t i = 0; i < planes; ++i) { |
| 196 mailbox_holders[i].sync_point = sync_point; |
| 197 } |
| 198 |
| 199 // Create the VideoFrame backed by native textures. |
| 200 return VideoFrame::WrapYUV420NativeTextures( |
| 201 mailbox_holders[VideoFrame::kYPlane], |
| 202 mailbox_holders[VideoFrame::kUPlane], |
| 203 mailbox_holders[VideoFrame::kVPlane], |
| 204 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources), |
| 205 size, video_frame->visible_rect(), video_frame->natural_size(), |
| 206 video_frame->timestamp(), video_frame->allow_overlay()); |
| 207 } |
| 208 |
| 209 // Destroy all the resources posting one task per FrameResources |
| 210 // to the |task_runner_|. |
| 211 GpuMemoryBufferVideoFramePool::PoolImpl::~PoolImpl() { |
| 212 // Delete all the resources on the media thread. |
| 213 while (!resources_pool_.empty()) { |
| 214 FrameResources* frame_resources = resources_pool_.front(); |
| 215 resources_pool_.pop_front(); |
| 216 task_runner_->PostTask( |
| 217 FROM_HERE, base::Bind(&PoolImpl::DeleteFrameResources, gpu_factories_, |
| 218 base::Owned(frame_resources))); |
| 219 } |
| 220 } |
| 221 |
| 222 // Tries to find the resources in the pool or create them. |
| 223 // Incompatible resources will be dropped. |
| 224 GpuMemoryBufferVideoFramePool::PoolImpl::FrameResources* |
| 225 GpuMemoryBufferVideoFramePool::PoolImpl::GetOrCreateFrameResources( |
| 226 const gfx::Size& size, |
| 227 VideoFrame::Format format) { |
| 228 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 229 |
| 230 auto it = resources_pool_.begin(); |
| 231 while (it != resources_pool_.end()) { |
| 232 FrameResources* frame_resources = *it; |
| 233 if (!frame_resources->in_use) { |
| 234 if (AreFrameResourcesCompatible(frame_resources, size, format)) { |
| 235 frame_resources->in_use = true; |
| 236 return frame_resources; |
| 237 } else { |
| 238 resources_pool_.erase(it++); |
| 239 DeleteFrameResources(gpu_factories_, frame_resources); |
| 240 delete frame_resources; |
| 241 } |
| 242 } else { |
| 243 it++; |
| 244 } |
| 245 } |
| 246 |
| 247 // Create the resources. |
| 248 gpu::gles2::GLES2Interface* gles2 = gpu_factories_->GetGLES2Interface(); |
| 249 DCHECK(gles2); |
| 250 gles2->ActiveTexture(GL_TEXTURE0); |
| 251 size_t planes = VideoFrame::NumPlanes(format); |
| 252 FrameResources* frame_resources = new FrameResources(format, size); |
| 253 resources_pool_.push_back(frame_resources); |
| 254 for (size_t i = 0; i < planes; ++i) { |
| 255 PlaneResource& plane_resource = frame_resources->plane_resources[i]; |
| 256 const size_t width = VideoFrame::Columns(i, format, size.width()); |
| 257 const size_t height = VideoFrame::Rows(i, format, size.height()); |
| 258 const gfx::Size plane_size(width, height); |
| 259 plane_resource.gpu_memory_buffer = gpu_factories_->AllocateGpuMemoryBuffer( |
| 260 plane_size, gfx::GpuMemoryBuffer::R_8, gfx::GpuMemoryBuffer::MAP); |
| 261 |
| 262 gles2->GenTextures(1, &plane_resource.texture_id); |
| 263 gles2->BindTexture(texture_target_, plane_resource.texture_id); |
| 264 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 265 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 266 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 267 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 268 gles2->GenMailboxCHROMIUM(plane_resource.mailbox.name); |
| 269 gles2->ProduceTextureCHROMIUM(texture_target_, plane_resource.mailbox.name); |
| 270 } |
| 271 return frame_resources; |
| 272 } |
| 273 |
| 274 // static |
| 275 void GpuMemoryBufferVideoFramePool::PoolImpl::DeleteFrameResources( |
| 276 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, |
| 277 FrameResources* frame_resources) { |
| 278 // TODO(dcastagna): As soon as the context lost is dealt with in media, |
| 279 // make sure that we won't execute this callback (use a weak pointer to |
| 280 // the old context). |
| 281 gpu::gles2::GLES2Interface* gles2 = gpu_factories->GetGLES2Interface(); |
| 282 if (!gles2) |
| 283 return; |
| 284 |
| 285 for (PlaneResource& plane_resource : frame_resources->plane_resources) { |
| 286 if (plane_resource.image_id) |
| 287 gles2->DestroyImageCHROMIUM(plane_resource.image_id); |
| 288 if (plane_resource.texture_id) |
| 289 gles2->DeleteTextures(1, &plane_resource.texture_id); |
| 290 } |
| 291 } |
| 292 |
| 293 // Called when a VideoFrame is no longer references. |
| 294 void GpuMemoryBufferVideoFramePool::PoolImpl::MailboxHoldersReleased( |
| 295 FrameResources* frame_resources, |
| 296 uint32 sync_point) { |
| 297 // Return the resource on the media thread. |
| 298 task_runner_->PostTask(FROM_HERE, base::Bind(&PoolImpl::ReturnFrameResources, |
| 299 this, frame_resources)); |
| 300 } |
| 301 |
| 302 // Put back the resoruces in the pool. |
| 303 void GpuMemoryBufferVideoFramePool::PoolImpl::ReturnFrameResources( |
| 304 FrameResources* frame_resources) { |
| 305 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 306 |
| 307 auto it = std::find(resources_pool_.begin(), resources_pool_.end(), |
| 308 frame_resources); |
| 309 DCHECK(it != resources_pool_.end()); |
| 310 // We want the pool to behave in a FIFO way. |
| 311 // This minimizes the chances of locking the buffer that might be |
| 312 // still needed for drawing. |
| 313 std::swap(*it, resources_pool_.back()); |
| 314 frame_resources->in_use = false; |
| 315 } |
| 316 |
| 317 GpuMemoryBufferVideoFramePool::GpuMemoryBufferVideoFramePool( |
| 318 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 319 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories) |
| 320 : pool_impl_(new PoolImpl(task_runner, gpu_factories)) { |
| 321 } |
| 322 |
| 323 GpuMemoryBufferVideoFramePool::~GpuMemoryBufferVideoFramePool() { |
| 324 } |
| 325 |
| 326 scoped_refptr<VideoFrame> |
| 327 GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( |
| 328 const scoped_refptr<VideoFrame>& video_frame) { |
| 329 switch (video_frame->format()) { |
| 330 // Supported cases. |
| 331 case VideoFrame::YV12: |
| 332 case VideoFrame::I420: |
| 333 return pool_impl_->CreateHardwareFrame(video_frame); |
| 334 // Unsupported cases. |
| 335 case media::VideoFrame::YV12A: |
| 336 case media::VideoFrame::YV16: |
| 337 case media::VideoFrame::YV12J: |
| 338 case media::VideoFrame::YV12HD: |
| 339 case media::VideoFrame::YV24: |
| 340 #if defined(VIDEO_HOLE) |
| 341 case media::VideoFrame::HOLE: |
| 342 #endif // defined(VIDEO_HOLE) |
| 343 case media::VideoFrame::ARGB: |
| 344 case media::VideoFrame::NATIVE_TEXTURE: |
| 345 case media::VideoFrame::UNKNOWN: |
| 346 case media::VideoFrame::NV12: |
| 347 break; |
| 348 } |
| 349 return video_frame; |
| 350 } |
| 351 |
| 352 } // namespace media |
OLD | NEW |