drm_blend.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2016 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Marek Szyprowski <m.szyprowski@samsung.com>
  5. *
  6. * DRM core plane blending related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_atomic.h>
  28. #include <drm/drm_crtc.h>
  29. #include <linux/export.h>
  30. #include <linux/slab.h>
  31. #include <linux/sort.h>
  32. #include "drm_internal.h"
  33. /**
  34. * drm_plane_create_zpos_property - create mutable zpos property
  35. * @plane: drm plane
  36. * @zpos: initial value of zpos property
  37. * @min: minimal possible value of zpos property
  38. * @max: maximal possible value of zpos property
  39. *
  40. * This function initializes generic mutable zpos property and enables support
  41. * for it in drm core. Drivers can then attach this property to planes to enable
  42. * support for configurable planes arrangement during blending operation.
  43. * Once mutable zpos property has been enabled, the DRM core will automatically
  44. * calculate drm_plane_state->normalized_zpos values. Usually min should be set
  45. * to 0 and max to maximal number of planes for given crtc - 1.
  46. *
  47. * If zpos of some planes cannot be changed (like fixed background or
  48. * cursor/topmost planes), driver should adjust min/max values and assign those
  49. * planes immutable zpos property with lower or higher values (for more
  50. * information, see drm_mode_create_zpos_immutable_property() function). In such
  51. * case driver should also assign proper initial zpos values for all planes in
  52. * its plane_reset() callback, so the planes will be always sorted properly.
  53. *
  54. * Returns:
  55. * Zero on success, negative errno on failure.
  56. */
  57. int drm_plane_create_zpos_property(struct drm_plane *plane,
  58. unsigned int zpos,
  59. unsigned int min, unsigned int max)
  60. {
  61. struct drm_property *prop;
  62. prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
  63. if (!prop)
  64. return -ENOMEM;
  65. drm_object_attach_property(&plane->base, prop, zpos);
  66. plane->zpos_property = prop;
  67. if (plane->state) {
  68. plane->state->zpos = zpos;
  69. plane->state->normalized_zpos = zpos;
  70. }
  71. return 0;
  72. }
  73. EXPORT_SYMBOL(drm_plane_create_zpos_property);
  74. /**
  75. * drm_plane_create_zpos_immutable_property - create immuttable zpos property
  76. * @plane: drm plane
  77. * @zpos: value of zpos property
  78. *
  79. * This function initializes generic immutable zpos property and enables
  80. * support for it in drm core. Using this property driver lets userspace
  81. * to get the arrangement of the planes for blending operation and notifies
  82. * it that the hardware (or driver) doesn't support changing of the planes'
  83. * order.
  84. *
  85. * Returns:
  86. * Zero on success, negative errno on failure.
  87. */
  88. int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
  89. unsigned int zpos)
  90. {
  91. struct drm_property *prop;
  92. prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
  93. "zpos", zpos, zpos);
  94. if (!prop)
  95. return -ENOMEM;
  96. drm_object_attach_property(&plane->base, prop, zpos);
  97. plane->zpos_property = prop;
  98. if (plane->state) {
  99. plane->state->zpos = zpos;
  100. plane->state->normalized_zpos = zpos;
  101. }
  102. return 0;
  103. }
  104. EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
  105. static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
  106. {
  107. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  108. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  109. if (sa->zpos != sb->zpos)
  110. return sa->zpos - sb->zpos;
  111. else
  112. return sa->plane->base.id - sb->plane->base.id;
  113. }
  114. /**
  115. * drm_atomic_helper_crtc_normalize_zpos - calculate normalized zpos values
  116. * @crtc: crtc with planes, which have to be considered for normalization
  117. * @crtc_state: new atomic state to apply
  118. *
  119. * This function checks new states of all planes assigned to given crtc and
  120. * calculates normalized zpos value for them. Planes are compared first by their
  121. * zpos values, then by plane id (if zpos equals). Plane with lowest zpos value
  122. * is at the bottom. The plane_state->normalized_zpos is then filled with unique
  123. * values from 0 to number of active planes in crtc minus one.
  124. *
  125. * RETURNS
  126. * Zero for success or -errno
  127. */
  128. static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
  129. struct drm_crtc_state *crtc_state)
  130. {
  131. struct drm_atomic_state *state = crtc_state->state;
  132. struct drm_device *dev = crtc->dev;
  133. int total_planes = dev->mode_config.num_total_plane;
  134. struct drm_plane_state **states;
  135. struct drm_plane *plane;
  136. int i, n = 0;
  137. int ret = 0;
  138. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
  139. crtc->base.id, crtc->name);
  140. states = kmalloc_array(total_planes, sizeof(*states), GFP_TEMPORARY);
  141. if (!states)
  142. return -ENOMEM;
  143. /*
  144. * Normalization process might create new states for planes which
  145. * normalized_zpos has to be recalculated.
  146. */
  147. drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
  148. struct drm_plane_state *plane_state =
  149. drm_atomic_get_plane_state(state, plane);
  150. if (IS_ERR(plane_state)) {
  151. ret = PTR_ERR(plane_state);
  152. goto done;
  153. }
  154. states[n++] = plane_state;
  155. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
  156. plane->base.id, plane->name,
  157. plane_state->zpos);
  158. }
  159. sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
  160. for (i = 0; i < n; i++) {
  161. plane = states[i]->plane;
  162. states[i]->normalized_zpos = i;
  163. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
  164. plane->base.id, plane->name, i);
  165. }
  166. crtc_state->zpos_changed = true;
  167. done:
  168. kfree(states);
  169. return ret;
  170. }
  171. /**
  172. * drm_atomic_helper_normalize_zpos - calculate normalized zpos values for all
  173. * crtcs
  174. * @dev: DRM device
  175. * @state: atomic state of DRM device
  176. *
  177. * This function calculates normalized zpos value for all modified planes in
  178. * the provided atomic state of DRM device. For more information, see
  179. * drm_atomic_helper_crtc_normalize_zpos() function.
  180. *
  181. * RETURNS
  182. * Zero for success or -errno
  183. */
  184. int drm_atomic_helper_normalize_zpos(struct drm_device *dev,
  185. struct drm_atomic_state *state)
  186. {
  187. struct drm_crtc *crtc;
  188. struct drm_crtc_state *crtc_state;
  189. struct drm_plane *plane;
  190. struct drm_plane_state *plane_state;
  191. int i, ret = 0;
  192. for_each_plane_in_state(state, plane, plane_state, i) {
  193. crtc = plane_state->crtc;
  194. if (!crtc)
  195. continue;
  196. if (plane->state->zpos != plane_state->zpos) {
  197. crtc_state =
  198. drm_atomic_get_existing_crtc_state(state, crtc);
  199. crtc_state->zpos_changed = true;
  200. }
  201. }
  202. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  203. if (crtc_state->plane_mask != crtc->state->plane_mask ||
  204. crtc_state->zpos_changed) {
  205. ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
  206. crtc_state);
  207. if (ret)
  208. return ret;
  209. }
  210. }
  211. return 0;
  212. }