2012-03-19 05:58:07 -07:00
|
|
|
//Compile with:
|
|
|
|
//gcc -o efl_thread_6 efl_thread_6.c -g `pkg-config --cflags --libs elementary`
|
2011-09-28 04:53:41 -07:00
|
|
|
#include <Elementary.h>
|
|
|
|
|
|
|
|
static Evas_Object *win = NULL;
|
|
|
|
|
|
|
|
struct info
|
|
|
|
{
|
|
|
|
Evas_Object *obj;
|
|
|
|
int *pix;
|
|
|
|
};
|
|
|
|
|
2012-03-23 00:23:26 -07:00
|
|
|
// BEGIN - code running in my custom thread instance
|
2011-09-28 04:53:41 -07:00
|
|
|
//
|
|
|
|
static void
|
|
|
|
mandel(int *pix, int w, int h)
|
|
|
|
{
|
|
|
|
double x, xx, y, cx, cy, cox, coy;
|
|
|
|
int iteration, hx, hy, val, r, g, b, rr, gg, bb;
|
|
|
|
int itermax = 10000;
|
|
|
|
double magnify = 0.02;
|
|
|
|
|
|
|
|
// this mandel calc is run in the worker threads so it's here. it is
|
|
|
|
// just here to calculate something and consume cpu to demonstrate the
|
|
|
|
// ecore thread worker queue. don't pay much attention to the below code
|
|
|
|
magnify += ((double)(rand() % 100) / 100.0) / 4.0;
|
|
|
|
cox = (double)(rand() % 100) / 100.0;
|
|
|
|
coy = (double)(rand() % 100) / 100.0;
|
|
|
|
cox /= (magnify * 3.0);
|
|
|
|
r = rand() % 255; g = rand() % 255; b = rand() % 255;
|
2011-10-05 04:34:11 -07:00
|
|
|
for (hy = 0; hy < h; hy++)
|
2011-09-28 04:53:41 -07:00
|
|
|
{
|
2011-10-05 04:34:11 -07:00
|
|
|
for (hx = 0; hx < w; hx++)
|
2011-09-28 04:53:41 -07:00
|
|
|
{
|
|
|
|
cx = (((float)hx) / ((float)w) - 0.5) / (magnify * 3.0);
|
|
|
|
cy = (((float)hy) / ((float)h) - 0.5) / (magnify * 3.0);
|
|
|
|
cx += cox;
|
|
|
|
cy += coy;
|
|
|
|
x = 0.0;
|
|
|
|
y = 0.0;
|
|
|
|
for (iteration = 1; iteration < itermax; iteration++)
|
|
|
|
{
|
|
|
|
xx = (x * x) - (y * y) + cx;
|
|
|
|
y = (2.0 * x * y) + cy;
|
|
|
|
x = xx;
|
|
|
|
if (((x * x) + (y * y)) > 100.0) iteration = 999999;
|
|
|
|
}
|
|
|
|
val = (((x * x) + (y * y)) * 2.55) / 100.0;
|
|
|
|
if (val > 255) val = 255;
|
2012-03-23 00:23:26 -07:00
|
|
|
if (iteration >= 99999)
|
2011-09-28 04:53:41 -07:00
|
|
|
{
|
|
|
|
rr = (r * val) / 255;
|
|
|
|
gg = (g * val) / 255;
|
|
|
|
bb = (b * val) / 255;
|
2012-03-23 00:23:26 -07:00
|
|
|
pix[(hy * w) + hx] =
|
2011-09-28 04:53:41 -07:00
|
|
|
(val << 24) | (rr << 16) | (gg << 8) | (bb);
|
|
|
|
}
|
|
|
|
else
|
2013-04-04 00:45:09 -07:00
|
|
|
pix[(hy * w) + hx] = 0xffffffff;
|
2011-09-28 04:53:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-28 01:42:20 -07:00
|
|
|
th_do(void *data, Ecore_Thread *th)
|
2011-09-28 04:53:41 -07:00
|
|
|
{
|
|
|
|
struct info *inf = data;
|
|
|
|
// CANNOT TOUCH inf->obj here! just inf->pix which is 256x256 @ 32bpp
|
|
|
|
// quick and dirty to consume some cpu - do a mandelbrot calc
|
|
|
|
mandel(inf->pix, 256, 256);
|
|
|
|
}
|
|
|
|
//
|
2012-03-23 00:23:26 -07:00
|
|
|
// END - code running in my custom thread instance
|
2011-09-28 04:53:41 -07:00
|
|
|
|
|
|
|
static void // thread job finished - collect results and put in img obj
|
2012-03-28 01:42:20 -07:00
|
|
|
th_end(void *data, Ecore_Thread *th)
|
2011-10-05 04:34:11 -07:00
|
|
|
{
|
2011-09-28 04:53:41 -07:00
|
|
|
struct info *inf = data;
|
|
|
|
|
|
|
|
// copy data to object, free calculated data and info struc
|
|
|
|
evas_object_image_data_copy_set(inf->obj, inf->pix);
|
|
|
|
evas_object_show(inf->obj);
|
|
|
|
free(inf->pix);
|
|
|
|
free(inf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void // if the thread is cancelled - free pix, keep obj tho
|
2012-03-28 01:42:20 -07:00
|
|
|
th_cancel(void *data, Ecore_Thread *th)
|
2011-10-05 04:34:11 -07:00
|
|
|
{
|
2011-09-28 04:53:41 -07:00
|
|
|
struct info *inf = data;
|
2011-10-05 04:34:11 -07:00
|
|
|
|
2011-09-28 04:53:41 -07:00
|
|
|
// just free pixel data and info struct
|
|
|
|
free(inf->pix);
|
|
|
|
free(inf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool // animate the objects so you see all the madels move
|
|
|
|
anim(void *data)
|
|
|
|
{
|
|
|
|
Evas_Object *o = data;
|
|
|
|
double t, z;
|
|
|
|
int w, h, v;
|
|
|
|
Evas_Coord x, y;
|
|
|
|
|
|
|
|
// just calculate some position using the pointer value of the object as
|
|
|
|
// a seed value to make different objects go into different places over time
|
|
|
|
v = ((int)o) & 0xff;
|
|
|
|
t = ecore_loop_time_get();
|
|
|
|
w = 100 + ((v * 100) >> 8);
|
|
|
|
h = 100 + ((v * 100) >> 8);
|
|
|
|
z = (double)(v) / 100.0;
|
|
|
|
x = (w * sin(t));
|
|
|
|
y = (h * cos(t + z));
|
|
|
|
// do the actual move
|
|
|
|
evas_object_move(o, 200 + x - 128, 200 + y - 128);
|
|
|
|
// keep looping - return true
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-27 23:04:36 -07:00
|
|
|
EAPI_MAIN int
|
2012-03-28 01:42:20 -07:00
|
|
|
elm_main(int argc, char **argv)
|
2011-09-28 04:53:41 -07:00
|
|
|
{
|
2013-06-25 23:34:38 -07:00
|
|
|
Evas_Object *o;
|
2011-09-28 04:53:41 -07:00
|
|
|
int i;
|
2011-10-05 04:34:11 -07:00
|
|
|
|
2012-03-19 05:58:07 -07:00
|
|
|
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
|
2013-06-25 23:34:38 -07:00
|
|
|
|
|
|
|
win = elm_win_util_standard_add("efl-thread-6", "EFL Thread 6");
|
|
|
|
elm_win_autodel_set(win, EINA_TRUE);
|
2011-10-05 04:34:11 -07:00
|
|
|
|
2011-09-28 04:53:41 -07:00
|
|
|
// queue up 64 mandel generation thread jobs
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
struct info *inf;
|
2011-10-05 04:34:11 -07:00
|
|
|
|
2011-09-28 04:53:41 -07:00
|
|
|
// create ecore thread to do some threaded job inside the worker pool
|
|
|
|
inf = malloc(sizeof(struct info));
|
|
|
|
if (inf)
|
|
|
|
{
|
|
|
|
o = evas_object_image_filled_add(evas_object_evas_get(win));
|
|
|
|
evas_object_image_size_set(o, 256, 256);
|
|
|
|
evas_object_image_alpha_set(o, EINA_TRUE);
|
|
|
|
evas_object_resize(o, 256, 256);
|
|
|
|
inf->obj = o;
|
|
|
|
inf->pix = malloc(256 * 256 * sizeof(int));
|
|
|
|
ecore_thread_run(th_do, th_end, th_cancel, inf);
|
|
|
|
// bonus - slide the objects around all the time with an
|
|
|
|
// animator that ticks off every frame.
|
|
|
|
ecore_animator_add(anim, o);
|
|
|
|
}
|
|
|
|
}
|
2011-10-05 04:34:11 -07:00
|
|
|
|
2013-12-14 12:57:30 -08:00
|
|
|
evas_object_resize(win, 400, 400);
|
|
|
|
evas_object_show(win);
|
|
|
|
|
2011-09-28 04:53:41 -07:00
|
|
|
elm_run();
|
2012-03-27 23:04:36 -07:00
|
|
|
elm_shutdown();
|
|
|
|
|
2011-09-28 04:53:41 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ELM_MAIN()
|