OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkVolatileImage.c
1 /* gnu_java_awt_peer_gtk_VolatileImage.c
2    Copyright (C)  2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 #include "jcl.h"
39 #include "gtkpeer.h"
40 #include <gdk/gdkx.h>
41 #include <gdk/gdktypes.h>
42 #include <gdk/gdkprivate.h>
43 #include <gdk/gdkx.h>
44 #include <gdk-pixbuf/gdk-pixbuf.h>
45 #include <gdk-pixbuf/gdk-pixdata.h>
46
47 #include "gnu_java_awt_peer_gtk_GtkVolatileImage.h"
48 #include "cairographics2d.h"
49
50
51 /**
52  * Creates a cairo surface, ARGB32, native ordering, premultiplied alpha.
53  */
54 JNIEXPORT jlong JNICALL 
55 Java_gnu_java_awt_peer_gtk_GtkVolatileImage_init (JNIEnv *env, 
56                                                   jobject obj __attribute__ ((__unused__)), 
57                                                   jobject peer,
58                                                   jint width, jint height)
59 {
60   GtkWidget *widget = NULL;
61   GdkPixmap* pixmap;
62   void *ptr = NULL;
63
64   gdk_threads_enter();
65
66   if( peer != NULL )
67     {
68       ptr = NSA_GET_PTR (env, peer);
69       g_assert (ptr != NULL);
70       
71       widget = GTK_WIDGET (ptr);
72       g_assert (widget != NULL);
73       pixmap = gdk_pixmap_new( widget->window, width, height, -1 );
74     }
75   else
76     pixmap = gdk_pixmap_new( NULL, width, height, 
77                              gdk_rgb_get_visual()->depth );
78
79   gdk_threads_leave();
80
81   g_assert( pixmap != NULL );
82
83   return PTR_TO_JLONG( pixmap );
84 }
85
86 /**
87  * Destroy the surface
88  */
89 JNIEXPORT void JNICALL 
90 Java_gnu_java_awt_peer_gtk_GtkVolatileImage_destroy
91 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
92  jlong pointer)
93 {
94   GdkPixmap* pixmap = JLONG_TO_PTR(GdkPixmap, pointer);
95   if( pixmap != NULL )
96     {
97       gdk_threads_enter();
98       g_object_unref( pixmap );
99       gdk_threads_leave();
100     }
101 }
102
103 /**
104  * Gets all pixels in an array
105  */
106 JNIEXPORT jintArray JNICALL 
107 Java_gnu_java_awt_peer_gtk_GtkVolatileImage_nativeGetPixels
108 (JNIEnv *env, jobject obj, jlong pointer)
109 {
110   /* jint *pixeldata, *jpixdata; */
111   jint *jpixdata;
112   GdkPixmap *pixmap;
113   GdkPixbuf *pixbuf;
114   jintArray jpixels;
115   int width, height, depth, size;
116   jclass cls;
117   jfieldID field;
118   guchar *pixels;
119
120   cls = (*env)->GetObjectClass (env, obj);
121   field = (*env)->GetFieldID (env, cls, "width", "I");
122   g_assert (field != 0);
123   width = (*env)->GetIntField (env, obj, field);
124
125   field = (*env)->GetFieldID (env, cls, "height", "I");
126   g_assert (field != 0);
127   height = (*env)->GetIntField (env, obj, field);
128
129   pixmap = JLONG_TO_PTR(GdkPixmap, pointer);
130   g_assert(pixmap != NULL);
131
132   gdk_threads_enter();
133
134   /* get depth in bytes */
135   depth = gdk_drawable_get_depth( pixmap ) >> 3;
136   size = width * height;
137   jpixels = (*env)->NewIntArray ( env, size );
138   jpixdata = (*env)->GetIntArrayElements (env, jpixels, NULL);
139   
140   pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE, 8, width, height );
141   gdk_pixbuf_get_from_drawable( pixbuf, pixmap, NULL, 0, 0, 0, 0, width, height );
142   
143   if (pixbuf != NULL)
144     {
145       pixels = gdk_pixbuf_get_pixels(pixbuf);
146       memcpy (jpixdata, pixels, size * sizeof(jint));
147     }
148     
149   (*env)->ReleaseIntArrayElements (env, jpixels, jpixdata, 0);
150
151   gdk_threads_leave();
152
153   return jpixels;
154 }
155
156 /**
157  * Copy area
158  */
159 JNIEXPORT void JNICALL 
160 Java_gnu_java_awt_peer_gtk_GtkVolatileImage_nativeCopyArea
161 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
162  jlong pointer, jint x, jint y, jint w, jint h, jint dx, jint dy)
163 {
164   GdkPixbuf *pixbuf;
165   GdkPixmap* pixmap = JLONG_TO_PTR(GdkPixmap, pointer);
166
167   g_assert (pixmap != NULL);
168
169   gdk_threads_enter();
170   
171   pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE, 8, w, h );
172   gdk_pixbuf_get_from_drawable( pixbuf, pixmap, NULL, x, y, 0, 0, w, h );
173   gdk_draw_pixbuf (pixmap, NULL, pixbuf,
174                    0, 0, x + dx, y + dy, 
175                    w, h, 
176                    GDK_RGB_DITHER_NORMAL, 0, 0);
177   gdk_threads_leave();
178 }
179
180 JNIEXPORT void JNICALL 
181 Java_gnu_java_awt_peer_gtk_GtkVolatileImage_nativeDrawVolatile
182 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
183  jlong pointer, jlong srcptr, jint x, jint y, jint w, jint h)
184 {
185   GdkPixmap *dst, *src;
186   GdkGC *gc;
187
188   src = JLONG_TO_PTR(GdkPixmap, srcptr);
189   dst = JLONG_TO_PTR(GdkPixmap, pointer);
190   g_assert (src != NULL);
191   g_assert (dst != NULL);
192
193   gdk_threads_enter();
194  
195   gc = gdk_gc_new( dst );
196   gdk_draw_drawable(dst,
197                     gc,
198                     src,
199                     0, 0,
200                     x, y,
201                     w, h);
202   g_object_unref( gc );
203
204   gdk_threads_leave();
205 }
206