/**
 * games_card_textures_cache_get_card_texture_by_id:
 * @cache:
 * @card_id:
 *
 * Returns: a cached #CoglHandle for @card_id.
 */
CoglHandle
games_card_textures_cache_get_card_texture_by_id (GamesCardTexturesCache *cache,
                                                  guint card_id)
{
  GamesCardTexturesCachePrivate *priv = cache->priv;
  CoglHandle handle;

  g_return_val_if_fail (card_id < GAMES_CARDS_TOTAL , NULL);

  LOG_CALL (cache);

  handle = priv->cards[card_id];
  if (IS_FAILED_HANDLE (handle)) {
    LOG_CACHE_HIT (cache);
    return COGL_INVALID_HANDLE;
  }

  if (handle == COGL_INVALID_HANDLE) {
    GdkPixbuf *pixbuf;

    LOG_CACHE_MISS (cache);

    pixbuf = games_card_theme_get_card_pixbuf (priv->theme, card_id);
    if (!pixbuf) {
      priv->cards[card_id] = FAILED_HANDLE;
      return COGL_INVALID_HANDLE;
    }

    handle = cogl_texture_new_from_data (gdk_pixbuf_get_width (pixbuf),
                                         gdk_pixbuf_get_height (pixbuf),
                                         64, FALSE,
                                         gdk_pixbuf_get_has_alpha (pixbuf)
                                         ? COGL_PIXEL_FORMAT_RGBA_8888
                                         : COGL_PIXEL_FORMAT_RGB_888,
                                         COGL_PIXEL_FORMAT_ANY,
                                         gdk_pixbuf_get_rowstride (pixbuf),
                                         gdk_pixbuf_get_pixels (pixbuf));
    g_object_unref (pixbuf);

    if (handle == COGL_INVALID_HANDLE) {
      priv->cards[card_id] = FAILED_HANDLE;
      return COGL_INVALID_HANDLE;
    }

    priv->cards[card_id] = handle;
  } else {
    LOG_CACHE_HIT (cache);
  }

  return handle;
}
/**
 * ar_card_surface_cache_get_card_surface_by_id:
 * @cache:
 * @card_id:
 *
 * Returns: a cached #cairo_surface_t for @card_id.
 */
cairo_surface_t *
ar_card_surface_cache_get_card_surface_by_id (ArCardSurfaceCache *cache,
                                              guint card_id)
{
  ArCardSurfaceCachePrivate *priv = cache->priv;
  cairo_surface_t *surface;

  g_return_val_if_fail (card_id < AR_CARDS_TOTAL , NULL);

  LOG_CALL (cache);

  surface = priv->cards[card_id];
  if (IS_FAILED_SURFACE (surface)) {
    LOG_CACHE_HIT (cache);
    return NULL;
  }

  if (surface == NULL) {
    CardSize card_size;
    cairo_t *cr;

    LOG_CACHE_MISS (cache);

    ar_card_theme_get_size (priv->theme, &card_size);

    if (priv->drawable != NULL) {
      surface = gdk_window_create_similar_surface (priv->drawable, CAIRO_CONTENT_COLOR_ALPHA,
                                                   card_size.width, card_size.height);
    } else {
      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                            card_size.width, card_size.height);
    }

    cr = cairo_create (surface);
    ar_card_theme_paint_card (priv->theme, cr, card_id);
    cairo_destroy (cr);

    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
      cairo_surface_destroy (surface);
      priv->cards[card_id] = FAILED_SURFACE;
      return NULL;
    }

    priv->cards[card_id] = surface; /* adopts */
  } else {
    LOG_CACHE_HIT (cache);
  }

  return surface;
}