From nobody Thu Nov 28 09:43:18 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1693828546; cv=none; d=zohomail.com; s=zohoarc; b=doYQnLrrYN9Foapbdi5YwPWGMECLVXB603mI1vFr0f+39t3jlaSt3zxvoL9Emy6CIty7BcH4zXIwYiu1JI6PQEIiEPnPngh1P4ilIszv1i+RNmp4p+7TQ1D2rjUU6E4pjGj2uo0/rVEtPMuAKgSzU6aNInwS70ZcnhvB4k7YrH8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693828546; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=y1MGrArPqzHA8EdRM0SFKnq0sku8Y9CAUZaaK9jJl0Y=; b=LUc+wK+rn9O7tV252gthdK+SubiWsYzeiZJDD5nfh7qInbhz+Qe7ccKxOCIqhYP6PraEM6ADuxEtiHpxSs2dqmL+Zu4LLlm+wrSU14S4WKakAjnoSBhCiO6n5zfl19pUWqsXcK7hQA7na0BY96kGz/WBPBReNxfCLKrtGYMWJto= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16938285461151010.7552864028688; Mon, 4 Sep 2023 04:55:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qd88p-00025i-97; Mon, 04 Sep 2023 07:53:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd88l-00022s-Uu for qemu-devel@nongnu.org; Mon, 04 Sep 2023 07:53:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qd88j-0008G7-1T for qemu-devel@nongnu.org; Mon, 04 Sep 2023 07:53:11 -0400 Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-352-HW2Z4IpFN2GH1Sn-mk1MBg-1; Mon, 04 Sep 2023 07:53:07 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B59FA3C02B88 for ; Mon, 4 Sep 2023 11:53:06 +0000 (UTC) Received: from localhost (unknown [10.39.208.42]) by smtp.corp.redhat.com (Postfix) with ESMTP id 20379140E963; Mon, 4 Sep 2023 11:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693828388; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y1MGrArPqzHA8EdRM0SFKnq0sku8Y9CAUZaaK9jJl0Y=; b=eHaNU0zpPE/vzQO6h3k7reAVNlodjh192qsEvOiGNijwLXxOroTwRRgZrOOw8q+GK7b/fb ODU24FyGTgtzM06ldLKBUuNHmZXONWYL+XL3SNYNsl5ugL/uQgpAlElHGPsJOR4gtnNFS8 DV4SKX/SwR494j1MdmV2VptfLS0K4DY= X-MC-Unique: HW2Z4IpFN2GH1Sn-mk1MBg-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Cc: stefanha@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Gerd Hoffmann Subject: [PULL 03/52] ui/qmp: move screendump to ui-qmp-cmds.c Date: Mon, 4 Sep 2023 15:52:00 +0400 Message-ID: <20230904115251.4161397-4-marcandre.lureau@redhat.com> In-Reply-To: <20230904115251.4161397-1-marcandre.lureau@redhat.com> References: <20230904115251.4161397-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693828546487100001 From: Marc-Andr=C3=A9 Lureau console.c unit is over-crowded. This code is specific to the handling of the QMP screendump command, so move it in ui-qmp-cmds. Signed-off-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Daniel P. Berrang=C3=A9 Message-Id: <20230830093843.3531473-4-marcandre.lureau@redhat.com> --- include/ui/console.h | 1 + ui/console.c | 212 ++++--------------------------------------- ui/ui-qmp-cmds.c | 187 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+), 195 deletions(-) diff --git a/include/ui/console.h b/include/ui/console.h index 3e8b22d6c6..1b08b0f8ad 100644 --- a/include/ui/console.h +++ b/include/ui/console.h @@ -504,6 +504,7 @@ void qemu_console_set_window_id(QemuConsole *con, int w= indow_id); void console_select(unsigned int index); void qemu_console_resize(QemuConsole *con, int width, int height); DisplaySurface *qemu_console_surface(QemuConsole *con); +void coroutine_fn qemu_console_co_wait_update(QemuConsole *con); =20 /* console-gl.c */ #ifdef CONFIG_OPENGL diff --git a/ui/console.c b/ui/console.c index 8da2170a7e..9c17024dbc 100644 --- a/ui/console.c +++ b/ui/console.c @@ -28,8 +28,8 @@ #include "qapi/error.h" #include "qapi/qapi-commands-ui.h" #include "qemu/coroutine.h" -#include "qemu/error-report.h" #include "qemu/fifo8.h" +#include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/module.h" #include "qemu/option.h" @@ -37,11 +37,7 @@ #include "chardev/char.h" #include "trace.h" #include "exec/memory.h" -#include "io/channel-file.h" #include "qom/object.h" -#ifdef CONFIG_PNG -#include -#endif =20 #define DEFAULT_BACKSCROLL 512 #define CONSOLE_CURSOR_PERIOD 500 @@ -239,6 +235,22 @@ void graphic_hw_update(QemuConsole *con) } } =20 +static void graphic_hw_update_bh(void *con) +{ + graphic_hw_update(con); +} + +void qemu_console_co_wait_update(QemuConsole *con) +{ + if (qemu_co_queue_empty(&con->dump_queue)) { + /* Defer the update, it will restart the pending coroutines */ + aio_bh_schedule_oneshot(qemu_get_aio_context(), + graphic_hw_update_bh, con); + } + qemu_co_queue_wait(&con->dump_queue, NULL); + +} + static void graphic_hw_gl_unblock_timer(void *opaque) { warn_report("console: no gl-unblock within one second"); @@ -292,196 +304,6 @@ void graphic_hw_invalidate(QemuConsole *con) } } =20 -#ifdef CONFIG_PNG -/** - * png_save: Take a screenshot as PNG - * - * Saves screendump as a PNG file - * - * Returns true for success or false for error. - * - * @fd: File descriptor for PNG file. - * @image: Image data in pixman format. - * @errp: Pointer to an error. - */ -static bool png_save(int fd, pixman_image_t *image, Error **errp) -{ - int width =3D pixman_image_get_width(image); - int height =3D pixman_image_get_height(image); - png_struct *png_ptr; - png_info *info_ptr; - g_autoptr(pixman_image_t) linebuf =3D - qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); - uint8_t *buf =3D (uint8_t *)pixman_image_get_data(linebuf); - FILE *f =3D fdopen(fd, "wb"); - int y; - if (!f) { - error_setg_errno(errp, errno, - "Failed to create file from file descriptor"); - return false; - } - - png_ptr =3D png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, - NULL, NULL); - if (!png_ptr) { - error_setg(errp, "PNG creation failed. Unable to write struct"); - fclose(f); - return false; - } - - info_ptr =3D png_create_info_struct(png_ptr); - - if (!info_ptr) { - error_setg(errp, "PNG creation failed. Unable to write info"); - fclose(f); - png_destroy_write_struct(&png_ptr, &info_ptr); - return false; - } - - png_init_io(png_ptr, f); - - png_set_IHDR(png_ptr, info_ptr, width, height, 8, - PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - - png_write_info(png_ptr, info_ptr); - - for (y =3D 0; y < height; ++y) { - qemu_pixman_linebuf_fill(linebuf, image, width, 0, y); - png_write_row(png_ptr, buf); - } - - png_write_end(png_ptr, NULL); - - png_destroy_write_struct(&png_ptr, &info_ptr); - - if (fclose(f) !=3D 0) { - error_setg_errno(errp, errno, - "PNG creation failed. Unable to close file"); - return false; - } - - return true; -} - -#else /* no png support */ - -static bool png_save(int fd, pixman_image_t *image, Error **errp) -{ - error_setg(errp, "Enable PNG support with libpng for screendump"); - return false; -} - -#endif /* CONFIG_PNG */ - -static bool ppm_save(int fd, pixman_image_t *image, Error **errp) -{ - int width =3D pixman_image_get_width(image); - int height =3D pixman_image_get_height(image); - g_autoptr(Object) ioc =3D OBJECT(qio_channel_file_new_fd(fd)); - g_autofree char *header =3D NULL; - g_autoptr(pixman_image_t) linebuf =3D NULL; - int y; - - trace_ppm_save(fd, image); - - header =3D g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255); - if (qio_channel_write_all(QIO_CHANNEL(ioc), - header, strlen(header), errp) < 0) { - return false; - } - - linebuf =3D qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); - for (y =3D 0; y < height; y++) { - qemu_pixman_linebuf_fill(linebuf, image, width, 0, y); - if (qio_channel_write_all(QIO_CHANNEL(ioc), - (char *)pixman_image_get_data(linebuf), - pixman_image_get_stride(linebuf), errp) = < 0) { - return false; - } - } - - return true; -} - -static void graphic_hw_update_bh(void *con) -{ - graphic_hw_update(con); -} - -/* Safety: coroutine-only, concurrent-coroutine safe, main thread only */ -void coroutine_fn -qmp_screendump(const char *filename, const char *device, - bool has_head, int64_t head, - bool has_format, ImageFormat format, Error **errp) -{ - g_autoptr(pixman_image_t) image =3D NULL; - QemuConsole *con; - DisplaySurface *surface; - int fd; - - if (device) { - con =3D qemu_console_lookup_by_device_name(device, has_head ? head= : 0, - errp); - if (!con) { - return; - } - } else { - if (has_head) { - error_setg(errp, "'head' must be specified together with 'devi= ce'"); - return; - } - con =3D qemu_console_lookup_by_index(0); - if (!con) { - error_setg(errp, "There is no console to take a screendump fro= m"); - return; - } - } - - if (qemu_co_queue_empty(&con->dump_queue)) { - /* Defer the update, it will restart the pending coroutines */ - aio_bh_schedule_oneshot(qemu_get_aio_context(), - graphic_hw_update_bh, con); - } - qemu_co_queue_wait(&con->dump_queue, NULL); - - /* - * All pending coroutines are woken up, while the BQL is held. No - * further graphic update are possible until it is released. Take - * an image ref before that. - */ - surface =3D qemu_console_surface(con); - if (!surface) { - error_setg(errp, "no surface"); - return; - } - image =3D pixman_image_ref(surface->image); - - fd =3D qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY= , 0666); - if (fd =3D=3D -1) { - error_setg(errp, "failed to open file '%s': %s", filename, - strerror(errno)); - return; - } - - /* - * The image content could potentially be updated as the coroutine - * yields and releases the BQL. It could produce corrupted dump, but - * it should be otherwise safe. - */ - if (has_format && format =3D=3D IMAGE_FORMAT_PNG) { - /* PNG format specified for screendump */ - if (!png_save(fd, image, errp)) { - qemu_unlink(filename); - } - } else { - /* PPM format specified/default for screendump */ - if (!ppm_save(fd, image, errp)) { - qemu_unlink(filename); - } - } -} - void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata) { if (!con) { diff --git a/ui/ui-qmp-cmds.c b/ui/ui-qmp-cmds.c index a37a7024f3..debc07d678 100644 --- a/ui/ui-qmp-cmds.c +++ b/ui/ui-qmp-cmds.c @@ -14,13 +14,20 @@ */ =20 #include "qemu/osdep.h" + +#include "io/channel-file.h" #include "monitor/qmp-helpers.h" #include "qapi/qapi-commands-ui.h" #include "qapi/qmp/qerror.h" +#include "qemu/coroutine.h" #include "qemu/cutils.h" +#include "trace.h" #include "ui/console.h" #include "ui/dbus-display.h" #include "ui/qemu-spice.h" +#ifdef CONFIG_PNG +#include +#endif =20 void qmp_set_password(SetPasswordOptions *opts, Error **errp) { @@ -204,3 +211,183 @@ void qmp_client_migrate_info(const char *protocol, co= nst char *hostname, =20 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'"); } + +#ifdef CONFIG_PNG +/** + * png_save: Take a screenshot as PNG + * + * Saves screendump as a PNG file + * + * Returns true for success or false for error. + * + * @fd: File descriptor for PNG file. + * @image: Image data in pixman format. + * @errp: Pointer to an error. + */ +static bool png_save(int fd, pixman_image_t *image, Error **errp) +{ + int width =3D pixman_image_get_width(image); + int height =3D pixman_image_get_height(image); + png_struct *png_ptr; + png_info *info_ptr; + g_autoptr(pixman_image_t) linebuf =3D + qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); + uint8_t *buf =3D (uint8_t *)pixman_image_get_data(linebuf); + FILE *f =3D fdopen(fd, "wb"); + int y; + if (!f) { + error_setg_errno(errp, errno, + "Failed to create file from file descriptor"); + return false; + } + + png_ptr =3D png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, + NULL, NULL); + if (!png_ptr) { + error_setg(errp, "PNG creation failed. Unable to write struct"); + fclose(f); + return false; + } + + info_ptr =3D png_create_info_struct(png_ptr); + + if (!info_ptr) { + error_setg(errp, "PNG creation failed. Unable to write info"); + fclose(f); + png_destroy_write_struct(&png_ptr, &info_ptr); + return false; + } + + png_init_io(png_ptr, f); + + png_set_IHDR(png_ptr, info_ptr, width, height, 8, + PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + png_write_info(png_ptr, info_ptr); + + for (y =3D 0; y < height; ++y) { + qemu_pixman_linebuf_fill(linebuf, image, width, 0, y); + png_write_row(png_ptr, buf); + } + + png_write_end(png_ptr, NULL); + + png_destroy_write_struct(&png_ptr, &info_ptr); + + if (fclose(f) !=3D 0) { + error_setg_errno(errp, errno, + "PNG creation failed. Unable to close file"); + return false; + } + + return true; +} + +#else /* no png support */ + +static bool png_save(int fd, pixman_image_t *image, Error **errp) +{ + error_setg(errp, "Enable PNG support with libpng for screendump"); + return false; +} + +#endif /* CONFIG_PNG */ + +static bool ppm_save(int fd, pixman_image_t *image, Error **errp) +{ + int width =3D pixman_image_get_width(image); + int height =3D pixman_image_get_height(image); + g_autoptr(Object) ioc =3D OBJECT(qio_channel_file_new_fd(fd)); + g_autofree char *header =3D NULL; + g_autoptr(pixman_image_t) linebuf =3D NULL; + int y; + + trace_ppm_save(fd, image); + + header =3D g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255); + if (qio_channel_write_all(QIO_CHANNEL(ioc), + header, strlen(header), errp) < 0) { + return false; + } + + linebuf =3D qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width); + for (y =3D 0; y < height; y++) { + qemu_pixman_linebuf_fill(linebuf, image, width, 0, y); + if (qio_channel_write_all(QIO_CHANNEL(ioc), + (char *)pixman_image_get_data(linebuf), + pixman_image_get_stride(linebuf), errp) = < 0) { + return false; + } + } + + return true; +} + +/* Safety: coroutine-only, concurrent-coroutine safe, main thread only */ +void coroutine_fn +qmp_screendump(const char *filename, const char *device, + bool has_head, int64_t head, + bool has_format, ImageFormat format, Error **errp) +{ + g_autoptr(pixman_image_t) image =3D NULL; + QemuConsole *con; + DisplaySurface *surface; + int fd; + + if (device) { + con =3D qemu_console_lookup_by_device_name(device, has_head ? head= : 0, + errp); + if (!con) { + return; + } + } else { + if (has_head) { + error_setg(errp, "'head' must be specified together with 'devi= ce'"); + return; + } + con =3D qemu_console_lookup_by_index(0); + if (!con) { + error_setg(errp, "There is no console to take a screendump fro= m"); + return; + } + } + + qemu_console_co_wait_update(con); + + /* + * All pending coroutines are woken up, while the BQL is held. No + * further graphic update are possible until it is released. Take + * an image ref before that. + */ + surface =3D qemu_console_surface(con); + if (!surface) { + error_setg(errp, "no surface"); + return; + } + image =3D pixman_image_ref(surface->image); + + fd =3D qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY= , 0666); + if (fd =3D=3D -1) { + error_setg(errp, "failed to open file '%s': %s", filename, + strerror(errno)); + return; + } + + /* + * The image content could potentially be updated as the coroutine + * yields and releases the BQL. It could produce corrupted dump, but + * it should be otherwise safe. + */ + if (has_format && format =3D=3D IMAGE_FORMAT_PNG) { + /* PNG format specified for screendump */ + if (!png_save(fd, image, errp)) { + qemu_unlink(filename); + } + } else { + /* PPM format specified/default for screendump */ + if (!ppm_save(fd, image, errp)) { + qemu_unlink(filename); + } + } +} --=20 2.41.0