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
14
15namespace {
16
17class DlImpellerPixelData : public flutter::testing::DlPixelData {
18 public:
19 explicit DlImpellerPixelData(
20 std::unique_ptr<impeller::testing::Screenshot> screenshot)
21 : screenshot_(std::move(screenshot)) {}
22 ~DlImpellerPixelData() override = default;
23
24 virtual const uint32_t* addr32(uint32_t x, uint32_t y) const override {
25 if (x >= width() || y >= height()) {
26 return nullptr;
27 }
28 fml::SafeMath safe_math;
29 size_t offset = safe_math.mul(screenshot_->GetBytesPerRow(), y);
30 offset += safe_math.mul(x, 4u);
31 if (safe_math.overflow_detected()) {
32 return nullptr;
33 }
34 return reinterpret_cast<const uint32_t*>(screenshot_->GetBytes() + offset);
35 }
36
37 virtual size_t width() const override { return screenshot_->GetWidth(); }
38 virtual size_t height() const override { return screenshot_->GetHeight(); }
39
40 virtual bool write(const std::string& path) const override {
41 return screenshot_->WriteToPNG(path);
42 }
43
44 private:
45 std::unique_ptr<impeller::testing::Screenshot> screenshot_;
46};
47
48} // namespace
49
50namespace flutter {
51namespace testing {
52
54 std::shared_ptr<impeller::Context> context,
55 std::shared_ptr<impeller::Surface> surface)
56 : context_(std::move(context)),
57 surface_(std::move(surface)),
58 aiks_context_(context_, typographer_context_) {}
59
61 std::shared_ptr<impeller::Context> context,
62 std::shared_ptr<impeller::RenderTarget> target)
63 : context_(std::move(context)),
64 target_holder_(std::move(target)),
65 aiks_context_(context_, typographer_context_) {}
66
68
69inline const impeller::RenderTarget&
70DlSurfaceInstanceImpeller::GetRenderTarget() const {
71 if (surface_) {
72 return surface_->GetRenderTarget();
73 }
74 if (target_holder_) {
75 return *target_holder_;
76 }
78}
79
81 if (!builder_.IsEmpty()) {
82 // Use the Build method to clear whatever is in the builder as it is
83 // now irrelevant and ignore the returned DisplayList as it would be
84 // useless to try to render it before a surface clear.
85 std::ignore = builder_.Build();
86 }
87 builder_.Clear(color);
88 DoRenderDisplayList(builder_.Build());
89}
90
92 return &builder_;
93}
94
96 const sk_sp<DisplayList>& display_list) {
97 Flush();
98 DoRenderDisplayList(display_list);
99}
100
102 Flush();
103 if (!context_->FinishQueue()) {
104 FML_LOG(ERROR) << "Impeller backend did not implement FinishQueue";
106 }
107}
108
109inline void DlSurfaceInstanceImpeller::Flush() {
110 if (!builder_.IsEmpty()) {
111 // Render anything accumulated previously by making calls on GetCanvas().
112 DoRenderDisplayList(builder_.Build());
113 }
114}
115
116void DlSurfaceInstanceImpeller::DoRenderDisplayList(
117 const sk_sp<DisplayList>& display_list) {
118 // RenderToTarget requires us to pass in a cull_rect, but we don't want
119 // benchmarks to do extra overhead for culling, so we make a large enough
120 // cull rect that the dispatcher decides to do a regular sequential dispatch.
121 DlRect cull_rect = display_list->GetBounds().Expand(1.0f);
122 impeller::RenderToTarget(aiks_context_.GetContentContext(), GetRenderTarget(),
123 display_list, cull_rect, false, false);
124}
125
127 const {
128 std::shared_ptr<impeller::Context> context = aiks_context_.GetContext();
129 std::unique_ptr<impeller::testing::Screenshot> snapshot =
131 context, GetRenderTarget().GetRenderTargetTexture());
132 return snapshot ? std::make_unique<DlImpellerPixelData>(std::move(snapshot))
133 : nullptr;
134}
135
137 auto texture = GetRenderTarget().GetRenderTargetTexture();
138 // temp_image will not be a snapshot, so we must make a copy of it to
139 // satisfy the demands of the "SnapshotToImage" API.
140 auto temp_image = impeller::DlImageImpeller::Make(texture);
141
142 // Make a temporary "image surface" into which we can copy our current
143 // texture so that we can return a stable snapshot DlImage from the copy.
145 context_, temp_image->width(), temp_image->height(),
146 DlSurfaceProvider::PixelFormat::kN32Premul);
147
148 // Copy the temp_image made from our texture into the image surface.
149 image_surface->GetCanvas()->DrawImage(temp_image, DlPoint(0, 0),
151 image_surface->FlushSubmitCpuSync();
152
153 // Return an image based on the temporary, but stable, image_surface's
154 // texture.
156 image_surface->GetRenderTarget().GetRenderTargetTexture());
157}
158
159bool DlSurfaceInstanceImpeller::SnapshotToFile(std::string& filename) const {
160 return false;
161}
162
164 return GetRenderTarget().GetRenderTargetSize().width;
165}
166
168 return GetRenderTarget().GetRenderTargetSize().height;
169}
170
171std::shared_ptr<impeller::TypographerContext>
172 DlSurfaceInstanceImpeller::typographer_context_ =
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
std::shared_ptr< Context > GetContext() 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()
static std::unique_ptr< Screenshot > MakeScreenshot(std::shared_ptr< Context > &context, const std::shared_ptr< Texture > &texture)
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
std::shared_ptr< ContextGLES > context
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