This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Reland "[Windows] Move to FlutterCompositor for rendering" #49726
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_ | ||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_ | ||
|
||
#include "flutter/shell/platform/embedder/embedder.h" | ||
|
||
namespace flutter { | ||
|
||
// Enables the Flutter engine to render content on Windows. | ||
// | ||
// The engine uses this to: | ||
// | ||
// 1. Create backing stores used for rendering Flutter content | ||
// 2. Composite and present Flutter content and platform views onto a view | ||
// | ||
// Platform views are not yet supported. | ||
class Compositor { | ||
public: | ||
virtual ~Compositor() = default; | ||
|
||
// Creates a backing store used for rendering Flutter content. | ||
// | ||
// The backing store's configuration is stored in |backing_store_out|. | ||
virtual bool CreateBackingStore(const FlutterBackingStoreConfig& config, | ||
FlutterBackingStore* backing_store_out) = 0; | ||
|
||
// Destroys a backing store and releases its resources. | ||
virtual bool CollectBackingStore(const FlutterBackingStore* store) = 0; | ||
|
||
// Present Flutter content and platform views onto the view. | ||
virtual bool Present(const FlutterLayer** layers, size_t layers_count) = 0; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_COMPOSITOR_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/shell/platform/windows/compositor_opengl.h" | ||
|
||
#include "GLES3/gl3.h" | ||
#include "flutter/shell/platform/windows/flutter_windows_view.h" | ||
|
||
namespace flutter { | ||
|
||
namespace { | ||
|
||
constexpr uint32_t kWindowFrameBufferId = 0; | ||
|
||
// The metadata for an OpenGL framebuffer backing store. | ||
struct FramebufferBackingStore { | ||
uint32_t framebuffer_id; | ||
uint32_t texture_id; | ||
}; | ||
|
||
// Based off Skia's logic: | ||
// https://github.com/google/skia/blob/4738ed711e03212aceec3cd502a4adb545f38e63/src/gpu/ganesh/gl/GrGLCaps.cpp#L1963-L2116 | ||
int GetSupportedTextureFormat(const impeller::DescriptionGLES* description) { | ||
if (description->HasExtension("GL_EXT_texture_format_BGRA8888")) { | ||
return GL_BGRA8_EXT; | ||
} else if (description->HasExtension("GL_APPLE_texture_format_BGRA8888") && | ||
description->GetGlVersion().IsAtLeast(impeller::Version(3, 0))) { | ||
return GL_BGRA8_EXT; | ||
} else { | ||
return GL_RGBA8; | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
CompositorOpenGL::CompositorOpenGL(FlutterWindowsEngine* engine, | ||
impeller::ProcTableGLES::Resolver resolver) | ||
: engine_(engine), resolver_(resolver) {} | ||
|
||
bool CompositorOpenGL::CreateBackingStore( | ||
const FlutterBackingStoreConfig& config, | ||
FlutterBackingStore* result) { | ||
if (!is_initialized_ && !Initialize()) { | ||
return false; | ||
} | ||
|
||
auto store = std::make_unique<FramebufferBackingStore>(); | ||
|
||
gl_->GenTextures(1, &store->texture_id); | ||
gl_->GenFramebuffers(1, &store->framebuffer_id); | ||
|
||
gl_->BindFramebuffer(GL_FRAMEBUFFER, store->framebuffer_id); | ||
|
||
gl_->BindTexture(GL_TEXTURE_2D, store->texture_id); | ||
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
gl_->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, config.size.width, | ||
config.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); | ||
gl_->BindTexture(GL_TEXTURE_2D, 0); | ||
|
||
gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT, | ||
GL_TEXTURE_2D, store->texture_id, 0); | ||
|
||
result->type = kFlutterBackingStoreTypeOpenGL; | ||
result->open_gl.type = kFlutterOpenGLTargetTypeFramebuffer; | ||
result->open_gl.framebuffer.name = store->framebuffer_id; | ||
result->open_gl.framebuffer.target = format_; | ||
result->open_gl.framebuffer.user_data = store.release(); | ||
result->open_gl.framebuffer.destruction_callback = [](void* user_data) { | ||
// Backing store destroyed in `CompositorOpenGL::CollectBackingStore`, set | ||
// on FlutterCompositor.collect_backing_store_callback during engine start. | ||
}; | ||
return true; | ||
} | ||
|
||
bool CompositorOpenGL::CollectBackingStore(const FlutterBackingStore* store) { | ||
FML_DCHECK(is_initialized_); | ||
FML_DCHECK(store->type == kFlutterBackingStoreTypeOpenGL); | ||
FML_DCHECK(store->open_gl.type == kFlutterOpenGLTargetTypeFramebuffer); | ||
|
||
auto user_data = static_cast<FramebufferBackingStore*>( | ||
store->open_gl.framebuffer.user_data); | ||
|
||
gl_->DeleteFramebuffers(1, &user_data->framebuffer_id); | ||
gl_->DeleteTextures(1, &user_data->texture_id); | ||
|
||
delete user_data; | ||
return true; | ||
} | ||
|
||
bool CompositorOpenGL::Present(const FlutterLayer** layers, | ||
size_t layers_count) { | ||
if (!engine_->view()) { | ||
return false; | ||
} | ||
|
||
// Clear the view if there are no layers to present. | ||
if (layers_count == 0) { | ||
// Normally the compositor is initialized when the first backing store is | ||
// created. However, on an empty frame no backing stores are created and | ||
// the present needs to initialize the compositor. | ||
if (!is_initialized_ && !Initialize()) { | ||
return false; | ||
} | ||
|
||
return ClearSurface(); | ||
} | ||
|
||
// TODO: Support compositing layers and platform views. | ||
// See: https://github.com/flutter/flutter/issues/31713 | ||
FML_DCHECK(is_initialized_); | ||
FML_DCHECK(layers_count == 1); | ||
FML_DCHECK(layers[0]->offset.x == 0 && layers[0]->offset.y == 0); | ||
FML_DCHECK(layers[0]->type == kFlutterLayerContentTypeBackingStore); | ||
FML_DCHECK(layers[0]->backing_store->type == kFlutterBackingStoreTypeOpenGL); | ||
FML_DCHECK(layers[0]->backing_store->open_gl.type == | ||
kFlutterOpenGLTargetTypeFramebuffer); | ||
|
||
auto width = layers[0]->size.width; | ||
auto height = layers[0]->size.height; | ||
|
||
// Check if this frame can be presented. This resizes the surface if a resize | ||
// is pending and |width| and |height| match the target size. | ||
if (!engine_->view()->OnFrameGenerated(width, height)) { | ||
return false; | ||
} | ||
|
||
if (!engine_->surface_manager()->MakeCurrent()) { | ||
return false; | ||
} | ||
|
||
auto source_id = layers[0]->backing_store->open_gl.framebuffer.name; | ||
|
||
// Disable the scissor test as it can affect blit operations. | ||
// Prevents regressions like: https://github.com/flutter/flutter/issues/140828 | ||
// See OpenGL specification version 4.6, section 18.3.1. | ||
gl_->Disable(GL_SCISSOR_TEST); | ||
|
||
gl_->BindFramebuffer(GL_READ_FRAMEBUFFER, source_id); | ||
gl_->BindFramebuffer(GL_DRAW_FRAMEBUFFER, kWindowFrameBufferId); | ||
|
||
gl_->BlitFramebuffer(0, // srcX0 | ||
0, // srcY0 | ||
width, // srcX1 | ||
height, // srcY1 | ||
0, // dstX0 | ||
0, // dstY0 | ||
width, // dstX1 | ||
height, // dstY1 | ||
GL_COLOR_BUFFER_BIT, // mask | ||
GL_NEAREST // filter | ||
); | ||
|
||
return engine_->view()->SwapBuffers(); | ||
} | ||
|
||
bool CompositorOpenGL::Initialize() { | ||
FML_DCHECK(!is_initialized_); | ||
|
||
if (!engine_->surface_manager()->MakeCurrent()) { | ||
return false; | ||
} | ||
|
||
gl_ = std::make_unique<impeller::ProcTableGLES>(resolver_); | ||
if (!gl_->IsValid()) { | ||
gl_.reset(); | ||
return false; | ||
} | ||
|
||
format_ = GetSupportedTextureFormat(gl_->GetDescription()); | ||
is_initialized_ = true; | ||
return true; | ||
} | ||
|
||
bool CompositorOpenGL::ClearSurface() { | ||
FML_DCHECK(is_initialized_); | ||
|
||
// Resize the surface if needed. | ||
engine_->view()->OnEmptyFrameGenerated(); | ||
|
||
if (!engine_->surface_manager()->MakeCurrent()) { | ||
return false; | ||
} | ||
|
||
gl_->ClearColor(0.0f, 0.0f, 0.0f, 0.0f); | ||
gl_->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | ||
|
||
return engine_->view()->SwapBuffers(); | ||
} | ||
|
||
} // namespace flutter |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
frambuffer: most, but not quite all, of a framebuffer. 🤣