2019-05-01 21:58:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2016 Endless
|
|
|
|
* 2018 Benjamin Otte
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: Benjamin Otte <otte@gnome.org>
|
|
|
|
*/
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-05-01 21:58:57 +00:00
|
|
|
#include "gskcairorenderer.h"
|
2016-03-17 13:48:19 +00:00
|
|
|
|
|
|
|
#include "gskdebugprivate.h"
|
|
|
|
#include "gskrendererprivate.h"
|
|
|
|
#include "gskrendernodeprivate.h"
|
2024-07-02 15:39:16 +00:00
|
|
|
#include "gdk/gdkcolorstateprivate.h"
|
|
|
|
#include "gdk/gdkdrawcontextprivate.h"
|
2017-11-02 20:39:00 +00:00
|
|
|
#include "gdk/gdktextureprivate.h"
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-11-20 04:28:52 +00:00
|
|
|
typedef struct {
|
|
|
|
GQuark cpu_time;
|
|
|
|
GQuark gpu_time;
|
|
|
|
} ProfileTimers;
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
struct _GskCairoRenderer
|
|
|
|
{
|
|
|
|
GskRenderer parent_instance;
|
|
|
|
|
2018-04-11 22:16:43 +00:00
|
|
|
GdkCairoContext *cairo_context;
|
|
|
|
|
2016-11-20 04:28:52 +00:00
|
|
|
ProfileTimers profile_timers;
|
2016-03-17 13:48:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GskCairoRendererClass
|
|
|
|
{
|
|
|
|
GskRendererClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GskCairoRenderer, gsk_cairo_renderer, GSK_TYPE_RENDERER)
|
|
|
|
|
|
|
|
static gboolean
|
2016-11-29 03:51:07 +00:00
|
|
|
gsk_cairo_renderer_realize (GskRenderer *renderer,
|
2024-01-07 09:00:04 +00:00
|
|
|
GdkDisplay *display,
|
2018-04-11 22:16:43 +00:00
|
|
|
GdkSurface *surface,
|
2016-11-29 03:51:07 +00:00
|
|
|
GError **error)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2018-04-11 22:16:43 +00:00
|
|
|
GskCairoRenderer *self = GSK_CAIRO_RENDERER (renderer);
|
|
|
|
|
2021-10-20 18:30:08 +00:00
|
|
|
if (surface)
|
|
|
|
self->cairo_context = gdk_surface_create_cairo_context (surface);
|
2018-04-11 22:16:43 +00:00
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_cairo_renderer_unrealize (GskRenderer *renderer)
|
|
|
|
{
|
2018-04-11 22:16:43 +00:00
|
|
|
GskCairoRenderer *self = GSK_CAIRO_RENDERER (renderer);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2018-04-11 22:16:43 +00:00
|
|
|
g_clear_object (&self->cairo_context);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-12-21 19:23:46 +00:00
|
|
|
gsk_cairo_renderer_do_render (GskRenderer *renderer,
|
|
|
|
cairo_t *cr,
|
2024-07-02 15:39:16 +00:00
|
|
|
GdkColorState *ccs,
|
2016-12-21 19:23:46 +00:00
|
|
|
GskRenderNode *root)
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2016-12-21 19:23:46 +00:00
|
|
|
GskCairoRenderer *self = GSK_CAIRO_RENDERER (renderer);
|
2016-11-20 04:28:52 +00:00
|
|
|
GskProfiler *profiler;
|
|
|
|
gint64 cpu_time;
|
|
|
|
|
2016-12-21 19:23:46 +00:00
|
|
|
profiler = gsk_renderer_get_profiler (renderer);
|
|
|
|
gsk_profiler_timer_begin (profiler, self->profile_timers.cpu_time);
|
|
|
|
|
2024-07-02 15:39:16 +00:00
|
|
|
gsk_render_node_draw_with_color_state (root, cr, ccs);
|
2016-12-21 19:23:46 +00:00
|
|
|
|
|
|
|
cpu_time = gsk_profiler_timer_end (profiler, self->profile_timers.cpu_time);
|
|
|
|
gsk_profiler_timer_set (profiler, self->profile_timers.cpu_time, cpu_time);
|
|
|
|
|
|
|
|
gsk_profiler_push_samples (profiler);
|
|
|
|
}
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
static GdkTexture *
|
2016-12-21 19:23:46 +00:00
|
|
|
gsk_cairo_renderer_render_texture (GskRenderer *renderer,
|
|
|
|
GskRenderNode *root,
|
|
|
|
const graphene_rect_t *viewport)
|
|
|
|
{
|
2017-11-02 20:39:00 +00:00
|
|
|
GdkTexture *texture;
|
2016-12-21 19:23:46 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
2022-02-25 02:40:57 +00:00
|
|
|
int width, height;
|
|
|
|
/* limit from cairo's source code */
|
|
|
|
#define MAX_IMAGE_SIZE 32767
|
|
|
|
|
|
|
|
width = ceil (viewport->size.width);
|
|
|
|
height = ceil (viewport->size.height);
|
|
|
|
if (width > MAX_IMAGE_SIZE || height > MAX_IMAGE_SIZE)
|
|
|
|
{
|
|
|
|
gsize x, y, size, stride;
|
|
|
|
GBytes *bytes;
|
|
|
|
guchar *data;
|
|
|
|
|
|
|
|
stride = width * 4;
|
|
|
|
size = stride * height;
|
|
|
|
data = g_malloc_n (stride, height);
|
|
|
|
|
|
|
|
for (y = 0; y < height; y += MAX_IMAGE_SIZE)
|
|
|
|
{
|
|
|
|
for (x = 0; x < width; x += MAX_IMAGE_SIZE)
|
|
|
|
{
|
|
|
|
texture = gsk_cairo_renderer_render_texture (renderer, root,
|
|
|
|
&GRAPHENE_RECT_INIT (x, y,
|
|
|
|
MIN (MAX_IMAGE_SIZE, viewport->size.width - x),
|
|
|
|
MIN (MAX_IMAGE_SIZE, viewport->size.height - y)));
|
|
|
|
gdk_texture_download (texture,
|
|
|
|
data + stride * y + x * 4,
|
|
|
|
stride);
|
|
|
|
g_object_unref (texture);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = g_bytes_new_take (data, size);
|
|
|
|
texture = gdk_memory_texture_new (width, height, GDK_MEMORY_DEFAULT, bytes, stride);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
|
2016-12-21 19:23:46 +00:00
|
|
|
cr = cairo_create (surface);
|
|
|
|
|
|
|
|
cairo_translate (cr, - viewport->origin.x, - viewport->origin.y);
|
|
|
|
|
2024-07-02 15:39:16 +00:00
|
|
|
gsk_cairo_renderer_do_render (renderer, cr, GDK_COLOR_STATE_SRGB, root);
|
2016-12-21 19:23:46 +00:00
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2017-11-02 20:39:00 +00:00
|
|
|
texture = gdk_texture_new_for_surface (surface);
|
2016-12-21 19:23:46 +00:00
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-03-28 13:01:34 +00:00
|
|
|
gsk_cairo_renderer_render (GskRenderer *renderer,
|
|
|
|
GskRenderNode *root,
|
2023-11-14 20:45:47 +00:00
|
|
|
const cairo_region_t *region)
|
2016-12-21 19:23:46 +00:00
|
|
|
{
|
2018-04-11 22:16:43 +00:00
|
|
|
GskCairoRenderer *self = GSK_CAIRO_RENDERER (renderer);
|
2024-08-09 17:26:43 +00:00
|
|
|
graphene_rect_t opaque_tmp;
|
|
|
|
const graphene_rect_t *opaque;
|
2016-08-25 10:29:00 +00:00
|
|
|
cairo_t *cr;
|
|
|
|
|
2024-08-09 17:26:43 +00:00
|
|
|
if (gsk_render_node_get_opaque_rect (root, &opaque_tmp))
|
|
|
|
opaque = &opaque_tmp;
|
|
|
|
else
|
|
|
|
opaque = NULL;
|
2024-08-09 02:30:39 +00:00
|
|
|
gdk_draw_context_begin_frame_full (GDK_DRAW_CONTEXT (self->cairo_context),
|
|
|
|
GDK_MEMORY_U8,
|
2024-08-09 17:26:43 +00:00
|
|
|
region,
|
|
|
|
opaque);
|
2018-04-12 00:10:22 +00:00
|
|
|
cr = gdk_cairo_context_cairo_create (self->cairo_context);
|
2016-08-25 10:31:56 +00:00
|
|
|
|
2016-12-13 04:07:43 +00:00
|
|
|
g_return_if_fail (cr != NULL);
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2018-01-14 20:11:33 +00:00
|
|
|
if (GSK_RENDERER_DEBUG_CHECK (renderer, GEOMETRY))
|
2016-03-17 13:48:19 +00:00
|
|
|
{
|
2018-04-23 15:28:55 +00:00
|
|
|
GdkSurface *surface = gsk_renderer_get_surface (renderer);
|
|
|
|
|
2016-03-17 13:48:19 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
|
|
|
|
cairo_rectangle (cr,
|
2017-11-04 19:05:56 +00:00
|
|
|
0, 0,
|
2018-03-21 10:49:14 +00:00
|
|
|
gdk_surface_get_width (surface), gdk_surface_get_height (surface));
|
2016-03-17 13:48:19 +00:00
|
|
|
cairo_set_source_rgba (cr, 0, 0, 0.85, 0.5);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
|
2024-07-02 15:39:16 +00:00
|
|
|
gsk_cairo_renderer_do_render (renderer,
|
|
|
|
cr,
|
|
|
|
gdk_draw_context_get_color_state (GDK_DRAW_CONTEXT (self->cairo_context)),
|
|
|
|
root);
|
2018-03-28 13:01:34 +00:00
|
|
|
|
2018-04-12 00:10:22 +00:00
|
|
|
cairo_destroy (cr);
|
|
|
|
|
2024-08-09 17:26:43 +00:00
|
|
|
gdk_draw_context_end_frame_full (GDK_DRAW_CONTEXT (self->cairo_context));
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_cairo_renderer_class_init (GskCairoRendererClass *klass)
|
|
|
|
{
|
|
|
|
GskRendererClass *renderer_class = GSK_RENDERER_CLASS (klass);
|
|
|
|
|
|
|
|
renderer_class->realize = gsk_cairo_renderer_realize;
|
|
|
|
renderer_class->unrealize = gsk_cairo_renderer_unrealize;
|
|
|
|
renderer_class->render = gsk_cairo_renderer_render;
|
2016-12-21 19:23:46 +00:00
|
|
|
renderer_class->render_texture = gsk_cairo_renderer_render_texture;
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gsk_cairo_renderer_init (GskCairoRenderer *self)
|
|
|
|
{
|
2016-11-20 04:28:52 +00:00
|
|
|
GskProfiler *profiler = gsk_renderer_get_profiler (GSK_RENDERER (self));
|
2016-03-17 13:48:19 +00:00
|
|
|
|
2016-11-20 04:28:52 +00:00
|
|
|
self->profile_timers.cpu_time = gsk_profiler_add_timer (profiler, "cpu-time", "CPU time", FALSE, TRUE);
|
2016-03-17 13:48:19 +00:00
|
|
|
}
|
2019-05-01 21:58:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gsk_cairo_renderer_new:
|
|
|
|
*
|
|
|
|
* Creates a new Cairo renderer.
|
|
|
|
*
|
|
|
|
* The Cairo renderer is the fallback renderer drawing in ways similar
|
|
|
|
* to how GTK 3 drew its content. Its primary use is as comparison tool.
|
|
|
|
*
|
|
|
|
* The Cairo renderer is incomplete. It cannot render 3D transformed
|
|
|
|
* content and will instead render an error marker. Its usage should be
|
|
|
|
* avoided.
|
|
|
|
*
|
|
|
|
* Returns: a new Cairo renderer.
|
|
|
|
**/
|
|
|
|
GskRenderer *
|
|
|
|
gsk_cairo_renderer_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GSK_TYPE_CAIRO_RENDERER, NULL);
|
|
|
|
}
|