-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable partial repaint for Android/OpenGL #29591
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a3620eb
Enable partial repaint on Android/OpenGL
knopp fa494ce
Set FrameBufferInfo::supports_partial_repaint
knopp 5a64880
Support for quad buffering
knopp 719f8ce
Use std::array instead of vector
knopp dc74809
Disable partial redraw for older devices
knopp 6e2b31e
Enable partial repaint on Android 9
knopp 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,8 +6,12 @@ | |
|
||
#include <EGL/eglext.h> | ||
|
||
#include <list> | ||
#include <utility> | ||
|
||
// required to get API level | ||
#include <sys/system_properties.h> | ||
|
||
#include "flutter/fml/trace_event.h" | ||
|
||
namespace flutter { | ||
|
@@ -105,10 +109,135 @@ static bool TeardownContext(EGLDisplay display, EGLContext context) { | |
return true; | ||
} | ||
|
||
class AndroidEGLSurfaceDamage { | ||
public: | ||
void init(EGLDisplay display, EGLContext context) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. I think I just overlooked it. |
||
if (GetAPILevel() < 28) { | ||
// Disable partial repaint for devices older than Android 9. There | ||
// are old devices that have extensions below available but the | ||
// implementation causes glitches (i.e. Xperia Z3 with Android 6). | ||
partial_redraw_supported_ = false; | ||
return; | ||
} | ||
|
||
const char* extensions = eglQueryString(display, EGL_EXTENSIONS); | ||
|
||
if (HasExtension(extensions, "EGL_KHR_partial_update")) { | ||
set_damage_region_ = reinterpret_cast<PFNEGLSETDAMAGEREGIONKHRPROC>( | ||
eglGetProcAddress("eglSetDamageRegionKHR")); | ||
} | ||
|
||
if (HasExtension(extensions, "EGL_EXT_swap_buffers_with_damage")) { | ||
swap_buffers_with_damage_ = | ||
reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>( | ||
eglGetProcAddress("eglSwapBuffersWithDamageEXT")); | ||
} else if (HasExtension(extensions, "EGL_KHR_swap_buffers_with_damage")) { | ||
swap_buffers_with_damage_ = | ||
reinterpret_cast<PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC>( | ||
eglGetProcAddress("eglSwapBuffersWithDamageKHR")); | ||
} | ||
|
||
partial_redraw_supported_ = | ||
set_damage_region_ != nullptr && swap_buffers_with_damage_ != nullptr; | ||
} | ||
|
||
static int GetAPILevel() { | ||
char sdk_version_string[PROP_VALUE_MAX]; | ||
if (__system_property_get("ro.build.version.sdk", sdk_version_string)) { | ||
return atoi(sdk_version_string); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
void SetDamageRegion(EGLDisplay display, | ||
EGLSurface surface, | ||
const std::optional<SkIRect>& region) { | ||
if (set_damage_region_ && region) { | ||
auto rects = RectToInts(display, surface, *region); | ||
set_damage_region_(display, surface, rects.data(), 1); | ||
} | ||
} | ||
|
||
// Maximum damage history - for triple buffering we need to store damage for | ||
// last two frames; Some Android devices (Pixel 4) use quad buffering. | ||
static const int kMaxHistorySize = 10; | ||
|
||
bool SupportsPartialRepaint() const { return partial_redraw_supported_; } | ||
|
||
std::optional<SkIRect> InitialDamage(EGLDisplay display, EGLSurface surface) { | ||
if (!partial_redraw_supported_) { | ||
return std::nullopt; | ||
} | ||
|
||
EGLint age; | ||
eglQuerySurface(display, surface, EGL_BUFFER_AGE_EXT, &age); | ||
|
||
if (age == 0) { // full repaint | ||
return std::nullopt; | ||
} else { | ||
// join up to (age - 1) last rects from damage history | ||
--age; | ||
auto res = SkIRect::MakeEmpty(); | ||
for (auto i = damage_history_.rbegin(); | ||
i != damage_history_.rend() && age > 0; ++i, --age) { | ||
res.join(*i); | ||
} | ||
return res; | ||
} | ||
} | ||
|
||
bool SwapBuffersWithDamage(EGLDisplay display, | ||
EGLSurface surface, | ||
const std::optional<SkIRect>& damage) { | ||
if (swap_buffers_with_damage_ && damage) { | ||
damage_history_.push_back(*damage); | ||
if (damage_history_.size() > kMaxHistorySize) { | ||
damage_history_.pop_front(); | ||
} | ||
auto rects = RectToInts(display, surface, *damage); | ||
return swap_buffers_with_damage_(display, surface, rects.data(), 1); | ||
} else { | ||
return eglSwapBuffers(display, surface); | ||
} | ||
} | ||
|
||
private: | ||
std::array<EGLint, 4> static RectToInts(EGLDisplay display, | ||
EGLSurface surface, | ||
const SkIRect& rect) { | ||
EGLint height; | ||
eglQuerySurface(display, surface, EGL_HEIGHT, &height); | ||
|
||
std::array<EGLint, 4> res{rect.left(), height - rect.bottom(), rect.width(), | ||
rect.height()}; | ||
return res; | ||
} | ||
|
||
PFNEGLSETDAMAGEREGIONKHRPROC set_damage_region_ = nullptr; | ||
PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage_ = nullptr; | ||
|
||
bool partial_redraw_supported_; | ||
|
||
bool HasExtension(const char* extensions, const char* name) { | ||
const char* r = strstr(extensions, name); | ||
auto len = strlen(name); | ||
// check that the extension name is terminated by space or null terminator | ||
return r != nullptr && (r[len] == ' ' || r[len] == 0); | ||
} | ||
|
||
std::list<SkIRect> damage_history_; | ||
}; | ||
|
||
AndroidEGLSurface::AndroidEGLSurface(EGLSurface surface, | ||
EGLDisplay display, | ||
EGLContext context) | ||
: surface_(surface), display_(display), context_(context) {} | ||
: surface_(surface), | ||
display_(display), | ||
context_(context), | ||
damage_(std::make_unique<AndroidEGLSurfaceDamage>()) { | ||
damage_->init(display_, context); | ||
} | ||
|
||
AndroidEGLSurface::~AndroidEGLSurface() { | ||
auto result = eglDestroySurface(display_, surface_); | ||
|
@@ -128,9 +257,23 @@ bool AndroidEGLSurface::MakeCurrent() const { | |
return true; | ||
} | ||
|
||
bool AndroidEGLSurface::SwapBuffers() { | ||
void AndroidEGLSurface::SetDamageRegion( | ||
const std::optional<SkIRect>& buffer_damage) { | ||
damage_->SetDamageRegion(display_, surface_, buffer_damage); | ||
} | ||
|
||
bool AndroidEGLSurface::SwapBuffers( | ||
const std::optional<SkIRect>& surface_damage) { | ||
TRACE_EVENT0("flutter", "AndroidContextGL::SwapBuffers"); | ||
return eglSwapBuffers(display_, surface_); | ||
return damage_->SwapBuffersWithDamage(display_, surface_, surface_damage); | ||
} | ||
|
||
bool AndroidEGLSurface::SupportsPartialRepaint() const { | ||
return damage_->SupportsPartialRepaint(); | ||
} | ||
|
||
std::optional<SkIRect> AndroidEGLSurface::InitialDamage() { | ||
return damage_->InitialDamage(display_, surface_); | ||
} | ||
|
||
SkISize AndroidEGLSurface::GetSize() const { | ||
|
This file contains 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 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 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 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 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 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
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.
any reason this class is forward declared rather than have the definition in
android_context_gl.h
?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.
I prefer not having to split it into header/implementation but don't have strong feelings about it, if you prefer the definition in header I can certainly do it.