summaryrefslogtreecommitdiff
path: root/gdk/directfb/gdkpixmap-directfb.c
blob: 198b93ccda16ba21bab80a6c1b15e157e0b4dccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 * Copyright (C) 1998-1999 Tor Lillqvist
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.
 */

/*
 * GTK+ DirectFB backend
 * Copyright (C) 2001-2002  convergence integrated media GmbH
 * Copyright (C) 2002-2004  convergence GmbH
 * Written by Denis Oliver Kropp <dok@convergence.de> and
 *            Sven Neumann <sven@convergence.de>
 */

#include "config.h"
#include "gdk.h"

#include <stdlib.h>
#include <string.h>

#include "gdkdirectfb.h"
#include "gdkprivate-directfb.h"

#include "gdkinternals.h"

#include "gdkpixmap.h"
#include "gdkalias.h"


static void gdk_pixmap_impl_directfb_init       (GdkPixmapImplDirectFB      *pixmap);
static void gdk_pixmap_impl_directfb_class_init (GdkPixmapImplDirectFBClass *klass);
static void gdk_pixmap_impl_directfb_finalize   (GObject                    *object);


static gpointer parent_class = NULL;


GType
gdk_pixmap_impl_directfb_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
        {
          sizeof (GdkPixmapImplDirectFBClass),
          (GBaseInitFunc) NULL,
          (GBaseFinalizeFunc) NULL,
          (GClassInitFunc) gdk_pixmap_impl_directfb_class_init,
          NULL,           /* class_finalize */
          NULL,           /* class_data */
          sizeof (GdkPixmapImplDirectFB),
          0,              /* n_preallocs */
          (GInstanceInitFunc) gdk_pixmap_impl_directfb_init,
        };

      object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_DIRECTFB,
                                            "GdkPixmapImplDirectFB",
                                            &object_info, 0);
    }

  return object_type;
}

GType
_gdk_pixmap_impl_get_type (void)
{
  return gdk_pixmap_impl_directfb_get_type ();
}

static void
gdk_pixmap_impl_directfb_init (GdkPixmapImplDirectFB *impl)
{
  GdkDrawableImplDirectFB *draw_impl = GDK_DRAWABLE_IMPL_DIRECTFB (impl);
  draw_impl->width  = 1;
  draw_impl->height = 1;
}

static void
gdk_pixmap_impl_directfb_class_init (GdkPixmapImplDirectFBClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_pixmap_impl_directfb_finalize;
}

static void
gdk_pixmap_impl_directfb_finalize (GObject *object)
{
  if (G_OBJECT_CLASS (parent_class)->finalize)
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

GdkPixmap*
gdk_pixmap_new (GdkDrawable *drawable,
                gint       width,
                gint       height,
                gint       depth)
{
  DFBSurfacePixelFormat    format;
  IDirectFBSurface        *surface;
  GdkPixmap               *pixmap;
  GdkDrawableImplDirectFB *draw_impl;

  g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (drawable != NULL || depth != -1, NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  if (!drawable)
    drawable = _gdk_parent_root;

  if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
    return NULL;

  GDK_NOTE (MISC, g_print ("gdk_pixmap_new: %dx%dx%d\n",
                           width, height, depth));

  if (depth == -1)
    {
      draw_impl =
        GDK_DRAWABLE_IMPL_DIRECTFB (GDK_WINDOW_OBJECT (drawable)->impl);

      g_return_val_if_fail (draw_impl != NULL, NULL);

      draw_impl->surface->GetPixelFormat (draw_impl->surface, &format);
      depth = DFB_BITS_PER_PIXEL (format);
    }
  else
    {
      switch (depth)
        {
        case  1:
          format = DSPF_A8;
          break;
        case  8:
          format = DSPF_LUT8;
          break;
        case 15:
          format = DSPF_ARGB1555;
          break;
        case 16:
          format = DSPF_RGB16;
          break;
        case 24:
          format = DSPF_RGB24;
          break;
        case 32:
          format = DSPF_RGB32;
          break;
        default:
          g_message ("unimplemented %s for depth %d", __FUNCTION__, depth);
          return NULL;
        }
    }

  if( !(surface = 
	gdk_display_dfb_create_surface(_gdk_display,format,width,height) )) { 
    g_assert( surface != NULL);
    return NULL;
  }

  pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
  draw_impl = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl);
  draw_impl->surface = surface;
  surface->Clear (surface, 0x0, 0x0, 0x0, 0x0);
  surface->GetSize (surface, &draw_impl->width, &draw_impl->height);
  surface->GetPixelFormat (surface, &draw_impl->format);

  draw_impl->abs_x = draw_impl->abs_y = 0;

  GDK_PIXMAP_OBJECT (pixmap)->depth = depth;

  return pixmap;
}

GdkPixmap *
gdk_bitmap_create_from_data (GdkDrawable   *drawable,
                             const gchar *data,
                             gint         width,
                             gint         height)
{
  GdkPixmap *pixmap;

  g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (data != NULL, NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  GDK_NOTE (MISC, g_print ("gdk_bitmap_create_from_data: %dx%d\n",
                           width, height));

  pixmap = gdk_pixmap_new (drawable, width, height, 1);

#define GET_PIXEL(data,pixel) \
  ((data[(pixel / 8)] & (0x1 << ((pixel) % 8))) >> ((pixel) % 8))

  if (pixmap)
    {
      guchar *dst;
      gint    pitch;

      IDirectFBSurface *surface;

      surface = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl)->surface;

      if (surface->Lock( surface, DSLF_WRITE, (void**)(&dst), &pitch ) == DFB_OK)
        {
          gint i, j;

          for (i = 0; i < height; i++)
            {
	      for (j = 0; j < width; j++)
		{
		  dst[j] = GET_PIXEL (data, j) * 255;
		}

              data += (width + 7) / 8;
	      dst += pitch;
            }

          surface->Unlock( surface );
        }
    }

#undef GET_PIXEL

  return pixmap;
}

GdkPixmap*
gdk_pixmap_create_from_data (GdkDrawable   *drawable,
                             const gchar *data,
                             gint         width,
                             gint         height,
                             gint         depth,
                             const GdkColor    *fg,
                             const GdkColor    *bg)
{
  GdkPixmap *pixmap;

  g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (data != NULL, NULL);
  g_return_val_if_fail (drawable != NULL || depth > 0, NULL);
  g_return_val_if_fail (width > 0 && height > 0, NULL);

  GDK_NOTE (MISC, g_print ("gdk_pixmap_create_from_data: %dx%dx%d\n",
                           width, height, depth));

  pixmap = gdk_pixmap_new (drawable, width, height, depth);

  if (pixmap)
    {
      IDirectFBSurface *surface;
      gchar            *dst;
      gint              pitch;
      gint              src_pitch;

      depth = gdk_drawable_get_depth (pixmap);
      src_pitch = width * ((depth + 7) / 8);

      surface = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl)->surface;

      if (surface->Lock( surface,
                         DSLF_WRITE, (void**)(&dst), &pitch ) == DFB_OK)
        {
          gint i;

          for (i = 0; i < height; i++)
            {
              memcpy (dst, data, src_pitch);
              dst += pitch;
              data += src_pitch;
            }

          surface->Unlock( surface );
        }
    }

  return pixmap;
}

GdkPixmap*
gdk_pixmap_foreign_new (GdkNativeWindow anid)
{
  g_warning(" gdk_pixmap_foreign_new unsuporrted \n");
  return NULL;
}

GdkPixmap*
gdk_pixmap_foreign_new_for_display (GdkDisplay *display, GdkNativeWindow anid)
{
  return gdk_pixmap_foreign_new(anid);
}

GdkPixmap*
gdk_pixmap_foreign_new_for_screen (GdkScreen       *screen,
                                   GdkNativeWindow  anid,
                                   gint             width,
                                   gint             height,
                                   gint             depth)
{
  /*Use the root drawable for now since only one screen */
  return gdk_pixmap_new(NULL,width,height,depth);
}


GdkPixmap*
gdk_pixmap_lookup (GdkNativeWindow anid)
{
  g_warning(" gdk_pixmap_lookup unsuporrted \n");
  return NULL;
}

GdkPixmap* gdk_pixmap_lookup_for_display (GdkDisplay *display,GdkNativeWindow anid)
{
  return gdk_pixmap_lookup (anid);
}

#define __GDK_PIXMAP_X11_C__
#include "gdkaliasdef.c"