drm_blend.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright (C) 2016 Samsung Electronics Co.Ltd
  3. * Authors:
  4. * Marek Szyprowski <[email protected]>
  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 appropriately 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. * Note that all the property extensions described here apply either to the
  185. * plane or the CRTC (e.g. for the background color, which currently is not
  186. * exposed and assumed to be black).
  187. *
  188. * SCALING_FILTER:
  189. * Indicates scaling filter to be used for plane scaler
  190. *
  191. * The value of this property can be one of the following:
  192. *
  193. * Default:
  194. * Driver's default scaling filter
  195. * Nearest Neighbor:
  196. * Nearest Neighbor scaling filter
  197. *
  198. * Drivers can set up this property for a plane by calling
  199. * drm_plane_create_scaling_filter_property
  200. */
  201. /**
  202. * drm_plane_create_alpha_property - create a new alpha property
  203. * @plane: drm plane
  204. *
  205. * This function creates a generic, mutable, alpha property and enables support
  206. * for it in the DRM core. It is attached to @plane.
  207. *
  208. * The alpha property will be allowed to be within the bounds of 0
  209. * (transparent) to 0xffff (opaque).
  210. *
  211. * Returns:
  212. * 0 on success, negative error code on failure.
  213. */
  214. int drm_plane_create_alpha_property(struct drm_plane *plane)
  215. {
  216. struct drm_property *prop;
  217. prop = drm_property_create_range(plane->dev, 0, "alpha",
  218. 0, DRM_BLEND_ALPHA_OPAQUE);
  219. if (!prop)
  220. return -ENOMEM;
  221. drm_object_attach_property(&plane->base, prop, DRM_BLEND_ALPHA_OPAQUE);
  222. plane->alpha_property = prop;
  223. if (plane->state)
  224. plane->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
  225. return 0;
  226. }
  227. EXPORT_SYMBOL(drm_plane_create_alpha_property);
  228. /**
  229. * drm_plane_create_rotation_property - create a new rotation property
  230. * @plane: drm plane
  231. * @rotation: initial value of the rotation property
  232. * @supported_rotations: bitmask of supported rotations and reflections
  233. *
  234. * This creates a new property with the selected support for transformations.
  235. *
  236. * Since a rotation by 180° degress is the same as reflecting both along the x
  237. * and the y axis the rotation property is somewhat redundant. Drivers can use
  238. * drm_rotation_simplify() to normalize values of this property.
  239. *
  240. * The property exposed to userspace is a bitmask property (see
  241. * drm_property_create_bitmask()) called "rotation" and has the following
  242. * bitmask enumaration values:
  243. *
  244. * DRM_MODE_ROTATE_0:
  245. * "rotate-0"
  246. * DRM_MODE_ROTATE_90:
  247. * "rotate-90"
  248. * DRM_MODE_ROTATE_180:
  249. * "rotate-180"
  250. * DRM_MODE_ROTATE_270:
  251. * "rotate-270"
  252. * DRM_MODE_REFLECT_X:
  253. * "reflect-x"
  254. * DRM_MODE_REFLECT_Y:
  255. * "reflect-y"
  256. *
  257. * Rotation is the specified amount in degrees in counter clockwise direction,
  258. * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
  259. * rotation. After reflection, the rotation is applied to the image sampled from
  260. * the source rectangle, before scaling it to fit the destination rectangle.
  261. */
  262. int drm_plane_create_rotation_property(struct drm_plane *plane,
  263. unsigned int rotation,
  264. unsigned int supported_rotations)
  265. {
  266. static const struct drm_prop_enum_list props[] = {
  267. { __builtin_ffs(DRM_MODE_ROTATE_0) - 1, "rotate-0" },
  268. { __builtin_ffs(DRM_MODE_ROTATE_90) - 1, "rotate-90" },
  269. { __builtin_ffs(DRM_MODE_ROTATE_180) - 1, "rotate-180" },
  270. { __builtin_ffs(DRM_MODE_ROTATE_270) - 1, "rotate-270" },
  271. { __builtin_ffs(DRM_MODE_REFLECT_X) - 1, "reflect-x" },
  272. { __builtin_ffs(DRM_MODE_REFLECT_Y) - 1, "reflect-y" },
  273. };
  274. struct drm_property *prop;
  275. WARN_ON((supported_rotations & DRM_MODE_ROTATE_MASK) == 0);
  276. WARN_ON(!is_power_of_2(rotation & DRM_MODE_ROTATE_MASK));
  277. WARN_ON(rotation & ~supported_rotations);
  278. prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
  279. props, ARRAY_SIZE(props),
  280. supported_rotations);
  281. if (!prop)
  282. return -ENOMEM;
  283. drm_object_attach_property(&plane->base, prop, rotation);
  284. if (plane->state)
  285. plane->state->rotation = rotation;
  286. plane->rotation_property = prop;
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(drm_plane_create_rotation_property);
  290. /**
  291. * drm_rotation_simplify() - Try to simplify the rotation
  292. * @rotation: Rotation to be simplified
  293. * @supported_rotations: Supported rotations
  294. *
  295. * Attempt to simplify the rotation to a form that is supported.
  296. * Eg. if the hardware supports everything except DRM_MODE_REFLECT_X
  297. * one could call this function like this:
  298. *
  299. * drm_rotation_simplify(rotation, DRM_MODE_ROTATE_0 |
  300. * DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  301. * DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_Y);
  302. *
  303. * to eliminate the DRM_MODE_REFLECT_X flag. Depending on what kind of
  304. * transforms the hardware supports, this function may not
  305. * be able to produce a supported transform, so the caller should
  306. * check the result afterwards.
  307. */
  308. unsigned int drm_rotation_simplify(unsigned int rotation,
  309. unsigned int supported_rotations)
  310. {
  311. if (rotation & ~supported_rotations) {
  312. rotation ^= DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  313. rotation = (rotation & DRM_MODE_REFLECT_MASK) |
  314. BIT((ffs(rotation & DRM_MODE_ROTATE_MASK) + 1)
  315. % 4);
  316. }
  317. return rotation;
  318. }
  319. EXPORT_SYMBOL(drm_rotation_simplify);
  320. /**
  321. * drm_plane_create_zpos_property - create mutable zpos property
  322. * @plane: drm plane
  323. * @zpos: initial value of zpos property
  324. * @min: minimal possible value of zpos property
  325. * @max: maximal possible value of zpos property
  326. *
  327. * This function initializes generic mutable zpos property and enables support
  328. * for it in drm core. Drivers can then attach this property to planes to enable
  329. * support for configurable planes arrangement during blending operation.
  330. * Drivers that attach a mutable zpos property to any plane should call the
  331. * drm_atomic_normalize_zpos() helper during their implementation of
  332. * &drm_mode_config_funcs.atomic_check(), which will update the normalized zpos
  333. * values and store them in &drm_plane_state.normalized_zpos. Usually min
  334. * should be set to 0 and max to maximal number of planes for given crtc - 1.
  335. *
  336. * If zpos of some planes cannot be changed (like fixed background or
  337. * cursor/topmost planes), drivers shall adjust the min/max values and assign
  338. * those planes immutable zpos properties with lower or higher values (for more
  339. * information, see drm_plane_create_zpos_immutable_property() function). In such
  340. * case drivers shall also assign proper initial zpos values for all planes in
  341. * its plane_reset() callback, so the planes will be always sorted properly.
  342. *
  343. * See also drm_atomic_normalize_zpos().
  344. *
  345. * The property exposed to userspace is called "zpos".
  346. *
  347. * Returns:
  348. * Zero on success, negative errno on failure.
  349. */
  350. int drm_plane_create_zpos_property(struct drm_plane *plane,
  351. unsigned int zpos,
  352. unsigned int min, unsigned int max)
  353. {
  354. struct drm_property *prop;
  355. prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
  356. if (!prop)
  357. return -ENOMEM;
  358. drm_object_attach_property(&plane->base, prop, zpos);
  359. plane->zpos_property = prop;
  360. if (plane->state) {
  361. plane->state->zpos = zpos;
  362. plane->state->normalized_zpos = zpos;
  363. }
  364. return 0;
  365. }
  366. EXPORT_SYMBOL(drm_plane_create_zpos_property);
  367. /**
  368. * drm_plane_create_zpos_immutable_property - create immuttable zpos property
  369. * @plane: drm plane
  370. * @zpos: value of zpos property
  371. *
  372. * This function initializes generic immutable zpos property and enables
  373. * support for it in drm core. Using this property driver lets userspace
  374. * to get the arrangement of the planes for blending operation and notifies
  375. * it that the hardware (or driver) doesn't support changing of the planes'
  376. * order. For mutable zpos see drm_plane_create_zpos_property().
  377. *
  378. * The property exposed to userspace is called "zpos".
  379. *
  380. * Returns:
  381. * Zero on success, negative errno on failure.
  382. */
  383. int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
  384. unsigned int zpos)
  385. {
  386. struct drm_property *prop;
  387. prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
  388. "zpos", zpos, zpos);
  389. if (!prop)
  390. return -ENOMEM;
  391. drm_object_attach_property(&plane->base, prop, zpos);
  392. plane->zpos_property = prop;
  393. if (plane->state) {
  394. plane->state->zpos = zpos;
  395. plane->state->normalized_zpos = zpos;
  396. }
  397. return 0;
  398. }
  399. EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
  400. static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
  401. {
  402. const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
  403. const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
  404. if (sa->zpos != sb->zpos)
  405. return sa->zpos - sb->zpos;
  406. else
  407. return sa->plane->base.id - sb->plane->base.id;
  408. }
  409. static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
  410. struct drm_crtc_state *crtc_state)
  411. {
  412. struct drm_atomic_state *state = crtc_state->state;
  413. struct drm_device *dev = crtc->dev;
  414. int total_planes = dev->mode_config.num_total_plane;
  415. struct drm_plane_state **states;
  416. struct drm_plane *plane;
  417. int i, n = 0;
  418. int ret = 0;
  419. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
  420. crtc->base.id, crtc->name);
  421. states = kmalloc_array(total_planes, sizeof(*states), GFP_KERNEL);
  422. if (!states)
  423. return -ENOMEM;
  424. /*
  425. * Normalization process might create new states for planes which
  426. * normalized_zpos has to be recalculated.
  427. */
  428. drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
  429. struct drm_plane_state *plane_state =
  430. drm_atomic_get_plane_state(state, plane);
  431. if (IS_ERR(plane_state)) {
  432. ret = PTR_ERR(plane_state);
  433. goto done;
  434. }
  435. states[n++] = plane_state;
  436. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
  437. plane->base.id, plane->name,
  438. plane_state->zpos);
  439. }
  440. sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
  441. for (i = 0; i < n; i++) {
  442. plane = states[i]->plane;
  443. states[i]->normalized_zpos = i;
  444. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
  445. plane->base.id, plane->name, i);
  446. }
  447. crtc_state->zpos_changed = true;
  448. done:
  449. kfree(states);
  450. return ret;
  451. }
  452. /**
  453. * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
  454. * @dev: DRM device
  455. * @state: atomic state of DRM device
  456. *
  457. * This function calculates normalized zpos value for all modified planes in
  458. * the provided atomic state of DRM device.
  459. *
  460. * For every CRTC this function checks new states of all planes assigned to
  461. * it and calculates normalized zpos value for these planes. Planes are compared
  462. * first by their zpos values, then by plane id (if zpos is equal). The plane
  463. * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
  464. * is then filled with unique values from 0 to number of active planes in crtc
  465. * minus one.
  466. *
  467. * RETURNS
  468. * Zero for success or -errno
  469. */
  470. int drm_atomic_normalize_zpos(struct drm_device *dev,
  471. struct drm_atomic_state *state)
  472. {
  473. struct drm_crtc *crtc;
  474. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  475. struct drm_plane *plane;
  476. struct drm_plane_state *old_plane_state, *new_plane_state;
  477. int i, ret = 0;
  478. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
  479. crtc = new_plane_state->crtc;
  480. if (!crtc)
  481. continue;
  482. if (old_plane_state->zpos != new_plane_state->zpos) {
  483. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  484. new_crtc_state->zpos_changed = true;
  485. }
  486. }
  487. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  488. if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
  489. new_crtc_state->zpos_changed) {
  490. ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
  491. new_crtc_state);
  492. if (ret)
  493. return ret;
  494. }
  495. }
  496. return 0;
  497. }
  498. EXPORT_SYMBOL(drm_atomic_normalize_zpos);
  499. /**
  500. * drm_plane_create_blend_mode_property - create a new blend mode property
  501. * @plane: drm plane
  502. * @supported_modes: bitmask of supported modes, must include
  503. * BIT(DRM_MODE_BLEND_PREMULTI). Current DRM assumption is
  504. * that alpha is premultiplied, and old userspace can break if
  505. * the property defaults to anything else.
  506. *
  507. * This creates a new property describing the blend mode.
  508. *
  509. * The property exposed to userspace is an enumeration property (see
  510. * drm_property_create_enum()) called "pixel blend mode" and has the
  511. * following enumeration values:
  512. *
  513. * "None":
  514. * Blend formula that ignores the pixel alpha.
  515. *
  516. * "Pre-multiplied":
  517. * Blend formula that assumes the pixel color values have been already
  518. * pre-multiplied with the alpha channel values.
  519. *
  520. * "Coverage":
  521. * Blend formula that assumes the pixel color values have not been
  522. * pre-multiplied and will do so when blending them to the background color
  523. * values.
  524. *
  525. * RETURNS:
  526. * Zero for success or -errno
  527. */
  528. int drm_plane_create_blend_mode_property(struct drm_plane *plane,
  529. unsigned int supported_modes)
  530. {
  531. struct drm_device *dev = plane->dev;
  532. struct drm_property *prop;
  533. static const struct drm_prop_enum_list props[] = {
  534. { DRM_MODE_BLEND_PIXEL_NONE, "None" },
  535. { DRM_MODE_BLEND_PREMULTI, "Pre-multiplied" },
  536. { DRM_MODE_BLEND_COVERAGE, "Coverage" },
  537. };
  538. unsigned int valid_mode_mask = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
  539. BIT(DRM_MODE_BLEND_PREMULTI) |
  540. BIT(DRM_MODE_BLEND_COVERAGE);
  541. int i;
  542. if (WARN_ON((supported_modes & ~valid_mode_mask) ||
  543. ((supported_modes & BIT(DRM_MODE_BLEND_PREMULTI)) == 0)))
  544. return -EINVAL;
  545. prop = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  546. "pixel blend mode",
  547. hweight32(supported_modes));
  548. if (!prop)
  549. return -ENOMEM;
  550. for (i = 0; i < ARRAY_SIZE(props); i++) {
  551. int ret;
  552. if (!(BIT(props[i].type) & supported_modes))
  553. continue;
  554. ret = drm_property_add_enum(prop, props[i].type,
  555. props[i].name);
  556. if (ret) {
  557. drm_property_destroy(dev, prop);
  558. return ret;
  559. }
  560. }
  561. drm_object_attach_property(&plane->base, prop, DRM_MODE_BLEND_PREMULTI);
  562. plane->blend_mode_property = prop;
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(drm_plane_create_blend_mode_property);