PageRenderTime 51ms CodeModel.GetById 15ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/media_plugins/example/media_plugin_example.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 412 lines | 304 code | 54 blank | 54 comment | 84 complexity | d05dcf08c56402b0849340d22b13ab62 MD5 | raw file
  1/**
  2 * @file media_plugin_example.cpp
  3 * @brief Example plugin for LLMedia API plugin system
  4 *
  5 * @cond
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 *
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 *
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 *
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 * @endcond
 27 */
 28
 29#include "linden_common.h"
 30
 31#include "llgl.h"
 32#include "llplugininstance.h"
 33#include "llpluginmessage.h"
 34#include "llpluginmessageclasses.h"
 35#include "media_plugin_base.h"
 36
 37#include <time.h>
 38
 39////////////////////////////////////////////////////////////////////////////////
 40//
 41class MediaPluginExample :
 42        public MediaPluginBase
 43{
 44    public:
 45        MediaPluginExample( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data );
 46        ~MediaPluginExample();
 47
 48        /*virtual*/ void receiveMessage( const char* message_string );
 49
 50    private:
 51        bool init();
 52        void update( F64 milliseconds );
 53        void write_pixel( int x, int y, unsigned char r, unsigned char g, unsigned char b );
 54        bool mFirstTime;
 55
 56        time_t mLastUpdateTime;
 57        enum Constants { ENumObjects = 10 };
 58        unsigned char* mBackgroundPixels;
 59        int mColorR[ ENumObjects ];
 60        int mColorG[ ENumObjects ];
 61        int mColorB[ ENumObjects ];
 62        int mXpos[ ENumObjects ];
 63        int mYpos[ ENumObjects ];
 64        int mXInc[ ENumObjects ];
 65        int mYInc[ ENumObjects ];
 66        int mBlockSize[ ENumObjects ];
 67        bool mMouseButtonDown;
 68        bool mStopAction;
 69};
 70
 71////////////////////////////////////////////////////////////////////////////////
 72//
 73MediaPluginExample::MediaPluginExample( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data ) :
 74    MediaPluginBase( host_send_func, host_user_data )
 75{
 76    mFirstTime = true;
 77    mWidth = 0;
 78    mHeight = 0;
 79    mDepth = 4;
 80    mPixels = 0;
 81    mMouseButtonDown = false;
 82    mStopAction = false;
 83    mLastUpdateTime = 0;
 84}
 85
 86////////////////////////////////////////////////////////////////////////////////
 87//
 88MediaPluginExample::~MediaPluginExample()
 89{
 90}
 91
 92////////////////////////////////////////////////////////////////////////////////
 93//
 94void MediaPluginExample::receiveMessage( const char* message_string )
 95{
 96//  std::cerr << "MediaPluginWebKit::receiveMessage: received message: \"" << message_string << "\"" << std::endl;
 97    LLPluginMessage message_in;
 98
 99    if(message_in.parse(message_string) >= 0)
100    {
101        std::string message_class = message_in.getClass();
102        std::string message_name = message_in.getName();
103        if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
104        {
105            if(message_name == "init")
106            {
107                LLPluginMessage message("base", "init_response");
108                LLSD versions = LLSD::emptyMap();
109                versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
110                versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
111                versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
112                message.setValueLLSD("versions", versions);
113
114                std::string plugin_version = "Example plugin 1.0..0";
115                message.setValue("plugin_version", plugin_version);
116                sendMessage(message);
117            }
118            else if(message_name == "idle")
119            {
120                // no response is necessary here.
121                F64 time = message_in.getValueReal("time");
122
123                // Convert time to milliseconds for update()
124                update((int)(time * 1000.0f));
125            }
126            else if(message_name == "cleanup")
127            {
128            }
129            else if(message_name == "shm_added")
130            {
131                SharedSegmentInfo info;
132                info.mAddress = message_in.getValuePointer("address");
133                info.mSize = (size_t)message_in.getValueS32("size");
134                std::string name = message_in.getValue("name");
135
136                mSharedSegments.insert(SharedSegmentMap::value_type(name, info));
137
138            }
139            else if(message_name == "shm_remove")
140            {
141                std::string name = message_in.getValue("name");
142
143                SharedSegmentMap::iterator iter = mSharedSegments.find(name);
144                if(iter != mSharedSegments.end())
145                {
146                    if(mPixels == iter->second.mAddress)
147                    {
148                        // This is the currently active pixel buffer.  Make sure we stop drawing to it.
149                        mPixels = NULL;
150                        mTextureSegmentName.clear();
151                    }
152                    mSharedSegments.erase(iter);
153                }
154                else
155                {
156//                  std::cerr << "MediaPluginWebKit::receiveMessage: unknown shared memory region!" << std::endl;
157                }
158
159                // Send the response so it can be cleaned up.
160                LLPluginMessage message("base", "shm_remove_response");
161                message.setValue("name", name);
162                sendMessage(message);
163            }
164            else
165            {
166//              std::cerr << "MediaPluginWebKit::receiveMessage: unknown base message: " << message_name << std::endl;
167            }
168        }
169        else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
170        {
171            if(message_name == "init")
172            {
173                // Plugin gets to decide the texture parameters to use.
174                mDepth = 4;
175                LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
176                message.setValueS32("default_width", 1024);
177                message.setValueS32("default_height", 1024);
178                message.setValueS32("depth", mDepth);
179                message.setValueU32("internalformat", GL_RGBA);
180                message.setValueU32("format", GL_RGBA);
181                message.setValueU32("type", GL_UNSIGNED_BYTE);
182                message.setValueBoolean("coords_opengl", true);
183                sendMessage(message);
184            }
185            else if(message_name == "size_change")
186            {
187                std::string name = message_in.getValue("name");
188                S32 width = message_in.getValueS32("width");
189                S32 height = message_in.getValueS32("height");
190                S32 texture_width = message_in.getValueS32("texture_width");
191                S32 texture_height = message_in.getValueS32("texture_height");
192
193                if(!name.empty())
194                {
195                    // Find the shared memory region with this name
196                    SharedSegmentMap::iterator iter = mSharedSegments.find(name);
197                    if(iter != mSharedSegments.end())
198                    {
199                        mPixels = (unsigned char*)iter->second.mAddress;
200                        mWidth = width;
201                        mHeight = height;
202
203                        mTextureWidth = texture_width;
204                        mTextureHeight = texture_height;
205                    };
206                };
207
208                LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
209                message.setValue("name", name);
210                message.setValueS32("width", width);
211                message.setValueS32("height", height);
212                message.setValueS32("texture_width", texture_width);
213                message.setValueS32("texture_height", texture_height);
214                sendMessage(message);
215
216            }
217            else if(message_name == "load_uri")
218            {
219            }
220            else if(message_name == "mouse_event")
221            {
222                std::string event = message_in.getValue("event");
223                if(event == "down")
224                {
225
226                }
227                else if(event == "up")
228                {
229                }
230                else if(event == "double_click")
231                {
232                }
233            }
234        }
235        else
236        {
237//          std::cerr << "MediaPluginWebKit::receiveMessage: unknown message class: " << message_class << std::endl;
238        };
239    }
240}
241
242////////////////////////////////////////////////////////////////////////////////
243//
244void MediaPluginExample::write_pixel( int x, int y, unsigned char r, unsigned char g, unsigned char b )
245{
246    // make sure we don't write outside the buffer
247    if ( ( x < 0 ) || ( x >= mWidth ) || ( y < 0 ) || ( y >= mHeight ) )
248        return;
249
250    if ( mBackgroundPixels != NULL )
251    {
252        unsigned char *pixel = mBackgroundPixels;
253        pixel += y * mWidth * mDepth;
254        pixel += ( x * mDepth );
255        pixel[ 0 ] = b;
256        pixel[ 1 ] = g;
257        pixel[ 2 ] = r;
258
259        setDirty( x, y, x + 1, y + 1 );
260    };
261}
262
263////////////////////////////////////////////////////////////////////////////////
264//
265void MediaPluginExample::update( F64 milliseconds )
266{
267    if ( mWidth < 1 || mWidth > 2048 || mHeight < 1 || mHeight > 2048 )
268        return;
269
270    if ( mPixels == 0 )
271            return;
272
273    if ( mFirstTime )
274    {
275        for( int n = 0; n < ENumObjects; ++n )
276        {
277            mXpos[ n ] = ( mWidth / 2 ) + rand() % ( mWidth / 16 ) - ( mWidth / 32 );
278            mYpos[ n ] = ( mHeight / 2 ) + rand() % ( mHeight / 16 ) - ( mHeight / 32 );
279
280            mColorR[ n ] = rand() % 0x60 + 0x60;
281            mColorG[ n ] = rand() % 0x60 + 0x60;
282            mColorB[ n ] = rand() % 0x60 + 0x60;
283
284            mXInc[ n ] = 0;
285            while ( mXInc[ n ] == 0 )
286                mXInc[ n ] = rand() % 7 - 3;
287
288            mYInc[ n ] = 0;
289            while ( mYInc[ n ] == 0 )
290                mYInc[ n ] = rand() % 9 - 4;
291
292            mBlockSize[ n ] = rand() % 0x30 + 0x10;
293        };
294
295        delete [] mBackgroundPixels;
296
297        mBackgroundPixels = new unsigned char[ mWidth * mHeight * mDepth ];
298
299        mFirstTime = false;
300    };
301
302    if ( mStopAction )
303        return;
304
305    if ( time( NULL ) > mLastUpdateTime + 3 )
306    {
307        const int num_squares = rand() % 20 + 4;
308        int sqr1_r = rand() % 0x80 + 0x20;
309        int sqr1_g = rand() % 0x80 + 0x20;
310        int sqr1_b = rand() % 0x80 + 0x20;
311        int sqr2_r = rand() % 0x80 + 0x20;
312        int sqr2_g = rand() % 0x80 + 0x20;
313        int sqr2_b = rand() % 0x80 + 0x20;
314
315        for ( int y1 = 0; y1 < num_squares; ++y1 )
316        {
317            for ( int x1 = 0; x1 < num_squares; ++x1 )
318            {
319                int px_start = mWidth * x1 / num_squares;
320                int px_end = ( mWidth * ( x1 + 1 ) ) / num_squares;
321                int py_start = mHeight * y1 / num_squares;
322                int py_end = ( mHeight * ( y1 + 1 ) ) / num_squares;
323
324                for( int y2 = py_start; y2 < py_end; ++y2 )
325                {
326                    for( int x2 = px_start; x2 < px_end; ++x2 )
327                    {
328                        int rowspan = mWidth * mDepth;
329
330                        if ( ( y1 % 2 ) ^ ( x1 % 2 ) )
331                        {
332                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr1_r;
333                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr1_g;
334                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr1_b;
335                        }
336                        else
337                        {
338                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr2_r;
339                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr2_g;
340                            mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr2_b;
341                        };
342                    };
343                };
344            };
345        };
346
347        time( &mLastUpdateTime );
348    };
349
350    memcpy( mPixels, mBackgroundPixels, mWidth * mHeight * mDepth );
351
352    for( int n = 0; n < ENumObjects; ++n )
353    {
354        if ( rand() % 50 == 0 )
355        {
356                mXInc[ n ] = 0;
357                while ( mXInc[ n ] == 0 )
358                    mXInc[ n ] = rand() % 7 - 3;
359
360                mYInc[ n ] = 0;
361                while ( mYInc[ n ] == 0 )
362                    mYInc[ n ] = rand() % 9 - 4;
363        };
364
365        if ( mXpos[ n ] + mXInc[ n ] < 0 || mXpos[ n ] + mXInc[ n ] >= mWidth - mBlockSize[ n ] )
366            mXInc[ n ] =- mXInc[ n ];
367
368        if ( mYpos[ n ] + mYInc[ n ] < 0 || mYpos[ n ] + mYInc[ n ] >= mHeight - mBlockSize[ n ] )
369            mYInc[ n ] =- mYInc[ n ];
370
371        mXpos[ n ] += mXInc[ n ];
372        mYpos[ n ] += mYInc[ n ];
373
374        for( int y = 0; y < mBlockSize[ n ]; ++y )
375        {
376            for( int x = 0; x < mBlockSize[ n ]; ++x )
377            {
378                mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 0 ] = mColorR[ n ];
379                mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 1 ] = mColorG[ n ];
380                mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 2 ] = mColorB[ n ];
381            };
382        };
383    };
384
385    setDirty( 0, 0, mWidth, mHeight );
386};
387
388////////////////////////////////////////////////////////////////////////////////
389//
390bool MediaPluginExample::init()
391{
392    LLPluginMessage message( LLPLUGIN_MESSAGE_CLASS_MEDIA, "name_text" );
393    message.setValue( "name", "Example Plugin" );
394    sendMessage( message );
395
396    return true;
397};
398
399////////////////////////////////////////////////////////////////////////////////
400//
401int init_media_plugin( LLPluginInstance::sendMessageFunction host_send_func,
402                        void* host_user_data,
403                        LLPluginInstance::sendMessageFunction *plugin_send_func,
404                        void **plugin_user_data )
405{
406    MediaPluginExample* self = new MediaPluginExample( host_send_func, host_user_data );
407    *plugin_send_func = MediaPluginExample::staticReceiveMessage;
408    *plugin_user_data = ( void* )self;
409
410    return 0;
411}
412