drm_blend.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 <linux/export.h>
  27. #include <linux/slab.h>
  28. #include <linux/sort.h>
  29. #include <drm/drm_atomic.h>
  30. #include <drm/drm_blend.h>
  31. #include <drm/drm_device.h>
  32. #include <drm/drm_print.h>
  33. #include "drm_crtc_internal.h"
  34. /**
  35. * DOC: overview
  36. *
  37. * The basic plane composition model supported by standard plane properties only
  38. * has a source rectangle (in logical pixels within the &drm_framebuffer), with
  39. * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
  40. * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
  41. * defined by the horizontal and vertical visible pixels (stored in @hdisplay
  42. * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
  43. * two rectangles are both stored in the &drm_plane_state.
  44. *
  45. * For the atomic ioctl the following standard (atomic) properties on the plane object
  46. * encode the basic plane composition model:
  47. *
  48. * SRC_X:
  49. * X coordinate offset for the source rectangle within the
  50. * &drm_framebuffer, in 16.16 fixed point. Must be positive.
  51. * SRC_Y:
  52. * Y coordinate offset for the source rectangle within the
  53. * &drm_framebuffer, in 16.16 fixed point. Must be positive.
  54. * SRC_W:
  55. * Width for the source rectangle within the &drm_framebuffer, in 16.16
  56. * fixed point. SRC_X plus SRC_W must be within the width of the source
  57. * framebuffer. Must be positive.
  58. * SRC_H:
  59. * Height for the source rectangle within the &drm_framebuffer, in 16.16
  60. * fixed point. SRC_Y plus SRC_H must be within the height of the source
  61. * framebuffer. Must be positive.
  62. * CRTC_X:
  63. * X coordinate offset for the destination rectangle. Can be negative.
  64. * CRTC_Y:
  65. * Y coordinate offset for the destination rectangle. Can be negative.
  66. * CRTC_W:
  67. * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
  68. * the currently visible horizontal area of the &drm_crtc.
  69. * CRTC_H:
  70. * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
  71. * the currently visible vertical area of the &drm_crtc.
  72. * FB_ID:
  73. * Mode object ID of the &drm_framebuffer this plane should scan out.
  74. * CRTC_ID:
  75. * Mode object ID of the &drm_crtc this plane should be connected to.
  76. *
  77. * Note that the source rectangle must fully lie within the bounds of the
  78. * &drm_framebuffer. The destination rectangle can lie outside of the visible
  79. * area of the current mode of the CRTC. It must be apprpriately clipped by the
  80. * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
  81. * are also allowed to round the subpixel sampling positions appropriately, but
  82. * only to the next full pixel. No pixel outside of the source rectangle may
  83. * ever be sampled, which is important when applying more sophisticated
  84. * filtering than just a bilinear one when scaling. The filtering mode when
  85. * scaling is unspecified.
  86. *
  87. * On top of this basic transformation additional properties can be exposed by
  88. * the driver:
  89. *
  90. * alpha:
  91. * Alpha is setup with drm_plane_create_alpha_property(). It controls the
  92. * plane-wide opacity, from transparent (0) to opaque (0xffff). It can be
  93. * combined with pixel alpha.
  94. * The pixel values in the framebuffers are expected to not be
  95. * pre-multiplied by the global alpha associated to the plane.
  96. *
  97. * rotation:
  98. * Rotation is set up with drm_plane_create_rotation_property(). It adds a
  99. * rotation and reflection step between the source and destination rectangles.
  100. * Without this property the rectangle is only scaled, but not rotated or
  101. * reflected.
  102. *
  103. * Possbile values:
  104. *
  105. * "rotate-<degrees>":
  106. * Signals that a drm plane is rotated <degrees> degrees in counter
  107. * clockwise direction.
  108. *
  109. * "reflect-<axis>":
  110. * Signals that the contents of a drm plane is reflected along the
  111. * <axis> axis, in the same way as mirroring.
  112. *
  113. * reflect-x::
  114. *
  115. * |o | | o|
  116. * | | -> | |
  117. * | v| |v |
  118. *
  119. * reflect-y::
  120. *
  121. * |o | | ^|
  122. * | | -> | |
  123. * | v| |o |
  124. *
  125. * zpos:
  126. * Z position is set up with drm_plane_create_zpos_immutable_property() and
  127. * drm_plane_create_zpos_property(). It controls the visibility of overlapping
  128. * planes. Without this property the primary plane is always below the cursor
  129. * plane, and ordering between all other planes is undefined. The positive
  130. * Z axis points towards the user, i.e. planes with lower Z position values
  131. * are underneath planes with higher Z position values. Two planes with the
  132. * same Z position value have undefined ordering. Note that the Z position
  133. * value can also be immutable, to inform userspace about the hard-coded
  134. * stacking of planes, see drm_plane_create_zpos_immutable_property(). If
  135. * any plane has a zpos property (either mutable or immutable), then all
  136. * planes shall have a zpos property.
  137. *
  138. * pixel blend mode:
  139. * Pixel blend mode is set up with drm_plane_create_blend_mode_property().
  140. * It adds a blend mode for alpha blending equation selection, describing
  141. * how the pixels from the current plane are composited with the
  142. * background.
  143. *
  144. * Three alpha blending equations are defined:
  145. *
  146. * "None":
  147. * Blend formula that ignores the pixel alpha::
  148. *
  149. * out.rgb = plane_alpha * fg.rgb +
  150. * (1 - plane_alpha) * bg.rgb
  151. *
  152. * "Pre-multiplied":
  153. * Blend formula that assumes the pixel color values
  154. * have been already pre-multiplied with the alpha
  155. * channel values::
  156. *
  157. * out.rgb = plane_alpha * fg.rgb +
  158. * (1 - (plane_alpha * fg.alpha)) * bg.rgb
  159. *
  160. * "Coverage":
  161. * Blend formula that assumes the pixel color values have not
  162. * been pre-multiplied and will do so when blending them to the
  163. * background color values::
  164. *
  165. * out.rgb = plane_alpha * fg.alpha * fg.rgb +
  166. * (1 - (plane_alpha * fg.alpha)) * bg.rgb
  167. *
  168. * Using the following symbols:
  169. *
  170. * "fg.rgb":
  171. * Each of the RGB component values from the plane's pixel
  172. * "fg.alpha":
  173. * Alpha component value from the plane's pixel. If the plane's
  174. * pixel format has no alpha component, then this is assumed to be
  175. * 1.0. In these cases, this property has no effect, as all three
  176. * equations become equivalent.
  177. * "bg.rgb":
  178. * Each of the RGB component values from the background
  179. * "plane_alpha":
  180. * Plane alpha value set by the plane "alpha" property. If the
  181. * plane does not expose the "alpha" property, then this is
  182. * assumed to be 1.0
  183. *
  184. * IN_FORMATS:
  185. * Blob property which contains the set of buffer format and modifier
  186. * pairs supported by this plane. The blob is a drm_format_modifier_blob
  187. * struct. Without this property the plane doesn't support buffers with
  188. * modifiers. Userspace cannot change this property.
  189. *
  190. * Note that all the property extensions described here apply either to the
  191. * plane or the CRTC (e.g. for the background color, which currently is not
  192. * exposed and assumed to be black).
  193. */
  194. /**
  195. * drm_plane_create_alpha_property - create a new alpha property
  196. * @plane: drm plane
  197. *
  198. * This function creates a generic, mutable, alpha property and enables support
  199. * for it in the DRM core. It is attached to @plane.
  200. *
  201. * The alpha property will be allowed to be within the bounds of 0
  202. * (transparent) to 0xffff (opaque).
  203. *
  204. * Returns:
  205. * 0 on success, negative error code on failure.
  206. */
  207. int drm_plane_create_alpha_property(struct drm_plane *plane)
  208. {
  209. struct drm_property *prop;
  210. prop = drm_property_create_range(plane->dev, 0, "alpha",
  211. 0, DRM_BLEND_ALPHA_OPAQUE);
  212. if (!prop)
  213. return -ENOMEM;
  214. drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
  215. plane->alpha_property = prop;
  216. if (plane->state)
  217. plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
  218. return 0;
  219. }
  220. EXPORT_SYMBOL(drm_plane_create_alpha_property);
  221. /**
  222. * drm_plane_create_rotation_property - create a new rotation property
  223. * @plane: drm plane
  224. * @rotation: initial value of the rotation property
  225. * @supported_rotations: bitmask of supported rotations and reflections
  226. *
  227. * This creates a new property with the selected support for transformations.
  228. *
  229. * Since a rotation by 180° degress is the same as reflecting both along the x
  230. * and the y axis the rotation property is somewhat redundant. Drivers can use
  231. * drm_rotation_simplify() to normalize values of this property.
  232. *
  233. * The property exposed to userspace is a bitmask property (see
  234. * drm_property_create_bitmask()) called "rotation" and has the following
  235. * bitmask enumaration values:
  236. *
  237. * DRM_MODE_ROTATE_0:
  238. * "rotate-0"
  239. * DRM_MODE_ROTATE_90:
  240. * "rotate-90"
  241. * DRM_MODE_ROTATE_180:
  242. * "rotate-180"
  243. * DRM_MODE_ROTATE_270:
  244. * "rotate-270"
  245. * DRM_MODE_REFLECT_X:
  246. * "reflect-x"
  247. * DRM_MODE_REFLECT_Y:
  248. * "reflect-y"
  249. *
  250. * Rotation is the specified amount in degrees in counter clockwise direction,
  251. * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
  252. * rotation. After reflection, the rotation is applied to the image sampled from
  253. * the source rectangle, before scaling it to fit the destination rectangle.
  254. */
  255. int drm_plane_create_rotation_property(struct drm_plane *plane,
  256. unsigned int rotation,
  257. unsigned int supported_rotations)
  258. {
  259. static const struct drm_prop_enum_list props[] = {
  260. { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
  261. { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
  262. { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
  263. { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
  264. { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
  265. { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
  266. };
  267. struct drm_property *prop;
  268. WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
  269. WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
  270. WARN_ON(rotation & ~supported_rotations);
  271. prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
  272. props, ARRAY_SIZE(props),
  273. supported_rotations);
  274. if (!prop)
  275. return -ENOMEM;
  276. drm_object_attach_property(&plane->base, prop, rotation);
  277. if (plane->state)
  278. plane->state->rotation = rotation;
  279. plane->rotation_property = prop;
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(drm_plane_create_rotation_property);
  283. /**
  284. * drm_rotation_simplify() - Try to simplify the rotation
  285. * @rotation: Rotation to be simplified
  286. * @supported_rotations: Supported rotations
  287. *
  288. * Attempt to simplify the rotation to a form that is supported.
  289. * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
  290. * one could call this function like this:
  291. *
  292. * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
  293. * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  294. * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
  295. *
  296. * to eliminate the DRM_MODE_ROTATE_X flag. Depending on what kind of
  297. * transforms the hardware supports, this function may not
  298. * be able to produce a supported transform, so the caller should
  299. * check the result afterwards.
  300. */
  301. unsigned int drm_rotation_simplify(unsigned int rotation,
  302. unsigned int supported_rotations)
  303. {
  304. if (rotation & ~supported_rotations) {
  305. rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  306. rotation = (rotation & DRM_MODE_REFLECT_MASK) |
  307. BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
  308. % 4);
  309. }
  310. return rotation;
  311. }
  312. EXPORT_SYMBOL(drm_rotation_simplify);
  313. /**
  314. * drm_plane_create_zpos_property - create mutable zpos property
  315. * @plane: drm plane
  316. * @zpos: initial value of zpos property
  317. * @min: minimal possible value of zpos property
  318. * @max: maximal possible value of zpos property
  319. *
  320. * This function initializes generic mutable zpos property and enables support
  321. * for it in drm core. Drivers can then attach this property to planes to enable
  322. * support for configurable planes arrangement during blending operation.
  323. * Drivers that attach a mutable zpos property to any plane should call the
  324. * drm_atomic_normalize_zpos() helper during their implementation of
  325. * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
  326. * values and store them in &drm_plane_state.normalized_zpos. Usually min
  327. * should be set to 0 and max to maximal number of planes for given crtc - 1.
  328. *
  329. * If zpos of some planes cannot be changed (like fixed background or
  330. * cursor/topmost planes), drivers shall adjust the min/max values and assign
  331. * those planes immutable zpos properties with lower or higher values (for more
  332. * information, see drm_plane_create_zpos_immutable_property() function). In such
  333. * case drivers shall also assign proper initial zpos values for all planes in
  334. * its plane_reset() callback, so the planes will be always sorted properly.
  335. *
  336. * See also drm_atomic_normalize_zpos().
  337. *
  338. * The property exposed to userspace is called "zpos".
  339. *
  340. * Returns:
  341. * Zero on success, negative errno on failure.
  342. */
  343. int drm_plane_create_zpos_property(struct drm_plane *plane,
  344. unsigned int zpos,
  345. unsigned int min, unsigned int max)
  346. {
  347. struct drm_property *prop;
  348. prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
  349. if (!prop)
  350. return -ENOMEM;
  351. drm_object_attach_property(&plane->base, prop, zpos);
  352. plane->zpos_property = prop;
  353. if (plane->state) {
  354. plane->state->zpos = zpos;
  355. plane->state->normalized_zpos = zpos;
  356. }
  357. return 0;
  358. }
  359. EXPORT_SYMBOL(drm_plane_create_zpos_property);
  360. /**
  361. * drm_plane_create_zpos_immutable_property - create immuttable zpos property
  362. * @plane: drm plane
  363. * @zpos: value of zpos property
  364. *
  365. * This function initializes generic immutable zpos property and enables
  366. * support for it in drm core. Using this property driver lets userspace
  367. * to get the arrangement of the planes for blending operation and notifies
  368. * it that the hardware (or driver) doesn't support changing of the planes'
  369. * order. For mutable zpos see drm_plane_create_zpos_property().
  370. *
  371. * The property exposed to userspace is called "zpos".
  372. *
  373. * Returns:
  374. * Zero on success, negative errno on failure.
  375. */
  376. int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
  377. unsigned int zpos)
  378. {
  379. struct drm_property *prop;
  380. prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
  381. "zpos", zpos, zpos);
  382. if (!prop)
  383. return -ENOMEM;
  384. drm_object_attach_property(&plane->base, prop, zpos);
  385. plane->zpos_property = prop;
  386. if (plane->state) {
  387. plane->state->zpos = zpos;
  388. plane->state->normalized_zpos = zpos;
  389. }
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
  393. static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
  394. {
  395. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  396. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  397. if (sa->zpos != sb->zpos)
  398. return sa->zpos - sb->zpos;
  399. else
  400. return sa->plane->base.id - sb->plane->base.id;
  401. }
  402. static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
  403. struct drm_crtc_state *crtc_state)
  404. {
  405. struct drm_atomic_state *state = crtc_state->state;
  406. struct drm_device *dev = crtc->dev;
  407. int total_planes = dev->mode_config.num_total_plane;
  408. struct drm_plane_state **states;
  409. struct drm_plane *plane;
  410. int i, n = 0;
  411. int ret = 0;
  412. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
  413. crtc->base.id, crtc->name);
  414. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  415. if (!states)
  416. return -ENOMEM;
  417. /*
  418. * Normalization process might create new states for planes which
  419. * normalized_zpos has to be recalculated.
  420. */
  421. drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
  422. struct drm_plane_state *plane_state =
  423. drm_atomic_get_plane_state(state, plane);
  424. if (IS_ERR(plane_state)) {
  425. ret = PTR_ERR(plane_state);
  426. goto done;
  427. }
  428. states[n++] = plane_state;
  429. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
  430. plane->base.id, plane->name,
  431. plane_state->zpos);
  432. }
  433. sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
  434. for (i = 0; i < n; i++) {
  435. plane = states[i]->plane;
  436. states[i]->normalized_zpos = i;
  437. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
  438. plane->base.id, plane->name, i);
  439. }
  440. crtc_state->zpos_changed = true;
  441. done:
  442. kfree(states);
  443. return ret;
  444. }
  445. /**
  446. * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
  447. * @dev: DRM device
  448. * @state: atomic state of DRM device
  449. *
  450. * This function calculates normalized zpos value for all modified planes in
  451. * the provided atomic state of DRM device.
  452. *
  453. * For every CRTC this function checks new states of all planes assigned to
  454. * it and calculates normalized zpos value for these planes. Planes are compared
  455. * first by their zpos values, then by plane id (if zpos is equal). The plane
  456. * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
  457. * is then filled with unique values from 0 to number of active planes in crtc
  458. * minus one.
  459. *
  460. * RETURNS
  461. * Zero for success or -errno
  462. */
  463. int drm_atomic_normalize_zpos(struct drm_device *dev,
  464. struct drm_atomic_state *state)
  465. {
  466. struct drm_crtc *crtc;
  467. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  468. struct drm_plane *plane;
  469. struct drm_plane_state *old_plane_state, *new_plane_state;
  470. int i, ret = 0;
  471. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  472. crtc = new_plane_state->crtc;
  473. if (!crtc)
  474. continue;
  475. if (old_plane_state->zpos != new_plane_state->zpos) {
  476. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  477. new_crtc_state->zpos_changed = true;
  478. }
  479. }
  480. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  481. if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
  482. new_crtc_state->zpos_changed) {
  483. ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
  484. new_crtc_state);
  485. if (ret)
  486. return ret;
  487. }
  488. }
  489. return 0;
  490. }
  491. EXPORT_SYMBOL(drm_atomic_normalize_zpos);
  492. /**
  493. * drm_plane_create_blend_mode_property - create a new blend mode property
  494. * @plane: drm plane
  495. * @supported_modes: bitmask of supported modes, must include
  496. * BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
  497. * that alpha is premultiplied, and old userspace can break if
  498. * the property defaults to anything else.
  499. *
  500. * This creates a new property describing the blend mode.
  501. *
  502. * The property exposed to userspace is an enumeration property (see
  503. * drm_property_create_enum()) called "pixel blend mode" and has the
  504. * following enumeration values:
  505. *
  506. * "None":
  507. * Blend formula that ignores the pixel alpha.
  508. *
  509. * "Pre-multiplied":
  510. * Blend formula that assumes the pixel color values have been already
  511. * pre-multiplied with the alpha channel values.
  512. *
  513. * "Coverage":
  514. * Blend formula that assumes the pixel color values have not been
  515. * pre-multiplied and will do so when blending them to the background color
  516. * values.
  517. *
  518. * RETURNS:
  519. * Zero for success or -errno
  520. */
  521. int drm_plane_create_blend_mode_property(struct drm_plane *plane,
  522. unsigned int supported_modes)
  523. {
  524. struct drm_device *dev = plane->dev;
  525. struct drm_property *prop;
  526. static const struct drm_prop_enum_list props[] = {
  527. { DRM_MODE_BLEND_PIXEL_NONE, "None" },
  528. { DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
  529. { DRM_MODE_BLEND_COVERAGE, "Coverage" },
  530. };
  531. unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
  532. BIT(DRM_MODE_BLEND_PREMULTI) |
  533. BIT(DRM_MODE_BLEND_COVERAGE);
  534. int i;
  535. if (WARN_ON((supported_modes & ~valid_mode_mask) ||
  536. ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
  537. return -EINVAL;
  538. prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  539. "pixel blend mode",
  540. hweight32(supported_modes));
  541. if (!prop)
  542. return -ENOMEM;
  543. for (i = 0; i < ARRAY_SIZE(props); i++) {
  544. int ret;
  545. if (!(BIT(props[i].type) & supported_modes))
  546. continue;
  547. ret = drm_property_add_enum(prop, props[i].type,
  548. props[i].name);
  549. if (ret) {
  550. drm_property_destroy(dev, prop);
  551. return ret;
  552. }
  553. }
  554. drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
  555. plane->blend_mode_property = prop;
  556. return 0;
  557. }
  558. EXPORT_SYMBOL(drm_plane_create_blend_mode_property);