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.
5 #ifndef CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_
6 #define CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/time/time.h"
15 #include "cc/base/cc_export.h"
16 #include "cc/resources/release_callback_impl.h"
17 #include "cc/resources/resource_format.h"
18 #include "cc/resources/texture_mailbox.h"
19 #include "ui/gfx/geometry/size.h"
22 class SkCanvasVideoRenderer
;
27 class ContextProvider
;
28 class ResourceProvider
;
30 class CC_EXPORT VideoFrameExternalResources
{
32 // Specifies what type of data is contained in the mailboxes, as well as how
33 // many mailboxes will be present.
39 STREAM_TEXTURE_RESOURCE
,
42 #if defined(VIDEO_HOLE)
43 // TODO(danakj): Implement this with a solid color layer instead of a video
44 // frame and video layer.
46 #endif // defined(VIDEO_HOLE)
48 // TODO(danakj): Remove this and abstract TextureMailbox into
49 // "ExternalResource" that can hold a hardware or software backing.
54 std::vector
<TextureMailbox
> mailboxes
;
55 std::vector
<ReleaseCallbackImpl
> release_callbacks
;
56 bool read_lock_fences_enabled
;
58 // TODO(danakj): Remove these too.
59 std::vector
<unsigned> software_resources
;
60 ReleaseCallbackImpl software_release_callback
;
62 VideoFrameExternalResources();
63 ~VideoFrameExternalResources();
66 // VideoResourceUpdater is used by the video system to produce frame content as
67 // resources consumable by the compositor.
68 class CC_EXPORT VideoResourceUpdater
69 : public base::SupportsWeakPtr
<VideoResourceUpdater
> {
71 VideoResourceUpdater(ContextProvider
* context_provider
,
72 ResourceProvider
* resource_provider
);
73 ~VideoResourceUpdater();
75 VideoFrameExternalResources
CreateExternalResourcesFromVideoFrame(
76 const scoped_refptr
<media::VideoFrame
>& video_frame
);
79 struct PlaneResource
{
81 gfx::Size resource_size
;
82 ResourceFormat resource_format
;
84 // The balance between the number of times this resource has been returned
85 // from CreateForSoftwarePlanes vs released in RecycleResource.
87 // These last three members will be used for identifying the data stored in
88 // this resource, and uniquely identifies a media::VideoFrame plane. The
89 // frame pointer will only be used for pointer comparison, i.e. the
90 // underlying data will not be accessed.
91 const void* frame_ptr
;
93 base::TimeDelta timestamp
;
95 PlaneResource(unsigned resource_id
,
96 const gfx::Size
& resource_size
,
97 ResourceFormat resource_format
,
98 gpu::Mailbox mailbox
);
101 static bool PlaneResourceMatchesUniqueID(const PlaneResource
& plane_resource
,
102 const media::VideoFrame
* video_frame
,
105 static void SetPlaneResourceUniqueId(const media::VideoFrame
* video_frame
,
107 PlaneResource
* plane_resource
);
109 // This needs to be a container where iterators can be erased without
110 // invalidating other iterators.
111 typedef std::list
<PlaneResource
> ResourceList
;
112 ResourceList::iterator
AllocateResource(const gfx::Size
& plane_size
,
113 ResourceFormat format
,
115 void DeleteResource(ResourceList::iterator resource_it
);
116 bool VerifyFrame(const scoped_refptr
<media::VideoFrame
>& video_frame
);
117 VideoFrameExternalResources
CreateForHardwarePlanes(
118 const scoped_refptr
<media::VideoFrame
>& video_frame
);
119 VideoFrameExternalResources
CreateForSoftwarePlanes(
120 const scoped_refptr
<media::VideoFrame
>& video_frame
);
122 static void RecycleResource(base::WeakPtr
<VideoResourceUpdater
> updater
,
123 unsigned resource_id
,
126 BlockingTaskRunner
* main_thread_task_runner
);
127 static void ReturnTexture(base::WeakPtr
<VideoResourceUpdater
> updater
,
128 const scoped_refptr
<media::VideoFrame
>& video_frame
,
131 BlockingTaskRunner
* main_thread_task_runner
);
133 ContextProvider
* context_provider_
;
134 ResourceProvider
* resource_provider_
;
135 scoped_ptr
<media::SkCanvasVideoRenderer
> video_renderer_
;
136 std::vector
<uint8_t> upload_pixels_
;
138 // Recycle resources so that we can reduce the number of allocations and
140 ResourceList all_resources_
;
142 DISALLOW_COPY_AND_ASSIGN(VideoResourceUpdater
);
147 #endif // CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_