James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/resources/video_resource_updater.h" |
| 6 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/debug/trace_event.h" |
| 11 | #include "cc/output/gl_renderer.h" |
| 12 | #include "cc/resources/resource_provider.h" |
| 13 | #include "gpu/GLES2/gl2extchromium.h" |
| 14 | #include "gpu/command_buffer/client/gles2_interface.h" |
| 15 | #include "media/base/video_frame.h" |
| 16 | #include "media/filters/skcanvas_video_renderer.h" |
| 17 | #include "third_party/khronos/GLES2/gl2.h" |
| 18 | #include "third_party/khronos/GLES2/gl2ext.h" |
James Robinson | 30d547e | 2014-10-23 18:20:06 -0700 | [diff] [blame] | 19 | #include "ui/gfx/geometry/size_conversions.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 20 | |
| 21 | namespace cc { |
| 22 | |
| 23 | namespace { |
| 24 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 25 | const ResourceFormat kRGBResourceFormat = RGBA_8888; |
| 26 | |
| 27 | class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { |
| 28 | public: |
| 29 | explicit SyncPointClientImpl(gpu::gles2::GLES2Interface* gl) : gl_(gl) {} |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 30 | ~SyncPointClientImpl() override {} |
| 31 | uint32 InsertSyncPoint() override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 32 | return GLC(gl_, gl_->InsertSyncPointCHROMIUM()); |
| 33 | } |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 34 | void WaitSyncPoint(uint32 sync_point) override { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 35 | GLC(gl_, gl_->WaitSyncPointCHROMIUM(sync_point)); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | gpu::gles2::GLES2Interface* gl_; |
| 40 | }; |
| 41 | |
| 42 | } // namespace |
| 43 | |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 44 | VideoResourceUpdater::PlaneResource::PlaneResource( |
| 45 | unsigned int resource_id, |
| 46 | const gfx::Size& resource_size, |
| 47 | ResourceFormat resource_format, |
| 48 | gpu::Mailbox mailbox) |
| 49 | : resource_id(resource_id), |
| 50 | resource_size(resource_size), |
| 51 | resource_format(resource_format), |
| 52 | mailbox(mailbox), |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 53 | ref_count(0), |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 54 | frame_ptr(nullptr), |
| 55 | plane_index(0) { |
| 56 | } |
| 57 | |
| 58 | bool VideoResourceUpdater::PlaneResourceMatchesUniqueID( |
| 59 | const PlaneResource& plane_resource, |
| 60 | const media::VideoFrame* video_frame, |
| 61 | int plane_index) { |
| 62 | return plane_resource.frame_ptr == video_frame && |
| 63 | plane_resource.plane_index == plane_index && |
| 64 | plane_resource.timestamp == video_frame->timestamp(); |
| 65 | } |
| 66 | |
| 67 | void VideoResourceUpdater::SetPlaneResourceUniqueId( |
| 68 | const media::VideoFrame* video_frame, |
| 69 | int plane_index, |
| 70 | PlaneResource* plane_resource) { |
| 71 | plane_resource->frame_ptr = video_frame; |
| 72 | plane_resource->plane_index = plane_index; |
| 73 | plane_resource->timestamp = video_frame->timestamp(); |
| 74 | } |
| 75 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 76 | VideoFrameExternalResources::VideoFrameExternalResources() : type(NONE) {} |
| 77 | |
| 78 | VideoFrameExternalResources::~VideoFrameExternalResources() {} |
| 79 | |
| 80 | VideoResourceUpdater::VideoResourceUpdater(ContextProvider* context_provider, |
| 81 | ResourceProvider* resource_provider) |
| 82 | : context_provider_(context_provider), |
| 83 | resource_provider_(resource_provider) { |
| 84 | } |
| 85 | |
| 86 | VideoResourceUpdater::~VideoResourceUpdater() { |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 87 | for (const PlaneResource& plane_resource : all_resources_) |
| 88 | resource_provider_->DeleteResource(plane_resource.resource_id); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 91 | VideoResourceUpdater::ResourceList::iterator |
| 92 | VideoResourceUpdater::AllocateResource(const gfx::Size& plane_size, |
| 93 | ResourceFormat format, |
| 94 | bool has_mailbox) { |
| 95 | // TODO(danakj): Abstract out hw/sw resource create/delete from |
| 96 | // ResourceProvider and stop using ResourceProvider in this class. |
| 97 | const ResourceProvider::ResourceId resource_id = |
| 98 | resource_provider_->CreateResource(plane_size, GL_CLAMP_TO_EDGE, |
| 99 | ResourceProvider::TextureHintImmutable, |
| 100 | format); |
| 101 | if (resource_id == 0) |
| 102 | return all_resources_.end(); |
| 103 | |
| 104 | gpu::Mailbox mailbox; |
| 105 | if (has_mailbox) { |
| 106 | DCHECK(context_provider_); |
| 107 | |
| 108 | gpu::gles2::GLES2Interface* gl = context_provider_->ContextGL(); |
| 109 | |
| 110 | GLC(gl, gl->GenMailboxCHROMIUM(mailbox.name)); |
| 111 | ResourceProvider::ScopedWriteLockGL lock(resource_provider_, resource_id); |
| 112 | GLC(gl, gl->ProduceTextureDirectCHROMIUM(lock.texture_id(), GL_TEXTURE_2D, |
| 113 | mailbox.name)); |
| 114 | } |
| 115 | all_resources_.push_front( |
| 116 | PlaneResource(resource_id, plane_size, format, mailbox)); |
| 117 | return all_resources_.begin(); |
| 118 | } |
| 119 | |
| 120 | void VideoResourceUpdater::DeleteResource(ResourceList::iterator resource_it) { |
| 121 | DCHECK_EQ(resource_it->ref_count, 0); |
| 122 | resource_provider_->DeleteResource(resource_it->resource_id); |
| 123 | all_resources_.erase(resource_it); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | VideoFrameExternalResources VideoResourceUpdater:: |
| 127 | CreateExternalResourcesFromVideoFrame( |
| 128 | const scoped_refptr<media::VideoFrame>& video_frame) { |
| 129 | if (!VerifyFrame(video_frame)) |
| 130 | return VideoFrameExternalResources(); |
| 131 | |
| 132 | if (video_frame->format() == media::VideoFrame::NATIVE_TEXTURE) |
| 133 | return CreateForHardwarePlanes(video_frame); |
| 134 | else |
| 135 | return CreateForSoftwarePlanes(video_frame); |
| 136 | } |
| 137 | |
| 138 | bool VideoResourceUpdater::VerifyFrame( |
| 139 | const scoped_refptr<media::VideoFrame>& video_frame) { |
| 140 | switch (video_frame->format()) { |
| 141 | // Acceptable inputs. |
| 142 | case media::VideoFrame::YV12: |
| 143 | case media::VideoFrame::I420: |
| 144 | case media::VideoFrame::YV12A: |
| 145 | case media::VideoFrame::YV16: |
| 146 | case media::VideoFrame::YV12J: |
| 147 | case media::VideoFrame::YV24: |
| 148 | case media::VideoFrame::NATIVE_TEXTURE: |
| 149 | #if defined(VIDEO_HOLE) |
| 150 | case media::VideoFrame::HOLE: |
| 151 | #endif // defined(VIDEO_HOLE) |
James Robinson | d753aca | 2015-01-12 13:07:20 -0800 | [diff] [blame] | 152 | case media::VideoFrame::ARGB: |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 153 | return true; |
| 154 | |
| 155 | // Unacceptable inputs. ¯\(°_o)/¯ |
| 156 | case media::VideoFrame::UNKNOWN: |
| 157 | case media::VideoFrame::NV12: |
| 158 | break; |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // For frames that we receive in software format, determine the dimensions of |
| 164 | // each plane in the frame. |
| 165 | static gfx::Size SoftwarePlaneDimension( |
| 166 | const scoped_refptr<media::VideoFrame>& input_frame, |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 167 | bool software_compositor, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 168 | size_t plane_index) { |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 169 | if (!software_compositor) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 170 | return media::VideoFrame::PlaneSize( |
| 171 | input_frame->format(), plane_index, input_frame->coded_size()); |
| 172 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 173 | return input_frame->coded_size(); |
| 174 | } |
| 175 | |
| 176 | VideoFrameExternalResources VideoResourceUpdater::CreateForSoftwarePlanes( |
| 177 | const scoped_refptr<media::VideoFrame>& video_frame) { |
| 178 | TRACE_EVENT0("cc", "VideoResourceUpdater::CreateForSoftwarePlanes"); |
| 179 | media::VideoFrame::Format input_frame_format = video_frame->format(); |
| 180 | |
| 181 | #if defined(VIDEO_HOLE) |
| 182 | if (input_frame_format == media::VideoFrame::HOLE) { |
| 183 | VideoFrameExternalResources external_resources; |
| 184 | external_resources.type = VideoFrameExternalResources::HOLE; |
| 185 | return external_resources; |
| 186 | } |
| 187 | #endif // defined(VIDEO_HOLE) |
| 188 | |
| 189 | // Only YUV software video frames are supported. |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 190 | if (input_frame_format != media::VideoFrame::YV12 && |
| 191 | input_frame_format != media::VideoFrame::I420 && |
| 192 | input_frame_format != media::VideoFrame::YV12A && |
| 193 | input_frame_format != media::VideoFrame::YV12J && |
| 194 | input_frame_format != media::VideoFrame::YV16 && |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 195 | input_frame_format != media::VideoFrame::YV24) { |
| 196 | NOTREACHED() << input_frame_format; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 197 | return VideoFrameExternalResources(); |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 198 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 199 | |
| 200 | bool software_compositor = context_provider_ == NULL; |
| 201 | |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 202 | ResourceFormat output_resource_format = |
| 203 | resource_provider_->yuv_resource_format(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 204 | size_t output_plane_count = media::VideoFrame::NumPlanes(input_frame_format); |
| 205 | |
| 206 | // TODO(skaslev): If we're in software compositing mode, we do the YUV -> RGB |
| 207 | // conversion here. That involves an extra copy of each frame to a bitmap. |
| 208 | // Obviously, this is suboptimal and should be addressed once ubercompositor |
| 209 | // starts shaping up. |
| 210 | if (software_compositor) { |
| 211 | output_resource_format = kRGBResourceFormat; |
| 212 | output_plane_count = 1; |
| 213 | } |
| 214 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 215 | // Drop recycled resources that are the wrong format. |
| 216 | for (auto it = all_resources_.begin(); it != all_resources_.end();) { |
| 217 | if (it->ref_count == 0 && it->resource_format != output_resource_format) |
| 218 | DeleteResource(it++); |
| 219 | else |
| 220 | ++it; |
| 221 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 222 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 223 | const int max_resource_size = resource_provider_->max_texture_size(); |
| 224 | std::vector<ResourceList::iterator> plane_resources; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 225 | for (size_t i = 0; i < output_plane_count; ++i) { |
| 226 | gfx::Size output_plane_resource_size = |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 227 | SoftwarePlaneDimension(video_frame, software_compositor, i); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 228 | if (output_plane_resource_size.IsEmpty() || |
| 229 | output_plane_resource_size.width() > max_resource_size || |
| 230 | output_plane_resource_size.height() > max_resource_size) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 231 | break; |
| 232 | } |
| 233 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 234 | // Try recycle a previously-allocated resource. |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 235 | ResourceList::iterator resource_it = all_resources_.end(); |
| 236 | for (auto it = all_resources_.begin(); it != all_resources_.end(); ++it) { |
| 237 | if (it->resource_size == output_plane_resource_size && |
| 238 | it->resource_format == output_resource_format) { |
| 239 | if (PlaneResourceMatchesUniqueID(*it, video_frame.get(), i)) { |
| 240 | // Bingo, we found a resource that already contains the data we are |
| 241 | // planning to put in it. It's safe to reuse it even if |
| 242 | // resource_provider_ holds some references to it, because those |
| 243 | // references are read-only. |
| 244 | resource_it = it; |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 245 | break; |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // This extra check is needed because resources backed by SharedMemory |
| 249 | // are not ref-counted, unlike mailboxes. Full discussion in |
| 250 | // codereview.chromium.org/145273021. |
| 251 | const bool in_use = |
| 252 | software_compositor && |
| 253 | resource_provider_->InUseByConsumer(it->resource_id); |
| 254 | if (it->ref_count == 0 && !in_use) { |
| 255 | // We found a resource with the correct size that we can overwrite. |
| 256 | resource_it = it; |
| 257 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 261 | // Check if we need to allocate a new resource. |
| 262 | if (resource_it == all_resources_.end()) { |
| 263 | resource_it = |
| 264 | AllocateResource(output_plane_resource_size, output_resource_format, |
| 265 | !software_compositor); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 266 | } |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 267 | if (resource_it == all_resources_.end()) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 268 | break; |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 269 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 270 | ++resource_it->ref_count; |
| 271 | plane_resources.push_back(resource_it); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 274 | if (plane_resources.size() != output_plane_count) { |
| 275 | // Allocation failed, nothing will be returned so restore reference counts. |
| 276 | for (ResourceList::iterator resource_it : plane_resources) |
| 277 | --resource_it->ref_count; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 278 | return VideoFrameExternalResources(); |
| 279 | } |
| 280 | |
| 281 | VideoFrameExternalResources external_resources; |
| 282 | |
| 283 | if (software_compositor) { |
| 284 | DCHECK_EQ(plane_resources.size(), 1u); |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 285 | PlaneResource& plane_resource = *plane_resources[0]; |
| 286 | DCHECK_EQ(plane_resource.resource_format, kRGBResourceFormat); |
| 287 | DCHECK(plane_resource.mailbox.IsZero()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 288 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 289 | if (!PlaneResourceMatchesUniqueID(plane_resource, video_frame.get(), 0)) { |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 290 | // We need to transfer data from |video_frame| to the plane resource. |
| 291 | if (!video_renderer_) |
| 292 | video_renderer_.reset(new media::SkCanvasVideoRenderer); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 293 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 294 | ResourceProvider::ScopedWriteLockSoftware lock( |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 295 | resource_provider_, plane_resource.resource_id); |
James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame] | 296 | SkCanvas canvas(lock.sk_bitmap()); |
| 297 | video_renderer_->Copy(video_frame, &canvas); |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 298 | SetPlaneResourceUniqueId(video_frame.get(), 0, &plane_resource); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 301 | external_resources.software_resources.push_back(plane_resource.resource_id); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 302 | external_resources.software_release_callback = |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 303 | base::Bind(&RecycleResource, AsWeakPtr(), plane_resource.resource_id); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 304 | external_resources.type = VideoFrameExternalResources::SOFTWARE_RESOURCE; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 305 | return external_resources; |
| 306 | } |
| 307 | |
| 308 | for (size_t i = 0; i < plane_resources.size(); ++i) { |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 309 | PlaneResource& plane_resource = *plane_resources[i]; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 310 | // Update each plane's resource id with its content. |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 311 | DCHECK_EQ(plane_resource.resource_format, |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 312 | resource_provider_->yuv_resource_format()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 313 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 314 | if (!PlaneResourceMatchesUniqueID(plane_resource, video_frame.get(), i)) { |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 315 | // We need to transfer data from |video_frame| to the plane resource. |
| 316 | const uint8_t* input_plane_pixels = video_frame->data(i); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 317 | |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 318 | gfx::Rect image_rect(0, 0, video_frame->stride(i), |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 319 | plane_resource.resource_size.height()); |
| 320 | gfx::Rect source_rect(plane_resource.resource_size); |
| 321 | resource_provider_->SetPixels(plane_resource.resource_id, |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 322 | input_plane_pixels, image_rect, source_rect, |
| 323 | gfx::Vector2d()); |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 324 | SetPlaneResourceUniqueId(video_frame.get(), i, &plane_resource); |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 325 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 326 | |
| 327 | external_resources.mailboxes.push_back( |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 328 | TextureMailbox(plane_resource.mailbox, GL_TEXTURE_2D, 0)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 329 | external_resources.release_callbacks.push_back( |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 330 | base::Bind(&RecycleResource, AsWeakPtr(), plane_resource.resource_id)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | external_resources.type = VideoFrameExternalResources::YUV_RESOURCE; |
| 334 | return external_resources; |
| 335 | } |
| 336 | |
| 337 | // static |
| 338 | void VideoResourceUpdater::ReturnTexture( |
| 339 | base::WeakPtr<VideoResourceUpdater> updater, |
| 340 | const scoped_refptr<media::VideoFrame>& video_frame, |
| 341 | uint32 sync_point, |
| 342 | bool lost_resource, |
| 343 | BlockingTaskRunner* main_thread_task_runner) { |
| 344 | // TODO(dshwang) this case should be forwarded to the decoder as lost |
| 345 | // resource. |
| 346 | if (lost_resource || !updater.get()) |
| 347 | return; |
| 348 | // VideoFrame::UpdateReleaseSyncPoint() creates new sync point using the same |
| 349 | // GL context which created the given |sync_point|, so discard the |
| 350 | // |sync_point|. |
| 351 | SyncPointClientImpl client(updater->context_provider_->ContextGL()); |
| 352 | video_frame->UpdateReleaseSyncPoint(&client); |
| 353 | } |
| 354 | |
| 355 | VideoFrameExternalResources VideoResourceUpdater::CreateForHardwarePlanes( |
| 356 | const scoped_refptr<media::VideoFrame>& video_frame) { |
| 357 | TRACE_EVENT0("cc", "VideoResourceUpdater::CreateForHardwarePlanes"); |
| 358 | media::VideoFrame::Format frame_format = video_frame->format(); |
| 359 | |
| 360 | DCHECK_EQ(frame_format, media::VideoFrame::NATIVE_TEXTURE); |
| 361 | if (frame_format != media::VideoFrame::NATIVE_TEXTURE) |
| 362 | return VideoFrameExternalResources(); |
| 363 | |
| 364 | if (!context_provider_) |
| 365 | return VideoFrameExternalResources(); |
| 366 | |
| 367 | const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); |
| 368 | VideoFrameExternalResources external_resources; |
| 369 | switch (mailbox_holder->texture_target) { |
| 370 | case GL_TEXTURE_2D: |
| 371 | external_resources.type = VideoFrameExternalResources::RGB_RESOURCE; |
| 372 | break; |
| 373 | case GL_TEXTURE_EXTERNAL_OES: |
| 374 | external_resources.type = |
| 375 | VideoFrameExternalResources::STREAM_TEXTURE_RESOURCE; |
| 376 | break; |
| 377 | case GL_TEXTURE_RECTANGLE_ARB: |
| 378 | external_resources.type = VideoFrameExternalResources::IO_SURFACE; |
| 379 | break; |
| 380 | default: |
| 381 | NOTREACHED(); |
| 382 | return VideoFrameExternalResources(); |
| 383 | } |
| 384 | |
| 385 | external_resources.mailboxes.push_back( |
| 386 | TextureMailbox(mailbox_holder->mailbox, |
| 387 | mailbox_holder->texture_target, |
| 388 | mailbox_holder->sync_point)); |
| 389 | external_resources.release_callbacks.push_back( |
| 390 | base::Bind(&ReturnTexture, AsWeakPtr(), video_frame)); |
| 391 | return external_resources; |
| 392 | } |
| 393 | |
| 394 | // static |
| 395 | void VideoResourceUpdater::RecycleResource( |
| 396 | base::WeakPtr<VideoResourceUpdater> updater, |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 397 | ResourceProvider::ResourceId resource_id, |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 398 | uint32 sync_point, |
| 399 | bool lost_resource, |
| 400 | BlockingTaskRunner* main_thread_task_runner) { |
| 401 | if (!updater.get()) { |
| 402 | // Resource was already deleted. |
| 403 | return; |
| 404 | } |
| 405 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 406 | const ResourceList::iterator resource_it = std::find_if( |
| 407 | updater->all_resources_.begin(), updater->all_resources_.end(), |
| 408 | [resource_id](const PlaneResource& plane_resource) { |
| 409 | return plane_resource.resource_id == resource_id; |
| 410 | }); |
| 411 | if (resource_it == updater->all_resources_.end()) |
| 412 | return; |
| 413 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 414 | ContextProvider* context_provider = updater->context_provider_; |
| 415 | if (context_provider && sync_point) { |
| 416 | GLC(context_provider->ContextGL(), |
| 417 | context_provider->ContextGL()->WaitSyncPointCHROMIUM(sync_point)); |
| 418 | } |
| 419 | |
| 420 | if (lost_resource) { |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 421 | resource_it->ref_count = 0; |
| 422 | updater->DeleteResource(resource_it); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 423 | return; |
| 424 | } |
| 425 | |
Etienne Membrives | b1556b3 | 2014-12-16 13:56:09 +0100 | [diff] [blame] | 426 | --resource_it->ref_count; |
| 427 | DCHECK_GE(resource_it->ref_count, 0); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | } // namespace cc |