Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dri2.c 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Mesa 3-D graphics library
  3. * Version: 7.9
  4. *
  5. * Copyright 2009, VMware, Inc.
  6. * All Rights Reserved.
  7. * Copyright (C) 2010 LunarG Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Whitwell <keithw@vmware.com>
  28. * Jakob Bornecrantz <wallbraker@gmail.com>
  29. * Chia-I Wu <olv@lunarg.com>
  30. */
  31. #include "util/u_memory.h"
  32. #include "util/u_inlines.h"
  33. #include "util/u_format.h"
  34. #include "util/u_debug.h"
  35. #include "state_tracker/drm_driver.h"
  36. #include "dri_screen.h"
  37. #include "dri_context.h"
  38. #include "dri_drawable.h"
  39. #include "dri2_buffer.h"
  40. /**
  41. * DRI2 flush extension.
  42. */
  43. static void
  44. dri2_flush_drawable(__DRIdrawable *dPriv)
  45. {
  46. struct dri_context *ctx = dri_get_current(dPriv->driScreenPriv);
  47. struct dri_drawable *drawable = dri_drawable(dPriv);
  48. struct pipe_resource *ptex = drawable->textures[ST_ATTACHMENT_BACK_LEFT];
  49. if (ctx) {
  50. if (ptex && ctx->pp && drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL])
  51. pp_run(ctx->pp, ptex, ptex, drawable->textures[ST_ATTACHMENT_DEPTH_STENCIL]);
  52. ctx->st->flush(ctx->st, 0, NULL);
  53. }
  54. }
  55. static void
  56. dri2_invalidate_drawable(__DRIdrawable *dPriv)
  57. {
  58. struct dri_drawable *drawable = dri_drawable(dPriv);
  59. dri2InvalidateDrawable(dPriv);
  60. drawable->dPriv->lastStamp = drawable->dPriv->dri2.stamp;
  61. p_atomic_inc(&drawable->base.stamp);
  62. }
  63. static const __DRI2flushExtension dri2FlushExtension = {
  64. { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
  65. dri2_flush_drawable,
  66. dri2_invalidate_drawable,
  67. };
  68. /**
  69. * Retrieve __DRIbuffer from the DRI loader.
  70. */
  71. static __DRIbuffer *
  72. dri2_drawable_get_buffers(struct dri_drawable *drawable,
  73. const enum st_attachment_type *statts,
  74. unsigned *count)
  75. {
  76. __DRIdrawable *dri_drawable = drawable->dPriv;
  77. struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
  78. boolean with_format;
  79. __DRIbuffer *buffers;
  80. int num_buffers;
  81. unsigned attachments[10];
  82. unsigned num_attachments, i;
  83. assert(loader);
  84. with_format = dri_with_format(drawable->sPriv);
  85. num_attachments = 0;
  86. /* for Xserver 1.6.0 (DRI2 version 1) we always need to ask for the front */
  87. if (!with_format)
  88. attachments[num_attachments++] = __DRI_BUFFER_FRONT_LEFT;
  89. for (i = 0; i < *count; i++) {
  90. enum pipe_format format;
  91. unsigned bind;
  92. int att, depth;
  93. dri_drawable_get_format(drawable, statts[i], &format, &bind);
  94. if (format == PIPE_FORMAT_NONE)
  95. continue;
  96. switch (statts[i]) {
  97. case ST_ATTACHMENT_FRONT_LEFT:
  98. /* already added */
  99. if (!with_format)
  100. continue;
  101. att = __DRI_BUFFER_FRONT_LEFT;
  102. break;
  103. case ST_ATTACHMENT_BACK_LEFT:
  104. att = __DRI_BUFFER_BACK_LEFT;
  105. break;
  106. case ST_ATTACHMENT_FRONT_RIGHT:
  107. att = __DRI_BUFFER_FRONT_RIGHT;
  108. break;
  109. case ST_ATTACHMENT_BACK_RIGHT:
  110. att = __DRI_BUFFER_BACK_RIGHT;
  111. break;
  112. case ST_ATTACHMENT_DEPTH_STENCIL:
  113. att = __DRI_BUFFER_DEPTH_STENCIL;
  114. break;
  115. default:
  116. att = -1;
  117. break;
  118. }
  119. /*
  120. * In this switch statement we must support all formats that
  121. * may occur as the stvis->color_format or
  122. * stvis->depth_stencil_format.
  123. */
  124. switch(format) {
  125. case PIPE_FORMAT_B8G8R8A8_UNORM:
  126. depth = 32;
  127. break;
  128. case PIPE_FORMAT_B8G8R8X8_UNORM:
  129. depth = 24;
  130. break;
  131. case PIPE_FORMAT_B5G6R5_UNORM:
  132. depth = 16;
  133. break;
  134. case PIPE_FORMAT_Z16_UNORM:
  135. att = __DRI_BUFFER_DEPTH;
  136. depth = 16;
  137. break;
  138. case PIPE_FORMAT_Z24X8_UNORM:
  139. case PIPE_FORMAT_X8Z24_UNORM:
  140. att = __DRI_BUFFER_DEPTH;
  141. depth = 24;
  142. break;
  143. case PIPE_FORMAT_Z24_UNORM_S8_UINT:
  144. case PIPE_FORMAT_S8_UINT_Z24_UNORM:
  145. depth = 32;
  146. break;
  147. case PIPE_FORMAT_Z32_UNORM:
  148. att = __DRI_BUFFER_DEPTH;
  149. depth = 32;
  150. break;
  151. default:
  152. depth = util_format_get_blocksizebits(format);
  153. assert(!"Unexpected format in dri2_drawable_get_buffers()");
  154. }
  155. if (att >= 0) {
  156. attachments[num_attachments++] = att;
  157. if (with_format) {
  158. attachments[num_attachments++] = depth;
  159. }
  160. }
  161. }
  162. if (with_format) {
  163. num_attachments /= 2;
  164. buffers = loader->getBuffersWithFormat(dri_drawable,
  165. &dri_drawable->w, &dri_drawable->h,
  166. attachments, num_attachments,
  167. &num_buffers, dri_drawable->loaderPrivate);
  168. }
  169. else {
  170. buffers = loader->getBuffers(dri_drawable,
  171. &dri_drawable->w, &dri_drawable->h,
  172. attachments, num_attachments,
  173. &num_buffers, dri_drawable->loaderPrivate);
  174. }
  175. if (buffers)
  176. *count = num_buffers;
  177. return buffers;
  178. }
  179. /**
  180. * Process __DRIbuffer and convert them into pipe_resources.
  181. */
  182. static void
  183. dri2_drawable_process_buffers(struct dri_drawable *drawable,
  184. __DRIbuffer *buffers, unsigned count)
  185. {
  186. struct dri_screen *screen = dri_screen(drawable->sPriv);
  187. __DRIdrawable *dri_drawable = drawable->dPriv;
  188. struct pipe_resource templ;
  189. struct winsys_handle whandle;
  190. boolean have_depth = FALSE;
  191. unsigned i, bind;
  192. if (drawable->old_num == count &&
  193. drawable->old_w == dri_drawable->w &&
  194. drawable->old_h == dri_drawable->h &&
  195. memcmp(drawable->old, buffers, sizeof(__DRIbuffer) * count) == 0)
  196. return;
  197. for (i = 0; i < ST_ATTACHMENT_COUNT; i++)
  198. pipe_resource_reference(&drawable->textures[i], NULL);
  199. memset(&templ, 0, sizeof(templ));
  200. templ.target = screen->target;
  201. templ.last_level = 0;
  202. templ.width0 = dri_drawable->w;
  203. templ.height0 = dri_drawable->h;
  204. templ.depth0 = 1;
  205. templ.array_size = 1;
  206. memset(&whandle, 0, sizeof(whandle));
  207. for (i = 0; i < count; i++) {
  208. __DRIbuffer *buf = &buffers[i];
  209. enum st_attachment_type statt;
  210. enum pipe_format format;
  211. switch (buf->attachment) {
  212. case __DRI_BUFFER_FRONT_LEFT:
  213. if (!screen->auto_fake_front) {
  214. statt = ST_ATTACHMENT_INVALID;
  215. break;
  216. }
  217. /* fallthrough */
  218. case __DRI_BUFFER_FAKE_FRONT_LEFT:
  219. statt = ST_ATTACHMENT_FRONT_LEFT;
  220. break;
  221. case __DRI_BUFFER_BACK_LEFT:
  222. statt = ST_ATTACHMENT_BACK_LEFT;
  223. break;
  224. case __DRI_BUFFER_DEPTH:
  225. case __DRI_BUFFER_DEPTH_STENCIL:
  226. case __DRI_BUFFER_STENCIL:
  227. /* use only the first depth/stencil buffer */
  228. if (!have_depth) {
  229. have_depth = TRUE;
  230. statt = ST_ATTACHMENT_DEPTH_STENCIL;
  231. }
  232. else {
  233. statt = ST_ATTACHMENT_INVALID;
  234. }
  235. break;
  236. default:
  237. statt = ST_ATTACHMENT_INVALID;
  238. break;
  239. }
  240. dri_drawable_get_format(drawable, statt, &format, &bind);
  241. if (statt == ST_ATTACHMENT_INVALID || format == PIPE_FORMAT_NONE)
  242. continue;
  243. templ.format = format;
  244. templ.bind = bind;
  245. whandle.handle = buf->name;
  246. whandle.stride = buf->pitch;
  247. drawable->textures[statt] =
  248. screen->base.screen->resource_from_handle(screen->base.screen,
  249. &templ, &whandle);
  250. }
  251. drawable->old_num = count;
  252. drawable->old_w = dri_drawable->w;
  253. drawable->old_h = dri_drawable->h;
  254. memcpy(drawable->old, buffers, sizeof(__DRIbuffer) * count);
  255. }
  256. static __DRIbuffer *
  257. dri2_allocate_buffer(__DRIscreen *sPriv,
  258. unsigned attachment, unsigned format,
  259. int width, int height)
  260. {
  261. struct dri_screen *screen = dri_screen(sPriv);
  262. struct dri2_buffer *buffer;
  263. struct pipe_resource templ;
  264. enum pipe_format pf;
  265. unsigned bind = 0;
  266. struct winsys_handle whandle;
  267. switch (attachment) {
  268. case __DRI_BUFFER_FRONT_LEFT:
  269. case __DRI_BUFFER_FAKE_FRONT_LEFT:
  270. bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  271. break;
  272. case __DRI_BUFFER_BACK_LEFT:
  273. bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  274. break;
  275. case __DRI_BUFFER_DEPTH:
  276. case __DRI_BUFFER_DEPTH_STENCIL:
  277. case __DRI_BUFFER_STENCIL:
  278. bind = PIPE_BIND_DEPTH_STENCIL; /* XXX sampler? */
  279. break;
  280. }
  281. /* because we get the handle and stride */
  282. bind |= PIPE_BIND_SHARED;
  283. switch (format) {
  284. case 32:
  285. pf = PIPE_FORMAT_B8G8R8A8_UNORM;
  286. break;
  287. case 24:
  288. pf = PIPE_FORMAT_B8G8R8X8_UNORM;
  289. break;
  290. case 16:
  291. pf = PIPE_FORMAT_Z16_UNORM;
  292. break;
  293. default:
  294. return NULL;
  295. }
  296. buffer = CALLOC_STRUCT(dri2_buffer);
  297. if (!buffer)
  298. return NULL;
  299. memset(&templ, 0, sizeof(templ));
  300. templ.bind = bind;
  301. templ.format = pf;
  302. templ.target = PIPE_TEXTURE_2D;
  303. templ.last_level = 0;
  304. templ.width0 = width;
  305. templ.height0 = height;
  306. templ.depth0 = 1;
  307. templ.array_size = 1;
  308. buffer->resource =
  309. screen->base.screen->resource_create(screen->base.screen, &templ);
  310. if (!buffer->resource) {
  311. FREE(buffer);
  312. return NULL;
  313. }
  314. memset(&whandle, 0, sizeof(whandle));
  315. whandle.type = DRM_API_HANDLE_TYPE_SHARED;
  316. screen->base.screen->resource_get_handle(screen->base.screen,
  317. buffer->resource, &whandle);
  318. buffer->base.attachment = attachment;
  319. buffer->base.name = whandle.handle;
  320. buffer->base.cpp = util_format_get_blocksize(pf);
  321. buffer->base.pitch = whandle.stride;
  322. return &buffer->base;
  323. }
  324. static void
  325. dri2_release_buffer(__DRIscreen *sPriv, __DRIbuffer *bPriv)
  326. {
  327. struct dri2_buffer *buffer = dri2_buffer(bPriv);
  328. pipe_resource_reference(&buffer->resource, NULL);
  329. FREE(buffer);
  330. }
  331. /*
  332. * Backend functions for st_framebuffer interface.
  333. */
  334. static void
  335. dri2_allocate_textures(struct dri_drawable *drawable,
  336. const enum st_attachment_type *statts,
  337. unsigned count)
  338. {
  339. __DRIbuffer *buffers;
  340. unsigned num_buffers = count;
  341. buffers = dri2_drawable_get_buffers(drawable, statts, &num_buffers);
  342. if (buffers)
  343. dri2_drawable_process_buffers(drawable, buffers, num_buffers);
  344. }
  345. static void
  346. dri2_flush_frontbuffer(struct dri_drawable *drawable,
  347. enum st_attachment_type statt)
  348. {
  349. __DRIdrawable *dri_drawable = drawable->dPriv;
  350. struct __DRIdri2LoaderExtensionRec *loader = drawable->sPriv->dri2.loader;
  351. if (loader->flushFrontBuffer == NULL)
  352. return;
  353. if (statt == ST_ATTACHMENT_FRONT_LEFT) {
  354. loader->flushFrontBuffer(dri_drawable, dri_drawable->loaderPrivate);
  355. }
  356. }
  357. static void
  358. dri2_update_tex_buffer(struct dri_drawable *drawable,
  359. struct dri_context *ctx,
  360. struct pipe_resource *res)
  361. {
  362. /* no-op */
  363. }
  364. static __DRIimage *
  365. dri2_lookup_egl_image(struct dri_screen *screen, void *handle)
  366. {
  367. __DRIimageLookupExtension *loader = screen->sPriv->dri2.image;
  368. __DRIimage *img;
  369. if (!loader->lookupEGLImage)
  370. return NULL;
  371. img = loader->lookupEGLImage(screen->sPriv,
  372. handle, screen->sPriv->loaderPrivate);
  373. return img;
  374. }
  375. static __DRIimage *
  376. dri2_create_image_from_name(__DRIscreen *_screen,
  377. int width, int height, int format,
  378. int name, int pitch, void *loaderPrivate)
  379. {
  380. struct dri_screen *screen = dri_screen(_screen);
  381. __DRIimage *img;
  382. struct pipe_resource templ;
  383. struct winsys_handle whandle;
  384. unsigned tex_usage;
  385. enum pipe_format pf;
  386. tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  387. switch (format) {
  388. case __DRI_IMAGE_FORMAT_RGB565:
  389. pf = PIPE_FORMAT_B5G6R5_UNORM;
  390. break;
  391. case __DRI_IMAGE_FORMAT_XRGB8888:
  392. pf = PIPE_FORMAT_B8G8R8X8_UNORM;
  393. break;
  394. case __DRI_IMAGE_FORMAT_ARGB8888:
  395. pf = PIPE_FORMAT_B8G8R8A8_UNORM;
  396. break;
  397. case __DRI_IMAGE_FORMAT_ABGR8888:
  398. pf = PIPE_FORMAT_R8G8B8A8_UNORM;
  399. break;
  400. default:
  401. pf = PIPE_FORMAT_NONE;
  402. break;
  403. }
  404. if (pf == PIPE_FORMAT_NONE)
  405. return NULL;
  406. img = CALLOC_STRUCT(__DRIimageRec);
  407. if (!img)
  408. return NULL;
  409. memset(&templ, 0, sizeof(templ));
  410. templ.bind = tex_usage;
  411. templ.format = pf;
  412. templ.target = screen->target;
  413. templ.last_level = 0;
  414. templ.width0 = width;
  415. templ.height0 = height;
  416. templ.depth0 = 1;
  417. templ.array_size = 1;
  418. memset(&whandle, 0, sizeof(whandle));
  419. whandle.handle = name;
  420. whandle.stride = pitch * util_format_get_blocksize(pf);
  421. img->texture = screen->base.screen->resource_from_handle(screen->base.screen,
  422. &templ, &whandle);
  423. if (!img->texture) {
  424. FREE(img);
  425. return NULL;
  426. }
  427. img->level = 0;
  428. img->layer = 0;
  429. img->dri_format = format;
  430. img->loader_private = loaderPrivate;
  431. return img;
  432. }
  433. static __DRIimage *
  434. dri2_create_image_from_renderbuffer(__DRIcontext *context,
  435. int renderbuffer, void *loaderPrivate)
  436. {
  437. struct dri_context *ctx = dri_context(context);
  438. if (!ctx->st->get_resource_for_egl_image)
  439. return NULL;
  440. /* TODO */
  441. return NULL;
  442. }
  443. static __DRIimage *
  444. dri2_create_image(__DRIscreen *_screen,
  445. int width, int height, int format,
  446. unsigned int use, void *loaderPrivate)
  447. {
  448. struct dri_screen *screen = dri_screen(_screen);
  449. __DRIimage *img;
  450. struct pipe_resource templ;
  451. unsigned tex_usage;
  452. enum pipe_format pf;
  453. tex_usage = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
  454. if (use & __DRI_IMAGE_USE_SCANOUT)
  455. tex_usage |= PIPE_BIND_SCANOUT;
  456. if (use & __DRI_IMAGE_USE_SHARE)
  457. tex_usage |= PIPE_BIND_SHARED;
  458. if (use & __DRI_IMAGE_USE_CURSOR) {
  459. if (width != 64 || height != 64)
  460. return NULL;
  461. tex_usage |= PIPE_BIND_CURSOR;
  462. }
  463. switch (format) {
  464. case __DRI_IMAGE_FORMAT_RGB565:
  465. pf = PIPE_FORMAT_B5G6R5_UNORM;
  466. break;
  467. case __DRI_IMAGE_FORMAT_XRGB8888:
  468. pf = PIPE_FORMAT_B8G8R8X8_UNORM;
  469. break;
  470. case __DRI_IMAGE_FORMAT_ARGB8888:
  471. pf = PIPE_FORMAT_B8G8R8A8_UNORM;
  472. break;
  473. case __DRI_IMAGE_FORMAT_ABGR8888:
  474. pf = PIPE_FORMAT_R8G8B8A8_UNORM;
  475. break;
  476. default:
  477. pf = PIPE_FORMAT_NONE;
  478. break;
  479. }
  480. if (pf == PIPE_FORMAT_NONE)
  481. return NULL;
  482. img = CALLOC_STRUCT(__DRIimageRec);
  483. if (!img)
  484. return NULL;
  485. memset(&templ, 0, sizeof(templ));
  486. templ.bind = tex_usage;
  487. templ.format = pf;
  488. templ.target = PIPE_TEXTURE_2D;
  489. templ.last_level = 0;
  490. templ.width0 = width;
  491. templ.height0 = height;
  492. templ.depth0 = 1;
  493. templ.array_size = 1;
  494. img->texture = screen->base.screen->resource_create(screen->base.screen, &templ);
  495. if (!img->texture) {
  496. FREE(img);
  497. return NULL;
  498. }
  499. img->level = 0;
  500. img->layer = 0;
  501. img->dri_format = format;
  502. img->dri_components = 0;
  503. img->loader_private = loaderPrivate;
  504. return img;
  505. }
  506. static GLboolean
  507. dri2_query_image(__DRIimage *image, int attrib, int *value)
  508. {
  509. struct winsys_handle whandle;
  510. memset(&whandle, 0, sizeof(whandle));
  511. switch (attrib) {
  512. case __DRI_IMAGE_ATTRIB_STRIDE:
  513. image->texture->screen->resource_get_handle(image->texture->screen,
  514. image->texture, &whandle);
  515. *value = whandle.stride;
  516. return GL_TRUE;
  517. case __DRI_IMAGE_ATTRIB_HANDLE:
  518. whandle.type = DRM_API_HANDLE_TYPE_KMS;
  519. image->texture->screen->resource_get_handle(image->texture->screen,
  520. image->texture, &whandle);
  521. *value = whandle.handle;
  522. return GL_TRUE;
  523. case __DRI_IMAGE_ATTRIB_NAME:
  524. whandle.type = DRM_API_HANDLE_TYPE_SHARED;
  525. image->texture->screen->resource_get_handle(image->texture->screen,
  526. image->texture, &whandle);
  527. *value = whandle.handle;
  528. return GL_TRUE;
  529. case __DRI_IMAGE_ATTRIB_FORMAT:
  530. *value = image->dri_format;
  531. return GL_TRUE;
  532. case __DRI_IMAGE_ATTRIB_WIDTH:
  533. *value = image->texture->width0;
  534. return GL_TRUE;
  535. case __DRI_IMAGE_ATTRIB_HEIGHT:
  536. *value = image->texture->height0;
  537. return GL_TRUE;
  538. case __DRI_IMAGE_ATTRIB_COMPONENTS:
  539. if (image->dri_components == 0)
  540. return GL_FALSE;
  541. *value = image->dri_components;
  542. return GL_TRUE;
  543. default:
  544. return GL_FALSE;
  545. }
  546. }
  547. static __DRIimage *
  548. dri2_dup_image(__DRIimage *image, void *loaderPrivate)
  549. {
  550. __DRIimage *img;
  551. img = CALLOC_STRUCT(__DRIimageRec);
  552. if (!img)
  553. return NULL;
  554. img->texture = NULL;
  555. pipe_resource_reference(&img->texture, image->texture);
  556. img->level = image->level;
  557. img->layer = image->layer;
  558. /* This should be 0 for sub images, but dup is also used for base images. */
  559. img->dri_components = image->dri_components;
  560. img->loader_private = loaderPrivate;
  561. return img;
  562. }
  563. static GLboolean
  564. dri2_validate_usage(__DRIimage *image, unsigned int use)
  565. {
  566. /*
  567. * Gallium drivers are bad at adding usages to the resources
  568. * once opened again in another process, which is the main use
  569. * case for this, so we have to lie.
  570. */
  571. if (image != NULL)
  572. return GL_TRUE;
  573. else
  574. return GL_FALSE;
  575. }
  576. static __DRIimage *
  577. dri2_from_names(__DRIscreen *screen, int width, int height, int format,
  578. int *names, int num_names, int *strides, int *offsets,
  579. void *loaderPrivate)
  580. {
  581. __DRIimage *img;
  582. int stride, dri_components;
  583. if (num_names != 1)
  584. return NULL;
  585. if (offsets[0] != 0)
  586. return NULL;
  587. switch(format) {
  588. case __DRI_IMAGE_FOURCC_RGB565:
  589. format = __DRI_IMAGE_FORMAT_RGB565;
  590. dri_components = __DRI_IMAGE_COMPONENTS_RGB;
  591. break;
  592. case __DRI_IMAGE_FOURCC_ARGB8888:
  593. format = __DRI_IMAGE_FORMAT_ARGB8888;
  594. dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
  595. break;
  596. case __DRI_IMAGE_FOURCC_XRGB8888:
  597. format = __DRI_IMAGE_FORMAT_XRGB8888;
  598. dri_components = __DRI_IMAGE_COMPONENTS_RGB;
  599. break;
  600. case __DRI_IMAGE_FOURCC_ABGR8888:
  601. format = __DRI_IMAGE_FORMAT_ABGR8888;
  602. dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
  603. break;
  604. case __DRI_IMAGE_FOURCC_XBGR8888:
  605. format = __DRI_IMAGE_FORMAT_XBGR8888;
  606. dri_components = __DRI_IMAGE_COMPONENTS_RGB;
  607. break;
  608. default:
  609. return NULL;
  610. }
  611. /* Strides are in bytes not pixels. */
  612. stride = strides[0] /4;
  613. img = dri2_create_image_from_name(screen, width, height, format,
  614. names[0], stride, loaderPrivate);
  615. if (img == NULL)
  616. return NULL;
  617. img->dri_components = dri_components;
  618. return img;
  619. }
  620. static __DRIimage *
  621. dri2_from_planar(__DRIimage *image, int plane, void *loaderPrivate)
  622. {
  623. __DRIimage *img;
  624. if (plane != 0)
  625. return NULL;
  626. if (image->dri_components == 0)
  627. return NULL;
  628. img = dri2_dup_image(image, loaderPrivate);
  629. if (img == NULL)
  630. return NULL;
  631. /* set this to 0 for sub images. */
  632. img->dri_components = 0;
  633. return img;
  634. }
  635. static void
  636. dri2_destroy_image(__DRIimage *img)
  637. {
  638. pipe_resource_reference(&img->texture, NULL);
  639. FREE(img);
  640. }
  641. static struct __DRIimageExtensionRec dri2ImageExtension = {
  642. { __DRI_IMAGE, 5 },
  643. dri2_create_image_from_name,
  644. dri2_create_image_from_renderbuffer,
  645. dri2_destroy_image,
  646. dri2_create_image,
  647. dri2_query_image,
  648. dri2_dup_image,
  649. dri2_validate_usage,
  650. dri2_from_names,
  651. dri2_from_planar,
  652. };
  653. /*
  654. * Backend function init_screen.
  655. */
  656. static const __DRIextension *dri_screen_extensions[] = {
  657. &driTexBufferExtension.base,
  658. &dri2FlushExtension.base,
  659. &dri2ImageExtension.base,
  660. &dri2ConfigQueryExtension.base,
  661. NULL
  662. };
  663. static const __DRIextension *dri_screen_extensions_throttle[] = {
  664. &driTexBufferExtension.base,
  665. &dri2FlushExtension.base,
  666. &dri2ImageExtension.base,
  667. &dri2ConfigQueryExtension.base,
  668. &dri2ThrottleExtension.base,
  669. NULL
  670. };
  671. /**
  672. * This is the driver specific part of the createNewScreen entry point.
  673. *
  674. * Returns the struct gl_config supported by this driver.
  675. */
  676. static const __DRIconfig **
  677. dri2_init_screen(__DRIscreen * sPriv)
  678. {
  679. const __DRIconfig **configs;
  680. struct dri_screen *screen;
  681. struct pipe_screen *pscreen;
  682. const struct drm_conf_ret *throttle_ret = NULL;
  683. screen = CALLOC_STRUCT(dri_screen);
  684. if (!screen)
  685. return NULL;
  686. screen->sPriv = sPriv;
  687. screen->fd = sPriv->fd;
  688. sPriv->driverPrivate = (void *)screen;
  689. pscreen = driver_descriptor.create_screen(screen->fd);
  690. if (driver_descriptor.configuration)
  691. throttle_ret = driver_descriptor.configuration(DRM_CONF_THROTTLE);
  692. if (throttle_ret && throttle_ret->val.val_int != -1) {
  693. sPriv->extensions = dri_screen_extensions_throttle;
  694. screen->default_throttle_frames = throttle_ret->val.val_int;
  695. } else
  696. sPriv->extensions = dri_screen_extensions;
  697. /* dri_init_screen_helper checks pscreen for us */
  698. configs = dri_init_screen_helper(screen, pscreen, 32);
  699. if (!configs)
  700. goto fail;
  701. sPriv->api_mask = 0;
  702. if (screen->st_api->profile_mask & ST_PROFILE_DEFAULT_MASK)
  703. sPriv->api_mask |= 1 << __DRI_API_OPENGL;
  704. if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES1_MASK)
  705. sPriv->api_mask |= 1 << __DRI_API_GLES;
  706. if (screen->st_api->profile_mask & ST_PROFILE_OPENGL_ES2_MASK)
  707. sPriv->api_mask |= 1 << __DRI_API_GLES2;
  708. screen->auto_fake_front = dri_with_format(sPriv);
  709. screen->broken_invalidate = !sPriv->dri2.useInvalidate;
  710. screen->lookup_egl_image = dri2_lookup_egl_image;
  711. return configs;
  712. fail:
  713. dri_destroy_screen_helper(screen);
  714. FREE(screen);
  715. return NULL;
  716. }
  717. static boolean
  718. dri2_create_buffer(__DRIscreen * sPriv,
  719. __DRIdrawable * dPriv,
  720. const struct gl_config * visual, boolean isPixmap)
  721. {
  722. struct dri_drawable *drawable = NULL;
  723. if (!dri_create_buffer(sPriv, dPriv, visual, isPixmap))
  724. return FALSE;
  725. drawable = dPriv->driverPrivate;
  726. drawable->allocate_textures = dri2_allocate_textures;
  727. drawable->flush_frontbuffer = dri2_flush_frontbuffer;
  728. drawable->update_tex_buffer = dri2_update_tex_buffer;
  729. return TRUE;
  730. }
  731. /**
  732. * DRI driver virtual function table.
  733. *
  734. * DRI versions differ in their implementation of init_screen and swap_buffers.
  735. */
  736. const struct __DriverAPIRec driDriverAPI = {
  737. .InitScreen = dri2_init_screen,
  738. .DestroyScreen = dri_destroy_screen,
  739. .CreateContext = dri_create_context,
  740. .DestroyContext = dri_destroy_context,
  741. .CreateBuffer = dri2_create_buffer,
  742. .DestroyBuffer = dri_destroy_buffer,
  743. .MakeCurrent = dri_make_current,
  744. .UnbindContext = dri_unbind_context,
  745. .AllocateBuffer = dri2_allocate_buffer,
  746. .ReleaseBuffer = dri2_release_buffer,
  747. };
  748. /* This is the table of extensions that the loader will dlsym() for. */
  749. PUBLIC const __DRIextension *__driDriverExtensions[] = {
  750. &driCoreExtension.base,
  751. &driDRI2Extension.base,
  752. NULL
  753. };
  754. /* vim: set sw=3 ts=8 sts=3 expandtab: */