2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is libguac-client-rdp.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2011
20 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include <cairo/cairo.h>
44 #include <guacamole/socket.h>
45 #include <guacamole/client.h>
46 #include <guacamole/protocol.h>
48 #include <freerdp/freerdp.h>
49 #include <freerdp/utils/memory.h>
50 #include <freerdp/codec/color.h>
51 #include <freerdp/codec/bitmap.h>
54 #include "rdp_bitmap.h"
56 void __guac_rdp_cache_bitmap(rdpContext* context, rdpBitmap* bitmap) {
58 guac_client* client = ((rdp_freerdp_context*) context)->client;
59 guac_socket* socket = client->socket;
62 guac_layer* buffer = guac_client_alloc_buffer(client);
64 /* Cache image data if present */
65 if (bitmap->data != NULL) {
67 /* Create surface from image data */
68 cairo_surface_t* surface = cairo_image_surface_create_for_data(
69 bitmap->data, CAIRO_FORMAT_RGB24,
70 bitmap->width, bitmap->height, 4*bitmap->width);
72 /* Send surface to buffer */
73 guac_protocol_send_png(socket,
74 GUAC_COMP_SRC, buffer, 0, 0, surface);
77 cairo_surface_destroy(surface);
81 /* Store buffer reference in bitmap */
82 ((guac_rdp_bitmap*) bitmap)->layer = buffer;
87 void guac_rdp_bitmap_new(rdpContext* context, rdpBitmap* bitmap) {
89 /* Convert image data if present */
90 if (bitmap->data != NULL) {
92 /* Convert image data to 32-bit RGB */
93 unsigned char* image_buffer = freerdp_image_convert(bitmap->data, NULL,
94 bitmap->width, bitmap->height,
95 context->instance->settings->color_depth,
96 32, ((rdp_freerdp_context*) context)->clrconv);
98 /* Free existing image, if any */
99 if (image_buffer != bitmap->data)
102 /* Store converted image in bitmap */
103 bitmap->data = image_buffer;
107 /* No corresponding layer yet - caching is deferred. */
108 ((guac_rdp_bitmap*) bitmap)->layer = NULL;
110 /* Start at zero usage */
111 ((guac_rdp_bitmap*) bitmap)->used = 0;
115 void guac_rdp_bitmap_paint(rdpContext* context, rdpBitmap* bitmap) {
117 guac_client* client = ((rdp_freerdp_context*) context)->client;
118 guac_socket* socket = client->socket;
120 int width = bitmap->right - bitmap->left + 1;
121 int height = bitmap->bottom - bitmap->top + 1;
123 /* If not cached, cache if necessary */
124 if (((guac_rdp_bitmap*) bitmap)->layer == NULL
125 && ((guac_rdp_bitmap*) bitmap)->used >= 2) {
126 __guac_rdp_cache_bitmap(context, bitmap);
128 guac_client_log_info(client, "Deferred cache! bitmap used=%i", ((guac_rdp_bitmap*) bitmap)->used);
132 /* If cached, retrieve from cache */
133 if (((guac_rdp_bitmap*) bitmap)->layer != NULL)
134 guac_protocol_send_copy(socket,
135 ((guac_rdp_bitmap*) bitmap)->layer,
138 GUAC_DEFAULT_LAYER, bitmap->left, bitmap->top);
140 /* Otherwise, draw with stored image data */
141 else if (bitmap->data != NULL) {
143 /* Create surface from image data */
144 cairo_surface_t* surface = cairo_image_surface_create_for_data(
145 bitmap->data, CAIRO_FORMAT_RGB24,
146 width, height, 4*bitmap->width);
148 /* Send surface to buffer */
149 guac_protocol_send_png(socket,
150 GUAC_COMP_OVER, GUAC_DEFAULT_LAYER,
151 bitmap->left, bitmap->top, surface);
154 cairo_surface_destroy(surface);
158 /* Increment usage counter */
159 ((guac_rdp_bitmap*) bitmap)->used++;
161 guac_client_log_info(client, "Used bitmap... used=%i", ((guac_rdp_bitmap*) bitmap)->used);
165 void guac_rdp_bitmap_free(rdpContext* context, rdpBitmap* bitmap) {
166 guac_client* client = ((rdp_freerdp_context*) context)->client;
168 /* If cached, free buffer */
169 if (((guac_rdp_bitmap*) bitmap)->layer != NULL)
170 guac_client_free_buffer(client, ((guac_rdp_bitmap*) bitmap)->layer);
174 void guac_rdp_bitmap_setsurface(rdpContext* context, rdpBitmap* bitmap, boolean primary) {
175 guac_client* client = ((rdp_freerdp_context*) context)->client;
178 ((rdp_guac_client_data*) client->data)->current_surface
179 = GUAC_DEFAULT_LAYER;
183 /* If not available as a surface, make available. */
184 if (((guac_rdp_bitmap*) bitmap)->layer == NULL)
185 __guac_rdp_cache_bitmap(context, bitmap);
187 ((rdp_guac_client_data*) client->data)->current_surface
188 = ((guac_rdp_bitmap*) bitmap)->layer;
194 void guac_rdp_bitmap_decompress(rdpContext* context, rdpBitmap* bitmap, uint8* data, int width, int height, int bpp, int length, boolean compressed) {
196 int size = width * height * (bpp + 7) / 8;
198 if (bitmap->data == NULL)
199 bitmap->data = (uint8*) xmalloc(size);
201 bitmap->data = (uint8*) xrealloc(bitmap->data, size);
204 bitmap_decompress(data, bitmap->data, width, height, length, bpp, bpp);
206 freerdp_image_flip(data, bitmap->data, width, height, bpp);
208 bitmap->compressed = false;
209 bitmap->length = size;