drm_blend.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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_blend.h>
  29. #include <linux/export.h>
  30. #include <linux/slab.h>
  31. #include <linux/sort.h>
  32. #include "drm_crtc_internal.h"
  33. /**
  34. * DOC: overview
  35. *
  36. * The basic plane composition model supported by standard plane properties only
  37. * has a source rectangle (in logical pixels within the &drm_framebuffer), with
  38. * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
  39. * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
  40. * defined by the horizontal and vertical visible pixels (stored in @hdisplay
  41. * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
  42. * two rectangles are both stored in the &drm_plane_state.
  43. *
  44. * For the atomic ioctl the following standard (atomic) properties on the plane object
  45. * encode the basic plane composition model:
  46. *
  47. * SRC_X:
  48. * X coordinate offset for the source rectangle within the
  49. * &drm_framebuffer, in 16.16 fixed point. Must be positive.
  50. * SRC_Y:
  51. * Y coordinate offset for the source rectangle within the
  52. * &drm_framebuffer, in 16.16 fixed point. Must be positive.
  53. * SRC_W:
  54. * Width for the source rectangle within the &drm_framebuffer, in 16.16
  55. * fixed point. SRC_X plus SRC_W must be within the width of the source
  56. * framebuffer. Must be positive.
  57. * SRC_H:
  58. * Height for the source rectangle within the &drm_framebuffer, in 16.16
  59. * fixed point. SRC_Y plus SRC_H must be within the height of the source
  60. * framebuffer. Must be positive.
  61. * CRTC_X:
  62. * X coordinate offset for the destination rectangle. Can be negative.
  63. * CRTC_Y:
  64. * Y coordinate offset for the destination rectangle. Can be negative.
  65. * CRTC_W:
  66. * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
  67. * the currently visible horizontal area of the &drm_crtc.
  68. * CRTC_H:
  69. * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
  70. * the currently visible vertical area of the &drm_crtc.
  71. * FB_ID:
  72. * Mode object ID of the &drm_framebuffer this plane should scan out.
  73. * CRTC_ID:
  74. * Mode object ID of the &drm_crtc this plane should be connected to.
  75. *
  76. * Note that the source rectangle must fully lie within the bounds of the
  77. * &drm_framebuffer. The destination rectangle can lie outside of the visible
  78. * area of the current mode of the CRTC. It must be apprpriately clipped by the
  79. * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
  80. * are also allowed to round the subpixel sampling positions appropriately, but
  81. * only to the next full pixel. No pixel outside of the source rectangle may
  82. * ever be sampled, which is important when applying more sophisticated
  83. * filtering than just a bilinear one when scaling. The filtering mode when
  84. * scaling is unspecified.
  85. *
  86. * On top of this basic transformation additional properties can be exposed by
  87. * the driver:
  88. *
  89. * rotation:
  90. * Rotation is set up with drm_plane_create_rotation_property(). It adds a
  91. * rotation and reflection step between the source and destination rectangles.
  92. * Without this property the rectangle is only scaled, but not rotated or
  93. * reflected.
  94. *
  95. * zpos:
  96. * Z position is set up with drm_plane_create_zpos_immutable_property() and
  97. * drm_plane_create_zpos_property(). It controls the visibility of overlapping
  98. * planes. Without this property the primary plane is always below the cursor
  99. * plane, and ordering between all other planes is undefined.
  100. *
  101. * Note that all the property extensions described here apply either to the
  102. * plane or the CRTC (e.g. for the background color, which currently is not
  103. * exposed and assumed to be black).
  104. */
  105. /**
  106. * drm_plane_create_rotation_property - create a new rotation property
  107. * @plane: drm plane
  108. * @rotation: initial value of the rotation property
  109. * @supported_rotations: bitmask of supported rotations and reflections
  110. *
  111. * This creates a new property with the selected support for transformations.
  112. *
  113. * Since a rotation by 180° degress is the same as reflecting both along the x
  114. * and the y axis the rotation property is somewhat redundant. Drivers can use
  115. * drm_rotation_simplify() to normalize values of this property.
  116. *
  117. * The property exposed to userspace is a bitmask property (see
  118. * drm_property_create_bitmask()) called "rotation" and has the following
  119. * bitmask enumaration values:
  120. *
  121. * DRM_MODE_ROTATE_0:
  122. * "rotate-0"
  123. * DRM_MODE_ROTATE_90:
  124. * "rotate-90"
  125. * DRM_MODE_ROTATE_180:
  126. * "rotate-180"
  127. * DRM_MODE_ROTATE_270:
  128. * "rotate-270"
  129. * DRM_MODE_REFLECT_X:
  130. * "reflect-x"
  131. * DRM_MODE_REFLECT_Y:
  132. * "reflect-y"
  133. *
  134. * Rotation is the specified amount in degrees in counter clockwise direction,
  135. * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
  136. * rotation. After reflection, the rotation is applied to the image sampled from
  137. * the source rectangle, before scaling it to fit the destination rectangle.
  138. */
  139. int drm_plane_create_rotation_property(struct drm_plane *plane,
  140. unsigned int rotation,
  141. unsigned int supported_rotations)
  142. {
  143. static const struct drm_prop_enum_list props[] = {
  144. { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
  145. { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
  146. { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
  147. { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
  148. { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
  149. { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
  150. };
  151. struct drm_property *prop;
  152. WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
  153. WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
  154. WARN_ON(rotation & ~supported_rotations);
  155. prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
  156. props, ARRAY_SIZE(props),
  157. supported_rotations);
  158. if (!prop)
  159. return -ENOMEM;
  160. drm_object_attach_property(&plane->base, prop, rotation);
  161. if (plane->state)
  162. plane->state->rotation = rotation;
  163. plane->rotation_property = prop;
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(drm_plane_create_rotation_property);
  167. /**
  168. * drm_rotation_simplify() - Try to simplify the rotation
  169. * @rotation: Rotation to be simplified
  170. * @supported_rotations: Supported rotations
  171. *
  172. * Attempt to simplify the rotation to a form that is supported.
  173. * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
  174. * one could call this function like this:
  175. *
  176. * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
  177. * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  178. * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
  179. *
  180. * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
  181. * transforms the hardware supports, this function may not
  182. * be able to produce a supported transform, so the caller should
  183. * check the result afterwards.
  184. */
  185. unsigned int drm_rotation_simplify(unsigned int rotation,
  186. unsigned int supported_rotations)
  187. {
  188. if (rotation & ~supported_rotations) {
  189. rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  190. rotation = (rotation & DRM_MODE_REFLECT_MASK) |
  191. BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
  192. % 4);
  193. }
  194. return rotation;
  195. }
  196. EXPORT_SYMBOL(drm_rotation_simplify);
  197. /**
  198. * drm_plane_create_zpos_property - create mutable zpos property
  199. * @plane: drm plane
  200. * @zpos: initial value of zpos property
  201. * @min: minimal possible value of zpos property
  202. * @max: maximal possible value of zpos property
  203. *
  204. * This function initializes generic mutable zpos property and enables support
  205. * for it in drm core. Drivers can then attach this property to planes to enable
  206. * support for configurable planes arrangement during blending operation.
  207. * Drivers that attach a mutable zpos property to any plane should call the
  208. * drm_atomic_normalize_zpos() helper during their implementation of
  209. * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
  210. * values and store them in &drm_plane_state.normalized_zpos. Usually min
  211. * should be set to 0 and max to maximal number of planes for given crtc - 1.
  212. *
  213. * If zpos of some planes cannot be changed (like fixed background or
  214. * cursor/topmost planes), driver should adjust min/max values and assign those
  215. * planes immutable zpos property with lower or higher values (for more
  216. * information, see drm_plane_create_zpos_immutable_property() function). In such
  217. * case driver should also assign proper initial zpos values for all planes in
  218. * its plane_reset() callback, so the planes will be always sorted properly.
  219. *
  220. * See also drm_atomic_normalize_zpos().
  221. *
  222. * The property exposed to userspace is called "zpos".
  223. *
  224. * Returns:
  225. * Zero on success, negative errno on failure.
  226. */
  227. int drm_plane_create_zpos_property(struct drm_plane *plane,
  228. unsigned int zpos,
  229. unsigned int min, unsigned int max)
  230. {
  231. struct drm_property *prop;
  232. prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
  233. if (!prop)
  234. return -ENOMEM;
  235. drm_object_attach_property(&plane->base, prop, zpos);
  236. plane->zpos_property = prop;
  237. if (plane->state) {
  238. plane->state->zpos = zpos;
  239. plane->state->normalized_zpos = zpos;
  240. }
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(drm_plane_create_zpos_property);
  244. /**
  245. * drm_plane_create_zpos_immutable_property - create immuttable zpos property
  246. * @plane: drm plane
  247. * @zpos: value of zpos property
  248. *
  249. * This function initializes generic immutable zpos property and enables
  250. * support for it in drm core. Using this property driver lets userspace
  251. * to get the arrangement of the planes for blending operation and notifies
  252. * it that the hardware (or driver) doesn't support changing of the planes'
  253. * order. For mutable zpos see drm_plane_create_zpos_property().
  254. *
  255. * The property exposed to userspace is called "zpos".
  256. *
  257. * Returns:
  258. * Zero on success, negative errno on failure.
  259. */
  260. int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
  261. unsigned int zpos)
  262. {
  263. struct drm_property *prop;
  264. prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
  265. "zpos", zpos, zpos);
  266. if (!prop)
  267. return -ENOMEM;
  268. drm_object_attach_property(&plane->base, prop, zpos);
  269. plane->zpos_property = prop;
  270. if (plane->state) {
  271. plane->state->zpos = zpos;
  272. plane->state->normalized_zpos = zpos;
  273. }
  274. return 0;
  275. }
  276. EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
  277. static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
  278. {
  279. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  280. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  281. if (sa->zpos != sb->zpos)
  282. return sa->zpos - sb->zpos;
  283. else
  284. return sa->plane->base.id - sb->plane->base.id;
  285. }
  286. static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
  287. struct drm_crtc_state *crtc_state)
  288. {
  289. struct drm_atomic_state *state = crtc_state->state;
  290. struct drm_device *dev = crtc->dev;
  291. int total_planes = dev->mode_config.num_total_plane;
  292. struct drm_plane_state **states;
  293. struct drm_plane *plane;
  294. int i, n = 0;
  295. int ret = 0;
  296. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
  297. crtc->base.id, crtc->name);
  298. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  299. if (!states)
  300. return -ENOMEM;
  301. /*
  302. * Normalization process might create new states for planes which
  303. * normalized_zpos has to be recalculated.
  304. */
  305. drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
  306. struct drm_plane_state *plane_state =
  307. drm_atomic_get_plane_state(state, plane);
  308. if (IS_ERR(plane_state)) {
  309. ret = PTR_ERR(plane_state);
  310. goto done;
  311. }
  312. states[n++] = plane_state;
  313. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
  314. plane->base.id, plane->name,
  315. plane_state->zpos);
  316. }
  317. sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
  318. for (i = 0; i < n; i++) {
  319. plane = states[i]->plane;
  320. states[i]->normalized_zpos = i;
  321. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
  322. plane->base.id, plane->name, i);
  323. }
  324. crtc_state->zpos_changed = true;
  325. done:
  326. kfree(states);
  327. return ret;
  328. }
  329. /**
  330. * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
  331. * @dev: DRM device
  332. * @state: atomic state of DRM device
  333. *
  334. * This function calculates normalized zpos value for all modified planes in
  335. * the provided atomic state of DRM device.
  336. *
  337. * For every CRTC this function checks new states of all planes assigned to
  338. * it and calculates normalized zpos value for these planes. Planes are compared
  339. * first by their zpos values, then by plane id (if zpos is equal). The plane
  340. * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
  341. * is then filled with unique values from 0 to number of active planes in crtc
  342. * minus one.
  343. *
  344. * RETURNS
  345. * Zero for success or -errno
  346. */
  347. int drm_atomic_normalize_zpos(struct drm_device *dev,
  348. struct drm_atomic_state *state)
  349. {
  350. struct drm_crtc *crtc;
  351. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  352. struct drm_plane *plane;
  353. struct drm_plane_state *old_plane_state, *new_plane_state;
  354. int i, ret = 0;
  355. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  356. crtc = new_plane_state->crtc;
  357. if (!crtc)
  358. continue;
  359. if (old_plane_state->zpos != new_plane_state->zpos) {
  360. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  361. new_crtc_state->zpos_changed = true;
  362. }
  363. }
  364. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  365. if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
  366. new_crtc_state->zpos_changed) {
  367. ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
  368. new_crtc_state);
  369. if (ret)
  370. return ret;
  371. }
  372. }
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(drm_atomic_normalize_zpos);