e061106fba88b774874519e440e312cbef7d43af
[reactos.git] / reactos / subsys / win32k / eng / surface.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Driver Surace Functions
5 * FILE: subsys/win32k/eng/surface.c
6 * PROGRAMER: Jason Filby
7 * REVISION HISTORY:
8 * 3/7/1999: Created
9 */
10
11 #include <ddk/winddi.h>
12 #include "objects.h"
13
14 BYTE bytesPerPixel(ULONG Format)
15 {
16 // FIXME: GDI bitmaps are supposed to be pixel-packed. Right now if the
17 // pixel size if < 1 byte we expand it to 1 byte
18
19 if(Format==BMF_1BPP)
20 {
21 return 1;
22 } else
23 if((Format==BMF_4BPP) || (Format==BMF_4RLE))
24 {
25 return 1;
26 } else
27 if((Format==BMF_8BPP) || (Format==BMF_8RLE))
28 {
29 return 1;
30 } else
31 if(Format==BMF_16BPP)
32 {
33 return 2;
34 } else
35 if(Format==BMF_24BPP)
36 {
37 return 3;
38 } else
39 if(Format==BMF_32BPP)
40 {
41 return 4;
42 }
43
44 return 0;
45 }
46
47 VOID InitializeHooks(SURFGDI *SurfGDI)
48 {
49 SurfGDI->BitBlt = NULL;
50 SurfGDI->CopyBits = NULL;
51 }
52
53 HBITMAP EngCreateDeviceBitmap(DHSURF dhsurf, SIZEL Size, ULONG Format)
54 {
55 HBITMAP NewBitmap;
56 SURFOBJ *SurfObj;
57 SURFGDI *SurfGDI;
58
59 SurfObj = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFOBJ), NULL);
60 SurfGDI = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFGDI), NULL);
61
62 NewBitmap = CreateGDIHandle(SurfGDI, SurfObj);
63
64 InitializeHooks(SurfGDI);
65 SurfGDI->BytesPerPixel = bytesPerPixel(Format);
66
67 SurfObj->dhsurf = dhsurf;
68 SurfObj->hsurf = dhsurf; // FIXME: Is this correct??
69 SurfObj->sizlBitmap = Size;
70 SurfObj->iBitmapFormat = Format;
71 SurfObj->lDelta = SurfGDI->BytesPerPixel * Size.cx;
72 SurfObj->iType = STYPE_DEVBITMAP;
73
74 return NewBitmap;
75 }
76
77 HBITMAP EngCreateBitmap(IN SIZEL Size,
78 IN LONG Width,
79 IN ULONG Format,
80 IN ULONG Flags,
81 IN PVOID Bits)
82 {
83 HBITMAP NewBitmap;
84 SURFOBJ *SurfObj;
85 SURFGDI *SurfGDI;
86
87 SurfObj = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFOBJ), NULL);
88 SurfGDI = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFGDI), NULL);
89
90 NewBitmap = CreateGDIHandle(SurfGDI, SurfObj);
91
92 InitializeHooks(SurfGDI);
93 SurfGDI->BytesPerPixel = bytesPerPixel(Format);
94
95 SurfObj->cjBits = Width * Size.cy;
96
97 if(Bits!=NULL)
98 {
99 SurfObj->pvBits = Bits;
100 } else
101 {
102 if(Flags & BMF_USERMEM)
103 {
104 SurfObj->pvBits = EngAllocUserMem(SurfObj->cjBits, 0);
105 } else {
106 if(Flags & BMF_NOZEROINIT)
107 {
108 SurfObj->pvBits = EngAllocMem(0, SurfObj->cjBits, 0);
109 } else {
110 SurfObj->pvBits = EngAllocMem(FL_ZERO_MEMORY, SurfObj->cjBits, 0);
111 }
112 }
113 }
114
115 SurfObj->dhsurf = 0;
116 SurfObj->hsurf = 0;
117 SurfObj->sizlBitmap = Size;
118 SurfObj->iBitmapFormat = Format;
119 SurfObj->lDelta = Width;
120 SurfObj->iType = STYPE_BITMAP;
121
122 // Use flags to determine bitmap type -- TOP_DOWN or whatever
123
124 return NewBitmap;
125 }
126
127 HSURF EngCreateDeviceSurface(DHSURF dhsurf, SIZEL Size, ULONG Format)
128 {
129 HSURF NewSurface;
130 SURFOBJ *SurfObj;
131 SURFGDI *SurfGDI;
132
133 // DrvCreateDeviceSurface???
134
135 SurfObj = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFOBJ), NULL);
136 SurfGDI = EngAllocMem(FL_ZERO_MEMORY, sizeof(SURFGDI), NULL);
137
138 NewSurface = CreateGDIHandle(SurfGDI, SurfObj);
139
140 InitializeHooks(SurfGDI);
141 SurfGDI->BytesPerPixel = bytesPerPixel(Format);
142
143 SurfObj->dhsurf = dhsurf;
144 SurfObj->hsurf = dhsurf; // FIXME: Is this correct??
145 SurfObj->sizlBitmap = Size;
146 SurfObj->iBitmapFormat = Format;
147 SurfObj->lDelta = SurfGDI->BytesPerPixel * Size.cx;
148 SurfObj->iType = STYPE_DEVICE;
149
150 return NewSurface;
151 }
152
153 PFN DriverFunction(DRVENABLEDATA *DED, ULONG DriverFunc)
154 {
155 ULONG i;
156
157 for(i=0; i<DED->c; i++)
158 {
159 if(DED->pdrvfn[i].iFunc == DriverFunc)
160 return DED->pdrvfn[i].pfn;
161 }
162 return NULL;
163 }
164
165 BOOL EngAssociateSurface(HSURF Surface, HDEV Dev, ULONG Hooks)
166 {
167 SURFOBJ *SurfObj;
168 SURFGDI *SurfGDI;
169
170 DRVENABLEDATA *DED;
171
172 SurfGDI = AccessInternalObject(Surface);
173 SurfObj = AccessUserObject(Surface);
174
175 DED = AccessInternalObject(Dev);
176
177 // Associate the hdev
178 SurfObj->hdev = Dev;
179
180 // Hook up specified functions
181 if(Hooks & HOOK_BITBLT)
182 SurfGDI->BitBlt = DriverFunction(DED, INDEX_DrvBitBlt);
183 if(Hooks & HOOK_STRETCHBLT)
184 SurfGDI->StretchBlt = DriverFunction(DED, INDEX_DrvStretchBlt);
185 if(Hooks & HOOK_TEXTOUT)
186 SurfGDI->TextOut = DriverFunction(DED, INDEX_DrvTextOut);
187 if(Hooks & HOOK_PAINT)
188 SurfGDI->Paint = DriverFunction(DED, INDEX_DrvPaint);
189 if(Hooks & HOOK_STROKEPATH)
190 SurfGDI->StrokePath = DriverFunction(DED, INDEX_DrvStrokePath);
191 if(Hooks & HOOK_FILLPATH)
192 SurfGDI->FillPath = DriverFunction(DED, INDEX_DrvFillPath);
193 if(Hooks & HOOK_STROKEANDFILLPATH)
194 SurfGDI->StrokeAndFillPath = DriverFunction(DED, INDEX_DrvStrokeAndFillPath);
195 if(Hooks & HOOK_LINETO)
196 SurfGDI->LineTo = DriverFunction(DED, INDEX_DrvLineTo);
197 if(Hooks & HOOK_COPYBITS)
198 SurfGDI->CopyBits = DriverFunction(DED, INDEX_DrvCopyBits);
199 if(Hooks & HOOK_SYNCHRONIZE)
200 SurfGDI->Synchronize = DriverFunction(DED, INDEX_DrvSynchronize);
201 if(Hooks & HOOK_SYNCHRONIZEACCESS)
202 SurfGDI->SynchronizeAccess = TRUE;
203
204 return TRUE;
205 }
206
207 BOOL EngDeleteSurface(HSURF Surface)
208 {
209 SURFOBJ *SurfObj;
210 SURFGDI *SurfGDI;
211
212 SurfGDI = AccessInternalObject(Surface);
213 SurfObj = AccessUserObject(Surface);
214
215 EngFreeMem(SurfGDI);
216 EngFreeMem(SurfObj);
217 FreeGDIHandle(Surface);
218
219 return TRUE;
220 }
221
222 SURFOBJ *EngLockSurface(HSURF Surface)
223 {
224 // FIXME: Do we need to allocate new memory for this user object??
225
226 return AccessUserObject(Surface);
227 }