Flutter Impeller
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 
14 namespace impeller {
15 
17 
18 static 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 
25 static 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 
122 namespace {
123 struct TexImage2DData {
124  GLint internal_format = 0;
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() ||
222  source.range.length !=
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
impeller::PixelFormat::kS8UInt
@ kS8UInt
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::ReactorGLES::GetProcTable
const ProcTableGLES & GetProcTable() const
Get the OpenGL proc. table the reactor uses to manage handles.
Definition: reactor_gles.cc:48
point.h
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::BlitCopyTextureToTextureCommandGLES::Encode
bool Encode(const ReactorGLES &reactor) const override
Definition: blit_command_gles.cc:68
impeller::PixelFormat::kB10G10R10A10XR
@ kB10G10R10A10XR
impeller::PixelFormat::kB8G8R8A8UNormIntSRGB
@ kB8G8R8A8UNormIntSRGB
impeller::PixelFormat::kA8UNormInt
@ kA8UNormInt
impeller::DeleteFBO
static void DeleteFBO(const ProcTableGLES &gl, GLuint fbo, GLenum type)
Definition: blit_command_gles.cc:18
impeller::BlitCopyBufferToTextureCommandGLES::Encode
bool Encode(const ReactorGLES &reactor) const override
Definition: blit_command_gles.cc:200
external_format
GLenum external_format
Definition: blit_command_gles.cc:125
impeller::PixelFormat::kR8UNormInt
@ kR8UNormInt
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
impeller::TextureGLES::MarkSliceInitialized
void MarkSliceInitialized(size_t slice) const
Definition: texture_gles.cc:456
impeller::ConfigureFBO
static std::optional< GLuint > ConfigureFBO(const ProcTableGLES &gl, const std::shared_ptr< Texture > &texture, GLenum fbo_type)
Definition: blit_command_gles.cc:25
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::Texture::GetTextureDescriptor
const TextureDescriptor & GetTextureDescriptor() const
Definition: texture.cc:57
impeller::BlitCopyTextureToTextureCommand::destination
std::shared_ptr< Texture > destination
Definition: blit_command.h:20
impeller::TextureGLES::GetGLHandle
std::optional< GLuint > GetGLHandle() const
Definition: texture_gles.cc:424
texture_gles.h
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:127
impeller::TRect::GetX
constexpr Type GetX() const
Returns the X coordinate of the upper left corner, equivalent to |GetOrigin().x|.
Definition: rect.h:323
impeller::BlitCopyBufferToTextureCommand::destination_region
IRect destination_region
Definition: blit_command.h:35
impeller::BufferView::range
Range range
Definition: buffer_view.h:17
impeller::TRect::GetHeight
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:337
impeller::TextureGLES::AttachmentType::kColor0
@ kColor0
validation.h
impeller::BlitCopyTextureToTextureCommand::destination_origin
IPoint destination_origin
Definition: blit_command.h:22
impeller::PixelFormat::kD32FloatS8UInt
@ kD32FloatS8UInt
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
impeller::BlitCopyTextureToTextureCommandGLES::~BlitCopyTextureToTextureCommandGLES
~BlitCopyTextureToTextureCommandGLES() override
impeller::TextureCoordinateSystem::kUploadFromHost
@ kUploadFromHost
impeller::TextureGLES::IsSliceInitialized
bool IsSliceInitialized(size_t slice) const
Definition: texture_gles.cc:460
impeller::BlitGenerateMipmapCommand::texture
std::shared_ptr< Texture > texture
Definition: blit_command.h:40
impeller::TextureType::kTexture2DMultisample
@ kTexture2DMultisample
device_buffer_gles.h
impeller::BlitCommand::label
std::string label
Definition: blit_command.h:15
impeller::BlitGenerateMipmapCommandGLES::Encode
bool Encode(const ReactorGLES &reactor) const override
Definition: blit_command_gles.cc:347
impeller::BlitCopyBufferToTextureCommand::source
BufferView source
Definition: blit_command.h:33
internal_format
GLint internal_format
Definition: blit_command_gles.cc:124
impeller::BytesPerPixelForPixelFormat
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:460
impeller::PixelFormat::kR8G8UNormInt
@ kR8G8UNormInt
impeller::BlitCopyBufferToTextureCommand::slice
uint32_t slice
Definition: blit_command.h:36
impeller::TRect::GetWidth
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:331
impeller::PixelFormat::kB10G10R10XR
@ kB10G10R10XR
impeller::BlitCopyTextureToBufferCommand::destination_offset
size_t destination_offset
Definition: blit_command.h:29
impeller::PixelFormat::kD24UnormS8Uint
@ kD24UnormS8Uint
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::TextureType::kTextureCube
@ kTextureCube
impeller::BlitCopyTextureToBufferCommand::destination
std::shared_ptr< DeviceBuffer > destination
Definition: blit_command.h:27
impeller::BlitCopyTextureToBufferCommandGLES::GetLabel
std::string GetLabel() const override
Definition: blit_command_gles.cc:306
impeller::PixelFormat::kR16G16B16A16Float
@ kR16G16B16A16Float
impeller::ProcTableGLES
Definition: proc_table_gles.h:228
impeller::BlitCopyTextureToTextureCommand::source
std::shared_ptr< Texture > source
Definition: blit_command.h:19
impeller::BlitCopyTextureToBufferCommandGLES::Encode
bool Encode(const ReactorGLES &reactor) const override
Definition: blit_command_gles.cc:310
impeller::TextureType::kTexture2D
@ kTexture2D
impeller::PixelFormat::kR32G32B32A32Float
@ kR32G32B32A32Float
impeller::DeviceBufferGLES::UpdateBufferData
void UpdateBufferData(const std::function< void(uint8_t *, size_t length)> &update_buffer_data)
Definition: device_buffer_gles.cc:117
impeller::TPoint::x
Type x
Definition: point.h:30
blit_command_gles.h
impeller::PixelFormat::kUnknown
@ kUnknown
impeller::BlitGenerateMipmapCommandGLES::GetLabel
std::string GetLabel() const override
Definition: blit_command_gles.cc:343
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::TextureGLES::GetType
Type GetType() const
Definition: texture_gles.cc:498
impeller::BlitCopyTextureToBufferCommandGLES::~BlitCopyTextureToBufferCommandGLES
~BlitCopyTextureToBufferCommandGLES() override
impeller::PixelFormat::kR8G8B8A8UNormIntSRGB
@ kR8G8B8A8UNormIntSRGB
impeller::BlitCopyBufferToTextureCommandGLES::~BlitCopyBufferToTextureCommandGLES
~BlitCopyBufferToTextureCommandGLES() override
impeller::BackendCast< TextureGLES, Texture >::Cast
static TextureGLES & Cast(Texture &base)
Definition: backend_cast.h:13
impeller::BlitCopyTextureToBufferCommand::source
std::shared_ptr< Texture > source
Definition: blit_command.h:26
impeller::BlitCopyTextureToTextureCommand::source_region
IRect source_region
Definition: blit_command.h:21
impeller::TRect::Area
constexpr T Area() const
Get the area of the rectangle, equivalent to |GetSize().Area()|.
Definition: rect.h:366
impeller::ReactorGLES
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
Definition: reactor_gles.h:55
impeller::TextureGLES::IsWrapped
IsWrapped
Definition: texture_gles.h:27
impeller::PixelFormat::kB10G10R10XRSRGB
@ kB10G10R10XRSRGB
impeller::TextureGLES
Definition: texture_gles.h:17
impeller::Range::length
size_t length
Definition: range.h:14
impeller::PixelFormat::kB8G8R8A8UNormInt
@ kB8G8R8A8UNormInt
impeller::TextureGLES::Type::kTexture
@ kTexture
impeller::BlitGenerateMipmapCommandGLES::~BlitGenerateMipmapCommandGLES
~BlitGenerateMipmapCommandGLES() override
impeller::BlitCopyTextureToTextureCommandGLES::GetLabel
std::string GetLabel() const override
Definition: blit_command_gles.cc:64
impeller::TRect::GetY
constexpr Type GetY() const
Returns the Y coordinate of the upper left corner, equivalent to |GetOrigin().y|.
Definition: rect.h:327
impeller
Definition: aiks_blend_unittests.cc:18
impeller::BlitCopyTextureToBufferCommand::source_region
IRect source_region
Definition: blit_command.h:28
impeller::BlitCopyBufferToTextureCommand::destination
std::shared_ptr< Texture > destination
Definition: blit_command.h:34
impeller::BlitCopyBufferToTextureCommandGLES::GetLabel
std::string GetLabel() const override
Definition: blit_command_gles.cc:196
impeller::TextureGLES::SetAsFramebufferAttachment
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type) const
Definition: texture_gles.cc:513
impeller::BlitEncodeGLES::~BlitEncodeGLES
virtual ~BlitEncodeGLES()