drm_plane.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_PLANE_H__
  23. #define __DRM_PLANE_H__
  24. #include <linux/list.h>
  25. #include <linux/ctype.h>
  26. #include <drm/drm_mode_object.h>
  27. #include <drm/drm_color_mgmt.h>
  28. struct drm_crtc;
  29. /**
  30. * struct drm_plane_state - mutable plane state
  31. * @plane: backpointer to the plane
  32. * @crtc: currently bound CRTC, NULL if disabled
  33. * @fb: currently bound framebuffer
  34. * @fence: optional fence to wait for before scanning out @fb
  35. * @crtc_x: left position of visible portion of plane on crtc
  36. * @crtc_y: upper position of visible portion of plane on crtc
  37. * @crtc_w: width of visible portion of plane on crtc
  38. * @crtc_h: height of visible portion of plane on crtc
  39. * @src_x: left position of visible portion of plane within
  40. * plane (in 16.16)
  41. * @src_y: upper position of visible portion of plane within
  42. * plane (in 16.16)
  43. * @src_w: width of visible portion of plane (in 16.16)
  44. * @src_h: height of visible portion of plane (in 16.16)
  45. * @rotation: rotation of the plane
  46. * @zpos: priority of the given plane on crtc (optional)
  47. * Note that multiple active planes on the same crtc can have an identical
  48. * zpos value. The rule to solving the conflict is to compare the plane
  49. * object IDs; the plane with a higher ID must be stacked on top of a
  50. * plane with a lower ID.
  51. * @normalized_zpos: normalized value of zpos: unique, range from 0 to N-1
  52. * where N is the number of active planes for given crtc. Note that
  53. * the driver must call drm_atomic_normalize_zpos() to update this before
  54. * it can be trusted.
  55. * @src: clipped source coordinates of the plane (in 16.16)
  56. * @dst: clipped destination coordinates of the plane
  57. * @visible: visibility of the plane
  58. * @state: backpointer to global drm_atomic_state
  59. */
  60. struct drm_plane_state {
  61. struct drm_plane *plane;
  62. struct drm_crtc *crtc; /* do not write directly, use drm_atomic_set_crtc_for_plane() */
  63. struct drm_framebuffer *fb; /* do not write directly, use drm_atomic_set_fb_for_plane() */
  64. struct fence *fence;
  65. /* Signed dest location allows it to be partially off screen */
  66. int32_t crtc_x, crtc_y;
  67. uint32_t crtc_w, crtc_h;
  68. /* Source values are 16.16 fixed point */
  69. uint32_t src_x, src_y;
  70. uint32_t src_h, src_w;
  71. /* Plane rotation */
  72. unsigned int rotation;
  73. /* Plane zpos */
  74. unsigned int zpos;
  75. unsigned int normalized_zpos;
  76. /* Color encoding for non RGB formats */
  77. enum drm_color_encoding color_encoding;
  78. enum drm_color_range color_range;
  79. /* Clipped coordinates */
  80. struct drm_rect src, dst;
  81. /*
  82. * Is the plane actually visible? Can be false even
  83. * if fb!=NULL and crtc!=NULL, due to clipping.
  84. */
  85. bool visible;
  86. struct drm_atomic_state *state;
  87. };
  88. /**
  89. * struct drm_plane_funcs - driver plane control functions
  90. */
  91. struct drm_plane_funcs {
  92. /**
  93. * @update_plane:
  94. *
  95. * This is the legacy entry point to enable and configure the plane for
  96. * the given CRTC and framebuffer. It is never called to disable the
  97. * plane, i.e. the passed-in crtc and fb paramters are never NULL.
  98. *
  99. * The source rectangle in frame buffer memory coordinates is given by
  100. * the src_x, src_y, src_w and src_h parameters (as 16.16 fixed point
  101. * values). Devices that don't support subpixel plane coordinates can
  102. * ignore the fractional part.
  103. *
  104. * The destination rectangle in CRTC coordinates is given by the
  105. * crtc_x, crtc_y, crtc_w and crtc_h parameters (as integer values).
  106. * Devices scale the source rectangle to the destination rectangle. If
  107. * scaling is not supported, and the source rectangle size doesn't match
  108. * the destination rectangle size, the driver must return a
  109. * -<errorname>EINVAL</errorname> error.
  110. *
  111. * Drivers implementing atomic modeset should use
  112. * drm_atomic_helper_update_plane() to implement this hook.
  113. *
  114. * RETURNS:
  115. *
  116. * 0 on success or a negative error code on failure.
  117. */
  118. int (*update_plane)(struct drm_plane *plane,
  119. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  120. int crtc_x, int crtc_y,
  121. unsigned int crtc_w, unsigned int crtc_h,
  122. uint32_t src_x, uint32_t src_y,
  123. uint32_t src_w, uint32_t src_h);
  124. /**
  125. * @disable_plane:
  126. *
  127. * This is the legacy entry point to disable the plane. The DRM core
  128. * calls this method in response to a DRM_IOCTL_MODE_SETPLANE IOCTL call
  129. * with the frame buffer ID set to 0. Disabled planes must not be
  130. * processed by the CRTC.
  131. *
  132. * Drivers implementing atomic modeset should use
  133. * drm_atomic_helper_disable_plane() to implement this hook.
  134. *
  135. * RETURNS:
  136. *
  137. * 0 on success or a negative error code on failure.
  138. */
  139. int (*disable_plane)(struct drm_plane *plane);
  140. /**
  141. * @destroy:
  142. *
  143. * Clean up plane resources. This is only called at driver unload time
  144. * through drm_mode_config_cleanup() since a plane cannot be hotplugged
  145. * in DRM.
  146. */
  147. void (*destroy)(struct drm_plane *plane);
  148. /**
  149. * @reset:
  150. *
  151. * Reset plane hardware and software state to off. This function isn't
  152. * called by the core directly, only through drm_mode_config_reset().
  153. * It's not a helper hook only for historical reasons.
  154. *
  155. * Atomic drivers can use drm_atomic_helper_plane_reset() to reset
  156. * atomic state using this hook.
  157. */
  158. void (*reset)(struct drm_plane *plane);
  159. /**
  160. * @set_property:
  161. *
  162. * This is the legacy entry point to update a property attached to the
  163. * plane.
  164. *
  165. * Drivers implementing atomic modeset should use
  166. * drm_atomic_helper_plane_set_property() to implement this hook.
  167. *
  168. * This callback is optional if the driver does not support any legacy
  169. * driver-private properties.
  170. *
  171. * RETURNS:
  172. *
  173. * 0 on success or a negative error code on failure.
  174. */
  175. int (*set_property)(struct drm_plane *plane,
  176. struct drm_property *property, uint64_t val);
  177. /**
  178. * @atomic_duplicate_state:
  179. *
  180. * Duplicate the current atomic state for this plane and return it.
  181. * The core and helpers gurantee that any atomic state duplicated with
  182. * this hook and still owned by the caller (i.e. not transferred to the
  183. * driver by calling ->atomic_commit() from struct
  184. * &drm_mode_config_funcs) will be cleaned up by calling the
  185. * @atomic_destroy_state hook in this structure.
  186. *
  187. * Atomic drivers which don't subclass struct &drm_plane_state should use
  188. * drm_atomic_helper_plane_duplicate_state(). Drivers that subclass the
  189. * state structure to extend it with driver-private state should use
  190. * __drm_atomic_helper_plane_duplicate_state() to make sure shared state is
  191. * duplicated in a consistent fashion across drivers.
  192. *
  193. * It is an error to call this hook before plane->state has been
  194. * initialized correctly.
  195. *
  196. * NOTE:
  197. *
  198. * If the duplicate state references refcounted resources this hook must
  199. * acquire a reference for each of them. The driver must release these
  200. * references again in @atomic_destroy_state.
  201. *
  202. * RETURNS:
  203. *
  204. * Duplicated atomic state or NULL when the allocation failed.
  205. */
  206. struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
  207. /**
  208. * @atomic_destroy_state:
  209. *
  210. * Destroy a state duplicated with @atomic_duplicate_state and release
  211. * or unreference all resources it references
  212. */
  213. void (*atomic_destroy_state)(struct drm_plane *plane,
  214. struct drm_plane_state *state);
  215. /**
  216. * @atomic_set_property:
  217. *
  218. * Decode a driver-private property value and store the decoded value
  219. * into the passed-in state structure. Since the atomic core decodes all
  220. * standardized properties (even for extensions beyond the core set of
  221. * properties which might not be implemented by all drivers) this
  222. * requires drivers to subclass the state structure.
  223. *
  224. * Such driver-private properties should really only be implemented for
  225. * truly hardware/vendor specific state. Instead it is preferred to
  226. * standardize atomic extension and decode the properties used to expose
  227. * such an extension in the core.
  228. *
  229. * Do not call this function directly, use
  230. * drm_atomic_plane_set_property() instead.
  231. *
  232. * This callback is optional if the driver does not support any
  233. * driver-private atomic properties.
  234. *
  235. * NOTE:
  236. *
  237. * This function is called in the state assembly phase of atomic
  238. * modesets, which can be aborted for any reason (including on
  239. * userspace's request to just check whether a configuration would be
  240. * possible). Drivers MUST NOT touch any persistent state (hardware or
  241. * software) or data structures except the passed in @state parameter.
  242. *
  243. * Also since userspace controls in which order properties are set this
  244. * function must not do any input validation (since the state update is
  245. * incomplete and hence likely inconsistent). Instead any such input
  246. * validation must be done in the various atomic_check callbacks.
  247. *
  248. * RETURNS:
  249. *
  250. * 0 if the property has been found, -EINVAL if the property isn't
  251. * implemented by the driver (which shouldn't ever happen, the core only
  252. * asks for properties attached to this plane). No other validation is
  253. * allowed by the driver. The core already checks that the property
  254. * value is within the range (integer, valid enum value, ...) the driver
  255. * set when registering the property.
  256. */
  257. int (*atomic_set_property)(struct drm_plane *plane,
  258. struct drm_plane_state *state,
  259. struct drm_property *property,
  260. uint64_t val);
  261. /**
  262. * @atomic_get_property:
  263. *
  264. * Reads out the decoded driver-private property. This is used to
  265. * implement the GETPLANE IOCTL.
  266. *
  267. * Do not call this function directly, use
  268. * drm_atomic_plane_get_property() instead.
  269. *
  270. * This callback is optional if the driver does not support any
  271. * driver-private atomic properties.
  272. *
  273. * RETURNS:
  274. *
  275. * 0 on success, -EINVAL if the property isn't implemented by the
  276. * driver (which should never happen, the core only asks for
  277. * properties attached to this plane).
  278. */
  279. int (*atomic_get_property)(struct drm_plane *plane,
  280. const struct drm_plane_state *state,
  281. struct drm_property *property,
  282. uint64_t *val);
  283. /**
  284. * @late_register:
  285. *
  286. * This optional hook can be used to register additional userspace
  287. * interfaces attached to the plane like debugfs interfaces.
  288. * It is called late in the driver load sequence from drm_dev_register().
  289. * Everything added from this callback should be unregistered in
  290. * the early_unregister callback.
  291. *
  292. * Returns:
  293. *
  294. * 0 on success, or a negative error code on failure.
  295. */
  296. int (*late_register)(struct drm_plane *plane);
  297. /**
  298. * @early_unregister:
  299. *
  300. * This optional hook should be used to unregister the additional
  301. * userspace interfaces attached to the plane from
  302. * late_unregister(). It is called from drm_dev_unregister(),
  303. * early in the driver unload sequence to disable userspace access
  304. * before data structures are torndown.
  305. */
  306. void (*early_unregister)(struct drm_plane *plane);
  307. };
  308. /**
  309. * enum drm_plane_type - uapi plane type enumeration
  310. *
  311. * For historical reasons not all planes are made the same. This enumeration is
  312. * used to tell the different types of planes apart to implement the different
  313. * uapi semantics for them. For userspace which is universal plane aware and
  314. * which is using that atomic IOCTL there's no difference between these planes
  315. * (beyong what the driver and hardware can support of course).
  316. *
  317. * For compatibility with legacy userspace, only overlay planes are made
  318. * available to userspace by default. Userspace clients may set the
  319. * DRM_CLIENT_CAP_UNIVERSAL_PLANES client capability bit to indicate that they
  320. * wish to receive a universal plane list containing all plane types. See also
  321. * drm_for_each_legacy_plane().
  322. *
  323. * WARNING: The values of this enum is UABI since they're exposed in the "type"
  324. * property.
  325. */
  326. enum drm_plane_type {
  327. /**
  328. * @DRM_PLANE_TYPE_OVERLAY:
  329. *
  330. * Overlay planes represent all non-primary, non-cursor planes. Some
  331. * drivers refer to these types of planes as "sprites" internally.
  332. */
  333. DRM_PLANE_TYPE_OVERLAY,
  334. /**
  335. * @DRM_PLANE_TYPE_PRIMARY:
  336. *
  337. * Primary planes represent a "main" plane for a CRTC. Primary planes
  338. * are the planes operated upon by CRTC modesetting and flipping
  339. * operations described in the page_flip and set_config hooks in struct
  340. * &drm_crtc_funcs.
  341. */
  342. DRM_PLANE_TYPE_PRIMARY,
  343. /**
  344. * @DRM_PLANE_TYPE_CURSOR:
  345. *
  346. * Cursor planes represent a "cursor" plane for a CRTC. Cursor planes
  347. * are the planes operated upon by the DRM_IOCTL_MODE_CURSOR and
  348. * DRM_IOCTL_MODE_CURSOR2 IOCTLs.
  349. */
  350. DRM_PLANE_TYPE_CURSOR,
  351. };
  352. /**
  353. * struct drm_plane - central DRM plane control structure
  354. * @dev: DRM device this plane belongs to
  355. * @head: for list management
  356. * @name: human readable name, can be overwritten by the driver
  357. * @base: base mode object
  358. * @possible_crtcs: pipes this plane can be bound to
  359. * @format_types: array of formats supported by this plane
  360. * @format_count: number of formats supported
  361. * @format_default: driver hasn't supplied supported formats for the plane
  362. * @crtc: currently bound CRTC
  363. * @fb: currently bound fb
  364. * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
  365. * drm_mode_set_config_internal() to implement correct refcounting.
  366. * @funcs: helper functions
  367. * @properties: property tracking for this plane
  368. * @type: type of plane (overlay, primary, cursor)
  369. * @state: current atomic state for this plane
  370. * @zpos_property: zpos property for this plane
  371. * @rotation_property: rotation property for this plane
  372. * @helper_private: mid-layer private data
  373. */
  374. struct drm_plane {
  375. struct drm_device *dev;
  376. struct list_head head;
  377. char *name;
  378. /**
  379. * @mutex:
  380. *
  381. * Protects modeset plane state, together with the mutex of &drm_crtc
  382. * this plane is linked to (when active, getting actived or getting
  383. * disabled).
  384. */
  385. struct drm_modeset_lock mutex;
  386. struct drm_mode_object base;
  387. uint32_t possible_crtcs;
  388. uint32_t *format_types;
  389. unsigned int format_count;
  390. bool format_default;
  391. struct drm_crtc *crtc;
  392. struct drm_framebuffer *fb;
  393. struct drm_framebuffer *old_fb;
  394. const struct drm_plane_funcs *funcs;
  395. struct drm_object_properties properties;
  396. enum drm_plane_type type;
  397. /**
  398. * @index: Position inside the mode_config.list, can be used as an array
  399. * index. It is invariant over the lifetime of the plane.
  400. */
  401. unsigned index;
  402. const struct drm_plane_helper_funcs *helper_private;
  403. struct drm_plane_state *state;
  404. struct drm_property *zpos_property;
  405. struct drm_property *rotation_property;
  406. struct drm_property *color_encoding_property;
  407. struct drm_property *color_range_property;
  408. };
  409. #define obj_to_plane(x) container_of(x, struct drm_plane, base)
  410. extern __printf(8, 9)
  411. int drm_universal_plane_init(struct drm_device *dev,
  412. struct drm_plane *plane,
  413. unsigned long possible_crtcs,
  414. const struct drm_plane_funcs *funcs,
  415. const uint32_t *formats,
  416. unsigned int format_count,
  417. enum drm_plane_type type,
  418. const char *name, ...);
  419. extern int drm_plane_init(struct drm_device *dev,
  420. struct drm_plane *plane,
  421. unsigned long possible_crtcs,
  422. const struct drm_plane_funcs *funcs,
  423. const uint32_t *formats, unsigned int format_count,
  424. bool is_primary);
  425. extern void drm_plane_cleanup(struct drm_plane *plane);
  426. /**
  427. * drm_plane_index - find the index of a registered plane
  428. * @plane: plane to find index for
  429. *
  430. * Given a registered plane, return the index of that plane within a DRM
  431. * device's list of planes.
  432. */
  433. static inline unsigned int drm_plane_index(struct drm_plane *plane)
  434. {
  435. return plane->index;
  436. }
  437. extern struct drm_plane * drm_plane_from_index(struct drm_device *dev, int idx);
  438. extern void drm_plane_force_disable(struct drm_plane *plane);
  439. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  440. struct drm_property *property,
  441. uint64_t value);
  442. /**
  443. * drm_plane_find - find a &drm_plane
  444. * @dev: DRM device
  445. * @id: plane id
  446. *
  447. * Returns the plane with @id, NULL if it doesn't exist. Simple wrapper around
  448. * drm_mode_object_find().
  449. */
  450. static inline struct drm_plane *drm_plane_find(struct drm_device *dev,
  451. uint32_t id)
  452. {
  453. struct drm_mode_object *mo;
  454. mo = drm_mode_object_find(dev, id, DRM_MODE_OBJECT_PLANE);
  455. return mo ? obj_to_plane(mo) : NULL;
  456. }
  457. /**
  458. * drm_for_each_plane_mask - iterate over planes specified by bitmask
  459. * @plane: the loop cursor
  460. * @dev: the DRM device
  461. * @plane_mask: bitmask of plane indices
  462. *
  463. * Iterate over all planes specified by bitmask.
  464. */
  465. #define drm_for_each_plane_mask(plane, dev, plane_mask) \
  466. list_for_each_entry((plane), &(dev)->mode_config.plane_list, head) \
  467. for_each_if ((plane_mask) & (1 << drm_plane_index(plane)))
  468. /**
  469. * drm_for_each_legacy_plane - iterate over all planes for legacy userspace
  470. * @plane: the loop cursor
  471. * @dev: the DRM device
  472. *
  473. * Iterate over all legacy planes of @dev, excluding primary and cursor planes.
  474. * This is useful for implementing userspace apis when userspace is not
  475. * universal plane aware. See also enum &drm_plane_type.
  476. */
  477. #define drm_for_each_legacy_plane(plane, dev) \
  478. list_for_each_entry(plane, &(dev)->mode_config.plane_list, head) \
  479. for_each_if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  480. /**
  481. * drm_for_each_plane - iterate over all planes
  482. * @plane: the loop cursor
  483. * @dev: the DRM device
  484. *
  485. * Iterate over all planes of @dev, include primary and cursor planes.
  486. */
  487. #define drm_for_each_plane(plane, dev) \
  488. list_for_each_entry(plane, &(dev)->mode_config.plane_list, head)
  489. #endif