bitblt.c 75.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * GDI bit-blit operations
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1993, 1994  Alexandre Julliard
5
 * Copyright 2006 Damjan Jovanovic
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.1 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, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

Patrik Stridvall's avatar
Patrik Stridvall committed
22 23
#include "config.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <assert.h>
25
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26 27
#include <stdio.h>
#include <stdlib.h>
28

29
#include "windef.h"
30 31 32
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33
#include "x11drv.h"
34
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36
WINE_DEFAULT_DEBUG_CHANNEL(bitblt);
37

Alexandre Julliard's avatar
Alexandre Julliard committed
38

Alexandre Julliard's avatar
Alexandre Julliard committed
39 40 41
#define DST 0   /* Destination drawable */
#define SRC 1   /* Source drawable */
#define TMP 2   /* Temporary drawable */
Alexandre Julliard's avatar
Alexandre Julliard committed
42 43
#define PAT 3   /* Pattern (brush) in destination DC */

Alexandre Julliard's avatar
Alexandre Julliard committed
44
#define OP(src,dst,rop)   (OP_ARGS(src,dst) << 4 | (rop))
Alexandre Julliard's avatar
Alexandre Julliard committed
45 46
#define OP_ARGS(src,dst)  (((src) << 2) | (dst))

Alexandre Julliard's avatar
Alexandre Julliard committed
47
#define OP_SRC(opcode)    ((opcode) >> 6)
48
#define OP_DST(opcode)    (((opcode) >> 4) & 3)
Alexandre Julliard's avatar
Alexandre Julliard committed
49 50
#define OP_SRCDST(opcode) ((opcode) >> 4)
#define OP_ROP(opcode)    ((opcode) & 0x0f)
Alexandre Julliard's avatar
Alexandre Julliard committed
51

Alexandre Julliard's avatar
Alexandre Julliard committed
52
#define MAX_OP_LEN  6  /* Longest opcode + 1 for the terminating 0 */
Alexandre Julliard's avatar
Alexandre Julliard committed
53

Alexandre Julliard's avatar
Alexandre Julliard committed
54
#define SWAP_INT32(i1,i2) \
55
    do { INT __t = *(i1); *(i1) = *(i2); *(i2) = __t; } while(0)
Alexandre Julliard's avatar
Alexandre Julliard committed
56

Alexandre Julliard's avatar
Alexandre Julliard committed
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
static const unsigned char BITBLT_Opcodes[256][MAX_OP_LEN] =
{
    { OP(PAT,DST,GXclear) },                         /* 0x00  0              */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXnor) },         /* 0x01  ~(D|(P|S))     */
    { OP(PAT,SRC,GXnor), OP(SRC,DST,GXand) },        /* 0x02  D&~(P|S)       */
    { OP(PAT,SRC,GXnor) },                           /* 0x03  ~(P|S)         */
    { OP(PAT,DST,GXnor), OP(SRC,DST,GXand) },        /* 0x04  S&~(D|P)       */
    { OP(PAT,DST,GXnor) },                           /* 0x05  ~(D|P)         */
    { OP(SRC,DST,GXequiv), OP(PAT,DST,GXnor), },     /* 0x06  ~(P|~(D^S))    */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXnor) },        /* 0x07  ~(P|(D&S))     */
    { OP(PAT,DST,GXandInverted), OP(SRC,DST,GXand) },/* 0x08  S&D&~P         */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXnor) },        /* 0x09  ~(P|(D^S))     */
    { OP(PAT,DST,GXandInverted) },                   /* 0x0a  D&~P           */
    { OP(SRC,DST,GXandReverse), OP(PAT,DST,GXnor) }, /* 0x0b  ~(P|(S&~D))    */
    { OP(PAT,SRC,GXandInverted) },                   /* 0x0c  S&~P           */
    { OP(SRC,DST,GXandInverted), OP(PAT,DST,GXnor) },/* 0x0d  ~(P|(D&~S))    */
    { OP(SRC,DST,GXnor), OP(PAT,DST,GXnor) },        /* 0x0e  ~(P|~(D|S))    */
    { OP(PAT,DST,GXcopyInverted) },                  /* 0x0f  ~P             */
    { OP(SRC,DST,GXnor), OP(PAT,DST,GXand) },        /* 0x10  P&~(S|D)       */
    { OP(SRC,DST,GXnor) },                           /* 0x11  ~(D|S)         */
    { OP(PAT,DST,GXequiv), OP(SRC,DST,GXnor) },      /* 0x12  ~(S|~(D^P))    */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXnor) },        /* 0x13  ~(S|(D&P))     */
    { OP(PAT,SRC,GXequiv), OP(SRC,DST,GXnor) },      /* 0x14  ~(D|~(P^S))    */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXnor) },        /* 0x15  ~(D|(P&S))     */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXnand),
      OP(TMP,DST,GXand), OP(SRC,DST,GXxor),
      OP(PAT,DST,GXxor) },                           /* 0x16  P^S^(D&~(P&S)  */
    { OP(SRC,TMP,GXcopy), OP(SRC,DST,GXxor),
      OP(PAT,SRC,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXequiv) },                         /* 0x17 ~S^((S^P)&(S^D))*/
    { OP(PAT,SRC,GXxor), OP(PAT,DST,GXxor),
        OP(SRC,DST,GXand) },                         /* 0x18  (S^P)&(D^P)    */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXnand),
      OP(TMP,DST,GXand), OP(SRC,DST,GXequiv) },      /* 0x19  ~S^(D&~(P&S))  */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXor),
      OP(PAT,DST,GXxor) },                           /* 0x1a  P^(D|(S&P))    */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXxor),
      OP(TMP,DST,GXand), OP(SRC,DST,GXequiv) },      /* 0x1b  ~S^(D&(P^S))   */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXor),
      OP(PAT,DST,GXxor) },                           /* 0x1c  P^(S|(D&P))    */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXequiv) },      /* 0x1d  ~D^(S&(D^P))   */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXxor) },         /* 0x1e  P^(D|S)        */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXnand) },        /* 0x1f  ~(P&(D|S))     */
    { OP(PAT,SRC,GXandReverse), OP(SRC,DST,GXand) }, /* 0x20  D&(P&~S)       */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXnor) },        /* 0x21  ~(S|(D^P))     */
    { OP(SRC,DST,GXandInverted) },                   /* 0x22  ~S&D           */
    { OP(PAT,DST,GXandReverse), OP(SRC,DST,GXnor) }, /* 0x23  ~(S|(P&~D))    */
    { OP(SRC,DST,GXxor), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXand) },                           /* 0x24   (S^P)&(S^D)   */
    { OP(PAT,SRC,GXnand), OP(SRC,DST,GXand),
      OP(PAT,DST,GXequiv) },                         /* 0x25  ~P^(D&~(S&P))  */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXand),
      OP(TMP,DST,GXor), OP(SRC,DST,GXxor) },         /* 0x26  S^(D|(S&P))    */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXequiv),
      OP(TMP,DST,GXor), OP(SRC,DST,GXxor) },         /* 0x27  S^(D|~(P^S))   */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXand) },        /* 0x28  D&(P^S)        */
    { OP(SRC,TMP,GXcopy), OP(PAT,TMP,GXand),
      OP(TMP,DST,GXor), OP(SRC,DST,GXxor),
      OP(PAT,DST,GXequiv) },                         /* 0x29  ~P^S^(D|(P&S)) */
    { OP(PAT,SRC,GXnand), OP(SRC,DST,GXand) },       /* 0x2a  D&~(P&S)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXxor),
      OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXequiv) },                         /* 0x2b ~S^((P^S)&(P^D))*/
    { OP(SRC,DST,GXor), OP(PAT,DST,GXand),
      OP(SRC,DST,GXxor) },                           /* 0x2c  S^(P&(S|D))    */
    { OP(SRC,DST,GXorReverse), OP(PAT,DST,GXxor) },  /* 0x2d  P^(S|~D)       */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXor),
      OP(PAT,DST,GXxor) },                           /* 0x2e  P^(S|(D^P))    */
    { OP(SRC,DST,GXorReverse), OP(PAT,DST,GXnand) }, /* 0x2f  ~(P&(S|~D))    */
    { OP(PAT,SRC,GXandReverse) },                    /* 0x30  P&~S           */
    { OP(PAT,DST,GXandInverted), OP(SRC,DST,GXnor) },/* 0x31  ~(S|(D&~P))    */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x32  S^(D|P|S)      */
    { OP(SRC,DST,GXcopyInverted) },                  /* 0x33  ~S             */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x34  S^(P|(D&S))    */
    { OP(SRC,DST,GXequiv), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x35  S^(P|~(D^S))   */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXxor) },         /* 0x36  S^(D|P)        */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXnand) },        /* 0x37  ~(S&(D|P))     */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXxor) },                           /* 0x38  P^(S&(D|P))    */
    { OP(PAT,DST,GXorReverse), OP(SRC,DST,GXxor) },  /* 0x39  S^(P|~D)       */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x3a  S^(P|(D^S))    */
    { OP(PAT,DST,GXorReverse), OP(SRC,DST,GXnand) }, /* 0x3b  ~(S&(P|~D))    */
    { OP(PAT,SRC,GXxor) },                           /* 0x3c  P^S            */
    { OP(SRC,DST,GXnor), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x3d  S^(P|~(D|S))   */
    { OP(SRC,DST,GXandInverted), OP(PAT,DST,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x3e  S^(P|(D&~S))   */
    { OP(PAT,SRC,GXnand) },                          /* 0x3f  ~(P&S)         */
    { OP(SRC,DST,GXandReverse), OP(PAT,DST,GXand) }, /* 0x40  P&S&~D         */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXnor) },        /* 0x41  ~(D|(P^S))     */
    { OP(DST,SRC,GXxor), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXand) },                           /* 0x42  (S^D)&(P^D)    */
    { OP(SRC,DST,GXnand), OP(PAT,DST,GXand),
      OP(SRC,DST,GXequiv) },                         /* 0x43  ~S^(P&~(D&S))  */
    { OP(SRC,DST,GXandReverse) },                    /* 0x44  S&~D           */
    { OP(PAT,SRC,GXandReverse), OP(SRC,DST,GXnor) }, /* 0x45  ~(D|(P&~S))    */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXand),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor) },         /* 0x46  D^(S|(P&D))    */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXequiv) },                         /* 0x47  ~P^(S&(D^P))   */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXand) },        /* 0x48  S&(P^D)        */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXand),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor),
      OP(PAT,DST,GXequiv) },                         /* 0x49  ~P^D^(S|(P&D)) */
    { OP(DST,SRC,GXor), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXxor) },                           /* 0x4a  D^(P&(S|D))    */
    { OP(SRC,DST,GXorInverted), OP(PAT,DST,GXxor) }, /* 0x4b  P^(D|~S)       */
    { OP(PAT,DST,GXnand), OP(SRC,DST,GXand) },       /* 0x4c  S&~(D&P)       */
    { OP(SRC,TMP,GXcopy), OP(SRC,DST,GXxor),
      OP(PAT,SRC,GXxor), OP(SRC,DST,GXor),
      OP(TMP,DST,GXequiv) },                         /* 0x4d ~S^((S^P)|(S^D))*/
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXor),
      OP(PAT,DST,GXxor) },                           /* 0x4e  P^(D|(S^P))    */
    { OP(SRC,DST,GXorInverted), OP(PAT,DST,GXnand) },/* 0x4f  ~(P&(D|~S))    */
    { OP(PAT,DST,GXandReverse) },                    /* 0x50  P&~D           */
    { OP(PAT,SRC,GXandInverted), OP(SRC,DST,GXnor) },/* 0x51  ~(D|(S&~P))    */
    { OP(DST,SRC,GXand), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x52  D^(P|(S&D))    */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXand),
      OP(SRC,DST,GXequiv) },                         /* 0x53  ~S^(P&(D^S))   */
    { OP(PAT,SRC,GXnor), OP(SRC,DST,GXnor) },        /* 0x54  ~(D|~(P|S))    */
    { OP(PAT,DST,GXinvert) },                        /* 0x55  ~D             */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXxor) },         /* 0x56  D^(P|S)        */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXnand) },        /* 0x57  ~(D&(P|S))     */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXxor) },                           /* 0x58  P^(D&(P|S))    */
    { OP(PAT,SRC,GXorReverse), OP(SRC,DST,GXxor) },  /* 0x59  D^(P|~S)       */
    { OP(PAT,DST,GXxor) },                           /* 0x5a  D^P            */
    { OP(DST,SRC,GXnor), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x5b  D^(P|~(S|D))   */
    { OP(DST,SRC,GXxor), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x5c  D^(P|(S^D))    */
    { OP(PAT,SRC,GXorReverse), OP(SRC,DST,GXnand) }, /* 0x5d  ~(D&(P|~S))    */
    { OP(DST,SRC,GXandInverted), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXxor) },                           /* 0x5e  D^(P|(S&~D))   */
    { OP(PAT,DST,GXnand) },                          /* 0x5f  ~(D&P)         */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXand) },        /* 0x60  P&(D^S)        */
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXand),
      OP(PAT,DST,GXor), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXequiv) },                         /* 0x61  ~D^S^(P|(D&S)) */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0x62  D^(S&(P|D))    */
    { OP(PAT,DST,GXorInverted), OP(SRC,DST,GXxor) }, /* 0x63  S^(D|~P)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0x64  S^(D&(P|S))    */
    { OP(PAT,SRC,GXorInverted), OP(SRC,DST,GXxor) }, /* 0x65  D^(S|~P)       */
    { OP(SRC,DST,GXxor) },                           /* 0x66  S^D            */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXnor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor) },         /* 0x67  S^(D|~(S|P)    */
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXnor),
      OP(PAT,DST,GXor), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXequiv) },                         /* 0x68  ~D^S^(P|~(D|S))*/
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXequiv) },      /* 0x69  ~P^(D^S)       */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXxor) },        /* 0x6a  D^(P&S)        */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor),
      OP(PAT,DST,GXequiv) },                         /* 0x6b  ~P^S^(D&(P|S)) */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXxor) },        /* 0x6c  S^(D&P)        */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor),
      OP(PAT,DST,GXequiv) },                         /* 0x6d  ~P^D^(S&(P|D)) */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXorReverse),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0x6e  S^(D&(P|~S))   */
    { OP(SRC,DST,GXequiv), OP(PAT,DST,GXnand) },     /* 0x6f  ~(P&~(S^D))    */
    { OP(SRC,DST,GXnand), OP(PAT,DST,GXand) },       /* 0x70  P&~(D&S)       */
    { OP(SRC,TMP,GXcopy), OP(DST,SRC,GXxor),
      OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXequiv) },                         /* 0x71 ~S^((S^D)&(P^D))*/
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor) },         /* 0x72  S^(D|(P^S))    */
    { OP(PAT,DST,GXorInverted), OP(SRC,DST,GXnand) },/* 0x73  ~(S&(D|~P))    */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor) },         /* 0x74   D^(S|(P^D))   */
    { OP(PAT,SRC,GXorInverted), OP(SRC,DST,GXnand) },/* 0x75  ~(D&(S|~P))    */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXandReverse),
      OP(SRC,DST,GXor), OP(TMP,DST,GXxor) },         /* 0x76  S^(D|(P&~S))   */
    { OP(SRC,DST,GXnand) },                          /* 0x77  ~(S&D)         */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXxor) },        /* 0x78  P^(D&S)        */
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXor),
      OP(PAT,DST,GXand), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXequiv) },                         /* 0x79  ~D^S^(P&(D|S)) */
    { OP(DST,SRC,GXorInverted), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXxor) },                           /* 0x7a  D^(P&(S|~D))   */
    { OP(PAT,DST,GXequiv), OP(SRC,DST,GXnand) },     /* 0x7b  ~(S&~(D^P))    */
    { OP(SRC,DST,GXorInverted), OP(PAT,DST,GXand),
      OP(SRC,DST,GXxor) },                           /* 0x7c  S^(P&(D|~S))   */
    { OP(PAT,SRC,GXequiv), OP(SRC,DST,GXnand) },     /* 0x7d  ~(D&~(P^S))    */
    { OP(SRC,DST,GXxor), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXor) },                            /* 0x7e  (S^P)|(S^D)    */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXnand) },       /* 0x7f  ~(D&P&S)       */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXand) },        /* 0x80  D&P&S          */
    { OP(SRC,DST,GXxor), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXnor) },                           /* 0x81  ~((S^P)|(S^D)) */
    { OP(PAT,SRC,GXequiv), OP(SRC,DST,GXand) },      /* 0x82  D&~(P^S)       */
    { OP(SRC,DST,GXorInverted), OP(PAT,DST,GXand),
      OP(SRC,DST,GXequiv) },                         /* 0x83  ~S^(P&(D|~S))  */
    { OP(PAT,DST,GXequiv), OP(SRC,DST,GXand) },      /* 0x84  S&~(D^P)       */
    { OP(PAT,SRC,GXorInverted), OP(SRC,DST,GXand),
      OP(PAT,DST,GXequiv) },                         /* 0x85  ~P^(D&(S|~P))  */
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXor),
      OP(PAT,DST,GXand), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0x86  D^S^(P&(D|S))  */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXequiv) },      /* 0x87  ~P^(D&S)       */
    { OP(SRC,DST,GXand) },                           /* 0x88  S&D            */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXandReverse),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0x89  ~S^(D|(P&~S))  */
    { OP(PAT,SRC,GXorInverted), OP(SRC,DST,GXand) }, /* 0x8a  D&(S|~P)       */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0x8b  ~D^(S|(P^D))   */
    { OP(PAT,DST,GXorInverted), OP(SRC,DST,GXand) }, /* 0x8c  S&(D|~P)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0x8d  ~S^(D|(P^S))   */
    { OP(SRC,TMP,GXcopy), OP(DST,SRC,GXxor),
      OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXxor) },                           /* 0x8e  S^((S^D)&(P^D))*/
    { OP(SRC,DST,GXnand), OP(PAT,DST,GXnand) },      /* 0x8f  ~(P&~(D&S))    */
    { OP(SRC,DST,GXequiv), OP(PAT,DST,GXand) },      /* 0x90  P&~(D^S)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXorReverse),
      OP(SRC,DST,GXand), OP(TMP,DST,GXequiv) },      /* 0x91  ~S^(D&(P|~S))  */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXor),
      OP(SRC,DST,GXand), OP(PAT,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0x92  D^P^(S&(D|P))  */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXequiv) },      /* 0x93  ~S^(P&D)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXand), OP(PAT,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0x94  S^P^(D&(P|S))  */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXequiv) },      /* 0x95  ~D^(P&S)       */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXxor) },        /* 0x96  D^P^S          */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXnor),
      OP(SRC,DST,GXor), OP(PAT,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0x97  S^P^(D|~(P|S)) */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXnor),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0x98  ~S^(D|~(P|S))  */
    { OP(SRC,DST,GXequiv) },                         /* 0x99  ~S^D           */
    { OP(PAT,SRC,GXandReverse), OP(SRC,DST,GXxor) }, /* 0x9a  D^(P&~S)       */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXequiv) },      /* 0x9b  ~S^(D&(P|S))   */
    { OP(PAT,DST,GXandReverse), OP(SRC,DST,GXxor) }, /* 0x9c  S^(P&~D)       */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXequiv) },      /* 0x9d  ~D^(S&(P|D))   */
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXand),
      OP(PAT,DST,GXor), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0x9e  D^S^(P|(D&S))  */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXnand) },       /* 0x9f  ~(P&(D^S))     */
    { OP(PAT,DST,GXand) },                           /* 0xa0  D&P            */
    { OP(PAT,SRC,GXandInverted), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xa1  ~P^(D|(S&~P))  */
    { OP(PAT,SRC,GXorReverse), OP(SRC,DST,GXand) },  /* 0xa2  D&(P|~S)       */
    { OP(DST,SRC,GXxor), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xa3  ~D^(P|(S^D))   */
    { OP(PAT,SRC,GXnor), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xa4  ~P^(D|~(S|P))  */
    { OP(PAT,DST,GXequiv) },                         /* 0xa5  ~P^D           */
    { OP(PAT,SRC,GXandInverted), OP(SRC,DST,GXxor) },/* 0xa6  D^(S&~P)       */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXequiv) },                         /* 0xa7  ~P^(D&(S|P))   */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXand) },         /* 0xa8  D&(P|S)        */
    { OP(PAT,SRC,GXor), OP(SRC,DST,GXequiv) },       /* 0xa9  ~D^(P|S)       */
320
    { OP(PAT,DST,GXnoop) },                          /* 0xaa  D              */
Alexandre Julliard's avatar
Alexandre Julliard committed
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 473
    { OP(PAT,SRC,GXnor), OP(SRC,DST,GXor) },         /* 0xab  D|~(P|S)       */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXand),
      OP(SRC,DST,GXxor) },                           /* 0xac  S^(P&(D^S))    */
    { OP(DST,SRC,GXand), OP(PAT,SRC,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xad  ~D^(P|(S&D))   */
    { OP(PAT,SRC,GXandInverted), OP(SRC,DST,GXor) }, /* 0xae  D|(S&~P)       */
    { OP(PAT,DST,GXorInverted) },                    /* 0xaf  D|~P           */
    { OP(SRC,DST,GXorInverted), OP(PAT,DST,GXand) }, /* 0xb0  P&(D|~S)       */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xb1  ~P^(D|(S^P))   */
    { OP(SRC,TMP,GXcopy), OP(SRC,DST,GXxor),
      OP(PAT,SRC,GXxor), OP(SRC,DST,GXor),
      OP(TMP,DST,GXxor) },                           /* 0xb2  S^((S^P)|(S^D))*/
    { OP(PAT,DST,GXnand), OP(SRC,DST,GXnand) },      /* 0xb3  ~(S&~(D&P))    */
    { OP(SRC,DST,GXandReverse), OP(PAT,DST,GXxor) }, /* 0xb4  P^(S&~D)       */
    { OP(DST,SRC,GXor), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXequiv) },                         /* 0xb5  ~D^(P&(S|D))   */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXand),
      OP(SRC,DST,GXor), OP(PAT,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0xb6  D^P^(S|(D&P))  */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXnand) },       /* 0xb7  ~(S&(D^P))     */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXxor) },                           /* 0xb8  P^(S&(D^P))    */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXand),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0xb9  ~D^(S|(P&D))   */
    { OP(PAT,SRC,GXandReverse), OP(SRC,DST,GXor) },  /* 0xba  D|(P&~S)       */
    { OP(SRC,DST,GXorInverted) },                    /* 0xbb  ~S|D           */
    { OP(SRC,DST,GXnand), OP(PAT,DST,GXand),
      OP(SRC,DST,GXxor) },                           /* 0xbc  S^(P&~(D&S))   */
    { OP(DST,SRC,GXxor), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXnand) },                          /* 0xbd  ~((S^D)&(P^D)) */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXor) },         /* 0xbe  D|(P^S)        */
    { OP(PAT,SRC,GXnand), OP(SRC,DST,GXor) },        /* 0xbf  D|~(P&S)       */
    { OP(PAT,SRC,GXand) },                           /* 0xc0  P&S            */
    { OP(SRC,DST,GXandInverted), OP(PAT,DST,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xc1  ~S^(P|(D&~S))  */
    { OP(SRC,DST,GXnor), OP(PAT,DST,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xc2  ~S^(P|~(D|S))  */
    { OP(PAT,SRC,GXequiv) },                         /* 0xc3  ~P^S           */
    { OP(PAT,DST,GXorReverse), OP(SRC,DST,GXand) },  /* 0xc4  S&(P|~D)       */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xc5  ~S^(P|(D^S))   */
    { OP(PAT,DST,GXandInverted), OP(SRC,DST,GXxor) },/* 0xc6  S^(D&~P)       */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXequiv) },                         /* 0xc7  ~P^(S&(D|P))   */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXand) },         /* 0xc8  S&(D|P)        */
    { OP(PAT,DST,GXor), OP(SRC,DST,GXequiv) },       /* 0xc9  ~S^(P|D)       */
    { OP(DST,SRC,GXxor), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXxor) },                           /* 0xca  D^(P&(S^D))    */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXor),
      OP(SRC,DST,GXequiv) },                         /* 0xcb  ~S^(P|(D&S))   */
    { OP(SRC,DST,GXcopy) },                          /* 0xcc  S              */
    { OP(PAT,DST,GXnor), OP(SRC,DST,GXor) },         /* 0xcd  S|~(D|P)       */
    { OP(PAT,DST,GXandInverted), OP(SRC,DST,GXor) }, /* 0xce  S|(D&~P)       */
    { OP(PAT,SRC,GXorInverted) },                    /* 0xcf  S|~P           */
    { OP(SRC,DST,GXorReverse), OP(PAT,DST,GXand) },  /* 0xd0  P&(S|~D)       */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xd1  ~P^(S|(D^P))   */
    { OP(SRC,DST,GXandInverted), OP(PAT,DST,GXxor) },/* 0xd2  P^(D&~S)       */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXand),
      OP(SRC,DST,GXequiv) },                         /* 0xd3  ~S^(P&(D|S))   */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXxor),
      OP(PAT,DST,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXxor) },                           /* 0xd4  S^((S^P)&(D^P))*/
    { OP(PAT,SRC,GXnand), OP(SRC,DST,GXnand) },      /* 0xd5  ~(D&~(P&S))    */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXor), OP(PAT,DST,GXxor),
      OP(TMP,DST,GXxor) },                           /* 0xd6  S^P^(D|(P&S))  */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXnand) },       /* 0xd7  ~(D&(P^S))     */
    { OP(PAT,SRC,GXxor), OP(SRC,DST,GXand),
      OP(PAT,DST,GXxor) },                           /* 0xd8  P^(D&(S^P))    */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXor), OP(TMP,DST,GXequiv) },       /* 0xd9  ~S^(D|(P&S))   */
    { OP(DST,SRC,GXnand), OP(PAT,SRC,GXand),
      OP(SRC,DST,GXxor) },                           /* 0xda  D^(P&~(S&D))   */
    { OP(SRC,DST,GXxor), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXnand) },                          /* 0xdb  ~((S^P)&(S^D)) */
    { OP(PAT,DST,GXandReverse), OP(SRC,DST,GXor) },  /* 0xdc  S|(P&~D)       */
    { OP(SRC,DST,GXorReverse) },                     /* 0xdd  S|~D           */
    { OP(PAT,DST,GXxor), OP(SRC,DST,GXor) },         /* 0xde  S|(D^P)        */
    { OP(PAT,DST,GXnand), OP(SRC,DST,GXor) },        /* 0xdf  S|~(D&P)       */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXand) },         /* 0xe0  P&(D|S)        */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXequiv) },       /* 0xe1  ~P^(D|S)       */
    { OP(DST,TMP,GXcopy), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0xe2  D^(S&(P^D))    */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xe3  ~P^(S|(D&P))   */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXxor),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0xe4  S^(D&(P^S))    */
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXor),
      OP(PAT,DST,GXequiv) },                         /* 0xe5  ~P^(D|(S&P))   */
    { OP(SRC,TMP,GXcopy), OP(PAT,SRC,GXnand),
      OP(SRC,DST,GXand), OP(TMP,DST,GXxor) },        /* 0xe6  S^(D&~(P&S))   */
    { OP(PAT,SRC,GXxor), OP(PAT,DST,GXxor),
      OP(SRC,DST,GXnand) },                          /* 0xe7  ~((S^P)&(D^P)) */
    { OP(SRC,TMP,GXcopy), OP(SRC,DST,GXxor),
      OP(PAT,SRC,GXxor), OP(SRC,DST,GXand),
      OP(TMP,DST,GXxor) },                           /* 0xe8  S^((S^P)&(S^D))*/
    { OP(DST,TMP,GXcopy), OP(SRC,DST,GXnand),
      OP(PAT,DST,GXand), OP(SRC,DST,GXxor),
      OP(TMP,DST,GXequiv) },                         /* 0xe9  ~D^S^(P&~(S&D))*/
    { OP(PAT,SRC,GXand), OP(SRC,DST,GXor) },         /* 0xea  D|(P&S)        */
    { OP(PAT,SRC,GXequiv), OP(SRC,DST,GXor) },       /* 0xeb  D|~(P^S)       */
    { OP(PAT,DST,GXand), OP(SRC,DST,GXor) },         /* 0xec  S|(D&P)        */
    { OP(PAT,DST,GXequiv), OP(SRC,DST,GXor) },       /* 0xed  S|~(D^P)       */
    { OP(SRC,DST,GXor) },                            /* 0xee  S|D            */
    { OP(PAT,DST,GXorInverted), OP(SRC,DST,GXor) },  /* 0xef  S|D|~P         */
    { OP(PAT,DST,GXcopy) },                          /* 0xf0  P              */
    { OP(SRC,DST,GXnor), OP(PAT,DST,GXor) },         /* 0xf1  P|~(D|S)       */
    { OP(SRC,DST,GXandInverted), OP(PAT,DST,GXor) }, /* 0xf2  P|(D&~S)       */
    { OP(PAT,SRC,GXorReverse) },                     /* 0xf3  P|~S           */
    { OP(SRC,DST,GXandReverse), OP(PAT,DST,GXor) },  /* 0xf4  P|(S&~D)       */
    { OP(PAT,DST,GXorReverse) },                     /* 0xf5  P|~D           */
    { OP(SRC,DST,GXxor), OP(PAT,DST,GXor) },         /* 0xf6  P|(D^S)        */
    { OP(SRC,DST,GXnand), OP(PAT,DST,GXor) },        /* 0xf7  P|~(S&D)       */
    { OP(SRC,DST,GXand), OP(PAT,DST,GXor) },         /* 0xf8  P|(D&S)        */
    { OP(SRC,DST,GXequiv), OP(PAT,DST,GXor) },       /* 0xf9  P|~(D^S)       */
    { OP(PAT,DST,GXor) },                            /* 0xfa  D|P            */
    { OP(PAT,SRC,GXorReverse), OP(SRC,DST,GXor) },   /* 0xfb  D|P|~S         */
    { OP(PAT,SRC,GXor) },                            /* 0xfc  P|S            */
    { OP(SRC,DST,GXorReverse), OP(PAT,DST,GXor) },   /* 0xfd  P|S|~D         */
    { OP(SRC,DST,GXor), OP(PAT,DST,GXor) },          /* 0xfe  P|D|S          */
    { OP(PAT,DST,GXset) }                            /* 0xff  1              */
};


#ifdef BITBLT_TEST  /* Opcodes test */

static int do_bitop( int s, int d, int rop )
{
    int res;
    switch(rop)
    {
    case GXclear:        res = 0; break;
    case GXand:          res = s & d; break;
    case GXandReverse:   res = s & ~d; break;
    case GXcopy:         res = s; break;
    case GXandInverted:  res = ~s & d; break;
    case GXnoop:         res = d; break;
    case GXxor:          res = s ^ d; break;
    case GXor:           res = s | d; break;
    case GXnor:          res = ~(s | d); break;
    case GXequiv:        res = ~s ^ d; break;
    case GXinvert:       res = ~d; break;
    case GXorReverse:    res = s | ~d; break;
    case GXcopyInverted: res = ~s; break;
    case GXorInverted:   res = ~s | d; break;
    case GXnand:         res = ~(s & d); break;
    case GXset:          res = 1; break;
    }
    return res & 1;
}

474
int main()
Alexandre Julliard's avatar
Alexandre Julliard committed
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
{
    int rop, i, res, src, dst, pat, tmp, dstUsed;
    const BYTE *opcode;

    for (rop = 0; rop < 256; rop++)
    {
        res = dstUsed = 0;
        for (i = 0; i < 8; i++)
        {
            pat = (i >> 2) & 1;
            src = (i >> 1) & 1;
            dst = i & 1;
            for (opcode = BITBLT_Opcodes[rop]; *opcode; opcode++)
            {
                switch(*opcode >> 4)
                {
                case OP_ARGS(DST,TMP):
                    tmp = do_bitop( dst, tmp, *opcode & 0xf );
                    break;
                case OP_ARGS(DST,SRC):
                    src = do_bitop( dst, src, *opcode & 0xf );
                    break;
                case OP_ARGS(SRC,TMP):
                    tmp = do_bitop( src, tmp, *opcode & 0xf );
                    break;
                case OP_ARGS(SRC,DST):
                    dst = do_bitop( src, dst, *opcode & 0xf );
                    dstUsed = 1;
                    break;
                case OP_ARGS(PAT,TMP):
                    tmp = do_bitop( pat, tmp, *opcode & 0xf );
                    break;
                case OP_ARGS(PAT,DST):
                    dst = do_bitop( pat, dst, *opcode & 0xf );
                    dstUsed = 1;
                    break;
                case OP_ARGS(PAT,SRC):
                    src = do_bitop( pat, src, *opcode & 0xf );
                    break;
                case OP_ARGS(TMP,DST):
                    dst = do_bitop( tmp, dst, *opcode & 0xf );
                    dstUsed = 1;
                    break;
                case OP_ARGS(TMP,SRC):
                    src = do_bitop( tmp, src, *opcode & 0xf );
                    break;
                default:
                    printf( "Invalid opcode %x\n", *opcode );
                }
            }
            if (!dstUsed) dst = src;
            if (dst) res |= 1 << i;
        }
        if (res != rop) printf( "%02x: ERROR, res=%02x\n", rop, res );
    }
530 531

    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
532 533 534 535
}

#endif  /* BITBLT_TEST */

Alexandre Julliard's avatar
Alexandre Julliard committed
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
static void get_colors(X11DRV_PDEVICE *physDevDst, X11DRV_PDEVICE *physDevSrc,
		       int *fg, int *bg)
{
    RGBQUAD rgb[2];

    *fg = physDevDst->textPixel;
    *bg = physDevDst->backgroundPixel;
    if(physDevSrc->depth == 1) {
        if(GetDIBColorTable(physDevSrc->hdc, 0, 2, rgb) == 2) {
            DWORD logcolor;
            logcolor = RGB(rgb[0].rgbRed, rgb[0].rgbGreen, rgb[0].rgbBlue);
            *fg = X11DRV_PALETTE_ToPhysical( physDevDst, logcolor );
            logcolor = RGB(rgb[1].rgbRed, rgb[1].rgbGreen,rgb[1].rgbBlue);
            *bg = X11DRV_PALETTE_ToPhysical( physDevDst, logcolor );
        }
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
555
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
556 557 558
 *           BITBLT_StretchRow
 *
 * Stretch a row of pixels. Helper function for BITBLT_StretchImage.
Alexandre Julliard's avatar
Alexandre Julliard committed
559
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
560
static void BITBLT_StretchRow( int *rowSrc, int *rowDst,
561 562
                               INT startDst, INT widthDst,
                               INT xinc, INT xoff, WORD mode )
Alexandre Julliard's avatar
Alexandre Julliard committed
563
{
564
    register INT xsrc = xinc * startDst + xoff;
Alexandre Julliard's avatar
Alexandre Julliard committed
565 566
    rowDst += startDst;
    switch(mode)
Alexandre Julliard's avatar
Alexandre Julliard committed
567
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
568 569 570 571 572 573 574 575 576 577 578 579
    case STRETCH_ANDSCANS:
        for(; widthDst > 0; widthDst--, xsrc += xinc)
            *rowDst++ &= rowSrc[xsrc >> 16];
        break;
    case STRETCH_ORSCANS:
        for(; widthDst > 0; widthDst--, xsrc += xinc)
            *rowDst++ |= rowSrc[xsrc >> 16];
        break;
    case STRETCH_DELETESCANS:
        for(; widthDst > 0; widthDst--, xsrc += xinc)
            *rowDst++ = rowSrc[xsrc >> 16];
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
580
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
581 582 583 584 585 586 587 588 589
}


/***********************************************************************
 *           BITBLT_ShrinkRow
 *
 * Shrink a row of pixels. Helper function for BITBLT_StretchImage.
 */
static void BITBLT_ShrinkRow( int *rowSrc, int *rowDst,
590 591
                              INT startSrc, INT widthSrc,
                              INT xinc, INT xoff, WORD mode )
Alexandre Julliard's avatar
Alexandre Julliard committed
592
{
593
    register INT xdst = xinc * startSrc + xoff;
Alexandre Julliard's avatar
Alexandre Julliard committed
594 595
    rowSrc += startSrc;
    switch(mode)
Alexandre Julliard's avatar
Alexandre Julliard committed
596
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
597 598 599 600 601 602 603 604 605 606 607 608
    case STRETCH_ORSCANS:
        for(; widthSrc > 0; widthSrc--, xdst += xinc)
            rowDst[xdst >> 16] |= *rowSrc++;
        break;
    case STRETCH_ANDSCANS:
        for(; widthSrc > 0; widthSrc--, xdst += xinc)
            rowDst[xdst >> 16] &= *rowSrc++;
        break;
    case STRETCH_DELETESCANS:
        for(; widthSrc > 0; widthSrc--, xdst += xinc)
            rowDst[xdst >> 16] = *rowSrc++;
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
609 610 611 612
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
613
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
614
 *           BITBLT_GetRow
Alexandre Julliard's avatar
Alexandre Julliard committed
615
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
616
 * Retrieve a row from an image. Helper function for BITBLT_StretchImage.
Alexandre Julliard's avatar
Alexandre Julliard committed
617
 */
618 619 620
static void BITBLT_GetRow( XImage *image, int *pdata, INT row,
                           INT start, INT width, INT depthDst,
                           int fg, int bg, BOOL swap)
Alexandre Julliard's avatar
Alexandre Julliard committed
621
{
622
    register INT i;
Alexandre Julliard's avatar
Alexandre Julliard committed
623

Alexandre Julliard's avatar
Alexandre Julliard committed
624 625 626
    assert( (row >= 0) && (row < image->height) );
    assert( (start >= 0) && (width <= image->width) );

Alexandre Julliard's avatar
Alexandre Julliard committed
627
    pdata += swap ? start+width-1 : start;
Alexandre Julliard's avatar
Alexandre Julliard committed
628
    if (image->depth == depthDst)  /* color -> color */
Alexandre Julliard's avatar
Alexandre Julliard committed
629
    {
630
        if (X11DRV_PALETTE_XPixelToPalette && (depthDst != 1))
Alexandre Julliard's avatar
Alexandre Julliard committed
631
            if (swap) for (i = 0; i < width; i++)
632
                *pdata-- = X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, i, row )];
Alexandre Julliard's avatar
Alexandre Julliard committed
633
            else for (i = 0; i < width; i++)
634
                *pdata++ = X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, i, row )];
Alexandre Julliard's avatar
Alexandre Julliard committed
635 636
        else
            if (swap) for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
637
                *pdata-- = XGetPixel( image, i, row );
Alexandre Julliard's avatar
Alexandre Julliard committed
638
            else for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
639
                *pdata++ = XGetPixel( image, i, row );
Alexandre Julliard's avatar
Alexandre Julliard committed
640
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
641
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
642
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
643 644
        if (image->depth == 1)  /* monochrome -> color */
        {
645
            if (X11DRV_PALETTE_XPixelToPalette)
Alexandre Julliard's avatar
Alexandre Julliard committed
646
            {
647 648
                fg = X11DRV_PALETTE_XPixelToPalette[fg];
                bg = X11DRV_PALETTE_XPixelToPalette[bg];
Alexandre Julliard's avatar
Alexandre Julliard committed
649
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
650
            if (swap) for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
651
                *pdata-- = XGetPixel( image, i, row ) ? bg : fg;
Alexandre Julliard's avatar
Alexandre Julliard committed
652
            else for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
653
                *pdata++ = XGetPixel( image, i, row ) ? bg : fg;
Alexandre Julliard's avatar
Alexandre Julliard committed
654 655 656
        }
        else  /* color -> monochrome */
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
657
            if (swap) for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
658
                *pdata-- = (XGetPixel( image, i, row ) == bg) ? 1 : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
659
            else for (i = 0; i < width; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
660
                *pdata++ = (XGetPixel( image, i, row ) == bg) ? 1 : 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
661
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
662
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
663 664 665 666 667 668 669
}


/***********************************************************************
 *           BITBLT_StretchImage
 *
 * Stretch an X image.
Alexandre Julliard's avatar
Alexandre Julliard committed
670
 * FIXME: does not work for full 32-bit coordinates.
Alexandre Julliard's avatar
Alexandre Julliard committed
671 672
 */
static void BITBLT_StretchImage( XImage *srcImage, XImage *dstImage,
673 674 675
                                 INT widthSrc, INT heightSrc,
                                 INT widthDst, INT heightDst,
                                 RECT *visRectSrc, RECT *visRectDst,
Alexandre Julliard's avatar
Alexandre Julliard committed
676 677 678
                                 int foreground, int background, WORD mode )
{
    int *rowSrc, *rowDst, *pixel;
Alexandre Julliard's avatar
Alexandre Julliard committed
679
    char *pdata;
680 681 682
    INT xinc, xoff, yinc, ysrc, ydst;
    register INT x, y;
    BOOL hstretch, vstretch, hswap, vswap;
Alexandre Julliard's avatar
Alexandre Julliard committed
683

684 685
    hswap = widthSrc * widthDst < 0;
    vswap = heightSrc * heightDst < 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
686 687 688 689 690
    widthSrc  = abs(widthSrc);
    heightSrc = abs(heightSrc);
    widthDst  = abs(widthDst);
    heightDst = abs(heightDst);

691 692
    if (!(rowSrc = HeapAlloc( GetProcessHeap(), 0,
                              (widthSrc+widthDst)*sizeof(int) ))) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
693
    rowDst = rowSrc + widthSrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
694

Alexandre Julliard's avatar
Alexandre Julliard committed
695 696 697 698
      /* When stretching, all modes are the same, and DELETESCANS is faster */
    if ((widthSrc < widthDst) && (heightSrc < heightDst))
        mode = STRETCH_DELETESCANS;

699 700 701
    if (mode == STRETCH_HALFTONE) /* FIXME */
        mode = STRETCH_DELETESCANS;

Alexandre Julliard's avatar
Alexandre Julliard committed
702 703 704 705
    if (mode != STRETCH_DELETESCANS)
        memset( rowDst, (mode == STRETCH_ANDSCANS) ? 0xff : 0x00,
                widthDst*sizeof(int) );

Alexandre Julliard's avatar
Alexandre Julliard committed
706 707 708
    hstretch = (widthSrc < widthDst);
    vstretch = (heightSrc < heightDst);

Alexandre Julliard's avatar
Alexandre Julliard committed
709 710
    if (hstretch)
    {
711
        xinc = (widthSrc << 16) / widthDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
712 713 714 715 716 717 718
        xoff = ((widthSrc << 16) - (xinc * widthDst)) / 2;
    }
    else
    {
        xinc = ((int)widthDst << 16) / widthSrc;
        xoff = ((widthDst << 16) - (xinc * widthSrc)) / 2;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
719

720
    wine_tsx11_lock();
Alexandre Julliard's avatar
Alexandre Julliard committed
721
    if (vstretch)
Alexandre Julliard's avatar
Alexandre Julliard committed
722
    {
723
        yinc = (heightSrc << 16) / heightDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
724
        ydst = visRectDst->top;
Alexandre Julliard's avatar
Alexandre Julliard committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
        if (vswap)
        {
            ysrc = yinc * (heightDst - ydst - 1);
            yinc = -yinc;
        }
        else
            ysrc = yinc * ydst;

        for ( ; (ydst < visRectDst->bottom); ysrc += yinc, ydst++)
        {
            if (((ysrc >> 16) < visRectSrc->top) ||
                ((ysrc >> 16) >= visRectSrc->bottom)) continue;

            /* Retrieve a source row */
            BITBLT_GetRow( srcImage, rowSrc, (ysrc >> 16) - visRectSrc->top,
                           hswap ? widthSrc - visRectSrc->right
                                 : visRectSrc->left,
                           visRectSrc->right - visRectSrc->left,
                           dstImage->depth, foreground, background, hswap );

            /* Stretch or shrink it */
            if (hstretch)
                BITBLT_StretchRow( rowSrc, rowDst, visRectDst->left,
                                   visRectDst->right - visRectDst->left,
                                   xinc, xoff, mode );
            else BITBLT_ShrinkRow( rowSrc, rowDst,
                                   hswap ? widthSrc - visRectSrc->right
                                         : visRectSrc->left,
                                   visRectSrc->right - visRectSrc->left,
                                   xinc, xoff, mode );

            /* Store the destination row */
            pixel = rowDst + visRectDst->right - 1;
            y = ydst - visRectDst->top;
            for (x = visRectDst->right-visRectDst->left-1; x >= 0; x--)
Alexandre Julliard's avatar
Alexandre Julliard committed
760
                XPutPixel( dstImage, x, y, *pixel-- );
Alexandre Julliard's avatar
Alexandre Julliard committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
            if (mode != STRETCH_DELETESCANS)
                memset( rowDst, (mode == STRETCH_ANDSCANS) ? 0xff : 0x00,
                        widthDst*sizeof(int) );

            /* Make copies of the destination row */

            pdata = dstImage->data + dstImage->bytes_per_line * y;
            while (((ysrc + yinc) >> 16 == ysrc >> 16) &&
                   (ydst < visRectDst->bottom-1))
            {
                memcpy( pdata + dstImage->bytes_per_line, pdata,
                        dstImage->bytes_per_line );
                pdata += dstImage->bytes_per_line;
                ysrc += yinc;
                ydst++;
            }
777
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
778
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
779
    else  /* Shrinking */
Alexandre Julliard's avatar
Alexandre Julliard committed
780
    {
781
        yinc = (heightDst << 16) / heightSrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
782
        ysrc = visRectSrc->top;
Alexandre Julliard's avatar
Alexandre Julliard committed
783 784 785 786 787 788 789 790
        ydst = ((heightDst << 16) - (yinc * heightSrc)) / 2;
        if (vswap)
        {
            ydst += yinc * (heightSrc - ysrc - 1);
            yinc = -yinc;
        }
        else
            ydst += yinc * ysrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
791

Alexandre Julliard's avatar
Alexandre Julliard committed
792
        for( ; (ysrc < visRectSrc->bottom); ydst += yinc, ysrc++)
Alexandre Julliard's avatar
Alexandre Julliard committed
793
        {
Alexandre Julliard's avatar
Alexandre Julliard committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
            if (((ydst >> 16) < visRectDst->top) ||
                ((ydst >> 16) >= visRectDst->bottom)) continue;

            /* Retrieve a source row */
            BITBLT_GetRow( srcImage, rowSrc, ysrc - visRectSrc->top,
                           hswap ? widthSrc - visRectSrc->right
                                 : visRectSrc->left,
                           visRectSrc->right - visRectSrc->left,
                           dstImage->depth, foreground, background, hswap );

            /* Stretch or shrink it */
            if (hstretch)
                BITBLT_StretchRow( rowSrc, rowDst, visRectDst->left,
                                   visRectDst->right - visRectDst->left,
                                   xinc, xoff, mode );
            else BITBLT_ShrinkRow( rowSrc, rowDst,
                                   hswap ? widthSrc - visRectSrc->right
                                         : visRectSrc->left,
                                   visRectSrc->right - visRectSrc->left,
                                   xinc, xoff, mode );

            /* Merge several source rows into the destination */
Alexandre Julliard's avatar
Alexandre Julliard committed
816
            if (mode == STRETCH_DELETESCANS)
Alexandre Julliard's avatar
Alexandre Julliard committed
817
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
818
                /* Simply skip the overlapping rows */
Alexandre Julliard's avatar
Alexandre Julliard committed
819 820 821 822 823 824
                while (((ydst + yinc) >> 16 == ydst >> 16) &&
                       (ysrc < visRectSrc->bottom-1))
                {
                    ydst += yinc;
                    ysrc++;
                }
Alexandre Julliard's avatar
Alexandre Julliard committed
825
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
826 827 828
            else if (((ydst + yinc) >> 16 == ydst >> 16) &&
                     (ysrc < visRectSrc->bottom-1))
                continue;  /* Restart loop for next overlapping row */
829

Alexandre Julliard's avatar
Alexandre Julliard committed
830 831 832 833
            /* Store the destination row */
            pixel = rowDst + visRectDst->right - 1;
            y = (ydst >> 16) - visRectDst->top;
            for (x = visRectDst->right-visRectDst->left-1; x >= 0; x--)
Alexandre Julliard's avatar
Alexandre Julliard committed
834
                XPutPixel( dstImage, x, y, *pixel-- );
Alexandre Julliard's avatar
Alexandre Julliard committed
835 836 837
            if (mode != STRETCH_DELETESCANS)
                memset( rowDst, (mode == STRETCH_ANDSCANS) ? 0xff : 0x00,
                        widthDst*sizeof(int) );
Alexandre Julliard's avatar
Alexandre Julliard committed
838
        }
839
    }
840
    wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
841
    HeapFree( GetProcessHeap(), 0, rowSrc );
Alexandre Julliard's avatar
Alexandre Julliard committed
842 843 844 845
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
846
 *           BITBLT_GetSrcAreaStretch
Alexandre Julliard's avatar
Alexandre Julliard committed
847
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
848 849
 * Retrieve an area from the source DC, stretching and mapping all the
 * pixels to Windows colors.
Alexandre Julliard's avatar
Alexandre Julliard committed
850
 */
851
static int BITBLT_GetSrcAreaStretch( X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
Alexandre Julliard's avatar
Alexandre Julliard committed
852
                                      Pixmap pixmap, GC gc,
853 854 855 856 857
                                      INT xSrc, INT ySrc,
                                      INT widthSrc, INT heightSrc,
                                      INT xDst, INT yDst,
                                      INT widthDst, INT heightDst,
                                      RECT *visRectSrc, RECT *visRectDst )
Alexandre Julliard's avatar
Alexandre Julliard committed
858
{
Alexandre Julliard's avatar
Alexandre Julliard committed
859
    XImage *imageSrc, *imageDst;
860 861
    RECT rectSrc = *visRectSrc;
    RECT rectDst = *visRectDst;
862
    int fg, bg;
Alexandre Julliard's avatar
Alexandre Julliard committed
863 864 865 866 867

    if (widthSrc < 0) xSrc += widthSrc;
    if (widthDst < 0) xDst += widthDst;
    if (heightSrc < 0) ySrc += heightSrc;
    if (heightDst < 0) yDst += heightDst;
868 869 870 871 872 873 874 875
    rectSrc.left   -= xSrc;
    rectSrc.right  -= xSrc;
    rectSrc.top    -= ySrc;
    rectSrc.bottom -= ySrc;
    rectDst.left   -= xDst;
    rectDst.right  -= xDst;
    rectDst.top    -= yDst;
    rectDst.bottom -= yDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
876

877
    get_colors(physDevDst, physDevSrc, &fg, &bg);
878
    wine_tsx11_lock();
Alexandre Julliard's avatar
Alexandre Julliard committed
879
    /* FIXME: avoid BadMatch errors */
880
    imageSrc = XGetImage( gdi_display, physDevSrc->drawable,
881 882
                          physDevSrc->dc_rect.left + visRectSrc->left,
                          physDevSrc->dc_rect.top + visRectSrc->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
883 884 885
                          visRectSrc->right - visRectSrc->left,
                          visRectSrc->bottom - visRectSrc->top,
                          AllPlanes, ZPixmap );
886 887
    wine_tsx11_unlock();

888
    imageDst = X11DRV_DIB_CreateXImage( rectDst.right - rectDst.left,
889
                                        rectDst.bottom - rectDst.top, physDevDst->depth );
Alexandre Julliard's avatar
Alexandre Julliard committed
890 891
    BITBLT_StretchImage( imageSrc, imageDst, widthSrc, heightSrc,
                         widthDst, heightDst, &rectSrc, &rectDst,
892 893
                         fg, physDevDst->depth != 1 ?
                         bg : physDevSrc->backgroundPixel,
894
                         GetStretchBltMode(physDevDst->hdc) );
895
    wine_tsx11_lock();
896
    XPutImage( gdi_display, pixmap, gc, imageDst, 0, 0, 0, 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
897
               rectDst.right - rectDst.left, rectDst.bottom - rectDst.top );
Alexandre Julliard's avatar
Alexandre Julliard committed
898
    XDestroyImage( imageSrc );
899
    X11DRV_DIB_DestroyXImage( imageDst );
900
    wine_tsx11_unlock();
901
    return 0;  /* no exposure events generated */
Alexandre Julliard's avatar
Alexandre Julliard committed
902
}
Alexandre Julliard's avatar
Alexandre Julliard committed
903

Alexandre Julliard's avatar
Alexandre Julliard committed
904 905 906 907 908 909 910

/***********************************************************************
 *           BITBLT_GetSrcArea
 *
 * Retrieve an area from the source DC, mapping all the
 * pixels to Windows colors.
 */
911
static int BITBLT_GetSrcArea( X11DRV_PDEVICE *physDevSrc, X11DRV_PDEVICE *physDevDst,
912
                              Pixmap pixmap, GC gc, RECT *visRectSrc )
Alexandre Julliard's avatar
Alexandre Julliard committed
913 914
{
    XImage *imageSrc, *imageDst;
915
    register INT x, y;
916
    int exposures = 0;
917 918
    INT width  = visRectSrc->right - visRectSrc->left;
    INT height = visRectSrc->bottom - visRectSrc->top;
919
    int fg, bg;
920
    BOOL memdc = (GetObjectType(physDevSrc->hdc) == OBJ_MEMDC);
Alexandre Julliard's avatar
Alexandre Julliard committed
921

922
    if (physDevSrc->depth == physDevDst->depth)
Alexandre Julliard's avatar
Alexandre Julliard committed
923
    {
924
        wine_tsx11_lock();
925
        if (!X11DRV_PALETTE_XPixelToPalette ||
926
            (physDevDst->depth == 1))  /* monochrome -> monochrome */
Alexandre Julliard's avatar
Alexandre Julliard committed
927
        {
928
            if (physDevDst->depth == 1)
929 930
            {
                /* MSDN says if StretchBlt must convert a bitmap from monochrome
Austin English's avatar
Austin English committed
931
                   to color or vice versa, the foreground and background color of
932
                   the device context are used.  In fact, it also applies to the
933
                   case when it is converted from mono to mono. */
934 935 936
                XSetBackground( gdi_display, gc, physDevDst->textPixel );
                XSetForeground( gdi_display, gc, physDevDst->backgroundPixel );
                XCopyPlane( gdi_display, physDevSrc->drawable, pixmap, gc,
937 938
                            physDevSrc->dc_rect.left + visRectSrc->left,
                            physDevSrc->dc_rect.top + visRectSrc->top,
939 940 941
                            width, height, 0, 0, 1);
            }
            else
942
                XCopyArea( gdi_display, physDevSrc->drawable, pixmap, gc,
943 944
                           physDevSrc->dc_rect.left + visRectSrc->left,
                           physDevSrc->dc_rect.top + visRectSrc->top,
945
                           width, height, 0, 0);
946
            exposures++;
Alexandre Julliard's avatar
Alexandre Julliard committed
947 948 949
        }
        else  /* color -> color */
        {
950
            if (memdc)
951
                imageSrc = XGetImage( gdi_display, physDevSrc->drawable,
952 953
                                      physDevSrc->dc_rect.left + visRectSrc->left,
                                      physDevSrc->dc_rect.top + visRectSrc->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
954 955 956 957
                                      width, height, AllPlanes, ZPixmap );
            else
            {
                /* Make sure we don't get a BadMatch error */
958
                XCopyArea( gdi_display, physDevSrc->drawable, pixmap, gc,
959 960
                           physDevSrc->dc_rect.left + visRectSrc->left,
                           physDevSrc->dc_rect.top + visRectSrc->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
961
                           width, height, 0, 0);
962
                exposures++;
963
                imageSrc = XGetImage( gdi_display, pixmap, 0, 0, width, height,
Alexandre Julliard's avatar
Alexandre Julliard committed
964 965
                                      AllPlanes, ZPixmap );
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
966 967
            for (y = 0; y < height; y++)
                for (x = 0; x < width; x++)
Alexandre Julliard's avatar
Alexandre Julliard committed
968
                    XPutPixel(imageSrc, x, y,
969
                              X11DRV_PALETTE_XPixelToPalette[XGetPixel(imageSrc, x, y)]);
970
            XPutImage( gdi_display, pixmap, gc, imageSrc,
Alexandre Julliard's avatar
Alexandre Julliard committed
971
                       0, 0, 0, 0, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
972
            XDestroyImage( imageSrc );
Alexandre Julliard's avatar
Alexandre Julliard committed
973
        }
974
        wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
975 976 977
    }
    else
    {
978
        if (physDevSrc->depth == 1)  /* monochrome -> color */
Alexandre Julliard's avatar
Alexandre Julliard committed
979
        {
980 981
	    get_colors(physDevDst, physDevSrc, &fg, &bg);

982
            wine_tsx11_lock();
983
            if (X11DRV_PALETTE_XPixelToPalette)
Alexandre Julliard's avatar
Alexandre Julliard committed
984
            {
985
                XSetBackground( gdi_display, gc,
986
                             X11DRV_PALETTE_XPixelToPalette[fg] );
987
                XSetForeground( gdi_display, gc,
988
                             X11DRV_PALETTE_XPixelToPalette[bg]);
Alexandre Julliard's avatar
Alexandre Julliard committed
989
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
990 991
            else
            {
992 993
                XSetBackground( gdi_display, gc, fg );
                XSetForeground( gdi_display, gc, bg );
Alexandre Julliard's avatar
Alexandre Julliard committed
994
            }
995
            XCopyPlane( gdi_display, physDevSrc->drawable, pixmap, gc,
996 997
                        physDevSrc->dc_rect.left + visRectSrc->left,
                        physDevSrc->dc_rect.top + visRectSrc->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
998
                        width, height, 0, 0, 1 );
999
            exposures++;
1000
            wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1001 1002 1003
        }
        else  /* color -> monochrome */
        {
1004
            wine_tsx11_lock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1005
            /* FIXME: avoid BadMatch error */
1006
            imageSrc = XGetImage( gdi_display, physDevSrc->drawable,
1007 1008
                                  physDevSrc->dc_rect.left + visRectSrc->left,
                                  physDevSrc->dc_rect.top + visRectSrc->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
1009
                                  width, height, AllPlanes, ZPixmap );
1010 1011
            if (!imageSrc)
            {
1012
                wine_tsx11_unlock();
1013 1014
                return exposures;
            }
1015
            imageDst = X11DRV_DIB_CreateXImage( width, height, physDevDst->depth );
1016
            if (!imageDst)
1017 1018
            {
                XDestroyImage(imageSrc);
1019
                wine_tsx11_unlock();
1020 1021
                return exposures;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1022 1023
            for (y = 0; y < height; y++)
                for (x = 0; x < width; x++)
Alexandre Julliard's avatar
Alexandre Julliard committed
1024
                    XPutPixel(imageDst, x, y, (XGetPixel(imageSrc,x,y) ==
1025
                                               physDevSrc->backgroundPixel) );
1026
            XPutImage( gdi_display, pixmap, gc, imageDst,
Alexandre Julliard's avatar
Alexandre Julliard committed
1027
                       0, 0, 0, 0, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
1028
            XDestroyImage( imageSrc );
1029
            X11DRV_DIB_DestroyXImage( imageDst );
1030
            wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1031
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1032
    }
1033
    return exposures;
Alexandre Julliard's avatar
Alexandre Julliard committed
1034 1035 1036 1037
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1038
 *           BITBLT_GetDstArea
Alexandre Julliard's avatar
Alexandre Julliard committed
1039
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1040 1041
 * Retrieve an area from the destination DC, mapping all the
 * pixels to Windows colors.
Alexandre Julliard's avatar
Alexandre Julliard committed
1042
 */
1043
static int BITBLT_GetDstArea(X11DRV_PDEVICE *physDev, Pixmap pixmap, GC gc, RECT *visRectDst)
Alexandre Julliard's avatar
Alexandre Julliard committed
1044
{
1045
    int exposures = 0;
1046 1047
    INT width  = visRectDst->right - visRectDst->left;
    INT height = visRectDst->bottom - visRectDst->top;
1048 1049 1050
    BOOL memdc = (GetObjectType( physDev->hdc ) == OBJ_MEMDC);

    wine_tsx11_lock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1051

1052
    if (!X11DRV_PALETTE_XPixelToPalette || (physDev->depth == 1) ||
1053
	(X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) )
Alexandre Julliard's avatar
Alexandre Julliard committed
1054
    {
1055
        XCopyArea( gdi_display, physDev->drawable, pixmap, gc,
1056
                   physDev->dc_rect.left + visRectDst->left, physDev->dc_rect.top + visRectDst->top,
1057
                   width, height, 0, 0 );
1058
        exposures++;
Alexandre Julliard's avatar
Alexandre Julliard committed
1059 1060 1061
    }
    else
    {
1062
        register INT x, y;
Alexandre Julliard's avatar
Alexandre Julliard committed
1063 1064
        XImage *image;

1065
        if (memdc)
1066
            image = XGetImage( gdi_display, physDev->drawable,
1067 1068
                               physDev->dc_rect.left + visRectDst->left,
                               physDev->dc_rect.top + visRectDst->top,
Alexandre Julliard's avatar
Alexandre Julliard committed
1069 1070 1071 1072
                               width, height, AllPlanes, ZPixmap );
        else
        {
            /* Make sure we don't get a BadMatch error */
1073
            XCopyArea( gdi_display, physDev->drawable, pixmap, gc,
1074 1075
                       physDev->dc_rect.left + visRectDst->left,
                       physDev->dc_rect.top + visRectDst->top,
1076
                       width, height, 0, 0);
1077
            exposures++;
1078
            image = XGetImage( gdi_display, pixmap, 0, 0, width, height,
Alexandre Julliard's avatar
Alexandre Julliard committed
1079 1080
                               AllPlanes, ZPixmap );
        }
1081 1082 1083 1084 1085 1086 1087 1088 1089
        if (image)
        {
            for (y = 0; y < height; y++)
                for (x = 0; x < width; x++)
                    XPutPixel( image, x, y,
                               X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y )]);
            XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, width, height );
            XDestroyImage( image );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1090
    }
1091 1092

    wine_tsx11_unlock();
1093
    return exposures;
Alexandre Julliard's avatar
Alexandre Julliard committed
1094 1095 1096 1097 1098 1099 1100 1101 1102
}


/***********************************************************************
 *           BITBLT_PutDstArea
 *
 * Put an area back into the destination DC, mapping the pixel
 * colors to X pixels.
 */
1103
static int BITBLT_PutDstArea(X11DRV_PDEVICE *physDev, Pixmap pixmap, RECT *visRectDst)
Alexandre Julliard's avatar
Alexandre Julliard committed
1104
{
1105
    int exposures = 0;
1106 1107
    INT width  = visRectDst->right - visRectDst->left;
    INT height = visRectDst->bottom - visRectDst->top;
Alexandre Julliard's avatar
Alexandre Julliard committed
1108

1109
    /* !X11DRV_PALETTE_PaletteToXPixel is _NOT_ enough */
Alexandre Julliard's avatar
Alexandre Julliard committed
1110

1111
    if (!X11DRV_PALETTE_PaletteToXPixel || (physDev->depth == 1) ||
1112
        (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_VIRTUAL) )
Alexandre Julliard's avatar
Alexandre Julliard committed
1113
    {
1114
        XCopyArea( gdi_display, pixmap, physDev->drawable, physDev->gc, 0, 0, width, height,
1115 1116
                   physDev->dc_rect.left + visRectDst->left,
                   physDev->dc_rect.top + visRectDst->top );
1117
        exposures++;
Alexandre Julliard's avatar
Alexandre Julliard committed
1118
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1119
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
1120
    {
1121
        register INT x, y;
1122
        XImage *image = XGetImage( gdi_display, pixmap, 0, 0, width, height,
Alexandre Julliard's avatar
Alexandre Julliard committed
1123 1124 1125 1126
                                   AllPlanes, ZPixmap );
        for (y = 0; y < height; y++)
            for (x = 0; x < width; x++)
            {
Alexandre Julliard's avatar
Alexandre Julliard committed
1127
                XPutPixel( image, x, y,
1128
                           X11DRV_PALETTE_PaletteToXPixel[XGetPixel( image, x, y )]);
Alexandre Julliard's avatar
Alexandre Julliard committed
1129
            }
1130
        XPutImage( gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1131 1132
                   physDev->dc_rect.left + visRectDst->left,
                   physDev->dc_rect.top + visRectDst->top, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
1133
        XDestroyImage( image );
Alexandre Julliard's avatar
Alexandre Julliard committed
1134
    }
1135
    return exposures;
Alexandre Julliard's avatar
Alexandre Julliard committed
1136 1137 1138
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1139
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1140 1141 1142 1143
 *           BITBLT_GetVisRectangles
 *
 * Get the source and destination visible rectangles for StretchBlt().
 * Return FALSE if one of the rectangles is empty.
Alexandre Julliard's avatar
Alexandre Julliard committed
1144
 */
1145
static BOOL BITBLT_GetVisRectangles( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
1146
                                     INT widthDst, INT heightDst,
1147
                                     X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
1148 1149
                                     INT widthSrc, INT heightSrc,
                                     RECT *visRectSrc, RECT *visRectDst )
Alexandre Julliard's avatar
Alexandre Julliard committed
1150
{
1151
    RECT rect, clipRect;
Alexandre Julliard's avatar
Alexandre Julliard committed
1152

Alexandre Julliard's avatar
Alexandre Julliard committed
1153
      /* Get the destination visible rectangle */
Alexandre Julliard's avatar
Alexandre Julliard committed
1154

1155 1156 1157 1158
    rect.left   = xDst;
    rect.top    = yDst;
    rect.right  = xDst + widthDst;
    rect.bottom = yDst + heightDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
1159 1160
    if (widthDst < 0) SWAP_INT32( &rect.left, &rect.right );
    if (heightDst < 0) SWAP_INT32( &rect.top, &rect.bottom );
1161
    GetRgnBox( physDevDst->region, &clipRect );
1162
    if (!IntersectRect( visRectDst, &rect, &clipRect )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1163 1164

      /* Get the source visible rectangle */
Alexandre Julliard's avatar
Alexandre Julliard committed
1165

1166
    if (!physDevSrc) return TRUE;
1167 1168 1169 1170
    rect.left   = xSrc;
    rect.top    = ySrc;
    rect.right  = xSrc + widthSrc;
    rect.bottom = ySrc + heightSrc;
Alexandre Julliard's avatar
Alexandre Julliard committed
1171 1172
    if (widthSrc < 0) SWAP_INT32( &rect.left, &rect.right );
    if (heightSrc < 0) SWAP_INT32( &rect.top, &rect.bottom );
1173
    /* Apparently the clipping and visible regions are only for output,
1174
       so just check against dc extent here to avoid BadMatch errors */
1175 1176 1177
    clipRect = physDevSrc->drawable_rect;
    OffsetRect( &clipRect, -(physDevSrc->drawable_rect.left + physDevSrc->dc_rect.left),
                -(physDevSrc->drawable_rect.top + physDevSrc->dc_rect.top) );
1178
    if (!IntersectRect( visRectSrc, &rect, &clipRect ))
1179
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1180

Alexandre Julliard's avatar
Alexandre Julliard committed
1181
      /* Intersect the rectangles */
Alexandre Julliard's avatar
Alexandre Julliard committed
1182

Alexandre Julliard's avatar
Alexandre Julliard committed
1183
    if ((widthSrc == widthDst) && (heightSrc == heightDst)) /* no stretching */
Alexandre Julliard's avatar
Alexandre Julliard committed
1184
    {
1185 1186 1187 1188
        visRectSrc->left   += xDst - xSrc;
        visRectSrc->right  += xDst - xSrc;
        visRectSrc->top    += yDst - ySrc;
        visRectSrc->bottom += yDst - ySrc;
1189
        if (!IntersectRect( &rect, visRectSrc, visRectDst )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1190
        *visRectSrc = *visRectDst = rect;
1191 1192 1193 1194
        visRectSrc->left   += xSrc - xDst;
        visRectSrc->right  += xSrc - xDst;
        visRectSrc->top    += ySrc - yDst;
        visRectSrc->bottom += ySrc - yDst;
Alexandre Julliard's avatar
Alexandre Julliard committed
1195
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1196
    else  /* stretching */
Alexandre Julliard's avatar
Alexandre Julliard committed
1197
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1198 1199 1200 1201 1202 1203 1204
        /* Map source rectangle into destination coordinates */
        rect.left = xDst + (visRectSrc->left - xSrc)*widthDst/widthSrc;
        rect.top = yDst + (visRectSrc->top - ySrc)*heightDst/heightSrc;
        rect.right = xDst + ((visRectSrc->right - xSrc)*widthDst)/widthSrc;
        rect.bottom = yDst + ((visRectSrc->bottom - ySrc)*heightDst)/heightSrc;
        if (rect.left > rect.right) SWAP_INT32( &rect.left, &rect.right );
        if (rect.top > rect.bottom) SWAP_INT32( &rect.top, &rect.bottom );
1205 1206 1207 1208 1209 1210

        /* Avoid rounding errors */
        rect.left--;
        rect.top--;
        rect.right++;
        rect.bottom++;
1211
        if (!IntersectRect( visRectDst, &rect, visRectDst )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1212 1213 1214 1215 1216 1217 1218 1219 1220

        /* Map destination rectangle back to source coordinates */
        rect = *visRectDst;
        rect.left = xSrc + (visRectDst->left - xDst)*widthSrc/widthDst;
        rect.top = ySrc + (visRectDst->top - yDst)*heightSrc/heightDst;
        rect.right = xSrc + ((visRectDst->right - xDst)*widthSrc)/widthDst;
        rect.bottom = ySrc + ((visRectDst->bottom - yDst)*heightSrc)/heightDst;
        if (rect.left > rect.right) SWAP_INT32( &rect.left, &rect.right );
        if (rect.top > rect.bottom) SWAP_INT32( &rect.top, &rect.bottom );
1221 1222 1223 1224 1225 1226

        /* Avoid rounding errors */
        rect.left--;
        rect.top--;
        rect.right++;
        rect.bottom++;
1227
        if (!IntersectRect( visRectSrc, &rect, visRectSrc )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1228
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1229 1230 1231 1232 1233
    return TRUE;
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1234 1235 1236
 *           BITBLT_InternalStretchBlt
 *
 * Implementation of PatBlt(), BitBlt() and StretchBlt().
Alexandre Julliard's avatar
Alexandre Julliard committed
1237
 */
1238 1239 1240 1241 1242
static BOOL BITBLT_InternalStretchBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
                                       INT widthDst, INT heightDst,
                                       X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
                                       INT widthSrc, INT heightSrc,
                                       DWORD rop )
Alexandre Julliard's avatar
Alexandre Julliard committed
1243
{
1244 1245 1246
    BOOL usePat, useSrc, useDst, destUsed, fStretch, fNullBrush;
    RECT visRectDst, visRectSrc;
    INT width, height;
Alexandre Julliard's avatar
Alexandre Julliard committed
1247
    const BYTE *opcode;
Alexandre Julliard's avatar
Alexandre Julliard committed
1248
    Pixmap pixmaps[3] = { 0, 0, 0 };  /* pixmaps for DST, SRC, TMP */
Alexandre Julliard's avatar
Alexandre Julliard committed
1249
    GC tmpGC = 0;
1250
    POINT pts[2];
Alexandre Julliard's avatar
Alexandre Julliard committed
1251

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
    /* compensate for off-by-one shifting for negative widths and heights */
    if (widthDst < 0)
        ++xDst;
    if (heightDst < 0)
        ++yDst;
    if (widthSrc < 0)
        ++xSrc;
    if (heightSrc < 0)
        ++ySrc;

Alexandre Julliard's avatar
Alexandre Julliard committed
1262 1263 1264
    usePat = (((rop >> 4) & 0x0f0000) != (rop & 0x0f0000));
    useSrc = (((rop >> 2) & 0x330000) != (rop & 0x330000));
    useDst = (((rop >> 1) & 0x550000) != (rop & 0x550000));
1265
    if (!physDevSrc && useSrc) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1266

Alexandre Julliard's avatar
Alexandre Julliard committed
1267
      /* Map the coordinates to device coords */
Alexandre Julliard's avatar
Alexandre Julliard committed
1268

1269 1270 1271 1272 1273 1274 1275 1276 1277
    pts[0].x = xDst;
    pts[0].y = yDst;
    pts[1].x = xDst + widthDst;
    pts[1].y = yDst + heightDst;
    LPtoDP(physDevDst->hdc, pts, 2);
    xDst      = pts[0].x;
    yDst      = pts[0].y;
    widthDst  = pts[1].x - pts[0].x;
    heightDst = pts[1].y - pts[0].y;
Alexandre Julliard's avatar
Alexandre Julliard committed
1278

1279
    TRACE("    rectdst=%d,%d-%d,%d orgdst=%d,%d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1280
                    xDst, yDst, widthDst, heightDst,
1281
                    physDevDst->dc_rect.left, physDevDst->dc_rect.top );
Alexandre Julliard's avatar
Alexandre Julliard committed
1282

Alexandre Julliard's avatar
Alexandre Julliard committed
1283
    if (useSrc)
Alexandre Julliard's avatar
Alexandre Julliard committed
1284
    {
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
        pts[0].x = xSrc;
        pts[0].y = ySrc;
        pts[1].x = xSrc + widthSrc;
        pts[1].y = ySrc + heightSrc;
        LPtoDP(physDevSrc->hdc, pts, 2);
        xSrc      = pts[0].x;
        ySrc      = pts[0].y;
        widthSrc  = pts[1].x - pts[0].x;
        heightSrc = pts[1].y - pts[0].y;

Alexandre Julliard's avatar
Alexandre Julliard committed
1295
        fStretch  = (widthSrc != widthDst) || (heightSrc != heightDst);
1296
        TRACE("    rectsrc=%d,%d-%d,%d orgsrc=%d,%d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1297
                        xSrc, ySrc, widthSrc, heightSrc,
1298
                        physDevSrc->dc_rect.left, physDevSrc->dc_rect.top );
1299 1300
        if (!BITBLT_GetVisRectangles( physDevDst, xDst, yDst, widthDst, heightDst,
                                      physDevSrc, xSrc, ySrc, widthSrc, heightSrc,
Alexandre Julliard's avatar
Alexandre Julliard committed
1301 1302
                                      &visRectSrc, &visRectDst ))
            return TRUE;
1303
        TRACE("    vissrc=%d,%d-%d,%d visdst=%d,%d-%d,%d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1304 1305 1306 1307
                        visRectSrc.left, visRectSrc.top,
                        visRectSrc.right, visRectSrc.bottom,
                        visRectDst.left, visRectDst.top,
                        visRectDst.right, visRectDst.bottom );
Alexandre Julliard's avatar
Alexandre Julliard committed
1308 1309 1310 1311
    }
    else
    {
        fStretch = FALSE;
1312
        if (!BITBLT_GetVisRectangles( physDevDst, xDst, yDst, widthDst, heightDst,
Alexandre Julliard's avatar
Alexandre Julliard committed
1313 1314
                                      NULL, 0, 0, 0, 0, NULL, &visRectDst ))
            return TRUE;
1315
        TRACE("    vissrc=none visdst=%d,%d-%d,%d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
1316 1317
                        visRectDst.left, visRectDst.top,
                        visRectDst.right, visRectDst.bottom );
Alexandre Julliard's avatar
Alexandre Julliard committed
1318 1319
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1320 1321
    width  = visRectDst.right - visRectDst.left;
    height = visRectDst.bottom - visRectDst.top;
Alexandre Julliard's avatar
Alexandre Julliard committed
1322

1323
    opcode = BITBLT_Opcodes[(rop >> 16) & 0xff];
1324

Austin English's avatar
Austin English committed
1325
    /* a few optimizations for single-op ROPs */
1326 1327 1328
    if (!fStretch && !opcode[1])
    {
        if (OP_SRCDST(*opcode) == OP_ARGS(PAT,DST))
Alexandre Julliard's avatar
Alexandre Julliard committed
1329
        {
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
            switch(rop)  /* a few special cases */
            {
            case BLACKNESS:  /* 0x00 */
            case WHITENESS:  /* 0xff */
                if ((physDevDst->depth != 1) && X11DRV_PALETTE_PaletteToXPixel)
                {
                    wine_tsx11_lock();
                    XSetFunction( gdi_display, physDevDst->gc, GXcopy );
                    if (rop == BLACKNESS)
                        XSetForeground( gdi_display, physDevDst->gc, X11DRV_PALETTE_PaletteToXPixel[0] );
                    else
                        XSetForeground( gdi_display, physDevDst->gc,
                                        WhitePixel( gdi_display, DefaultScreen(gdi_display) ));
                    XSetFillStyle( gdi_display, physDevDst->gc, FillSolid );
                    XFillRectangle( gdi_display, physDevDst->drawable, physDevDst->gc,
                                    physDevDst->dc_rect.left + visRectDst.left,
                                    physDevDst->dc_rect.top + visRectDst.top,
                                    width, height );
                    wine_tsx11_unlock();
                    return TRUE;
                }
                break;
            case DSTINVERT:  /* 0x55 */
                if (!(X11DRV_PALETTE_PaletteFlags & (X11DRV_PALETTE_PRIVATE | X11DRV_PALETTE_VIRTUAL)))
                {
                    /* Xor is much better when we do not have full colormap.   */
                    /* Using white^black ensures that we invert at least black */
                    /* and white. */
                    unsigned long xor_pix = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
                                             BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
                    wine_tsx11_lock();
                    XSetFunction( gdi_display, physDevDst->gc, GXxor );
                    XSetForeground( gdi_display, physDevDst->gc, xor_pix);
                    XSetFillStyle( gdi_display, physDevDst->gc, FillSolid );
                    XFillRectangle( gdi_display, physDevDst->drawable, physDevDst->gc,
                                    physDevDst->dc_rect.left + visRectDst.left,
                                    physDevDst->dc_rect.top + visRectDst.top,
                                    width, height );
                    wine_tsx11_unlock();
                    return TRUE;
                }
                break;
            }
            if (!usePat || X11DRV_SetupGCForBrush( physDevDst ))
            {
                wine_tsx11_lock();
                XSetFunction( gdi_display, physDevDst->gc, OP_ROP(*opcode) );
                XFillRectangle( gdi_display, physDevDst->drawable, physDevDst->gc,
                                physDevDst->dc_rect.left + visRectDst.left,
                                physDevDst->dc_rect.top + visRectDst.top,
                                width, height );
                wine_tsx11_unlock();
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1383 1384
            return TRUE;
        }
1385
        else if (OP_SRCDST(*opcode) == OP_ARGS(SRC,DST))
Alexandre Julliard's avatar
Alexandre Julliard committed
1386
        {
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
            if (physDevSrc->depth == physDevDst->depth)
            {
                wine_tsx11_lock();
                XSetFunction( gdi_display, physDevDst->gc, OP_ROP(*opcode) );
                XCopyArea( gdi_display, physDevSrc->drawable,
                           physDevDst->drawable, physDevDst->gc,
                           physDevSrc->dc_rect.left + visRectSrc.left,
                           physDevSrc->dc_rect.top + visRectSrc.top,
                           width, height,
                           physDevDst->dc_rect.left + visRectDst.left,
                           physDevDst->dc_rect.top + visRectDst.top );
                physDevDst->exposures++;
                wine_tsx11_unlock();
                return TRUE;
            }
            if (physDevSrc->depth == 1)
            {
                int fg, bg;
                get_colors(physDevDst, physDevSrc, &fg, &bg);
                wine_tsx11_lock();

                XSetBackground( gdi_display, physDevDst->gc, fg );
                XSetForeground( gdi_display, physDevDst->gc, bg );
                XSetFunction( gdi_display, physDevDst->gc, OP_ROP(*opcode) );
                XCopyPlane( gdi_display, physDevSrc->drawable,
                            physDevDst->drawable, physDevDst->gc,
                            physDevSrc->dc_rect.left + visRectSrc.left,
                            physDevSrc->dc_rect.top + visRectSrc.top,
                            width, height,
                            physDevDst->dc_rect.left + visRectDst.left,
                            physDevDst->dc_rect.top + visRectDst.top, 1 );
                physDevDst->exposures++;
                wine_tsx11_unlock();
                return TRUE;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1422
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1423
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1424

1425
    wine_tsx11_lock();
1426
    tmpGC = XCreateGC( gdi_display, physDevDst->drawable, 0, NULL );
1427
    XSetSubwindowMode( gdi_display, tmpGC, IncludeInferiors );
1428 1429
    XSetGraphicsExposures( gdi_display, tmpGC, False );
    pixmaps[DST] = XCreatePixmap( gdi_display, root_window, width, height,
1430
                                  physDevDst->depth );
1431 1432
    wine_tsx11_unlock();

Alexandre Julliard's avatar
Alexandre Julliard committed
1433
    if (useSrc)
Alexandre Julliard's avatar
Alexandre Julliard committed
1434
    {
1435
        wine_tsx11_lock();
1436
        pixmaps[SRC] = XCreatePixmap( gdi_display, root_window, width, height,
1437
                                      physDevDst->depth );
1438
        wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1439
        if (fStretch)
1440
            BITBLT_GetSrcAreaStretch( physDevSrc, physDevDst, pixmaps[SRC], tmpGC,
Alexandre Julliard's avatar
Alexandre Julliard committed
1441 1442 1443 1444
                                      xSrc, ySrc, widthSrc, heightSrc,
                                      xDst, yDst, widthDst, heightDst,
                                      &visRectSrc, &visRectDst );
        else
1445
            BITBLT_GetSrcArea( physDevSrc, physDevDst, pixmaps[SRC], tmpGC,
1446
                               &visRectSrc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1447
    }
1448

1449 1450
    if (useDst) BITBLT_GetDstArea( physDevDst, pixmaps[DST], tmpGC, &visRectDst );
    if (usePat) fNullBrush = !X11DRV_SetupGCForPatBlt( physDevDst, tmpGC, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1451
    else fNullBrush = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1452 1453
    destUsed = FALSE;

1454
    wine_tsx11_lock();
1455
    for ( ; *opcode; opcode++)
Alexandre Julliard's avatar
Alexandre Julliard committed
1456
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1457
        if (OP_DST(*opcode) == DST) destUsed = TRUE;
1458
        XSetFunction( gdi_display, tmpGC, OP_ROP(*opcode) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1459
        switch(OP_SRCDST(*opcode))
Alexandre Julliard's avatar
Alexandre Julliard committed
1460 1461 1462
        {
        case OP_ARGS(DST,TMP):
        case OP_ARGS(SRC,TMP):
Alexandre Julliard's avatar
Alexandre Julliard committed
1463
            if (!pixmaps[TMP])
1464
                pixmaps[TMP] = XCreatePixmap( gdi_display, root_window,
1465
                                              width, height, physDevDst->depth );
Alexandre Julliard's avatar
Alexandre Julliard committed
1466 1467
            /* fall through */
        case OP_ARGS(DST,SRC):
Alexandre Julliard's avatar
Alexandre Julliard committed
1468
        case OP_ARGS(SRC,DST):
Alexandre Julliard's avatar
Alexandre Julliard committed
1469 1470
        case OP_ARGS(TMP,SRC):
        case OP_ARGS(TMP,DST):
1471 1472 1473 1474
	    if (useSrc)
		XCopyArea( gdi_display, pixmaps[OP_SRC(*opcode)],
			   pixmaps[OP_DST(*opcode)], tmpGC,
			   0, 0, width, height, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1475 1476 1477
            break;

        case OP_ARGS(PAT,TMP):
Alexandre Julliard's avatar
Alexandre Julliard committed
1478
            if (!pixmaps[TMP] && !fNullBrush)
1479
                pixmaps[TMP] = XCreatePixmap( gdi_display, root_window,
1480
                                              width, height, physDevDst->depth );
Alexandre Julliard's avatar
Alexandre Julliard committed
1481
            /* fall through */
Alexandre Julliard's avatar
Alexandre Julliard committed
1482 1483
        case OP_ARGS(PAT,DST):
        case OP_ARGS(PAT,SRC):
Alexandre Julliard's avatar
Alexandre Julliard committed
1484
            if (!fNullBrush)
1485
                XFillRectangle( gdi_display, pixmaps[OP_DST(*opcode)],
Alexandre Julliard's avatar
Alexandre Julliard committed
1486
                                tmpGC, 0, 0, width, height );
Alexandre Julliard's avatar
Alexandre Julliard committed
1487 1488 1489
            break;
        }
    }
1490
    XSetFunction( gdi_display, physDevDst->gc, GXcopy );
1491 1492
    physDevDst->exposures += BITBLT_PutDstArea( physDevDst, pixmaps[destUsed ? DST : SRC],
                                                &visRectDst );
1493 1494 1495 1496
    XFreePixmap( gdi_display, pixmaps[DST] );
    if (pixmaps[SRC]) XFreePixmap( gdi_display, pixmaps[SRC] );
    if (pixmaps[TMP]) XFreePixmap( gdi_display, pixmaps[TMP] );
    XFreeGC( gdi_display, tmpGC );
1497
    wine_tsx11_unlock();
Alexandre Julliard's avatar
Alexandre Julliard committed
1498
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1499
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1500 1501


Alexandre Julliard's avatar
Alexandre Julliard committed
1502
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1503
 *           X11DRV_PatBlt
Alexandre Julliard's avatar
Alexandre Julliard committed
1504
 */
1505
BOOL X11DRV_PatBlt( X11DRV_PDEVICE *physDev, INT left, INT top, INT width, INT height, DWORD rop )
Alexandre Julliard's avatar
Alexandre Julliard committed
1506
{
1507
    BOOL result;
1508

1509
    X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod );
1510 1511
    result = BITBLT_InternalStretchBlt( physDev, left, top, width, height, NULL, 0, 0, 0, 0, rop );
    X11DRV_UnlockDIBSection( physDev, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1512
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1513 1514
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1515

1516 1517 1518 1519 1520 1521 1522 1523 1524
/***********************************************************************
 *           X11DRV_ClientSideDIBCopy
 */
static BOOL X11DRV_ClientSideDIBCopy( X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
                                      X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
                                      INT width, INT height )
{
    DIBSECTION srcDib, dstDib;
    BYTE *srcPtr, *dstPtr;
1525
    INT srcRowOffset, dstRowOffset;
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
    INT bytesPerPixel;
    INT bytesToCopy;
    INT y;
    static RECT unusedRect;

    if (GetObjectW(physDevSrc->bitmap->hbitmap, sizeof(srcDib), &srcDib) != sizeof(srcDib))
      return FALSE;
    if (GetObjectW(physDevDst->bitmap->hbitmap, sizeof(dstDib), &dstDib) != sizeof(dstDib))
      return FALSE;

    /* check for oversized values, just like X11DRV_DIB_CopyDIBSection() */
    if (xSrc > srcDib.dsBm.bmWidth || ySrc > srcDib.dsBm.bmHeight)
      return FALSE;
    if (xSrc + width > srcDib.dsBm.bmWidth)
      width = srcDib.dsBm.bmWidth - xSrc;
    if (ySrc + height > srcDib.dsBm.bmHeight)
      height = srcDib.dsBm.bmHeight - ySrc;

1544
    if (GetRgnBox(physDevDst->region, &unusedRect) == COMPLEXREGION)
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
    {
      /* for simple regions, the clipping was already done by BITBLT_GetVisRectangles */
      FIXME("potential optimization: client-side complex region clipping\n");
      return FALSE;
    }
    if (dstDib.dsBm.bmBitsPixel <= 8)
    {
      FIXME("potential optimization: client-side color-index mode DIB copy\n");
      return FALSE;
    }
    if (!(srcDib.dsBmih.biCompression == BI_BITFIELDS &&
          dstDib.dsBmih.biCompression == BI_BITFIELDS &&
          !memcmp(srcDib.dsBitfields, dstDib.dsBitfields, 3*sizeof(DWORD)))
        && !(srcDib.dsBmih.biCompression == BI_RGB &&
             dstDib.dsBmih.biCompression == BI_RGB))
    {
      FIXME("potential optimization: client-side compressed DIB copy\n");
      return FALSE;
    }
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    if (srcDib.dsBm.bmBitsPixel != dstDib.dsBm.bmBitsPixel)
    {
      FIXME("potential optimization: pixel format conversion\n");
      return FALSE;
    }
    if (srcDib.dsBmih.biWidth < 0 || dstDib.dsBmih.biWidth < 0)
    {
      FIXME("negative widths not yet implemented\n");
      return FALSE;
    }
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593

    switch (dstDib.dsBm.bmBitsPixel)
    {
      case 15:
      case 16:
        bytesPerPixel = 2;
        break;
      case 24:
        bytesPerPixel = 3;
        break;
      case 32:
        bytesPerPixel = 4;
        break;
      default:
        FIXME("don't know how to work with a depth of %d\n", physDevSrc->depth);
        return FALSE;
    }

    bytesToCopy = width * bytesPerPixel;

1594 1595 1596 1597 1598 1599 1600
    if (srcDib.dsBmih.biHeight < 0)
    {
      srcPtr = &physDevSrc->bitmap->base[ySrc*srcDib.dsBm.bmWidthBytes + xSrc*bytesPerPixel];
      srcRowOffset = srcDib.dsBm.bmWidthBytes;
    }
    else
    {
1601
      srcPtr = &physDevSrc->bitmap->base[(srcDib.dsBm.bmHeight-ySrc-1)*srcDib.dsBm.bmWidthBytes
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
        + xSrc*bytesPerPixel];
      srcRowOffset = -srcDib.dsBm.bmWidthBytes;
    }
    if (dstDib.dsBmih.biHeight < 0)
    {
      dstPtr = &physDevDst->bitmap->base[yDst*dstDib.dsBm.bmWidthBytes + xDst*bytesPerPixel];
      dstRowOffset = dstDib.dsBm.bmWidthBytes;
    }
    else
    {
1612
      dstPtr = &physDevDst->bitmap->base[(dstDib.dsBm.bmHeight-yDst-1)*dstDib.dsBm.bmWidthBytes
1613 1614 1615
        + xDst*bytesPerPixel];
      dstRowOffset = -dstDib.dsBm.bmWidthBytes;
    }
1616

1617 1618 1619 1620 1621 1622 1623 1624 1625
    /* Handle overlapping regions on the same DIB */
    if (physDevSrc == physDevDst && ySrc < yDst)
    {
      srcPtr += srcRowOffset * (height - 1);
      srcRowOffset = -srcRowOffset;
      dstPtr += dstRowOffset * (height - 1);
      dstRowOffset = -dstRowOffset;
    }

1626 1627
    for (y = yDst; y < yDst + height; ++y)
    {
1628
      memmove(dstPtr, srcPtr, bytesToCopy);
1629 1630
      srcPtr += srcRowOffset;
      dstPtr += dstRowOffset;
1631 1632 1633 1634 1635 1636
    }

    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1637
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1638
 *           X11DRV_BitBlt
Alexandre Julliard's avatar
Alexandre Julliard committed
1639
 */
1640 1641 1642
BOOL X11DRV_BitBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
                    INT width, INT height, X11DRV_PDEVICE *physDevSrc,
                    INT xSrc, INT ySrc, DWORD rop )
Alexandre Julliard's avatar
Alexandre Julliard committed
1643
{
1644 1645
    BOOL result = FALSE;
    INT sSrc, sDst;
1646
    RECT visRectDst, visRectSrc;
1647 1648 1649 1650 1651 1652 1653

    if (((rop >> 16) & 0x55) == ((rop >> 17) & 0x55)) {
      /* FIXME: seems the ROP doesn't include destination;
       * now if the destination area include the entire dcDst,
       * we can pass TRUE instead of FALSE to CoerceDIBSection(dcDst...),
       * which may avoid a copy in some situations */
    }
1654

1655
    sDst = X11DRV_LockDIBSection( physDevDst, DIB_Status_None );
1656
    if (physDevDst != physDevSrc)
1657
        sSrc = X11DRV_LockDIBSection( physDevSrc, DIB_Status_None );
1658 1659
    else
        sSrc = sDst;
1660

1661
    if ((sSrc == DIB_Status_AppMod) && (rop == SRCCOPY) &&
1662
        (physDevSrc->depth == physDevDst->depth))
1663
    {
1664
      POINT pts[2];
1665
      /* do everything ourselves; map coordinates */
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683

      pts[0].x = xSrc;
      pts[0].y = ySrc;
      pts[1].x = xSrc + width;
      pts[1].y = ySrc + height;

      LPtoDP(physDevSrc->hdc, pts, 2);
      width = pts[1].x - pts[0].x;
      height = pts[1].y - pts[0].y;
      xSrc = pts[0].x;
      ySrc = pts[0].y;

      pts[0].x = xDst;
      pts[0].y = yDst;
      LPtoDP(physDevDst->hdc, pts, 1);

      xDst = pts[0].x;
      yDst = pts[0].y;
1684 1685

      /* Perform basic clipping */
1686 1687
      if (!BITBLT_GetVisRectangles( physDevDst, xDst, yDst, width, height,
                                    physDevSrc, xSrc, ySrc, width, height,
1688
                                    &visRectSrc, &visRectDst ))
1689 1690
      {
        result = TRUE;
1691
        goto END;
1692
      }
1693

1694 1695 1696 1697 1698 1699 1700
      xSrc = visRectSrc.left;
      ySrc = visRectSrc.top;
      xDst = visRectDst.left;
      yDst = visRectDst.top;
      width = visRectDst.right - visRectDst.left;
      height = visRectDst.bottom - visRectDst.top;

1701
      if (sDst == DIB_Status_AppMod) {
1702 1703 1704 1705 1706 1707
        result = X11DRV_ClientSideDIBCopy( physDevSrc, xSrc, ySrc,
                                           physDevDst, xDst, yDst,
                                           width, height );
        if (result)
          goto END;
        /* fall back to X server copying */
1708
      }
1709
      X11DRV_CoerceDIBSection( physDevDst, DIB_Status_GdiMod );
1710

1711 1712 1713 1714
      wine_tsx11_lock();
      XSetFunction( gdi_display, physDevDst->gc, GXcopy );
      wine_tsx11_unlock();

1715
      X11DRV_DIB_CopyDIBSection( physDevSrc, physDevDst, xSrc, ySrc, xDst, yDst, width, height );
1716
      result = TRUE;
1717
      goto END;
1718
    }
1719

1720
    X11DRV_CoerceDIBSection( physDevDst, DIB_Status_GdiMod );
1721
    if (physDevDst != physDevSrc)
1722
      X11DRV_CoerceDIBSection( physDevSrc, DIB_Status_GdiMod );
1723

1724 1725
    result = BITBLT_InternalStretchBlt( physDevDst, xDst, yDst, width, height,
                                        physDevSrc, xSrc, ySrc, width, height, rop );
1726 1727

END:
1728 1729
    if (physDevDst != physDevSrc)
      X11DRV_UnlockDIBSection( physDevSrc, FALSE );
1730
    X11DRV_UnlockDIBSection( physDevDst, TRUE );
1731

Alexandre Julliard's avatar
Alexandre Julliard committed
1732
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1733 1734
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1735

Alexandre Julliard's avatar
Alexandre Julliard committed
1736
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1737
 *           X11DRV_StretchBlt
Alexandre Julliard's avatar
Alexandre Julliard committed
1738
 */
1739 1740 1741 1742
BOOL X11DRV_StretchBlt( X11DRV_PDEVICE *physDevDst, INT xDst, INT yDst,
                        INT widthDst, INT heightDst,
                        X11DRV_PDEVICE *physDevSrc, INT xSrc, INT ySrc,
                        INT widthSrc, INT heightSrc, DWORD rop )
Alexandre Julliard's avatar
Alexandre Julliard committed
1743
{
1744
    BOOL result;
1745

1746
    X11DRV_LockDIBSection( physDevDst, DIB_Status_GdiMod );
1747
    if (physDevDst != physDevSrc)
1748
      X11DRV_LockDIBSection( physDevSrc, DIB_Status_GdiMod );
1749

1750 1751
    result = BITBLT_InternalStretchBlt( physDevDst, xDst, yDst, widthDst, heightDst,
                                        physDevSrc, xSrc, ySrc, widthSrc, heightSrc, rop );
1752

1753 1754
    if (physDevDst != physDevSrc)
      X11DRV_UnlockDIBSection( physDevSrc, FALSE );
1755
    X11DRV_UnlockDIBSection( physDevDst, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1756
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
1757
}