Mobile native

[UI Sample] Ecore Thread 5 Sample Overview

The Ecore Thread 5 sample demonstrates how to use the multi-thread functionality by EAPI.

The following figure illustrates the main screen of the Ecore Thread 5 sample.

Figure: Ecore Thread 5 screen

Ecore Thread 5 screen

Implementation

typedef struct user_data 
{
   Evas_Object *obj;
   int *pixel;
} user_data;

static void
thread_run_cb(void *data, Ecore_Thread *thread)
{
   user_data *ud = data;

   // Mandel calc is run in the worker threads so it is here
   // to calculate something and consume cpu to demonstrate the
   // Ecore thread worker queue. Do not pay much attention to the below code
   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;
   int w, h;

   evas_object_image_size_get(ud->obj, &w, &h);

   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;
   for (hy = 0; hy < h; hy++) 
   {
      for (hx = 0; hx < w; hx++) 
      {
         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;
         if (iteration >= 99999) 
         {
            rr = (r * val) / 255;
            gg = (g * val) / 255;
            bb = (b * val) / 255;   
            ud->pixel[(hy * w) + hx] = (val  << 24) | (rr << 16) | (gg << 8) | (bb);
         } 
         else
            ud->pixel[(hy * w) + hx] = 0xffffffff;
      }
   }
}

static void
thread_end_cb(void *data, Ecore_Thread *thread)
{
   user_data *ud = data;
   Evas_Coord w, h;

   // Update image data; since thread_end_cb() is called from the main loop thread,
   // you can access EAPI without considering synchronization
   evas_object_image_size_get(ud->obj, &w, &h);
   evas_object_image_data_copy_set(ud->obj, ud->pixel);
   evas_object_image_data_update_add(ud->obj, 0, 0, w, h);

   free(ud->pixel);
   free(ud);
}

static void
thread_cancel_cb(void *data, Ecore_Thread *thread)
{
   user_data *ud = data;

   free(ud->pixel);
   free(ud);
}

static void
mouse_down_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
   user_data *ud = calloc(1, sizeof(user_data));
   ud->obj = obj;
   ud->pixel = malloc(256 * 256 * sizeof(int));

   // Run a thread
   ecore_thread_run(thread_run_cb, thread_end_cb, thread_cancel_cb, ud);
}

void
ecore_thread_run_exam(Evas_Object *win)
{
   Evas_Object *img;

   // Create an image
   img = evas_object_image_filled_add(evas_object_evas_get(win));
   evas_object_event_callback_add(img, EVAS_CALLBACK_MOUSE_DOWN, mouse_down_cb, NULL);
   evas_object_image_size_set(img, 256, 256);
   evas_object_size_hint_weight_set(img, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_show(img);

   elm_win_resize_object_add(win, img);
}
Go to top