drm_framebuffer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/ctype.h>
  25. #include <linux/list.h>
  26. #include <linux/sched.h>
  27. #include <drm/drm_fourcc.h>
  28. #include <drm/drm_mode_object.h>
  29. struct drm_clip_rect;
  30. struct drm_device;
  31. struct drm_file;
  32. struct drm_framebuffer;
  33. struct drm_gem_object;
  34. /**
  35. * struct drm_framebuffer_funcs - framebuffer hooks
  36. */
  37. struct drm_framebuffer_funcs {
  38. /**
  39. * @destroy:
  40. *
  41. * Clean up framebuffer resources, specifically also unreference the
  42. * backing storage. The core guarantees to call this function for every
  43. * framebuffer successfully created by calling
  44. * &drm_mode_config_funcs.fb_create. Drivers must also call
  45. * drm_framebuffer_cleanup() to release DRM core resources for this
  46. * framebuffer.
  47. */
  48. void (*destroy)(struct drm_framebuffer *framebuffer);
  49. /**
  50. * @create_handle:
  51. *
  52. * Create a buffer handle in the driver-specific buffer manager (either
  53. * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
  54. * the core to implement the GETFB IOCTL, which returns (for
  55. * sufficiently priviledged user) also a native buffer handle. This can
  56. * be used for seamless transitions between modesetting clients by
  57. * copying the current screen contents to a private buffer and blending
  58. * between that and the new contents.
  59. *
  60. * GEM based drivers should call drm_gem_handle_create() to create the
  61. * handle.
  62. *
  63. * RETURNS:
  64. *
  65. * 0 on success or a negative error code on failure.
  66. */
  67. int (*create_handle)(struct drm_framebuffer *fb,
  68. struct drm_file *file_priv,
  69. unsigned int *handle);
  70. /**
  71. * @dirty:
  72. *
  73. * Optional callback for the dirty fb IOCTL.
  74. *
  75. * Userspace can notify the driver via this callback that an area of the
  76. * framebuffer has changed and should be flushed to the display
  77. * hardware. This can also be used internally, e.g. by the fbdev
  78. * emulation, though that's not the case currently.
  79. *
  80. * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
  81. * for more information as all the semantics and arguments have a one to
  82. * one mapping on this function.
  83. *
  84. * Atomic drivers should use drm_atomic_helper_dirtyfb() to implement
  85. * this hook.
  86. *
  87. * RETURNS:
  88. *
  89. * 0 on success or a negative error code on failure.
  90. */
  91. int (*dirty)(struct drm_framebuffer *framebuffer,
  92. struct drm_file *file_priv, unsigned flags,
  93. unsigned color, struct drm_clip_rect *clips,
  94. unsigned num_clips);
  95. };
  96. /**
  97. * struct drm_framebuffer - frame buffer object
  98. *
  99. * Note that the fb is refcounted for the benefit of driver internals,
  100. * for example some hw, disabling a CRTC/plane is asynchronous, and
  101. * scanout does not actually complete until the next vblank. So some
  102. * cleanup (like releasing the reference(s) on the backing GEM bo(s))
  103. * should be deferred. In cases like this, the driver would like to
  104. * hold a ref to the fb even though it has already been removed from
  105. * userspace perspective. See drm_framebuffer_get() and
  106. * drm_framebuffer_put().
  107. *
  108. * The refcount is stored inside the mode object @base.
  109. */
  110. struct drm_framebuffer {
  111. /**
  112. * @dev: DRM device this framebuffer belongs to
  113. */
  114. struct drm_device *dev;
  115. /**
  116. * @head: Place on the &drm_mode_config.fb_list, access protected by
  117. * &drm_mode_config.fb_lock.
  118. */
  119. struct list_head head;
  120. /**
  121. * @base: base modeset object structure, contains the reference count.
  122. */
  123. struct drm_mode_object base;
  124. /**
  125. * @comm: Name of the process allocating the fb, used for fb dumping.
  126. */
  127. char comm[TASK_COMM_LEN];
  128. /**
  129. * @format: framebuffer format information
  130. */
  131. const struct drm_format_info *format;
  132. /**
  133. * @funcs: framebuffer vfunc table
  134. */
  135. const struct drm_framebuffer_funcs *funcs;
  136. /**
  137. * @pitches: Line stride per buffer. For userspace created object this
  138. * is copied from drm_mode_fb_cmd2.
  139. */
  140. unsigned int pitches[DRM_FORMAT_MAX_PLANES];
  141. /**
  142. * @offsets: Offset from buffer start to the actual pixel data in bytes,
  143. * per buffer. For userspace created object this is copied from
  144. * drm_mode_fb_cmd2.
  145. *
  146. * Note that this is a linear offset and does not take into account
  147. * tiling or buffer layout per @modifier. It is meant to be used when
  148. * the actual pixel data for this framebuffer plane starts at an offset,
  149. * e.g. when multiple planes are allocated within the same backing
  150. * storage buffer object. For tiled layouts this generally means its
  151. * @offsets must at least be tile-size aligned, but hardware often has
  152. * stricter requirements.
  153. *
  154. * This should not be used to specifiy x/y pixel offsets into the buffer
  155. * data (even for linear buffers). Specifying an x/y pixel offset is
  156. * instead done through the source rectangle in &struct drm_plane_state.
  157. */
  158. unsigned int offsets[DRM_FORMAT_MAX_PLANES];
  159. /**
  160. * @modifier: Data layout modifier. This is used to describe
  161. * tiling, or also special layouts (like compression) of auxiliary
  162. * buffers. For userspace created object this is copied from
  163. * drm_mode_fb_cmd2.
  164. */
  165. uint64_t modifier;
  166. /**
  167. * @width: Logical width of the visible area of the framebuffer, in
  168. * pixels.
  169. */
  170. unsigned int width;
  171. /**
  172. * @height: Logical height of the visible area of the framebuffer, in
  173. * pixels.
  174. */
  175. unsigned int height;
  176. /**
  177. * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
  178. * DRM_MODE_FB_MODIFIERS.
  179. */
  180. int flags;
  181. /**
  182. * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
  183. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  184. * universal plane.
  185. */
  186. int hot_x;
  187. /**
  188. * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
  189. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  190. * universal plane.
  191. */
  192. int hot_y;
  193. /**
  194. * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
  195. */
  196. struct list_head filp_head;
  197. /**
  198. * @obj: GEM objects backing the framebuffer, one per plane (optional).
  199. *
  200. * This is used by the GEM framebuffer helpers, see e.g.
  201. * drm_gem_fb_create().
  202. */
  203. struct drm_gem_object *obj[DRM_FORMAT_MAX_PLANES];
  204. };
  205. #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
  206. int drm_framebuffer_init(struct drm_device *dev,
  207. struct drm_framebuffer *fb,
  208. const struct drm_framebuffer_funcs *funcs);
  209. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  210. struct drm_file *file_priv,
  211. uint32_t id);
  212. void drm_framebuffer_remove(struct drm_framebuffer *fb);
  213. void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
  214. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
  215. /**
  216. * drm_framebuffer_get - acquire a framebuffer reference
  217. * @fb: DRM framebuffer
  218. *
  219. * This function increments the framebuffer's reference count.
  220. */
  221. static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
  222. {
  223. drm_mode_object_get(&fb->base);
  224. }
  225. /**
  226. * drm_framebuffer_put - release a framebuffer reference
  227. * @fb: DRM framebuffer
  228. *
  229. * This function decrements the framebuffer's reference count and frees the
  230. * framebuffer if the reference count drops to zero.
  231. */
  232. static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
  233. {
  234. drm_mode_object_put(&fb->base);
  235. }
  236. /**
  237. * drm_framebuffer_read_refcount - read the framebuffer reference count.
  238. * @fb: framebuffer
  239. *
  240. * This functions returns the framebuffer's reference count.
  241. */
  242. static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb)
  243. {
  244. return kref_read(&fb->base.refcount);
  245. }
  246. /**
  247. * drm_framebuffer_assign - store a reference to the fb
  248. * @p: location to store framebuffer
  249. * @fb: new framebuffer (maybe NULL)
  250. *
  251. * This functions sets the location to store a reference to the framebuffer,
  252. * unreferencing the framebuffer that was previously stored in that location.
  253. */
  254. static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
  255. struct drm_framebuffer *fb)
  256. {
  257. if (fb)
  258. drm_framebuffer_get(fb);
  259. if (*p)
  260. drm_framebuffer_put(*p);
  261. *p = fb;
  262. }
  263. /*
  264. * drm_for_each_fb - iterate over all framebuffers
  265. * @fb: the loop cursor
  266. * @dev: the DRM device
  267. *
  268. * Iterate over all framebuffers of @dev. User must hold
  269. * &drm_mode_config.fb_lock.
  270. */
  271. #define drm_for_each_fb(fb, dev) \
  272. for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
  273. fb = list_first_entry(&(dev)->mode_config.fb_list, \
  274. struct drm_framebuffer, head); \
  275. &fb->head != (&(dev)->mode_config.fb_list); \
  276. fb = list_next_entry(fb, head))
  277. int drm_framebuffer_plane_width(int width,
  278. const struct drm_framebuffer *fb, int plane);
  279. int drm_framebuffer_plane_height(int height,
  280. const struct drm_framebuffer *fb, int plane);
  281. /**
  282. * struct drm_afbc_framebuffer - a special afbc frame buffer object
  283. *
  284. * A derived class of struct drm_framebuffer, dedicated for afbc use cases.
  285. */
  286. struct drm_afbc_framebuffer {
  287. /**
  288. * @base: base framebuffer structure.
  289. */
  290. struct drm_framebuffer base;
  291. /**
  292. * @block_width: width of a single afbc block
  293. */
  294. u32 block_width;
  295. /**
  296. * @block_height: height of a single afbc block
  297. */
  298. u32 block_height;
  299. /**
  300. * @aligned_width: aligned frame buffer width
  301. */
  302. u32 aligned_width;
  303. /**
  304. * @aligned_height: aligned frame buffer height
  305. */
  306. u32 aligned_height;
  307. /**
  308. * @offset: offset of the first afbc header
  309. */
  310. u32 offset;
  311. /**
  312. * @afbc_size: minimum size of afbc buffer
  313. */
  314. u32 afbc_size;
  315. };
  316. #define fb_to_afbc_fb(x) container_of(x, struct drm_afbc_framebuffer, base)
  317. #endif