Fixing bugs in examples.
[framework/uifw/elementary.git] / src / examples / efl_thread_6.c
1 //Compile with:
2 //gcc -o efl_thread_6 efl_thread_6.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
4
5 static Evas_Object *win = NULL;
6 static Evas_Object *rect = NULL;
7
8 struct info
9 {
10    Evas_Object *obj;
11    int *pix;
12 };
13
14 // BEGIN - code running in my custom pthread instance
15 //
16 static void
17 mandel(int *pix, int w, int h)
18 {
19    double x, xx, y, cx, cy, cox, coy;
20    int iteration, hx, hy, val, r, g, b, rr, gg, bb;
21    int itermax = 10000;
22    double magnify = 0.02;
23
24    // this mandel calc is run in the worker threads so it's here. it is
25    // just here to calculate something and consume cpu to demonstrate the
26    // ecore thread worker queue. don't pay much attention to the below code
27    magnify += ((double)(rand() % 100) / 100.0) / 4.0;
28    cox = (double)(rand() % 100) / 100.0;
29    coy = (double)(rand() % 100) / 100.0;
30    cox /= (magnify * 3.0);
31    r = rand() % 255; g = rand() % 255; b = rand() % 255;
32    for (hy = 0; hy < h; hy++)
33      {
34         for (hx = 0; hx < w; hx++)
35           {
36              cx = (((float)hx) / ((float)w) - 0.5) / (magnify * 3.0);
37              cy = (((float)hy) / ((float)h) - 0.5) / (magnify * 3.0);
38              cx += cox;
39              cy += coy;
40              x = 0.0;
41              y = 0.0;
42              for (iteration = 1; iteration < itermax; iteration++)
43                {
44                   xx = (x * x) - (y * y) + cx;
45                   y = (2.0 * x * y) + cy;
46                   x = xx;
47                   if (((x * x) + (y * y)) > 100.0) iteration = 999999;
48                }
49              val = (((x * x) + (y * y)) * 2.55) / 100.0;
50              if (val > 255) val = 255;
51              if (iteration >= 99999) 
52                {
53                   rr = (r * val) / 255;
54                   gg = (g * val) / 255;
55                   bb = (b * val) / 255;
56                   pix[(hy * w) + hx] = 
57                      (val  << 24) | (rr << 16) | (gg << 8) | (bb);
58                }
59              else
60                 pix[(hy * w) + hx] = 0xffffffff;
61           }
62      }
63 }
64
65 static void
66 th_do(void *data, Ecore_Thread *th)
67 {
68    struct info *inf = data;
69    // CANNOT TOUCH inf->obj here! just inf->pix which is 256x256 @ 32bpp
70    // quick and dirty to consume some cpu - do a mandelbrot calc
71    mandel(inf->pix, 256, 256);
72 }
73 //
74 // END - code running in my custom pthread instance
75
76 static void // thread job finished - collect results and put in img obj
77 th_end(void *data, Ecore_Thread *th)
78 {
79    struct info *inf = data;
80
81    // copy data to object, free calculated data and info struc
82    evas_object_image_data_copy_set(inf->obj, inf->pix);
83    evas_object_show(inf->obj);
84    free(inf->pix);
85    free(inf);
86 }
87
88 static void // if the thread is cancelled - free pix, keep obj tho
89 th_cancel(void *data, Ecore_Thread *th)
90 {
91    struct info *inf = data;
92
93    // just free pixel data and info struct
94    free(inf->pix);
95    free(inf);
96 }
97
98 static Eina_Bool // animate the objects so you see all the madels move
99 anim(void *data)
100 {
101    Evas_Object *o = data;
102    double t, z;
103    int w, h, v;
104    Evas_Coord x, y;
105
106    // just calculate some position using the pointer value of the object as
107    // a seed value to make different objects go into different places over time
108    v = ((int)o) & 0xff;
109    t = ecore_loop_time_get();
110    w = 100 + ((v * 100) >> 8);
111    h = 100 + ((v * 100) >> 8);
112    z = (double)(v) / 100.0;
113    x = (w * sin(t));
114    y = (h * cos(t + z));
115    // do the actual move
116    evas_object_move(o, 200 + x - 128, 200 + y - 128);
117    // keep looping - return true
118    return EINA_TRUE;
119 }
120
121 int
122 elm_main(int argc, char **argv)
123 {
124    Evas_Object *o, *bg;
125    int i;
126
127    win = elm_win_add(NULL, "efl-thread-6", ELM_WIN_BASIC);
128    elm_win_title_set(win, "EFL Thread 6");
129    elm_win_autodel_set(win, EINA_TRUE);
130    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
131    evas_object_resize(win, 400, 400);
132    evas_object_show(win);
133
134    bg = elm_bg_add(win);
135    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
136    elm_win_resize_object_add(win, bg);
137    evas_object_show(bg);
138
139    // queue up 64 mandel generation thread jobs
140    for (i = 0; i < 64; i++)
141      {
142         struct info *inf;
143
144         // create ecore thread to do some threaded job inside the worker pool
145         inf = malloc(sizeof(struct info));
146         if (inf)
147           {
148              Evas_Object *o;
149
150              o = evas_object_image_filled_add(evas_object_evas_get(win));
151              evas_object_image_size_set(o, 256, 256);
152              evas_object_image_alpha_set(o, EINA_TRUE);
153              evas_object_resize(o, 256, 256);
154              inf->obj = o;
155              inf->pix = malloc(256 * 256 * sizeof(int));
156              ecore_thread_run(th_do, th_end, th_cancel, inf);
157              // bonus - slide the objects around all the time with an
158              // animator that ticks off every frame.
159              ecore_animator_add(anim, o);
160           }
161      }
162
163    elm_run();
164    return 0;
165 }
166
167 ELM_MAIN()
168