Xext: xvmc: simplify reply struct initialization

* use static initialization where possible
* put the lists into one one block, so they can be written in one pass
* simplify size computations

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-07-12 16:43:55 +02:00
parent 49a5292516
commit e03b163b9b

View File

@ -112,7 +112,6 @@ ProcXvMCQueryVersion(ClientPtr client)
xvmcQueryVersionReply rep = { xvmcQueryVersionReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.length = 0,
.major = SERVER_XVMC_MAJOR_VERSION, .major = SERVER_XVMC_MAJOR_VERSION,
.minor = SERVER_XVMC_MINOR_VERSION .minor = SERVER_XVMC_MINOR_VERSION
}; };
@ -129,11 +128,7 @@ ProcXvMCListSurfaceTypes(ClientPtr client)
{ {
XvPortPtr pPort; XvPortPtr pPort;
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
xvmcListSurfaceTypesReply rep;
xvmcSurfaceInfo info;
XvMCAdaptorPtr adaptor = NULL; XvMCAdaptorPtr adaptor = NULL;
XvMCSurfaceInfoPtr surface;
int num_surfaces;
REQUEST(xvmcListSurfaceTypesReq); REQUEST(xvmcListSurfaceTypesReq);
REQUEST_SIZE_MATCH(xvmcListSurfaceTypesReq); REQUEST_SIZE_MATCH(xvmcListSurfaceTypesReq);
@ -153,28 +148,33 @@ ProcXvMCListSurfaceTypes(ClientPtr client)
} }
} }
num_surfaces = (adaptor) ? adaptor->num_surfaces : 0; int num_surfaces = (adaptor) ? adaptor->num_surfaces : 0;
rep = (xvmcListSurfaceTypesReply) { xvmcSurfaceInfo *info = calloc(sizeof(xvmcSurfaceInfo), num_surfaces);
if (!info && num_surfaces)
return BadAlloc;
for (int i = 0; i < num_surfaces; i++) {
XvMCSurfaceInfoPtr surface = adaptor->surfaces[i];
info[i].surface_type_id = surface->surface_type_id;
info[i].chroma_format = surface->chroma_format;
info[i].max_width = surface->max_width;
info[i].max_height = surface->max_height;
info[i].subpicture_max_width = surface->subpicture_max_width;
info[i].subpicture_max_height = surface->subpicture_max_height;
info[i].mc_type = surface->mc_type;
info[i].flags = surface->flags;
}
xvmcListSurfaceTypesReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.num = num_surfaces, .num = num_surfaces,
.length = bytes_to_int32(num_surfaces * sizeof(xvmcSurfaceInfo)), .length = bytes_to_int32(sizeof(xvmcSurfaceInfo) * num_surfaces),
}; };
WriteToClient(client, sizeof(xvmcListSurfaceTypesReply), &rep); WriteToClient(client, sizeof(xvmcListSurfaceTypesReply), &rep);
WriteToClient(client, sizeof(xvmcSurfaceInfo) * num_surfaces, info);
for (int i = 0; i < num_surfaces; i++) { free(info);
surface = adaptor->surfaces[i];
info.surface_type_id = surface->surface_type_id;
info.chroma_format = surface->chroma_format;
info.max_width = surface->max_width;
info.max_height = surface->max_height;
info.subpicture_max_width = surface->subpicture_max_width;
info.subpicture_max_height = surface->subpicture_max_height;
info.mc_type = surface->mc_type;
info.flags = surface->flags;
WriteToClient(client, sizeof(xvmcSurfaceInfo), &info);
}
return Success; return Success;
} }
@ -191,7 +191,6 @@ ProcXvMCCreateContext(ClientPtr client)
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
XvMCAdaptorPtr adaptor = NULL; XvMCAdaptorPtr adaptor = NULL;
XvMCSurfaceInfoPtr surface = NULL; XvMCSurfaceInfoPtr surface = NULL;
xvmcCreateContextReply rep;
REQUEST(xvmcCreateContextReq); REQUEST(xvmcCreateContextReq);
REQUEST_SIZE_MATCH(xvmcCreateContextReq); REQUEST_SIZE_MATCH(xvmcCreateContextReq);
@ -256,7 +255,7 @@ ProcXvMCCreateContext(ClientPtr client)
return BadAlloc; return BadAlloc;
} }
rep = (xvmcCreateContextReply) { xvmcCreateContextReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.length = dwords, .length = dwords,
@ -302,7 +301,6 @@ ProcXvMCCreateSurface(ClientPtr client)
XvMCContextPtr pContext; XvMCContextPtr pContext;
XvMCSurfacePtr pSurface; XvMCSurfacePtr pSurface;
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
xvmcCreateSurfaceReply rep;
REQUEST(xvmcCreateSurfaceReq); REQUEST(xvmcCreateSurfaceReq);
REQUEST_SIZE_MATCH(xvmcCreateSurfaceReq); REQUEST_SIZE_MATCH(xvmcCreateSurfaceReq);
@ -335,7 +333,7 @@ ProcXvMCCreateSurface(ClientPtr client)
return BadAlloc; return BadAlloc;
} }
rep = (xvmcCreateSurfaceReply) { xvmcCreateSurfaceReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.length = dwords .length = dwords
@ -380,7 +378,6 @@ ProcXvMCCreateSubpicture(ClientPtr client)
XvMCContextPtr pContext; XvMCContextPtr pContext;
XvMCSubpicturePtr pSubpicture; XvMCSubpicturePtr pSubpicture;
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
xvmcCreateSubpictureReply rep;
XvMCAdaptorPtr adaptor; XvMCAdaptorPtr adaptor;
XvMCSurfaceInfoPtr surface = NULL; XvMCSurfaceInfoPtr surface = NULL;
@ -455,7 +452,7 @@ ProcXvMCCreateSubpicture(ClientPtr client)
return BadAlloc; return BadAlloc;
} }
rep = (xvmcCreateSubpictureReply) { xvmcCreateSubpictureReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.length = dwords, .length = dwords,
@ -503,12 +500,10 @@ static int
ProcXvMCListSubpictureTypes(ClientPtr client) ProcXvMCListSubpictureTypes(ClientPtr client)
{ {
XvPortPtr pPort; XvPortPtr pPort;
xvmcListSubpictureTypesReply rep;
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
ScreenPtr pScreen; ScreenPtr pScreen;
XvMCAdaptorPtr adaptor = NULL; XvMCAdaptorPtr adaptor = NULL;
XvMCSurfaceInfoPtr surface = NULL; XvMCSurfaceInfoPtr surface = NULL;
xvImageFormatInfo info;
XvImagePtr pImage; XvImagePtr pImage;
REQUEST(xvmcListSubpictureTypesReq); REQUEST(xvmcListSubpictureTypesReq);
@ -544,19 +539,14 @@ ProcXvMCListSubpictureTypes(ClientPtr client)
if (!surface) if (!surface)
return BadMatch; return BadMatch;
rep = (xvmcListSubpictureTypesReply) { int num = (surface->compatible_subpictures ?
.type = X_Reply, surface->compatible_subpictures->num_xvimages : 0);
.sequenceNumber = client->sequence,
.num = 0
};
if (surface->compatible_subpictures)
rep.num = surface->compatible_subpictures->num_xvimages;
rep.length = bytes_to_int32(rep.num * sizeof(xvImageFormatInfo)); xvImageFormatInfo *info = calloc(sizeof(xvImageFormatInfo), num);
if (!info && num)
return BadAlloc;
WriteToClient(client, sizeof(xvmcListSubpictureTypesReply), &rep); for (int i = 0; i < num; i++) {
for (int i = 0; i < rep.num; i++) {
pImage = NULL; pImage = NULL;
for (int j = 0; j < adaptor->num_subpictures; j++) { for (int j = 0; j < adaptor->num_subpictures; j++) {
if (surface->compatible_subpictures->xvimage_ids[i] == if (surface->compatible_subpictures->xvimage_ids[i] ==
@ -565,41 +555,51 @@ ProcXvMCListSubpictureTypes(ClientPtr client)
break; break;
} }
} }
if (!pImage) if (!pImage) {
free(info);
return BadImplementation; return BadImplementation;
}
info.id = pImage->id; info[i].id = pImage->id;
info.type = pImage->type; info[i].type = pImage->type;
info.byte_order = pImage->byte_order; info[i].byte_order = pImage->byte_order;
memcpy(&info.guid, pImage->guid, 16); memcpy(&info[i].guid, pImage->guid, 16);
info.bpp = pImage->bits_per_pixel; info[i].bpp = pImage->bits_per_pixel;
info.num_planes = pImage->num_planes; info[i].num_planes = pImage->num_planes;
info.depth = pImage->depth; info[i].depth = pImage->depth;
info.red_mask = pImage->red_mask; info[i].red_mask = pImage->red_mask;
info.green_mask = pImage->green_mask; info[i].green_mask = pImage->green_mask;
info.blue_mask = pImage->blue_mask; info[i].blue_mask = pImage->blue_mask;
info.format = pImage->format; info[i].format = pImage->format;
info.y_sample_bits = pImage->y_sample_bits; info[i].y_sample_bits = pImage->y_sample_bits;
info.u_sample_bits = pImage->u_sample_bits; info[i].u_sample_bits = pImage->u_sample_bits;
info.v_sample_bits = pImage->v_sample_bits; info[i].v_sample_bits = pImage->v_sample_bits;
info.horz_y_period = pImage->horz_y_period; info[i].horz_y_period = pImage->horz_y_period;
info.horz_u_period = pImage->horz_u_period; info[i].horz_u_period = pImage->horz_u_period;
info.horz_v_period = pImage->horz_v_period; info[i].horz_v_period = pImage->horz_v_period;
info.vert_y_period = pImage->vert_y_period; info[i].vert_y_period = pImage->vert_y_period;
info.vert_u_period = pImage->vert_u_period; info[i].vert_u_period = pImage->vert_u_period;
info.vert_v_period = pImage->vert_v_period; info[i].vert_v_period = pImage->vert_v_period;
memcpy(&info.comp_order, pImage->component_order, 32); memcpy(&info[i].comp_order, pImage->component_order, 32);
info.scanline_order = pImage->scanline_order; info[i].scanline_order = pImage->scanline_order;
WriteToClient(client, sizeof(xvImageFormatInfo), &info);
} }
xvmcListSubpictureTypesReply rep = {
.type = X_Reply,
.sequenceNumber = client->sequence,
.num = num,
.length = bytes_to_int32(sizeof(info)),
};
WriteToClient(client, sizeof(xvmcListSubpictureTypesReply), &rep);
WriteToClient(client, sizeof(xvImageFormatInfo) * num, info);
free(info);
return Success; return Success;
} }
static int static int
ProcXvMCGetDRInfo(ClientPtr client) ProcXvMCGetDRInfo(ClientPtr client)
{ {
xvmcGetDRInfoReply rep;
XvPortPtr pPort; XvPortPtr pPort;
ScreenPtr pScreen; ScreenPtr pScreen;
XvMCScreenPtr pScreenPriv; XvMCScreenPtr pScreenPriv;
@ -616,21 +616,30 @@ ProcXvMCGetDRInfo(ClientPtr client)
pScreen = pPort->pAdaptor->pScreen; pScreen = pPort->pAdaptor->pScreen;
pScreenPriv = XVMC_GET_PRIVATE(pScreen); pScreenPriv = XVMC_GET_PRIVATE(pScreen);
rep = (xvmcGetDRInfoReply) { int nameLen = strlen(pScreenPriv->clientDriverName) + 1;
int busIDLen = strlen(pScreenPriv->busID) + 1;
// buffer holds two zero-terminated strings, padded to 4-byte ints
const size_t buflen = pad_to_int32(nameLen+busIDLen);
char *buf = calloc(1, buflen);
if (!buf)
return BadAlloc;
memcpy(buf, pScreenPriv->clientDriverName, nameLen);
memcpy(buf+nameLen, pScreenPriv->busID, busIDLen);
xvmcGetDRInfoReply rep = {
.type = X_Reply, .type = X_Reply,
.sequenceNumber = client->sequence, .sequenceNumber = client->sequence,
.major = pScreenPriv->major, .major = pScreenPriv->major,
.minor = pScreenPriv->minor, .minor = pScreenPriv->minor,
.patchLevel = pScreenPriv->patchLevel, .patchLevel = pScreenPriv->patchLevel,
.nameLen = bytes_to_int32(strlen(pScreenPriv->clientDriverName) + 1), .nameLen = nameLen,
.busIDLen = bytes_to_int32(strlen(pScreenPriv->busID) + 1), .busIDLen = busIDLen,
.length = bytes_to_int32(sizeof(buf)),
.isLocal = 1 .isLocal = 1
}; };
rep.length = rep.nameLen + rep.busIDLen;
rep.nameLen <<= 2;
rep.busIDLen <<= 2;
/* /*
* Read back to the client what she has put in the shared memory * Read back to the client what she has put in the shared memory
* segment she prepared for us. * segment she prepared for us.
@ -658,10 +667,8 @@ ProcXvMCGetDRInfo(ClientPtr client)
#endif /* HAS_XVMCSHM */ #endif /* HAS_XVMCSHM */
WriteToClient(client, sizeof(xvmcGetDRInfoReply), &rep); WriteToClient(client, sizeof(xvmcGetDRInfoReply), &rep);
if (rep.length) { WriteToClient(client, buflen, buf);
WriteToClient(client, rep.nameLen, pScreenPriv->clientDriverName); free(buf);
WriteToClient(client, rep.busIDLen, pScreenPriv->busID);
}
return Success; return Success;
} }