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