Home | History | Annotate | Line # | Download | only in qxl
qxl_image.c revision 1.1.1.1.32.1
      1 /*	$NetBSD: qxl_image.c,v 1.1.1.1.32.1 2019/06/10 22:08:24 christos Exp $	*/
      2 
      3 /*
      4  * Copyright 2013 Red Hat Inc.
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  * OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  * Authors: Dave Airlie
     25  *          Alon Levy
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: qxl_image.c,v 1.1.1.1.32.1 2019/06/10 22:08:24 christos Exp $");
     30 
     31 #include <linux/gfp.h>
     32 #include <linux/slab.h>
     33 
     34 #include "qxl_drv.h"
     35 #include "qxl_object.h"
     36 
     37 static int
     38 qxl_allocate_chunk(struct qxl_device *qdev,
     39 		   struct qxl_release *release,
     40 		   struct qxl_drm_image *image,
     41 		   unsigned int chunk_size)
     42 {
     43 	struct qxl_drm_chunk *chunk;
     44 	int ret;
     45 
     46 	chunk = kmalloc(sizeof(struct qxl_drm_chunk), GFP_KERNEL);
     47 	if (!chunk)
     48 		return -ENOMEM;
     49 
     50 	ret = qxl_alloc_bo_reserved(qdev, release, chunk_size, &chunk->bo);
     51 	if (ret) {
     52 		kfree(chunk);
     53 		return ret;
     54 	}
     55 
     56 	list_add_tail(&chunk->head, &image->chunk_list);
     57 	return 0;
     58 }
     59 
     60 int
     61 qxl_image_alloc_objects(struct qxl_device *qdev,
     62 			struct qxl_release *release,
     63 			struct qxl_drm_image **image_ptr,
     64 			int height, int stride)
     65 {
     66 	struct qxl_drm_image *image;
     67 	int ret;
     68 
     69 	image = kmalloc(sizeof(struct qxl_drm_image), GFP_KERNEL);
     70 	if (!image)
     71 		return -ENOMEM;
     72 
     73 	INIT_LIST_HEAD(&image->chunk_list);
     74 
     75 	ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_image), &image->bo);
     76 	if (ret) {
     77 		kfree(image);
     78 		return ret;
     79 	}
     80 
     81 	ret = qxl_allocate_chunk(qdev, release, image, sizeof(struct qxl_data_chunk) + stride * height);
     82 	if (ret) {
     83 		qxl_bo_unref(&image->bo);
     84 		kfree(image);
     85 		return ret;
     86 	}
     87 	*image_ptr = image;
     88 	return 0;
     89 }
     90 
     91 void qxl_image_free_objects(struct qxl_device *qdev, struct qxl_drm_image *dimage)
     92 {
     93 	struct qxl_drm_chunk *chunk, *tmp;
     94 
     95 	list_for_each_entry_safe(chunk, tmp, &dimage->chunk_list, head) {
     96 		qxl_bo_unref(&chunk->bo);
     97 		kfree(chunk);
     98 	}
     99 
    100 	qxl_bo_unref(&dimage->bo);
    101 	kfree(dimage);
    102 }
    103 
    104 static int
    105 qxl_image_init_helper(struct qxl_device *qdev,
    106 		      struct qxl_release *release,
    107 		      struct qxl_drm_image *dimage,
    108 		      const uint8_t *data,
    109 		      int width, int height,
    110 		      int depth, unsigned int hash,
    111 		      int stride)
    112 {
    113 	struct qxl_drm_chunk *drv_chunk;
    114 	struct qxl_image *image;
    115 	struct qxl_data_chunk *chunk;
    116 	int i;
    117 	int chunk_stride;
    118 	int linesize = width * depth / 8;
    119 	struct qxl_bo *chunk_bo, *image_bo;
    120 	void *ptr;
    121 	/* Chunk */
    122 	/* FIXME: Check integer overflow */
    123 	/* TODO: variable number of chunks */
    124 
    125 	drv_chunk = list_first_entry(&dimage->chunk_list, struct qxl_drm_chunk, head);
    126 
    127 	chunk_bo = drv_chunk->bo;
    128 	chunk_stride = stride; /* TODO: should use linesize, but it renders
    129 				  wrong (check the bitmaps are sent correctly
    130 				  first) */
    131 
    132 	ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, 0);
    133 	chunk = ptr;
    134 	chunk->data_size = height * chunk_stride;
    135 	chunk->prev_chunk = 0;
    136 	chunk->next_chunk = 0;
    137 	qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
    138 
    139 	{
    140 		void *k_data, *i_data;
    141 		int remain;
    142 		int page;
    143 		int size;
    144 		if (stride == linesize && chunk_stride == stride) {
    145 			remain = linesize * height;
    146 			page = 0;
    147 			i_data = (void *)data;
    148 
    149 			while (remain > 0) {
    150 				ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page << PAGE_SHIFT);
    151 
    152 				if (page == 0) {
    153 					chunk = ptr;
    154 					k_data = chunk->data;
    155 					size = PAGE_SIZE - offsetof(struct qxl_data_chunk, data);
    156 				} else {
    157 					k_data = ptr;
    158 					size = PAGE_SIZE;
    159 				}
    160 				size = min(size, remain);
    161 
    162 				memcpy(k_data, i_data, size);
    163 
    164 				qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
    165 				i_data += size;
    166 				remain -= size;
    167 				page++;
    168 			}
    169 		} else {
    170 			unsigned page_base, page_offset, out_offset;
    171 			for (i = 0 ; i < height ; ++i) {
    172 				i_data = (void *)data + i * stride;
    173 				remain = linesize;
    174 				out_offset = offsetof(struct qxl_data_chunk, data) + i * chunk_stride;
    175 
    176 				while (remain > 0) {
    177 					page_base = out_offset & PAGE_MASK;
    178 					page_offset = offset_in_page(out_offset);
    179 					size = min((int)(PAGE_SIZE - page_offset), remain);
    180 
    181 					ptr = qxl_bo_kmap_atomic_page(qdev, chunk_bo, page_base);
    182 					k_data = ptr + page_offset;
    183 					memcpy(k_data, i_data, size);
    184 					qxl_bo_kunmap_atomic_page(qdev, chunk_bo, ptr);
    185 					remain -= size;
    186 					i_data += size;
    187 					out_offset += size;
    188 				}
    189 			}
    190 		}
    191 	}
    192 	qxl_bo_kunmap(chunk_bo);
    193 
    194 	image_bo = dimage->bo;
    195 	ptr = qxl_bo_kmap_atomic_page(qdev, image_bo, 0);
    196 	image = ptr;
    197 
    198 	image->descriptor.id = 0;
    199 	image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
    200 
    201 	image->descriptor.flags = 0;
    202 	image->descriptor.width = width;
    203 	image->descriptor.height = height;
    204 
    205 	switch (depth) {
    206 	case 1:
    207 		/* TODO: BE? check by arch? */
    208 		image->u.bitmap.format = SPICE_BITMAP_FMT_1BIT_BE;
    209 		break;
    210 	case 24:
    211 		image->u.bitmap.format = SPICE_BITMAP_FMT_24BIT;
    212 		break;
    213 	case 32:
    214 		image->u.bitmap.format = SPICE_BITMAP_FMT_32BIT;
    215 		break;
    216 	default:
    217 		DRM_ERROR("unsupported image bit depth\n");
    218 		return -EINVAL; /* TODO: cleanup */
    219 	}
    220 	image->u.bitmap.flags = QXL_BITMAP_TOP_DOWN;
    221 	image->u.bitmap.x = width;
    222 	image->u.bitmap.y = height;
    223 	image->u.bitmap.stride = chunk_stride;
    224 	image->u.bitmap.palette = 0;
    225 	image->u.bitmap.data = qxl_bo_physical_address(qdev, chunk_bo, 0);
    226 
    227 	qxl_bo_kunmap_atomic_page(qdev, image_bo, ptr);
    228 
    229 	return 0;
    230 }
    231 
    232 int qxl_image_init(struct qxl_device *qdev,
    233 		     struct qxl_release *release,
    234 		     struct qxl_drm_image *dimage,
    235 		     const uint8_t *data,
    236 		     int x, int y, int width, int height,
    237 		     int depth, int stride)
    238 {
    239 	data += y * stride + x * (depth / 8);
    240 	return qxl_image_init_helper(qdev, release, dimage, data,
    241 				       width, height, depth, 0, stride);
    242 }
    243