glamor_copyarea.c 25.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright © 2008 Intel Corporation
 * Copyright © 1998 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
22 23 24 25
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *    Zhigang Gong <zhigang.gong@linux.intel.com>
26 27 28 29 30 31 32 33
 */

#include "glamor_priv.h"

/** @file glamor_copyarea.c
 *
 * GC CopyArea implementation
 */
34 35
static Bool
glamor_copy_n_to_n_fbo_blit(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
36 37
                            DrawablePtr dst,
                            GCPtr gc, BoxPtr box, int nbox, int dx, int dy)
38
{
Eric Anholt's avatar
Eric Anholt committed
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
    ScreenPtr screen = dst->pScreen;
    PixmapPtr dst_pixmap = glamor_get_drawable_pixmap(dst);
    PixmapPtr src_pixmap = glamor_get_drawable_pixmap(src);
    glamor_pixmap_private *src_pixmap_priv, *dst_pixmap_priv;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    int dst_x_off, dst_y_off, src_x_off, src_y_off, i;
    int fbo_x_off, fbo_y_off;
    int src_fbo_x_off, src_fbo_y_off;

    if (!glamor_priv->has_fbo_blit) {
        glamor_delayed_fallback(screen, "no EXT_framebuffer_blit\n");
        return FALSE;
    }
    src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
    dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);

    if (gc) {
        if (gc->alu != GXcopy) {
            glamor_delayed_fallback(screen, "non-copy ALU\n");
            return FALSE;
        }
    }

    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(src_pixmap_priv)) {
        glamor_delayed_fallback(screen, "no src fbo\n");
        return FALSE;
    }

    if (glamor_set_destination_pixmap(dst_pixmap))
        return FALSE;

    pixmap_priv_get_fbo_off(dst_pixmap_priv, &fbo_x_off, &fbo_y_off);
    pixmap_priv_get_fbo_off(src_pixmap_priv, &src_fbo_x_off, &src_fbo_y_off);

73
    glamor_make_current(glamor_priv);
74
    glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, src_pixmap_priv->base.fbo->fb);
Eric Anholt's avatar
Eric Anholt committed
75 76 77 78 79 80 81 82 83
    glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off, &dst_y_off);
    glamor_get_drawable_deltas(src, src_pixmap, &src_x_off, &src_y_off);
    dst_x_off += fbo_x_off;
    dst_y_off += fbo_y_off;
    src_y_off += dy + src_fbo_y_off;
    src_x_off += src_fbo_x_off;

    for (i = 0; i < nbox; i++) {
        if (glamor_priv->yInverted) {
84 85 86 87 88 89 90 91 92
            glBlitFramebuffer(box[i].x1 + dx + src_x_off,
                              box[i].y1 + src_y_off,
                              box[i].x2 + dx + src_x_off,
                              box[i].y2 + src_y_off,
                              box[i].x1 + dst_x_off,
                              box[i].y1 + dst_y_off,
                              box[i].x2 + dst_x_off,
                              box[i].y2 + dst_y_off,
                              GL_COLOR_BUFFER_BIT, GL_NEAREST);
Eric Anholt's avatar
Eric Anholt committed
93 94 95 96 97 98 99 100 101 102 103
        }
        else {
            int flip_dst_y1 =
                dst_pixmap->drawable.height - (box[i].y2 + dst_y_off);
            int flip_dst_y2 =
                dst_pixmap->drawable.height - (box[i].y1 + dst_y_off);
            int flip_src_y1 =
                src_pixmap->drawable.height - (box[i].y2 + src_y_off);
            int flip_src_y2 =
                src_pixmap->drawable.height - (box[i].y1 + src_y_off);

104 105 106 107 108 109 110 111 112
            glBlitFramebuffer(box[i].x1 + dx + src_x_off,
                              flip_src_y1,
                              box[i].x2 + dx + src_x_off,
                              flip_src_y2,
                              box[i].x1 + dst_x_off,
                              flip_dst_y1,
                              box[i].x2 + dst_x_off,
                              flip_dst_y2,
                              GL_COLOR_BUFFER_BIT, GL_NEAREST);
Eric Anholt's avatar
Eric Anholt committed
113 114 115 116
        }
    }
    glamor_priv->state = BLIT_STATE;
    return TRUE;
117 118
}

119 120
static Bool
glamor_copy_n_to_n_textured(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
121 122
                            DrawablePtr dst,
                            GCPtr gc, BoxPtr box, int nbox, int dx, int dy)
123
{
Eric Anholt's avatar
Eric Anholt committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    glamor_screen_private *glamor_priv =
        glamor_get_screen_private(dst->pScreen);
    PixmapPtr src_pixmap = glamor_get_drawable_pixmap(src);
    PixmapPtr dst_pixmap = glamor_get_drawable_pixmap(dst);
    int i;
    float vertices[8], texcoords[8];
    glamor_pixmap_private *src_pixmap_priv;
    glamor_pixmap_private *dst_pixmap_priv;
    int src_x_off, src_y_off, dst_x_off, dst_y_off;
    enum glamor_pixmap_status src_status = GLAMOR_NONE;
    GLfloat dst_xscale, dst_yscale, src_xscale, src_yscale;

    src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
    dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);

139
    if (src_pixmap_priv->base.gl_fbo == GLAMOR_FBO_UNATTACHED) {
140
#ifndef GLAMOR_PIXMAP_DYNAMIC_UPLOAD
Eric Anholt's avatar
Eric Anholt committed
141 142
        glamor_delayed_fallback(dst->pScreen, "src has no fbo.\n");
        return FALSE;
143
#else
Eric Anholt's avatar
Eric Anholt committed
144 145 146
        src_status = glamor_upload_pixmap_to_texture(src_pixmap);
        if (src_status != GLAMOR_UPLOAD_DONE)
            return FALSE;
147

Eric Anholt's avatar
Eric Anholt committed
148
        src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
149
#endif
Eric Anholt's avatar
Eric Anholt committed
150
    }
Zhigang Gong's avatar
Zhigang Gong committed
151

Eric Anholt's avatar
Eric Anholt committed
152 153
    pixmap_priv_get_dest_scale(dst_pixmap_priv, &dst_xscale, &dst_yscale);
    pixmap_priv_get_scale(src_pixmap_priv, &src_xscale, &src_yscale);
154

Eric Anholt's avatar
Eric Anholt committed
155
    glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off, &dst_y_off);
156

157
    glamor_make_current(glamor_priv);
158

Eric Anholt's avatar
Eric Anholt committed
159
    glamor_set_destination_pixmap_priv_nc(dst_pixmap_priv);
160 161 162
    glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT,
                          GL_FALSE, 2 * sizeof(float), vertices);
    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
Chris Wilson's avatar
Chris Wilson committed
163

Eric Anholt's avatar
Eric Anholt committed
164 165 166
    glamor_get_drawable_deltas(src, src_pixmap, &src_x_off, &src_y_off);
    dx += src_x_off;
    dy += src_y_off;
167

168 169
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, src_pixmap_priv->base.fbo->tex);
170 171 172 173
    if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP) {
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    }
174 175 176 177 178 179 180 181 182
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_FLOAT, GL_FALSE,
                          2 * sizeof(float), texcoords);
    glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
    glUseProgram(glamor_priv->finish_access_prog[0]);
    glUniform1i(glamor_priv->finish_access_revert[0], REVERT_NONE);
    glUniform1i(glamor_priv->finish_access_swap_rb[0], SWAP_NONE_UPLOADING);
Eric Anholt's avatar
Eric Anholt committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

    for (i = 0; i < nbox; i++) {

        glamor_set_normalize_vcoords(dst_pixmap_priv,
                                     dst_xscale, dst_yscale,
                                     box[i].x1 + dst_x_off,
                                     box[i].y1 + dst_y_off,
                                     box[i].x2 + dst_x_off,
                                     box[i].y2 + dst_y_off,
                                     glamor_priv->yInverted, vertices);

        glamor_set_normalize_tcoords(src_pixmap_priv,
                                     src_xscale,
                                     src_yscale,
                                     box[i].x1 + dx,
                                     box[i].y1 + dy,
                                     box[i].x2 + dx,
                                     box[i].y2 + dy,
                                     glamor_priv->yInverted, texcoords);
202
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Eric Anholt's avatar
Eric Anholt committed
203 204
    }

205 206
    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
    glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
Eric Anholt's avatar
Eric Anholt committed
207 208 209 210
    /* The source texture is bound to a fbo, we have to flush it here. */
    glamor_priv->state = RENDER_STATE;
    glamor_priv->render_idle_cnt = 0;
    return TRUE;
211
}
212

213 214
static Bool
__glamor_copy_n_to_n(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
215 216 217 218 219 220 221 222
                     DrawablePtr dst,
                     GCPtr gc,
                     BoxPtr box,
                     int nbox,
                     int dx,
                     int dy,
                     Bool reverse,
                     Bool upsidedown, Pixel bitplane, void *closure)
223
{
Eric Anholt's avatar
Eric Anholt committed
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
    PixmapPtr dst_pixmap, src_pixmap, temp_pixmap = NULL;
    DrawablePtr temp_src = src;
    glamor_pixmap_private *dst_pixmap_priv, *src_pixmap_priv;
    glamor_screen_private *glamor_priv;
    BoxRec bound;
    ScreenPtr screen;
    int temp_dx = dx;
    int temp_dy = dy;
    int src_x_off, src_y_off, dst_x_off, dst_y_off;
    int i;
    int overlaped = 0;
    Bool ret = FALSE;

    dst_pixmap = glamor_get_drawable_pixmap(dst);
    dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);
    src_pixmap = glamor_get_drawable_pixmap(src);
    src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
    screen = dst_pixmap->drawable.pScreen;
    glamor_priv = glamor_get_screen_private(dst->pScreen);
    glamor_get_drawable_deltas(src, src_pixmap, &src_x_off, &src_y_off);

    glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off, &dst_y_off);

    if (src_pixmap_priv->base.fbo
        && src_pixmap_priv->base.fbo->fb == dst_pixmap_priv->base.fbo->fb) {
        int x_shift = abs(src_x_off - dx - dst_x_off);
        int y_shift = abs(src_y_off - dy - dst_y_off);

        for (i = 0; i < nbox; i++) {
            if (x_shift < abs(box[i].x2 - box[i].x1)
                && y_shift < abs(box[i].y2 - box[i].y1)) {
                overlaped = 1;
                break;
            }
        }
    }
    DEBUGF("Copy %d %d %dx%d dx %d dy %d from %p to %p \n",
           box[0].x1, box[0].y1,
           box[0].x2 - box[0].x1, box[0].y2 - box[0].y1,
           dx, dy, src_pixmap, dst_pixmap);
264 265
    if (glamor_priv->gl_flavor == GLAMOR_GL_DESKTOP &&
        !overlaped &&
Eric Anholt's avatar
Eric Anholt committed
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
        (glamor_priv->state != RENDER_STATE
         || !src_pixmap_priv->base.gl_tex || !dst_pixmap_priv->base.gl_tex)
        && glamor_copy_n_to_n_fbo_blit(src, dst, gc, box, nbox, dx, dy)) {
        ret = TRUE;
        goto done;
    }
    glamor_calculate_boxes_bound(&bound, box, nbox);

    /*  Overlaped indicate the src and dst are the same pixmap. */
    if (overlaped || (!GLAMOR_PIXMAP_PRIV_HAS_FBO(src_pixmap_priv)
                      && (((bound.x2 - bound.x1) * (bound.y2 - bound.y1)
                           * 4 >
                           src_pixmap->drawable.width *
                           src_pixmap->drawable.height)
                          || !(glamor_check_fbo_size(glamor_priv,
                                                     src_pixmap->drawable.width,
                                                     src_pixmap->drawable.
                                                     height))))) {

        temp_pixmap = glamor_create_pixmap(screen,
                                           bound.x2 - bound.x1,
                                           bound.y2 - bound.y1,
                                           src_pixmap->drawable.depth,
                                           overlaped ? 0 :
                                           GLAMOR_CREATE_PIXMAP_CPU);
        assert(bound.x2 - bound.x1 <= glamor_priv->max_fbo_size);
        assert(bound.y2 - bound.y1 <= glamor_priv->max_fbo_size);
        if (!temp_pixmap)
            goto done;
        glamor_translate_boxes(box, nbox, -bound.x1, -bound.y1);
        temp_src = &temp_pixmap->drawable;

        if (overlaped)
            glamor_copy_n_to_n_textured(src, temp_src, gc, box,
                                        nbox,
                                        temp_dx + bound.x1, temp_dy + bound.y1);
        else
            fbCopyNtoN(src, temp_src, gc, box, nbox,
                       temp_dx + bound.x1, temp_dy + bound.y1,
                       reverse, upsidedown, bitplane, closure);
        glamor_translate_boxes(box, nbox, bound.x1, bound.y1);
        temp_dx = -bound.x1;
        temp_dy = -bound.y1;
    }
    else {
        temp_dx = dx;
        temp_dy = dy;
        temp_src = src;
    }

    if (glamor_copy_n_to_n_textured
        (temp_src, dst, gc, box, nbox, temp_dx, temp_dy)) {
        ret = TRUE;
    }
 done:
    if (temp_src != src)
        glamor_destroy_pixmap(temp_pixmap);
    return ret;
324 325 326 327
}

static Bool
_glamor_copy_n_to_n(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
328 329 330 331 332 333 334 335 336
                    DrawablePtr dst,
                    GCPtr gc,
                    BoxPtr box,
                    int nbox,
                    int dx,
                    int dy,
                    Bool reverse,
                    Bool upsidedown, Pixel bitplane,
                    void *closure, Bool fallback)
337
{
338
    ScreenPtr screen = dst->pScreen;
Eric Anholt's avatar
Eric Anholt committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    PixmapPtr dst_pixmap, src_pixmap;
    glamor_pixmap_private *dst_pixmap_priv, *src_pixmap_priv;
    glamor_screen_private *glamor_priv;
    BoxPtr extent;
    RegionRec region;
    int src_x_off, src_y_off, dst_x_off, dst_y_off;
    Bool ok = FALSE;
    int force_clip = 0;

    if (nbox == 0)
        return TRUE;
    dst_pixmap = glamor_get_drawable_pixmap(dst);
    dst_pixmap_priv = glamor_get_pixmap_private(dst_pixmap);
    src_pixmap = glamor_get_drawable_pixmap(src);
    src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);

355
    glamor_priv = glamor_get_screen_private(screen);
Eric Anholt's avatar
Eric Anholt committed
356 357 358 359 360 361 362 363 364 365 366 367

    DEBUGF("Copy %d %d %dx%d dx %d dy %d from %p to %p \n",
           box[0].x1, box[0].y1,
           box[0].x2 - box[0].x1, box[0].y2 - box[0].y1,
           dx, dy, src_pixmap, dst_pixmap);

    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dst_pixmap_priv))
        goto fall_back;

    if (gc) {
        if (!glamor_set_planemask(dst_pixmap, gc->planemask))
            goto fall_back;
368
        glamor_make_current(glamor_priv);
369
        if (!glamor_set_alu(screen, gc->alu)) {
Eric Anholt's avatar
Eric Anholt committed
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 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
            goto fail;
        }
    }

    if (!src_pixmap_priv) {
        glamor_set_pixmap_type(src_pixmap, GLAMOR_MEMORY);
        src_pixmap_priv = glamor_get_pixmap_private(src_pixmap);
    }

    glamor_get_drawable_deltas(src, src_pixmap, &src_x_off, &src_y_off);
    glamor_get_drawable_deltas(dst, dst_pixmap, &dst_x_off, &dst_y_off);

    RegionInitBoxes(&region, box, nbox);
    extent = RegionExtents(&region);

    if (!glamor_check_fbo_size(glamor_priv,
                               extent->x2 - extent->x1, extent->y2 - extent->y1)
        && (src_pixmap_priv->type == GLAMOR_MEMORY
            || (src_pixmap_priv == dst_pixmap_priv))) {
        force_clip = 1;
    }

    if (force_clip || dst_pixmap_priv->type == GLAMOR_TEXTURE_LARGE
        || src_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
        glamor_pixmap_clipped_regions *clipped_dst_regions;
        int n_dst_region, i, j;
        PixmapPtr temp_source_pixmap;
        glamor_pixmap_private *temp_source_priv = NULL;

        RegionTranslate(&region, dst_x_off, dst_y_off);
        if (!force_clip)
            clipped_dst_regions =
                glamor_compute_clipped_regions(dst_pixmap_priv, &region,
                                               &n_dst_region, 0, reverse,
                                               upsidedown);
        else
            clipped_dst_regions =
                glamor_compute_clipped_regions_ext(dst_pixmap_priv, &region,
                                                   &n_dst_region,
                                                   glamor_priv->max_fbo_size,
                                                   glamor_priv->max_fbo_size,
                                                   reverse, upsidedown);
        for (i = 0; i < n_dst_region; i++) {
            int n_src_region;
            glamor_pixmap_clipped_regions *clipped_src_regions;
            BoxPtr current_boxes;
            int n_current_boxes;

            SET_PIXMAP_FBO_CURRENT(dst_pixmap_priv,
                                   clipped_dst_regions[i].block_idx);

            temp_source_pixmap = NULL;
            if (src_pixmap_priv->type == GLAMOR_TEXTURE_LARGE) {
                RegionTranslate(clipped_dst_regions[i].region,
                                -dst_x_off + src_x_off + dx,
                                -dst_y_off + src_y_off + dy);
                clipped_src_regions =
                    glamor_compute_clipped_regions(src_pixmap_priv,
                                                   clipped_dst_regions[i].
                                                   region, &n_src_region, 0,
                                                   reverse, upsidedown);
                DEBUGF("Source is large pixmap.\n");
                for (j = 0; j < n_src_region; j++) {
                    if (src_pixmap_priv != dst_pixmap_priv)
                        SET_PIXMAP_FBO_CURRENT(src_pixmap_priv,
                                               clipped_src_regions[j].
                                               block_idx);
                    else if (src_pixmap_priv == dst_pixmap_priv &&
                             clipped_src_regions[j].block_idx !=
                             clipped_dst_regions[i].block_idx) {
                        /* source and the dest are the same, but need different block_idx.
                         * we create a empty pixmap and fill the required source fbo and box to
                         * it. It's a little hacky, but avoid extra copy. */
                        temp_source_pixmap =
                            glamor_create_pixmap(src->pScreen, 0, 0, src->depth,
                                                 0);
                        if (!temp_source_pixmap) {
                            ok = FALSE;
                            goto fail;
                        }
                        src->pScreen->ModifyPixmapHeader(temp_source_pixmap,
                                                         src_pixmap->drawable.
                                                         width,
                                                         src_pixmap->drawable.
                                                         height, 0, 0,
                                                         src_pixmap->devKind,
                                                         NULL);
                        temp_source_priv =
                            glamor_get_pixmap_private(temp_source_pixmap);
                        *temp_source_priv = *src_pixmap_priv;
                        temp_source_priv->large.box =
                            src_pixmap_priv->large.
                            box_array[clipped_src_regions[j].block_idx];
                        temp_source_priv->base.fbo =
                            src_pixmap_priv->large.
                            fbo_array[clipped_src_regions[j].block_idx];
                        temp_source_priv->base.pixmap = temp_source_pixmap;
                    }
                    assert(temp_source_pixmap ||
                           !(src_pixmap_priv == dst_pixmap_priv &&
                             (clipped_src_regions[j].block_idx !=
                              clipped_dst_regions[i].block_idx)));

                    RegionTranslate(clipped_src_regions[j].region,
                                    -src_x_off - dx, -src_y_off - dy);
                    current_boxes = RegionRects(clipped_src_regions[j].region);
                    n_current_boxes =
                        RegionNumRects(clipped_src_regions[j].region);
                    DEBUGF("dst pixmap fbo idx %d src pixmap fbo idx %d \n",
                           clipped_dst_regions[i].block_idx,
                           clipped_src_regions[j].block_idx);
                    DEBUGF("Copy %d %d %d %d dx %d dy %d from %p to %p \n",
                           current_boxes[0].x1, current_boxes[0].y1,
                           current_boxes[0].x2, current_boxes[0].y2, dx, dy,
                           src_pixmap, dst_pixmap);
                    if (!temp_source_pixmap)
                        ok = __glamor_copy_n_to_n(src, dst, gc, current_boxes,
                                                  n_current_boxes, dx, dy,
                                                  reverse, upsidedown, bitplane,
                                                  closure);
                    else {
                        ok = __glamor_copy_n_to_n(&temp_source_pixmap->drawable,
                                                  dst, gc, current_boxes,
                                                  n_current_boxes, dx, dy,
                                                  reverse, upsidedown, bitplane,
                                                  closure);
                        temp_source_priv->type = GLAMOR_MEMORY;
                        temp_source_priv->base.fbo = NULL;
                        glamor_destroy_pixmap(temp_source_pixmap);
                        temp_source_pixmap = NULL;
                    }

                    RegionDestroy(clipped_src_regions[j].region);
                    if (!ok) {
                        assert(0);
                        goto fail;
                    }
                }

                if (n_src_region == 0)
                    ok = TRUE;
                free(clipped_src_regions);
            }
            else {
                RegionTranslate(clipped_dst_regions[i].region,
                                -dst_x_off, -dst_y_off);
                current_boxes = RegionRects(clipped_dst_regions[i].region);
                n_current_boxes = RegionNumRects(clipped_dst_regions[i].region);

                DEBUGF("dest pixmap fbo idx %d \n",
                       clipped_dst_regions[i].block_idx);
                DEBUGF("Copy %d %d %d %d dx %d dy %d from %p to %p \n",
                       current_boxes[0].x1, current_boxes[0].y1,
                       current_boxes[0].x2, current_boxes[0].y2,
                       dx, dy, src_pixmap, dst_pixmap);

                ok = __glamor_copy_n_to_n(src, dst, gc, current_boxes,
                                          n_current_boxes, dx, dy, reverse,
                                          upsidedown, bitplane, closure);

            }
            RegionDestroy(clipped_dst_regions[i].region);
        }
        if (n_dst_region == 0)
            ok = TRUE;
        free(clipped_dst_regions);
        RegionUninit(&region);
    }
    else {
        ok = __glamor_copy_n_to_n(src, dst, gc, box, nbox, dx, dy,
                                  reverse, upsidedown, bitplane, closure);
    }

 fail:
544
    glamor_make_current(glamor_priv);
545
    glamor_set_alu(screen, GXcopy);
Eric Anholt's avatar
Eric Anholt committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

    if (ok)
        return TRUE;
 fall_back:
    if (!fallback && glamor_ddx_fallback_check_pixmap(src)
        && glamor_ddx_fallback_check_pixmap(dst))
        goto done;

    if (src_pixmap_priv->type == GLAMOR_DRM_ONLY
        || dst_pixmap_priv->type == GLAMOR_DRM_ONLY) {
        LogMessage(X_WARNING,
                   "Access a DRM only pixmap is not allowed within glamor.\n");
        return TRUE;
    }
    glamor_report_delayed_fallbacks(src->pScreen);
    glamor_report_delayed_fallbacks(dst->pScreen);

    glamor_fallback("from %p to %p (%c,%c)\n", src, dst,
                    glamor_get_drawable_location(src),
                    glamor_get_drawable_location(dst));

567
    if (glamor_prepare_access(dst, GLAMOR_ACCESS_RW) &&
568 569
        glamor_prepare_access(src, GLAMOR_ACCESS_RO) &&
        glamor_prepare_access_gc(gc)) {
570 571
        fbCopyNtoN(src, dst, gc, box, nbox,
                   dx, dy, reverse, upsidedown, bitplane, closure);
Eric Anholt's avatar
Eric Anholt committed
572
    }
573
    glamor_finish_access_gc(gc);
574 575
    glamor_finish_access(src);
    glamor_finish_access(dst);
Eric Anholt's avatar
Eric Anholt committed
576 577 578 579 580 581
    ok = TRUE;

 done:
    glamor_clear_delayed_fallbacks(src->pScreen);
    glamor_clear_delayed_fallbacks(dst->pScreen);
    return ok;
582 583 584 585
}

RegionPtr
glamor_copy_area(DrawablePtr src, DrawablePtr dst, GCPtr gc,
Eric Anholt's avatar
Eric Anholt committed
586
                 int srcx, int srcy, int width, int height, int dstx, int dsty)
587
{
Eric Anholt's avatar
Eric Anholt committed
588
    RegionPtr region;
589

Eric Anholt's avatar
Eric Anholt committed
590 591 592 593 594
    region = miDoCopy(src, dst, gc,
                      srcx, srcy, width, height,
                      dstx, dsty, glamor_copy_n_to_n, 0, NULL);

    return region;
595
}
596 597 598

void
glamor_copy_n_to_n(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
599 600 601 602 603 604 605
                   DrawablePtr dst,
                   GCPtr gc,
                   BoxPtr box,
                   int nbox,
                   int dx,
                   int dy,
                   Bool reverse, Bool upsidedown, Pixel bitplane, void *closure)
606
{
Eric Anholt's avatar
Eric Anholt committed
607 608
    _glamor_copy_n_to_n(src, dst, gc, box, nbox, dx,
                        dy, reverse, upsidedown, bitplane, closure, TRUE);
609 610 611 612
}

Bool
glamor_copy_n_to_n_nf(DrawablePtr src,
Eric Anholt's avatar
Eric Anholt committed
613 614 615 616 617 618 619 620
                      DrawablePtr dst,
                      GCPtr gc,
                      BoxPtr box,
                      int nbox,
                      int dx,
                      int dy,
                      Bool reverse,
                      Bool upsidedown, Pixel bitplane, void *closure)
621
{
Eric Anholt's avatar
Eric Anholt committed
622 623 624
    return _glamor_copy_n_to_n(src, dst, gc, box, nbox, dx,
                               dy, reverse, upsidedown, bitplane, closure,
                               FALSE);
625
}