Skip to content

Commit

Permalink
Add support for VRAM-compressed custom mouse cursor images
Browse files Browse the repository at this point in the history
No memory is actually saved when using this, so lossless modes are
still recommended as they look better and load faster.
  • Loading branch information
Calinou committed Apr 21, 2023
1 parent 6f1a52b commit 303bf24
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/classes/Input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
[param image]'s size must be lower than 256×256.
[param hotspot] must be within [param image]'s size.
[b]Note:[/b] [AnimatedTexture]s aren't supported as custom mouse cursors. If using an [AnimatedTexture], only the first frame will be displayed.
[b]Note:[/b] Only images imported with the [b]Lossless[/b], [b]Lossy[/b] or [b]Uncompressed[/b] compression modes are supported. The [b]Video RAM[/b] compression mode can't be used for custom cursors.
[b]Note:[/b] The [b]Lossless[/b], [b]Lossy[/b] or [b]Uncompressed[/b] compression modes are recommended. The [b]Video RAM[/b] compression mode can be used, but it will be decompressed on the CPU, which means loading times are slowed down and no memory is saved compared to lossless modes.
[b]Note:[/b] On the web platform, the maximum allowed cursor image size is 128×128. Cursor images larger than 32×32 will also only be displayed if the mouse cursor image is entirely located within the page for [url=https://chromestatus.com/feature/5825971391299584]security reasons[/url].
</description>
</method>
Expand Down
5 changes: 5 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,11 @@ void DisplayServerX11::cursor_set_custom_image(const Ref<Resource> &p_cursor, Cu
Ref<Image> image = texture->get_image();

ERR_FAIL_COND(!image.is_valid());
if (image->is_compressed()) {
image = image->duplicate(true);
Error err = image->decompress();
ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
}

// Create the cursor structure
XcursorImage *cursor_image = XcursorImageCreate(texture_size.width, texture_size.height);
Expand Down
5 changes: 5 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,11 @@
Ref<Image> image = texture->get_image();

ERR_FAIL_COND(!image.is_valid());
if (image->is_compressed()) {
image = image->duplicate(true);
Error err = image->decompress();
ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
}

NSBitmapImageRep *imgrep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nullptr
Expand Down
7 changes: 6 additions & 1 deletion platform/web/display_server_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,12 @@ void DisplayServerWeb::cursor_set_custom_image(const Ref<Resource> &p_cursor, Cu

ERR_FAIL_COND(!image.is_valid());

image = image->duplicate();
image = image->duplicate(true);

if (image->is_compressed()) {
Error err = image->decompress();
ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
}

if (atlas_texture.is_valid()) {
image->crop_from_point(
Expand Down
5 changes: 5 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,11 @@ void DisplayServerWindows::cursor_set_custom_image(const Ref<Resource> &p_cursor
Ref<Image> image = texture->get_image();

ERR_FAIL_COND(!image.is_valid());
if (image->is_compressed()) {
image = image->duplicate(true);
Error err = image->decompress();
ERR_FAIL_COND_MSG(err != OK, "Couldn't decompress VRAM-compressed custom mouse cursor image. Switch to a lossless compression mode in the Import dock.");
}

UINT image_size = texture_size.width * texture_size.height;

Expand Down

0 comments on commit 303bf24

Please sign in to comment.