summaryrefslogtreecommitdiff
path: root/cogl/cogl-sub-texture.c
blob: 1730c871e94ced63e23d8f4011920357350ef4b8 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Cogl
 *
 * An object oriented GL/GLES Abstraction/Utility Layer
 *
 * Copyright (C) 2009,2010 Intel Corporation.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 *
 *
 * Authors:
 *  Neil Roberts   <neil@linux.intel.com>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "cogl.h"
#include "cogl-internal.h"
#include "cogl-util.h"
#include "cogl-texture-private.h"
#include "cogl-sub-texture-private.h"
#include "cogl-context.h"
#include "cogl-handle.h"
#include "cogl-texture-driver.h"

#include <string.h>
#include <math.h>

static void _cogl_sub_texture_free (CoglSubTexture *sub_tex);

COGL_TEXTURE_INTERNAL_DEFINE (SubTexture, sub_texture);

static const CoglTextureVtable cogl_sub_texture_vtable;

static void
_cogl_sub_texture_map_range (float *t1, float *t2,
                             int sub_offset,
                             int sub_size,
                             int full_size)
{
  float t1_frac, t1_int, t2_frac, t2_int;

  t1_frac = modff (*t1, &t1_int);
  t2_frac = modff (*t2, &t2_int);

  if (t1_frac < 0.0f)
    {
      t1_frac += 1.0f;
      t1_int -= 1.0f;
    }
  if (t2_frac < 0.0f)
    {
      t2_frac += 1.0f;
      t2_int -= 1.0f;
    }

  /* If one of the coordinates is zero we need to make sure it is
     still greater than the other coordinate if it was originally so
     we'll flip it to the other side  */
  if (*t1 < *t2)
    {
      if (t2_frac == 0.0f)
        {
          t2_frac = 1.0f;
          t2_int -= 1.0f;
        }
    }
  else
    {
      if (t1_frac == 0.0f)
        {
          t1_frac = 1.0f;
          t1_int -= 1.0f;
        }
    }

  /* Convert the fractional part leaving the integer part intact */
  t1_frac = (sub_offset + t1_frac * sub_size) / full_size;
  *t1 = t1_frac + t1_int;

  t2_frac = (sub_offset + t2_frac * sub_size) / full_size;
  *t2 = t2_frac + t2_int;
}

static void
_cogl_sub_texture_map_quad (CoglSubTexture *sub_tex,
                            float *coords)
{
  unsigned int full_width = cogl_texture_get_width (sub_tex->full_texture);
  unsigned int full_height = cogl_texture_get_height (sub_tex->full_texture);

  _cogl_sub_texture_map_range (coords + 0, coords + 2,
                               sub_tex->sub_x, sub_tex->sub_width,
                               full_width);
  _cogl_sub_texture_map_range (coords + 1, coords + 3,
                               sub_tex->sub_y, sub_tex->sub_height,
                               full_height);
}

/* Maps from the texture coordinates of the full texture to the
   texture coordinates of the sub texture */
static float
_cogl_sub_texture_unmap_coord (float t,
                               int sub_offset,
                               int sub_size,
                               int full_size)
{
  float frac_part, int_part;

  /* Convert the fractional part leaving the integer part in tact */
  frac_part = modff (t, &int_part);

  if (cogl_util_float_signbit (frac_part))
    frac_part = ((1.0f + frac_part) * full_size -
                 sub_offset - sub_size) / sub_size;
  else
    frac_part = (frac_part * full_size - sub_offset) / sub_size;

  return frac_part + int_part;
}

static void
_cogl_sub_texture_unmap_coords (CoglSubTexture *sub_tex,
                                float *s,
                                float *t)
{
  unsigned int full_width = cogl_texture_get_width (sub_tex->full_texture);
  unsigned int full_height = cogl_texture_get_height (sub_tex->full_texture);

  *s = _cogl_sub_texture_unmap_coord (*s, sub_tex->sub_x, sub_tex->sub_width,
                                      full_width);
  *t = _cogl_sub_texture_unmap_coord (*t, sub_tex->sub_y, sub_tex->sub_height,
                                      full_height);
}

typedef struct _CoglSubTextureForeachData
{
  CoglSubTexture *sub_tex;
  CoglTextureSliceCallback callback;
  void *user_data;
} CoglSubTextureForeachData;

static void
_cogl_sub_texture_foreach_cb (CoglHandle handle,
                              const float *slice_coords,
                              const float *full_virtual_coords,
                              void *user_data)
{
  CoglSubTextureForeachData *data = user_data;
  float virtual_coords[4];

  memcpy (virtual_coords, full_virtual_coords, sizeof (virtual_coords));
  /* Convert the virtual coords from the full-texture space to the sub
     texture space */
  _cogl_sub_texture_unmap_coords (data->sub_tex,
                                  &virtual_coords[0],
                                  &virtual_coords[1]);
  _cogl_sub_texture_unmap_coords (data->sub_tex,
                                  &virtual_coords[2],
                                  &virtual_coords[3]);

  data->callback (handle,
                  slice_coords, virtual_coords,
                  data->user_data);
}

static void
_cogl_sub_texture_manual_repeat_cb (const float *coords,
                                    void *user_data)
{
  CoglSubTextureForeachData *data = user_data;
  float mapped_coords[4];

  memcpy (mapped_coords, coords, sizeof (mapped_coords));

  _cogl_sub_texture_map_quad (data->sub_tex, mapped_coords);

  _cogl_texture_foreach_sub_texture_in_region (data->sub_tex->full_texture,
                                               mapped_coords[0],
                                               mapped_coords[1],
                                               mapped_coords[2],
                                               mapped_coords[3],
                                               _cogl_sub_texture_foreach_cb,
                                               user_data);
}

static void
_cogl_sub_texture_foreach_sub_texture_in_region (
                                       CoglTexture *tex,
                                       float virtual_tx_1,
                                       float virtual_ty_1,
                                       float virtual_tx_2,
                                       float virtual_ty_2,
                                       CoglTextureSliceCallback callback,
                                       void *user_data)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);
  CoglSubTextureForeachData data;

  data.sub_tex = sub_tex;
  data.callback = callback;
  data.user_data = user_data;

  _cogl_texture_iterate_manual_repeats (_cogl_sub_texture_manual_repeat_cb,
                                        virtual_tx_1, virtual_ty_1,
                                        virtual_tx_2, virtual_ty_2,
                                        &data);
}

static void
_cogl_sub_texture_set_wrap_mode_parameters (CoglTexture *tex,
                                            GLenum wrap_mode_s,
                                            GLenum wrap_mode_t,
                                            GLenum wrap_mode_p)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  _cogl_texture_set_wrap_mode_parameters (sub_tex->full_texture,
                                          wrap_mode_s,
                                          wrap_mode_t,
                                          wrap_mode_p);
}

static void
_cogl_sub_texture_free (CoglSubTexture *sub_tex)
{
  cogl_handle_unref (sub_tex->next_texture);
  cogl_handle_unref (sub_tex->full_texture);

  /* Chain up */
  _cogl_texture_free (COGL_TEXTURE (sub_tex));
}

CoglHandle
_cogl_sub_texture_new (CoglHandle next_texture,
                       int sub_x, int sub_y,
                       int sub_width, int sub_height)
{
  CoglHandle      full_texture;
  CoglSubTexture *sub_tex;
  CoglTexture    *tex;
  unsigned int    next_width, next_height;

  next_width = cogl_texture_get_width (next_texture);
  next_height = cogl_texture_get_height (next_texture);

  /* The region must specify a non-zero subset of the full texture */
  g_return_val_if_fail (sub_x >= 0 && sub_y >= 0, COGL_INVALID_HANDLE);
  g_return_val_if_fail (sub_width > 0 && sub_height > 0, COGL_INVALID_HANDLE);
  g_return_val_if_fail (sub_x + sub_width <= next_width, COGL_INVALID_HANDLE);
  g_return_val_if_fail (sub_y + sub_height <= next_height, COGL_INVALID_HANDLE);

  sub_tex = g_new (CoglSubTexture, 1);

  tex = COGL_TEXTURE (sub_tex);
  tex->vtable = &cogl_sub_texture_vtable;

  /* If the next texture is also a sub texture we can avoid one level
     of indirection by referencing the full texture of that texture
     instead. */
  if (_cogl_is_sub_texture (next_texture))
    {
      CoglSubTexture *other_sub_tex =
        _cogl_sub_texture_pointer_from_handle (next_texture);
      full_texture = other_sub_tex->full_texture;
      sub_x += other_sub_tex->sub_x;
      sub_y += other_sub_tex->sub_y;
    }
  else
    full_texture = next_texture;

  sub_tex->next_texture = cogl_handle_ref (next_texture);
  sub_tex->full_texture = cogl_handle_ref (full_texture);

  sub_tex->sub_x = sub_x;
  sub_tex->sub_y = sub_y;
  sub_tex->sub_width = sub_width;
  sub_tex->sub_height = sub_height;

  return _cogl_sub_texture_handle_new (sub_tex);
}

static int
_cogl_sub_texture_get_max_waste (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return cogl_texture_get_max_waste (sub_tex->full_texture);
}

static gboolean
_cogl_sub_texture_is_sliced (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return cogl_texture_is_sliced (sub_tex->full_texture);
}

static gboolean
_cogl_sub_texture_can_hardware_repeat (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  /* We can hardware repeat if the subtexture actually represents all of the
     of the full texture */
  return (sub_tex->sub_width ==
          cogl_texture_get_width (sub_tex->full_texture) &&
          sub_tex->sub_height ==
          cogl_texture_get_height (sub_tex->full_texture) &&
          _cogl_texture_can_hardware_repeat (sub_tex->full_texture));
}

static void
_cogl_sub_texture_transform_coords_to_gl (CoglTexture *tex,
                                          float *s,
                                          float *t)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  /* This won't work if the sub texture is not the size of the full
     texture and the coordinates are outside the range [0,1] */
  *s = ((*s * sub_tex->sub_width + sub_tex->sub_x) /
        cogl_texture_get_width (sub_tex->full_texture));
  *t = ((*t * sub_tex->sub_height + sub_tex->sub_y) /
        cogl_texture_get_height (sub_tex->full_texture));

  _cogl_texture_transform_coords_to_gl (sub_tex->full_texture, s, t);
}

static CoglTransformResult
_cogl_sub_texture_transform_quad_coords_to_gl (CoglTexture *tex,
                                               float *coords)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);
  int i;

  /* We can't support repeating with this method. In this case
     cogl-primitives will resort to manual repeating */
  for (i = 0; i < 4; i++)
    if (coords[i] < 0.0f || coords[i] > 1.0f)
      return COGL_TRANSFORM_SOFTWARE_REPEAT;

  _cogl_sub_texture_map_quad (sub_tex, coords);

  return _cogl_texture_transform_quad_coords_to_gl (sub_tex->full_texture,
                                                    coords);
}

static gboolean
_cogl_sub_texture_get_gl_texture (CoglTexture *tex,
                                  GLuint *out_gl_handle,
                                  GLenum *out_gl_target)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return cogl_texture_get_gl_texture (sub_tex->full_texture,
                                      out_gl_handle,
                                      out_gl_target);
}

static void
_cogl_sub_texture_set_filters (CoglTexture *tex,
                               GLenum min_filter,
                               GLenum mag_filter)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  _cogl_texture_set_filters (sub_tex->full_texture, min_filter, mag_filter);
}

static void
_cogl_sub_texture_pre_paint (CoglTexture *tex,
                             CoglTexturePrePaintFlags flags)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  _cogl_texture_pre_paint (sub_tex->full_texture, flags);
}

static void
_cogl_sub_texture_ensure_non_quad_rendering (CoglTexture *tex)
{
}

static gboolean
_cogl_sub_texture_set_region (CoglTexture    *tex,
                              int             src_x,
                              int             src_y,
                              int             dst_x,
                              int             dst_y,
                              unsigned int    dst_width,
                              unsigned int    dst_height,
                              CoglBitmap     *bmp)
{
  CoglSubTexture  *sub_tex = COGL_SUB_TEXTURE (tex);

  return _cogl_texture_set_region_from_bitmap (sub_tex->full_texture,
                                               src_x, src_y,
                                               dst_x + sub_tex->sub_x,
                                               dst_y + sub_tex->sub_y,
                                               dst_width, dst_height,
                                               bmp);
}

static CoglPixelFormat
_cogl_sub_texture_get_format (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return cogl_texture_get_format (sub_tex->full_texture);
}

static GLenum
_cogl_sub_texture_get_gl_format (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return _cogl_texture_get_gl_format (sub_tex->full_texture);
}

static int
_cogl_sub_texture_get_width (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return sub_tex->sub_width;
}

static int
_cogl_sub_texture_get_height (CoglTexture *tex)
{
  CoglSubTexture *sub_tex = COGL_SUB_TEXTURE (tex);

  return sub_tex->sub_height;
}

static const CoglTextureVtable
cogl_sub_texture_vtable =
  {
    _cogl_sub_texture_set_region,
    NULL, /* get_data */
    _cogl_sub_texture_foreach_sub_texture_in_region,
    _cogl_sub_texture_get_max_waste,
    _cogl_sub_texture_is_sliced,
    _cogl_sub_texture_can_hardware_repeat,
    _cogl_sub_texture_transform_coords_to_gl,
    _cogl_sub_texture_transform_quad_coords_to_gl,
    _cogl_sub_texture_get_gl_texture,
    _cogl_sub_texture_set_filters,
    _cogl_sub_texture_pre_paint,
    _cogl_sub_texture_ensure_non_quad_rendering,
    _cogl_sub_texture_set_wrap_mode_parameters,
    _cogl_sub_texture_get_format,
    _cogl_sub_texture_get_gl_format,
    _cogl_sub_texture_get_width,
    _cogl_sub_texture_get_height,
    NULL /* is_foreign */
  };