1 #include <Elementary.h>
3 static Evas_Object *win = NULL;
4 static Evas_Object *rect = NULL;
12 // BEGIN - code running in my custom pthread instance
15 mandel(int *pix, int w, int h)
17 double x, xx, y, cx, cy, cox, coy;
18 int iteration, hx, hy, val, r, g, b, rr, gg, bb;
20 double magnify = 0.02;
22 // this mandel calc is run in the worker threads so it's here. it is
23 // just here to calculate something and consume cpu to demonstrate the
24 // ecore thread worker queue. don't pay much attention to the below code
25 magnify += ((double)(rand() % 100) / 100.0) / 4.0;
26 cox = (double)(rand() % 100) / 100.0;
27 coy = (double)(rand() % 100) / 100.0;
28 cox /= (magnify * 3.0);
29 r = rand() % 255; g = rand() % 255; b = rand() % 255;
30 for (hy = 0; hy < h; hy++)
32 for (hx = 0; hx < w; hx++)
34 cx = (((float)hx) / ((float)w) - 0.5) / (magnify * 3.0);
35 cy = (((float)hy) / ((float)h) - 0.5) / (magnify * 3.0);
40 for (iteration = 1; iteration < itermax; iteration++)
42 xx = (x * x) - (y * y) + cx;
43 y = (2.0 * x * y) + cy;
45 if (((x * x) + (y * y)) > 100.0) iteration = 999999;
47 val = (((x * x) + (y * y)) * 2.55) / 100.0;
48 if (val > 255) val = 255;
49 if (iteration >= 99999)
55 (val << 24) | (rr << 16) | (gg << 8) | (bb);
58 pix[(hy * w) + hx] = 0xffffffff;
64 th_do(void *data, Ecore_Thread *th)
66 struct info *inf = data;
67 // CANNOT TOUCH inf->obj here! just inf->pix which is 256x256 @ 32bpp
68 // quick and dirty to consume some cpu - do a mandelbrot calc
69 mandel(inf->pix, 256, 256);
72 // END - code running in my custom pthread instance
74 static void // thread job finished - collect results and put in img obj
75 th_end(void *data, Ecore_Thread *th)
77 struct info *inf = data;
79 // copy data to object, free calculated data and info struc
80 evas_object_image_data_copy_set(inf->obj, inf->pix);
81 evas_object_show(inf->obj);
86 static void // if the thread is cancelled - free pix, keep obj tho
87 th_cancel(void *data, Ecore_Thread *th)
89 struct info *inf = data;
91 // just free pixel data and info struct
96 static Eina_Bool // animate the objects so you see all the madels move
99 Evas_Object *o = data;
104 // just calculate some position using the pointer value of the object as
105 // a seed value to make different objects go into different places over time
107 t = ecore_loop_time_get();
108 w = 100 + ((v * 100) >> 8);
109 h = 100 + ((v * 100) >> 8);
110 z = (double)(v) / 100.0;
112 y = (h * cos(t + z));
113 // do the actual move
114 evas_object_move(o, 200 + x - 128, 200 + y - 128);
115 // keep looping - return true
120 elm_main(int argc, char **argv)
125 win = elm_win_add(NULL, "efl-thread-6", ELM_WIN_BASIC);
126 elm_win_title_set(win, "EFL Thread 6");
127 evas_object_resize(win, 400, 400);
128 evas_object_show(win);
130 bg = elm_bg_add(win);
131 elm_win_resize_object_add(win, bg);
132 evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
133 evas_object_show(bg);
135 // queue up 64 mandel generation thread jobs
136 for (i = 0; i < 64; i++)
140 // create ecore thread to do some threaded job inside the worker pool
141 inf = malloc(sizeof(struct info));
146 o = evas_object_image_filled_add(evas_object_evas_get(win));
147 evas_object_image_size_set(o, 256, 256);
148 evas_object_image_alpha_set(o, EINA_TRUE);
149 evas_object_resize(o, 256, 256);
151 inf->pix = malloc(256 * 256 * sizeof(int));
152 ecore_thread_run(th_do, th_end, th_cancel, inf);
153 // bonus - slide the objects around all the time with an
154 // animator that ticks off every frame.
155 ecore_animator_add(anim, o);