summaryrefslogtreecommitdiff
path: root/src/qxl_image.c
blob: f1916f1e699e4b460cee059e0ce51b762990d9de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright 2009, 2010 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
/** \file QXLImage.c
 * \author Søren Sandmann <sandmann@redhat.com>
 */

#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include "qxl.h"
#include "lookup3.h"

typedef struct image_info_t image_info_t;

struct image_info_t
{
    struct QXLImage *image;
    int ref_count;
    image_info_t *next;
};

#define HASH_SIZE 4096
static image_info_t *image_table[HASH_SIZE];

static unsigned int
hash_and_copy (const uint8_t *src, int src_stride,
	       uint8_t *dest, int dest_stride,
	       int bytes_per_pixel, int width, int height,
	       unsigned int hash)
{
    int i;
  
    for (i = 0; i < height; ++i)
    {
	const uint8_t *src_line = src + i * src_stride;
	uint8_t *dest_line = dest + i * dest_stride;
	int n_bytes = width * bytes_per_pixel;

	if (dest)
	    memcpy (dest_line, src_line, n_bytes);

	hash = hashlittle (src_line, n_bytes, hash);
    }

    return hash;
}

static image_info_t *
lookup_image_info (unsigned int hash,
		   int width,
		   int height)
{
    struct image_info_t *info = image_table[hash % HASH_SIZE];

    while (info)
    {
	struct QXLImage *image = info->image;

	if (image->descriptor.id == hash		&&
	    image->descriptor.width == width		&&
	    image->descriptor.height == height)
	{
	    return info;
	}

	info = info->next;
    }

#if 0
    ErrorF ("lookup of %u failed\n", hash);
#endif
    
    return NULL;
}

static image_info_t *
insert_image_info (unsigned int hash)
{
    struct image_info_t *info = malloc (sizeof (image_info_t));

    if (!info)
	return NULL;

    info->next = image_table[hash % HASH_SIZE];
    image_table[hash % HASH_SIZE] = info;
    
    return info;
}

static void
remove_image_info (image_info_t *info)
{
    struct image_info_t **location = &image_table[info->image->descriptor.id % HASH_SIZE];

    while (*location && (*location) != info)
	location = &((*location)->next);

    if (*location)
	*location = info->next;

    free (info);
}

#define MAX(a,b)  (((a) > (b))? (a) : (b))
#define MIN(a,b)  (((a) < (b))? (a) : (b))

struct QXLImage *
qxl_image_create (qxl_screen_t *qxl, const uint8_t *data,
		  int x, int y, int width, int height,
		  int stride, int Bpp, Bool fallback)
{
    unsigned int hash;
    image_info_t *info;

    data += y * stride + x * Bpp;

    {
	struct QXLImage *image;
	struct QXLDataChunk *head;
	struct QXLDataChunk *tail;
	int dest_stride = width * Bpp;
	int h;

#if 0
	ErrorF ("Must create new image of size %d %d\n", width, height);
#endif
	
	/* Chunk */
	
	/* FIXME: Check integer overflow */

	head = tail = NULL;

	hash = 0;
	h = height;
	while (h)
	{
	    int chunk_size = MAX (512 * 512, dest_stride);
	    int n_lines = MIN ((chunk_size / dest_stride), h);
	    QXLDataChunk *chunk =
		qxl_allocnf (qxl, sizeof *chunk + n_lines * dest_stride);

	    chunk->data_size = n_lines * dest_stride;
	    hash = hash_and_copy (data, stride,
				  chunk->data, dest_stride,
				  Bpp, width, n_lines, hash);
	    
	    if (tail)
	    {
		tail->next_chunk = physical_address (qxl, chunk, qxl->main_mem_slot);
		chunk->prev_chunk = physical_address (qxl, tail, qxl->main_mem_slot);
		chunk->next_chunk = 0;
		
		tail = chunk;
	    }
	    else
	    {
		head = tail = chunk;
		chunk->next_chunk = 0;
		chunk->prev_chunk = 0;
	    }

	    data += n_lines * stride;
	    h -= n_lines;
	}

	/* Image */
	image = qxl_allocnf (qxl, sizeof *image);

	image->descriptor.id = 0;
	image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
	
	image->descriptor.flags = 0;
	image->descriptor.width = width;
	image->descriptor.height = height;

	if (Bpp == 2)
	{
	    image->bitmap.format = SPICE_BITMAP_FMT_16BIT;
	}
	else if (Bpp == 1)
	{
	    image->bitmap.format = SPICE_BITMAP_FMT_8BIT;
	}
	else if (Bpp == 4)
	{
	    image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
	}
	else
	{
	    abort();
	}

	image->bitmap.flags = SPICE_BITMAP_FLAGS_TOP_DOWN;
	image->bitmap.x = width;
	image->bitmap.y = height;
	image->bitmap.stride = width * Bpp;
	image->bitmap.palette = 0;
	image->bitmap.data = physical_address (qxl, head, qxl->main_mem_slot);

#if 0
	ErrorF ("%p has size %d %d\n", image, width, height);
#endif
	
	/* Add to hash table if caching is enabled */
	if ((fallback && qxl->enable_fallback_cache)	||
	    (!fallback && qxl->enable_image_cache))
	{
	    if ((info = insert_image_info (hash)))
	    {
		info->image = image;
		info->ref_count = 1;

		image->descriptor.id = hash;
		image->descriptor.flags = QXL_IMAGE_CACHE;

#if 0
		ErrorF ("added with hash %u\n", hash);
#endif
	    }
	}

	return image;
    }
}

void
qxl_image_destroy (qxl_screen_t *qxl,
		   struct QXLImage *image)
{
    image_info_t *info;
    uint64_t chunk;

    info = lookup_image_info (image->descriptor.id,
			      image->descriptor.width,
			      image->descriptor.height);

    if (info && info->image == image)
    {
	--info->ref_count;

	if (info->ref_count != 0)
	    return;

#if 0
	ErrorF ("removed %p from hash table\n", info->image);
#endif
	
	remove_image_info (info);
    }

    
    chunk = image->bitmap.data;
    while (chunk)
    {
	struct QXLDataChunk *virtual;

	virtual = virtual_address (qxl, u64_to_pointer (chunk), qxl->main_mem_slot);

	chunk = virtual->next_chunk;

	qxl_free (qxl->mem, virtual);
    }
    
    qxl_free (qxl->mem, image);
}

void
qxl_drop_image_cache (qxl_screen_t *qxl)
{
    memset (image_table, 0, HASH_SIZE * sizeof (image_info_t *));
}