Flutter Engine
The Flutter Engine
blit_command_gles.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
7#include "flutter/fml/closure.h"
8#include "fml/trace_event.h"
13
14namespace impeller {
15
17
18static void DeleteFBO(const ProcTableGLES& gl, GLuint fbo, GLenum type) {
19 if (fbo != GL_NONE) {
20 gl.BindFramebuffer(type, GL_NONE);
21 gl.DeleteFramebuffers(1u, &fbo);
22 }
23};
24
25static std::optional<GLuint> ConfigureFBO(
26 const ProcTableGLES& gl,
27 const std::shared_ptr<Texture>& texture,
28 GLenum fbo_type) {
29 auto handle = TextureGLES::Cast(texture.get())->GetGLHandle();
30 if (!handle.has_value()) {
31 return std::nullopt;
32 }
33
34 if (TextureGLES::Cast(*texture).IsWrapped()) {
35 // The texture is attached to the default FBO, so there's no need to
36 // create/configure one.
37 gl.BindFramebuffer(fbo_type, 0);
38 return 0;
39 }
40
41 GLuint fbo;
42 gl.GenFramebuffers(1u, &fbo);
43 gl.BindFramebuffer(fbo_type, fbo);
44
47 VALIDATION_LOG << "Could not attach texture to framebuffer.";
48 DeleteFBO(gl, fbo, fbo_type);
49 return std::nullopt;
50 }
51
52 if (gl.CheckFramebufferStatus(fbo_type) != GL_FRAMEBUFFER_COMPLETE) {
53 VALIDATION_LOG << "Could not create a complete framebuffer.";
54 DeleteFBO(gl, fbo, fbo_type);
55 return std::nullopt;
56 }
57
58 return fbo;
59};
60
62 default;
63
65 return label;
66}
67
69 const ReactorGLES& reactor) const {
70 const auto& gl = reactor.GetProcTable();
71
72 // glBlitFramebuffer is a GLES3 proc. Since we target GLES2, we need to
73 // emulate the blit when it's not available in the driver.
74 if (!gl.BlitFramebuffer.IsAvailable()) {
75 // TODO(bdero): Emulate the blit using a raster draw call here.
76 FML_LOG(ERROR) << "Texture blit fallback not implemented yet for GLES2.";
77 return false;
78 }
79
80 GLuint read_fbo = GL_NONE;
81 GLuint draw_fbo = GL_NONE;
82 fml::ScopedCleanupClosure delete_fbos([&gl, &read_fbo, &draw_fbo]() {
83 DeleteFBO(gl, read_fbo, GL_READ_FRAMEBUFFER);
84 DeleteFBO(gl, draw_fbo, GL_DRAW_FRAMEBUFFER);
85 });
86
87 {
88 auto read = ConfigureFBO(gl, source, GL_READ_FRAMEBUFFER);
89 if (!read.has_value()) {
90 return false;
91 }
92 read_fbo = read.value();
93 }
94
95 {
96 auto draw = ConfigureFBO(gl, destination, GL_DRAW_FRAMEBUFFER);
97 if (!draw.has_value()) {
98 return false;
99 }
100 draw_fbo = draw.value();
101 }
102
103 gl.Disable(GL_SCISSOR_TEST);
104 gl.Disable(GL_DEPTH_TEST);
105 gl.Disable(GL_STENCIL_TEST);
106
107 gl.BlitFramebuffer(source_region.GetX(), // srcX0
108 source_region.GetY(), // srcY0
109 source_region.GetWidth(), // srcX1
110 source_region.GetHeight(), // srcY1
111 destination_origin.x, // dstX0
112 destination_origin.y, // dstY0
113 source_region.GetWidth(), // dstX1
114 source_region.GetHeight(), // dstY1
115 GL_COLOR_BUFFER_BIT, // mask
116 GL_NEAREST // filter
117 );
118
119 return true;
120};
121
122namespace {
123struct TexImage2DData {
125 GLenum external_format = GL_NONE;
126 GLenum type = GL_NONE;
127 BufferView buffer_view;
128
129 explicit TexImage2DData(PixelFormat pixel_format) {
130 switch (pixel_format) {
132 internal_format = GL_ALPHA;
133 external_format = GL_ALPHA;
134 type = GL_UNSIGNED_BYTE;
135 break;
137 internal_format = GL_RED;
138 external_format = GL_RED;
139 type = GL_UNSIGNED_BYTE;
140 break;
145 internal_format = GL_RGBA;
146 external_format = GL_RGBA;
147 type = GL_UNSIGNED_BYTE;
148 break;
150 internal_format = GL_RGBA;
151 external_format = GL_RGBA;
152 type = GL_FLOAT;
153 break;
155 internal_format = GL_RGBA;
156 external_format = GL_RGBA;
157 type = GL_HALF_FLOAT;
158 break;
160 // Pure stencil textures are only available in OpenGL 4.4+, which is
161 // ~0% of mobile devices. Instead, we use a depth-stencil texture and
162 // only use the stencil component.
163 //
164 // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
166 internal_format = GL_DEPTH_STENCIL;
167 external_format = GL_DEPTH_STENCIL;
168 type = GL_UNSIGNED_INT_24_8;
169 break;
176 return;
177 }
178 is_valid_ = true;
179 }
180
181 TexImage2DData(PixelFormat pixel_format, BufferView p_buffer_view)
182 : TexImage2DData(pixel_format) {
183 buffer_view = std::move(p_buffer_view);
184 }
185
186 bool IsValid() const { return is_valid_; }
187
188 private:
189 bool is_valid_ = false;
190};
191} // namespace
192
194 default;
195
197 return label;
198}
199
201 const ReactorGLES& reactor) const {
202 TextureGLES& texture_gles = TextureGLES::Cast(*destination);
203
204 if (texture_gles.GetType() != TextureGLES::Type::kTexture) {
205 VALIDATION_LOG << "Incorrect texture usage flags for setting contents on "
206 "this texture object.";
207 return false;
208 }
209
210 if (texture_gles.IsWrapped()) {
211 VALIDATION_LOG << "Cannot set the contents of a wrapped texture.";
212 return false;
213 }
214
215 const auto& tex_descriptor = texture_gles.GetTextureDescriptor();
216
217 if (tex_descriptor.size.IsEmpty()) {
218 return true;
219 }
220
221 if (!tex_descriptor.IsValid() ||
223 BytesPerPixelForPixelFormat(tex_descriptor.format) *
225 return false;
226 }
227
229
230 GLenum texture_type;
231 GLenum texture_target;
232 switch (tex_descriptor.type) {
234 texture_type = GL_TEXTURE_2D;
235 texture_target = GL_TEXTURE_2D;
236 break;
238 VALIDATION_LOG << "Multisample texture uploading is not supported for "
239 "the OpenGLES backend.";
240 return false;
242 texture_type = GL_TEXTURE_CUBE_MAP;
243 texture_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice;
244 break;
246 texture_type = GL_TEXTURE_EXTERNAL_OES;
247 texture_target = GL_TEXTURE_EXTERNAL_OES;
248 break;
249 }
250
251 TexImage2DData data = TexImage2DData(tex_descriptor.format, source);
252 if (!data.IsValid()) {
253 VALIDATION_LOG << "Invalid texture format.";
254 return false;
255 }
256
257 auto gl_handle = texture_gles.GetGLHandle();
258 if (!gl_handle.has_value()) {
260 << "Texture was collected before it could be uploaded to the GPU.";
261 return false;
262 }
263 const auto& gl = reactor.GetProcTable();
264 gl.BindTexture(texture_type, gl_handle.value());
265 const GLvoid* tex_data =
266 data.buffer_view.buffer->OnGetContents() + data.buffer_view.range.offset;
267
268 // GL_INVALID_OPERATION if the texture array has not been
269 // defined by a previous glTexImage2D operation.
270 if (!texture_gles.IsSliceInitialized(slice)) {
271 gl.TexImage2D(texture_target, // target
272 0u, // LOD level
273 data.internal_format, // internal format
274 tex_descriptor.size.width, // width
275 tex_descriptor.size.height, // height
276 0u, // border
277 data.external_format, // external format
278 data.type, // type
279 nullptr // data
280 );
281 texture_gles.MarkSliceInitialized(slice);
282 }
283
284 {
285 TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes",
286 std::to_string(data.buffer_view.range.length).c_str());
287 gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
288 gl.TexSubImage2D(texture_target, // target
289 0u, // LOD level
290 destination_region.GetX(), // xoffset
291 destination_region.GetY(), // yoffset
292 destination_region.GetWidth(), // width
293 destination_region.GetHeight(), // height
294 data.external_format, // external format
295 data.type, // type
296 tex_data // data
297
298 );
299 }
300 return true;
301}
302
304 default;
305
307 return label;
308}
309
311 const ReactorGLES& reactor) const {
312 if (source->GetTextureDescriptor().format != PixelFormat::kR8G8B8A8UNormInt) {
313 VALIDATION_LOG << "Only textures with pixel format RGBA are supported yet.";
314 return false;
315 }
316
317 const auto& gl = reactor.GetProcTable();
318
319 GLuint read_fbo = GL_NONE;
320 fml::ScopedCleanupClosure delete_fbos(
321 [&gl, &read_fbo]() { DeleteFBO(gl, read_fbo, GL_READ_FRAMEBUFFER); });
322
323 {
324 auto read = ConfigureFBO(gl, source, GL_READ_FRAMEBUFFER);
325 if (!read.has_value()) {
326 return false;
327 }
328 read_fbo = read.value();
329 }
330
332 .UpdateBufferData([&gl, this](uint8_t* data, size_t length) {
333 gl.ReadPixels(source_region.GetX(), source_region.GetY(),
335 GL_RGBA, GL_UNSIGNED_BYTE, data + destination_offset);
336 });
337
338 return true;
339};
340
342
344 return label;
345}
346
348 auto texture_gles = TextureGLES::Cast(texture.get());
349 if (!texture_gles->GenerateMipmap()) {
350 return false;
351 }
352
353 return true;
354};
355
356} // namespace impeller
static bool read(SkStream *stream, void *buffer, size_t amount)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
GLenum type
GLenum external_format
GLint internal_format
BufferView buffer_view
const char * c_str() const
Definition: SkString.h:133
Wraps a closure that is invoked in the destructor unless released by the caller.
Definition: closure.h:32
static TextureGLES & Cast(Texture &base)
Definition: backend_cast.h:13
void UpdateBufferData(const std::function< void(uint8_t *, size_t length)> &update_buffer_data)
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
Definition: reactor_gles.h:55
const ProcTableGLES & GetProcTable() const
Get the OpenGL proc. table the reactor uses to manage handles.
Definition: reactor_gles.cc:48
bool IsSliceInitialized(size_t slice) const
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type) const
void MarkSliceInitialized(size_t slice) const
std::optional< GLuint > GetGLHandle() const
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
#define FML_LOG(severity)
Definition: logging.h:82
size_t length
FlTexture * texture
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:460
static std::optional< GLuint > ConfigureFBO(const ProcTableGLES &gl, const std::shared_ptr< Texture > &texture, GLenum fbo_type)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
static void DeleteFBO(const ProcTableGLES &gl, GLuint fbo, GLenum type)
gl
Definition: malisc.py:41
static SkString to_string(int n)
Definition: nanobench.cpp:119
bool Encode(const ReactorGLES &reactor) const override
std::shared_ptr< Texture > destination
Definition: blit_command.h:34
bool Encode(const ReactorGLES &reactor) const override
std::shared_ptr< DeviceBuffer > destination
Definition: blit_command.h:27
std::shared_ptr< Texture > source
Definition: blit_command.h:26
bool Encode(const ReactorGLES &reactor) const override
std::shared_ptr< Texture > destination
Definition: blit_command.h:20
std::shared_ptr< Texture > source
Definition: blit_command.h:19
bool Encode(const ReactorGLES &reactor) const override
std::string GetLabel() const override
std::shared_ptr< Texture > texture
Definition: blit_command.h:40
size_t length
Definition: range.h:14
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition: rect.h:327
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:337
constexpr T Area() const
Get the area of the rectangle, equivalent to |GetSize().Area()|.
Definition: rect.h:366
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition: rect.h:323
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:331
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
#define ERROR(message)
Definition: elf_loader.cc:260
#define TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
Definition: trace_event.h:141
#define VALIDATION_LOG
Definition: validation.h:73