1/*
2 * image_handler.h - image image handler class
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#ifndef XCAM_IMAGE_HANDLER_H
22#define XCAM_IMAGE_HANDLER_H
23
24#include <xcam_std.h>
25#include <meta_data.h>
26#include <buffer_pool.h>
27#include <worker.h>
28
29#define DECLARE_HANDLER_CALLBACK(CbClass, Next, mem_func)                \
30    class CbClass : public ::XCam::ImageHandler::Callback {              \
31        private: ::XCam::SmartPtr<Next>  _h;                             \
32        public: CbClass (const ::XCam::SmartPtr<Next> &h) { _h = h;}     \
33        protected: void execute_status (                                 \
34            const ::XCam::SmartPtr<::XCam::ImageHandler> &handler,       \
35            const ::XCam::SmartPtr<::XCam::ImageHandler::Parameters> &params,  \
36            const XCamReturn error) {                                    \
37            _h->mem_func (handler, params, error);  }                    \
38    }
39
40namespace XCam {
41
42class ImageHandler;
43
44class ImageHandler
45    : public RefObj
46{
47public:
48    struct Parameters {
49        SmartPtr<VideoBuffer> in_buf;
50        SmartPtr<VideoBuffer> out_buf;
51
52        Parameters (const SmartPtr<VideoBuffer> &in = NULL, const SmartPtr<VideoBuffer> &out = NULL)
53            : in_buf (in), out_buf (out)
54        {}
55        virtual ~Parameters() {}
56        bool add_meta (const SmartPtr<MetaBase> &meta);
57        template <typename MType> SmartPtr<MType> find_meta ();
58
59    private:
60        MetaBaseList       _metas;
61    };
62
63    class Callback {
64    public:
65        Callback () {}
66        virtual ~Callback () {}
67        virtual void execute_status (
68            const SmartPtr<ImageHandler> &handler, const SmartPtr<Parameters> &params, const XCamReturn error) = 0;
69
70    private:
71        XCAM_DEAD_COPY (Callback);
72    };
73
74public:
75    explicit ImageHandler (const char* name);
76    virtual ~ImageHandler ();
77
78    bool set_callback (SmartPtr<Callback> cb) {
79        _callback = cb;
80        return true;
81    }
82    const SmartPtr<Callback> & get_callback () const {
83        return _callback;
84    }
85    const char *get_name () const {
86        return _name;
87    }
88
89    // virtual functions
90    // execute_buffer params should  NOT be const
91    virtual XCamReturn execute_buffer (const SmartPtr<Parameters> &params, bool sync) = 0;
92    virtual XCamReturn finish ();
93    virtual XCamReturn terminate ();
94
95protected:
96    virtual void execute_status_check (const SmartPtr<Parameters> &params, const XCamReturn error);
97
98    bool set_allocator (const SmartPtr<BufferPool> &allocator);
99    const SmartPtr<BufferPool> &get_allocator () const {
100        return _allocator;
101    }
102    XCamReturn reserve_buffers (const VideoBufferInfo &info, uint32_t count);
103    SmartPtr<VideoBuffer> get_free_buf ();
104
105private:
106    XCAM_DEAD_COPY (ImageHandler);
107
108private:
109    SmartPtr<Callback>      _callback;
110    SmartPtr<BufferPool>    _allocator;
111    char                   *_name;
112};
113
114inline bool
115ImageHandler::Parameters::add_meta (const SmartPtr<MetaBase> &meta)
116{
117    if (!meta.ptr ())
118        return false;
119
120    _metas.push_back (meta);
121    return true;
122}
123
124template <typename MType>
125SmartPtr<MType>
126ImageHandler::Parameters::find_meta ()
127{
128    for (MetaBaseList::iterator i = _metas.begin (); i != _metas.end (); ++i) {
129        SmartPtr<MType> m = (*i).dynamic_cast_ptr<MType> ();
130        if (m.ptr ())
131            return m;
132    }
133    return NULL;
134}
135
136};
137
138#endif //XCAM_IMAGE_HANDLER_H
139