forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpu_surface_gl_impeller.cc
More file actions
177 lines (147 loc) · 5.28 KB
/
gpu_surface_gl_impeller.cc
File metadata and controls
177 lines (147 loc) · 5.28 KB
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/gpu/gpu_surface_gl_impeller.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/impeller/display_list/dl_dispatcher.h"
#include "flutter/impeller/renderer/backend/gles/surface_gles.h"
#include "flutter/impeller/renderer/renderer.h"
namespace flutter {
GPUSurfaceGLImpeller::GPUSurfaceGLImpeller(
GPUSurfaceGLDelegate* delegate,
std::shared_ptr<impeller::Context> context)
: weak_factory_(this) {
if (delegate == nullptr) {
return;
}
if (!context || !context->IsValid()) {
return;
}
auto renderer = std::make_shared<impeller::Renderer>(context);
if (!renderer->IsValid()) {
return;
}
auto aiks_context = std::make_shared<impeller::AiksContext>(context);
if (!aiks_context->IsValid()) {
return;
}
delegate_ = delegate;
impeller_context_ = std::move(context);
impeller_renderer_ = std::move(renderer);
aiks_context_ = std::move(aiks_context);
is_valid_ = true;
}
// |Surface|
GPUSurfaceGLImpeller::~GPUSurfaceGLImpeller() = default;
// |Surface|
bool GPUSurfaceGLImpeller::IsValid() {
return is_valid_;
}
// |Surface|
std::unique_ptr<SurfaceFrame> GPUSurfaceGLImpeller::AcquireFrame(
const SkISize& size) {
if (!IsValid()) {
FML_LOG(ERROR) << "OpenGL surface was invalid.";
return nullptr;
}
auto swap_callback = [weak = weak_factory_.GetWeakPtr(),
delegate = delegate_]() -> bool {
if (weak) {
GLPresentInfo present_info = {
.fbo_id = 0u,
.frame_damage = std::nullopt,
// TODO (https://github.com/flutter/flutter/issues/105597): wire-up
// presentation time to impeller backend.
.presentation_time = std::nullopt,
.buffer_damage = std::nullopt,
};
delegate->GLContextPresent(present_info);
}
return true;
};
auto context_switch = delegate_->GLContextMakeCurrent();
if (!context_switch->GetResult()) {
FML_LOG(ERROR)
<< "Could not make the context current to acquire the frame.";
return nullptr;
}
GLFrameInfo frame_info = {static_cast<uint32_t>(size.width()),
static_cast<uint32_t>(size.height())};
const GLFBOInfo fbo_info = delegate_->GLContextFBO(frame_info);
auto surface = impeller::SurfaceGLES::WrapFBO(
impeller_context_, // context
swap_callback, // swap_callback
fbo_info.fbo_id, // fbo
impeller::PixelFormat::kR8G8B8A8UNormInt, // color_format
impeller::ISize{size.width(), size.height()} // fbo_size
);
SurfaceFrame::SubmitCallback submit_callback =
fml::MakeCopyable([renderer = impeller_renderer_, //
aiks_context = aiks_context_, //
surface = std::move(surface) //
](SurfaceFrame& surface_frame, DlCanvas* canvas) mutable -> bool {
if (!aiks_context) {
return false;
}
auto display_list = surface_frame.BuildDisplayList();
if (!display_list) {
FML_LOG(ERROR) << "Could not build display list for surface frame.";
return false;
}
auto cull_rect =
surface->GetTargetRenderPassDescriptor().GetRenderTargetSize();
impeller::DlDispatcher impeller_dispatcher;
display_list->Dispatch(
impeller_dispatcher,
SkIRect::MakeWH(cull_rect.width, cull_rect.height));
auto picture = impeller_dispatcher.EndRecordingAsPicture();
return renderer->Render(
std::move(surface),
fml::MakeCopyable(
[aiks_context, picture = std::move(picture)](
impeller::RenderTarget& render_target) -> bool {
return aiks_context->Render(picture, render_target);
}));
});
return std::make_unique<SurfaceFrame>(
nullptr, // surface
delegate_->GLContextFramebufferInfo(), // framebuffer info
submit_callback, // submit callback
size, // frame size
std::move(context_switch), // context result
true // display list fallback
);
}
// |Surface|
SkMatrix GPUSurfaceGLImpeller::GetRootTransformation() const {
// This backend does not currently support root surface transformations. Just
// return identity.
return {};
}
// |Surface|
GrDirectContext* GPUSurfaceGLImpeller::GetContext() {
// Impeller != Skia.
return nullptr;
}
// |Surface|
std::unique_ptr<GLContextResult>
GPUSurfaceGLImpeller::MakeRenderContextCurrent() {
return delegate_->GLContextMakeCurrent();
}
// |Surface|
bool GPUSurfaceGLImpeller::ClearRenderContext() {
return delegate_->GLContextClearCurrent();
}
bool GPUSurfaceGLImpeller::AllowsDrawingWhenGpuDisabled() const {
return delegate_->AllowsDrawingWhenGpuDisabled();
}
// |Surface|
bool GPUSurfaceGLImpeller::EnableRasterCache() const {
return false;
}
// |Surface|
std::shared_ptr<impeller::AiksContext> GPUSurfaceGLImpeller::GetAiksContext()
const {
return aiks_context_;
}
} // namespace flutter