aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/test_apps/llplugintest/demo_media_plugin_2.cpp
blob: 7a76f7405e5cc8cdd53a4e283aa407689044af36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/** 
 * @file demo_plugin.cpp
 * @brief Test plugin to be loaded by the llplugin testbed.
 *
 * $LicenseInfo:firstyear=2008&license=viewergpl$
 * 
 * Copyright (c) 2008-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llgl.h"

#include "llplugininstance.h"
#include "llpluginmessage.h"
#include "llpluginmessageclasses.h"

// TODO: Make sure that the only symbol exported from this library is LLPluginInitEntryPoint

class DemoMediaPlugin2
{
public:

	static int init(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data, LLPluginInstance::sendMessageFunction *plugin_send_func, void **plugin_user_data);

private:
	DemoMediaPlugin2(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data);
	~DemoMediaPlugin2();

	static void staticReceiveMessage(const char *message_string, void **user_data);
	void receiveMessage(const char *message_string);
	void sendMessage(const LLPluginMessage &message);
	void setDirty(int left, int top, int right, int bottom);
	
	LLPluginInstance::sendMessageFunction mHostSendFunction;
	void *mHostUserData;
	bool mDeleteMe;
	
	class SharedSegmentInfo
	{
	public:
		void *mAddress;
		size_t mSize;
	};
	
	typedef std::map<std::string, SharedSegmentInfo> SharedSegmentMap;
	
	SharedSegmentMap mSharedSegments;

public:
	void updatePixels(void)
	{
		if ( mWidth < 1 || mWidth > 2048 || mHeight < 1 || mHeight > 2048 )
			return;

		if ( mPixels == 0 )
				return;

		if ( mFirstTime )
		{
			// now we have a valid width/height - we can initialize positions
			for( int n = 0; n < NumObjects; ++n )
			{
				mXpos[ n ] = ( mWidth / 2 ) + rand() % ( mWidth / 16 ) - ( mWidth / 32 );
				mYpos[ n ] = ( mHeight / 2 ) + rand() % ( mHeight / 16 ) - ( mHeight / 32 );

				mColorR[ n ] = rand() % 0xa0 + 0x60;
				mColorG[ n ] = rand() % 0xa0 + 0x60;
				mColorB[ n ] = rand() % 0xa0 + 0x60;

				mXInc[ n ] = 0;
				while ( mXInc[ n ] == 0 )
					mXInc[ n ] = rand() % 7 - 3;

				mYInc[ n ] = 0;
				while ( mYInc[ n ] == 0 )
					mYInc[ n ] = rand() % 9 - 4;

				mBlockSize[ n ] = rand() % 0x16 + 0x04;
			};

			delete [] mBackgroundPixels;
				
			mBackgroundPixels = new unsigned char[ mWidth * mHeight * mDepth ];

			mFirstTime = false;
		};

		if ( time( NULL ) > mLastUpdateTime + 3 )
		{
			const int num_squares = rand() % 20 + 4;
			int sqr1_r = rand() % 0x80 + 0x20;
			int sqr1_g = rand() % 0x80 + 0x20;
			int sqr1_b = rand() % 0x80 + 0x20;
			int sqr2_r = rand() % 0x80 + 0x20;
			int sqr2_g = rand() % 0x80 + 0x20;
			int sqr2_b = rand() % 0x80 + 0x20;

			for ( int y1 = 0; y1 < num_squares; ++y1 )
			{
				for ( int x1 = 0; x1 < num_squares; ++x1 )
				{
					int px_start = mWidth * x1 / num_squares;
					int px_end = ( mWidth * ( x1 + 1 ) ) / num_squares;
					int py_start = mHeight * y1 / num_squares;
					int py_end = ( mHeight * ( y1 + 1 ) ) / num_squares;

					for( int y2 = py_start; y2 < py_end; ++y2 )
					{
						for( int x2 = px_start; x2 < px_end; ++x2 )
						{
							int rowspan = mWidth * mDepth;

							if ( ( y1 % 2 ) ^ ( x1 % 2 ) )
							{
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr1_r;
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr1_g;
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr1_b;
							}
							else
							{
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr2_r;
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr2_g;
								mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr2_b;
							};
						};
					};
				};
			};

			time( &mLastUpdateTime );
		};

		memcpy( mPixels, mBackgroundPixels, mWidth * mHeight * mDepth );

		for( int n = 0; n < NumObjects; ++n )
		{
			if ( rand() % 50 == 0 )
			{
					mXInc[ n ] = 0;
					while ( mXInc[ n ] == 0 )
						mXInc[ n ] = rand() % 7 - 3;

					mYInc[ n ] = 0;
					while ( mYInc[ n ] == 0 )
						mYInc[ n ] = rand() % 9 - 4;
			};

			if ( mXpos[ n ] + mXInc[ n ] < 0 || mXpos[ n ] + mXInc[ n ] >= mWidth - mBlockSize[ n ] )
				mXInc[ n ] =- mXInc[ n ];

			if ( mYpos[ n ] + mYInc[ n ] < 0 || mYpos[ n ] + mYInc[ n ] >= mHeight - mBlockSize[ n ] )
				mYInc[ n ] =- mYInc[ n ];

			mXpos[ n ] += mXInc[ n ];
			mYpos[ n ] += mYInc[ n ];

			for( int y = 0; y < mBlockSize[ n ]; ++y )
			{
				for( int x = 0; x < mBlockSize[ n ]; ++x )
				{
					mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 0 ] = mColorR[ n ];
					mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 1 ] = mColorG[ n ];
					mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 2 ] = mColorB[ n ];
				};
			};
		};
		
		// TODO: experiment with setDirty to just follow block position
		setDirty( 0, 0, mWidth, mHeight );
	};

	////////////////////////////////////////////////////////////////////////////////
	//
	void update()
	{
		updatePixels();
	};

	////////////////////////////////////////////////////////////////////////////////
	//
	void write_pixel( int x, int y, unsigned char r, unsigned char g, unsigned char b )
	{
		// make sure we don't write outside the buffer
		if((x < 0) || (x >= mWidth) || (y < 0) || (y >= mHeight))
			return;
			
		if( mBackgroundPixels != NULL)
		{
			unsigned char *pixel = mBackgroundPixels;
			pixel += y * mWidth * mDepth;	// row offset
			pixel += (x * mDepth);					// columm offset
			pixel[0] = b;
			pixel[1] = g;
			pixel[2] = r;

			setDirty(x, y, x+1, y+1);
		}
	}

	////////////////////////////////////////////////////////////////////////////////
	//
	void mouseDown( int x, int y )
	{
		write_pixel( x, y, 0xff, 0x00, 0x00 );
	};

	////////////////////////////////////////////////////////////////////////////////
	//
	void mouseUp( int x, int y )
	{
		write_pixel( x, y, 0xff, 0xff, 0x00 );
	};

	////////////////////////////////////////////////////////////////////////////////
	//
	void mouseMove( int x, int y )
	{
		write_pixel( x  , y  , 0xff, 0x00, 0xff );
	};

	////////////////////////////////////////////////////////////////////////////////
	//
	void keyPress( unsigned char key )
	{
	};

private:
	unsigned char* mPixels;
	int mWidth;
	int mHeight;
	int mTextureWidth;
	int mTextureHeight;
	int mDepth;
	time_t mLastUpdateTime;
	bool mFirstTime;
	unsigned char* mBackgroundPixels;
	enum Constants2 { NumObjects = 20 };
	int mColorR[ NumObjects ];
	int mColorG[ NumObjects ];
	int mColorB[ NumObjects ];
	int mXpos[ NumObjects ];
	int mYpos[ NumObjects ];
	int mXInc[ NumObjects ];
	int mYInc[ NumObjects ];
	int mBlockSize[ NumObjects ];
};

int DemoMediaPlugin2::init(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data, LLPluginInstance::sendMessageFunction *plugin_send_func, void **plugin_user_data)
{
	DemoMediaPlugin2 *self = new DemoMediaPlugin2(host_send_func, host_user_data);
	*plugin_send_func = staticReceiveMessage;
	*plugin_user_data = (void*)self;

	return 0;
}


DemoMediaPlugin2::DemoMediaPlugin2(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data)
{
	std::cerr << "DemoMediaPlugin2 constructor" << std::endl;

	mHostSendFunction = host_send_func;
	mHostUserData = host_user_data;
	mDeleteMe = false;

	mLastUpdateTime = 0;
	mFirstTime = true;
	mLastUpdateTime = 0;
	mWidth = 0;
	mHeight = 0;
	mDepth = 0;
	mPixels = 0;
	mBackgroundPixels = 0;

	srand( get_clock_count() ) ;
}

DemoMediaPlugin2::~DemoMediaPlugin2()
{
	std::cerr << "DemoMediaPlugin2 destructor" << std::endl;
}

void DemoMediaPlugin2::staticReceiveMessage(const char *message_string, void **user_data)
{
	DemoMediaPlugin2 *self = (DemoMediaPlugin2*)*user_data;
	
	if(self != NULL)
	{
		self->receiveMessage(message_string);
	
		// If the plugin has processed the delete message, delete it.
		if(self->mDeleteMe)
		{
			delete self;
			*user_data = NULL;
		}
	}
}

void DemoMediaPlugin2::receiveMessage(const char *message_string)
{
//	std::cerr << "DemoMediaPlugin2::receiveMessage: received message: \"" << message_string << "\"" << std::endl;
	LLPluginMessage message_in;
	
	if(message_in.parse(message_string) >= 0)
	{
		std::string message_class = message_in.getClass();
		std::string message_name = message_in.getName();
		if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
		{
			if(message_name == "init")
			{
				LLPluginMessage message("base", "init_response");
				LLSD versions = LLSD::emptyMap();
				versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
				// Normally a plugin would only specify one of these two subclasses, but this is a demo...
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
				versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME] = LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME_VERSION;
				message.setValueLLSD("versions", versions);
				sendMessage(message);
				
				// Plugin gets to decide the texture parameters to use.
				mDepth = 3;

				message.setMessage(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
				message.setValueS32("depth", mDepth);
				message.setValueU32("internalformat", GL_RGB);
				message.setValueU32("format", GL_RGB);
				message.setValueU32("type", GL_UNSIGNED_BYTE);
				message.setValueBoolean("coords_opengl", false);	// true == use OpenGL-style coordinates, false == (0,0) is upper left.
				sendMessage(message);
			}
			else if(message_name == "idle")
			{
				// no response is necessary here.
				update();
			}
			else if(message_name == "shutdown")
			{
				sendMessage(LLPluginMessage("base", "shutdown_response"));
				
				mDeleteMe = true;
			}
			else if(message_name == "shm_added")
			{
				SharedSegmentInfo info;
				info.mAddress =  (void*)message_in.getValueU32("address");
				info.mSize = (size_t)message_in.getValueS32("size");
				std::string name = message_in.getValue("name");
				
				
				std::cerr << "DemoMediaPlugin2::receiveMessage: shared memory added, name: " << name 
					<< ", size: " << info.mSize 
					<< ", address: " << info.mAddress 
					<< std::endl;

				mSharedSegments.insert(SharedSegmentMap::value_type(name, info));
			
			}
			else if(message_name == "shm_remove")
			{
				std::string name = message_in.getValue("name");
				
				std::cerr << "DemoMediaPlugin2::receiveMessage: shared memory remove, name = " << name << std::endl;

				SharedSegmentMap::iterator iter = mSharedSegments.find(name);
				if(iter != mSharedSegments.end())
				{
					if(mPixels == iter->second.mAddress)
					{
						// This is the currently active pixel buffer.  Make sure we stop drawing to it.
						mPixels = NULL;
					}
					mSharedSegments.erase(iter);
				}
				else
				{
					std::cerr << "DemoMediaPlugin2::receiveMessage: unknown shared memory region!" << std::endl;
				}

				// Send the response so it can be cleaned up.
				LLPluginMessage message("base", "shm_remove_response");
				message.setValue("name", name);
				sendMessage(message);
			}
			else
			{
				std::cerr << "DemoMediaPlugin2::receiveMessage: unknown base message: " << message_name << std::endl;
			}
		}
		else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
		{
			if(message_name == "size_change")
			{
				std::string name = message_in.getValue("name");
				S32 width = message_in.getValueS32("width");
				S32 height = message_in.getValueS32("height");
				S32 texture_width = message_in.getValueS32("texture_width");
				S32 texture_height = message_in.getValueS32("texture_height");
				
				LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
				message.setValue("name", name);
				message.setValueS32("width", width);
				message.setValueS32("height", height);
				message.setValueS32("texture_width", texture_width);
				message.setValueS32("texture_height", texture_height);
				sendMessage(message);

				if(!name.empty())
				{
					// Find the shared memory region with this name
					SharedSegmentMap::iterator iter = mSharedSegments.find(name);
					if(iter != mSharedSegments.end())
					{
						std::cerr << "Got size change, new size is " << width << " by " << height << std::endl;
						std::cerr << "    texture size is " << texture_width << " by " << texture_height << std::endl;
						
						mPixels = (unsigned char*)iter->second.mAddress;
						mWidth = width;
						mHeight = height;

						mTextureWidth = texture_width;
						mTextureHeight = texture_height;
						
						mFirstTime = true;
						updatePixels();
					}
				}
			}
			else if(message_name == "mouse_event")
			{
				std::string event = message_in.getValue("event");
				S32 x = message_in.getValueS32("x");
				S32 y = message_in.getValueS32("y");
// 				std::string modifiers = message.getValue("modifiers");

//				std::cerr << "DemoMediaPlugin2::receiveMessage: mouse event \"" << event 
//					<< "\", coords " << x << ", " << y
//					<< std::endl;
				
				if(event == "down")
				{
					mouseDown(x, y);
				}
				else if(event == "up")
				{
					mouseUp(x, y);
				}
				else if(event == "move")
				{
					mouseMove(x, y);
				}
			}
			else
			{
				std::cerr << "DemoMediaPlugin2::receiveMessage: unknown media message: " << message_string << std::endl;
			}
		}
		else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER)
		{
			if(message_name == "focus")
			{
				// foo = message_in.getValueBoolean("focused");
			}
			else if(message_name == "clear_cache")
			{
			}
			else if(message_name == "clear_cookies")
			{
			}
			else if(message_name == "enable_cookies")
			{
				// foo = message_in.getValueBoolean("enable");
			}
			else if(message_name == "proxy_setup")
			{
				// foo = message_in.getValueBoolean("enable");
				// bar = message_in.getValue("host");
				// baz = message_in.getValueS32("port");
			}
			else if(message_name == "browse_stop")
			{
			}
			else if(message_name == "browse_reload")
			{
				// foo = message_in.getValueBoolean("ignore_cache");
			}
			else if(message_name == "browse_forward")
			{
			}
			else if(message_name == "browse_back")
			{
			}
			else if(message_name == "set_status_redirect")
			{
				// foo = message_in.getValueS32("code");
				// bar = message_in.getValue("url");
			}
			else
			{
				std::cerr << "DemoMediaPlugin2::receiveMessage: unknown media_browser message: " << message_string << std::endl;
			}
		}
		else
		{
			std::cerr << "DemoMediaPlugin2::receiveMessage: unknown message class: " << message_class << std::endl;
		}

	}
}

void DemoMediaPlugin2::sendMessage(const LLPluginMessage &message)
{
	std::string output = message.generate();
	mHostSendFunction(output.c_str(), &mHostUserData);
}

void DemoMediaPlugin2::setDirty(int left, int top, int right, int bottom)
{
	LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "updated");
	
	message.setValueS32("left", left);
	message.setValueS32("top", top);
	message.setValueS32("right", right);
	message.setValueS32("bottom", bottom);
	
	sendMessage(message);
}


extern "C"
{
#ifdef WIN32
	__declspec(dllexport)
#endif
		int LLPluginInitEntryPoint(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data, LLPluginInstance::sendMessageFunction *plugin_send_func, void **plugin_user_data);
}

int 
#ifdef WIN32
	__declspec(dllexport)
#endif
	LLPluginInitEntryPoint(LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data, LLPluginInstance::sendMessageFunction *plugin_send_func, void **plugin_user_data)
{
	return DemoMediaPlugin2::init(host_send_func, host_user_data, plugin_send_func, plugin_user_data);
}

#ifdef WIN32
int WINAPI DllEntryPoint( HINSTANCE hInstance, unsigned long reason, void* params )
{
	return 1;
}
#endif