Extract rendering into own unit

pull/6/head
Andri Yngvason 2022-04-03 22:17:27 +00:00
parent 9c7a4d6b07
commit ec51388fec
5 changed files with 142 additions and 53 deletions

35
include/buffer.h 100644
View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include <pixman.h>
struct wl_buffer;
struct buffer {
int width, height, stride;
size_t size;
uint32_t format;
struct wl_buffer* wl_buffer;
void* pixels;
bool is_attached;
bool please_clean_up;
struct pixman_region16 damage;
};

30
include/renderer.h 100644
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <pixman.h>
struct buffer;
struct image {
int width, height, stride;
uint32_t format;
void* pixels;
};
void render_image(struct buffer* dst, const struct image* src, double scale,
int pos_x, int pos_y);

View File

@ -71,6 +71,7 @@ sources = [
'src/evdev-to-qnum.c', 'src/evdev-to-qnum.c',
'src/pixels.c', 'src/pixels.c',
'src/region.c', 'src/region.c',
'src/renderer.c',
] ]
dependencies = [ dependencies = [

View File

@ -39,17 +39,8 @@
#include "vnc.h" #include "vnc.h"
#include "pixels.h" #include "pixels.h"
#include "region.h" #include "region.h"
#include "buffer.h"
struct buffer { #include "renderer.h"
int width, height, stride;
size_t size;
uint32_t format;
struct wl_buffer* wl_buffer;
void* pixels;
bool is_attached;
bool please_clean_up;
struct pixman_region16 damage;
};
struct window { struct window {
struct wl_surface* wl_surface; struct wl_surface* wl_surface;
@ -329,53 +320,20 @@ static void window_calculate_tranform(struct window* w, double* scale,
static void window_transfer_pixels(struct window* w) static void window_transfer_pixels(struct window* w)
{ {
void* src_pixels = w->vnc_fb;
int src_width = vnc_client_get_width(w->vnc);
int src_height = vnc_client_get_height(w->vnc);
int src_stride = vnc_client_get_stride(w->vnc);
void* dst_pixels = w->back_buffer->pixels;
int dst_width = w->back_buffer->width;
int dst_height = w->back_buffer->height;
int dst_stride = w->back_buffer->stride;
bool ok __attribute__((unused));
pixman_format_code_t dst_fmt = 0;
ok = drm_format_to_pixman_fmt(&dst_fmt, w->back_buffer->format);
assert(ok);
pixman_format_code_t src_fmt = 0;
ok = drm_format_to_pixman_fmt(&src_fmt, w->back_buffer->format);
assert(ok);
pixman_image_t* dstimg = pixman_image_create_bits_no_clear(
dst_fmt, dst_width, dst_height, dst_pixels, dst_stride);
pixman_image_t* srcimg = pixman_image_create_bits_no_clear(src_fmt,
src_width, src_height, src_pixels, src_stride);
double scale; double scale;
int x_pos, y_pos; int x_pos, y_pos;
window_calculate_tranform(w, &scale, &x_pos, &y_pos); window_calculate_tranform(w, &scale, &x_pos, &y_pos);
pixman_fixed_t src_scale = pixman_double_to_fixed(1.0 / scale);
pixman_transform_t xform; struct image image = {
pixman_transform_init_scale(&xform, src_scale, src_scale); .pixels = w->vnc_fb,
pixman_image_set_transform(srcimg, &xform); .width = vnc_client_get_width(w->vnc),
.height = vnc_client_get_height(w->vnc),
.stride = vnc_client_get_stride(w->vnc),
// TODO: Get the format from the vnc module
.format = w->back_buffer->format,
};
pixman_image_set_clip_region(dstimg, &w->back_buffer->damage); render_image(w->back_buffer, &image, scale, x_pos, y_pos);
pixman_image_composite(PIXMAN_OP_OVER, srcimg, NULL, dstimg,
0, 0,
0, 0,
x_pos, y_pos,
dst_width, dst_height);
pixman_image_unref(srcimg);
pixman_image_unref(dstimg);
pixman_region_clear(&w->back_buffer->damage);
} }
static void window_commit(struct window* w) static void window_commit(struct window* w)

65
src/renderer.c 100644
View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2022 Andri Yngvason
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include "renderer.h"
#include "buffer.h"
#include "pixels.h"
#include <stdbool.h>
#include <unistd.h>
#include <stdint.h>
#include <pixman.h>
#include <assert.h>
void render_image(struct buffer* dst, const struct image* src, double scale,
int x_pos, int y_pos)
{
bool ok __attribute__((unused));
pixman_format_code_t dst_fmt = 0;
ok = drm_format_to_pixman_fmt(&dst_fmt, dst->format);
assert(ok);
pixman_format_code_t src_fmt = 0;
ok = drm_format_to_pixman_fmt(&src_fmt, src->format);
assert(ok);
pixman_image_t* dstimg = pixman_image_create_bits_no_clear(dst_fmt,
dst->width, dst->height, dst->pixels, dst->stride);
pixman_image_t* srcimg = pixman_image_create_bits_no_clear(src_fmt,
src->width, src->height, src->pixels, src->stride);
pixman_fixed_t src_scale = pixman_double_to_fixed(1.0 / scale);
pixman_transform_t xform;
pixman_transform_init_scale(&xform, src_scale, src_scale);
pixman_image_set_transform(srcimg, &xform);
pixman_image_set_clip_region(dstimg, &dst->damage);
pixman_image_composite(PIXMAN_OP_OVER, srcimg, NULL, dstimg,
0, 0,
0, 0,
x_pos, y_pos,
dst->width, dst->height);
pixman_image_unref(srcimg);
pixman_image_unref(dstimg);
pixman_region_clear(&dst->damage);
}