-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
scenic_surface.h
205 lines (160 loc) · 6.88 KB
/
scenic_surface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_OZONE_PLATFORM_SCENIC_SCENIC_SURFACE_H_
#define UI_OZONE_PLATFORM_SCENIC_SCENIC_SURFACE_H_
#include <fuchsia/ui/scenic/cpp/fidl.h>
#include <lib/ui/scenic/cpp/resources.h>
#include <lib/ui/scenic/cpp/session.h>
#include <vulkan/vulkan.h>
#include <memory>
#include <unordered_map>
#include <vector>
#include "base/containers/circular_deque.h"
#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_checker.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/native_pixmap.h"
#include "ui/gfx/native_pixmap_handle.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/overlay_transform.h"
#include "ui/ozone/platform/scenic/safe_presenter.h"
#include "ui/ozone/public/platform_window_surface.h"
namespace ui {
class ScenicSurfaceFactory;
class SysmemBufferManager;
// Holder for Scenic resources backing rendering surface.
//
// This object creates some simple Scenic resources for containing a window's
// texture, and attaches them to the parent View (by sending an IPC to the
// browser process).
//
// The texture is updated through an image pipe.
class ScenicSurface : public PlatformWindowSurface {
public:
ScenicSurface(
ScenicSurfaceFactory* scenic_surface_factory,
SysmemBufferManager* sysmem_buffer_manager,
gfx::AcceleratedWidget window,
scenic::SessionPtrAndListenerRequest sesion_and_listener_request);
ScenicSurface(const ScenicSurface&) = delete;
ScenicSurface& operator=(const ScenicSurface&) = delete;
~ScenicSurface() override;
// PlatformWindowSurface overrides.
void Present(scoped_refptr<gfx::NativePixmap> primary_plane_pixmap,
std::vector<ui::OverlayPlane> overlays,
std::vector<gfx::GpuFenceHandle> acquire_fences,
std::vector<gfx::GpuFenceHandle> release_fences,
SwapCompletionCallback completion_callback,
BufferPresentedCallback presentation_callback) override;
// Allocates a new NativePixmap for the primary plane. The first time is
// invoked |image_pipe_| will be initialized.
scoped_refptr<gfx::NativePixmap> AllocatePrimaryPlanePixmap(
VkDevice vk_device,
const gfx::Size& size,
gfx::BufferFormat buffer_format);
// Sets the texture of the surface to a new image pipe.
void SetTextureToNewImagePipe(
fidl::InterfaceRequest<fuchsia::images::ImagePipe2> image_pipe_request);
// Sets the texture of the surface to an image resource.
void SetTextureToImage(const scenic::Image& image);
// Creates a View for this surface, and returns a ViewHolderToken handle
// that can be used to attach it into a scene graph.
mojo::PlatformHandle CreateView();
void OnScenicEvents(std::vector<fuchsia::ui::scenic::Event> events);
void AssertBelongsToCurrentThread() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
scenic::Session* scenic_session() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return &scenic_session_;
}
SafePresenter* safe_presenter() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
return &safe_presenter_;
}
private:
struct PresentationState {
int presented_frame_ordinal;
base::TimeTicks presentation_time;
base::TimeDelta interval;
};
struct PresentedFrame {
PresentedFrame(uint32_t ordinal,
uint32_t image_id,
scoped_refptr<gfx::NativePixmap> primary_plane,
SwapCompletionCallback completion_callback,
BufferPresentedCallback presentation_callback);
PresentedFrame(PresentedFrame&& other);
PresentedFrame& operator=(PresentedFrame&& other);
~PresentedFrame();
uint32_t ordinal;
uint32_t image_id;
// Ensures the pixmap is not destroyed until after frame is presented.
scoped_refptr<gfx::NativePixmap> primary_plane;
SwapCompletionCallback completion_callback;
BufferPresentedCallback presentation_callback;
};
void InitializeImagePipe();
// Removes a buffer collection registered with |image_pipe_| when it's no
// longer needed.
void RemoveBufferCollection(
gfx::SysmemBufferCollectionId buffer_collection_id);
void OnPresentComplete(fuchsia::images::PresentationInfo presentation_info);
void UpdateViewHolderScene();
void PresentEmptyImage();
scenic::Session scenic_session_;
std::unique_ptr<scenic::View> parent_;
// Used for safely queueing Present() operations on |scenic_session_|.
SafePresenter safe_presenter_;
fuchsia::images::ImagePipe2Ptr image_pipe_;
// Unique ids to be used as both image and uint32_t buffer collection ids.
uint32_t next_unique_id_ = 0;
// Mapping between the SysmemBufferCollectionId stored in NativePixmapHandles
// and uint32_t id registered with image pipe.
base::flat_map<gfx::SysmemBufferCollectionId, uint32_t>
buffer_collection_to_image_id_;
// Ordinal that will be assigned to the next frame. Ordinals are used to
// calculate frame position relative to the current frame stored in
// |presentation_state_|. They will wrap around when reaching 2^32, but the
// math used to calculate relative position will still work as expected.
uint32_t next_frame_ordinal_ = 0;
// Presentation information received from ImagePipe after rendering a frame.
// Used to calculate target presentation time for the frames presented in the
// future.
absl::optional<PresentationState> presentation_state_;
base::TimeTicks last_frame_present_time_;
base::circular_deque<PresentedFrame> pending_frames_;
std::vector<zx::event> release_fences_from_last_present_;
// Scenic resources used for the primary plane, that is not an overlay.
scenic::ShapeNode main_shape_;
scenic::Material main_material_;
gfx::SizeF main_shape_size_;
ScenicSurfaceFactory* const scenic_surface_factory_;
SysmemBufferManager* const sysmem_buffer_manager_;
const gfx::AcceleratedWidget window_;
struct OverlayViewInfo {
OverlayViewInfo(scenic::Session* scenic_session,
fuchsia::ui::views::ViewHolderToken view_holder_token);
scenic::ViewHolder view_holder;
scenic::EntityNode entity_node;
int plane_z_order = 0;
gfx::Rect display_bounds;
gfx::RectF crop_rect;
gfx::OverlayTransform plane_transform;
// Used only in `Present()` in order to update `visible`.
bool should_be_visible = false;
};
std::unordered_map<gfx::SysmemBufferCollectionId,
OverlayViewInfo,
base::UnguessableTokenHash>
overlay_views_;
THREAD_CHECKER(thread_checker_);
base::WeakPtrFactory<ScenicSurface> weak_ptr_factory_{this};
};
} // namespace ui
#endif // UI_OZONE_PLATFORM_SCENIC_SCENIC_SURFACE_H_