xref: /haiku/src/apps/mandelbrot/FractalEngine.h (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2016, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Augustin Cavalier <waddlesplash>
7  *		kerwizzy
8  */
9 #ifndef FRACTALENGINE_H
10 #define FRACTALENGINE_H
11 
12 #include <SupportDefs.h>
13 #include <Looper.h>
14 #include <Messenger.h>
15 
16 class BBitmap;
17 
18 
19 #define MAX_RENDER_THREADS 16
20 
21 
22 class FractalEngine : public BLooper {
23 public:
24 	enum {
25 		MSG_CHANGE_SET = 'Frct',
26 		MSG_SET_PALETTE,
27 		MSG_SET_ITERATIONS,
28 		MSG_SET_SUBSAMPLING,
29 		MSG_RESIZE,
30 		MSG_BUFFER_CREATED,
31 		MSG_RENDER,
32 		MSG_RENDER_COMPLETE,
33 		MSG_THREAD_RENDER_COMPLETE,
34 	};
35 
36 	FractalEngine(BHandler* parent, BLooper* looper);
37 	~FractalEngine();
38 
39 	virtual void MessageReceived(BMessage* msg);
40 	void WriteToBitmap(BBitmap*);
41 
42 private:
43 	uint16 fWidth;
44 	uint16 fHeight;
45 
46 	uint8* fRenderBuffer;
47 	uint32 fRenderBufferLen;
48 
49 	uint8 fSubsampling;
50 		// 1 disables subsampling.
51 
52 	BMessenger fMessenger;
53 
54 	uint8 fThreadCount;
55 	thread_id fRenderThreads[MAX_RENDER_THREADS];
56 	sem_id fRenderSem;
57 		// released to tell threads to start running
58 	sem_id fRenderStoppedSem;
59 		// released by threads when done rendering or otherwise stopped
60 
61 	bool fRenderStopping;
62 		// true when the render is trying to be stopped
63 	bool fRenderStopped;
64 	bool fResizing;
65 
66 	double fLocationX, fLocationY;
67 	double fSize;
68 		// the width on the complex plane of a single pixel
69 
70 	uint16 fIterations;
71 
72 	const uint8* fColorset;
73 
74 	int32 (FractalEngine::*fDoSet)(double real, double imaginary);
75 
76 	void Render(double locationX, double locationY, double size);
77 	void StopRender();
78 	static status_t RenderThread(void* data);
79 	void RenderPixel(uint32 x, uint32 y);
80 
81 	int32 DoSet_Mandelbrot(double real, double imaginary);
82 	int32 DoSet_BurningShip(double real, double imaginary);
83 	int32 DoSet_Tricorn(double real, double imaginary);
84 	int32 DoSet_Julia(double real, double imaginary);
85 	int32 DoSet_OrbitTrap(double real, double imaginary);
86 	int32 DoSet_Multibrot(double real, double imaginary);
87 };
88 
89 
90 #endif	/* FRACTALENGINE_H */
91