Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Mesa 3-D graphics library
  3.  *
  4.  * Copyright (C) 2010-2011 LunarG Inc.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included
  14.  * in all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22.  * DEALINGS IN THE SOFTWARE.
  23.  *
  24.  * Authors:
  25.  *    Chia-I Wu <olv@lunarg.com>
  26.  */
  27.  
  28. #include "pipe/p_compiler.h"
  29. #include "pipe/p_state.h"
  30. #include "util/u_memory.h"
  31. #include "util/u_format.h"
  32. #include "state_tracker/sw_winsys.h"
  33.  
  34. #include <hardware/gralloc.h>
  35. #include <utils/Errors.h>
  36.  
  37. #if ANDROID_VERSION < 0x0300
  38. #include <private/ui/sw_gralloc_handle.h>
  39. #endif
  40.  
  41. #include "android_sw_winsys.h"
  42.  
  43. struct android_sw_winsys
  44. {
  45.    struct sw_winsys base;
  46.  
  47.    const gralloc_module_t *grmod;
  48. };
  49.  
  50. struct android_sw_displaytarget
  51. {
  52.    buffer_handle_t handle;
  53.    int stride;
  54.    int width, height;
  55.    int usage; /* gralloc usage */
  56.  
  57.    void *mapped;
  58. };
  59.  
  60. static INLINE struct android_sw_winsys *
  61. android_sw_winsys(struct sw_winsys *ws)
  62. {
  63.    return (struct android_sw_winsys *) ws;
  64. }
  65.  
  66. static INLINE struct android_sw_displaytarget *
  67. android_sw_displaytarget(struct sw_displaytarget *dt)
  68. {
  69.    return (struct android_sw_displaytarget *) dt;
  70. }
  71.  
  72. namespace android {
  73.  
  74. static void
  75. android_displaytarget_display(struct sw_winsys *ws,
  76.                               struct sw_displaytarget *dt,
  77.                               void *context_private)
  78. {
  79. }
  80.  
  81. static struct sw_displaytarget *
  82. android_displaytarget_create(struct sw_winsys *ws,
  83.                              unsigned tex_usage,
  84.                              enum pipe_format format,
  85.                              unsigned width, unsigned height,
  86.                              unsigned alignment,
  87.                              unsigned *stride)
  88. {
  89.    return NULL;
  90. }
  91.  
  92. static void
  93. android_displaytarget_destroy(struct sw_winsys *ws,
  94.                               struct sw_displaytarget *dt)
  95. {
  96.    struct android_sw_displaytarget *adt = android_sw_displaytarget(dt);
  97.  
  98.    assert(!adt->mapped);
  99.    FREE(adt);
  100. }
  101.  
  102. static void
  103. android_displaytarget_unmap(struct sw_winsys *ws,
  104.                             struct sw_displaytarget *dt)
  105. {
  106.    struct android_sw_winsys *droid = android_sw_winsys(ws);
  107.    struct android_sw_displaytarget *adt = android_sw_displaytarget(dt);
  108.  
  109. #if ANDROID_VERSION < 0x0300
  110.    /* try sw_gralloc first */
  111.    if (adt->mapped && sw_gralloc_handle_t::validate(adt->handle) >= 0) {
  112.       adt->mapped = NULL;
  113.       return;
  114.    }
  115. #endif
  116.  
  117.    if (adt->mapped) {
  118.       droid->grmod->unlock(droid->grmod, adt->handle);
  119.       adt->mapped = NULL;
  120.    }
  121. }
  122.  
  123. static void *
  124. android_displaytarget_map(struct sw_winsys *ws,
  125.                           struct sw_displaytarget *dt,
  126.                           unsigned flags)
  127. {
  128.    struct android_sw_winsys *droid = android_sw_winsys(ws);
  129.    struct android_sw_displaytarget *adt = android_sw_displaytarget(dt);
  130.  
  131. #if ANDROID_VERSION < 0x0300
  132.    /* try sw_gralloc first */
  133.    if (sw_gralloc_handle_t::validate(adt->handle) >= 0) {
  134.       const sw_gralloc_handle_t *swhandle =
  135.          reinterpret_cast<const sw_gralloc_handle_t *>(adt->handle);
  136.       adt->mapped = reinterpret_cast<void *>(swhandle->base);
  137.  
  138.       return adt->mapped;
  139.    }
  140. #endif
  141.  
  142.    if (!adt->mapped) {
  143.       /* lock the buffer for CPU access */
  144.       droid->grmod->lock(droid->grmod, adt->handle,
  145.             adt->usage, 0, 0, adt->width, adt->height, &adt->mapped);
  146.    }
  147.  
  148.    return adt->mapped;
  149. }
  150.  
  151. static struct sw_displaytarget *
  152. android_displaytarget_from_handle(struct sw_winsys *ws,
  153.                                   const struct pipe_resource *templ,
  154.                                   struct winsys_handle *whandle,
  155.                                   unsigned *stride)
  156. {
  157.    struct android_winsys_handle *ahandle =
  158.       (struct android_winsys_handle *) whandle;
  159.    struct android_sw_displaytarget *adt;
  160.  
  161.    adt = CALLOC_STRUCT(android_sw_displaytarget);
  162.    if (!adt)
  163.       return NULL;
  164.  
  165.    adt->handle = ahandle->handle;
  166.    adt->stride = ahandle->stride;
  167.    adt->width = templ->width0;
  168.    adt->height = templ->height0;
  169.  
  170.    if (templ->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_TRANSFER_WRITE))
  171.       adt->usage |= GRALLOC_USAGE_SW_WRITE_OFTEN;
  172.    if (templ->bind & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_TRANSFER_READ))
  173.       adt->usage |= GRALLOC_USAGE_SW_READ_OFTEN;
  174.  
  175.    if (stride)
  176.       *stride = adt->stride;
  177.  
  178.    return reinterpret_cast<struct sw_displaytarget *>(adt);
  179. }
  180.  
  181. static boolean
  182. android_displaytarget_get_handle(struct sw_winsys *ws,
  183.                                  struct sw_displaytarget *dt,
  184.                                  struct winsys_handle *whandle)
  185. {
  186.    return FALSE;
  187. }
  188.  
  189. static boolean
  190. android_is_displaytarget_format_supported(struct sw_winsys *ws,
  191.                                           unsigned tex_usage,
  192.                                           enum pipe_format format)
  193. {
  194.    struct android_sw_winsys *droid = android_sw_winsys(ws);
  195.    int fmt = -1;
  196.  
  197.    switch (format) {
  198.    case PIPE_FORMAT_R8G8B8A8_UNORM:
  199.       fmt = HAL_PIXEL_FORMAT_RGBA_8888;
  200.       break;
  201.    case PIPE_FORMAT_R8G8B8X8_UNORM:
  202.       fmt = HAL_PIXEL_FORMAT_RGBX_8888;
  203.       break;
  204.    case PIPE_FORMAT_R8G8B8_UNORM:
  205.       fmt = HAL_PIXEL_FORMAT_RGB_888;
  206.       break;
  207.    case PIPE_FORMAT_B5G6R5_UNORM:
  208.       fmt = HAL_PIXEL_FORMAT_RGB_565;
  209.       break;
  210.    case PIPE_FORMAT_B8G8R8A8_UNORM:
  211.       fmt = HAL_PIXEL_FORMAT_BGRA_8888;
  212.       break;
  213.    default:
  214.       break;
  215.    }
  216.  
  217.    return (fmt != -1);
  218. }
  219.  
  220. static void
  221. android_destroy(struct sw_winsys *ws)
  222. {
  223.    struct android_sw_winsys *droid = android_sw_winsys(ws);
  224.  
  225.    FREE(droid);
  226. }
  227.  
  228. }; /* namespace android */
  229.  
  230. using namespace android;
  231.  
  232. struct sw_winsys *
  233. android_create_sw_winsys(void)
  234. {
  235.    struct android_sw_winsys *droid;
  236.    const hw_module_t *mod;
  237.  
  238.    droid = CALLOC_STRUCT(android_sw_winsys);
  239.    if (!droid)
  240.       return NULL;
  241.  
  242.    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod)) {
  243.       FREE(droid);
  244.       return NULL;
  245.    }
  246.  
  247.    droid->grmod = (const gralloc_module_t *) mod;
  248.  
  249.    droid->base.destroy = android_destroy;
  250.    droid->base.is_displaytarget_format_supported =
  251.       android_is_displaytarget_format_supported;
  252.  
  253.    droid->base.displaytarget_create = android_displaytarget_create;
  254.    droid->base.displaytarget_destroy = android_displaytarget_destroy;
  255.    droid->base.displaytarget_from_handle = android_displaytarget_from_handle;
  256.    droid->base.displaytarget_get_handle = android_displaytarget_get_handle;
  257.  
  258.    droid->base.displaytarget_map = android_displaytarget_map;
  259.    droid->base.displaytarget_unmap = android_displaytarget_unmap;
  260.    droid->base.displaytarget_display = android_displaytarget_display;
  261.  
  262.    return &droid->base;
  263. }
  264.