|
| 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 | +// The metadata for an OpenGL framebuffer backing store. |
| 15 | +struct FramebufferBackingStore { |
| 16 | + uint32_t framebuffer_id; |
| 17 | + uint32_t texture_id; |
| 18 | +}; |
| 19 | + |
| 20 | +// Based off Skia's logic: |
| 21 | +// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116 |
| 22 | +int GetSupportedTextureFormat(const impeller::DescriptionGLES* description) { |
| 23 | + if (description->HasExtension("GL_EXT_texture_format_BGRA8888")) { |
| 24 | + return GL_BGRA8_EXT; |
| 25 | + } else if (description->HasExtension("GL_APPLE_texture_format_BGRA8888") && |
| 26 | + description->GetGlVersion().IsAtLeast(impeller::Version(3, 0))) { |
| 27 | + return GL_BGRA8_EXT; |
| 28 | + } else { |
| 29 | + return GL_RGBA8; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +CompositorOpenGL::CompositorOpenGL(FlutterWindowsEngine* engine, |
| 36 | + impeller::ProcTableGLES::Resolver resolver) |
| 37 | + : engine_(engine), resolver_(resolver) {} |
| 38 | + |
| 39 | +bool CompositorOpenGL::CreateBackingStore( |
| 40 | + const FlutterBackingStoreConfig& config, |
| 41 | + FlutterBackingStore* result) { |
| 42 | + if (!is_initialized_ && !Initialize()) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + auto store = std::make_unique<FramebufferBackingStore>(); |
| 47 | + |
| 48 | + gl_->GenTextures(1, &store->texture_id); |
| 49 | + gl_->GenFramebuffers(1, &store->framebuffer_id); |
| 50 | + |
| 51 | + gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id); |
| 52 | + |
| 53 | + gl_->BindTexture(GL_TEXTURE_2D, store->texture_id); |
| 54 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 55 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 56 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 57 | + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 58 | + gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, config.size.width, |
| 59 | + config.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
| 60 | + gl_->BindTexture(GL_TEXTURE_2D, 0); |
| 61 | + |
| 62 | + gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT, |
| 63 | + GL_TEXTURE_2D, store->texture_id, 0); |
| 64 | + |
| 65 | + result->type = kFlutterBackingStoreTypeOpenGL; |
| 66 | + result->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer; |
| 67 | + result->open_gl.framebuffer.name = store->framebuffer_id; |
| 68 | + result->open_gl.framebuffer.target = format_; |
| 69 | + result->open_gl.framebuffer.user_data = store.release(); |
| 70 | + result->open_gl.framebuffer.destruction_callback = [](void* user_data) { |
| 71 | + // Backing store destroyed in `CompositorOpenGL::CollectBackingStore`, set |
| 72 | + // on FlutterCompositor.collect_backing_store_callback during engine start. |
| 73 | + }; |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) { |
| 78 | + FML_DCHECK(is_initialized_); |
| 79 | + FML_DCHECK(store->type == kFlutterBackingStoreTypeOpenGL); |
| 80 | + FML_DCHECK(store->open_gl.type == kFlutterOpenGLTargetTypeFramebuffer); |
| 81 | + |
| 82 | + auto user_data = static_cast<FramebufferBackingStore*>( |
| 83 | + store->open_gl.framebuffer.user_data); |
| 84 | + |
| 85 | + gl_->DeleteFramebuffers(1, &user_data->framebuffer_id); |
| 86 | + gl_->DeleteTextures(1, &user_data->texture_id); |
| 87 | + |
| 88 | + delete user_data; |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +bool CompositorOpenGL::Present(const FlutterLayer** layers, |
| 93 | + size_t layers_count) { |
| 94 | + // TODO: Support compositing layers and platform views. |
| 95 | + // See: https://github.com/flutter/flutter/issues/31713 |
| 96 | + FML_DCHECK(is_initialized_); |
| 97 | + FML_DCHECK(layers_count == 1); |
| 98 | + FML_DCHECK(layers[0]->type == kFlutterLayerContentTypeBackingStore); |
| 99 | + FML_DCHECK(layers[0]->backing_store->type == kFlutterBackingStoreTypeOpenGL); |
| 100 | + FML_DCHECK(layers[0]->backing_store->open_gl.type == |
| 101 | + kFlutterOpenGLTargetTypeFramebuffer); |
| 102 | + |
| 103 | + if (!engine_->view()) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + auto width = layers[0]->size.width; |
| 108 | + auto height = layers[0]->size.height; |
| 109 | + |
| 110 | + // Acquiring the view's framebuffer ID resizes its surface if necessary. |
| 111 | + auto destination_id = engine_->view()->GetFrameBufferId(width, height); |
| 112 | + auto source_id = layers[0]->backing_store->open_gl.framebuffer.name; |
| 113 | + |
| 114 | + if (!engine_->surface_manager()->MakeCurrent()) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + gl_->BindFramebuffer(GL_READ_FRAMEBUFFER, source_id); |
| 119 | + gl_->BindFramebuffer(GL_DRAW_FRAMEBUFFER, destination_id); |
| 120 | + |
| 121 | + gl_->BlitFramebuffer(0, // srcX0 |
| 122 | + 0, // srcY0 |
| 123 | + width, // srcX1 |
| 124 | + height, // srcY1 |
| 125 | + 0, // dstX0 |
| 126 | + 0, // dstY0 |
| 127 | + width, // dstX1 |
| 128 | + height, // dstY1 |
| 129 | + GL_COLOR_BUFFER_BIT, // mask |
| 130 | + GL_NEAREST // filter |
| 131 | + ); |
| 132 | + |
| 133 | + return engine_->view()->SwapBuffers(); |
| 134 | +} |
| 135 | + |
| 136 | +bool CompositorOpenGL::Initialize() { |
| 137 | + FML_DCHECK(!is_initialized_); |
| 138 | + |
| 139 | + if (!engine_->surface_manager()->MakeCurrent()) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + gl_ = std::make_unique<impeller::ProcTableGLES>(resolver_); |
| 144 | + if (!gl_->IsValid()) { |
| 145 | + gl_.reset(); |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + format_ = GetSupportedTextureFormat(gl_->GetDescription()); |
| 150 | + is_initialized_ = true; |
| 151 | + return true; |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace flutter |
0 commit comments