aboutsummaryrefslogtreecommitdiff
path: root/xcore/image_handler.h
blob: 80c64f547437528704d59a92304f831fb02ed8aa (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
/*
 * image_handler.h - image image handler class
 *
 *  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>
 */

#ifndef XCAM_IMAGE_HANDLER_H
#define XCAM_IMAGE_HANDLER_H

#include <xcam_std.h>
#include <meta_data.h>
#include <buffer_pool.h>
#include <worker.h>

#define DECLARE_HANDLER_CALLBACK(CbClass, Next, mem_func)                \
    class CbClass : public ::XCam::ImageHandler::Callback {              \
        private: ::XCam::SmartPtr<Next>  _h;                             \
        public: CbClass (const ::XCam::SmartPtr<Next> &h) { _h = h;}     \
        protected: void execute_status (                                 \
            const ::XCam::SmartPtr<::XCam::ImageHandler> &handler,       \
            const ::XCam::SmartPtr<::XCam::ImageHandler::Parameters> &params,  \
            const XCamReturn error) {                                    \
            _h->mem_func (handler, params, error);  }                    \
    }

#define XCAM_DEFAULT_HANDLER_BUF_CAP 4

namespace XCam {

class ImageHandler;

class ImageHandler
    : public RefObj
{
public:
    struct Parameters {
        SmartPtr<VideoBuffer> in_buf;
        SmartPtr<VideoBuffer> out_buf;

        Parameters (const SmartPtr<VideoBuffer> &in = NULL, const SmartPtr<VideoBuffer> &out = NULL)
            : in_buf (in), out_buf (out)
        {}
        virtual ~Parameters() {}
        bool add_meta (const SmartPtr<MetaBase> &meta);
        template <typename MType> SmartPtr<MType> find_meta ();

    private:
        MetaBaseList       _metas;
    };

    class Callback {
    public:
        Callback () {}
        virtual ~Callback () {}
        virtual void execute_status (
            const SmartPtr<ImageHandler> &handler, const SmartPtr<Parameters> &params, const XCamReturn error) = 0;

    private:
        XCAM_DEAD_COPY (Callback);
    };

public:
    explicit ImageHandler (const char* name);
    virtual ~ImageHandler ();

    bool set_callback (SmartPtr<Callback> cb) {
        _callback = cb;
        return true;
    }
    const SmartPtr<Callback> & get_callback () const {
        return _callback;
    }
    const char *get_name () const {
        return _name;
    }
    bool set_out_video_info (const VideoBufferInfo &info);
    bool enable_allocator (bool enable, uint32_t buf_count = XCAM_DEFAULT_HANDLER_BUF_CAP);

    // virtual functions
    // execute_buffer params should  NOT be const
    virtual XCamReturn execute_buffer (const SmartPtr<Parameters> &params, bool sync);
    virtual void execute_done (const SmartPtr<ImageHandler::Parameters> &param, XCamReturn err);
    virtual XCamReturn finish ();
    virtual XCamReturn terminate ();

protected:
    virtual XCamReturn configure_resource (const SmartPtr<Parameters> &param) = 0;
    virtual SmartPtr<BufferPool> create_allocator () = 0;
    virtual XCamReturn configure_rest ();
    virtual XCamReturn start_work (const SmartPtr<Parameters> &param) = 0;

    virtual void execute_status_check (const SmartPtr<Parameters> &params, const XCamReturn error);

    bool set_allocator (const SmartPtr<BufferPool> &allocator);
    const SmartPtr<BufferPool> &get_allocator () const {
        return _allocator;
    }
    XCamReturn reserve_buffers (const VideoBufferInfo &info, uint32_t count);
    SmartPtr<VideoBuffer> get_free_buf ();

    const VideoBufferInfo &get_out_video_info () {
        return _out_video_info;
    }

private:
    XCAM_DEAD_COPY (ImageHandler);

protected:
    bool                    _need_configure;
    bool                    _enable_allocator;

private:
    SmartPtr<Callback>      _callback;
    VideoBufferInfo         _out_video_info;
    SmartPtr<BufferPool>    _allocator;
    uint32_t                _buf_capacity;
    char                   *_name;
};

inline bool
ImageHandler::Parameters::add_meta (const SmartPtr<MetaBase> &meta)
{
    if (!meta.ptr ())
        return false;

    _metas.push_back (meta);
    return true;
}

template <typename MType>
SmartPtr<MType>
ImageHandler::Parameters::find_meta ()
{
    for (MetaBaseList::iterator i = _metas.begin (); i != _metas.end (); ++i) {
        SmartPtr<MType> m = (*i).dynamic_cast_ptr<MType> ();
        if (m.ptr ())
            return m;
    }
    return NULL;
}

};

#endif //XCAM_IMAGE_HANDLER_H