drm_framebuffer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 ->fb_create() in
  40. * &drm_mode_config_funcs. 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_reference() and
  99. * drm_framebuffer_unreference().
  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 dev->mode_config.fb_list, access protected by
  110. * dev->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. * @funcs: framebuffer vfunc table
  119. */
  120. const struct drm_framebuffer_funcs *funcs;
  121. /**
  122. * @pitches: Line stride per buffer. For userspace created object this
  123. * is copied from drm_mode_fb_cmd2.
  124. */
  125. unsigned int pitches[4];
  126. /**
  127. * @offsets: Offset from buffer start to the actual pixel data in bytes,
  128. * per buffer. For userspace created object this is copied from
  129. * drm_mode_fb_cmd2.
  130. *
  131. * Note that this is a linear offset and does not take into account
  132. * tiling or buffer laytou per @modifier. It meant to be used when the
  133. * actual pixel data for this framebuffer plane starts at an offset,
  134. * e.g. when multiple planes are allocated within the same backing
  135. * storage buffer object. For tiled layouts this generally means it
  136. * @offsets must at least be tile-size aligned, but hardware often has
  137. * stricter requirements.
  138. *
  139. * This should not be used to specifiy x/y pixel offsets into the buffer
  140. * data (even for linear buffers). Specifying an x/y pixel offset is
  141. * instead done through the source rectangle in struct &drm_plane_state.
  142. */
  143. unsigned int offsets[4];
  144. /**
  145. * @modifier: Data layout modifier, per buffer. This is used to describe
  146. * tiling, or also special layouts (like compression) of auxiliary
  147. * buffers. For userspace created object this is copied from
  148. * drm_mode_fb_cmd2.
  149. */
  150. uint64_t modifier[4];
  151. /**
  152. * @width: Logical width of the visible area of the framebuffer, in
  153. * pixels.
  154. */
  155. unsigned int width;
  156. /**
  157. * @height: Logical height of the visible area of the framebuffer, in
  158. * pixels.
  159. */
  160. unsigned int height;
  161. /**
  162. * @depth: Depth in bits per pixel for RGB formats. 0 for everything
  163. * else. Legacy information derived from @pixel_format, it's suggested to use
  164. * the DRM FOURCC codes and helper functions directly instead.
  165. */
  166. unsigned int depth;
  167. /**
  168. * @bits_per_pixel: Storage used bits per pixel for RGB formats. 0 for
  169. * everything else. Legacy information derived from @pixel_format, it's
  170. * suggested to use the DRM FOURCC codes and helper functions directly
  171. * instead.
  172. */
  173. int bits_per_pixel;
  174. /**
  175. * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
  176. * DRM_MODE_FB_MODIFIERS.
  177. */
  178. int flags;
  179. /**
  180. * @pixel_format: DRM FOURCC code describing the pixel format.
  181. */
  182. uint32_t pixel_format; /* fourcc format */
  183. /**
  184. * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
  185. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  186. * universal plane.
  187. */
  188. int hot_x;
  189. /**
  190. * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
  191. * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
  192. * universal plane.
  193. */
  194. int hot_y;
  195. /**
  196. * @filp_head: Placed on struct &drm_file fbs list_head, protected by
  197. * fbs_lock in the same structure.
  198. */
  199. struct list_head filp_head;
  200. };
  201. #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
  202. int drm_framebuffer_init(struct drm_device *dev,
  203. struct drm_framebuffer *fb,
  204. const struct drm_framebuffer_funcs *funcs);
  205. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  206. uint32_t id);
  207. void drm_framebuffer_remove(struct drm_framebuffer *fb);
  208. void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
  209. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
  210. /**
  211. * drm_framebuffer_reference - incr the fb refcnt
  212. * @fb: framebuffer
  213. *
  214. * This functions increments the fb's refcount.
  215. */
  216. static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
  217. {
  218. drm_mode_object_reference(&fb->base);
  219. }
  220. /**
  221. * drm_framebuffer_unreference - unref a framebuffer
  222. * @fb: framebuffer to unref
  223. *
  224. * This functions decrements the fb's refcount and frees it if it drops to zero.
  225. */
  226. static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  227. {
  228. drm_mode_object_unreference(&fb->base);
  229. }
  230. /**
  231. * drm_framebuffer_read_refcount - read the framebuffer reference count.
  232. * @fb: framebuffer
  233. *
  234. * This functions returns the framebuffer's reference count.
  235. */
  236. static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
  237. {
  238. return atomic_read(&fb->base.refcount.refcount);
  239. }
  240. /**
  241. * drm_for_each_fb - iterate over all framebuffers
  242. * @fb: the loop cursor
  243. * @dev: the DRM device
  244. *
  245. * Iterate over all framebuffers of @dev. User must hold the fb_lock from
  246. * &drm_mode_config.
  247. */
  248. #define drm_for_each_fb(fb, dev) \
  249. for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
  250. fb = list_first_entry(&(dev)->mode_config.fb_list, \
  251. struct drm_framebuffer, head); \
  252. &fb->head != (&(dev)->mode_config.fb_list); \
  253. fb = list_next_entry(fb, head))
  254. #endif