overlay.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 2013 Ilia Mirkin
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  19. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Implementation based on the pre-KMS implementation in xf86-video-nouveau,
  23. * written by Arthur Huillet.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/drm_crtc.h>
  27. #include <drm/drm_fourcc.h>
  28. #include "nouveau_drm.h"
  29. #include "nouveau_bo.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_display.h"
  32. #include "nvreg.h"
  33. struct nouveau_plane {
  34. struct drm_plane base;
  35. bool flip;
  36. struct nouveau_bo *cur;
  37. struct {
  38. struct drm_property *colorkey;
  39. struct drm_property *contrast;
  40. struct drm_property *brightness;
  41. struct drm_property *hue;
  42. struct drm_property *saturation;
  43. struct drm_property *iturbt_709;
  44. } props;
  45. int colorkey;
  46. int contrast;
  47. int brightness;
  48. int hue;
  49. int saturation;
  50. int iturbt_709;
  51. };
  52. static uint32_t formats[] = {
  53. DRM_FORMAT_NV12,
  54. DRM_FORMAT_UYVY,
  55. };
  56. /* Sine can be approximated with
  57. * http://en.wikipedia.org/wiki/Bhaskara_I's_sine_approximation_formula
  58. * sin(x degrees) ~= 4 x (180 - x) / (40500 - x (180 - x) )
  59. * Note that this only works for the range [0, 180].
  60. * Also note that sin(x) == -sin(x - 180)
  61. */
  62. static inline int
  63. sin_mul(int degrees, int factor)
  64. {
  65. if (degrees > 180) {
  66. degrees -= 180;
  67. factor *= -1;
  68. }
  69. return factor * 4 * degrees * (180 - degrees) /
  70. (40500 - degrees * (180 - degrees));
  71. }
  72. /* cos(x) = sin(x + 90) */
  73. static inline int
  74. cos_mul(int degrees, int factor)
  75. {
  76. return sin_mul((degrees + 90) % 360, factor);
  77. }
  78. static int
  79. nv10_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  80. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  81. unsigned int crtc_w, unsigned int crtc_h,
  82. uint32_t src_x, uint32_t src_y,
  83. uint32_t src_w, uint32_t src_h)
  84. {
  85. struct nouveau_device *dev = nouveau_dev(plane->dev);
  86. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  87. struct nouveau_framebuffer *nv_fb = nouveau_framebuffer(fb);
  88. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  89. struct nouveau_bo *cur = nv_plane->cur;
  90. bool flip = nv_plane->flip;
  91. int format = ALIGN(src_w * 4, 0x100);
  92. int soff = NV_PCRTC0_SIZE * nv_crtc->index;
  93. int soff2 = NV_PCRTC0_SIZE * !nv_crtc->index;
  94. int ret;
  95. if (format > 0xffff)
  96. return -EINVAL;
  97. ret = nouveau_bo_pin(nv_fb->nvbo, TTM_PL_FLAG_VRAM);
  98. if (ret)
  99. return ret;
  100. nv_plane->cur = nv_fb->nvbo;
  101. /* Source parameters given in 16.16 fixed point, ignore fractional. */
  102. src_x = src_x >> 16;
  103. src_y = src_y >> 16;
  104. src_w = src_w >> 16;
  105. src_h = src_h >> 16;
  106. nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff, NV_CRTC_FSEL_OVERLAY, NV_CRTC_FSEL_OVERLAY);
  107. nv_mask(dev, NV_PCRTC_ENGINE_CTRL + soff2, NV_CRTC_FSEL_OVERLAY, 0);
  108. nv_wr32(dev, NV_PVIDEO_BASE(flip), 0);
  109. nv_wr32(dev, NV_PVIDEO_OFFSET_BUFF(flip), nv_fb->nvbo->bo.offset);
  110. nv_wr32(dev, NV_PVIDEO_SIZE_IN(flip), src_h << 16 | src_w);
  111. nv_wr32(dev, NV_PVIDEO_POINT_IN(flip), src_y << 16 | src_x);
  112. nv_wr32(dev, NV_PVIDEO_DS_DX(flip), (src_w << 20) / crtc_w);
  113. nv_wr32(dev, NV_PVIDEO_DT_DY(flip), (src_h << 20) / crtc_h);
  114. nv_wr32(dev, NV_PVIDEO_POINT_OUT(flip), crtc_y << 16 | crtc_x);
  115. nv_wr32(dev, NV_PVIDEO_SIZE_OUT(flip), crtc_h << 16 | crtc_w);
  116. if (fb->pixel_format == DRM_FORMAT_NV12) {
  117. format |= NV_PVIDEO_FORMAT_COLOR_LE_CR8YB8CB8YA8;
  118. format |= NV_PVIDEO_FORMAT_PLANAR;
  119. }
  120. if (nv_plane->iturbt_709)
  121. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  122. if (nv_plane->colorkey & (1 << 24))
  123. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  124. if (fb->pixel_format == DRM_FORMAT_NV12) {
  125. nv_wr32(dev, NV_PVIDEO_UVPLANE_BASE(flip), 0);
  126. nv_wr32(dev, NV_PVIDEO_UVPLANE_OFFSET_BUFF(flip),
  127. nv_fb->nvbo->bo.offset + fb->offsets[1]);
  128. }
  129. nv_wr32(dev, NV_PVIDEO_FORMAT(flip), format);
  130. nv_wr32(dev, NV_PVIDEO_STOP, 0);
  131. /* TODO: wait for vblank? */
  132. nv_wr32(dev, NV_PVIDEO_BUFFER, flip ? 0x10 : 0x1);
  133. nv_plane->flip = !flip;
  134. if (cur)
  135. nouveau_bo_unpin(cur);
  136. return 0;
  137. }
  138. static int
  139. nv10_disable_plane(struct drm_plane *plane)
  140. {
  141. struct nouveau_device *dev = nouveau_dev(plane->dev);
  142. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  143. nv_wr32(dev, NV_PVIDEO_STOP, 1);
  144. if (nv_plane->cur) {
  145. nouveau_bo_unpin(nv_plane->cur);
  146. nv_plane->cur = NULL;
  147. }
  148. return 0;
  149. }
  150. static void
  151. nv10_destroy_plane(struct drm_plane *plane)
  152. {
  153. nv10_disable_plane(plane);
  154. drm_plane_cleanup(plane);
  155. kfree(plane);
  156. }
  157. static void
  158. nv10_set_params(struct nouveau_plane *plane)
  159. {
  160. struct nouveau_device *dev = nouveau_dev(plane->base.dev);
  161. u32 luma = (plane->brightness - 512) << 16 | plane->contrast;
  162. u32 chroma = ((sin_mul(plane->hue, plane->saturation) & 0xffff) << 16) |
  163. (cos_mul(plane->hue, plane->saturation) & 0xffff);
  164. u32 format = 0;
  165. nv_wr32(dev, NV_PVIDEO_LUMINANCE(0), luma);
  166. nv_wr32(dev, NV_PVIDEO_LUMINANCE(1), luma);
  167. nv_wr32(dev, NV_PVIDEO_CHROMINANCE(0), chroma);
  168. nv_wr32(dev, NV_PVIDEO_CHROMINANCE(1), chroma);
  169. nv_wr32(dev, NV_PVIDEO_COLOR_KEY, plane->colorkey & 0xffffff);
  170. if (plane->cur) {
  171. if (plane->iturbt_709)
  172. format |= NV_PVIDEO_FORMAT_MATRIX_ITURBT709;
  173. if (plane->colorkey & (1 << 24))
  174. format |= NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY;
  175. nv_mask(dev, NV_PVIDEO_FORMAT(plane->flip),
  176. NV_PVIDEO_FORMAT_MATRIX_ITURBT709 |
  177. NV_PVIDEO_FORMAT_DISPLAY_COLOR_KEY,
  178. format);
  179. }
  180. }
  181. static int
  182. nv10_set_property(struct drm_plane *plane,
  183. struct drm_property *property,
  184. uint64_t value)
  185. {
  186. struct nouveau_plane *nv_plane = (struct nouveau_plane *)plane;
  187. if (property == nv_plane->props.colorkey)
  188. nv_plane->colorkey = value;
  189. else if (property == nv_plane->props.contrast)
  190. nv_plane->contrast = value;
  191. else if (property == nv_plane->props.brightness)
  192. nv_plane->brightness = value;
  193. else if (property == nv_plane->props.hue)
  194. nv_plane->hue = value;
  195. else if (property == nv_plane->props.saturation)
  196. nv_plane->saturation = value;
  197. else if (property == nv_plane->props.iturbt_709)
  198. nv_plane->iturbt_709 = value;
  199. else
  200. return -EINVAL;
  201. nv10_set_params(nv_plane);
  202. return 0;
  203. }
  204. static const struct drm_plane_funcs nv10_plane_funcs = {
  205. .update_plane = nv10_update_plane,
  206. .disable_plane = nv10_disable_plane,
  207. .set_property = nv10_set_property,
  208. .destroy = nv10_destroy_plane,
  209. };
  210. static void
  211. nv10_overlay_init(struct drm_device *device)
  212. {
  213. struct nouveau_device *dev = nouveau_dev(device);
  214. struct nouveau_plane *plane = kzalloc(sizeof(struct nouveau_plane), GFP_KERNEL);
  215. int ret;
  216. if (!plane)
  217. return;
  218. ret = drm_plane_init(device, &plane->base, 3 /* both crtc's */,
  219. &nv10_plane_funcs,
  220. formats, ARRAY_SIZE(formats), false);
  221. if (ret)
  222. goto err;
  223. /* Set up the plane properties */
  224. plane->props.colorkey = drm_property_create_range(
  225. device, 0, "colorkey", 0, 0x01ffffff);
  226. plane->props.contrast = drm_property_create_range(
  227. device, 0, "contrast", 0, 8192 - 1);
  228. plane->props.brightness = drm_property_create_range(
  229. device, 0, "brightness", 0, 1024);
  230. plane->props.hue = drm_property_create_range(
  231. device, 0, "hue", 0, 359);
  232. plane->props.saturation = drm_property_create_range(
  233. device, 0, "saturation", 0, 8192 - 1);
  234. plane->props.iturbt_709 = drm_property_create_range(
  235. device, 0, "iturbt_709", 0, 1);
  236. if (!plane->props.colorkey ||
  237. !plane->props.contrast ||
  238. !plane->props.brightness ||
  239. !plane->props.hue ||
  240. !plane->props.saturation ||
  241. !plane->props.iturbt_709)
  242. goto cleanup;
  243. plane->colorkey = 0;
  244. drm_object_attach_property(&plane->base.base,
  245. plane->props.colorkey, plane->colorkey);
  246. plane->contrast = 0x1000;
  247. drm_object_attach_property(&plane->base.base,
  248. plane->props.contrast, plane->contrast);
  249. plane->brightness = 512;
  250. drm_object_attach_property(&plane->base.base,
  251. plane->props.brightness, plane->brightness);
  252. plane->hue = 0;
  253. drm_object_attach_property(&plane->base.base,
  254. plane->props.hue, plane->hue);
  255. plane->saturation = 0x1000;
  256. drm_object_attach_property(&plane->base.base,
  257. plane->props.saturation, plane->saturation);
  258. plane->iturbt_709 = 0;
  259. drm_object_attach_property(&plane->base.base,
  260. plane->props.iturbt_709, plane->iturbt_709);
  261. nv10_set_params(plane);
  262. nv_wr32(dev, NV_PVIDEO_STOP, 1);
  263. return;
  264. cleanup:
  265. drm_plane_cleanup(&plane->base);
  266. err:
  267. kfree(plane);
  268. nv_error(dev, "Failed to create plane\n");
  269. }
  270. void
  271. nouveau_overlay_init(struct drm_device *device)
  272. {
  273. struct nouveau_device *dev = nouveau_dev(device);
  274. if (dev->chipset >= 0x10 && dev->chipset <= 0x40)
  275. nv10_overlay_init(device);
  276. }