|
| 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 "flutter/shell/platform/windows/compositor_opengl.h" |
| 6 | + |
| 7 | +#include "GLES3/gl3.h" |
| 8 | +#include "flutter/shell/platform/windows/flutter_windows_view.h" |
| 9 | + |
| 10 | +namespace flutter { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +constexpr uint32_t kWindowFrameBufferId = 0; |
| 15 | + |
| 16 | +// The metadata for an OpenGL framebuffer backing store. |
| 17 | +struct FramebufferBackingStore { |
| 18 | + uint32_t framebuffer_id; |
| 19 | + uint32_t texture_id; |
| 20 | +}; |
| 21 | + |
| 22 | +// Based off Skia's logic: |
| 23 | +// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116 |
| 24 | +int GetSupportedTextureFormat(const impeller::DescriptionGLES* description) { |
| 25 | + if (description->HasExtension("GL_EXT_texture_format_BGRA8888")) { |
| 26 | + return GL_BGRA8_EXT; |
| 27 | + } else if (description->HasExtension("GL_APPLE_texture_format_BGRA8888") && |
| 28 | + description->GetGlVersion().IsAtLeast(impeller::Version(3, 0))) { |
| 29 | + return GL_BGRA8_EXT; |
| 30 | + } else { |
| 31 | + return GL_RGBA8; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +} // namespace |
| 36 | + |
| 37 | +CompositorOpenGL::CompositorOpenGL(FlutterWindowsEngine* engine, |
| 38 | + impeller::ProcTableGLES::Resolver resolver) |
| 39 | + : engine_(engine), resolver_(resolver) {} |
| 40 | + |
| 41 | +bool CompositorOpenGL::CreateBackingStore( |
| 42 | + const FlutterBackingStoreConfig& config, |
| 43 | + FlutterBackingStore* result) { |
| 44 | + if (!is_initialized_ && !Initialize()) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + auto store = std::make_unique<FramebufferBackingStore>(); |
| 49 | + |
| 50 | + gl_->GenTextures(1, &store->texture_id); |
| 51 | + gl_->GenFramebuffers(1, &store->framebuffer_id); |
| 52 | + |
| 53 | + gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id); |
| 54 | + |
| 55 | + gl_->BindTexture(GL_TEXTURE_2D, store->texture_id); |
| 56 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 57 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 58 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 59 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 60 | + gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, config.size.width, |
| 61 | + config.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
| 62 | + gl_->BindTexture(GL_TEXTURE_2D, 0); |
| 63 | + |
| 64 | + gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT, |
| 65 | + GL_TEXTURE_2D, store->texture_id, 0); |
| 66 | + |
| 67 | + result->type = kFlutterBackingStoreTypeOpenGL; |
| 68 | + result->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer; |
| 69 | + result->open_gl.framebuffer.name = store->framebuffer_id; |
| 70 | + result->open_gl.framebuffer.target = format_; |
| 71 | + result->open_gl.framebuffer.user_data = store.release(); |
| 72 | + result->open_gl.framebuffer.destruction_callback = [](void* user_data) { |
| 73 | + // Backing store destroyed in `CompositorOpenGL::CollectBackingStore`, set |
| 74 | + // on FlutterCompositor.collect_backing_store_callback during engine start. |
| 75 | + }; |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) { |
| 80 | + FML_DCHECK(is_initialized_); |
| 81 | + FML_DCHECK(store->type == kFlutterBackingStoreTypeOpenGL); |
| 82 | + FML_DCHECK(store->open_gl.type == kFlutterOpenGLTargetTypeFramebuffer); |
| 83 | + |
| 84 | + auto user_data = static_cast<FramebufferBackingStore*>( |
| 85 | + store->open_gl.framebuffer.user_data); |
| 86 | + |
| 87 | + gl_->DeleteFramebuffers(1, &user_data->framebuffer_id); |
| 88 | + gl_->DeleteTextures(1, &user_data->texture_id); |
| 89 | + |
| 90 | + delete user_data; |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +bool CompositorOpenGL::Present(const FlutterLayer** layers, |
| 95 | + size_t layers_count) { |
| 96 | + if (!engine_->view()) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + // Clear the view if there are no layers to present. |
| 101 | + if (layers_count == 0) { |
| 102 | + // Normally the compositor is initialized when the first backing store is |
| 103 | + // created. However, on an empty frame no backing stores are created and |
| 104 | + // the present needs to initialize the compositor. |
| 105 | + if (!is_initialized_ && !Initialize()) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + return ClearSurface(); |
| 110 | + } |
| 111 | + |
| 112 | + // TODO: Support compositing layers and platform views. |
| 113 | + // See: https://github.com/flutter/flutter/issues/31713 |
| 114 | + FML_DCHECK(is_initialized_); |
| 115 | + FML_DCHECK(layers_count == 1); |
| 116 | + FML_DCHECK(layers[0]->offset.x == 0 && layers[0]->offset.y == 0); |
| 117 | + FML_DCHECK(layers[0]->type == kFlutterLayerContentTypeBackingStore); |
| 118 | + FML_DCHECK(layers[0]->backing_store->type == kFlutterBackingStoreTypeOpenGL); |
| 119 | + FML_DCHECK(layers[0]->backing_store->open_gl.type == |
| 120 | + kFlutterOpenGLTargetTypeFramebuffer); |
| 121 | + |
| 122 | + auto width = layers[0]->size.width; |
| 123 | + auto height = layers[0]->size.height; |
| 124 | + |
| 125 | + // Check if this frame can be presented. This resizes the surface if a resize |
| 126 | + // is pending and |width| and |height| match the target size. |
| 127 | + if (!engine_->view()->OnFrameGenerated(width, height)) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + if (!engine_->surface_manager()->MakeCurrent()) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + auto source_id = layers[0]->backing_store->open_gl.framebuffer.name; |
| 136 | + |
| 137 | + // Disable the scissor test as it can affect blit operations. |
| 138 | + // Prevents regressions like: https://github.com/flutter/flutter/issues/140828 |
| 139 | + // See OpenGL specification version 4.6, section 18.3.1. |
| 140 | + gl_->Disable(GL_SCISSOR_TEST); |
| 141 | + |
| 142 | + gl_->BindFramebuffer(GL_READ_FRAMEBUFFER, source_id); |
| 143 | + gl_->BindFramebuffer(GL_DRAW_FRAMEBUFFER, kWindowFrameBufferId); |
| 144 | + |
| 145 | + gl_->BlitFramebuffer(0, // srcX0 |
| 146 | + 0, // srcY0 |
| 147 | + width, // srcX1 |
| 148 | + height, // srcY1 |
| 149 | + 0, // dstX0 |
| 150 | + 0, // dstY0 |
| 151 | + width, // dstX1 |
| 152 | + height, // dstY1 |
| 153 | + GL_COLOR_BUFFER_BIT, // mask |
| 154 | + GL_NEAREST // filter |
| 155 | + ); |
| 156 | + |
| 157 | + return engine_->view()->SwapBuffers(); |
| 158 | +} |
| 159 | + |
| 160 | +bool CompositorOpenGL::Initialize() { |
| 161 | + FML_DCHECK(!is_initialized_); |
| 162 | + |
| 163 | + if (!engine_->surface_manager()->MakeCurrent()) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + gl_ = std::make_unique<impeller::ProcTableGLES>(resolver_); |
| 168 | + if (!gl_->IsValid()) { |
| 169 | + gl_.reset(); |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + format_ = GetSupportedTextureFormat(gl_->GetDescription()); |
| 174 | + is_initialized_ = true; |
| 175 | + return true; |
| 176 | +} |
| 177 | + |
| 178 | +bool CompositorOpenGL::ClearSurface() { |
| 179 | + FML_DCHECK(is_initialized_); |
| 180 | + |
| 181 | + // Resize the surface if needed. |
| 182 | + engine_->view()->OnEmptyFrameGenerated(); |
| 183 | + |
| 184 | + if (!engine_->surface_manager()->MakeCurrent()) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 189 | + gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 190 | + |
| 191 | + return engine_->view()->SwapBuffers(); |
| 192 | +} |
| 193 | + |
| 194 | +} // namespace flutter |
0 commit comments