static int get_layer_ops_next(blit_hregion_t *hregion, int subregion, int l)
{
    while (++l < hregion->nlayers) {
        if (!empty_rect(&hregion->blitrects[l][subregion]))
            return l;
    }
    return -1;
}
static int get_top_rect(blit_hregion_t *hregion, int subregion, blit_rect_t **routp)
{
    int l = hregion->nlayers - 1;
    do {
        *routp = &hregion->blitrects[l][subregion];
        if (!empty_rect(*routp))
            break;
    }
    while (--l >= 0);
    return l;
}
/*!
  empties a clipping region.
  */
void gdi_empty_cliprgn(cliprgn_t *p_rgn)
{
  cliprc_t *p_rc = NULL, *p_tmp = NULL;

  p_rc = p_rgn->p_head;
  while(NULL != p_rc)
  {
    p_tmp = p_rc->p_next;
    gdi_free_cliprect(p_rgn->p_heap, p_rc);
    p_rc = p_tmp;
  }

  empty_rect(&p_rgn->bound);
  p_rgn->p_head = p_rgn->p_tail = NULL;
}
/*
 * The idea here is that we walk the layers from front to back and count the
 * number of layers in the hregion until the first layer which doesn't require
 * blending.
 */
static int get_layer_ops(blit_hregion_t *hregion, int subregion, int *bottom)
{
    int l = hregion->nlayers - 1;
    int ops = 0;
    *bottom = -1;
    do {
        if (!empty_rect(&hregion->blitrects[l][subregion])) {
            ops++;
            *bottom = l;
            hwc_layer_1_t *layer = hregion->layers[l];
            IMG_native_handle_t *h = (IMG_native_handle_t *)layer->handle;
            if ((layer->blending != HWC_BLENDING_PREMULT) || is_OPAQUE(h->iFormat))
                break;
        }
    }
    while (--l >= 0);
    return ops;
}
/*!
  initializes a clipping region.
  */
void gdi_init_cliprgn(cliprgn_t *p_rgn, lib_memf_t *p_heap)
{
  empty_rect(&p_rgn->bound);
  p_rgn->p_head = p_rgn->p_tail = NULL;
  p_rgn->p_heap = p_heap;
}