Пример #1
0
SmartPtr<BufferData>
X3aStatsPool::allocate_data (const VideoBufferInfo &buffer_info)
{
    XCam3AStats *stats = NULL;
    stats =
        (XCam3AStats *) xcam_malloc0 (
            sizeof (XCam3AStats) +
            sizeof (XCamGridStat) * _stats_info.aligned_width * _stats_info.aligned_height);
    stats->info = _stats_info;
    return new X3aStatsData (stats);
}
Пример #2
0
XCamReturn
CLKernel::build_kernel (const XCamKernelInfo& info, const char* options)
{
    KernelMap::iterator i_kernel;
    SmartPtr<CLKernel> single_kernel;
    char key_str[1024];
    uint8_t body_key[8];
    std::string key;
    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_FAIL_RETURN (ERROR, info.kernel_name, XCAM_RETURN_ERROR_PARAM, "build kernel failed since kernel name null");

    xcam_mem_clear (body_key);
    get_string_key_id (info.kernel_body, info.kernel_body_len, body_key);
    snprintf (
        key_str, sizeof(key_str),
        "%s#%02x%02x%02x%02x%02x%02x%02x%02x#%s",
        info.kernel_name,
        body_key[0], body_key[1], body_key[2], body_key[3], body_key[4], body_key[5], body_key[6], body_key[7],
        XCAM_STR(options));
    key = key_str;

    char temp_filename[XCAM_MAX_STR_SIZE] = {0};
    char cache_filename[XCAM_MAX_STR_SIZE] = {0};
    FileHandle temp_file;
    FileHandle cache_file;
    size_t read_cache_size = 0;
    size_t write_cache_size = 0;
    uint8_t *kernel_cache = NULL;
    bool load_cache = false;
    struct timeval ts;

    std::string cache_path = _kernel_cache_path;
    const char *env = std::getenv ("XCAM_CL_KERNEL_CACHE_PATH");
    if (env)
        cache_path.assign (env, strlen (env));

    snprintf (
        cache_filename, XCAM_MAX_STR_SIZE - 1,
        "%s/%s",
        cache_path.c_str (), key_str);

    {
        SmartLock locker (_kernel_map_mutex);

        i_kernel = _kernel_map.find (key);
        if (i_kernel == _kernel_map.end ()) {
            SmartPtr<CLContext>  context = get_context ();
            single_kernel = new CLKernel (context, info.kernel_name);
            XCAM_ASSERT (single_kernel.ptr ());

            if (access (cache_path.c_str (), F_OK) == -1) {
                mkdir (cache_path.c_str (), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
            }

            ret = cache_file.open (cache_filename, "r");
            if (ret == XCAM_RETURN_NO_ERROR) {
                cache_file.get_file_size (read_cache_size);
                if (read_cache_size > 0) {
                    kernel_cache = (uint8_t*) xcam_malloc0 (sizeof (uint8_t) * (read_cache_size + 1));
                    if (NULL != kernel_cache) {
                        cache_file.read_file (kernel_cache, read_cache_size);
                        cache_file.close ();

                        ret = single_kernel->load_from_binary (kernel_cache, read_cache_size);
                        xcam_free (kernel_cache);
                        kernel_cache = NULL;

                        XCAM_FAIL_RETURN (
                            ERROR, ret == XCAM_RETURN_NO_ERROR, ret,
                            "build kernel(%s) from binary failed", key_str);

                        load_cache = true;
                    }
                }
            } else {
                XCAM_LOG_DEBUG ("open kernel cache file to read failed ret(%d)", ret);
            }

            if (load_cache == false) {
                ret = single_kernel->load_from_source (info.kernel_body, strlen (info.kernel_body), &kernel_cache, &write_cache_size, options);
                XCAM_FAIL_RETURN (
                    ERROR, ret == XCAM_RETURN_NO_ERROR, ret,
                    "build kernel(%s) from source failed", key_str);
            }

            _kernel_map.insert (std::make_pair (key, single_kernel));
            //_kernel_map[key] = single_kernel;
        } else {
            single_kernel = i_kernel->second;
        }
    }

    if (load_cache == false && NULL != kernel_cache) {
        gettimeofday (&ts, NULL);
        snprintf (
            temp_filename, XCAM_MAX_STR_SIZE - 1,
            "%s." XCAM_TIMESTAMP_FORMAT,
            cache_filename, XCAM_TIMESTAMP_ARGS (XCAM_TIMEVAL_2_USEC (ts)));

        ret = temp_file.open (temp_filename, "wb");
        if (ret == XCAM_RETURN_NO_ERROR) {
            ret = temp_file.write_file (kernel_cache, write_cache_size);
            temp_file.close ();
            if (ret == XCAM_RETURN_NO_ERROR && write_cache_size > 0) {
                rename (temp_filename, cache_filename);
            } else {
                remove (temp_filename);
            }
        } else {
            XCAM_LOG_ERROR ("open kernel cache file to write failed ret(%d)", ret);
        }
        xcam_free (kernel_cache);
        kernel_cache = NULL;
    }

    XCAM_FAIL_RETURN (
        ERROR, (single_kernel.ptr () && single_kernel->is_valid ()), XCAM_RETURN_ERROR_UNKNOWN,
        "build kernel(%s) failed, unknown error", key_str);

    ret = this->clone (single_kernel);
    XCAM_FAIL_RETURN (
        ERROR, ret == XCAM_RETURN_NO_ERROR, ret,
        "load kernel(%s) from kernel failed", key_str);
    return ret;
}