1 // Copyright 2012 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 COMPONENTS_VIZ_COMMON_RESOURCES_TRANSFERABLE_RESOURCE_H_
6 #define COMPONENTS_VIZ_COMMON_RESOURCES_TRANSFERABLE_RESOURCE_H_
7 
8 #include <stdint.h>
9 
10 #include <vector>
11 
12 #include "build/build_config.h"
13 #include "components/viz/common/resources/resource_format.h"
14 #include "components/viz/common/resources/resource_id.h"
15 #include "components/viz/common/resources/shared_bitmap.h"
16 #include "components/viz/common/viz_common_export.h"
17 #include "gpu/command_buffer/common/mailbox_holder.h"
18 #include "gpu/ipc/common/vulkan_ycbcr_info.h"
19 #include "ui/gfx/buffer_types.h"
20 #include "ui/gfx/color_space.h"
21 #include "ui/gfx/geometry/size.h"
22 
23 namespace viz {
24 
25 struct ReturnedResource;
26 
27 struct VIZ_COMMON_EXPORT TransferableResource {
28   TransferableResource();
29   ~TransferableResource();
30 
31   TransferableResource(const TransferableResource& other);
32   TransferableResource& operator=(const TransferableResource& other);
33 
34   ReturnedResource ToReturnedResource() const;
35   static std::vector<ReturnedResource> ReturnResources(
36       const std::vector<TransferableResource>& input);
37 
MakeSoftwareTransferableResource38   static TransferableResource MakeSoftware(const SharedBitmapId& id,
39                                            const gfx::Size& size,
40                                            ResourceFormat format) {
41     TransferableResource r;
42     r.is_software = true;
43     r.mailbox_holder.mailbox = id;
44     r.size = size;
45     r.format = format;
46     return r;
47   }
48 
MakeGLTransferableResource49   static TransferableResource MakeGL(const gpu::Mailbox& mailbox,
50                                      uint32_t filter,
51                                      uint32_t texture_target,
52                                      const gpu::SyncToken& sync_token,
53                                      const gfx::Size& size,
54                                      bool is_overlay_candidate) {
55     TransferableResource r;
56     r.is_software = false;
57     r.filter = filter;
58     r.mailbox_holder.mailbox = mailbox;
59     r.mailbox_holder.texture_target = texture_target;
60     r.mailbox_holder.sync_token = sync_token;
61     r.size = size;
62     r.is_overlay_candidate = is_overlay_candidate;
63     return r;
64   }
65 
66   // TODO(danakj): Some of these fields are only GL, some are only Software,
67   // some are both but used for different purposes (like the mailbox name).
68   // It would be nice to group things together and make it more clear when
69   // they will be used or not, and provide easier access to fields such as the
70   // mailbox that also show the intent for software for GL.
71   // An |id| field that can be unique to this resource. For resources
72   // generated by compositor clients, this |id| may be used for their
73   // own book-keeping but need not be set at all.
74   ResourceId id = 0;
75 
76   // Indicates if the resource is gpu or software backed. If gpu, the
77   // mailbox field is a gpu::Mailbox, else it is a SharedBitmapId.
78   bool is_software = false;
79 
80   // The number of pixels in the gpu mailbox/software bitmap.
81   gfx::Size size;
82 
83   // The format of the pixels in the gpu mailbox/software bitmap. This should
84   // almost always be RGBA_8888 for resources generated by compositor clients,
85   // and must be RGBA_8888 always for software resources.
86   ResourceFormat format = RGBA_8888;
87 
88   // The |mailbox| inside here holds the gpu::Mailbox when this is a gpu
89   // resource, or the SharedBitmapId when it is a software resource.
90   // The |texture_target| and sync_token| inside here only apply for gpu
91   // resources.
92   gpu::MailboxHolder mailbox_holder;
93 
94   // The color space of the pixels in the resource.
95   gfx::ColorSpace color_space;
96 
97   // A gpu resource may be possible to use directly in an overlay if this is
98   // true.
99   bool is_overlay_candidate = false;
100   // For a gpu resource, the filter to use when scaling the resource when
101   // drawing it. Typically GL_LINEAR, or GL_NEAREST if no anti-aliasing
102   // during scaling is desired.
103   uint32_t filter = 0;
104   // If a gpu resource is backed by a GpuMemoryBuffer, then it will be accessed
105   // out-of-band, and a gpu fence needs to be waited on before the resource is
106   // returned and reused.
107   bool read_lock_fences_enabled = false;
108 
109   // YCbCr info for resources backed by YCbCr Vulkan images.
110   base::Optional<gpu::VulkanYCbCrInfo> ycbcr_info;
111 
112 #if defined(OS_ANDROID)
113   // Indicates whether this resource may not be overlayed on Android, since
114   // it's not backed by a SurfaceView.  This may be set in combination with
115   // |is_overlay_candidate|, to find out if switching the resource to a
116   // a SurfaceView would result in overlay promotion.  It's good to find this
117   // out in advance, since one has no fallback path for displaying a
118   // SurfaceView except via promoting it to an overlay.  Ideally, one _could_
119   // promote SurfaceTexture via the overlay path, even if one ended up just
120   // drawing a quad in the compositor.  However, for now, we use this flag to
121   // refuse to promote so that the compositor will draw the quad.
122   bool is_backed_by_surface_texture = false;
123   // Indicates that this resource would like a promotion hint.
124   bool wants_promotion_hint = false;
125 #endif
126 
127   bool operator==(const TransferableResource& o) const {
128     return id == o.id && is_software == o.is_software && size == o.size &&
129            format == o.format &&
130            mailbox_holder.mailbox == o.mailbox_holder.mailbox &&
131            mailbox_holder.sync_token == o.mailbox_holder.sync_token &&
132            mailbox_holder.texture_target == o.mailbox_holder.texture_target &&
133            color_space == o.color_space &&
134            is_overlay_candidate == o.is_overlay_candidate &&
135            filter == o.filter &&
136 #if defined(OS_ANDROID)
137            is_backed_by_surface_texture == o.is_backed_by_surface_texture &&
138            wants_promotion_hint == o.wants_promotion_hint &&
139 #endif
140            read_lock_fences_enabled == o.read_lock_fences_enabled;
141   }
142   bool operator!=(const TransferableResource& o) const { return !(*this == o); }
143 };
144 
145 }  // namespace viz
146 
147 #endif  // COMPONENTS_VIZ_COMMON_RESOURCES_TRANSFERABLE_RESOURCE_H_
148