1/*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#include "GraphicsPrimitiveMgr.h"
27#include "Region.h"
28
29#include "sun_java2d_loops_Blit.h"
30
31/*
32 * Class:     sun_java2d_loops_Blit
33 * Method:    Blit
34 * Signature: (Lsun/java2d/SurfaceData;Lsun/java2d/SurfaceData;Ljava/awt/Composite;IIIIII)V
35 */
36JNIEXPORT void JNICALL
37Java_sun_java2d_loops_Blit_Blit
38    (JNIEnv *env, jobject self,
39     jobject srcData, jobject dstData, jobject comp, jobject clip,
40     jint srcx, jint srcy, jint dstx, jint dsty, jint width, jint height)
41{
42    SurfaceDataOps *srcOps;
43    SurfaceDataOps *dstOps;
44    SurfaceDataRasInfo srcInfo;
45    SurfaceDataRasInfo dstInfo;
46    NativePrimitive *pPrim;
47    CompositeInfo compInfo;
48    RegionData clipInfo;
49    jint dstFlags;
50
51    pPrim = GetNativePrim(env, self);
52    if (pPrim == NULL) {
53        return;
54    }
55    if (pPrim->pCompType->getCompInfo != NULL) {
56        (*pPrim->pCompType->getCompInfo)(env, &compInfo, comp);
57    }
58    if (Region_GetInfo(env, clip, &clipInfo)) {
59        return;
60    }
61
62    srcOps = SurfaceData_GetOps(env, srcData);
63    if (srcOps == 0) {
64        return;
65    }
66    dstOps = SurfaceData_GetOps(env, dstData);
67    if (dstOps == 0) {
68        return;
69    }
70
71    srcInfo.bounds.x1 = srcx;
72    srcInfo.bounds.y1 = srcy;
73    srcInfo.bounds.x2 = srcx + width;
74    srcInfo.bounds.y2 = srcy + height;
75    dstInfo.bounds.x1 = dstx;
76    dstInfo.bounds.y1 = dsty;
77    dstInfo.bounds.x2 = dstx + width;
78    dstInfo.bounds.y2 = dsty + height;
79    srcx -= dstx;
80    srcy -= dsty;
81    SurfaceData_IntersectBounds(&dstInfo.bounds, &clipInfo.bounds);
82    if (srcOps->Lock(env, srcOps, &srcInfo, pPrim->srcflags) != SD_SUCCESS) {
83        return;
84    }
85
86    dstFlags = pPrim->dstflags;
87    if (!Region_IsRectangular(&clipInfo)) {
88        dstFlags |= SD_LOCK_PARTIAL_WRITE;
89    }
90    if (dstOps->Lock(env, dstOps, &dstInfo, dstFlags) != SD_SUCCESS) {
91        SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
92        return;
93    }
94    SurfaceData_IntersectBlitBounds(&dstInfo.bounds, &srcInfo.bounds,
95                                    srcx, srcy);
96    Region_IntersectBounds(&clipInfo, &dstInfo.bounds);
97
98    if (!Region_IsEmpty(&clipInfo)) {
99        srcOps->GetRasInfo(env, srcOps, &srcInfo);
100        dstOps->GetRasInfo(env, dstOps, &dstInfo);
101        if (srcInfo.rasBase && dstInfo.rasBase) {
102            SurfaceDataBounds span;
103            jint savesx = srcInfo.bounds.x1;
104            jint savedx = dstInfo.bounds.x1;
105            Region_StartIteration(env, &clipInfo);
106            while (Region_NextIteration(&clipInfo, &span)) {
107                void *pSrc = PtrCoord(srcInfo.rasBase,
108                                      srcx + span.x1, srcInfo.pixelStride,
109                                      srcy + span.y1, srcInfo.scanStride);
110                void *pDst = PtrCoord(dstInfo.rasBase,
111                                      span.x1, dstInfo.pixelStride,
112                                      span.y1, dstInfo.scanStride);
113                /*
114                 * Fix for 4804375
115                 * REMIND: There should probably be a better
116                 * way to give the span coordinates to the
117                 * inner loop.  This is only really needed
118                 * for the 1, 2, and 4 bit loops.
119                 */
120                srcInfo.bounds.x1 = srcx + span.x1;
121                dstInfo.bounds.x1 = span.x1;
122                (*pPrim->funcs.blit)(pSrc, pDst,
123                                     span.x2 - span.x1, span.y2 - span.y1,
124                                     &srcInfo, &dstInfo, pPrim, &compInfo);
125            }
126            Region_EndIteration(env, &clipInfo);
127            srcInfo.bounds.x1 = savesx;
128            dstInfo.bounds.x1 = savedx;
129        }
130        SurfaceData_InvokeRelease(env, dstOps, &dstInfo);
131        SurfaceData_InvokeRelease(env, srcOps, &srcInfo);
132    }
133    SurfaceData_InvokeUnlock(env, dstOps, &dstInfo);
134    SurfaceData_InvokeUnlock(env, srcOps, &srcInfo);
135}
136