-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
8313697: [XWayland][Screencast] consequent getPixelColor calls are slow #15250
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8f5b128
initial
azvegint 60ea277
Handle the case where a new screen capture requires a token different…
azvegint 81ff41d
Make multiple calls to doCleanup() safe
azvegint 0b6e8f4
switch to java.util.Timer
azvegint bb92788
fix app exit issue
azvegint 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 |
---|---|---|
|
@@ -43,6 +43,9 @@ int DEBUG_SCREENCAST_ENABLED = FALSE; | |
(*env)->ExceptionDescribe(env); \ | ||
} | ||
|
||
static volatile gboolean sessionClosed = TRUE; | ||
static GString *activeSessionToken; | ||
|
||
struct ScreenSpace screenSpace = {0}; | ||
static struct PwLoopData pw = {0}; | ||
|
||
|
@@ -89,8 +92,8 @@ static void doCleanup() { | |
struct ScreenProps *screenProps = &screenSpace.screens[i]; | ||
if (screenProps->data) { | ||
if (screenProps->data->stream) { | ||
fp_pw_thread_loop_lock(pw.loop); | ||
fp_pw_stream_disconnect(screenProps->data->stream); | ||
fp_pw_thread_loop_lock(pw.loop); | ||
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. Moving |
||
fp_pw_stream_destroy(screenProps->data->stream); | ||
fp_pw_thread_loop_unlock(pw.loop); | ||
screenProps->data->stream = NULL; | ||
|
@@ -123,7 +126,11 @@ static void doCleanup() { | |
if (screenSpace.screens) { | ||
free(screenSpace.screens); | ||
screenSpace.screens = NULL; | ||
screenSpace.screenCount = 0; | ||
} | ||
|
||
gtk->g_string_set_size(activeSessionToken, 0); | ||
sessionClosed = TRUE; | ||
} | ||
|
||
/** | ||
|
@@ -132,6 +139,24 @@ static void doCleanup() { | |
static gboolean initScreencast(const gchar *token, | ||
GdkRectangle *affectedBounds, | ||
gint affectedBoundsLength) { | ||
gboolean isSameToken = !token | ||
? FALSE | ||
: strcmp(token, activeSessionToken->str) == 0; | ||
|
||
if (!sessionClosed) { | ||
if (isSameToken) { | ||
DEBUG_SCREENCAST("Reusing active session.\n", NULL); | ||
return TRUE; | ||
} else { | ||
DEBUG_SCREENCAST( | ||
"Active session has a different token |%s| -> |%s|," | ||
" closing current session.\n", | ||
activeSessionToken->str, token | ||
); | ||
doCleanup(); | ||
} | ||
} | ||
|
||
fp_pw_init(NULL, NULL); | ||
|
||
pw.pwFd = RESULT_ERROR; | ||
|
@@ -145,6 +170,8 @@ static gboolean initScreencast(const gchar *token, | |
return FALSE; | ||
} | ||
|
||
gtk->g_string_printf(activeSessionToken, "%s", token); | ||
sessionClosed = FALSE; | ||
return TRUE; | ||
} | ||
|
||
|
@@ -386,8 +413,19 @@ static gboolean connectStream(int index) { | |
|
||
data->screenProps = &screenSpace.screens[index]; | ||
|
||
data->hasFormat = FALSE; | ||
if (!sessionClosed && data->stream) { | ||
fp_pw_thread_loop_lock(pw.loop); | ||
int result = fp_pw_stream_set_active(data->stream, TRUE); | ||
fp_pw_thread_loop_unlock(pw.loop); | ||
|
||
DEBUG_SCREEN_PREFIX(data->screenProps, | ||
"stream %p: activate result |%i|\n", | ||
data->stream, result); | ||
|
||
return result == 0; // 0 - success | ||
}; | ||
|
||
data->hasFormat = FALSE; | ||
|
||
data->stream = fp_pw_stream_new( | ||
pw.core, | ||
|
@@ -505,60 +543,64 @@ static const struct pw_core_events coreEvents = { | |
* @return TRUE on success | ||
*/ | ||
static gboolean doLoop(GdkRectangle requestedArea) { | ||
pw.loop = fp_pw_thread_loop_new("AWT Pipewire Thread", NULL); | ||
|
||
if (!pw.loop) { | ||
DEBUG_SCREENCAST("!!! Could not create a loop\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
|
||
pw.context = fp_pw_context_new( | ||
fp_pw_thread_loop_get_loop(pw.loop), | ||
NULL, | ||
0 | ||
); | ||
if (!pw.loop && !sessionClosed) { | ||
pw.loop = fp_pw_thread_loop_new("AWT Pipewire Thread", NULL); | ||
|
||
if (!pw.context) { | ||
DEBUG_SCREENCAST("!!! Could not create a pipewire context\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
if (!pw.loop) { | ||
DEBUG_SCREENCAST("!!! Could not create a loop\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
|
||
if (fp_pw_thread_loop_start(pw.loop) != 0) { | ||
DEBUG_SCREENCAST("!!! Could not start pipewire thread loop\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
pw.context = fp_pw_context_new( | ||
fp_pw_thread_loop_get_loop(pw.loop), | ||
NULL, | ||
0 | ||
); | ||
|
||
fp_pw_thread_loop_lock(pw.loop); | ||
if (!pw.context) { | ||
DEBUG_SCREENCAST("!!! Could not create a pipewire context\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
|
||
pw.core = fp_pw_context_connect_fd( | ||
pw.context, | ||
pw.pwFd, | ||
NULL, | ||
0 | ||
); | ||
if (fp_pw_thread_loop_start(pw.loop) != 0) { | ||
DEBUG_SCREENCAST("!!! Could not start pipewire thread loop\n", NULL); | ||
doCleanup(); | ||
return FALSE; | ||
} | ||
|
||
if (!pw.core) { | ||
DEBUG_SCREENCAST("!!! Could not create pipewire core\n", NULL); | ||
goto fail; | ||
} | ||
fp_pw_thread_loop_lock(pw.loop); | ||
|
||
pw_core_add_listener(pw.core, &pw.coreListener, &coreEvents, NULL); | ||
pw.core = fp_pw_context_connect_fd( | ||
pw.context, | ||
pw.pwFd, | ||
NULL, | ||
0 | ||
); | ||
|
||
for (int i = 0; i < screenSpace.screenCount; ++i) { | ||
struct PwStreamData *data = | ||
(struct PwStreamData*) malloc(sizeof (struct PwStreamData)); | ||
if (!data) { | ||
ERR("failed to allocate memory\n"); | ||
if (!pw.core) { | ||
DEBUG_SCREENCAST("!!! Could not create pipewire core\n", NULL); | ||
goto fail; | ||
} | ||
|
||
memset(data, 0, sizeof (struct PwStreamData)); | ||
pw_core_add_listener(pw.core, &pw.coreListener, &coreEvents, NULL); | ||
} | ||
|
||
for (int i = 0; i < screenSpace.screenCount; ++i) { | ||
struct ScreenProps *screen = &screenSpace.screens[i]; | ||
screen->data = data; | ||
if (!screen->data && !sessionClosed) { | ||
struct PwStreamData *data = | ||
(struct PwStreamData*) malloc(sizeof (struct PwStreamData)); | ||
if (!data) { | ||
ERR("failed to allocate memory\n"); | ||
goto fail; | ||
} | ||
|
||
memset(data, 0, sizeof (struct PwStreamData)); | ||
|
||
screen->data = data; | ||
} | ||
|
||
DEBUG_SCREEN_PREFIX(screen, "@@@ adding screen %i\n", i); | ||
if (checkScreen(i, requestedArea)) { | ||
|
@@ -746,6 +788,8 @@ JNIEXPORT jboolean JNICALL Java_sun_awt_screencast_ScreencastHelper_loadPipewire | |
return JNI_FALSE; | ||
} | ||
|
||
activeSessionToken = gtk->g_string_new(""); | ||
|
||
gboolean usable = initXdgDesktopPortal(); | ||
portalScreenCastCleanup(); | ||
return usable; | ||
|
@@ -783,6 +827,17 @@ static void arrayToRectangles(JNIEnv *env, | |
(*env)->ReleaseIntArrayElements(env, boundsArray, body, 0); | ||
} | ||
|
||
/* | ||
* Class: sun_awt_screencast_ScreencastHelper | ||
* Method: closeSession | ||
* Signature: ()V | ||
*/ | ||
JNIEXPORT void JNICALL | ||
Java_sun_awt_screencast_ScreencastHelper_closeSession(JNIEnv *env, jclass cls) { | ||
DEBUG_SCREENCAST("closing screencast session\n\n", NULL); | ||
doCleanup(); | ||
} | ||
|
||
/* | ||
* Class: sun_awt_screencast_ScreencastHelper | ||
* Method: getRGBPixelsImpl | ||
|
@@ -805,7 +860,7 @@ JNIEXPORT jint JNICALL Java_sun_awt_screencast_ScreencastHelper_getRGBPixelsImpl | |
boundsLen = (*env)->GetArrayLength(env, affectedScreensBoundsArray); | ||
EXCEPTION_CHECK_DESCRIBE(); | ||
if (boundsLen % 4 != 0) { | ||
DEBUG_SCREENCAST("%s:%i incorrect array length\n", __FUNCTION__, __LINE__); | ||
DEBUG_SCREENCAST("incorrect array length\n", NULL); | ||
return RESULT_ERROR; | ||
} | ||
affectedBoundsLength = boundsLen / 4; | ||
|
@@ -896,11 +951,12 @@ JNIEXPORT jint JNICALL Java_sun_awt_screencast_ScreencastHelper_getRGBPixelsImpl | |
|
||
fp_pw_thread_loop_lock(pw.loop); | ||
fp_pw_stream_set_active(screenProps->data->stream, FALSE); | ||
fp_pw_stream_disconnect(screenProps->data->stream); | ||
fp_pw_thread_loop_unlock(pw.loop); | ||
|
||
screenProps->captureDataReady = FALSE; | ||
} | ||
} | ||
doCleanup(); | ||
|
||
releaseToken(env, jtoken, token); | ||
return 0; | ||
} |
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
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.
What happen if the "closeSession" is executed why the new Robot#getRGBPixels is called? In the robot code we call timerCloseSessionRestart to restart/cancel timer, but I think it could be possible that closeSession() is already executed and may close session right before it will be used by the robot but after we "open" it.
Or this case is not possible?
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.
Both
ScreencastHelper#getRGBPixels
(Robot#getRGBPixels
calls it internally) andScreencastHelper#closeSession
are synchronized.So
ScreencastHelper#closeSession
will wait until the execution ofScreencastHelper#getRGBPixels
is finished and vice versa.Many hours of testing with random delays close to DELAY_BEFORE_SESSION_CLOSE worked fine.
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.
Java part looks good to me. Why 2s and not more or less ?
The delay could be set through a system property ?
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.
This is the average value chosen so as not to slow down tests execution, and not to annoy the user with the "Screen is being shared" icon while the session is still open.
This functionality could be added, but I don't see much reason to do so.
If a screenshot is taken from time to time, the benefit of holding the session for a long time will be quite small compared to frequent requests.
Plus it requires some CSR work.