wined3d: Add missing GL locking to ffp_blit_set() and ffp_blit_unset().
[wine] / dlls / gdi32 / enhmfdrv / bitblt.c
1 /*
2  * Enhanced MetaFile driver BitBlt functions
3  *
4  * Copyright 2002 Huw D M Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "enhmetafiledrv.h"
28 #include "wine/debug.h"
29
30 BOOL CDECL EMFDRV_PatBlt( PHYSDEV dev, INT left, INT top,
31                           INT width, INT height, DWORD rop )
32 {
33     EMRBITBLT emr;
34     BOOL ret;
35
36     emr.emr.iType = EMR_BITBLT;
37     emr.emr.nSize = sizeof(emr);
38     emr.rclBounds.left = left;
39     emr.rclBounds.top = top;
40     emr.rclBounds.right = left + width - 1;
41     emr.rclBounds.bottom = top + height - 1;
42     emr.xDest = left;
43     emr.yDest = top;
44     emr.cxDest = width;
45     emr.cyDest = height;
46     emr.dwRop = rop;
47     emr.xSrc = 0;
48     emr.ySrc = 0;
49     emr.xformSrc.eM11 = 1.0;
50     emr.xformSrc.eM12 = 0.0;
51     emr.xformSrc.eM21 = 0.0;
52     emr.xformSrc.eM22 = 1.0;
53     emr.xformSrc.eDx = 0.0;
54     emr.xformSrc.eDy = 0.0;
55     emr.crBkColorSrc = 0;
56     emr.iUsageSrc = 0;
57     emr.offBmiSrc = 0;
58     emr.cbBmiSrc = 0;
59     emr.offBitsSrc = 0;
60     emr.cbBitsSrc = 0;
61
62     ret = EMFDRV_WriteRecord( dev, &emr.emr );
63     if(ret)
64         EMFDRV_UpdateBBox( dev, &emr.rclBounds );
65     return ret;
66 }
67
68 /* Utilitarian function used by EMFDRV_BitBlt and EMFDRV_StretchBlt */
69
70 static BOOL EMFDRV_BitBlockTransfer( 
71     PHYSDEV devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,  
72     PHYSDEV devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, DWORD rop,
73     DWORD emrType)
74 {
75     BOOL ret;
76     PEMRBITBLT pEMR;
77     UINT emrSize;
78     UINT bmiSize;
79     UINT bitsSize;
80     UINT size;
81     BITMAP  BM;
82     WORD nBPP;
83     LPBITMAPINFOHEADER lpBmiH;
84     EMFDRV_PDEVICE* physDevSrc = (EMFDRV_PDEVICE*)devSrc;
85     HBITMAP hBitmap = GetCurrentObject(physDevSrc->hdc, OBJ_BITMAP);
86
87     if (emrType == EMR_BITBLT)
88         emrSize = sizeof(EMRBITBLT);
89     else if (emrType == EMR_STRETCHBLT)
90         emrSize = sizeof(EMRSTRETCHBLT);
91     else
92         return FALSE;
93
94     if(sizeof(BITMAP) != GetObjectW(hBitmap, sizeof(BITMAP), &BM))
95         return FALSE;
96
97     nBPP = BM.bmPlanes * BM.bmBitsPixel;
98     if(nBPP > 8) nBPP = 24; /* FIXME Can't get 16bpp to work for some reason */
99
100     bitsSize = DIB_GetDIBWidthBytes(BM.bmWidth, nBPP) * BM.bmHeight;
101     bmiSize = sizeof(BITMAPINFOHEADER) + 
102         (nBPP <= 8 ? 1 << nBPP : 0) * sizeof(RGBQUAD);
103     size = emrSize + bmiSize + bitsSize;
104
105     pEMR = HeapAlloc(GetProcessHeap(), 0, size);
106     if (!pEMR) return FALSE;
107
108     /* Initialize EMR */
109     pEMR->emr.iType = emrType;
110     pEMR->emr.nSize = size;
111     pEMR->rclBounds.left = xDst;
112     pEMR->rclBounds.top = yDst;
113     pEMR->rclBounds.right = xDst + widthDst - 1;
114     pEMR->rclBounds.bottom = yDst + heightDst - 1;
115     pEMR->xDest = xDst;
116     pEMR->yDest = yDst;
117     pEMR->cxDest = widthDst;
118     pEMR->cyDest = heightDst;
119     pEMR->dwRop = rop;
120     pEMR->xSrc = xSrc;
121     pEMR->ySrc = ySrc;
122     pEMR->xformSrc.eM11 = 1.0;  /** FIXME:           */
123     pEMR->xformSrc.eM12 = 0.0;  /** Setting default  */
124     pEMR->xformSrc.eM21 = 0.0;  /** value.           */
125     pEMR->xformSrc.eM22 = 1.0;  /** Where should we  */
126     pEMR->xformSrc.eDx = 0.0;   /** get that info    */
127     pEMR->xformSrc.eDy = 0.0;   /** ????             */
128     pEMR->crBkColorSrc = GetBkColor(physDevSrc->hdc);
129     pEMR->iUsageSrc = DIB_RGB_COLORS;
130     pEMR->offBmiSrc = emrSize;
131     pEMR->cbBmiSrc = bmiSize;
132     pEMR->offBitsSrc = emrSize + bmiSize;
133     pEMR->cbBitsSrc = bitsSize;
134     if (emrType == EMR_STRETCHBLT) 
135     {
136         PEMRSTRETCHBLT pEMRStretch = (PEMRSTRETCHBLT)pEMR;
137         pEMRStretch->cxSrc = widthSrc;
138         pEMRStretch->cySrc = heightSrc;
139     }
140
141     /* Initialize BITMAPINFO structure */
142     lpBmiH = (LPBITMAPINFOHEADER)((BYTE*)pEMR + pEMR->offBmiSrc);
143
144     lpBmiH->biSize = sizeof(BITMAPINFOHEADER); 
145     lpBmiH->biWidth =  BM.bmWidth;
146     lpBmiH->biHeight = BM.bmHeight;
147     lpBmiH->biPlanes = BM.bmPlanes;
148     lpBmiH->biBitCount = nBPP;
149     /* Assume the bitmap isn't compressed and set the BI_RGB flag. */
150     lpBmiH->biCompression = BI_RGB;
151     lpBmiH->biSizeImage = bitsSize;
152     lpBmiH->biYPelsPerMeter = /* 1 meter  = 39.37 inch */
153         MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSX),3937,100);
154     lpBmiH->biXPelsPerMeter = 
155         MulDiv(GetDeviceCaps(physDevSrc->hdc,LOGPIXELSY),3937,100);
156     lpBmiH->biClrUsed   = nBPP <= 8 ? 1 << nBPP : 0;
157     /* Set biClrImportant to 0, indicating that all of the 
158        device colors are important. */
159     lpBmiH->biClrImportant = 0; 
160
161     /* Initialize bitmap bits */
162     if (GetDIBits(physDevSrc->hdc, hBitmap, 0, (UINT)lpBmiH->biHeight,
163                   (BYTE*)pEMR + pEMR->offBitsSrc,
164                   (LPBITMAPINFO)lpBmiH, DIB_RGB_COLORS))
165     {
166         ret = EMFDRV_WriteRecord(devDst, (EMR*)pEMR);
167         if (ret) EMFDRV_UpdateBBox(devDst, &(pEMR->rclBounds));
168     } 
169     else
170         ret = FALSE;
171
172     HeapFree( GetProcessHeap(), 0, pEMR);
173     return ret;
174 }
175
176 BOOL CDECL EMFDRV_BitBlt(
177     PHYSDEV devDst, INT xDst, INT yDst, INT width, INT height,
178     PHYSDEV devSrc, INT xSrc, INT ySrc, DWORD rop)
179 {
180     return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, width, height,  
181                                     devSrc, xSrc, ySrc, width, height, 
182                                     rop, EMR_BITBLT );
183 }
184
185 BOOL CDECL EMFDRV_StretchBlt(
186     PHYSDEV devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,  
187     PHYSDEV devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, DWORD rop )
188 {
189     return EMFDRV_BitBlockTransfer( devDst, xDst, yDst, widthDst, heightDst,  
190                                     devSrc, xSrc, ySrc, widthSrc, heightSrc, 
191                                     rop, EMR_STRETCHBLT );
192 }
193
194 INT CDECL EMFDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
195                                       INT heightDst, INT xSrc, INT ySrc,
196                                       INT widthSrc, INT heightSrc,
197                                       const void *bits, const BITMAPINFO *info,
198                                       UINT wUsage, DWORD dwRop )
199 {
200     EMRSTRETCHDIBITS *emr;
201     BOOL ret;
202     UINT bmi_size=0, bits_size, emr_size;
203     
204     bits_size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
205                                      info->bmiHeader.biHeight,
206                                      info->bmiHeader.biBitCount);
207
208     /* calculate the size of the colour table */
209     bmi_size = bitmap_info_size(info, wUsage);
210
211     emr_size = sizeof (EMRSTRETCHDIBITS) + bmi_size + bits_size;
212     emr = HeapAlloc(GetProcessHeap(), 0, emr_size );
213     if (!emr) return 0;
214
215     /* write a bitmap info header (with colours) to the record */
216     memcpy( &emr[1], info, bmi_size);
217
218     /* write bitmap bits to the record */
219     memcpy ( ( (BYTE *) (&emr[1]) ) + bmi_size, bits, bits_size);
220
221     /* fill in the EMR header at the front of our piece of memory */
222     emr->emr.iType = EMR_STRETCHDIBITS;
223     emr->emr.nSize = emr_size;
224
225     emr->xDest     = xDst;
226     emr->yDest     = yDst;
227     emr->cxDest    = widthDst;
228     emr->cyDest    = heightDst;
229     emr->dwRop     = dwRop;
230     emr->xSrc      = xSrc; /* FIXME: only save the piece of the bitmap needed */
231     emr->ySrc      = ySrc;
232
233     emr->iUsageSrc    = wUsage;
234     emr->offBmiSrc    = sizeof (EMRSTRETCHDIBITS);
235     emr->cbBmiSrc     = bmi_size;
236     emr->offBitsSrc   = emr->offBmiSrc + bmi_size; 
237     emr->cbBitsSrc    = bits_size;
238
239     emr->cxSrc = widthSrc;
240     emr->cySrc = heightSrc;
241
242     emr->rclBounds.left   = xDst;
243     emr->rclBounds.top    = yDst;
244     emr->rclBounds.right  = xDst + widthDst;
245     emr->rclBounds.bottom = yDst + heightDst;
246
247     /* save the record we just created */
248     ret = EMFDRV_WriteRecord( dev, &emr->emr );
249     if(ret)
250         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
251
252     HeapFree(GetProcessHeap(), 0, emr);
253
254     return ret ? heightSrc : GDI_ERROR;
255 }
256
257 INT CDECL EMFDRV_SetDIBitsToDevice(
258     PHYSDEV dev, INT xDst, INT yDst, DWORD width, DWORD height,
259     INT xSrc, INT ySrc, UINT startscan, UINT lines,
260     LPCVOID bits, const BITMAPINFO *info, UINT wUsage )
261 {
262     EMRSETDIBITSTODEVICE* pEMR;
263     DWORD size, bmiSize, bitsSize;
264
265     bmiSize = bitmap_info_size(info, wUsage);
266     bitsSize = DIB_GetDIBImageBytes( info->bmiHeader.biWidth,
267                                      info->bmiHeader.biHeight,
268                                      info->bmiHeader.biBitCount );
269     size = sizeof(EMRSETDIBITSTODEVICE) + bmiSize + bitsSize;
270
271     pEMR = HeapAlloc(GetProcessHeap(), 0, size);
272     if (!pEMR) return 0;
273
274     pEMR->emr.iType = EMR_SETDIBITSTODEVICE;
275     pEMR->emr.nSize = size;
276     pEMR->rclBounds.left = xDst;
277     pEMR->rclBounds.top = yDst;
278     pEMR->rclBounds.right = xDst + width - 1;
279     pEMR->rclBounds.bottom = yDst + height - 1;
280     pEMR->xDest = xDst;
281     pEMR->yDest = yDst;
282     pEMR->xSrc = xSrc;
283     pEMR->ySrc = ySrc;
284     pEMR->cxSrc = width;
285     pEMR->cySrc = height;
286     pEMR->offBmiSrc = sizeof(EMRSETDIBITSTODEVICE);
287     pEMR->cbBmiSrc = bmiSize;
288     pEMR->offBitsSrc = sizeof(EMRSETDIBITSTODEVICE) + bmiSize;
289     pEMR->cbBitsSrc = bitsSize;
290     pEMR->iUsageSrc = wUsage;
291     pEMR->iStartScan = startscan;
292     pEMR->cScans = lines;
293     memcpy((BYTE*)pEMR + pEMR->offBmiSrc, info, bmiSize);
294     memcpy((BYTE*)pEMR + pEMR->offBitsSrc, bits, bitsSize);
295
296     if (EMFDRV_WriteRecord(dev, (EMR*)pEMR))
297         EMFDRV_UpdateBBox(dev, &(pEMR->rclBounds));
298
299     HeapFree( GetProcessHeap(), 0, pEMR);
300     return lines;
301 }