gimp/app/display/gimpdisplayshell-render.c

403 lines
14 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
1997-11-25 06:05:25 +08:00
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
1997-11-25 06:05:25 +08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
1997-11-25 06:05:25 +08:00
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1997-11-25 06:05:25 +08:00
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "display-types.h"
#include "config/gimpdisplayconfig.h"
2012-05-06 10:39:23 +08:00
#include "gegl/gimp-gegl-utils.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimppickable.h"
#ifdef USE_NODE_BLIT
#include "core/gimpprojectable.h"
#endif
#include "gimpdisplay.h"
2001-11-01 05:20:09 +08:00
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-transform.h"
#include "gimpdisplayshell-filter.h"
#include "gimpdisplayshell-profile.h"
app/display/Makefile.am app/display/gimpdisplay-callbacks.[ch] 2001-11-01 Michael Natterer <mitch@gimp.org> * app/display/Makefile.am * app/display/gimpdisplay-callbacks.[ch] * app/display/gimpdisplay-render.[ch] * app/display/gimpdisplay-scale.[ch] * app/display/gimpdisplay-scroll.[ch]: removed and added as gimpdisplayshell-foo.[ch] because they are all methods of the shell. * app/display/gimpdisplay.[ch] * app/display/gimpdisplayshell.[ch]: moved the "offset" and "size" variables from GimpDisplay to GimpDisplayShell. GimpDisplay should know nothing about screen coordinates. The gdisplay_[un]transform_foo() methods are still part of GimpDisplay but will be moved to GimpDisplayShell as soon as the tools' vitrual functions speak in image coordinates instead of GdkEvents. * app/display/gimpdisplayshell-callbacks.[ch]: prefixed all functions with gimp_display_shell_*. Moved some stuff to a "realize" callback File still has to be renamed. * app/display/gimpdisplay-foreach.[ch]: removed gdisplays_shrink_wrap(). * app/gui/menus.c * app/gui/view-commands.[ch] * app/display/gimpdisplayshell-scale.[ch]: implemented "Zoom to Fit Window" function (#57670). * app/nav_window.c * app/display/gimpdisplay-handlers.c * app/display/gimpdisplayshell-render.[ch] * app/display/gimpdisplayshell-scale.[ch] * app/display/gimpdisplayshell-scroll.[ch] * app/gui/colormap-dialog.c * app/gui/gui.c * app/gui/preferences-dialog.c * app/tools/gimpmagnifytool.c * app/tools/gimpmovetool.c * app/widgets/gimppreview.c: changed according to variable and filename changes. * app/tools/tool_manager.c: tool_manager_select_tool(): send the active tool a "HALT" command before selecting the new one. Fixes stale tool dialogs which were there because some other hack was removed (This is IMHO the right place to shut down the active tool). * app/tools/gimpcroptool.c: don't shrink wrap after cropping but let gimprc.allow_resize_windows decide. * app/tools/gimpselectiontool.c: gimage_mask_value() takes image, not screen coordinates. A good example of how braindead it is to pass GdkEvents to tools :-) Fixes incorrect cursor and oper update of the selection tools. * app/tools/gimptransformtool.c * app/undo.c: removed (#if 0 for now) some strange code which did manual exposing of GimpDisplayShell areas. This was definitely a hack and should not be there given the image emits correct "update" signals.
2001-11-02 17:31:21 +08:00
#include "gimpdisplayshell-render.h"
#include "gimpdisplayshell-scroll.h"
#include "gimpdisplayxfer.h"
/* #define GIMP_DISPLAY_RENDER_ENABLE_SCALING 1 */
1997-11-25 06:05:25 +08:00
void
gimp_display_shell_render (GimpDisplayShell *shell,
cairo_t *cr,
gint x,
gint y,
gint w,
gint h)
1997-11-25 06:05:25 +08:00
{
GimpImage *image;
GeglBuffer *buffer;
#ifdef USE_NODE_BLIT
GeglNode *node;
#endif
gdouble scale_x = 1.0;
gdouble scale_y = 1.0;
gdouble buffer_scale = 1.0;
gint viewport_offset_x;
gint viewport_offset_y;
gint viewport_width;
gint viewport_height;
gint scaled_x;
gint scaled_y;
gint scaled_width;
gint scaled_height;
app: Use SHM transport for data transfer for display Recent Cairo uses SHM transports when available, and exposes the ability for its users to manage images shared between it and the display. This allows us to eliminate copies, and if the architecture supports it even to upload directly into GPU addressable memory without any copies (all in normal system memory so we suffer no performance penalty when applying the filters). The caveat is that we need to be aware of the synchronize requirements, the cairo_surface_flush and cairo_surface_mark_dirty, around access to the transport image. To reduce the frequency of these barriers, we can subdivide the transport image into small chunks as to satisfy individual updates and delay the synchronisation barrier until we are forced to reuse earlier pixels. Note this bumps the required Cairo version to 1.12, and please be aware that the XSHM transport requires bug fixes from cairo.git (will be 1.12.12) v2: After further reflections with Mitch, we realized we can share the transport surface between all canvases by attaching it to the common screen. v3: Fix a couple of typos in insert_node() introduced when switching variables names. v4: Encapsulating within an image surface rather than a subsurface was hiding the backing SHM segment from cairo, causing it to allocate further SHM resources to stream the upload. We should be able to use a sub-surface here, but it is more convenient to wrap the pixels in an image surface for rendering the filters (and conveniently masking the callee flushes from invalidating our parent transport surface). Cc: Michael Natterer <mitch@gimp.org>
2013-01-30 17:50:54 +08:00
cairo_surface_t *xfer;
gint xfer_src_x;
gint xfer_src_y;
gint mask_src_x = 0;
gint mask_src_y = 0;
gint cairo_stride;
guchar *cairo_data;
GeglBuffer *cairo_buffer;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (cr != NULL);
g_return_if_fail (w > 0 && h > 0);
image = gimp_display_get_image (shell->display);
buffer = gimp_pickable_get_buffer (GIMP_PICKABLE (image));
#ifdef USE_NODE_BLIT
node = gimp_projectable_get_graph (GIMP_PROJECTABLE (image));
#endif
#ifdef GIMP_DISPLAY_RENDER_ENABLE_SCALING
/* if we had this future API, things would look pretty on hires (retina) */
scale_x = gdk_window_get_scale_factor (gtk_widget_get_window (gtk_widget_get_toplevel (GTK_WIDGET (shell))));
#endif
scale_x = MIN (scale_x, GIMP_DISPLAY_RENDER_MAX_SCALE);
scale_y = scale_x;
if (shell->scale_x > shell->scale_y)
{
scale_y *= (shell->scale_x / shell->scale_y);
buffer_scale = shell->scale_y * scale_y;
}
else if (shell->scale_y > shell->scale_x)
{
scale_x *= (shell->scale_y / shell->scale_x);
buffer_scale = shell->scale_x * scale_x;
}
else
{
buffer_scale = shell->scale_x * scale_x;
}
gimp_display_shell_scroll_get_scaled_viewport (shell,
&viewport_offset_x,
&viewport_offset_y,
&viewport_width,
&viewport_height);
scaled_x = floor ((x + viewport_offset_x) * scale_x);
scaled_y = floor ((y + viewport_offset_y) * scale_y);
scaled_width = ceil (w * scale_x);
scaled_height = ceil (h * scale_y);
if (shell->rotate_transform)
{
xfer = cairo_surface_create_similar_image (cairo_get_target (cr),
CAIRO_FORMAT_ARGB32,
scaled_width,
scaled_height);
cairo_surface_mark_dirty (xfer);
xfer_src_x = 0;
xfer_src_y = 0;
}
else
{
xfer = gimp_display_xfer_get_surface (shell->xfer,
scaled_width,
scaled_height,
&xfer_src_x,
&xfer_src_y);
}
app: Use SHM transport for data transfer for display Recent Cairo uses SHM transports when available, and exposes the ability for its users to manage images shared between it and the display. This allows us to eliminate copies, and if the architecture supports it even to upload directly into GPU addressable memory without any copies (all in normal system memory so we suffer no performance penalty when applying the filters). The caveat is that we need to be aware of the synchronize requirements, the cairo_surface_flush and cairo_surface_mark_dirty, around access to the transport image. To reduce the frequency of these barriers, we can subdivide the transport image into small chunks as to satisfy individual updates and delay the synchronisation barrier until we are forced to reuse earlier pixels. Note this bumps the required Cairo version to 1.12, and please be aware that the XSHM transport requires bug fixes from cairo.git (will be 1.12.12) v2: After further reflections with Mitch, we realized we can share the transport surface between all canvases by attaching it to the common screen. v3: Fix a couple of typos in insert_node() introduced when switching variables names. v4: Encapsulating within an image surface rather than a subsurface was hiding the backing SHM segment from cairo, causing it to allocate further SHM resources to stream the upload. We should be able to use a sub-surface here, but it is more convenient to wrap the pixels in an image surface for rendering the filters (and conveniently masking the callee flushes from invalidating our parent transport surface). Cc: Michael Natterer <mitch@gimp.org>
2013-01-30 17:50:54 +08:00
cairo_stride = cairo_image_surface_get_stride (xfer);
cairo_data = cairo_image_surface_get_data (xfer) +
xfer_src_y * cairo_stride + xfer_src_x * 4;
cairo_buffer = gegl_buffer_linear_new_from_data (cairo_data,
babl_format ("cairo-ARGB32"),
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height),
cairo_stride,
NULL, NULL);
if (shell->profile_transform ||
gimp_display_shell_has_filter (shell))
{
/* if there is a profile transform or a display filter, we need
* to use temp buffers
*/
/* create the filter buffer if we have filters
*/
if (gimp_display_shell_has_filter (shell) &&
! shell->filter_buffer)
{
gint w = GIMP_DISPLAY_RENDER_BUF_WIDTH * GIMP_DISPLAY_RENDER_MAX_SCALE;
gint h = GIMP_DISPLAY_RENDER_BUF_HEIGHT * GIMP_DISPLAY_RENDER_MAX_SCALE;
shell->filter_data =
gegl_malloc (w * h * babl_format_get_bytes_per_pixel (shell->filter_format));
shell->filter_stride =
w * babl_format_get_bytes_per_pixel (shell->filter_format);
shell->filter_buffer =
gegl_buffer_linear_new_from_data (shell->filter_data,
shell->filter_format,
GEGL_RECTANGLE (0, 0, w, h),
GEGL_AUTO_ROWSTRIDE,
(GDestroyNotify) gegl_free,
shell->filter_data);
}
if (shell->profile_transform)
{
/* if there is a profile transform, load the projection
* pixels into the profile_buffer
*/
#ifndef USE_NODE_BLIT
gegl_buffer_get (buffer,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
buffer_scale,
shell->profile_src_format,
shell->profile_data, shell->profile_stride,
GEGL_ABYSS_CLAMP);
#else
gegl_node_blit (node,
buffer_scale,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
shell->profile_src_format,
shell->profile_data, shell->profile_stride,
GEGL_BLIT_CACHE);
#endif
if (gimp_display_shell_has_filter (shell))
{
/* if there are filters, convert the pixels from the
* profile_buffer to the filter_buffer
*/
gimp_display_shell_profile_convert_buffer (shell,
shell->profile_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height),
shell->filter_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height));
}
else
{
/* otherwise, convert the profile_buffer directly into
* the cairo_buffer
*/
gimp_display_shell_profile_convert_buffer (shell,
shell->profile_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height),
cairo_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height));
}
}
else
{
/* otherwise, load the projection pixels directly into the
* filter_buffer
*/
#ifndef USE_NODE_BLIT
gegl_buffer_get (buffer,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
buffer_scale,
shell->filter_format,
shell->filter_data, shell->filter_stride,
GEGL_ABYSS_CLAMP);
#else
gegl_node_blit (node,
buffer_scale,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
shell->filter_format,
shell->filter_data, shell->filter_stride,
GEGL_BLIT_CACHE);
#endif
}
if (gimp_display_shell_has_filter (shell))
{
/* convert the filter_buffer in place
*/
gimp_color_display_stack_convert_buffer (shell->filter_stack,
shell->filter_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height));
/* finally, copy the filter buffer to the cairo-ARGB32 buffer
*/
gegl_buffer_get (shell->filter_buffer,
GEGL_RECTANGLE (0, 0,
scaled_width,
scaled_height),
1.0,
babl_format ("cairo-ARGB32"),
cairo_data, cairo_stride,
GEGL_ABYSS_CLAMP);
}
}
else
{
/* otherwise we can copy the projection pixels straight to the
* cairo-ARGB32 buffer
*/
#ifndef USE_NODE_BLIT
gegl_buffer_get (buffer,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
buffer_scale,
babl_format ("cairo-ARGB32"),
cairo_data, cairo_stride,
GEGL_ABYSS_CLAMP);
#else
gegl_node_blit (node,
buffer_scale,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
babl_format ("cairo-ARGB32"),
cairo_data, cairo_stride,
GEGL_BLIT_CACHE);
#endif
}
g_object_unref (cairo_buffer);
if (shell->mask)
{
if (! shell->mask_surface)
{
shell->mask_surface =
cairo_image_surface_create (CAIRO_FORMAT_A8,
GIMP_DISPLAY_RENDER_BUF_WIDTH *
GIMP_DISPLAY_RENDER_MAX_SCALE,
GIMP_DISPLAY_RENDER_BUF_HEIGHT *
GIMP_DISPLAY_RENDER_MAX_SCALE);
}
cairo_surface_mark_dirty (shell->mask_surface);
cairo_stride = cairo_image_surface_get_stride (shell->mask_surface);
cairo_data = cairo_image_surface_get_data (shell->mask_surface) +
mask_src_y * cairo_stride + mask_src_x * 4;
gegl_buffer_get (shell->mask,
GEGL_RECTANGLE (scaled_x, scaled_y,
scaled_width, scaled_height),
buffer_scale,
babl_format ("Y u8"),
cairo_data, cairo_stride,
GEGL_ABYSS_CLAMP);
if (shell->mask_inverted)
{
gint mask_height = scaled_height;
while (mask_height--)
{
gint mask_width = scaled_width;
guchar *d = cairo_data;
while (mask_width--)
{
guchar inv = 255 - *d;
*d++ = inv;
}
cairo_data += cairo_stride;
}
}
}
/* put it to the screen */
cairo_save (cr);
cairo_rectangle (cr, x, y, w, h);
cairo_scale (cr, 1.0 / scale_x, 1.0 / scale_y);
app: Use SHM transport for data transfer for display Recent Cairo uses SHM transports when available, and exposes the ability for its users to manage images shared between it and the display. This allows us to eliminate copies, and if the architecture supports it even to upload directly into GPU addressable memory without any copies (all in normal system memory so we suffer no performance penalty when applying the filters). The caveat is that we need to be aware of the synchronize requirements, the cairo_surface_flush and cairo_surface_mark_dirty, around access to the transport image. To reduce the frequency of these barriers, we can subdivide the transport image into small chunks as to satisfy individual updates and delay the synchronisation barrier until we are forced to reuse earlier pixels. Note this bumps the required Cairo version to 1.12, and please be aware that the XSHM transport requires bug fixes from cairo.git (will be 1.12.12) v2: After further reflections with Mitch, we realized we can share the transport surface between all canvases by attaching it to the common screen. v3: Fix a couple of typos in insert_node() introduced when switching variables names. v4: Encapsulating within an image surface rather than a subsurface was hiding the backing SHM segment from cairo, causing it to allocate further SHM resources to stream the upload. We should be able to use a sub-surface here, but it is more convenient to wrap the pixels in an image surface for rendering the filters (and conveniently masking the callee flushes from invalidating our parent transport surface). Cc: Michael Natterer <mitch@gimp.org>
2013-01-30 17:50:54 +08:00
cairo_set_source_surface (cr, xfer,
x * scale_x - xfer_src_x,
y * scale_y - xfer_src_y);
if (shell->rotate_transform)
{
cairo_pattern_t *pattern;
pattern = cairo_get_source (cr);
cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
cairo_set_line_width (cr, 1.0);
cairo_stroke_preserve (cr);
cairo_surface_destroy (xfer);
}
cairo_clip (cr);
cairo_paint (cr);
if (shell->mask)
{
gimp_cairo_set_source_rgba (cr, &shell->mask_color);
cairo_mask_surface (cr, shell->mask_surface,
(x - mask_src_x) * scale_x,
(y - mask_src_y) * scale_y);
}
cairo_restore (cr);
1997-11-25 06:05:25 +08:00
}