blob: 29993d7e5cd4ebf89a7d9dbd0f1e42ba7b870f65 [file] [log] [blame]
/*
* Copyright (c) 2013-2014 The Khronos Group Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and/or associated documentation files (the
* "Materials"), to deal in the Materials without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Materials, and to
* permit persons to whom the Materials are furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Materials.
*
* THE MATERIALS ARE 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 NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 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
* MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/*!
* \file
* \brief The Pixelwise Multiplication Kernel.
* \author Hans-Peter Nilsson <hp@axis.com>
*/
#include <VX/vx.h>
#include <VX/vx_helper.h>
#include <vxcl_kernel_module.h>
static vx_status VX_CALLBACK vxMultiplyInputValidator(vx_node node, vx_uint32 index)
{
vx_status status = VX_ERROR_INVALID_PARAMETERS;
if (index == 0)
{
vx_image input = 0;
vx_parameter param = vxGetParameterByIndex(node, index);
vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &input, sizeof(input));
if (input)
{
vx_df_image format = 0;
vxQueryImage(input, VX_IMAGE_ATTRIBUTE_FORMAT, &format, sizeof(format));
if (format == VX_DF_IMAGE_U8 || format == VX_DF_IMAGE_S16)
status = VX_SUCCESS;
vxReleaseImage(&input);
}
vxReleaseParameter(&param);
}
else if (index == 1)
{
vx_image images[2];
vx_parameter param[2] = {
vxGetParameterByIndex(node, 0),
vxGetParameterByIndex(node, 1),
};
vxQueryParameter(param[0], VX_PARAMETER_ATTRIBUTE_REF, &images[0], sizeof(images[0]));
vxQueryParameter(param[1], VX_PARAMETER_ATTRIBUTE_REF, &images[1], sizeof(images[1]));
if (images[0] && images[1])
{
vx_uint32 width[2], height[2];
vx_df_image format1;
vxQueryImage(images[0], VX_IMAGE_ATTRIBUTE_WIDTH, &width[0], sizeof(width[0]));
vxQueryImage(images[1], VX_IMAGE_ATTRIBUTE_WIDTH, &width[1], sizeof(width[1]));
vxQueryImage(images[0], VX_IMAGE_ATTRIBUTE_HEIGHT, &height[0], sizeof(height[0]));
vxQueryImage(images[1], VX_IMAGE_ATTRIBUTE_HEIGHT, &height[1], sizeof(height[1]));
vxQueryImage(images[1], VX_IMAGE_ATTRIBUTE_FORMAT, &format1, sizeof(format1));
if (width[0] == width[1] && height[0] == height[1] &&
(format1 == VX_DF_IMAGE_U8 || format1 == VX_DF_IMAGE_S16))
status = VX_SUCCESS;
vxReleaseImage(&images[0]);
vxReleaseImage(&images[1]);
}
vxReleaseParameter(&param[0]);
vxReleaseParameter(&param[1]);
}
else if (index == 2) /* scale: must be non-negative. */
{
vx_scalar scalar = 0;
vx_parameter param = vxGetParameterByIndex(node, index);
if (param)
{
vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &scalar, sizeof(scalar));
if (scalar)
{
vx_enum type = -1;
vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &type, sizeof(type));
if (type == VX_TYPE_FLOAT32)
{
vx_float32 scale = 0.0f;
if ((vxReadScalarValue(scalar, &scale) == VX_SUCCESS) &&
(scale >= 0))
{
status = VX_SUCCESS;
}
else
{
status = VX_ERROR_INVALID_VALUE;
}
}
else
{
status = VX_ERROR_INVALID_TYPE;
}
vxReleaseScalar(&scalar);
}
vxReleaseParameter(&param);
}
}
else if (index == 3) /* overflow_policy: truncate or saturate. */
{
vx_parameter param = vxGetParameterByIndex(node, index);
if (param)
{
vx_scalar scalar = 0;
vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &scalar, sizeof(scalar));
if (scalar)
{
vx_enum stype = 0;
vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &stype, sizeof(stype));
if (stype == VX_TYPE_ENUM)
{
vx_enum overflow_policy = 0;
vxReadScalarValue(scalar, &overflow_policy);
if ((overflow_policy == VX_CONVERT_POLICY_WRAP) ||
(overflow_policy == VX_CONVERT_POLICY_SATURATE))
{
status = VX_SUCCESS;
}
else
{
status = VX_ERROR_INVALID_VALUE;
}
}
else
{
status = VX_ERROR_INVALID_TYPE;
}
vxReleaseScalar(&scalar);
}
vxReleaseParameter(&param);
}
}
else if (index == 4) /* rounding_policy: truncate or saturate. */
{
vx_parameter param = vxGetParameterByIndex(node, index);
if (param)
{
vx_scalar scalar = 0;
vxQueryParameter(param, VX_PARAMETER_ATTRIBUTE_REF, &scalar, sizeof(scalar));
if (scalar)
{
vx_enum stype = 0;
vxQueryScalar(scalar, VX_SCALAR_ATTRIBUTE_TYPE, &stype, sizeof(stype));
if (stype == VX_TYPE_ENUM)
{
vx_enum rouding_policy = 0;
vxReadScalarValue(scalar, &rouding_policy);
if ((rouding_policy == VX_ROUND_POLICY_TO_ZERO) ||
(rouding_policy == VX_ROUND_POLICY_TO_NEAREST_EVEN))
{
status = VX_SUCCESS;
}
else
{
status = VX_ERROR_INVALID_VALUE;
}
}
else
{
status = VX_ERROR_INVALID_TYPE;
}
vxReleaseScalar(&scalar);
}
vxReleaseParameter(&param);
}
}
return status;
}
static vx_status VX_CALLBACK vxMultiplyOutputValidator(vx_node node, vx_uint32 index, vx_meta_format ptr)
{
vx_status status = VX_ERROR_INVALID_PARAMETERS;
if (index == 5)
{
/*
* We need to look at both input images, but only for the format:
* if either is S16 or the output type is not U8, then it's S16.
* The geometry of the output image is copied from the first parameter:
* the input images are known to match from input parameters validation.
*/
vx_parameter param[] = {
vxGetParameterByIndex(node, 0),
vxGetParameterByIndex(node, 1),
vxGetParameterByIndex(node, index),
};
if (param[0] && param[1] && param[2])
{
vx_image images[3];
vxQueryParameter(param[0], VX_PARAMETER_ATTRIBUTE_REF, &images[0], sizeof(images[0]));
vxQueryParameter(param[1], VX_PARAMETER_ATTRIBUTE_REF, &images[1], sizeof(images[1]));
vxQueryParameter(param[2], VX_PARAMETER_ATTRIBUTE_REF, &images[2], sizeof(images[2]));
if (images[0] && images[1] && images[2])
{
vx_uint32 width = 0, height = 0;
vx_df_image informat[2] = {VX_DF_IMAGE_VIRT, VX_DF_IMAGE_VIRT};
vx_df_image outformat = VX_DF_IMAGE_VIRT;
/*
* When passing on the geometry to the output image, we only look at
* image 0, as both input images are verified to match, at input
* validation.
*/
vxQueryImage(images[0], VX_IMAGE_ATTRIBUTE_WIDTH, &width, sizeof(width));
vxQueryImage(images[0], VX_IMAGE_ATTRIBUTE_HEIGHT, &height, sizeof(height));
vxQueryImage(images[0], VX_IMAGE_ATTRIBUTE_FORMAT, &informat[0], sizeof(informat[0]));
vxQueryImage(images[1], VX_IMAGE_ATTRIBUTE_FORMAT, &informat[1], sizeof(informat[1]));
vxQueryImage(images[2], VX_IMAGE_ATTRIBUTE_FORMAT, &outformat, sizeof(outformat));
if (informat[0] == VX_DF_IMAGE_U8 && informat[1] == VX_DF_IMAGE_U8 && outformat == VX_DF_IMAGE_U8)
{
status = VX_SUCCESS;
}
else
{
status = VX_SUCCESS;
outformat = VX_DF_IMAGE_S16;
}
vxSetMetaFormatAttribute(ptr, VX_IMAGE_ATTRIBUTE_FORMAT, &outformat, sizeof(outformat));
vxSetMetaFormatAttribute(ptr, VX_IMAGE_ATTRIBUTE_WIDTH, &width, sizeof(width));
vxSetMetaFormatAttribute(ptr, VX_IMAGE_ATTRIBUTE_HEIGHT, &height, sizeof(height));
vxReleaseImage(&images[0]);
vxReleaseImage(&images[1]);
vxReleaseImage(&images[2]);
}
vxReleaseParameter(&param[0]);
vxReleaseParameter(&param[1]);
vxReleaseParameter(&param[2]);
}
}
return status;
}
static vx_param_description_t multiply_kernel_params[] = {
{VX_INPUT, VX_TYPE_IMAGE, VX_PARAMETER_STATE_REQUIRED},
{VX_INPUT, VX_TYPE_IMAGE, VX_PARAMETER_STATE_REQUIRED},
{VX_INPUT, VX_TYPE_SCALAR, VX_PARAMETER_STATE_REQUIRED},
{VX_INPUT, VX_TYPE_SCALAR, VX_PARAMETER_STATE_REQUIRED},
{VX_INPUT, VX_TYPE_SCALAR, VX_PARAMETER_STATE_REQUIRED},
{VX_OUTPUT, VX_TYPE_IMAGE, VX_PARAMETER_STATE_REQUIRED},
};
vx_cl_kernel_description_t multiply_clkernel = {
{
VX_KERNEL_MULTIPLY,
"com.samsung.opencl.multiply",
NULL,
multiply_kernel_params, dimof(multiply_kernel_params),
vxMultiplyInputValidator,
vxMultiplyOutputValidator,
NULL,
NULL,
},
"/system/usr/vxcl/vx_multiply.cl",
"vx_multifly",
INIT_PROGRAMS,
INIT_KERNELS,
INIT_NUMKERNELS,
INIT_RETURNS,
NULL,
};