aboutsummaryrefslogtreecommitdiff
path: root/modules/ocl/cl_memory.cpp
blob: 9c04cbf5801d28de533ffe1663c5c5df48d9e04f (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 * cl_memory.cpp - CL memory
 *
 *  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>
 */

#include "cl_utils.h"
#include "cl_memory.h"
#include "drm_display.h"
#include "cl_image_bo_buffer.h"

namespace XCam {

CLImageDesc::CLImageDesc ()
    : format {CL_R, CL_UNORM_INT8}
, width (0)
, height (0)
, row_pitch (0)
, slice_pitch (0)
, array_size (0)
, size (0)
{
}

bool
CLImageDesc::operator == (const CLImageDesc& desc) const
{
    if (desc.format.image_channel_data_type == this->format.image_channel_data_type &&
            desc.format.image_channel_order == this->format.image_channel_order &&
            desc.width == this->width &&
            desc.height == this->height &&
            desc.row_pitch == this->row_pitch &&
            desc.slice_pitch == this->slice_pitch &&
            desc.array_size == this->array_size)// &&
        //desc.size == this->size)
        return true;
    return false;
}

CLMemory::CLMemory (const SmartPtr<CLContext> &context)
    : _context (context)
    , _mem_id (NULL)
    , _mem_fd (-1)
    , _mem_need_destroy (true)
    , _mapped_ptr (NULL)
{
    XCAM_ASSERT (context.ptr () && context->is_valid ());
}

CLMemory::~CLMemory ()
{
    release_fd ();

    if (_mapped_ptr)
        enqueue_unmap (_mapped_ptr);

    if (_mem_id && _mem_need_destroy) {
        _context->destroy_mem (_mem_id);
    }
}

int32_t
CLMemory::export_fd ()
{
    if (_mem_fd >= 0)
        return _mem_fd;

    _mem_fd = _context->export_mem_fd (_mem_id);
    return _mem_fd;
}

void
CLMemory::release_fd ()
{
    if (_mem_fd <= 0)
        return;

    close (_mem_fd);
    _mem_fd = -1;
}

XCamReturn
CLMemory::enqueue_unmap (
    void *ptr,
    CLEventList &event_waits,
    SmartPtr<CLEvent> &event_out)
{
    SmartPtr<CLContext> context = get_context ();
    cl_mem mem_id = get_mem_id ();

    XCAM_ASSERT (is_valid ());
    if (!is_valid ())
        return XCAM_RETURN_ERROR_PARAM;

    XCAM_ASSERT (ptr == _mapped_ptr);
    if (ptr == _mapped_ptr)
        _mapped_ptr = NULL;

    return context->enqueue_unmap (mem_id, ptr, event_waits, event_out);
}

bool CLMemory::get_cl_mem_info (
    cl_image_info param_name, size_t param_size,
    void *param, size_t *param_size_ret)
{
    cl_mem mem_id = get_mem_id ();
    cl_int error_code = CL_SUCCESS;
    if (!mem_id)
        return false;

    error_code = clGetMemObjectInfo (mem_id, param_name, param_size, param, param_size_ret);
    XCAM_FAIL_RETURN(
        WARNING,
        error_code == CL_SUCCESS,
        false,
        "clGetMemObjectInfo failed on param:%d, errno:%d", param_name, error_code);
    return true;
}

CLBuffer::CLBuffer (const SmartPtr<CLContext> &context)
    : CLMemory (context)
{
}

CLBuffer::CLBuffer (
    const SmartPtr<CLContext> &context, uint32_t size,
    cl_mem_flags  flags, void *host_ptr)
    : CLMemory (context)
    , _flags (flags)
    , _size (size)
{
    init_buffer (context, size, flags, host_ptr);
}

bool
CLBuffer::init_buffer (
    const SmartPtr<CLContext> &context, uint32_t size,
    cl_mem_flags  flags, void *host_ptr)
{
    cl_mem mem_id = NULL;

    mem_id = context->create_buffer (size, flags, host_ptr);
    if (mem_id == NULL) {
        XCAM_LOG_WARNING ("CLBuffer create buffer failed");
        return false;
    }

    set_mem_id (mem_id);
    return true;
}

XCamReturn
CLBuffer::enqueue_read (
    void *ptr, uint32_t offset, uint32_t size,
    CLEventList &event_waits,
    SmartPtr<CLEvent> &event_out)
{
    SmartPtr<CLContext> context = get_context ();
    cl_mem mem_id = get_mem_id ();

    XCAM_ASSERT (is_valid ());
    if (!is_valid ())
        return XCAM_RETURN_ERROR_PARAM;

    return context->enqueue_read_buffer (mem_id, ptr, offset, size, true, event_waits, event_out);
}

XCamReturn
CLBuffer::enqueue_write (
    void *ptr, uint32_t offset, uint32_t size,
    CLEventList &event_waits,
    SmartPtr<CLEvent> &event_out)
{
    SmartPtr<CLContext> context = get_context ();
    cl_mem mem_id = get_mem_id ();

    XCAM_ASSERT (is_valid ());
    if (!is_valid ())
        return XCAM_RETURN_ERROR_PARAM;

    return context->enqueue_write_buffer (mem_id, ptr, offset, size, true, event_waits, event_out);
}

XCamReturn
CLBuffer::enqueue_map (
    void *&ptr, uint32_t offset, uint32_t size,
    cl_map_flags map_flags,
    CLEventList &event_waits,
    SmartPtr<CLEvent> &event_out)
{
    SmartPtr<CLContext> context = get_context ();
    cl_mem mem_id = get_mem_id ();
    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_ASSERT (is_valid ());
    if (!is_valid ())
        return XCAM_RETURN_ERROR_PARAM;

    ret = context->enqueue_map_buffer (mem_id, ptr, offset, size, true, map_flags, event_waits, event_out);
    XCAM_FAIL_RETURN (
        WARNING,
        ret == XCAM_RETURN_NO_ERROR,
        ret,
        "enqueue_map failed ");

    set_mapped_ptr (ptr);
    return ret;
}

CLVaBuffer::CLVaBuffer (
    const SmartPtr<CLContext> &context,
    SmartPtr<DrmBoBuffer> &bo)
    : CLBuffer (context)
    , _bo (bo)
{
    init_va_buffer (context, bo);
}

bool
CLVaBuffer::init_va_buffer (const SmartPtr<CLContext> &context, SmartPtr<DrmBoBuffer> &bo)
{
    cl_mem mem_id = NULL;
    uint32_t bo_name = 0;
    cl_import_buffer_info_intel import_buffer_info;

    xcam_mem_clear (import_buffer_info);
    import_buffer_info.fd = bo->get_fd ();
    import_buffer_info.size = bo->get_size ();
    if (import_buffer_info.fd != -1) {
        mem_id = context->import_dma_buffer (import_buffer_info);
    }

    if (mem_id == NULL) {
        drm_intel_bo_flink (bo->get_bo (), &bo_name);
        mem_id = context->create_va_buffer (bo_name);
        if (mem_id == NULL) {
            XCAM_LOG_WARNING ("CLVaBuffer create va buffer failed");
            return false;
        }
    }

    set_mem_id (mem_id);
    return true;
}

CLImage::CLImage (const SmartPtr<CLContext> &context)
    : CLMemory (context)
{
}

uint32_t
CLImage::get_pixel_bytes () const
{
    return calculate_pixel_bytes(_image_desc.format);
}

bool
CLImage::get_cl_image_info (cl_image_info param_name, size_t param_size, void *param, size_t *param_size_ret)
{
    cl_mem mem_id = get_mem_id ();
    cl_int error_code = CL_SUCCESS;
    if (!mem_id)
        return false;

    error_code = clGetImageInfo (mem_id, param_name, param_size, param, param_size_ret);
    XCAM_FAIL_RETURN(
        WARNING,
        error_code == CL_SUCCESS,
        false,
        "clGetImageInfo failed on param:%d, errno:%d", param_name, error_code);
    return true;
}

uint32_t
CLImage::calculate_pixel_bytes (const cl_image_format &fmt)
{
    uint32_t a = 0, b = 0;
    switch (fmt.image_channel_order) {
    case CL_R:
    case CL_A:
    case CL_Rx:
        a = 1;
        break;
    case CL_RG:
    case CL_RA:
    case CL_RGx:
        a = 2;
        break;
    case CL_RGB:
    case CL_RGBx:
        a = 3;
        break;
    case CL_RGBA:
    case CL_BGRA:
    case CL_ARGB:
        a = 4;
        break;
    default:
        XCAM_LOG_DEBUG ("calculate_pixel_bytes with wrong channel_order:0x%04x", fmt.image_channel_order);
        return 0;
    }

    switch (fmt.image_channel_data_type) {
    case CL_UNORM_INT8:
    case CL_SNORM_INT8:
    case CL_SIGNED_INT8:
    case CL_UNSIGNED_INT8:
        b = 1;
        break;
    case CL_SNORM_INT16:
    case CL_UNORM_INT16:
    case CL_SIGNED_INT16:
    case CL_UNSIGNED_INT16:
    case CL_HALF_FLOAT:
        b = 2;
        break;
    case CL_UNORM_INT24:
        b = 3;
        break;
    case CL_SIGNED_INT32:
    case CL_UNSIGNED_INT32:
    case CL_FLOAT:
        b = 4;
        break;
    default:
        XCAM_LOG_DEBUG ("calculate_pixel_bytes with wrong channel_data_type:0x%04x", fmt.image_channel_data_type);
        return 0;
    }

    return a * b;
}

bool
CLImage::video_info_2_cl_image_desc (
    const VideoBufferInfo & video_info,
    CLImageDesc &image_desc)
{
    image_desc.width = video_info.width;
    image_desc.height = video_info.height;
    image_desc.array_size = 0;
    image_desc.row_pitch = video_info.strides[0];
    XCAM_ASSERT (image_desc.row_pitch >= image_desc.width);
    image_desc.slice_pitch = 0;

    switch (video_info.format) {
    case XCAM_PIX_FMT_RGB48:
        //cl_image_info.fmt.image_channel_order = CL_RGB;
        //cl_image_info.fmt.image_channel_data_type = CL_UNORM_INT16;
        XCAM_LOG_WARNING (
            "video_info to cl_image_info doesn't support XCAM_PIX_FMT_RGB48, maybe try XCAM_PIX_FMT_RGBA64 instread\n"
            " **** XCAM_PIX_FMT_RGB48 need check with cl implementation ****");
        return false;
        break;
    case V4L2_PIX_FMT_GREY:
        image_desc.format.image_channel_order = CL_R;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;

    case XCAM_PIX_FMT_RGBA64:
        image_desc.format.image_channel_order = CL_RGBA;
        image_desc.format.image_channel_data_type = CL_UNORM_INT16;
        break;

    case V4L2_PIX_FMT_RGB24:
        image_desc.format.image_channel_order = CL_RGB;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;

    case V4L2_PIX_FMT_RGB565:
        image_desc.format.image_channel_order = CL_RGB;
        image_desc.format.image_channel_data_type = CL_UNORM_SHORT_565;
        break;
    case V4L2_PIX_FMT_XBGR32:
    case V4L2_PIX_FMT_ABGR32:
    case V4L2_PIX_FMT_BGR32:
        image_desc.format.image_channel_order = CL_BGRA;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;
        // cl doesn'tn support ARGB32 up to now, how about consider V4L2_PIX_FMT_RGBA32
    case V4L2_PIX_FMT_RGB32:
    case V4L2_PIX_FMT_ARGB32:
    case V4L2_PIX_FMT_XRGB32:
        image_desc.format.image_channel_order = CL_ARGB;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;

    case V4L2_PIX_FMT_RGBA32:
        image_desc.format.image_channel_order = CL_RGBA;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;

    case V4L2_PIX_FMT_SBGGR10:
    case V4L2_PIX_FMT_SGBRG10:
    case V4L2_PIX_FMT_SGRBG10:
    case V4L2_PIX_FMT_SRGGB10:
    case V4L2_PIX_FMT_SBGGR12:
    case V4L2_PIX_FMT_SGBRG12:
    case V4L2_PIX_FMT_SGRBG12:
    case V4L2_PIX_FMT_SRGGB12:
    case V4L2_PIX_FMT_SBGGR16:
    case XCAM_PIX_FMT_SGRBG16:
        image_desc.format.image_channel_order = CL_R;
        image_desc.format.image_channel_data_type = CL_UNORM_INT16;
        break;

    case V4L2_PIX_FMT_SBGGR8:
    case V4L2_PIX_FMT_SGBRG8:
    case V4L2_PIX_FMT_SGRBG8:
    case V4L2_PIX_FMT_SRGGB8:
        image_desc.format.image_channel_order = CL_R;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        break;

    case V4L2_PIX_FMT_NV12:
        image_desc.format.image_channel_order = CL_R;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        image_desc.array_size = 2;
        image_desc.slice_pitch = video_info.strides [0] * video_info.aligned_height;
        break;

    case V4L2_PIX_FMT_YUYV:
        image_desc.format.image_channel_order = CL_RGBA;
        image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        image_desc.width /= 2;
        break;

    case XCAM_PIX_FMT_LAB:
        image_desc.format.image_channel_order = CL_R;
        image_desc.format.image_channel_data_type = CL_FLOAT;
        break;

    case XCAM_PIX_FMT_RGB48_planar:
    case XCAM_PIX_FMT_RGB24_planar:
        image_desc.format.image_channel_order = CL_RGBA;
        if (XCAM_PIX_FMT_RGB48_planar == video_info.format)
            image_desc.format.image_channel_data_type = CL_UNORM_INT16;
        else
            image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        image_desc.width = video_info.aligned_width / 4;
        image_desc.array_size = 3;
        image_desc.slice_pitch = video_info.strides [0] * video_info.aligned_height;
        break;

    case XCAM_PIX_FMT_SGRBG16_planar:
    case XCAM_PIX_FMT_SGRBG8_planar:
        image_desc.format.image_channel_order = CL_RGBA;
        if (XCAM_PIX_FMT_SGRBG16_planar == video_info.format)
            image_desc.format.image_channel_data_type = CL_UNORM_INT16;
        else
            image_desc.format.image_channel_data_type = CL_UNORM_INT8;
        image_desc.width = video_info.aligned_width / 4;
        image_desc.array_size = 4;
        image_desc.slice_pitch = video_info.strides [0] * video_info.aligned_height;
        break;

    default:
        XCAM_LOG_WARNING (
            "video_info to cl_image_info doesn't support format:%s",
            xcam_fourcc_to_string (video_info.format));
        return false;
    }

    return true;
}

void
CLImage::init_desc_by_image ()
{
    size_t width = 0, height = 0, row_pitch = 0, slice_pitch = 0, array_size = 0, mem_size = 0;
    cl_image_format format = {CL_R, CL_UNORM_INT8};

    get_cl_image_info (CL_IMAGE_FORMAT, sizeof(format), &format);
    get_cl_image_info (CL_IMAGE_WIDTH, sizeof(width), &width);
    get_cl_image_info (CL_IMAGE_HEIGHT, sizeof(height), &height);
    get_cl_image_info (CL_IMAGE_ROW_PITCH, sizeof(row_pitch), &row_pitch);
    get_cl_image_info (CL_IMAGE_SLICE_PITCH, sizeof(slice_pitch), &slice_pitch);
    get_cl_image_info (CL_IMAGE_ARRAY_SIZE, sizeof(array_size), &array_size);
    get_cl_mem_info (CL_MEM_SIZE, sizeof(mem_size), &mem_size);

    _image_desc.format = format;
    _image_desc.width = width;
    _image_desc.height = height;
    _image_desc.row_pitch = row_pitch;
    _image_desc.slice_pitch = slice_pitch;
    _image_desc.array_size = array_size;
    _image_desc.size = mem_size;
}

XCamReturn
CLImage::enqueue_map (
    void *&ptr,
    size_t *origin, size_t *region,
    size_t *row_pitch, size_t *slice_pitch,
    cl_map_flags map_flags,
    CLEventList &event_waits,
    SmartPtr<CLEvent> &event_out)
{
    SmartPtr<CLContext> context = get_context ();
    cl_mem mem_id = get_mem_id ();
    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_ASSERT (is_valid ());
    if (!is_valid ())
        return XCAM_RETURN_ERROR_PARAM;

    ret = context->enqueue_map_image (mem_id, ptr, origin, region, row_pitch, slice_pitch, true, map_flags, event_waits, event_out);
    XCAM_FAIL_RETURN (
        WARNING,
        ret == XCAM_RETURN_NO_ERROR,
        ret,
        "enqueue_map failed ");

    set_mapped_ptr (ptr);
    return ret;
}

CLVaImage::CLVaImage (
    const SmartPtr<CLContext> &context,
    SmartPtr<DrmBoBuffer> &bo,
    uint32_t offset,
    bool single_plane)
    : CLImage (context)
    , _bo (bo)
{
    CLImageDesc cl_desc;

    const VideoBufferInfo & video_info = bo->get_video_info ();
    if (!video_info_2_cl_image_desc (video_info, cl_desc)) {
        XCAM_LOG_WARNING ("CLVaImage create va image failed on default videoinfo");
        return;
    }
    if (single_plane) {
        cl_desc.array_size = 0;
        cl_desc.slice_pitch = 0;
    } else if (!merge_multi_plane (video_info, cl_desc)) {
        XCAM_LOG_WARNING ("CLVaImage create va image failed on merging planes");
        return;
    }

    init_va_image (context, bo, cl_desc, offset);
}

CLVaImage::CLVaImage (
    const SmartPtr<CLContext> &context,
    SmartPtr<DrmBoBuffer> &bo,
    const CLImageDesc &image_info,
    uint32_t offset)
    : CLImage (context)
    , _bo (bo)
{
    init_va_image (context, bo, image_info, offset);
}

bool
CLVaImage::merge_multi_plane (
    const VideoBufferInfo &video_info,
    CLImageDesc &cl_desc)
{
    if (cl_desc.array_size <= 1)
        return true;

    switch (video_info.format) {
    case V4L2_PIX_FMT_NV12:
        cl_desc.height = video_info.aligned_height + video_info.height / 2;
        break;

    case XCAM_PIX_FMT_RGB48_planar:
    case XCAM_PIX_FMT_RGB24_planar:
        cl_desc.height = video_info.aligned_height * 3;
        break;

    case XCAM_PIX_FMT_SGRBG16_planar:
    case XCAM_PIX_FMT_SGRBG8_planar:
        cl_desc.height = video_info.aligned_height * 4;
        break;

    default:
        XCAM_LOG_WARNING ("CLVaImage unknown format(%s) plane change", xcam_fourcc_to_string(video_info.format));
        return false;
    }
    cl_desc.array_size = 0;
    cl_desc.slice_pitch = 0;
    return true;
}

bool
CLVaImage::init_va_image (
    const SmartPtr<CLContext> &context, SmartPtr<DrmBoBuffer> &bo,
    const CLImageDesc &cl_desc, uint32_t offset)
{

    uint32_t bo_name = 0;
    cl_mem mem_id = 0;
    bool need_create = true;
    cl_libva_image va_image_info;
    cl_import_image_info_intel import_image_info;

    xcam_mem_clear (va_image_info);
    xcam_mem_clear (import_image_info);
    import_image_info.offset = va_image_info.offset = offset;
    import_image_info.width = va_image_info.width = cl_desc.width;
    import_image_info.height = va_image_info.height = cl_desc.height;
    import_image_info.fmt = va_image_info.fmt = cl_desc.format;
    import_image_info.row_pitch = va_image_info.row_pitch = cl_desc.row_pitch;
    import_image_info.size = cl_desc.size;
    import_image_info.type = CL_MEM_OBJECT_IMAGE2D;

    XCAM_ASSERT (bo.ptr ());

    SmartPtr<CLImageBoBuffer> cl_image_buffer = bo.dynamic_cast_ptr<CLImageBoBuffer> ();
    if (cl_image_buffer.ptr ()) {
        SmartPtr<CLImage> cl_image_data = cl_image_buffer->get_cl_image ();
        XCAM_ASSERT (cl_image_data.ptr ());
        CLImageDesc old_desc = cl_image_data->get_image_desc ();
        if (cl_desc == old_desc) {
            need_create = false;
            mem_id = cl_image_data->get_mem_id ();
        }
    }

    if (need_create) {
        import_image_info.fd = bo->get_fd();
        if (import_image_info.fd != -1)
            mem_id = context->import_dma_image (import_image_info);

        if (mem_id == NULL) {
            if (drm_intel_bo_flink (bo->get_bo (), &bo_name) == 0) {
                va_image_info.bo_name = bo_name;
                mem_id = context->create_va_image (va_image_info);
            }
            if (mem_id == NULL) {
                XCAM_LOG_WARNING ("create va image failed");
                return false;
            }
        }
    } else {
        va_image_info.bo_name = uint32_t(-1);
    }

    set_mem_id (mem_id, need_create);
    init_desc_by_image ();
    _va_image_info = va_image_info;
    return true;
}


CLImage2D::CLImage2D (
    const SmartPtr<CLContext> &context,
    const VideoBufferInfo &video_info,
    cl_mem_flags  flags)
    : CLImage (context)
{
    CLImageDesc cl_desc;

    if (!video_info_2_cl_image_desc (video_info, cl_desc)) {
        XCAM_LOG_WARNING ("CLVaImage create va image failed on default videoinfo");
        return;
    }

    init_image_2d (context, cl_desc, flags);
}

CLImage2D::CLImage2D (
    const SmartPtr<CLContext> &context,
    const CLImageDesc &cl_desc,
    cl_mem_flags  flags,
    SmartPtr<CLBuffer> bind_buf)
    : CLImage (context)
{
    _bind_buf = bind_buf;
    init_image_2d (context, cl_desc, flags);
}

bool CLImage2D::init_image_2d (
    const SmartPtr<CLContext> &context,
    const CLImageDesc &desc,
    cl_mem_flags  flags)
{
    cl_mem mem_id = 0;
    cl_image_desc cl_desc;

    xcam_mem_clear (cl_desc);
    cl_desc.image_type = CL_MEM_OBJECT_IMAGE2D;
    cl_desc.image_width = desc.width;
    cl_desc.image_height = desc.height;
    cl_desc.image_depth = 1;
    cl_desc.image_array_size = 0;
    cl_desc.image_row_pitch = 0;
    cl_desc.image_slice_pitch = 0;
    cl_desc.num_mip_levels = 0;
    cl_desc.num_samples = 0;
    cl_desc.buffer = NULL;
    if (_bind_buf.ptr ()) {
        if (desc.row_pitch)
            cl_desc.image_row_pitch = desc.row_pitch;
        else {
            cl_desc.image_row_pitch = calculate_pixel_bytes(desc.format) * desc.width;
        }
        XCAM_ASSERT (cl_desc.image_row_pitch);
        cl_desc.buffer = _bind_buf->get_mem_id ();
        XCAM_ASSERT (cl_desc.buffer);
    }

    mem_id = context->create_image (flags, desc.format, cl_desc);
    if (mem_id == NULL) {
        XCAM_LOG_WARNING ("CLImage2D create image 2d failed");
        return false;
    }
    set_mem_id (mem_id);
    init_desc_by_image ();
    return true;
}

CLImage2DArray::CLImage2DArray (
    const SmartPtr<CLContext> &context,
    const VideoBufferInfo &video_info,
    cl_mem_flags  flags,
    uint32_t extra_array_size)
    : CLImage (context)
{
    CLImageDesc cl_desc;

    XCAM_ASSERT (video_info.components >= 2);

    if (!video_info_2_cl_image_desc (video_info, cl_desc)) {
        XCAM_LOG_WARNING ("CLVaImage create va image failed on default videoinfo");
        return;
    }
    XCAM_ASSERT (cl_desc.array_size >= 2);

    //special process for BYT platform for slice-pitch
    //if (video_info.format == V4L2_PIX_FMT_NV12)
    cl_desc.height = XCAM_ALIGN_UP (cl_desc.height, 16);

    cl_desc.array_size += extra_array_size;

    init_image_2d_array (context, cl_desc, flags);
}

bool CLImage2DArray::init_image_2d_array (
    const SmartPtr<CLContext> &context,
    const CLImageDesc &desc,
    cl_mem_flags  flags)
{
    cl_mem mem_id = 0;
    cl_image_desc cl_desc;

    xcam_mem_clear (cl_desc);
    cl_desc.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY;
    cl_desc.image_width = desc.width;
    cl_desc.image_height = desc.height;
    cl_desc.image_depth = 1;
    cl_desc.image_array_size = desc.array_size;
    cl_desc.image_row_pitch = 0;
    cl_desc.image_slice_pitch = 0;
    cl_desc.num_mip_levels = 0;
    cl_desc.num_samples = 0;
    cl_desc.buffer = NULL;

    mem_id = context->create_image (flags, desc.format, cl_desc);
    if (mem_id == NULL) {
        XCAM_LOG_WARNING ("CLImage2D create image 2d failed");
        return false;
    }
    set_mem_id (mem_id);
    init_desc_by_image ();
    return true;
}


};