drm_framebuffer.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #ifndef __DRM_FRAMEBUFFER_H__
  23. #define __DRM_FRAMEBUFFER_H__
  24. #include <linux/list.h>
  25. #include <linux/ctype.h>
  26. #include <drm/drm_mode_object.h>
  27. struct drm_framebuffer;
  28. struct drm_file;
  29. struct drm_device;
  30. /**
  31. * struct drm_framebuffer_funcs - framebuffer hooks
  32. */
  33. struct drm_framebuffer_funcs {
  34. /**
  35. * @destroy:
  36. *
  37. * Clean up framebuffer resources, specifically also unreference the
  38. * backing storage. The core guarantees to call this function for every
  39. * framebuffer successfully created by calling
  40. * &drm_mode_config_funcs.fb_create. Drivers must also call
  41. * drm_framebuffer_cleanup() to release DRM core resources for this
  42. * framebuffer.
  43. */
  44. void (*destroy)(struct drm_framebuffer *framebuffer);
  45. /**
  46. * @create_handle:
  47. *
  48. * Create a buffer handle in the driver-specific buffer manager (either
  49. * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
  50. * the core to implement the GETFB IOCTL, which returns (for
  51. * sufficiently priviledged user) also a native buffer handle. This can
  52. * be used for seamless transitions between modesetting clients by
  53. * copying the current screen contents to a private buffer and blending
  54. * between that and the new contents.
  55. *
  56. * GEM based drivers should call drm_gem_handle_create() to create the
  57. * handle.
  58. *
  59. * RETURNS:
  60. *
  61. * 0 on success or a negative error code on failure.
  62. */
  63. int (*create_handle)(struct drm_framebuffer *fb,
  64. struct drm_file *file_priv,
  65. unsigned int *handle);
  66. /**
  67. * @dirty:
  68. *
  69. * Optional callback for the dirty fb IOCTL.
  70. *
  71. * Userspace can notify the driver via this callback that an area of the
  72. * framebuffer has changed and should be flushed to the display
  73. * hardware. This can also be used internally, e.g. by the fbdev
  74. * emulation, though that's not the case currently.
  75. *
  76. * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
  77. * for more information as all the semantics and arguments have a one to
  78. * one mapping on this function.
  79. *
  80. * RETURNS:
  81. *
  82. * 0 on success or a negative error code on failure.
  83. */
  84. int (*dirty)(struct drm_framebuffer *framebuffer,
  85. struct drm_file *file_priv, unsigned flags,
  86. unsigned color, struct drm_clip_rect *clips,
  87. unsigned num_clips);
  88. };
  89. /**
  90. * struct drm_framebuffer - frame buffer object
  91. *
  92. * Note that the fb is refcounted for the benefit of driver internals,
  93. * for example some hw, disabling a CRTC/plane is asynchronous, and
  94. * scanout does not actually complete until the next vblank. So some
  95. * cleanup (like releasing the reference(s) on the backing GEM bo(s))
  96. * should be deferred. In cases like this, the driver would like to
  97. * hold a ref to the fb even though it has already been removed from
  98. * userspace perspective. See drm_framebuffer_get() and
  99. * drm_framebuffer_put().
  100. *
  101. * The refcount is stored inside the mode object @base.
  102. */
  103. struct drm_framebuffer {
  104. /**
  105. * @dev: DRM device this framebuffer belongs to
  106. */
  107. struct drm_device *dev;
  108. /**
  109. * @head: Place on the &drm_mode_config.fb_list, access protected by
  110. * &drm_mode_config.fb_lock.
  111. */
  112. struct list_head head;
  113. /**
  114. * @base: base modeset object structure, contains the reference count.
  115. */
  116. struct drm_mode_object base;
  117. /**
  118. * @format: framebuffer format information
  119. */
  120. const struct drm_format_info *format;
  121. /**
  122. * @funcs: framebuffer vfunc table
  123. */
  124. const struct drm_framebuffer_funcs *funcs;
  125. /**
  126. * @pitches: Line stride per buffer. For userspace created object this
  127. * is copied from drm_mode_fb_cmd2.
  128. */
  129. unsigned int pitches[4];
  130. /**
  131. * @offsets: Offset from buffer start to the actual pixel data in bytes,
  132. * per buffer. For userspace created object this is copied from
  133. * drm_mode_fb_cmd2.
  134. *
  135. * Note that this is a linear offset and does not take into account
  136. * tiling or buffer laytou per @modifier. It meant to be used when the
  137. * actual pixel data for this framebuffer plane starts at an offset,
  138. * e.g. when multiple planes are allocated within the same backing
  139. * storage buffer object. For tiled layouts this generally means it
  140. * @offsets must at least be tile-size aligned, but hardware often has
  141. * stricter requirements.
  142. *
  143. * This should not be used to specifiy x/y pixel offsets into the buffer
  144. * data (even for linear buffers). Specifying an x/y pixel offset is
  145. * instead done through the source rectangle in &struct drm_plane_state.
  146. */
  147. unsigned int offsets[4];
  148. /**
  149. * @modifier: Data layout modifier. This is used to describe
  150. * tiling, or also special layouts (like compression) of auxiliary
  151. * buffers. For userspace created object this is copied from
  152. * drm_mode_fb_cmd2.
  153. */
  154. uint64_t modifier[4];
  155. /**
  156. * @width: Logical width of the visible area of the framebuffer, in
  157. * pixels.
  158. */
  159. unsigned int width;
  160. /**
  161. * @height: Logical height of the visible area of the framebuffer, in
  162. * pixels.
  163. */
  164. unsigned int height;
  165. /**
  166. * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
  167. * DRM_MODE_FB_MODIFIERS.
  168. */
  169. int flags;
  170. /**
  171. * @pixel_format: DRM FOURCC code describing the pixel format.
  172. */
  173. uint32_t pixel_format; /* fourcc format */
  174. /**
  175. * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
  176. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  177. * universal plane.
  178. */
  179. int hot_x;
  180. /**
  181. * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
  182. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  183. * universal plane.
  184. */
  185. int hot_y;
  186. /**
  187. * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
  188. */
  189. struct list_head filp_head;
  190. /**
  191. * @obj: GEM objects backing the framebuffer, one per plane (optional).
  192. *
  193. * This is used by the GEM framebuffer helpers, see e.g.
  194. * drm_gem_fb_create().
  195. */
  196. struct drm_gem_object *obj[4];
  197. };
  198. #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
  199. int drm_framebuffer_init(struct drm_device *dev,
  200. struct drm_framebuffer *fb,
  201. const struct drm_framebuffer_funcs *funcs);
  202. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  203. struct drm_file *file_priv,
  204. uint32_t id);
  205. void drm_framebuffer_remove(struct drm_framebuffer *fb);
  206. void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
  207. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
  208. /**
  209. * drm_framebuffer_get - acquire a framebuffer reference
  210. * @fb: DRM framebuffer
  211. *
  212. * This function increments the framebuffer's reference count.
  213. */
  214. static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
  215. {
  216. drm_mode_object_get(&fb->base);
  217. }
  218. /**
  219. * drm_framebuffer_put - release a framebuffer reference
  220. * @fb: DRM framebuffer
  221. *
  222. * This function decrements the framebuffer's reference count and frees the
  223. * framebuffer if the reference count drops to zero.
  224. */
  225. static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
  226. {
  227. drm_mode_object_put(&fb->base);
  228. }
  229. /**
  230. * drm_framebuffer_reference - acquire a framebuffer reference
  231. * @fb: DRM framebuffer
  232. *
  233. * This is a compatibility alias for drm_framebuffer_get() and should not be
  234. * used by new code.
  235. */
  236. static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
  237. {
  238. drm_framebuffer_get(fb);
  239. }
  240. /**
  241. * drm_framebuffer_unreference - release a framebuffer reference
  242. * @fb: DRM framebuffer
  243. *
  244. * This is a compatibility alias for drm_framebuffer_put() and should not be
  245. * used by new code.
  246. */
  247. static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  248. {
  249. drm_framebuffer_put(fb);
  250. }
  251. /**
  252. * drm_framebuffer_read_refcount - read the framebuffer reference count.
  253. * @fb: framebuffer
  254. *
  255. * This functions returns the framebuffer's reference count.
  256. */
  257. static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
  258. {
  259. return kref_read(&fb->base.refcount);
  260. }
  261. /**
  262. * drm_framebuffer_assign - store a reference to the fb
  263. * @p: location to store framebuffer
  264. * @fb: new framebuffer (maybe NULL)
  265. *
  266. * This functions sets the location to store a reference to the framebuffer,
  267. * unreferencing the framebuffer that was previously stored in that location.
  268. */
  269. static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
  270. struct drm_framebuffer *fb)
  271. {
  272. if (fb)
  273. drm_framebuffer_get(fb);
  274. if (*p)
  275. drm_framebuffer_put(*p);
  276. *p = fb;
  277. }
  278. /*
  279. * drm_for_each_fb - iterate over all framebuffers
  280. * @fb: the loop cursor
  281. * @dev: the DRM device
  282. *
  283. * Iterate over all framebuffers of @dev. User must hold
  284. * &drm_mode_config.fb_lock.
  285. */
  286. #define drm_for_each_fb(fb, dev) \
  287. for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
  288. fb = list_first_entry(&(dev)->mode_config.fb_list, \
  289. struct drm_framebuffer, head); \
  290. &fb->head != (&(dev)->mode_config.fb_list); \
  291. fb = list_next_entry(fb, head))
  292. int drm_framebuffer_plane_width(int width,
  293. const struct drm_framebuffer *fb, int plane);
  294. int drm_framebuffer_plane_height(int height,
  295. const struct drm_framebuffer *fb, int plane);
  296. #endif