[FIX] cherry-pick d89d303d37d714ced2d1a9945ae2ea054c5824c0 (#8:3,4,5) + changes
[platform/core/system/swap-probe.git] / probe_ui / osp_capture.cpp
1 /*
2  *  DA probe
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: 
7  *
8  * Jaewon Lim <jaewon81.lim@samsung.com>
9  * Woojin Jung <woojin2.jung@samsung.com>
10  * Juyoung Kim <j0.kim@samsung.com>
11  * Anastasia Lyupa <a.lyupa@samsung.com>
12  * 
13  * This library is free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License as published by the
15  * Free Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  * 
18  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21  * License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this library; if not, write to the Free Software Foundation, Inc., 51
25  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  * - Samsung RnD Institute Russia
30  * 
31  */
32
33 #include <Evas.h>
34
35 #include <FUi.h>
36 //#include <FMedia.h>
37
38 #include "daprobe.h"
39 #include "dahelper.h"
40 #include "dacollection.h"
41 #include "daerror.h"
42 //#include "da_ui.h"
43 #include "osp_probe.h"
44
45 using namespace Tizen::Base;
46 using namespace Tizen::Ui;
47 using namespace Tizen::Graphics;
48 using namespace Tizen::Media;
49
50 /*
51 #define LIBUTILX        "libutilX.so.1"
52
53 typedef void* (*utilx_create_screen_shot_type)(Display* dpy, int width, int height);
54 typedef void (*utilx_release_screen_shot_type)(void);
55
56 static Tizen::Graphics::Bitmap* sysutil_capture_screen()
57 {
58         static utilx_create_screen_shot_type utilx_create_screen_shotp;
59         static utilx_release_screen_shot_type utilx_release_screen_shotp;
60         result r;
61         Bitmap* pBitmap = NULL;
62
63         if(unlikely(utilx_create_screen_shotp == NULL))
64         {
65                 void* lib_handle = dlopen(LIBUTILX, RTLD_LAZY);
66                 if(lib_handle == NULL) {
67                         SetLastResult(ERR_DLOPEN);
68                         return NULL;
69                 }
70                 utilx_create_screen_shotp = (utilx_create_screen_shot_type)dlsym(lib_handle, "utilx_create_screen_shot");
71                 if(utilx_create_screen_shotp == NULL || dlerror() != NULL) {
72                         SetLastResult(ERR_DLSYM);
73                         return NULL;
74                 }
75         }
76
77         if(unlikely(utilx_release_screen_shotp == NULL))
78         {
79                 void* lib_handle = dlopen(LIBUTILX, RTLD_LAZY);
80                 if(lib_handle == NULL) {
81                         SetLastResult(ERR_DLOPEN);
82                         return NULL;
83                 }
84                 utilx_release_screen_shotp = (utilx_release_screen_shot_type)dlsym(lib_handle, "utilx_release_screen_shot");
85                 if(utilx_release_screen_shotp == NULL || dlerror() != NULL) {
86                         SetLastResult(ERR_DLSYM);
87                         return NULL;
88                 }
89         }
90
91         Display* pDpy = XOpenDisplay(NULL);
92         if(pDpy == NULL)
93         {
94                 SetLastResult(ERR_USER - 1);
95                 return NULL;
96         }
97
98         int width = DisplayWidth(pDpy, DefaultScreen(pDpy));
99         int height = DisplayHeight(pDpy, DefaultScreen(pDpy));
100
101         void* pDump = utilx_create_screen_shotp(pDpy, width, height);
102         if(likely(pDump != NULL))
103         {
104                 ByteBuffer buffer;
105                 r = buffer.Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);\
106                 if(likely(r == E_SUCCESS))
107                 {
108                         Tizen::Graphics::Dimension dim(width, height);
109                         Bitmap* pBitmap  = new Bitmap();
110                         if(likely(pBitmap != NULL))
111                         {
112                                 r = pBitmap->Construct(buffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
113                                 if(unlikely(r != E_SUCCESS))
114                                 {
115                                         SetLastResult(r);
116                                         delete pBitmap;
117                                         pBitmap = NULL;
118                                 }
119                                 else { SetLastResult(E_SUCCESS); }
120                         }
121                         else { SetLastResult(ERR_OUTOFMEMORY); }
122                 }
123                 else { SetLastResult(r); }
124         }
125         else { SetLastResult(ERR_USER - 2); }
126
127         utilx_release_screen_shotp();
128
129         return pBitmap;
130 }
131
132 static Bitmap* getBitmapFromBuffer(void* pDump, int width, int height)
133 {
134         result r;
135         ByteBuffer buffer;
136         Bitmap* pBitmap = NULL;
137
138         r = buffer.Construct(static_cast<byte*>(pDump), 0, width*height*4, width*height*4);
139         if(likely(r == E_SUCCESS))
140         {
141                 Tizen::Graphics::Dimension dim(width, height);
142                 pBitmap  = new Bitmap();
143                 if(likely(pBitmap != NULL))
144                 {
145                         r = pBitmap->Construct(buffer, dim, BITMAP_PIXEL_FORMAT_ARGB8888);
146                         if(unlikely(r != E_SUCCESS))
147                         {
148                                 SetLastResult(r);
149                                 delete pBitmap;
150                                 pBitmap = NULL;
151                         }
152                         else { SetLastResult(E_SUCCESS); }
153                 }
154                 else { SetLastResult(ERR_OUTOFMEMORY); }
155         }
156         else { SetLastResult(r); }
157
158         return pBitmap;
159 }
160
161 // return 0 if succeed
162 // return -1 if failed to capture
163 int captureScreen()
164 {
165         Image img;
166         Bitmap* bitmap;
167         probeInfo_t     probeInfo; log_t log;
168         result r;
169         int ret = 0, width, height;
170         char* capbuf;
171
172         probeBlockStart();
173
174         capbuf = captureScreenShotX(&width, &height);
175         if(capbuf != NULL)
176         {
177                 bitmap = getBitmapFromBuffer((void*)capbuf, width, height);
178                 if(bitmap != NULL)
179                 {
180                         String dstPath;
181                         setProbePoint(&probeInfo);
182
183                         dstPath.Format(MAX_PATH_LENGTH, L"/tmp/da/%d.jpg", probeInfo.eventIndex);
184                         img.Construct();
185                         r = img.EncodeToFile(*bitmap, IMG_FORMAT_JPG, dstPath, true);
186                         if(r == E_SUCCESS)
187                         {
188                                 INIT_LOG;
189                                 APPEND_LOG_BASIC_NAME(LC_SNAPSHOT, "captureScreen");
190                                 APPEND_LOG_COMMON_NONE(0);
191                                 log.length += sprintf(log.data + log.length, "`,%S", dstPath.GetPointer());
192                                 printLog(&log, MSG_LOG);
193                         }
194                         else
195                         {
196                                 ret = -1;
197                         }
198
199                         delete bitmap;
200                 }
201                 else
202                 {
203                         char buf[128];
204                         r = GetLastResult();
205                         sprintf(buf, "bitmap is null (%p, %d, %s)\n", capbuf, (int)r, GetErrorMessage(r));
206                         ret = -1;
207                 }
208         }
209         else
210         {
211                 ret = -1;
212         }
213
214         releaseScreenShotX();
215         probeBlockEnd();
216
217         return ret;
218 }
219 */
220
221 namespace Tizen { namespace Ui {
222 /*
223 class _EcoreEvas
224 {
225 public:
226         Evas* GetEvas(void) const;
227 };
228
229 class _EcoreEvasMgr
230 {
231         void SetEcoreEvas(const _EcoreEvas& ecoreevas);
232 };
233
234
235 void _EcoreEvasMgr::SetEcoreEvas(const _EcoreEvas& ecoreevas)
236 {
237         typedef void (_EcoreEvasMgr::*methodType)(const _EcoreEvas& ecoreevas);
238         static methodType _ecoreevasmgr_setecoreevasp;
239
240         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui13_EcoreEvasMgr12SetEcoreEvasERKNS0_10_EcoreEvasE,
241                         LIBOSP_UIFW, _ecoreevasmgr_setecoreevasp);
242
243         probeBlockStart();
244         evas_event_callback_add(ecoreevas.GetEvas(),
245                 EVAS_CALLBACK_RENDER_FLUSH_POST, _cb_render_post, NULL);
246         probeBlockEnd();
247
248         (this->*_ecoreevasmgr_setecoreevasp)(ecoreevas);
249 }
250 */
251
252 result Control::SetShowState(bool state)
253 {
254         typedef result (Control::*methodType)(bool state);
255         static methodType control_setshowstatep;
256         result ret;
257
258         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui7Control12SetShowStateEb,
259                         LIBOSP_UIFW, control_setshowstatep);
260
261         ret = (this->*control_setshowstatep)(state);
262
263         probeBlockStart();
264         {
265                 char *type, *classname;
266                 if(find_object_hash((void*)this, &type, &classname) == 1)
267                 {
268                         if(strcmp(type, "Panel") == 0 || strcmp(type, "OverlayPanel") == 0 || strcmp(type, "ScrollPanel") == 0)
269                         {
270                                 SCREENSHOT_SET();
271 //                              SCREENSHOT_DONE();
272                         }
273                         else
274                         {
275                                 // do nothing
276                         }
277                 }
278                 else
279                 {
280                         // never happened
281                 }
282         }
283         probeBlockEnd();
284
285         return ret;
286 }
287
288 namespace Controls {
289
290 result Frame::SetCurrentForm(const Form& form)
291 {
292         typedef result (Frame::*methodType)(const Form& form);
293         static methodType frame_setcurrentformp;
294         result ret;
295
296         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui8Controls5Frame14SetCurrentFormERKNS1_4FormE,
297                         LIBOSP_UIFW, frame_setcurrentformp);
298
299         ret = (this->*frame_setcurrentformp)(form);
300
301         probeBlockStart();
302         SCREENSHOT_SET();
303 //      SCREENSHOT_DONE();
304         probeBlockEnd();
305
306         return ret;
307 }
308
309 result Frame::SetCurrentForm(Form* pForm)
310 {
311         typedef result (Frame::*methodType)(Form* pForm);
312         static methodType frame_setcurrentformp;
313         result ret;
314
315         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui8Controls5Frame14SetCurrentFormEPNS1_4FormE,
316                         LIBOSP_UIFW, frame_setcurrentformp);
317
318         ret = (this->*frame_setcurrentformp)(pForm);
319
320         probeBlockStart();
321         SCREENSHOT_SET();
322 //      SCREENSHOT_DONE();
323         probeBlockEnd();
324
325         return ret;
326 }
327
328
329 }               // end of namespace Tizen::Ui::Controls
330
331 namespace Animations {
332
333 result FrameAnimator::SetCurrentForm(const Tizen::Ui::Controls::Form& form)
334 {
335         typedef result (FrameAnimator::*methodType)(const Tizen::Ui::Controls::Form& form);
336         static methodType frameanimator_setcurrentformp;
337         result ret;
338
339         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui10Animations13FrameAnimator14SetCurrentFormERKNS0_8Controls4FormE,
340                         LIBOSP_UIFW, frameanimator_setcurrentformp);
341
342         ret = (this->*frameanimator_setcurrentformp)(form);
343
344         probeBlockStart();
345         SCREENSHOT_SET();
346 //      SCREENSHOT_DONE();
347         probeBlockEnd();
348
349         return ret;
350 }
351
352 result FrameAnimator::SetCurrentForm(Tizen::Ui::Controls::Form* pForm)
353 {
354         typedef result (FrameAnimator::*methodType)(Tizen::Ui::Controls::Form* pForm);
355         static methodType frameanimator_setcurrentformp;
356         result ret;
357
358         GET_REAL_FUNC_OSP(_ZN5Tizen2Ui10Animations13FrameAnimator14SetCurrentFormEPNS0_8Controls4FormE,
359                         LIBOSP_UIFW, frameanimator_setcurrentformp);
360
361         ret = (this->*frameanimator_setcurrentformp)(pForm);
362
363         probeBlockStart();
364         SCREENSHOT_SET();
365 //      SCREENSHOT_DONE();
366         probeBlockEnd();
367
368         return ret;
369 }
370
371
372 }               // end of namespace Tizen::Ui::Animations
373
374 } }             // end of namespace Tizen::Ui
375
376