summaryrefslogtreecommitdiff
path: root/native/jni/gtk-peer/gnu_java_awt_peer_gtk_CairoSurface.c
blob: 23340fb5b95e579cdaac6bb01c6576f77fa60af7 (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
/* gnu_java_awt_peer_gtk_CairoSurface.c
   Copyright (C)  2006 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

#include "jcl.h"
#include "gtkpeer.h"

#include "gnu_java_awt_peer_gtk_CairoSurface.h"
#include "cairographics2d.h"

/**
 * Field names in CairoSurface.java
 */
#define SURFACE "surfacePointer"
#define SHARED "sharedBuffer"

/* prototypes */
static void setNativeObject( JNIEnv *env, jobject obj, void *ptr, const char *pointer );

/**
 * Creates a cairo surface, ARGB32, native ordering, premultiplied alpha.
 */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_create
(JNIEnv *env, jobject obj, jint width, jint height, jint stride,
 jintArray buf )
{
  cairo_surface_t* surface;
  jboolean isCopy;

  /* Retrieve java-created data array */
  void *data = (*env)->GetIntArrayElements (env, buf, &isCopy);
  
  /* Set sharedBuffer variable */
  jclass cls = (*env)->GetObjectClass (env, obj);
  jfieldID field = (*env)->GetFieldID (env, cls, SHARED, "Z");
  g_assert (field != 0);
  
  if (isCopy == JNI_TRUE)
    {
      (*env)->SetBooleanField (env, obj, field, JNI_FALSE);
      void* temp = g_malloc(stride * height * 4);
      memcpy(temp, data, stride * height * 4);
      (*env)->ReleaseIntArrayElements (env, buf, data, 0);
      data = temp;
    }
  else
    (*env)->SetBooleanField (env, obj, field, JNI_TRUE);

  /* Create the cairo surface and set the java pointer */  	
  surface = cairo_image_surface_create_for_data
    (data, CAIRO_FORMAT_ARGB32, width, height, stride * 4);

  setNativeObject(env, obj, surface, SURFACE);
}

/**
 * Destroy the surface
 */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_destroy
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer, jintArray buf)
{
  cairo_surface_t* surface = JLONG_TO_PTR(void, surfacePointer);
  void *data = cairo_image_surface_get_data(surface);
  if( surface != NULL )
  {
  	/* Release or free the data buffer as appropriate */
    jclass cls = (*env)->GetObjectClass (env, obj);
    jfieldID field = (*env)->GetFieldID (env, cls, SHARED, "Z");
    g_assert (field != 0);
    jboolean sharedBuffer = (*env)->GetBooleanField (env, obj, field);

    if (sharedBuffer == JNI_TRUE)
  	  (*env)->ReleaseIntArrayElements (env, buf, data, 0);
  	else
  	  g_free(data);
  	  
  	/* Destroy the cairo surface itself */
    cairo_surface_destroy(surface);
  }
}

JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_CairoSurface_nativeDrawSurface 
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer, jlong context, jdoubleArray java_matrix, double alpha,
 jint interpolation)
{
  struct cairographics2d *gr = JLONG_TO_PTR(struct cairographics2d, context);
  cairo_t *cr = gr->cr;
  jdouble *native_matrix = NULL;
  cairo_surface_t* surface = JLONG_TO_PTR(void, surfacePointer);
  g_assert(surface != NULL);
  g_assert(cr != NULL);

  native_matrix = (*env)->GetDoubleArrayElements (env, java_matrix, NULL);
  g_assert (native_matrix != NULL);
  g_assert ((*env)->GetArrayLength (env, java_matrix) == 6);

 {
   cairo_matrix_t mat;
   cairo_pattern_t *p;
   cairo_matrix_init_identity (&mat);
   cairo_matrix_init (&mat, 
                      native_matrix[0], native_matrix[1],
                      native_matrix[2], native_matrix[3],
                      native_matrix[4], native_matrix[5]);

   p = cairo_pattern_create_for_surface (surface);
   cairo_pattern_set_matrix (p, &mat);
   switch ((enum java_awt_rendering_hints_filter) interpolation)
     {
     case java_awt_rendering_hints_VALUE_INTERPOLATION_NEAREST_NEIGHBOR:
       cairo_pattern_set_filter (p, CAIRO_FILTER_NEAREST);
       break;
     case java_awt_rendering_hints_VALUE_INTERPOLATION_BILINEAR:
       cairo_pattern_set_filter (p, CAIRO_FILTER_BILINEAR);
       break; 
     case java_awt_rendering_hints_VALUE_INTERPOLATION_BICUBIC:
       cairo_pattern_set_filter (p, CAIRO_FILTER_GAUSSIAN);
       break; 
     case java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_SPEED:
       cairo_pattern_set_filter (p, CAIRO_FILTER_FAST);
       break;
     case java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_DEFAULT:
       cairo_pattern_set_filter (p, CAIRO_FILTER_NEAREST);
       break;
     case java_awt_rendering_hints_VALUE_ALPHA_INTERPOLATION_QUALITY:
       cairo_pattern_set_filter (p, CAIRO_FILTER_BEST);
       break;
     }

   cairo_set_source(cr, p);
   if (alpha == 1.0)
     cairo_paint(cr);
   else
     cairo_paint_with_alpha(cr, alpha);

   cairo_pattern_destroy(p);
 }
  
 (*env)->ReleaseDoubleArrayElements (env, java_matrix, native_matrix, 0);
}

JNIEXPORT jlong JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_getFlippedBuffer 
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer)
{
  cairo_surface_t* surface;
  jint *src;
  jint *dst;
  int i, t, width, height;
  jclass cls;
  jfieldID field;

  /* Retrieve pointer to cairo data buffer */  
  surface = JLONG_TO_PTR(void, surfacePointer);
  src = (jint*)cairo_image_surface_get_data(surface);
  
  /* Retrieve dimensions of surface, from java fields */
  cls = (*env)->GetObjectClass (env, obj);
  field = (*env)->GetFieldID (env, cls, "width", "I");
  g_assert (field != 0);
  width = (*env)->GetIntField (env, obj, field);

  field = (*env)->GetFieldID (env, cls, "height", "I");
  g_assert (field != 0);
  height = (*env)->GetIntField (env, obj, field);

  /* Create destination array */
  g_assert( src != NULL );
  dst = g_malloc( width * height * sizeof( jint ) );

  /* Copy data into destination array, reversing sample order of each pixel */
  for(i = 0; i < (height * width); i++ )
    {
      t = (src[i] & 0x0000FF) << 16;
      dst[i] = (src[i] & 0x00FF0000) >> 16;
      dst[i] |= (src[i] & 0xFF00FF00);
      dst[i] |= t;
    }

  return PTR_TO_JLONG(dst);
}

/**
 * Create and return a cairo context for drawing to the surface.
 */
JNIEXPORT jlong JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_nativeNewCairoContext
(JNIEnv *env __attribute((unused)), jobject obj __attribute((unused)),
 jlong surfacePointer)
{
  cairo_surface_t* surface = JLONG_TO_PTR(cairo_surface_t, surfacePointer);
  cairo_t *ptr;
  g_assert(surface != NULL);
  ptr = cairo_create(surface);
  g_assert(ptr != NULL);

  return PTR_TO_JLONG(ptr);
}

/**
 * copyArea.
 */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_copyAreaNative2
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer,
 jint x, jint y, jint w, jint h, jint dx, jint dy, jint stride)
{
  int row;
  int srcOffset, dstOffset;
  jint *temp;
  
  /* Retrieve pointer to cairo data buffer */  
  cairo_surface_t* surface = JLONG_TO_PTR(void, surfacePointer);
  jint *pixeldata = (jint*)cairo_image_surface_get_data(surface);
  g_assert( pixeldata != NULL );

  /* Create temporary buffer and calculate offsets */
  temp = g_malloc( h * w * 4 );
  g_assert( temp != NULL );

  srcOffset = x + (y * stride);
  dstOffset = (x + dx) + ((y + dy) * stride);

  /* Copy desired region into temporary buffer */
  for( row = 0; row < h; row++ )
    memcpy( temp + (w * row), pixeldata + srcOffset + (stride * row), w * 4 );

  /* Copy out of buffer and to destination */
  for( row = 0; row < h; row++ )
    memcpy( pixeldata + dstOffset + (stride * row), temp + (w * row), w * 4 );

  g_free( temp );
}

/*
 * Synchronizes the java and native data buffers, copying any changes made in
 * the java array into the native array.
 * This method should only be called if (sharedBuffer == false). 
 */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_syncJavaToNative
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer, jintArray buffer)
{
  /* Get size of java array */
  int size = (*env)->GetArrayLength(env, buffer);
  
  /* Get native data buffer */
  cairo_surface_t* surface = JLONG_TO_PTR(void, surfacePointer);
  g_assert(surface != NULL);
  void* nativeBuffer = cairo_image_surface_get_data(surface);
  
  /* Sync buffers */
  (*env)->GetIntArrayRegion(env, buffer, 0, size, nativeBuffer);
}

/*
 * Synchronizes the java and native data buffers, copying any changes made in
 * the native array into the java array.
 * This method should only be called if (sharedBuffer == false). 
 */
JNIEXPORT void JNICALL 
Java_gnu_java_awt_peer_gtk_CairoSurface_syncNativeToJava
(JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)),
 jlong surfacePointer, jintArray buffer)
{
  /* Get size of java array */
  int size = (*env)->GetArrayLength(env, buffer);
  
  /* Get native data buffer */
  cairo_surface_t* surface = JLONG_TO_PTR(void, surfacePointer);
  g_assert(surface != NULL);
  void* nativeBuffer = cairo_image_surface_get_data(surface);
  
  /* Sync buffers */
  (*env)->SetIntArrayRegion(env, buffer, 0, size, nativeBuffer);
}

/*
 * Sets the native object field.
 */
static void 
setNativeObject( JNIEnv *env, jobject obj, void *ptr, const char *pointer )
{   
  jclass cls;
  jlong value;
  jfieldID nofid;
  cls = (*env)->GetObjectClass( env, obj );
  value = PTR_TO_JLONG(ptr); 
  nofid = (*env)->GetFieldID( env, cls, pointer, "J" );
  (*env)->SetLongField( env, obj, nofid, value );
  (*env)->DeleteLocalRef( env, cls );
}