#include "evas_common_private.h"
#include "evas_engine.h"

void
evas_software_gdi_outbuf_init(void)
{
}

void
evas_software_gdi_outbuf_free(Outbuf *buf)
{
   if (!buf)
     return;

   while (buf->priv.pending_writes)
     {
	RGBA_Image *im;
	Outbuf_Region *obr;

	im = buf->priv.pending_writes->data;
	buf->priv.pending_writes = eina_list_remove_list(buf->priv.pending_writes, buf->priv.pending_writes);
	obr = im->extended_info;
	evas_cache_image_drop(&im->cache_entry);
	if (obr->gdiob)
          evas_software_gdi_output_buffer_free(obr->gdiob);
/* 	if (obr->mxob) _unfind_xob(obr->mxob, 0); */
	free(obr);
     }
   evas_software_gdi_outbuf_idle_flush(buf);
   evas_software_gdi_outbuf_flush(buf, NULL, NULL, MODE_FULL);

   evas_software_gdi_shutdown(buf);
   free(buf);
}

Outbuf *
evas_software_gdi_outbuf_setup(int          width,
                               int          height,
                               int          rotation,
                               HWND         window,
                               unsigned int borderless,
                               unsigned int fullscreen,
                               unsigned int region,
                               int          mask_dither,
                               int          destination_alpha)
{
   Outbuf *buf;

   buf = (Outbuf *)calloc(1, sizeof(Outbuf));
   if (!buf)
      return NULL;

   buf->width = width;
   buf->height = height;
   buf->rot = rotation;

   buf->priv.mask_dither = mask_dither;
   buf->priv.destination_alpha = destination_alpha;

   if (!evas_software_gdi_init(window, borderless, fullscreen, region, buf))
     {
        free(buf);
        return NULL;
     }

   {
      Gfx_Func_Convert  conv_func;
      Gdi_Output_Buffer *gdiob;

      gdiob = evas_software_gdi_output_buffer_new(buf->priv.gdi.dc, buf->priv.gdi.bitmap_info, 1, 1, NULL);

      conv_func = NULL;
      if (gdiob)
        {
           if ((rotation == 0) || (rotation == 180))
             conv_func = evas_common_convert_func_get(0,
                                                      width,
                                                      height,
                                                      32,
                                                      buf->priv.gdi.bitmap_info->masks[0],
                                                      buf->priv.gdi.bitmap_info->masks[1],
                                                      buf->priv.gdi.bitmap_info->masks[2],
                                                      PAL_MODE_NONE,
                                                      rotation);
           else if ((rotation == 90) || (rotation == 270))
             conv_func = evas_common_convert_func_get(0,
                                                      height,
                                                      width,
                                                      32,
                                                      buf->priv.gdi.bitmap_info->masks[0],
                                                      buf->priv.gdi.bitmap_info->masks[1],
                                                      buf->priv.gdi.bitmap_info->masks[2],
                                                      PAL_MODE_NONE,
                                                      rotation);

           evas_software_gdi_output_buffer_free(gdiob);

           if (!conv_func)
             {
                ERR(".[ soft_gdi engine Error ]."
                      " {"
                      "  At depth         32:"
                      "  RGB format mask: %08lx, %08lx, %08lx"
                      "  Not supported by and compiled in converters!"
                      " }",
                        buf->priv.gdi.bitmap_info->masks[0],
                        buf->priv.gdi.bitmap_info->masks[1],
                        buf->priv.gdi.bitmap_info->masks[2]);
             }
        }
   }

   return buf;
}

void
evas_software_gdi_outbuf_reconfigure(Outbuf      *buf,
                                     int          width,
                                     int          height,
                                     int          rotation,
                                     Outbuf_Depth depth EINA_UNUSED)
{
   if ((width == buf->width) && (height == buf->height) &&
       (rotation == buf->rot))
     return;
   buf->width = width;
   buf->height = height;
   buf->rot = rotation;
   evas_software_gdi_bitmap_resize(buf);
   buf->priv.region_built = 0;
}

void *
evas_software_gdi_outbuf_new_region_for_update(Outbuf *buf,
                                               int     x,
                                               int     y,
                                               int     w,
                                               int     h,
                                               int    *cx,
                                               int    *cy,
                                               int    *cw,
                                               int    *ch)
{
   RGBA_Image    *im;
   Outbuf_Region *obr;
   int            bpl = 0;
   int            alpha = 0;

   obr = calloc(1, sizeof(Outbuf_Region));
   obr->x = x;
   obr->y = y;
   obr->width = w;
   obr->height = h;
   *cx = 0;
   *cy = 0;
   *cw = w;
   *ch = h;

   alpha = ((buf->priv.gdi.region) || (buf->priv.destination_alpha));

   if ((buf->rot == 0) &&
       (buf->priv.gdi.bitmap_info->masks[0] == 0xff0000) &&
       (buf->priv.gdi.bitmap_info->masks[1] == 0x00ff00) &&
       (buf->priv.gdi.bitmap_info->masks[2] == 0x0000ff))
     {
        obr->gdiob = evas_software_gdi_output_buffer_new(buf->priv.gdi.dc,
                                                         buf->priv.gdi.bitmap_info,
                                                         w, h, NULL);
        im = (RGBA_Image *)evas_cache_image_data(evas_common_image_cache_get(),
                                                 w, h,
                                                 (DATA32 *)evas_software_gdi_output_buffer_data(obr->gdiob, &bpl),
                                                 alpha, EVAS_COLORSPACE_ARGB8888);
        im->extended_info = obr;
/* 	if (buf->priv.gdi.mask) */
/* 	  obr->mgdiob = _find_gdiob(buf->priv.gdi.dc, */
/*                                     buf->priv.gdi.bitmap_info, */
/*                                     1, */
/*                                     w, h, NULL); */
     }
   else
     {
        im = (RGBA_Image *) evas_cache_image_empty(evas_common_image_cache_get());
        im->cache_entry.flags.alpha |= alpha ? 1 : 0;
        evas_cache_image_surface_alloc(&im->cache_entry, w, h);
        im->extended_info = obr;
        if ((buf->rot == 0) || (buf->rot == 180))
          obr->gdiob = evas_software_gdi_output_buffer_new(buf->priv.gdi.dc,
                                                           buf->priv.gdi.bitmap_info,
                                                           w, h, NULL);
        else if ((buf->rot == 90) || (buf->rot == 270))
          obr->gdiob = evas_software_gdi_output_buffer_new(buf->priv.gdi.dc,
                                                           buf->priv.gdi.bitmap_info,
                                                           h, w, NULL);
/* 	if (buf->priv.gdi.mask) */
/* 	  obr->mgdiob = _find_gdiob(buf->priv.gdi.dc, */
/*                                     buf->priv.gdi.bitmap_info, */
/*                                     1, */
/*                                     w, h, NULL); */
     }
   if ((buf->priv.gdi.region) || (buf->priv.destination_alpha))
     {
        /* FIXME: faster memset! */
        /* memset(im->image.data, 0, w * h * sizeof(DATA32)); */
     }

   buf->priv.pending_writes = eina_list_append(buf->priv.pending_writes, im);
   return im;
}

void
evas_software_gdi_outbuf_push_updated_region(Outbuf     *buf,
                                             RGBA_Image *update,
                                             int         x,
                                             int         y,
                                             int         w,
                                             int         h)
{
   HRGN             regions = NULL;
   Gfx_Func_Convert conv_func;
   Outbuf_Region   *obr;
   DATA32          *src_data;
   void            *data;
   int              bpl = 0;

   conv_func = NULL;
   obr = update->extended_info;

   if ((buf->rot == 0) || (buf->rot == 180))
     conv_func = evas_common_convert_func_get(0, w, h, 32,
                                              buf->priv.gdi.bitmap_info->masks[0],
                                              buf->priv.gdi.bitmap_info->masks[1],
                                              buf->priv.gdi.bitmap_info->masks[2],
                                              PAL_MODE_NONE,
                                              buf->rot);
   else if ((buf->rot == 90) || (buf->rot == 270))
     conv_func = evas_common_convert_func_get(0, h, w, 32,
                                              buf->priv.gdi.bitmap_info->masks[0],
                                              buf->priv.gdi.bitmap_info->masks[1],
                                              buf->priv.gdi.bitmap_info->masks[2],
                                              PAL_MODE_NONE,
                                              buf->rot);
   if (!conv_func) return;

   data = evas_software_gdi_output_buffer_data(obr->gdiob, &bpl);
   src_data = update->image.data;
   if (buf->rot == 0)
     {
	obr->x = x;
	obr->y = y;
     }
   else if (buf->rot == 90)
     {
	obr->x = y;
	obr->y = buf->width - x - w;
     }
   else if (buf->rot == 180)
     {
	obr->x = buf->width - x - w;
	obr->y = buf->height - y - h;
     }
   else if (buf->rot == 270)
     {
	obr->x = buf->height - y - h;
	obr->y = x;
     }
   if ((buf->rot == 0) || (buf->rot == 180))
     {
	obr->width = w;
	obr->height = h;
     }
   else if ((buf->rot == 90) || (buf->rot == 270))
     {
	obr->width = h;
	obr->height = w;
     }

   if (data != src_data)
     conv_func(src_data, data,
               0,
               bpl / 4 - obr->width,
               obr->width,
               obr->height,
               x,
               y,
               NULL);

   /* Region code */
   if (!buf->priv.gdi.region)
     {
        if (buf->priv.gdi.regions)
          DeleteObject(buf->priv.gdi.regions);
        buf->priv.gdi.regions = NULL;
        SetWindowRgn(buf->priv.gdi.window, NULL, 1);
        return;
     }

   if (!buf->priv.region_built)
     {
        RECT  rect;
        POINT pt = { 0, 0 };
        HRGN region;
        DATA32 *tmp;
        int i;
        int j;
        int dx;
        int dy;
        int xmin;
        int xmax;

        if (!GetClientRect(buf->priv.gdi.window, &rect))
          return;

        if (!GetWindowRect(buf->priv.gdi.window, &rect))
          return;
        if (!ClientToScreen(buf->priv.gdi.window, &pt))
          return;
        dx = x + pt.x - rect.left;
        dy = y + pt.y - rect.top;

        tmp = src_data;

        for (j = 0; j < h; j++)
          {
             i = 0;
             while (i < w)
               {
                  if ((*tmp & 0xff000000) == 0xff000000)
                    {
                       xmin = dx + i;
                       if ((i + 1) == w)
                         {
                            xmax = dx + i;
                            region = CreateRectRgn(xmin, dy + j, xmax + 1, dy + j + 1);
                            if (regions == NULL)
                              regions = region;
                            else
                              {
                                 CombineRgn(regions, regions, region, RGN_OR);
                                 DeleteObject(region);
                              }
                         }
                       else
                         {
                            i++;
                            tmp++;

                            while (i < w)
                              {
                                 if ((*tmp & 0xff000000) == 0xff000000)
                                   {
                                      if ((i + 1) == w)
                                        {
                                           xmax = dx + i;
                                           region = CreateRectRgn(xmin, dy + j, xmax + 1, dy + j + 1);
                                           if (regions == NULL)
                                             regions = region;
                                           else
                                             {
                                                CombineRgn(regions, regions, region, RGN_OR);
                                                DeleteObject(region);
                                             }
                                           break;
                                        }
                                   }
                                 else
                                   {
                                      xmax = dx + i - 1;
                                      region = CreateRectRgn(xmin, dy + j, xmax + 1, dy + j + 1);
                                      if (regions == NULL)
                                        regions = region;
                                      else
                                        {
                                           CombineRgn(regions, regions, region, RGN_OR);
                                           DeleteObject(region);
                                        }
                                      break;
                                   }
                                 i++;
                                 tmp++;
                              }
                         }
                    }
                  i++;
                  tmp++;
               }
          }

        if (!buf->priv.gdi.borderless)
          {
            RECT rnc;
            RECT rc;
            POINT pt = { 0, 0 };
            LONG ncw;
            LONG nch;
            LONG cw;
            LONG ch;

            if (!GetWindowRect(buf->priv.gdi.window, &rnc))
              return;
            if (!GetClientRect(buf->priv.gdi.window, &rc))
              return;
            if (!ClientToScreen(buf->priv.gdi.window, &pt))
              return;

            ncw = rnc.right - rnc.left;
            nch = rnc.bottom - rnc.top;
            cw = rc.right - rc.left;
            ch = rc.bottom - rc.top;

            region = CreateRectRgn(0, 0, ncw, pt.y - rnc.top);
            if (!regions)
              regions = region;
            else
              {
                 CombineRgn(regions, regions, region, RGN_OR);
                 DeleteObject(region);
              }
            region = CreateRectRgn(0, pt.y - rnc.top, pt.x - rnc.left, nch);
            CombineRgn(regions, regions, region, RGN_OR);
            DeleteObject(region);
            region = CreateRectRgn(pt.x - rnc.left, pt.y - rnc.top + ch, pt.x - rnc.left + cw, nch);
            CombineRgn(regions, regions, region, RGN_OR);
            DeleteObject(region);
            region = CreateRectRgn(pt.x - rnc.left + cw, pt.y - rnc.top, ncw, nch);
            CombineRgn(regions, regions, region, RGN_OR);
            DeleteObject(region);
          }

        if (regions)
          SetWindowRgn(buf->priv.gdi.window, regions, 1);
        buf->priv.gdi.regions = regions;

        buf->priv.region_built = 1;
     }
}

void
evas_software_gdi_outbuf_flush(Outbuf *buf, Tilebuf_Rect *surface_damage EINA_UNUSED, Tilebuf_Rect *buffer_damage EINA_UNUSED, Evas_Render_Mode render_mode)
{
   Eina_List     *l;
   RGBA_Image    *im;
   Outbuf_Region *obr;

   if (render_mode == EVAS_RENDER_MODE_ASYNC_INIT) return;

   /* copy safely the images that need to be drawn onto the back surface */
   EINA_LIST_FOREACH(buf->priv.pending_writes, l, im)
     {
	Gdi_Output_Buffer *gdiob;

        obr = im->extended_info;
        gdiob = obr->gdiob;
        evas_software_gdi_output_buffer_paste(gdiob,
                                              obr->x,
                                              obr->y);
/*         if (obr->mgdiob) */
/*           evas_software_gdi_output_buffer_paste(obr->mgdiob, */
/*                                                 buf->priv.x11.xlib.mask, */
/*                                                 buf->priv.x11.xlib.gcm, */
/*                                                 obr->x, obr->y, 0); */
     }

   while (buf->priv.prev_pending_writes)
     {
        im = buf->priv.prev_pending_writes->data;
        buf->priv.prev_pending_writes =
          eina_list_remove_list(buf->priv.prev_pending_writes,
                                buf->priv.prev_pending_writes);
        obr = im->extended_info;
        evas_cache_image_drop(&im->cache_entry);
        if (obr->gdiob)
          evas_software_gdi_output_buffer_free(obr->gdiob);
/*         if (obr->mgdiob) _unfind_gdiob(obr->mgdiob); */
        free(obr);
     }
   buf->priv.prev_pending_writes = buf->priv.pending_writes;
   buf->priv.pending_writes = NULL;

   evas_common_cpu_end_opt();
}

void
evas_software_gdi_outbuf_idle_flush(Outbuf *buf)
{
   while (buf->priv.prev_pending_writes)
     {
        RGBA_Image *im;
        Outbuf_Region *obr;

        im = buf->priv.prev_pending_writes->data;
        buf->priv.prev_pending_writes =
          eina_list_remove_list(buf->priv.prev_pending_writes,
                                buf->priv.prev_pending_writes);
        obr = im->extended_info;
        evas_cache_image_drop((Image_Entry *)im);
        if (obr->gdiob)
          evas_software_gdi_output_buffer_free(obr->gdiob);
/*         if (obr->mxob) _unfind_xob(obr->mxob, 0); */
        free(obr);
     }
}

int
evas_software_gdi_outbuf_width_get(Outbuf *buf)
{
   return buf->width;
}

int
evas_software_gdi_outbuf_height_get(Outbuf *buf)
{
   return buf->height;
}

int
evas_software_gdi_outbuf_rot_get(Outbuf *buf)
{
   return buf->rot;
}
