aboutsummaryrefslogtreecommitdiff
path: root/src/render.c
diff options
context:
space:
mode:
authorcxp2249 <moritz.pirk@tuhh.de>2018-01-21 17:24:19 +0100
committercxp2249 <moritz.pirk@tuhh.de>2018-01-21 17:24:19 +0100
commitbce8d1b1e019e12402feff28ee2abeb1bbef63f4 (patch)
treedc05cf2231e4490ac734709cc6d32ca8e1706cb8 /src/render.c
parentdf57d3d0c8c7c33583722bded85deefcfdbca84d (diff)
added multithreading
Diffstat (limited to 'src/render.c')
-rw-r--r--src/render.c92
1 files changed, 73 insertions, 19 deletions
diff --git a/src/render.c b/src/render.c
index e06c967..528d775 100644
--- a/src/render.c
+++ b/src/render.c
@@ -6,6 +6,8 @@
*/
#include "render.h"
+#define HAVE_STRUCT_TIMESPEC
+#include <pthread.h>
void render_init(Config *config, u32 (*sfunc) (double, double, u32))
{
@@ -39,34 +41,86 @@ void on_draw(GtkWidget *widget, cairo_t *cr, gpointer data)
cairo_paint(cr);
}
+struct arg_thread {
+ cairo_surface_t *result;
+ unsigned short threadCount;
+ unsigned short threadNumber;
+ unsigned char *current_row;
+};
+
+void *imageCalcThread(void *arguments)
+{
+
+
+ struct arg_thread *args = arguments;
+ cairo_surface_t *result = args->result;
+ unsigned int threadCount = args->threadCount;
+ unsigned int threadNumber = args->threadNumber;
+ unsigned char *current_row = args->current_row;
+
+
+
+ int stride;
+ double x_math, y_math;
+ u32 iterations;
+
+ printf("thread calculating %s",result);
+ current_row = cairo_image_surface_get_data(result);
+ stride = cairo_image_surface_get_stride(result);
+
+
+ //Calculating start and endpoint for current thread to calculate
+ int startingPoint = (_config->height/threadCount)*(threadNumber);
+ int endPoint = ((_config->height/threadCount)*(threadNumber + 1)) - 1;
+
+
+ for (int y = startingPoint; y < endPoint && y < _config->height; y++) {
+ u32 *row = (void *) current_row;
+ for (int x = 0; x < _config->width; x++) {
+ x_math = /*x_MIN*/-2.0 + ((double) x * (/*x_MAX*/1.0 - /*x_MIN*/-2.0)) / _config->width;
+ y_math = /*y_MIN*/-1.0 + ((double) (_config->height - y) * (/*y_MAX*/1.0 - /*y_MIN*/-1.0)) / _config->height;
+ iterations = _sfunc(x_math, y_math, _config->iterations);
+ row[x] = (((1<<24)-1)*iterations)/_config->iterations;
+// if (iterations > 1<<8)
+// printf("schon gruen\n");
+ }
+
+ current_row += stride;
+ }
+
+}
+
cairo_surface_t *render_surface()
{
cairo_surface_t *result;
+ unsigned short threadCount = 4;
unsigned char *current_row;
- int stride;
- double x_math, y_math;
- u32 iterations;
- result = cairo_image_surface_create(CAIRO_FORMAT_RGB24, _config->width, _config->height);
- if (cairo_surface_status(result) != CAIRO_STATUS_SUCCESS)
- return result;
+ result = cairo_image_surface_create(CAIRO_FORMAT_RGB24, _config->width, _config->height);
+ if (cairo_surface_status(result) != CAIRO_STATUS_SUCCESS)
+ return result;
cairo_surface_flush(result);
- current_row = cairo_image_surface_get_data(result);
- stride = cairo_image_surface_get_stride(result);
- for (int y = 0; y < _config->height; y++) {
- u32 *row = (void *) current_row;
- for (int x = 0; x < _config->width; x++) {
- x_math = /*x_MIN*/-2.0 + ((double) x * (/*x_MAX*/1.0 - /*x_MIN*/-2.0)) / _config->width;
- y_math = /*y_MIN*/-1.0 + ((double) (_config->height - y) * (/*y_MAX*/1.0 - /*y_MIN*/-1.0)) / _config->height;
- iterations = _sfunc(x_math, y_math, _config->iterations);
- row[x] = (((1<<24)-1)*iterations)/_config->iterations;
-// if (iterations > 1<<8)
-// printf("schon gruen\n");
- }
- current_row += stride;
+
+
+ //initialize thread related values and put them into a struct, as just one parameter is passed
+ struct arg_thread args;
+ args.result = result;
+ args.threadCount = threadCount;
+ args.current_row = current_row;
+
+ pthread_t tid;
+
+ //Create threads up to the number specified in threadCount
+ for (int i = 0; i < threadCount; i++){
+ args.threadNumber = i;
+ pthread_create(&tid, NULL, imageCalcThread, (void *)&args);
}
+
+ //Wait for threads to finish and continue with the program
+ pthread_join(tid,NULL);
+
cairo_surface_mark_dirty(result);
return result;
}