blob: 9046059357f8e96c610864b0916470231c7bd246 [file] [log] [blame]
Chia-I Wu4ea339e2014-08-08 21:56:26 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, 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
14 * in 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu4ea339e2014-08-08 21:56:26 +080026 */
27
Chia-I Wu1db76e02014-09-15 14:21:14 +080028#include <unistd.h>
Chia-I Wue46da3e2014-08-08 21:52:48 +080029#include "kmd/winsys.h"
30#include "dev.h"
31#include "gpu.h"
Chia-I Wu1db76e02014-09-15 14:21:14 +080032#include "mem.h"
Chia-I Wue46da3e2014-08-08 21:52:48 +080033#include "img.h"
Chia-I Wu4ea339e2014-08-08 21:56:26 +080034
Chia-I Wu4ea339e2014-08-08 21:56:26 +080035/*
36 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
37 *
38 * "In addition to restrictions on maximum height, width, and depth,
39 * surfaces are also restricted to a maximum size in bytes. This
40 * maximum is 2 GB for all products and all surface types."
41 */
Chia-I Wue46da3e2014-08-08 21:52:48 +080042static const size_t intel_max_resource_size = 1u << 31;
Chia-I Wu4ea339e2014-08-08 21:56:26 +080043
Chia-I Wufeb441f2014-08-08 21:27:38 +080044static void img_destroy(struct intel_obj *obj)
45{
46 struct intel_img *img = intel_img_from_obj(obj);
47
48 intel_img_destroy(img);
49}
50
51static XGL_RESULT img_get_info(struct intel_base *base, int type,
52 XGL_SIZE *size, XGL_VOID *data)
53{
54 struct intel_img *img = intel_img_from_base(base);
55 XGL_RESULT ret = XGL_SUCCESS;
56
57 switch (type) {
58 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
59 {
60 XGL_MEMORY_REQUIREMENTS *mem_req = data;
61
Jon Ashburn408daec2014-12-05 09:23:52 -070062 *size = sizeof(XGL_MEMORY_REQUIREMENTS);
63 if (data == NULL)
64 return ret;
Chia-I Wu9b752e12014-08-15 16:21:44 +080065 mem_req->size = img->total_size;
Chia-I Wufeb441f2014-08-08 21:27:38 +080066 mem_req->alignment = 4096;
Chia-I Wufeb441f2014-08-08 21:27:38 +080067 }
68 break;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -070069 case XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS:
70 {
71 XGL_IMAGE_MEMORY_REQUIREMENTS *img_req = data;
72
73 *size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
74 if (data == NULL)
75 return ret;
76 img_req->usage = img->usage;
77 img_req->formatClass = img->format_class;
78 img_req->samples = img->samples;
79 }
80 break;
81 case XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS:
82 {
83 XGL_BUFFER_MEMORY_REQUIREMENTS *buf_req = data;
84
85 *size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
86 if (data == NULL)
87 return ret;
88 buf_req->usage = img->usage;
89 }
90 break;
Chia-I Wufeb441f2014-08-08 21:27:38 +080091 default:
92 ret = intel_base_get_info(base, type, size, data);
93 break;
94 }
95
96 return ret;
97}
98
99XGL_RESULT intel_img_create(struct intel_dev *dev,
100 const XGL_IMAGE_CREATE_INFO *info,
Chia-I Wu794d12a2014-09-15 14:55:25 +0800101 bool scanout,
Chia-I Wufeb441f2014-08-08 21:27:38 +0800102 struct intel_img **img_ret)
103{
Chia-I Wufeb441f2014-08-08 21:27:38 +0800104 struct intel_img *img;
Chia-I Wu37cba152014-08-15 16:03:10 +0800105 struct intel_layout *layout;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800106
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600107 img = (struct intel_img *) intel_base_create(dev, sizeof(*img),
Chia-I Wufeb441f2014-08-08 21:27:38 +0800108 dev->base.dbg, XGL_DBG_OBJECT_IMAGE, info, 0);
109 if (!img)
110 return XGL_ERROR_OUT_OF_MEMORY;
111
Chia-I Wu37cba152014-08-15 16:03:10 +0800112 layout = &img->layout;
113
Chia-I Wueb2da592014-08-16 14:19:39 +0800114 img->type = info->imageType;
Chia-I Wu73e326f2014-08-21 11:07:57 +0800115 img->depth = info->extent.depth;
Chia-I Wuaa759372014-10-18 12:47:35 +0800116 img->mip_levels = info->mipLevels;
Chia-I Wueb2da592014-08-16 14:19:39 +0800117 img->array_size = info->arraySize;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700118 img->usage = info->usage;
Jon Ashburn3cda13b2015-01-20 17:22:07 -0700119 if (info->tiling == XGL_LINEAR_TILING)
120 img->format_class = XGL_IMAGE_FORMAT_CLASS_LINEAR;
121 else
122 img->format_class = icd_format_get_class(info->format);
Chia-I Wueb2da592014-08-16 14:19:39 +0800123 img->samples = info->samples;
Chia-I Wu794d12a2014-09-15 14:55:25 +0800124 intel_layout_init(layout, dev, info, scanout);
Chia-I Wu37cba152014-08-15 16:03:10 +0800125
126 if (layout->bo_stride > intel_max_resource_size / layout->bo_height) {
127 intel_dev_log(dev, XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
128 XGL_NULL_HANDLE, 0, 0, "image too big");
129 intel_img_destroy(img);
130 return XGL_ERROR_INVALID_MEMORY_SIZE;
131 }
Chia-I Wufeb441f2014-08-08 21:27:38 +0800132
Chia-I Wu9b752e12014-08-15 16:21:44 +0800133 img->total_size = img->layout.bo_stride * img->layout.bo_height;
134
Chia-I Wu457d0a62014-08-18 13:02:26 +0800135 if (layout->aux != INTEL_LAYOUT_AUX_NONE) {
Chia-I Wu9b752e12014-08-15 16:21:44 +0800136 img->aux_offset = u_align(img->total_size, 4096);
137 img->total_size = img->aux_offset +
138 layout->aux_stride * layout->aux_height;
139 }
140
141 if (layout->separate_stencil) {
142 XGL_IMAGE_CREATE_INFO s8_info;
143
144 img->s8_layout = icd_alloc(sizeof(*img->s8_layout), 0,
145 XGL_SYSTEM_ALLOC_INTERNAL);
146 if (!img->s8_layout) {
147 intel_img_destroy(img);
148 return XGL_ERROR_OUT_OF_MEMORY;
149 }
150
151 s8_info = *info;
152 s8_info.format.channelFormat = XGL_CH_FMT_R8;
153 assert(info->format.numericFormat == XGL_NUM_FMT_DS);
154
Chia-I Wu794d12a2014-09-15 14:55:25 +0800155 intel_layout_init(img->s8_layout, dev, &s8_info, scanout);
Chia-I Wu9b752e12014-08-15 16:21:44 +0800156
157 img->s8_offset = u_align(img->total_size, 4096);
158 img->total_size = img->s8_offset +
159 img->s8_layout->bo_stride * img->s8_layout->bo_height;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800160 }
161
Chia-I Wufeb441f2014-08-08 21:27:38 +0800162 img->obj.destroy = img_destroy;
163 img->obj.base.get_info = img_get_info;
164
Chia-I Wu1db76e02014-09-15 14:21:14 +0800165#ifdef ENABLE_WSI_X11
166 img->x11_prime_fd = -1;
167#endif
168
Chia-I Wufeb441f2014-08-08 21:27:38 +0800169 *img_ret = img;
170
171 return XGL_SUCCESS;
172}
173
174void intel_img_destroy(struct intel_img *img)
175{
Chia-I Wu1db76e02014-09-15 14:21:14 +0800176#ifdef ENABLE_WSI_X11
177 if (img->x11_prime_fd >= 0) {
178 close(img->x11_prime_fd);
179 intel_mem_free(img->obj.mem);
180 }
181#endif
182
Chia-I Wu9b752e12014-08-15 16:21:44 +0800183 if (img->s8_layout)
184 icd_free(img->s8_layout);
185
Chia-I Wufeb441f2014-08-08 21:27:38 +0800186 intel_base_destroy(&img->obj.base);
187}
188
Chia-I Wu96177272015-01-03 15:27:41 +0800189ICD_EXPORT XGL_RESULT XGLAPI xglOpenPeerImage(
Chia-I Wu251e7d92014-08-19 13:35:42 +0800190 XGL_DEVICE device,
191 const XGL_PEER_IMAGE_OPEN_INFO* pOpenInfo,
192 XGL_IMAGE* pImage,
193 XGL_GPU_MEMORY* pMem)
194{
195 return XGL_ERROR_UNAVAILABLE;
196}
197
Chia-I Wu96177272015-01-03 15:27:41 +0800198ICD_EXPORT XGL_RESULT XGLAPI xglCreateImage(
Chia-I Wufeb441f2014-08-08 21:27:38 +0800199 XGL_DEVICE device,
200 const XGL_IMAGE_CREATE_INFO* pCreateInfo,
201 XGL_IMAGE* pImage)
202{
203 struct intel_dev *dev = intel_dev(device);
204
Chia-I Wu794d12a2014-09-15 14:55:25 +0800205 return intel_img_create(dev, pCreateInfo, false,
206 (struct intel_img **) pImage);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800207}
208
Chia-I Wu96177272015-01-03 15:27:41 +0800209ICD_EXPORT XGL_RESULT XGLAPI xglGetImageSubresourceInfo(
Chia-I Wufeb441f2014-08-08 21:27:38 +0800210 XGL_IMAGE image,
211 const XGL_IMAGE_SUBRESOURCE* pSubresource,
212 XGL_SUBRESOURCE_INFO_TYPE infoType,
213 XGL_SIZE* pDataSize,
214 XGL_VOID* pData)
215{
216 const struct intel_img *img = intel_img(image);
217 XGL_RESULT ret = XGL_SUCCESS;
218
219 switch (infoType) {
220 case XGL_INFO_TYPE_SUBRESOURCE_LAYOUT:
221 {
222 XGL_SUBRESOURCE_LAYOUT *layout = (XGL_SUBRESOURCE_LAYOUT *) pData;
Chia-I Wu2b685d72014-08-14 13:45:37 +0800223 unsigned x, y;
224
225 intel_layout_get_slice_pos(&img->layout, pSubresource->mipLevel,
226 pSubresource->arraySlice, &x, &y);
227 intel_layout_pos_to_mem(&img->layout, x, y, &x, &y);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800228
229 *pDataSize = sizeof(XGL_SUBRESOURCE_LAYOUT);
230
Jon Ashburnbf4182c2014-12-04 15:22:01 -0700231 if (pData == NULL)
232 return ret;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800233 layout->offset = intel_layout_mem_to_linear(&img->layout, x, y);
Chia-I Wu2b685d72014-08-14 13:45:37 +0800234 layout->size = intel_layout_get_slice_size(&img->layout,
235 pSubresource->mipLevel);
236 layout->rowPitch = img->layout.bo_stride;
237 layout->depthPitch = intel_layout_get_slice_stride(&img->layout,
238 pSubresource->mipLevel);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800239 }
240 break;
241 default:
242 ret = XGL_ERROR_INVALID_VALUE;
243 break;
244 }
245
246 return ret;
247}