neatvnc/src/cursor.c

127 lines
3.2 KiB
C
Raw Normal View History

2022-02-06 15:41:40 +00:00
/*
* 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 "cursor.h"
#include "fb.h"
#include "pixels.h"
#include "rfb-proto.h"
#include "vec.h"
#include "enc-util.h"
2022-05-16 22:47:35 +00:00
#include "resampler.h"
#include "transform-util.h"
2022-02-06 15:41:40 +00:00
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#define UDIV_UP(a, b) (((a) + (b) - 1) / (b))
2022-05-16 22:47:35 +00:00
static struct nvnc_fb* apply_transform(struct nvnc_fb* fb)
{
if (fb->transform == NVNC_TRANSFORM_NORMAL) {
nvnc_fb_ref(fb);
return fb;
}
uint32_t width = fb->width;
uint32_t height = fb->height;
nvnc_transform_dimensions(fb->transform, &width, &height);
struct nvnc_fb* dst = nvnc_fb_new(width, height, fb->fourcc_format,
width);
assert(dst);
// TODO: Don't assume bpp
memset(dst->addr, 0, width * height * 4);
resample_now(dst, fb, NULL);
return dst;
}
2022-02-06 15:41:40 +00:00
int cursor_encode(struct vec* dst, struct rfb_pixel_format* pixfmt,
struct nvnc_fb* image, uint32_t width, uint32_t height,
uint32_t hotspot_x, uint32_t hotspot_y)
2022-02-06 15:41:40 +00:00
{
int rc = -1;
2022-02-20 14:48:24 +00:00
// Empty cursor
if (!image)
return encode_rect_head(dst, RFB_ENCODING_CURSOR, 0, 0, 0, 0);
2022-05-16 22:47:35 +00:00
nvnc_transform_dimensions(image->transform, &width, &height);
nvnc_transform_dimensions(image->transform, &hotspot_x, &hotspot_y);
if (nvnc_fb_map(image) < 0)
2022-05-16 22:47:35 +00:00
goto failure;
image = apply_transform(image);
assert(width <= image->width);
assert(height <= image->height);
2022-02-06 15:41:40 +00:00
struct rfb_pixel_format srcfmt = { 0 };
rc = rfb_pixfmt_from_fourcc(&srcfmt, image->fourcc_format);
if (rc < 0)
2022-05-16 22:47:35 +00:00
goto failure;
2022-02-06 15:41:40 +00:00
rc = encode_rect_head(dst, RFB_ENCODING_CURSOR, hotspot_x, hotspot_y,
width, height);
2022-02-06 15:41:40 +00:00
if (rc < 0)
2022-05-16 22:47:35 +00:00
goto failure;
2022-02-06 15:41:40 +00:00
int bpp = pixfmt->bits_per_pixel / 8;
size_t size = width * height;
2022-02-06 15:41:40 +00:00
rc = vec_reserve(dst, dst->len + size * bpp + UDIV_UP(size, 8));
if (rc < 0)
2022-05-16 22:47:35 +00:00
goto failure;
2022-02-06 15:41:40 +00:00
uint8_t* dstdata = dst->data;
dstdata += dst->len;
int32_t src_byte_stride = image->stride * (srcfmt.bits_per_pixel / 8);
if((int32_t)width == image->stride) {
pixel_to_cpixel(dstdata, pixfmt, image->addr, &srcfmt, bpp, size);
2022-02-06 15:41:40 +00:00
} else {
2022-02-20 14:48:24 +00:00
for (uint32_t y = 0; y < height; ++y) {
pixel_to_cpixel(dstdata + y * bpp * width, pixfmt,
(uint8_t*)image->addr + y * src_byte_stride,
&srcfmt, bpp, width);
2022-02-06 15:41:40 +00:00
}
}
dst->len += size * bpp;
dstdata = dst->data;
dstdata += dst->len;
2022-02-20 14:48:24 +00:00
for (uint32_t y = 0; y < height; ++y) {
if (!extract_alpha_mask(dstdata + y * UDIV_UP(width, 8),
2022-02-06 17:44:12 +00:00
(uint32_t*)image->addr + y * image->stride,
image->fourcc_format, width))
2022-05-16 22:47:35 +00:00
goto failure;
2022-02-06 17:44:12 +00:00
dst->len += UDIV_UP(width, 8);
2022-02-06 15:41:40 +00:00
}
2022-05-16 22:47:35 +00:00
rc = 0;
failure:
nvnc_fb_unref(image);
return rc;
2022-02-06 15:41:40 +00:00
}