Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
dl_test_surface_instance_impeller.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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
6
12
13namespace {
14
15class DlImpellerPixelData : public flutter::testing::DlPixelData {
16 public:
17 explicit DlImpellerPixelData(
18 std::unique_ptr<impeller::testing::Screenshot> screenshot)
19 : screenshot_(std::move(screenshot)) {}
20 ~DlImpellerPixelData() override = default;
21
22 virtual const uint32_t* addr32(uint32_t x, uint32_t y) const override {
23 if (x >= width() || y >= height()) {
24 return nullptr;
25 }
26 fml::SafeMath safe_math;
27 size_t offset = safe_math.mul(screenshot_->GetBytesPerRow(), y);
28 offset += safe_math.mul(x, 4u);
29 if (safe_math.overflow_detected()) {
30 return nullptr;
31 }
32 return reinterpret_cast<const uint32_t*>(screenshot_->GetBytes() + offset);
33 }
34
35 virtual size_t width() const override { return screenshot_->GetWidth(); }
36 virtual size_t height() const override { return screenshot_->GetHeight(); }
37
38 virtual bool write(const std::string& path) const override {
39 return screenshot_->WriteToPNG(path);
40 }
41
42 private:
43 std::unique_ptr<impeller::testing::Screenshot> screenshot_;
44};
45
46} // namespace
47
48namespace flutter {
49namespace testing {
50
52 std::shared_ptr<impeller::Context> context,
53 std::shared_ptr<impeller::Surface> surface)
54 : context_(std::move(context)),
55 surface_(std::move(surface)),
56 aiks_context_(context_, typographer_context_) {}
57
59 std::shared_ptr<impeller::Context> context,
60 std::shared_ptr<impeller::RenderTarget> target)
61 : context_(std::move(context)),
62 target_holder_(std::move(target)),
63 aiks_context_(context_, typographer_context_) {}
64
66
67inline const impeller::RenderTarget&
68DlSurfaceInstanceImpeller::GetRenderTarget() const {
69 if (surface_) {
70 return surface_->GetRenderTarget();
71 }
72 if (target_holder_) {
73 return *target_holder_;
74 }
76}
77
79 if (!builder_.IsEmpty()) {
80 // Use the Build method to clear whatever is in the builder as it is
81 // now irrelevant and ignore the returned DisplayList as it would be
82 // useless to try to render it before a surface clear.
83 std::ignore = builder_.Build();
84 }
85 builder_.Clear(color);
86 DoRenderDisplayList(builder_.Build());
87}
88
90 return &builder_;
91}
92
94 const sk_sp<DisplayList>& display_list) {
95 Flush();
96 DoRenderDisplayList(display_list);
97}
98
100 Flush();
101 if (!context_->FinishQueue()) {
102 FML_LOG(ERROR) << "Impeller backend did not implement FinishQueue";
104 }
105}
106
107inline void DlSurfaceInstanceImpeller::Flush() {
108 if (!builder_.IsEmpty()) {
109 // Render anything accumulated previously by making calls on GetCanvas().
110 DoRenderDisplayList(builder_.Build());
111 }
112}
113
114void DlSurfaceInstanceImpeller::DoRenderDisplayList(
115 const sk_sp<DisplayList>& display_list) {
116 // RenderToTarget requires us to pass in a cull_rect, but we don't want
117 // benchmarks to do extra overhead for culling, so we make a large enough
118 // cull rect that the dispatcher decides to do a regular sequential dispatch.
119 DlRect cull_rect = display_list->GetBounds().Expand(1.0f);
120 impeller::RenderToTarget(aiks_context_.GetContentContext(), GetRenderTarget(),
121 display_list, cull_rect, false, false);
122}
123
125 const {
126 std::unique_ptr<impeller::testing::Screenshot> snapshot =
127 snapshotter_.MakeScreenshot(aiks_context_,
128 GetRenderTarget().GetRenderTargetTexture());
129 return snapshot ? std::make_unique<DlImpellerPixelData>(std::move(snapshot))
130 : nullptr;
131}
132
134 auto texture = GetRenderTarget().GetRenderTargetTexture();
135 // temp_image will not be a snapshot, so we must make a copy of it to
136 // satisfy the demands of the "SnapshotToImage" API.
137 auto temp_image = impeller::DlImageImpeller::Make(texture);
138
139 // Make a temporary "image surface" into which we can copy our current
140 // texture so that we can return a stable snapshot DlImage from the copy.
142 context_, temp_image->width(), temp_image->height(),
143 DlSurfaceProvider::PixelFormat::kN32Premul);
144
145 // Copy the temp_image made from our texture into the image surface.
146 image_surface->GetCanvas()->DrawImage(temp_image, DlPoint(0, 0),
148 image_surface->FlushSubmitCpuSync();
149
150 // Return an image based on the temporary, but stable, image_surface's
151 // texture.
153 image_surface->GetRenderTarget().GetRenderTargetTexture());
154}
155
156bool DlSurfaceInstanceImpeller::SnapshotToFile(std::string& filename) const {
157 return false;
158}
159
161 return GetRenderTarget().GetRenderTargetSize().width;
162}
163
165 return GetRenderTarget().GetRenderTargetSize().height;
166}
167
168std::shared_ptr<impeller::TypographerContext>
169 DlSurfaceInstanceImpeller::typographer_context_ =
171
172impeller::testing::MetalScreenshotter DlSurfaceInstanceImpeller::snapshotter_ =
174
175} // namespace testing
176} // namespace flutter
bool IsEmpty() const
Return true if the builder has not yet recorded any commands.
sk_sp< DisplayList > Build()
Developer-facing API for rendering anything within the engine.
Definition dl_canvas.h:32
void Clear(DlColor color)
Definition dl_canvas.h:104
virtual size_t height() const =0
virtual const uint32_t * addr32(uint32_t x, uint32_t y) const =0
virtual bool write(const std::string &path) const =0
virtual size_t width() const =0
void RenderDisplayList(const sk_sp< DisplayList > &display_list) override
DlSurfaceInstanceImpeller(std::shared_ptr< impeller::Context > context, std::shared_ptr< impeller::Surface > surface)
int height() const override
The height of the underlying surface.
bool SnapshotToFile(std::string &filename) const override
Store a snapshot of this Surface to the file indicated by the filename.
std::unique_ptr< DlPixelData > SnapshotToPixelData() const override
void Clear(const DlColor &color) override
Clear the entire surface to the indicated color.
int width() const override
The width of the underlying surface.
std::unique_ptr< DlSurfaceInstance > MakeOffscreenSurface(size_t width, size_t height, PixelFormat format) const override
size_t mul(size_t x, size_t y)
Definition safe_math.cc:12
bool overflow_detected() const
Definition safe_math.h:17
ContentContext & GetContentContext() const
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
std::shared_ptr< Texture > GetRenderTargetTexture() const
ISize GetRenderTargetSize() const
static std::shared_ptr< TypographerContext > Make()
std::unique_ptr< Screenshot > MakeScreenshot(const AiksContext &aiks_context, const std::shared_ptr< Texture > texture) override
int32_t x
VkSurfaceKHR surface
Definition main.cc:65
uint32_t * target
#define FML_LOG(severity)
Definition logging.h:101
#define FML_UNREACHABLE()
Definition logging.h:128
EGLSurface surface_
FlTexture * texture
double y
impeller::Rect DlRect
impeller::Point DlPoint
bool RenderToTarget(ContentContext &context, RenderTarget render_target, const sk_sp< flutter::DisplayList > &display_list, Rect cull_rect, bool reset_host_buffer, bool is_onscreen)
Render the provided display list to the render target.
Definition ref_ptr.h:261
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition rect.h:652
Type height
Definition size.h:29
Type width
Definition size.h:28