1/*
2 * soft_video_buf_allocator.cpp - soft video buffer allocator implementation
3 *
4 *  Copyright (c) 2017 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *      http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21#include "soft_video_buf_allocator.h"
22
23namespace XCam {
24
25class VideoMemData
26    : public BufferData
27{
28public:
29    explicit VideoMemData (uint32_t size);
30    virtual ~VideoMemData ();
31    bool is_valid () const {
32        return (_mem_ptr ? true : false);
33    }
34
35    //derive from BufferData
36    virtual uint8_t *map ();
37    virtual bool unmap ();
38
39private:
40    uint8_t    *_mem_ptr;
41    uint32_t    _mem_size;
42};
43
44VideoMemData::VideoMemData (uint32_t size)
45    : _mem_ptr (NULL)
46    , _mem_size (0)
47{
48    XCAM_ASSERT (size > 0);
49    _mem_ptr = xcam_malloc_type_array (uint8_t, size);
50    if (_mem_ptr)
51        _mem_size = size;
52}
53
54VideoMemData::~VideoMemData ()
55{
56    xcam_free (_mem_ptr);
57}
58
59uint8_t *
60VideoMemData::map ()
61{
62    XCAM_ASSERT (_mem_ptr);
63    return _mem_ptr;
64}
65
66bool
67VideoMemData::unmap ()
68{
69    return true;
70}
71
72SoftVideoBufAllocator::SoftVideoBufAllocator ()
73{
74}
75
76SoftVideoBufAllocator::SoftVideoBufAllocator (const VideoBufferInfo &info)
77{
78    set_video_info (info);
79}
80
81SoftVideoBufAllocator::~SoftVideoBufAllocator ()
82{
83}
84
85SmartPtr<BufferData>
86SoftVideoBufAllocator::allocate_data (const VideoBufferInfo &buffer_info)
87{
88    XCAM_FAIL_RETURN (
89        ERROR, buffer_info.size, NULL,
90        "SoftVideoBufAllocator allocate data failed. buf_size is zero");
91
92    SmartPtr<VideoMemData> data = new VideoMemData (buffer_info.size);
93    XCAM_FAIL_RETURN (
94        ERROR, data.ptr () && data->is_valid (), NULL,
95        "SoftVideoBufAllocator allocate data failed. buf_size:%d", buffer_info.size);
96
97    return data;
98}
99
100}
101
102