Home | History | Annotate | Download | only in xcore
      1 /*
      2  * drm_display.h - drm display
      3  *
      4  *  Copyright (c) 2015 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: John Ye <john.ye (at) intel.com>
     19  */
     20 
     21 #ifndef XCAM_DRM_DISPLAY_H
     22 #define XCAM_DRM_DISPLAY_H
     23 
     24 #include <xcam_std.h>
     25 #include <xcam_mutex.h>
     26 #include <v4l2_buffer_proxy.h>
     27 
     28 extern "C" {
     29 #include <drm.h>
     30 #include <drm_mode.h>
     31 #include <intel_bufmgr.h>
     32 #include <linux/videodev2.h>
     33 }
     34 
     35 #include <errno.h>
     36 #include <unistd.h>
     37 #include <xf86drm.h>
     38 #include <xf86drmMode.h>
     39 
     40 #include <list>
     41 #include <vector>
     42 #include <map>
     43 
     44 namespace XCam {
     45 
     46 class DrmBoData;
     47 class DrmBoBufferPool;
     48 class DrmBoBuffer;
     49 
     50 enum DrmDisplayMode {
     51     DRM_DISPLAY_MODE_NONE = 0,
     52     DRM_DISPLAY_MODE_PRIMARY,
     53     DRM_DISPLAY_MODE_OVERLAY,
     54 };
     55 
     56 class DrmDisplay {
     57     friend class DrmBoBufferPool;
     58     friend class CLBoBufferPool;
     59 
     60     struct FB {
     61         uint32_t fb_handle;
     62         uint32_t index;
     63 
     64         FB () : fb_handle (0), index (0) {}
     65     };
     66 
     67 public:
     68     // if need local preview, please call set_preview() before instance()
     69     static SmartPtr<DrmDisplay> instance ();
     70     static uint32_t to_drm_fourcc (uint32_t fourcc_of_v4l2);
     71 
     72     virtual ~DrmDisplay();
     73     const char *get_module_name () const {
     74         return _module;
     75     }
     76 
     77     bool is_render_inited () const {
     78         return _is_render_inited;
     79     }
     80     XCamReturn render_init (
     81         uint32_t con_id,
     82         uint32_t crtc_id,
     83         uint32_t width,
     84         uint32_t height,
     85         uint32_t format,
     86         const struct v4l2_rect* compose);
     87 
     88     bool has_frame_buffer (SmartPtr<VideoBuffer> &buf) {
     89         return _buf_fb_handles.find (buf.ptr ()) != _buf_fb_handles.end ();
     90     };
     91     XCamReturn render_setup_frame_buffer (SmartPtr<VideoBuffer> &buf);
     92     XCamReturn render_buffer (SmartPtr<VideoBuffer> &buf);
     93 
     94     int get_drm_handle() const {
     95         return _fd;
     96     };
     97 
     98     SmartPtr<V4l2Buffer> create_drm_buf (
     99         const struct v4l2_format &format,
    100         const uint32_t index,
    101         const enum v4l2_buf_type buf_type);
    102     SmartPtr<DrmBoBuffer> convert_to_drm_bo_buf (SmartPtr<DrmDisplay> &self, SmartPtr<VideoBuffer> &buf_in);
    103 
    104     static bool set_preview (bool flag);
    105     bool can_preview () {
    106         return _preview_flag;
    107     }
    108     bool set_display_mode (DrmDisplayMode mode) {
    109         _display_mode = mode;
    110         return true;
    111     };
    112 
    113 private:
    114     DrmDisplay (const char *module = NULL);
    115 
    116     SmartPtr<DrmBoData> create_drm_bo (SmartPtr<DrmDisplay> &self, const VideoBufferInfo& info);
    117     drm_intel_bo *create_drm_bo_from_fd (int32_t fd, uint32_t size);
    118 
    119     bool is_authenticated (int fd, const char *msg);
    120     int open_driver (const char *dev_path);
    121     int open_drivers (const char *base_path, int base_id);
    122 
    123     XCamReturn get_crtc(drmModeRes *res);
    124     XCamReturn get_connector(drmModeRes *res);
    125     XCamReturn get_plane();
    126     XCamReturn set_plane(const FB &fb);
    127     XCamReturn set_crtc(const FB &fb);
    128     XCamReturn page_flip(const FB &fb);
    129 
    130 private:
    131     typedef std::map<const VideoBuffer *, FB> FBMap;
    132 
    133     static bool    _preview_flag;
    134 
    135     char *_module;
    136     int _fd;
    137     drm_intel_bufmgr *_buf_manager;
    138     DrmDisplayMode _display_mode;
    139     int _crtc_index;
    140     unsigned int _crtc_id;
    141     unsigned int _con_id;
    142     unsigned int _encoder_id;
    143     unsigned int _plane_id;
    144     drmModeModeInfo _mode;
    145     drmModeConnector *_connector;
    146     bool _is_render_inited;
    147 
    148     unsigned int _format;
    149     unsigned int _width;
    150     unsigned int _height;
    151 
    152     struct v4l2_rect _compose;
    153 
    154     FBMap _buf_fb_handles;
    155     SmartPtr<VideoBuffer>  _display_buf;
    156 
    157 private:
    158     XCAM_DEAD_COPY (DrmDisplay);
    159 
    160 private:
    161     static SmartPtr<DrmDisplay> _instance;
    162     static Mutex                _mutex;
    163 };
    164 
    165 };
    166 #endif // XCAM_DRM_DISPLAY_H
    167 
    168