aboutsummaryrefslogtreecommitdiff
path: root/xcore/image_handler.cpp
blob: 2dabc9776476f17d30bbb32dedb72c0c68749acc (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
/*
 * image_handler.cpp - image handler implementation
 *
 *  Copyright (c) 2017 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 "image_handler.h"

namespace XCam {

ImageHandler::ImageHandler (const char* name)
    : _need_configure (true)
    , _enable_allocator (true)
    , _buf_capacity (XCAM_DEFAULT_HANDLER_BUF_CAP)
    , _name (NULL)
{
    if (name)
        _name = strndup (name, XCAM_MAX_STR_SIZE);
}

ImageHandler::~ImageHandler()
{
    xcam_mem_clear (_name);
}

bool
ImageHandler::set_out_video_info (const VideoBufferInfo &info)
{
    XCAM_ASSERT (info.width && info.height && info.format);
    _out_video_info = info;
    return true;
}

bool
ImageHandler::enable_allocator (bool enable, uint32_t buf_count)
{

    if (enable && !buf_count) {
        XCAM_LOG_ERROR (
            "ImageHandler(%s) enable allocator must with buf_count>0", XCAM_STR(get_name ()));
        return false;
    }

    _enable_allocator = enable;
    if (enable)
        _buf_capacity = buf_count;

    return true;
}

bool
ImageHandler::set_allocator (const SmartPtr<BufferPool> &allocator)
{
    XCAM_FAIL_RETURN (
        ERROR, allocator.ptr (), false,
        "ImageHandler(%s) set allocator(is NULL)", XCAM_STR(get_name ()));
    _allocator = allocator;
    return true;
}

XCamReturn
ImageHandler::configure_rest ()
{
    if (_enable_allocator) {
        XCAM_FAIL_RETURN (
            ERROR, _out_video_info.is_valid (), XCAM_RETURN_ERROR_PARAM,
            "image_hander(%s) configure reset failed before reserver buffer since out_video_info was not set",
            XCAM_STR (get_name ()));

        SmartPtr<BufferPool> allocator = create_allocator ();
        XCAM_FAIL_RETURN (
            ERROR, allocator.ptr (), XCAM_RETURN_ERROR_PARAM,
            "image_hander(%s) configure reset failed since allocator not created", XCAM_STR (get_name ()));
        _allocator = allocator;
        XCamReturn ret = reserve_buffers (_out_video_info, _buf_capacity);
        XCAM_FAIL_RETURN (
            ERROR, xcam_ret_is_ok (ret), ret,
            "soft_hander(%s) configure resource failed in reserving buffers", XCAM_STR (get_name ()));
    }
    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
ImageHandler::execute_buffer (const SmartPtr<ImageHandler::Parameters> &param, bool sync)
{
    XCAM_UNUSED (sync);

    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_FAIL_RETURN (
        ERROR, param.ptr (), XCAM_RETURN_ERROR_PARAM,
        "image_handler(%s) execute buffer failed, params is null",
        XCAM_STR (get_name ()));

    if (_need_configure) {
        ret = configure_resource (param);
        XCAM_FAIL_RETURN (
            WARNING, xcam_ret_is_ok (ret), ret,
            "image_handler(%s) configure resource failed", XCAM_STR (get_name ()));

        ret = configure_rest ();
        XCAM_FAIL_RETURN (
            WARNING, xcam_ret_is_ok (ret), ret,
            "image_handler(%s) configure rest failed", XCAM_STR (get_name ()));
        _need_configure = false;
    }

    if (!param->out_buf.ptr () && _enable_allocator) {
        param->out_buf = get_free_buf ();
        XCAM_FAIL_RETURN (
            ERROR, param->out_buf.ptr (), XCAM_RETURN_ERROR_PARAM,
            "image_handler:%s execute buffer failed, output buffer failed in allocation.",
            XCAM_STR (get_name ()));
    }

    ret = start_work (param);
    XCAM_FAIL_RETURN (
        ERROR, xcam_ret_is_ok (ret), ret,
        "image_handler(%s) execute buffer failed in starting workers", XCAM_STR (get_name ()));

    return ret;
}

void
ImageHandler::execute_done (const SmartPtr<ImageHandler::Parameters> &param, XCamReturn err)
{
    XCAM_ASSERT (param.ptr ());

    if (err < XCAM_RETURN_NO_ERROR) {
        XCAM_LOG_WARNING (
            "image_handler(%s) broken with errno %d", XCAM_STR (get_name ()), (int)err);
        return ;
    }

    if (err > XCAM_RETURN_NO_ERROR) {
        XCAM_LOG_WARNING (
            "image_handler(%s) continued with errno %d", XCAM_STR (get_name ()), (int)err);
    }

    execute_status_check (param, err);
}

XCamReturn
ImageHandler::finish ()
{
    return XCAM_RETURN_NO_ERROR;
}

XCamReturn
ImageHandler::terminate ()
{
    if (_allocator.ptr ())
        _allocator->stop ();

    return XCAM_RETURN_NO_ERROR;
}

void
ImageHandler::execute_status_check (const SmartPtr<ImageHandler::Parameters> &params, const XCamReturn error)
{
    if (_callback.ptr ())
        _callback->execute_status (this, params, error);
}

XCamReturn
ImageHandler::reserve_buffers (const VideoBufferInfo &info, uint32_t count)
{
    XCAM_FAIL_RETURN (
        ERROR, _allocator.ptr (), XCAM_RETURN_ERROR_PARAM,
        "ImageHandler(%s) reserve buffers failed, alloctor was not set", XCAM_STR(get_name ()));

    _allocator->set_video_info (info);

    XCAM_FAIL_RETURN (
        ERROR, _allocator->reserve (count), XCAM_RETURN_ERROR_MEM,
        "ImageHandler(%s) reserve buffers(%d) failed", XCAM_STR(get_name ()), count);

    return XCAM_RETURN_NO_ERROR;
}

SmartPtr<VideoBuffer>
ImageHandler::get_free_buf ()
{
    XCAM_FAIL_RETURN (
        ERROR, _allocator.ptr (), NULL,
        "ImageHandler(%s) get free buffer failed since allocator was not initilized", XCAM_STR(get_name ()));

    return _allocator->get_buffer (_allocator);
}

}