Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2009 Artur Wyszynski <harakash@gmail.com>
  4.  * Copyright 2013-2014 Alexander von Gluck IV <kallisti5@unixzen.com>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  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 NON-INFRINGEMENT. IN NO EVENT SHALL
  17.  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20.  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  *
  22.  * The above copyright notice and this permission notice (including the
  23.  * next paragraph) shall be included in all copies or substantial portions
  24.  * of the Software.
  25.  *
  26.  **************************************************************************/
  27.  
  28. #include <stdio.h>
  29.  
  30. #include "pipe/p_compiler.h"
  31. #include "pipe/p_defines.h"
  32. #include "pipe/p_format.h"
  33. #include "util/u_inlines.h"
  34. #include "util/u_format.h"
  35. #include "util/u_math.h"
  36. #include "util/u_memory.h"
  37. #include "state_tracker/st_api.h"
  38. #include "state_tracker/sw_winsys.h"
  39.  
  40. #include "bitmap_wrapper.h"
  41. #include "hgl_sw_winsys.h"
  42.  
  43.  
  44. #ifdef DEBUG
  45. #   define TRACE(x...) printf("hgl:winsys: " x)
  46. #   define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
  47. #else
  48. #   define TRACE(x...)
  49. #   define CALLED()
  50. #endif
  51. #define ERROR(x...) printf("hgl:winsys: " x)
  52.  
  53.  
  54. struct haiku_displaytarget
  55. {
  56.         enum pipe_format format;
  57.         color_space colorSpace;
  58.  
  59.         unsigned width;
  60.         unsigned height;
  61.         unsigned stride;
  62.  
  63.         unsigned size;
  64.  
  65.         void* data;
  66. };
  67.  
  68.  
  69. // Cast
  70. static INLINE struct haiku_displaytarget*
  71. hgl_sw_displaytarget(struct sw_displaytarget* target)
  72. {
  73.         return (struct haiku_displaytarget *)target;
  74. }
  75.  
  76.  
  77. static void
  78. hgl_winsys_destroy(struct sw_winsys* winsys)
  79. {
  80.         FREE(winsys);
  81. }
  82.  
  83.  
  84. static boolean
  85. hgl_winsys_is_displaytarget_format_supported(struct sw_winsys* winsys,
  86.         unsigned textureUsage, enum pipe_format format)
  87. {
  88.         // TODO STUB
  89.         return TRUE;
  90. }
  91.  
  92. static color_space
  93. hgl_winsys_convert_cs(enum pipe_format format)
  94. {
  95.         // TODO: B_RGB24, B_RGB16, B_RGB15?
  96.         switch(format) {
  97.                 case PIPE_FORMAT_B5G6R5_UNORM:
  98.                         return B_CMAP8;
  99.                 case PIPE_FORMAT_A8B8G8R8_UNORM:
  100.                 case PIPE_FORMAT_X8B8G8R8_UNORM:
  101.                 default:
  102.                         return B_RGB32;
  103.         }
  104. }
  105.  
  106. static struct sw_displaytarget*
  107. hgl_winsys_displaytarget_create(struct sw_winsys* winsys,
  108.         unsigned textureUsage, enum pipe_format format, unsigned width,
  109.         unsigned height, unsigned alignment, unsigned* stride)
  110. {
  111.         struct haiku_displaytarget* haikuDisplayTarget
  112.                 = CALLOC_STRUCT(haiku_displaytarget);
  113.         assert(haikuDisplayTarget);
  114.  
  115.         TRACE("%s: %d x %d\n", __func__, width, height);
  116.  
  117.         haikuDisplayTarget->colorSpace = hgl_winsys_convert_cs(format);
  118.         haikuDisplayTarget->format = format;
  119.         haikuDisplayTarget->width = width;
  120.         haikuDisplayTarget->height = height;
  121.  
  122.         size_t formatStride = util_format_get_stride(format, width);
  123.         unsigned blockSize = util_format_get_nblocksy(format, height);
  124.  
  125.         haikuDisplayTarget->stride = align(formatStride, alignment);
  126.         haikuDisplayTarget->size = haikuDisplayTarget->stride * blockSize;
  127.  
  128.         haikuDisplayTarget->data
  129.                 = align_malloc(haikuDisplayTarget->size, alignment);
  130.  
  131.         assert(haikuDisplayTarget->data);
  132.  
  133.         *stride = haikuDisplayTarget->stride;
  134.  
  135.         // Cast to ghost sw_displaytarget type
  136.         return (struct sw_displaytarget*)haikuDisplayTarget;
  137. }
  138.  
  139.  
  140. static void
  141. hgl_winsys_displaytarget_destroy(struct sw_winsys* winsys,
  142.         struct sw_displaytarget* displayTarget)
  143. {
  144.         struct haiku_displaytarget* haikuDisplayTarget
  145.                 = hgl_sw_displaytarget(displayTarget);
  146.  
  147.         if (!haikuDisplayTarget)
  148.                 return;
  149.  
  150.         if (haikuDisplayTarget->data != NULL)
  151.                 align_free(haikuDisplayTarget->data);
  152.  
  153.         FREE(haikuDisplayTarget);
  154. }
  155.  
  156.  
  157. static struct sw_displaytarget*
  158. hgl_winsys_displaytarget_from_handle(struct sw_winsys* winsys,
  159.         const struct pipe_resource* templat, struct winsys_handle* whandle,
  160.         unsigned* stride)
  161. {
  162.         return NULL;
  163. }
  164.  
  165.  
  166. static boolean
  167. hgl_winsys_displaytarget_get_handle(struct sw_winsys* winsys,
  168.         struct sw_displaytarget* displayTarget, struct winsys_handle* whandle)
  169. {
  170.         return FALSE;
  171. }
  172.  
  173.  
  174. static void*
  175. hgl_winsys_displaytarget_map(struct sw_winsys* winsys,
  176.         struct sw_displaytarget* displayTarget, unsigned flags)
  177. {
  178.         struct haiku_displaytarget* haikuDisplayTarget
  179.                 = hgl_sw_displaytarget(displayTarget);
  180.  
  181.         return haikuDisplayTarget->data;
  182. }
  183.  
  184.  
  185. static void
  186. hgl_winsys_displaytarget_unmap(struct sw_winsys* winsys,
  187.         struct sw_displaytarget* disptarget)
  188. {
  189.         return;
  190. }
  191.  
  192.  
  193. static void
  194. hgl_winsys_displaytarget_display(struct sw_winsys* winsys,
  195.         struct sw_displaytarget* displayTarget, void* contextPrivate,
  196.         struct pipe_box *box)
  197. {
  198.         assert(contextPrivate);
  199.  
  200.         Bitmap* bitmap = (Bitmap*)contextPrivate;
  201.  
  202.         struct haiku_displaytarget* haikuDisplayTarget
  203.                 = hgl_sw_displaytarget(displayTarget);
  204.  
  205.         import_bitmap_bits(bitmap, haikuDisplayTarget->data,
  206.                 haikuDisplayTarget->size, haikuDisplayTarget->stride,
  207.                 haikuDisplayTarget->colorSpace);
  208.  
  209.         // Dump the rendered bitmap to disk for debugging
  210.         //dump_bitmap(bitmap);
  211.  
  212.         return;
  213. }
  214.  
  215.  
  216. struct sw_winsys*
  217. hgl_create_sw_winsys()
  218. {
  219.         struct sw_winsys* winsys = CALLOC_STRUCT(sw_winsys);
  220.  
  221.         if (!winsys)
  222.                 return NULL;
  223.        
  224.         // Attach winsys hooks for Haiku
  225.         winsys->destroy = hgl_winsys_destroy;
  226.         winsys->is_displaytarget_format_supported
  227.                 = hgl_winsys_is_displaytarget_format_supported;
  228.         winsys->displaytarget_create = hgl_winsys_displaytarget_create;
  229.         winsys->displaytarget_from_handle = hgl_winsys_displaytarget_from_handle;
  230.         winsys->displaytarget_get_handle = hgl_winsys_displaytarget_get_handle;
  231.         winsys->displaytarget_map = hgl_winsys_displaytarget_map;
  232.         winsys->displaytarget_unmap = hgl_winsys_displaytarget_unmap;
  233.         winsys->displaytarget_display = hgl_winsys_displaytarget_display;
  234.         winsys->displaytarget_destroy = hgl_winsys_displaytarget_destroy;
  235.  
  236.         return winsys;
  237. }
  238.