aboutsummaryrefslogtreecommitdiff
path: root/modules/soft/soft_handler.cpp
blob: 984fa66d5233138c34cb2eeed676dad84a39f991 (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
/*
 * soft_handler.cpp - soft 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 "soft_handler.h"
#include "soft_video_buf_allocator.h"
#include "thread_pool.h"
#include "soft_worker.h"

#define DEFAULT_SOFT_BUF_COUNT 4

namespace XCam {

class SyncMeta
    : public MetaBase
{
public:
    SyncMeta ()
        : _done (false)
        , _error (XCAM_RETURN_NO_ERROR) {}
    void signal_done (XCamReturn err);
    void wakeup ();
    XCamReturn signal_wait_ret ();
    bool is_error () const;

private:
    mutable Mutex   _mutex;
    Cond            _cond;
    bool            _done;
    XCamReturn      _error;
};

void
SyncMeta::signal_done (XCamReturn err)
{
    SmartLock locker (_mutex);
    _done = true;
    _error = err;
    _cond.broadcast ();
}

void
SyncMeta::wakeup ()
{
    SmartLock locker (_mutex);
    _error = XCAM_RETURN_ERROR_UNKNOWN;
    _cond.broadcast ();
}

XCamReturn
SyncMeta::signal_wait_ret ()
{
    SmartLock locker (_mutex);
    if (_done)
        return _error;
    _cond.wait (_mutex);
    return _error;
}

bool
SyncMeta::is_error () const
{
    SmartLock locker (_mutex);
    return !xcam_ret_is_ok (_error);
}

SoftHandler::SoftHandler (const char* name)
    : ImageHandler (name)
    , _wip_buf_count (0)
{
}

SoftHandler::~SoftHandler ()
{
}

bool
SoftHandler::set_threads (const SmartPtr<ThreadPool> &pool)
{
    _threads = pool;
    return true;
}

SmartPtr<BufferPool>
SoftHandler::create_allocator ()
{
    return new SoftVideoBufAllocator;
}

XCamReturn
SoftHandler::configure_rest ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_ASSERT (_need_configure);
    ret = ImageHandler::configure_rest ();
    XCAM_FAIL_RETURN (
        ERROR, xcam_ret_is_ok (ret), ret,
        "soft_hander(%s) configure reset failed on ImageHandler::configure_rest", XCAM_STR (get_name ()));

    if (_threads.ptr () && !_threads->is_running ()) {
        ret = _threads->start ();
        XCAM_FAIL_RETURN (
            ERROR, xcam_ret_is_ok (ret), ret,
            "soft_hander(%s) configure reset failed when starting threads", XCAM_STR (get_name ()));
    }

    return ret;
}

XCamReturn
SoftHandler::execute_buffer (const SmartPtr<ImageHandler::Parameters> &param, bool sync)
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;

    XCAM_FAIL_RETURN (
        ERROR, param.ptr (), XCAM_RETURN_ERROR_PARAM,
        "soft_hander(%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,
            "soft_hander(%s) configure resource failed", XCAM_STR (get_name ()));

        ret = configure_rest ();
        XCAM_FAIL_RETURN (
            WARNING, xcam_ret_is_ok (ret), ret,
            "soft_hander(%s) confirm configure 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,
            "soft_hander:%s execute buffer failed, output buffer failed in allocation.",
            XCAM_STR (get_name ()));
    }

    XCAM_ASSERT (!param->find_meta<SyncMeta> ().ptr ());
    SmartPtr<SyncMeta> sync_meta = new SyncMeta ();
    XCAM_ASSERT (sync_meta.ptr ());
    param->add_meta (sync_meta);

#if 0
    SmartPtr<SoftWorker> worker = get_first_worker ().dynamic_cast_ptr<SoftWorker> ();
    XCAM_FAIL_RETURN (
        WARNING, worker.ptr (), XCAM_RETURN_ERROR_PARAM,
        "No worder set to soft_hander(%s)", XCAM_STR (get_name ()));

    SmartPtr<Worker::Arguments> args = get_first_worker_args (worker, params);
    XCAM_FAIL_RETURN (
        WARNING, args.ptr (), XCAM_RETURN_ERROR_PARAM,
        "soft_hander(%s) get first worker(%s) args failed",
        XCAM_STR (get_name ()), XCAM_STR (worker->get_name ()));

    _params.push (params);
    ret = worker->work (args);
#else
    _params.push (param);
    ret = start_work (param);
#endif

    if (!xcam_ret_is_ok (ret)) {
        _params.erase (param);
        XCAM_LOG_WARNING ("soft_hander(%s) execute buffer failed in starting workers", XCAM_STR (get_name ()));
        return ret;
    }

    ++_wip_buf_count;
    _cur_sync = sync_meta;

    if (sync) {
        XCAM_ASSERT (sync_meta.ptr ());
        ret = sync_meta->signal_wait_ret ();
        _cur_sync.release ();
    }

    return ret;
}

XCamReturn
SoftHandler::finish ()
{
    XCamReturn ret = XCAM_RETURN_NO_ERROR;
    SmartPtr<SyncMeta> sync = _cur_sync;
    if (sync.ptr ()) {
        ret = sync->signal_wait_ret ();
    }
    XCAM_ASSERT (_params.is_empty ());
    //wait for _wip_buf_count = 0
    //if (ret == XCAM_RETURN_NO_ERROR)
    //    XCAM_ASSERT (_wip_buf_count == 0);

    return ret;
}

XCamReturn
SoftHandler::terminate ()
{
    SmartPtr<SyncMeta> sync = _cur_sync;
    if (sync.ptr ()) {
        sync->wakeup ();
        sync.release ();
    }
    _params.clear ();
    return ImageHandler::terminate ();
}

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

    if (!xcam_ret_is_ok (err)) {
        XCAM_LOG_WARNING ("soft_hander(%s) work_well_done but errno(%d) is not ok", XCAM_STR (get_name ()), (int)err);
        //continue work
    }

    if (!_params.erase (param)) {
        XCAM_LOG_ERROR(
            "soft_hander(%s) last_work_done param already removed, who removed it?", XCAM_STR (get_name ()));
        return;
    }

    XCAM_LOG_DEBUG ("soft_hander(%s) work well done", XCAM_STR (get_name ()));

    param_ended (param, err);
}

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

    if (xcam_ret_is_ok (err)) {
        XCAM_LOG_WARNING ("soft_hander(%s) work_broken but the errno(%d) is ok", XCAM_STR (get_name ()), (int)err);
        //continue work
    }

    if (!_params.erase (param)) {
        //already removed by other handlers
        return;
    }
    XCAM_LOG_WARNING ("soft_hander(%s) work broken", XCAM_STR (get_name ()));

    param_ended (param, err);
}

void
SoftHandler::param_ended (SmartPtr<ImageHandler::Parameters> param, XCamReturn err)
{
    XCAM_ASSERT (param.ptr ());

    SmartPtr<SyncMeta> sync_meta = param->find_meta<SyncMeta> ();
    XCAM_ASSERT (sync_meta.ptr ());
    sync_meta->signal_done (err);
    --_wip_buf_count;
    execute_status_check (param, err);
}

bool
SoftHandler::check_work_continue (const SmartPtr<ImageHandler::Parameters> &param, XCamReturn err)
{
    if (!xcam_ret_is_ok (err)) {
        work_broken (param, err);
        return false;
    }

    if (is_param_error (param)) {
        XCAM_LOG_WARNING (
            "soft_handler(%s) check_work_continue found param broken", XCAM_STR(get_name ()));
        return false;
    }
    return true;
}

bool
SoftHandler::is_param_error (const SmartPtr<ImageHandler::Parameters> &param)
{
    XCAM_ASSERT (param.ptr ());
    SmartPtr<SyncMeta> meta = param->find_meta<SyncMeta> ();
    if (!meta.ptr ()) { // return ok if param not set
        XCAM_ASSERT (meta.ptr ());
        return false;
    }

    return meta->is_error ();
}

}