|
| 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 | + |
| 5 | +#include "impeller/renderer/backend/gles/blit_command_gles.h" |
| 6 | + |
| 7 | +#include "flutter/fml/closure.h" |
| 8 | +#include "impeller/base/validation.h" |
| 9 | +#include "impeller/renderer/backend/gles/texture_gles.h" |
| 10 | + |
| 11 | +namespace impeller { |
| 12 | + |
| 13 | +BlitEncodeGLES::~BlitEncodeGLES() = default; |
| 14 | + |
| 15 | +static void DeleteFBO(const ProcTableGLES& gl, GLuint fbo, GLenum type) { |
| 16 | + if (fbo != GL_NONE) { |
| 17 | + gl.BindFramebuffer(type, GL_NONE); |
| 18 | + gl.DeleteFramebuffers(1u, &fbo); |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +static std::optional<GLuint> ConfigureFBO( |
| 23 | + const ProcTableGLES& gl, |
| 24 | + const std::shared_ptr<Texture>& texture, |
| 25 | + GLenum fbo_type) { |
| 26 | + auto handle = TextureGLES::Cast(texture.get())->GetGLHandle(); |
| 27 | + if (!handle.has_value()) { |
| 28 | + return std::nullopt; |
| 29 | + } |
| 30 | + |
| 31 | + if (TextureGLES::Cast(*texture).IsWrapped()) { |
| 32 | + // The texture is attached to the default FBO, so there's no need to |
| 33 | + // create/configure one. |
| 34 | + gl.BindFramebuffer(fbo_type, 0); |
| 35 | + return 0; |
| 36 | + } |
| 37 | + |
| 38 | + GLuint fbo; |
| 39 | + gl.GenFramebuffers(1u, &fbo); |
| 40 | + gl.BindFramebuffer(fbo_type, fbo); |
| 41 | + |
| 42 | + if (!TextureGLES::Cast(*texture).SetAsFramebufferAttachment( |
| 43 | + fbo_type, fbo, TextureGLES::AttachmentPoint::kColor0)) { |
| 44 | + VALIDATION_LOG << "Could not attach texture to framebuffer."; |
| 45 | + DeleteFBO(gl, fbo, fbo_type); |
| 46 | + return std::nullopt; |
| 47 | + } |
| 48 | + |
| 49 | + if (gl.CheckFramebufferStatus(fbo_type) != GL_FRAMEBUFFER_COMPLETE) { |
| 50 | + VALIDATION_LOG << "Could not create a complete frambuffer."; |
| 51 | + DeleteFBO(gl, fbo, fbo_type); |
| 52 | + return std::nullopt; |
| 53 | + } |
| 54 | + |
| 55 | + return fbo; |
| 56 | +}; |
| 57 | + |
| 58 | +BlitCopyTextureToTextureCommandGLES::~BlitCopyTextureToTextureCommandGLES() = |
| 59 | + default; |
| 60 | + |
| 61 | +std::string BlitCopyTextureToTextureCommandGLES::GetLabel() const { |
| 62 | + return label; |
| 63 | +} |
| 64 | + |
| 65 | +bool BlitCopyTextureToTextureCommandGLES::Encode( |
| 66 | + const ReactorGLES& reactor) const { |
| 67 | + const auto& gl = reactor.GetProcTable(); |
| 68 | + |
| 69 | + // glBlitFramebuffer is a GLES3 proc. Since we target GLES2, we need to |
| 70 | + // emulate the blit when it's not available in the driver. |
| 71 | + if (!gl.BlitFramebuffer.IsAvailable()) { |
| 72 | + // TODO(bdero): Emulate the blit using a raster draw call here. |
| 73 | + FML_LOG(ERROR) << "Texture blit fallback not implemented yet for GLES2."; |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + GLuint read_fbo = GL_NONE; |
| 78 | + GLuint draw_fbo = GL_NONE; |
| 79 | + fml::ScopedCleanupClosure delete_fbos([&gl, &read_fbo, &draw_fbo]() { |
| 80 | + DeleteFBO(gl, read_fbo, GL_READ_FRAMEBUFFER); |
| 81 | + DeleteFBO(gl, draw_fbo, GL_DRAW_FRAMEBUFFER); |
| 82 | + }); |
| 83 | + |
| 84 | + { |
| 85 | + auto read = ConfigureFBO(gl, source, GL_READ_FRAMEBUFFER); |
| 86 | + if (!read.has_value()) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + read_fbo = read.value(); |
| 90 | + } |
| 91 | + |
| 92 | + { |
| 93 | + auto draw = ConfigureFBO(gl, destination, GL_DRAW_FRAMEBUFFER); |
| 94 | + if (!draw.has_value()) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + draw_fbo = draw.value(); |
| 98 | + } |
| 99 | + |
| 100 | + gl.Disable(GL_SCISSOR_TEST); |
| 101 | + gl.Disable(GL_DEPTH_TEST); |
| 102 | + gl.Disable(GL_STENCIL_TEST); |
| 103 | + |
| 104 | + gl.BlitFramebuffer(source_region.origin.x, // srcX0 |
| 105 | + source_region.origin.y, // srcY0 |
| 106 | + source_region.size.width, // srcX1 |
| 107 | + source_region.size.height, // srcY1 |
| 108 | + destination_origin.x, // dstX0 |
| 109 | + destination_origin.y, // dstY0 |
| 110 | + source_region.size.width, // dstX1 |
| 111 | + source_region.size.height, // dstY1 |
| 112 | + GL_COLOR_BUFFER_BIT, // mask |
| 113 | + GL_NEAREST // filter |
| 114 | + ); |
| 115 | + |
| 116 | + return true; |
| 117 | +}; |
| 118 | + |
| 119 | +BlitGenerateMipmapCommandGLES::~BlitGenerateMipmapCommandGLES() = default; |
| 120 | + |
| 121 | +std::string BlitGenerateMipmapCommandGLES::GetLabel() const { |
| 122 | + return label; |
| 123 | +} |
| 124 | + |
| 125 | +bool BlitGenerateMipmapCommandGLES::Encode(const ReactorGLES& reactor) const { |
| 126 | + auto texture_gles = TextureGLES::Cast(texture.get()); |
| 127 | + if (!texture_gles->GenerateMipmaps()) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | +}; |
| 133 | + |
| 134 | +} // namespace impeller |
0 commit comments