]> Pileus Git - ~andy/gtk/blob - gdk/directfb/gdkpixmap-directfb.c
gdk/directfb/gdkdirectfb.h gdk/directfb/gdkdisplay-directfb.c
[~andy/gtk] / gdk / directfb / gdkpixmap-directfb.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-1999 Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.
24  */
25
26 /*
27  * GTK+ DirectFB backend
28  * Copyright (C) 2001-2002  convergence integrated media GmbH
29  * Copyright (C) 2002-2004  convergence GmbH
30  * Written by Denis Oliver Kropp <dok@convergence.de> and
31  *            Sven Neumann <sven@convergence.de>
32  */
33
34 #include <config.h>
35 #include "gdk.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "gdkdirectfb.h"
41 #include "gdkprivate-directfb.h"
42
43 #include "gdkinternals.h"
44
45 #include "gdkpixmap.h"
46 #include "gdkalias.h"
47
48
49 static void gdk_pixmap_impl_directfb_init       (GdkPixmapImplDirectFB      *pixmap);
50 static void gdk_pixmap_impl_directfb_class_init (GdkPixmapImplDirectFBClass *klass);
51 static void gdk_pixmap_impl_directfb_finalize   (GObject                    *object);
52
53
54 static gpointer parent_class = NULL;
55
56
57 GType
58 gdk_pixmap_impl_directfb_get_type (void)
59 {
60   static GType object_type = 0;
61
62   if (!object_type)
63     {
64       static const GTypeInfo object_info =
65         {
66           sizeof (GdkPixmapImplDirectFBClass),
67           (GBaseInitFunc) NULL,
68           (GBaseFinalizeFunc) NULL,
69           (GClassInitFunc) gdk_pixmap_impl_directfb_class_init,
70           NULL,           /* class_finalize */
71           NULL,           /* class_data */
72           sizeof (GdkPixmapImplDirectFB),
73           0,              /* n_preallocs */
74           (GInstanceInitFunc) gdk_pixmap_impl_directfb_init,
75         };
76
77       object_type = g_type_register_static (GDK_TYPE_DRAWABLE_IMPL_DIRECTFB,
78                                             "GdkPixmapImplDirectFB",
79                                             &object_info, 0);
80     }
81
82   return object_type;
83 }
84
85 GType
86 _gdk_pixmap_impl_get_type (void)
87 {
88   return gdk_pixmap_impl_directfb_get_type ();
89 }
90
91 static void
92 gdk_pixmap_impl_directfb_init (GdkPixmapImplDirectFB *impl)
93 {
94   GdkDrawableImplDirectFB *draw_impl = GDK_DRAWABLE_IMPL_DIRECTFB (impl);
95   draw_impl->width  = 1;
96   draw_impl->height = 1;
97 }
98
99 static void
100 gdk_pixmap_impl_directfb_class_init (GdkPixmapImplDirectFBClass *klass)
101 {
102   GObjectClass *object_class = G_OBJECT_CLASS (klass);
103
104   parent_class = g_type_class_peek_parent (klass);
105
106   object_class->finalize = gdk_pixmap_impl_directfb_finalize;
107 }
108
109 static void
110 gdk_pixmap_impl_directfb_finalize (GObject *object)
111 {
112   if (G_OBJECT_CLASS (parent_class)->finalize)
113     G_OBJECT_CLASS (parent_class)->finalize (object);
114 }
115
116 GdkPixmap*
117 gdk_pixmap_new (GdkDrawable *drawable,
118                 gint       width,
119                 gint       height,
120                 gint       depth)
121 {
122   DFBSurfacePixelFormat    format;
123   IDirectFBSurface        *surface;
124   GdkPixmap               *pixmap;
125   GdkDrawableImplDirectFB *draw_impl;
126
127   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
128   g_return_val_if_fail (drawable != NULL || depth != -1, NULL);
129   g_return_val_if_fail (width > 0 && height > 0, NULL);
130
131   if (!drawable)
132     drawable = _gdk_parent_root;
133
134   if (GDK_IS_WINDOW (drawable) && GDK_WINDOW_DESTROYED (drawable))
135     return NULL;
136
137   GDK_NOTE (MISC, g_print ("gdk_pixmap_new: %dx%dx%d\n",
138                            width, height, depth));
139
140   if (depth == -1)
141     {
142       draw_impl =
143         GDK_DRAWABLE_IMPL_DIRECTFB (GDK_WINDOW_OBJECT (drawable)->impl);
144
145       g_return_val_if_fail (draw_impl != NULL, NULL);
146
147       draw_impl->surface->GetPixelFormat (draw_impl->surface, &format);
148       depth = DFB_BITS_PER_PIXEL (format);
149     }
150   else
151     {
152       switch (depth)
153         {
154         case  1:
155           format = DSPF_A8;
156           break;
157         case  8:
158           format = DSPF_LUT8;
159           break;
160         case 15:
161           format = DSPF_ARGB1555;
162           break;
163         case 16:
164           format = DSPF_RGB16;
165           break;
166         case 24:
167           format = DSPF_RGB24;
168           break;
169         case 32:
170           format = DSPF_RGB32;
171           break;
172         default:
173           g_message ("unimplemented %s for depth %d", __FUNCTION__, depth);
174           return NULL;
175         }
176     }
177
178   if( !(surface = 
179         gdk_display_dfb_create_surface(_gdk_display,format,width,height) )) { 
180     g_assert( surface != NULL);
181     return NULL;
182   }
183
184   pixmap = g_object_new (gdk_pixmap_get_type (), NULL);
185   draw_impl = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl);
186   draw_impl->surface = surface;
187   surface->Clear (surface, 0x0, 0x0, 0x0, 0x0);
188   surface->GetSize (surface, &draw_impl->width, &draw_impl->height);
189   surface->GetPixelFormat (surface, &draw_impl->format);
190
191   draw_impl->abs_x = draw_impl->abs_y = 0;
192
193   GDK_PIXMAP_OBJECT (pixmap)->depth = depth;
194
195   return pixmap;
196 }
197
198 GdkPixmap *
199 gdk_bitmap_create_from_data (GdkDrawable   *drawable,
200                              const gchar *data,
201                              gint         width,
202                              gint         height)
203 {
204   GdkPixmap *pixmap;
205
206   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
207   g_return_val_if_fail (data != NULL, NULL);
208   g_return_val_if_fail (width > 0 && height > 0, NULL);
209
210   GDK_NOTE (MISC, g_print ("gdk_bitmap_create_from_data: %dx%d\n",
211                            width, height));
212
213   pixmap = gdk_pixmap_new (drawable, width, height, 1);
214
215 #define GET_PIXEL(data,pixel) \
216   ((data[(pixel / 8)] & (0x1 << ((pixel) % 8))) >> ((pixel) % 8))
217
218   if (pixmap)
219     {
220       guchar *dst;
221       gint    pitch;
222
223       IDirectFBSurface *surface;
224
225       surface = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl)->surface;
226
227       if (surface->Lock( surface, DSLF_WRITE, (void**)(&dst), &pitch ) == DFB_OK)
228         {
229           gint i, j;
230
231           for (i = 0; i < height; i++)
232             {
233               for (j = 0; j < width; j++)
234                 {
235                   dst[j] = GET_PIXEL (data, j) * 255;
236                 }
237
238               data += (width + 7) / 8;
239               dst += pitch;
240             }
241
242           surface->Unlock( surface );
243         }
244     }
245
246 #undef GET_PIXEL
247
248   return pixmap;
249 }
250
251 GdkPixmap*
252 gdk_pixmap_create_from_data (GdkDrawable   *drawable,
253                              const gchar *data,
254                              gint         width,
255                              gint         height,
256                              gint         depth,
257                              const GdkColor    *fg,
258                              const GdkColor    *bg)
259 {
260   GdkPixmap *pixmap;
261
262   g_return_val_if_fail (drawable == NULL || GDK_IS_DRAWABLE (drawable), NULL);
263   g_return_val_if_fail (data != NULL, NULL);
264   g_return_val_if_fail (drawable != NULL || depth > 0, NULL);
265   g_return_val_if_fail (width > 0 && height > 0, NULL);
266
267   GDK_NOTE (MISC, g_print ("gdk_pixmap_create_from_data: %dx%dx%d\n",
268                            width, height, depth));
269
270   pixmap = gdk_pixmap_new (drawable, width, height, depth);
271
272   if (pixmap)
273     {
274       IDirectFBSurface *surface;
275       gchar            *dst;
276       gint              pitch;
277       gint              src_pitch;
278
279       depth = gdk_drawable_get_depth (pixmap);
280       src_pitch = width * ((depth + 7) / 8);
281
282       surface = GDK_DRAWABLE_IMPL_DIRECTFB (GDK_PIXMAP_OBJECT (pixmap)->impl)->surface;
283
284       if (surface->Lock( surface,
285                          DSLF_WRITE, (void**)(&dst), &pitch ) == DFB_OK)
286         {
287           gint i;
288
289           for (i = 0; i < height; i++)
290             {
291               memcpy (dst, data, src_pitch);
292               dst += pitch;
293               data += src_pitch;
294             }
295
296           surface->Unlock( surface );
297         }
298     }
299
300   return pixmap;
301 }
302
303 GdkPixmap*
304 gdk_pixmap_foreign_new (GdkNativeWindow anid)
305 {
306   g_warning(" gdk_pixmap_foreign_new unsuporrted \n");
307   return NULL;
308 }
309
310 GdkPixmap*
311 gdk_pixmap_foreign_new_for_display (GdkDisplay *display, GdkNativeWindow anid)
312 {
313   return gdk_pixmap_foreign_new(anid);
314 }
315
316 GdkPixmap*
317 gdk_pixmap_foreign_new_for_screen (GdkScreen       *screen,
318                                    GdkNativeWindow  anid,
319                                    gint             width,
320                                    gint             height,
321                                    gint             depth)
322 {
323   /*Use the root drawable for now since only one screen */
324   return gdk_pixmap_new(NULL,width,height,depth);
325 }
326
327
328 GdkPixmap*
329 gdk_pixmap_lookup (GdkNativeWindow anid)
330 {
331   g_warning(" gdk_pixmap_lookup unsuporrted \n");
332   return NULL;
333 }
334
335 GdkPixmap* gdk_pixmap_lookup_for_display (GdkDisplay *display,GdkNativeWindow anid)
336 {
337   return gdk_pixmap_lookup (anid);
338 }
339
340 #define __GDK_PIXMAP_X11_C__
341 #include "gdkaliasdef.c"