| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/resources/video_resource_updater.h" | 5 #include "cc/resources/video_resource_updater.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 11 #include "cc/base/util.h" | |
| 12 #include "cc/output/gl_renderer.h" | 11 #include "cc/output/gl_renderer.h" |
| 13 #include "cc/resources/resource_provider.h" | 12 #include "cc/resources/resource_provider.h" |
| 14 #include "gpu/GLES2/gl2extchromium.h" | 13 #include "gpu/GLES2/gl2extchromium.h" |
| 15 #include "gpu/command_buffer/client/gles2_interface.h" | 14 #include "gpu/command_buffer/client/gles2_interface.h" |
| 16 #include "media/base/video_frame.h" | 15 #include "media/base/video_frame.h" |
| 17 #include "media/filters/skcanvas_video_renderer.h" | 16 #include "media/filters/skcanvas_video_renderer.h" |
| 18 #include "third_party/khronos/GLES2/gl2.h" | 17 #include "third_party/khronos/GLES2/gl2.h" |
| 19 #include "third_party/khronos/GLES2/gl2ext.h" | 18 #include "third_party/khronos/GLES2/gl2ext.h" |
| 20 #include "ui/gfx/geometry/size_conversions.h" | 19 #include "ui/gfx/geometry/size_conversions.h" |
| 21 | 20 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 resource_provider_->DeleteResource(plane_resource.resource_id); | 88 resource_provider_->DeleteResource(plane_resource.resource_id); |
| 90 } | 89 } |
| 91 | 90 |
| 92 VideoResourceUpdater::ResourceList::iterator | 91 VideoResourceUpdater::ResourceList::iterator |
| 93 VideoResourceUpdater::AllocateResource(const gfx::Size& plane_size, | 92 VideoResourceUpdater::AllocateResource(const gfx::Size& plane_size, |
| 94 ResourceFormat format, | 93 ResourceFormat format, |
| 95 bool has_mailbox) { | 94 bool has_mailbox) { |
| 96 // TODO(danakj): Abstract out hw/sw resource create/delete from | 95 // TODO(danakj): Abstract out hw/sw resource create/delete from |
| 97 // ResourceProvider and stop using ResourceProvider in this class. | 96 // ResourceProvider and stop using ResourceProvider in this class. |
| 98 const ResourceProvider::ResourceId resource_id = | 97 const ResourceProvider::ResourceId resource_id = |
| 99 resource_provider_->CreateResource( | 98 resource_provider_->CreateResource(plane_size, GL_CLAMP_TO_EDGE, |
| 100 plane_size, GL_CLAMP_TO_EDGE, | 99 ResourceProvider::TextureHintImmutable, |
| 101 ResourceProvider::TEXTURE_HINT_IMMUTABLE, format); | 100 format); |
| 102 if (resource_id == 0) | 101 if (resource_id == 0) |
| 103 return all_resources_.end(); | 102 return all_resources_.end(); |
| 104 | 103 |
| 105 gpu::Mailbox mailbox; | 104 gpu::Mailbox mailbox; |
| 106 if (has_mailbox) { | 105 if (has_mailbox) { |
| 107 DCHECK(context_provider_); | 106 DCHECK(context_provider_); |
| 108 | 107 |
| 109 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); | 108 gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
| 110 | 109 |
| 111 GLC(gl, gl->GenMailboxCHROMIUM(mailbox.name)); | 110 GLC(gl, gl->GenMailboxCHROMIUM(mailbox.name)); |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 } | 310 } |
| 312 | 311 |
| 313 for (size_t i = 0; i < plane_resources.size(); ++i) { | 312 for (size_t i = 0; i < plane_resources.size(); ++i) { |
| 314 PlaneResource& plane_resource = *plane_resources[i]; | 313 PlaneResource& plane_resource = *plane_resources[i]; |
| 315 // Update each plane's resource id with its content. | 314 // Update each plane's resource id with its content. |
| 316 DCHECK_EQ(plane_resource.resource_format, | 315 DCHECK_EQ(plane_resource.resource_format, |
| 317 resource_provider_->yuv_resource_format()); | 316 resource_provider_->yuv_resource_format()); |
| 318 | 317 |
| 319 if (!PlaneResourceMatchesUniqueID(plane_resource, video_frame.get(), i)) { | 318 if (!PlaneResourceMatchesUniqueID(plane_resource, video_frame.get(), i)) { |
| 320 // We need to transfer data from |video_frame| to the plane resource. | 319 // We need to transfer data from |video_frame| to the plane resource. |
| 321 // TODO(reveman): Can use GpuMemoryBuffers here to improve performance. | 320 const uint8_t* input_plane_pixels = video_frame->data(i); |
| 322 | 321 |
| 323 // The |resource_size_pixels| is the size of the resource we want to | 322 gfx::Rect image_rect(0, 0, video_frame->stride(i), |
| 324 // upload to. | 323 plane_resource.resource_size.height()); |
| 325 gfx::Size resource_size_pixels = plane_resource.resource_size; | 324 gfx::Rect source_rect(plane_resource.resource_size); |
| 326 // The |video_stride_pixels| is the width of the video frame we are | 325 resource_provider_->SetPixels(plane_resource.resource_id, |
| 327 // uploading (including non-frame data to fill in the stride). | 326 input_plane_pixels, image_rect, source_rect, |
| 328 size_t video_stride_pixels = video_frame->stride(i); | 327 gfx::Vector2d()); |
| 329 | |
| 330 size_t bytes_per_pixel = BitsPerPixel(plane_resource.resource_format) / 8; | |
| 331 // Use 4-byte row alignment (OpenGL default) for upload performance. | |
| 332 // Assuming that GL_UNPACK_ALIGNMENT has not changed from default. | |
| 333 size_t upload_image_stride = | |
| 334 RoundUp<size_t>(bytes_per_pixel * resource_size_pixels.width(), 4u); | |
| 335 | |
| 336 const uint8_t* pixels; | |
| 337 if (upload_image_stride == video_stride_pixels * bytes_per_pixel) { | |
| 338 pixels = video_frame->data(i); | |
| 339 } else { | |
| 340 // Avoid malloc for each frame/plane if possible. | |
| 341 size_t needed_size = | |
| 342 upload_image_stride * resource_size_pixels.height(); | |
| 343 if (upload_pixels_.size() < needed_size) | |
| 344 upload_pixels_.resize(needed_size); | |
| 345 for (int row = 0; row < resource_size_pixels.height(); ++row) { | |
| 346 uint8_t* dst = &upload_pixels_[upload_image_stride * row]; | |
| 347 const uint8_t* src = video_frame->data(i) + | |
| 348 bytes_per_pixel * video_stride_pixels * row; | |
| 349 memcpy(dst, src, resource_size_pixels.width() * bytes_per_pixel); | |
| 350 } | |
| 351 pixels = &upload_pixels_[0]; | |
| 352 } | |
| 353 | |
| 354 resource_provider_->CopyToResource(plane_resource.resource_id, pixels, | |
| 355 resource_size_pixels); | |
| 356 SetPlaneResourceUniqueId(video_frame.get(), i, &plane_resource); | 328 SetPlaneResourceUniqueId(video_frame.get(), i, &plane_resource); |
| 357 } | 329 } |
| 358 | 330 |
| 359 external_resources.mailboxes.push_back( | 331 external_resources.mailboxes.push_back( |
| 360 TextureMailbox(plane_resource.mailbox, GL_TEXTURE_2D, 0)); | 332 TextureMailbox(plane_resource.mailbox, GL_TEXTURE_2D, 0)); |
| 361 external_resources.release_callbacks.push_back( | 333 external_resources.release_callbacks.push_back( |
| 362 base::Bind(&RecycleResource, AsWeakPtr(), plane_resource.resource_id)); | 334 base::Bind(&RecycleResource, AsWeakPtr(), plane_resource.resource_id)); |
| 363 } | 335 } |
| 364 | 336 |
| 365 external_resources.type = VideoFrameExternalResources::YUV_RESOURCE; | 337 external_resources.type = VideoFrameExternalResources::YUV_RESOURCE; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 resource_it->ref_count = 0; | 427 resource_it->ref_count = 0; |
| 456 updater->DeleteResource(resource_it); | 428 updater->DeleteResource(resource_it); |
| 457 return; | 429 return; |
| 458 } | 430 } |
| 459 | 431 |
| 460 --resource_it->ref_count; | 432 --resource_it->ref_count; |
| 461 DCHECK_GE(resource_it->ref_count, 0); | 433 DCHECK_GE(resource_it->ref_count, 0); |
| 462 } | 434 } |
| 463 | 435 |
| 464 } // namespace cc | 436 } // namespace cc |
| OLD | NEW |