move around - flatter.
[profile/ivi/evas.git] / src / modules / engines / software_16_wince / evas_wince_ddraw_buffer.cpp
1
2 #include <iostream>
3
4 #define WIN32_LEAN_AND_MEAN
5 #include <windows.h>
6 #undef WIN32_LEAN_AND_MEAN
7 #include <ddraw.h>
8
9 #include "evas_common.h"
10 #include "evas_engine.h"
11
12
13 typedef LONG (*fct_DirectDrawCreate)(LPGUID, LPUNKNOWN *, LPUNKNOWN *);
14
15 fct_DirectDrawCreate lib_DirectDrawCreate;
16
17 typedef struct Evas_Engine_WinCE_DDraw_Priv Evas_Engine_WinCE_DDraw_Priv;
18
19 struct Evas_Engine_WinCE_DDraw_Priv
20 {
21    HMODULE             module;
22    LPDIRECTDRAW        object;
23    LPDIRECTDRAWSURFACE surface;
24    int                 width;
25    int                 height;
26    int                 stride;
27 };
28
29 void *
30 evas_software_wince_ddraw_init(HWND window,
31                                int  width,
32                                int  height)
33 {
34    DDSURFACEDESC                 surface_desc;
35    Evas_Engine_WinCE_DDraw_Priv *priv;
36    HRESULT                       res;
37
38    priv = (Evas_Engine_WinCE_DDraw_Priv *)malloc(sizeof(Evas_Engine_WinCE_DDraw_Priv));
39    if (!priv)
40      return NULL;
41
42    priv->module = LoadLibrary(L"ddraw.dll");
43    if (!priv->module)
44      {
45         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not load ddraw.dll" << std::endl;
46         goto free_priv;
47      }
48
49    lib_DirectDrawCreate = (fct_DirectDrawCreate)GetProcAddress(priv->module, L"DirectDrawCreate");
50    if (!lib_DirectDrawCreate)
51      {
52         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not initialize DirectDraw" << std::endl;
53         goto free_lib;
54      }
55
56    res = lib_DirectDrawCreate(NULL, (IUnknown**)&priv->object, NULL);
57    if (FAILED(res))
58      {
59         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not create DirectDraw object" << std::endl;
60         goto free_lib;
61      }
62
63    res = priv->object->SetCooperativeLevel(window, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
64    if (FAILED(res))
65      {
66         std::cerr<< "[Evas] [Engine] [WinCE DDraw] Can not set window to fullscreen" << std::endl;
67         goto release_object;
68      }
69
70    memset(&surface_desc, 0, sizeof(surface_desc));
71    surface_desc.dwSize = sizeof(surface_desc);
72    surface_desc.dwFlags = DDSD_CAPS;
73    surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
74
75    res = priv->object->CreateSurface(&surface_desc, &priv->surface, NULL);
76    if (FAILED(res))
77      {
78         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not create surface" << std::endl;
79         goto release_object;
80      }
81
82    memset(&surface_desc, 0, sizeof(surface_desc));
83    surface_desc.dwSize = sizeof(surface_desc);
84    res = priv->surface->Lock(NULL, &surface_desc, DDLOCK_READONLY, NULL);
85    if (FAILED(res))
86      {
87         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not lock surface" << std::endl;
88         goto release_surface;
89      }
90
91    priv->width = surface_desc.dwWidth;
92    priv->height = surface_desc.dwHeight;
93    priv->stride = surface_desc.lPitch / 2;
94
95    if ((priv->width != width) ||
96        (priv->height != height))
97      {
98         std::cerr << "[Evas] [Engine] [WinCE DDraw] Size mismatch" << std::endl;
99         std::cerr << "[Evas] [Engine] [WinCE DDraw] asked: " << width << "x" << height << std::endl;
100         std::cerr << "[Evas] [Engine] [WinCE DDraw] got  : " << priv->width << "x" << priv->height << std::endl;
101         goto release_surface;
102      }
103
104    res = priv->surface->Unlock(NULL);
105    if (FAILED(res))
106      {
107         std::cerr << "[Evas] [Engine] [WinCE DDraw] Can not unlock surface" << std::endl;
108         goto release_surface;
109      }
110
111    return priv;
112
113  release_surface:
114    priv->surface->Release();
115  release_object:
116    priv->object->Release();
117  free_lib:
118    FreeLibrary(priv->module);
119  free_priv:
120    free(priv);
121
122   return 0;
123 }
124
125 void
126 evas_software_wince_ddraw_shutdown(void *priv)
127 {
128    ((Evas_Engine_WinCE_DDraw_Priv *)priv)->surface->Release();
129    ((Evas_Engine_WinCE_DDraw_Priv *)priv)->object->Release();
130    FreeLibrary(((Evas_Engine_WinCE_DDraw_Priv *)priv)->module);
131    free(priv);
132 }
133
134
135 FB_Output_Buffer *
136 evas_software_wince_ddraw_output_buffer_new(void *priv,
137                                             int   width,
138                                             int   height)
139 {
140    FB_Output_Buffer *fbob;
141    void             *buffer;
142
143    fbob = (FB_Output_Buffer *)calloc(1, sizeof(FB_Output_Buffer));
144    if (!fbob) return NULL;
145
146    buffer = malloc (width * height * 2); /* we are sure to have 16bpp */
147    if (!buffer)
148      {
149         free(fbob);
150         return NULL;
151      }
152
153    fbob->priv = priv;
154
155    fbob->im = (Soft16_Image *) evas_cache_image_data(evas_common_soft16_image_cache_get(), width, height, (DATA32 *)buffer, 0, EVAS_COLORSPACE_RGB565_A5P);
156    if (fbob->im)
157      fbob->im->stride = ((Evas_Engine_WinCE_DDraw_Priv *)priv)->stride;
158
159    return fbob;
160 }
161
162 void
163 evas_software_wince_ddraw_output_buffer_free(FB_Output_Buffer *fbob)
164 {
165    free(fbob->im->pixels);
166    free(fbob);
167 }
168
169 void
170 evas_software_wince_ddraw_output_buffer_paste(FB_Output_Buffer *fbob)
171 {
172    DDSURFACEDESC                 surface_desc;
173    Evas_Engine_WinCE_DDraw_Priv *priv;
174    HRESULT                       res;
175
176    priv = (Evas_Engine_WinCE_DDraw_Priv *)fbob->priv;
177
178    memset(&surface_desc, 0, sizeof(surface_desc));
179    surface_desc.dwSize = sizeof(surface_desc);
180    res = priv->surface->Lock(NULL, &surface_desc, DDLOCK_WRITEONLY, NULL);
181    if (FAILED(res))
182      return;
183
184    if ((fbob->im->cache_entry.w == surface_desc.dwWidth) &&
185        (fbob->im->cache_entry.h == surface_desc.dwHeight))
186      memcpy(surface_desc.lpSurface, fbob->im->pixels,
187             surface_desc.dwWidth * surface_desc.dwHeight * 2);
188
189    priv->surface->Unlock(NULL);
190 }
191
192 void
193 evas_software_wince_ddraw_surface_resize(FB_Output_Buffer *fbob)
194 {
195 }