1 // Copyright 2014 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_SERVICE_DISPLAY_OVERLAY_CANDIDATE_H_
6 #define COMPONENTS_VIZ_SERVICE_DISPLAY_OVERLAY_CANDIDATE_H_
7 
8 #include <map>
9 #include <vector>
10 
11 #include "base/containers/flat_map.h"
12 #include "build/build_config.h"
13 #include "components/viz/common/quads/render_pass.h"
14 #include "components/viz/common/resources/resource_id.h"
15 #include "components/viz/service/viz_service_export.h"
16 #include "gpu/command_buffer/common/mailbox.h"
17 #include "ui/gfx/buffer_types.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/rect_f.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/gfx/overlay_transform.h"
22 #include "ui/gfx/transform.h"
23 
24 namespace gfx {
25 class Rect;
26 }
27 
28 namespace viz {
29 class DisplayResourceProvider;
30 class StreamVideoDrawQuad;
31 class TextureDrawQuad;
32 class VideoHoleDrawQuad;
33 
34 class VIZ_SERVICE_EXPORT OverlayCandidate {
35  public:
36   // Returns true and fills in |candidate| if |draw_quad| is of a known quad
37   // type and contains an overlayable resource.
38   static bool FromDrawQuad(DisplayResourceProvider* resource_provider,
39                            const SkMatrix44& output_color_matrix,
40                            const DrawQuad* quad,
41                            OverlayCandidate* candidate);
42   // Returns true if |quad| will not block quads underneath from becoming
43   // an overlay.
44   static bool IsInvisibleQuad(const DrawQuad* quad);
45 
46   // Returns true if any of the quads in the list given by |quad_list_begin|
47   // and |quad_list_end| are visible and on top of |candidate|.
48   static bool IsOccluded(const OverlayCandidate& candidate,
49                          QuadList::ConstIterator quad_list_begin,
50                          QuadList::ConstIterator quad_list_end);
51 
52   // Returns true if any of the quads in the list given by |quad_list_begin|
53   // and |quad_list_end| have a filter associated and occlude |candidate|.
54   static bool IsOccludedByFilteredQuad(
55       const OverlayCandidate& candidate,
56       QuadList::ConstIterator quad_list_begin,
57       QuadList::ConstIterator quad_list_end,
58       const base::flat_map<RenderPassId, cc::FilterOperations*>&
59           render_pass_backdrop_filters);
60 
61   // Returns true if the |quad| cannot be displayed on the main plane. This is
62   // used in conjuction with protected content that can't be GPU composited and
63   // will be shown via an overlay.
64   static bool RequiresOverlay(const DrawQuad* quad);
65 
66   OverlayCandidate();
67   OverlayCandidate(const OverlayCandidate& other);
68   ~OverlayCandidate();
69 
70   // Transformation to apply to layer during composition.
71   gfx::OverlayTransform transform;
72   // Format of the buffer to scanout.
73   gfx::BufferFormat format;
74   // ColorSpace of the buffer for scanout.
75   gfx::ColorSpace color_space;
76   // Size of the resource, in pixels.
77   gfx::Size resource_size_in_pixels;
78   // Rect on the display to position the overlay to. Implementer must convert
79   // to integer coordinates if setting |overlay_handled| to true.
80   gfx::RectF display_rect;
81   // Crop within the buffer to be placed inside |display_rect|.
82   gfx::RectF uv_rect;
83   // Clip rect in the target content space after composition.
84   gfx::Rect clip_rect;
85   // If the quad is clipped after composition.
86   bool is_clipped;
87   // If the quad doesn't require blending.
88   bool is_opaque;
89   // The quad's occluding damage rect is empty.
90   bool no_occluding_damage;
91   // Texture resource to present in an overlay.
92   unsigned resource_id;
93   // Mailbox from resource_id. It is used by SkiaRenderer.
94   gpu::Mailbox mailbox;
95 
96 #if defined(OS_ANDROID)
97   // For candidates from StreamVideoDrawQuads, this records whether the quad is
98   // marked as being backed by a SurfaceTexture or not.  If so, it's not really
99   // promotable to an overlay.
100   bool is_backed_by_surface_texture;
101 
102   // Filled in by the OverlayCandidateValidator to indicate whether this is a
103   // promotable candidate or not.
104   bool is_promotable_hint;
105 #endif
106 
107   // Stacking order of the overlay plane relative to the main surface,
108   // which is 0. Signed to allow for "underlays".
109   int plane_z_order;
110   // True if the overlay does not have any visible quads on top of it. Set by
111   // the strategy so the OverlayProcessor can consider subtracting damage caused
112   // by underlay quads.
113   bool is_unoccluded;
114 
115   // To be modified by the implementer if this candidate can go into
116   // an overlay.
117   bool overlay_handled;
118 
119   // Gpu fence to wait for before overlay is ready for display.
120   unsigned gpu_fence_id;
121 
122  private:
123   static bool FromDrawQuadResource(DisplayResourceProvider* resource_provider,
124                                    const DrawQuad* quad,
125                                    ResourceId resource_id,
126                                    bool y_flipped,
127                                    OverlayCandidate* candidate);
128   static bool FromTextureQuad(DisplayResourceProvider* resource_provider,
129                               const TextureDrawQuad* quad,
130                               OverlayCandidate* candidate);
131   static bool FromStreamVideoQuad(DisplayResourceProvider* resource_provider,
132                                   const StreamVideoDrawQuad* quad,
133                                   OverlayCandidate* candidate);
134   static bool FromVideoHoleQuad(DisplayResourceProvider* resource_provider,
135                                 const VideoHoleDrawQuad* quad,
136                                 OverlayCandidate* candidate);
137 };
138 
139 using OverlayCandidateList = std::vector<OverlayCandidate>;
140 
141 }  // namespace viz
142 
143 #endif  // COMPONENTS_VIZ_SERVICE_DISPLAY_OVERLAY_CANDIDATE_H_
144