liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * cl_device.h - CL device
 *
 *  Copyright (c) 2015 Intel Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Wind Yuan <feng.yuan@intel.com>
 */
 
#ifndef XCAM_CL_DEVICE_H
#define XCAM_CL_DEVICE_H
 
#include <xcam_std.h>
#include <xcam_mutex.h>
#include <CL/cl.h>
 
namespace XCam {
 
class CLContext;
 
struct CLDevieInfo {
    uint32_t  max_compute_unit;
    uint32_t  max_work_item_dims;
    size_t    max_work_item_sizes [3];
    size_t    max_work_group_size;
    uint32_t  image_pitch_alignment;
 
    CLDevieInfo ()
        : max_compute_unit (0)
        , max_work_item_dims (0)
        , max_work_group_size (0)
        , image_pitch_alignment (4)
    {
        xcam_mem_clear (max_work_item_sizes);
    }
};
 
// terminate () must called before program exit
 
class CLDevice {
public:
    ~CLDevice ();
    static SmartPtr<CLDevice> instance ();
 
    bool is_inited () const {
        return _inited;
    }
    const CLDevieInfo &get_device_info () {
        return _device_info;
    }
    cl_device_id get_device_id () {
        return _device_id;
    }
    cl_platform_id get_platform_id () {
        return _platform_id;
    }
    char* get_platform_name () {
        return _platform_name;
    }
 
    SmartPtr<CLContext> get_context ();
    void *get_extension_function (const char *func_name);
    void terminate ();
 
private:
    CLDevice ();
    bool init ();
    bool query_device_info (cl_device_id device_id, CLDevieInfo &info);
    bool create_default_context ();
 
    XCAM_DEAD_COPY (CLDevice);
 
private:
    static SmartPtr<CLDevice>  _instance;
    static Mutex               _instance_mutex;
    char                       _platform_name[XCAM_CL_MAX_STR_SIZE];
    cl_platform_id             _platform_id;
    cl_device_id               _device_id;
    CLDevieInfo                _device_info;
    bool                       _inited;
 
    //Mutex                      _context_mutex;
    SmartPtr<CLContext>        _default_context;
};
 
};
 
#endif //XCAM_CL_DEVICE_H