drm_blend.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 is set up with drm_plane_create_rotation_property(). It adds a
  90. * rotation and reflection step between the source and destination rectangles.
  91. * Without this property the rectangle is only scaled, but not rotated or
  92. * reflected.
  93. *
  94. * - Z position is set up with drm_plane_create_zpos_immutable_property() and
  95. * drm_plane_create_zpos_property(). It controls the visibility of overlapping
  96. * planes. Without this property the primary plane is always below the cursor
  97. * plane, and ordering between all other planes is undefined.
  98. *
  99. * Note that all the property extensions described here apply either to the
  100. * plane or the CRTC (e.g. for the background color, which currently is not
  101. * exposed and assumed to be black).
  102. */
  103. /**
  104. * drm_plane_create_rotation_property - create a new rotation property
  105. * @plane: drm plane
  106. * @rotation: initial value of the rotation property
  107. * @supported_rotations: bitmask of supported rotations and reflections
  108. *
  109. * This creates a new property with the selected support for transformations.
  110. *
  111. * Since a rotation by 180° degress is the same as reflecting both along the x
  112. * and the y axis the rotation property is somewhat redundant. Drivers can use
  113. * drm_rotation_simplify() to normalize values of this property.
  114. *
  115. * The property exposed to userspace is a bitmask property (see
  116. * drm_property_create_bitmask()) called "rotation" and has the following
  117. * bitmask enumaration values:
  118. *
  119. * DRM_MODE_ROTATE_0:
  120. * "rotate-0"
  121. * DRM_MODE_ROTATE_90:
  122. * "rotate-90"
  123. * DRM_MODE_ROTATE_180:
  124. * "rotate-180"
  125. * DRM_MODE_ROTATE_270:
  126. * "rotate-270"
  127. * DRM_MODE_REFLECT_X:
  128. * "reflect-x"
  129. * DRM_MODE_REFLECT_Y:
  130. * "reflect-y"
  131. *
  132. * Rotation is the specified amount in degrees in counter clockwise direction,
  133. * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
  134. * rotation. After reflection, the rotation is applied to the image sampled from
  135. * the source rectangle, before scaling it to fit the destination rectangle.
  136. */
  137. int drm_plane_create_rotation_property(struct drm_plane *plane,
  138. unsigned int rotation,
  139. unsigned int supported_rotations)
  140. {
  141. static const struct drm_prop_enum_list props[] = {
  142. { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
  143. { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
  144. { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
  145. { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
  146. { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
  147. { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
  148. };
  149. struct drm_property *prop;
  150. WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
  151. WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
  152. WARN_ON(rotation & ~supported_rotations);
  153. prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
  154. props, ARRAY_SIZE(props),
  155. supported_rotations);
  156. if (!prop)
  157. return -ENOMEM;
  158. drm_object_attach_property(&plane->base, prop, rotation);
  159. if (plane->state)
  160. plane->state->rotation = rotation;
  161. plane->rotation_property = prop;
  162. return 0;
  163. }
  164. EXPORT_SYMBOL(drm_plane_create_rotation_property);
  165. /**
  166. * drm_rotation_simplify() - Try to simplify the rotation
  167. * @rotation: Rotation to be simplified
  168. * @supported_rotations: Supported rotations
  169. *
  170. * Attempt to simplify the rotation to a form that is supported.
  171. * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
  172. * one could call this function like this:
  173. *
  174. * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
  175. * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  176. * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
  177. *
  178. * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
  179. * transforms the hardware supports, this function may not
  180. * be able to produce a supported transform, so the caller should
  181. * check the result afterwards.
  182. */
  183. unsigned int drm_rotation_simplify(unsigned int rotation,
  184. unsigned int supported_rotations)
  185. {
  186. if (rotation & ~supported_rotations) {
  187. rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  188. rotation = (rotation & DRM_MODE_REFLECT_MASK) |
  189. BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
  190. % 4);
  191. }
  192. return rotation;
  193. }
  194. EXPORT_SYMBOL(drm_rotation_simplify);
  195. /**
  196. * drm_plane_create_zpos_property - create mutable zpos property
  197. * @plane: drm plane
  198. * @zpos: initial value of zpos property
  199. * @min: minimal possible value of zpos property
  200. * @max: maximal possible value of zpos property
  201. *
  202. * This function initializes generic mutable zpos property and enables support
  203. * for it in drm core. Drivers can then attach this property to planes to enable
  204. * support for configurable planes arrangement during blending operation.
  205. * Once mutable zpos property has been enabled, the DRM core will automatically
  206. * calculate &drm_plane_state.normalized_zpos values. Usually min should be set
  207. * to 0 and max to maximal number of planes for given crtc - 1.
  208. *
  209. * If zpos of some planes cannot be changed (like fixed background or
  210. * cursor/topmost planes), driver should adjust min/max values and assign those
  211. * planes immutable zpos property with lower or higher values (for more
  212. * information, see drm_plane_create_zpos_immutable_property() function). In such
  213. * case driver should also assign proper initial zpos values for all planes in
  214. * its plane_reset() callback, so the planes will be always sorted properly.
  215. *
  216. * See also drm_atomic_normalize_zpos().
  217. *
  218. * The property exposed to userspace is called "zpos".
  219. *
  220. * Returns:
  221. * Zero on success, negative errno on failure.
  222. */
  223. int drm_plane_create_zpos_property(struct drm_plane *plane,
  224. unsigned int zpos,
  225. unsigned int min, unsigned int max)
  226. {
  227. struct drm_property *prop;
  228. prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
  229. if (!prop)
  230. return -ENOMEM;
  231. drm_object_attach_property(&plane->base, prop, zpos);
  232. plane->zpos_property = prop;
  233. if (plane->state) {
  234. plane->state->zpos = zpos;
  235. plane->state->normalized_zpos = zpos;
  236. }
  237. return 0;
  238. }
  239. EXPORT_SYMBOL(drm_plane_create_zpos_property);
  240. /**
  241. * drm_plane_create_zpos_immutable_property - create immuttable zpos property
  242. * @plane: drm plane
  243. * @zpos: value of zpos property
  244. *
  245. * This function initializes generic immutable zpos property and enables
  246. * support for it in drm core. Using this property driver lets userspace
  247. * to get the arrangement of the planes for blending operation and notifies
  248. * it that the hardware (or driver) doesn't support changing of the planes'
  249. * order. For mutable zpos see drm_plane_create_zpos_property().
  250. *
  251. * The property exposed to userspace is called "zpos".
  252. *
  253. * Returns:
  254. * Zero on success, negative errno on failure.
  255. */
  256. int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
  257. unsigned int zpos)
  258. {
  259. struct drm_property *prop;
  260. prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
  261. "zpos", zpos, zpos);
  262. if (!prop)
  263. return -ENOMEM;
  264. drm_object_attach_property(&plane->base, prop, zpos);
  265. plane->zpos_property = prop;
  266. if (plane->state) {
  267. plane->state->zpos = zpos;
  268. plane->state->normalized_zpos = zpos;
  269. }
  270. return 0;
  271. }
  272. EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
  273. static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
  274. {
  275. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  276. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  277. if (sa->zpos != sb->zpos)
  278. return sa->zpos - sb->zpos;
  279. else
  280. return sa->plane->base.id - sb->plane->base.id;
  281. }
  282. static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
  283. struct drm_crtc_state *crtc_state)
  284. {
  285. struct drm_atomic_state *state = crtc_state->state;
  286. struct drm_device *dev = crtc->dev;
  287. int total_planes = dev->mode_config.num_total_plane;
  288. struct drm_plane_state **states;
  289. struct drm_plane *plane;
  290. int i, n = 0;
  291. int ret = 0;
  292. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
  293. crtc->base.id, crtc->name);
  294. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  295. if (!states)
  296. return -ENOMEM;
  297. /*
  298. * Normalization process might create new states for planes which
  299. * normalized_zpos has to be recalculated.
  300. */
  301. drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
  302. struct drm_plane_state *plane_state =
  303. drm_atomic_get_plane_state(state, plane);
  304. if (IS_ERR(plane_state)) {
  305. ret = PTR_ERR(plane_state);
  306. goto done;
  307. }
  308. states[n++] = plane_state;
  309. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
  310. plane->base.id, plane->name,
  311. plane_state->zpos);
  312. }
  313. sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
  314. for (i = 0; i < n; i++) {
  315. plane = states[i]->plane;
  316. states[i]->normalized_zpos = i;
  317. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
  318. plane->base.id, plane->name, i);
  319. }
  320. crtc_state->zpos_changed = true;
  321. done:
  322. kfree(states);
  323. return ret;
  324. }
  325. /**
  326. * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
  327. * @dev: DRM device
  328. * @state: atomic state of DRM device
  329. *
  330. * This function calculates normalized zpos value for all modified planes in
  331. * the provided atomic state of DRM device.
  332. *
  333. * For every CRTC this function checks new states of all planes assigned to
  334. * it and calculates normalized zpos value for these planes. Planes are compared
  335. * first by their zpos values, then by plane id (if zpos is equal). The plane
  336. * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
  337. * is then filled with unique values from 0 to number of active planes in crtc
  338. * minus one.
  339. *
  340. * RETURNS
  341. * Zero for success or -errno
  342. */
  343. int drm_atomic_normalize_zpos(struct drm_device *dev,
  344. struct drm_atomic_state *state)
  345. {
  346. struct drm_crtc *crtc;
  347. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  348. struct drm_plane *plane;
  349. struct drm_plane_state *old_plane_state, *new_plane_state;
  350. int i, ret = 0;
  351. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  352. crtc = new_plane_state->crtc;
  353. if (!crtc)
  354. continue;
  355. if (old_plane_state->zpos != new_plane_state->zpos) {
  356. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  357. new_crtc_state->zpos_changed = true;
  358. }
  359. }
  360. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  361. if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
  362. new_crtc_state->zpos_changed) {
  363. ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
  364. new_crtc_state);
  365. if (ret)
  366. return ret;
  367. }
  368. }
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(drm_atomic_normalize_zpos);