drm_blend.c 16 KB

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