12f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan/*
22f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * cl_event.cpp - CL event
32f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *
42f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *  Copyright (c) 2015 Intel Corporation
52f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *
62f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * Licensed under the Apache License, Version 2.0 (the "License");
72f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * you may not use this file except in compliance with the License.
82f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * You may obtain a copy of the License at
92f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *
102f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *      http://www.apache.org/licenses/LICENSE-2.0
112f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *
122f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * Unless required by applicable law or agreed to in writing, software
132f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * distributed under the License is distributed on an "AS IS" BASIS,
142f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
152f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * See the License for the specific language governing permissions and
162f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * limitations under the License.
172f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan *
182f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan * Author: Wind Yuan <feng.yuan@intel.com>
192f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan */
202f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
212f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan#include "cl_event.h"
222f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
232f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuannamespace XCam {
242f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
252f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanSmartPtr<CLEvent>  CLEvent::NullEvent;
262f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanCLEventList CLEvent::EmptyList;
272f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
282f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanCLEvent::CLEvent (cl_event event_id)
292f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    : _event_id (event_id)
302f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan{
312f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan}
322f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
332f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanCLEvent::~CLEvent ()
342f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan{
352f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    if (_event_id) {
362f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        clReleaseEvent (_event_id);
372f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    }
382f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan}
392f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
402f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanXCamReturn
412f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanCLEvent::wait ()
422f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan{
432f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    cl_int error_code = CL_SUCCESS;
442f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
452f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_FAIL_RETURN (
462f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        DEBUG,
472f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        _event_id,
482f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        XCAM_RETURN_ERROR_PARAM,
492f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        "cl event wait failed, there's no event id");
502f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
512f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    error_code = clWaitForEvents (1, &_event_id);
522f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
532f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_FAIL_RETURN (
542f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        WARNING,
552f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        error_code == CL_SUCCESS,
562f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        XCAM_RETURN_ERROR_CL,
572f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        "cl event wait failed with error cod:%d", error_code);
582f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
592f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    return XCAM_RETURN_NO_ERROR;
602f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan}
612f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
622f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuanbool
632f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanCLEvent::get_cl_event_info (
642f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    cl_event_info param_name, size_t param_size,
652f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    void *param, size_t *param_size_ret)
662f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan{
672f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    cl_int error_code = CL_SUCCESS;
682f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
692f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_FAIL_RETURN (
702f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        DEBUG,
712f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        _event_id,
722f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        false,
732f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        "cl event wait failed, there's no event id");
742f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
752f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    clGetEventInfo (_event_id, param_name, param_size, param, param_size_ret);
762f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
772f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_FAIL_RETURN(
782f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        WARNING,
792f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        error_code == CL_SUCCESS,
802f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        false,
812f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        "clGetEventInfo failed on param:%d, errno:%d", param_name, error_code);
822f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    return true;
832f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan}
842f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
852f30f038a16f544c87dbafcbcf89ddf38175e51aWind YuanXCamReturn
862f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuancl_events_wait (CLEventList &event_list)
872f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan{
882f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan#define XCAM_MAX_CL_EVENT_COUNT 256
892f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
902f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    cl_event event_ids [XCAM_MAX_CL_EVENT_COUNT];
912f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    uint32_t event_count = 0;
922f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    cl_int error_code = CL_SUCCESS;
932f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
942f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    if (event_list.empty ())
952f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        return XCAM_RETURN_NO_ERROR;
962f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
972f9c6c5fdfd5c87046295b37cd3bd99b6d4e7b09Wind Yuan    xcam_mem_clear (event_ids);
982f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    for (CLEventList::iterator iter = event_list.begin ();
992f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan            iter != event_list.end (); ++iter) {
1002f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        SmartPtr<CLEvent> &event = *iter;
1012f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        XCAM_ASSERT (event->get_event_id ());
1022f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        event_ids[event_count++] = event->get_event_id ();
1032f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        if (event_count >= XCAM_MAX_CL_EVENT_COUNT)
1042f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan            break;
1052f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    }
1062f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
1072f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_ASSERT (event_count > 0);
1082f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
1092f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    error_code = clWaitForEvents (event_count, event_ids);
1102f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
1112f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    XCAM_FAIL_RETURN (
1122f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        WARNING,
1132f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        error_code == CL_SUCCESS,
1142f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        XCAM_RETURN_ERROR_CL,
1152f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan        "cl events wait failed with error cod:%d", error_code);
1162f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
1172f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan    return XCAM_RETURN_NO_ERROR;
1182f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan}
1192f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan
1202f30f038a16f544c87dbafcbcf89ddf38175e51aWind Yuan};
121