2 //gcc -o efl_thread_6 efl_thread_6.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
5 static Evas_Object *win = NULL;
6 static Evas_Object *rect = NULL;
14 // BEGIN - code running in my custom pthread instance
17 mandel(int *pix, int w, int h)
19 double x, xx, y, cx, cy, cox, coy;
20 int iteration, hx, hy, val, r, g, b, rr, gg, bb;
22 double magnify = 0.02;
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++)
34 for (hx = 0; hx < w; hx++)
36 cx = (((float)hx) / ((float)w) - 0.5) / (magnify * 3.0);
37 cy = (((float)hy) / ((float)h) - 0.5) / (magnify * 3.0);
42 for (iteration = 1; iteration < itermax; iteration++)
44 xx = (x * x) - (y * y) + cx;
45 y = (2.0 * x * y) + cy;
47 if (((x * x) + (y * y)) > 100.0) iteration = 999999;
49 val = (((x * x) + (y * y)) * 2.55) / 100.0;
50 if (val > 255) val = 255;
51 if (iteration >= 99999)
57 (val << 24) | (rr << 16) | (gg << 8) | (bb);
60 pix[(hy * w) + hx] = 0xffffffff;
66 th_do(void *data, Ecore_Thread *th)
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);
74 // END - code running in my custom pthread instance
76 static void // thread job finished - collect results and put in img obj
77 th_end(void *data, Ecore_Thread *th)
79 struct info *inf = data;
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);
88 static void // if the thread is cancelled - free pix, keep obj tho
89 th_cancel(void *data, Ecore_Thread *th)
91 struct info *inf = data;
93 // just free pixel data and info struct
98 static Eina_Bool // animate the objects so you see all the madels move
101 Evas_Object *o = data;
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
109 t = ecore_loop_time_get();
110 w = 100 + ((v * 100) >> 8);
111 h = 100 + ((v * 100) >> 8);
112 z = (double)(v) / 100.0;
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
122 elm_main(int argc, char **argv)
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);
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);
139 // queue up 64 mandel generation thread jobs
140 for (i = 0; i < 64; i++)
144 // create ecore thread to do some threaded job inside the worker pool
145 inf = malloc(sizeof(struct info));
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);
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);