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#ifndef CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_
6#define CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "cc/base/cc_export.h"
15#include "cc/resources/release_callback_impl.h"
16#include "cc/resources/resource_format.h"
17#include "cc/resources/texture_mailbox.h"
18#include "ui/gfx/size.h"
19
20namespace media {
21class SkCanvasVideoRenderer;
22class VideoFrame;
23}
24
25namespace cc {
26class ContextProvider;
27class ResourceProvider;
28
29class CC_EXPORT VideoFrameExternalResources {
30 public:
31  // Specifies what type of data is contained in the mailboxes, as well as how
32  // many mailboxes will be present.
33  enum ResourceType {
34    NONE,
35    YUV_RESOURCE,
36    RGB_RESOURCE,
37    STREAM_TEXTURE_RESOURCE,
38    IO_SURFACE,
39
40#if defined(VIDEO_HOLE)
41    // TODO(danakj): Implement this with a solid color layer instead of a video
42    // frame and video layer.
43    HOLE,
44#endif  // defined(VIDEO_HOLE)
45
46    // TODO(danakj): Remove this and abstract TextureMailbox into
47    // "ExternalResource" that can hold a hardware or software backing.
48    SOFTWARE_RESOURCE
49  };
50
51  ResourceType type;
52  std::vector<TextureMailbox> mailboxes;
53  std::vector<ReleaseCallbackImpl> release_callbacks;
54
55  // TODO(danakj): Remove these too.
56  std::vector<unsigned> software_resources;
57  ReleaseCallbackImpl software_release_callback;
58
59  VideoFrameExternalResources();
60  ~VideoFrameExternalResources();
61};
62
63// VideoResourceUpdater is by the video system to produce frame content as
64// resources consumable by the compositor.
65class CC_EXPORT VideoResourceUpdater
66    : public base::SupportsWeakPtr<VideoResourceUpdater> {
67 public:
68  explicit VideoResourceUpdater(ContextProvider* context_provider,
69                                ResourceProvider* resource_provider);
70  ~VideoResourceUpdater();
71
72  VideoFrameExternalResources CreateExternalResourcesFromVideoFrame(
73      const scoped_refptr<media::VideoFrame>& video_frame);
74
75 private:
76  struct PlaneResource {
77    unsigned resource_id;
78    gfx::Size resource_size;
79    ResourceFormat resource_format;
80    gpu::Mailbox mailbox;
81
82    PlaneResource(unsigned resource_id,
83                  const gfx::Size& resource_size,
84                  ResourceFormat resource_format,
85                  gpu::Mailbox mailbox)
86        : resource_id(resource_id),
87          resource_size(resource_size),
88          resource_format(resource_format),
89          mailbox(mailbox) {}
90  };
91
92  void DeleteResource(unsigned resource_id);
93  bool VerifyFrame(const scoped_refptr<media::VideoFrame>& video_frame);
94  VideoFrameExternalResources CreateForHardwarePlanes(
95      const scoped_refptr<media::VideoFrame>& video_frame);
96  VideoFrameExternalResources CreateForSoftwarePlanes(
97      const scoped_refptr<media::VideoFrame>& video_frame);
98
99  struct RecycleResourceData {
100    unsigned resource_id;
101    gfx::Size resource_size;
102    ResourceFormat resource_format;
103    gpu::Mailbox mailbox;
104  };
105  static void RecycleResource(base::WeakPtr<VideoResourceUpdater> updater,
106                              RecycleResourceData data,
107                              uint32 sync_point,
108                              bool lost_resource,
109                              BlockingTaskRunner* main_thread_task_runner);
110  static void ReturnTexture(base::WeakPtr<VideoResourceUpdater> updater,
111                            const scoped_refptr<media::VideoFrame>& video_frame,
112                            uint32 sync_point,
113                            bool lost_resource,
114                            BlockingTaskRunner* main_thread_task_runner);
115
116  ContextProvider* context_provider_;
117  ResourceProvider* resource_provider_;
118  scoped_ptr<media::SkCanvasVideoRenderer> video_renderer_;
119
120  std::vector<unsigned> all_resources_;
121  std::vector<PlaneResource> recycled_resources_;
122
123  DISALLOW_COPY_AND_ASSIGN(VideoResourceUpdater);
124};
125
126}  // namespace cc
127
128#endif  // CC_RESOURCES_VIDEO_RESOURCE_UPDATER_H_
129