-2

I'm building a browser plugin which will draw pictures as a slideshow inside browser windows, however the plugin I created only draws on first plugin instance. If I open multiple instances of the plugin, it keeps on drawing on the first plugin window overlapping each picture.

I'm using opengl to draw picture from the url.

Following is a code which draws dummy opengl tringles in a loop using a thread:

FB::PluginWindowWin *pluginWindowWin = dynamic_cast(pluginWindow);

EnableOpenGL(pluginWindowWin->getHWND(), &hDC, &hRC);
SetFocus(pluginWindowWin->getHWND());
//FB::
static int fps = 1;
GLfloat rotate = 0;
static double start = 0, diff, wait;
wait = 1 / fps;

//return 0;
while (true)
{

    //lets check for keyboard input
    try
    {
        FB::Rect pos = pluginWindow->getWindowPosition();

        PAINTSTRUCT ps;
        if (pluginWindowWin){
            hDC = BeginPaint(pluginWindowWin->getHWND(), &ps);

            pos.right -= pos.left;
            pos.left = 0;
            pos.bottom -= pos.top;
            pos.top = 0;

            rotate += 0.1f;

            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();
            glRotatef(rotate, 0.0f, 1.0f, 0.0f);
            glBegin(GL_TRIANGLES);

            glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
            glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f);
            glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f);
            glEnd();

            glBegin(GL_QUADS);                                              // Draw A Quad
            glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.0f);                           // Top Left
            glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.0f);                            // Top Right
            glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.5f, -0.5f, 0.0f);                           // Bottom Right
            glColor3f(0.0f, 0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.0f);                           // Bottom Left
            glEnd();                                                        // Done Drawing The Quad
            glPopMatrix();
            glRotatef(rotate, 0.0f, 1.0f, 0.0f);


            SwapBuffers(hDC);
        }
        //rtri+=0.1f;
        ::SetTextAlign(hDC, TA_CENTER | TA_BASELINE);
        LPCTSTR pszText = _T("FireBreath Plugin!\n:-)");

        ::TextOut(hDC, pos.left + (pos.right - pos.left) / 2, pos.top + (pos.bottom - pos.top) / 2, pszText, lstrlen(pszText));

        if (pluginWindowWin) {
            // Release the device context
            EndPaint(pluginWindowWin->getHWND(), &ps);
        }
    }
    catch (...)
    {
        return 0;
    }



    Sleep(10);


}//end of while run

Any thing I'm doing wrong here?

taxilian
  • 14,229
  • 4
  • 34
  • 73
Dipen Shah
  • 25,562
  • 1
  • 32
  • 58
  • What browser(s) have you seen this occur on? Are you in windowed or windowless mode? (I assume windowed, but if not that's a big issue). Are you starting a new instance of the thread for each instance of the plugin? Does each thread have its own data, including its own instance of the plugin hwnd? Do you realize that the SetFocus call, if it does anything at all, is only called once at the beginning and never again for each window, meaning only the last one will still have focus? – taxilian Jul 18 '16 at 04:14
  • This occurs on both firefox and IE, and I'm using windowed mode only. Also each plugin instance has its own thread and data. About SetFocus I just used that code from opengl example plugin to replicate issue I was facing with my original implementation, removing SetFocus did not make any difference in the output. – Dipen Shah Jul 18 '16 at 13:14
  • Could you add in the definitions of hDC and hRC? I assume those are not shared? – taxilian Jul 18 '16 at 22:04
  • Yes, they are not shared. The code is same as in the threadedOpenGLTestPlugin demo plugin example, and the demo plugin itself is crashing when running multiple instances of it. – Dipen Shah Jul 18 '16 at 22:52
  • I see two possibilities: 1) there is something about openGL or your opengl code that doesn't work with two different windows in the same process or 2) You have something that is more shared than you realize. Without seeing more of your code I can't guess which. – taxilian Jul 19 '16 at 00:15
  • You can find code @ https://github.com/dipen08it419/Plugin, however, it is technically doing same thing as threadedOpenGLTestPlugin plugin, and both of them have same behavior. – Dipen Shah Jul 19 '16 at 03:11
  • it is not doing at all the same thing as threadedOpenGLTestPlugin -- though what you're doing looks to me to be more likely to work than the other. the test plugin linked from the firebreath site (which btw was a user submission, not from the FB team) has hWnd and hDC both as globals. That's not going to work. is SDL designed so you can use multiple instances in the same process? – taxilian Jul 19 '16 at 04:02
  • Actually, SDL doesn't support multiple window but SDL2 does it and I'm planning to update it once I tackle the issue with multiple instances of plugin, which is why in the code I posted in question I have used OpenGL which support multiple windows. – Dipen Shah Jul 19 '16 at 04:25
  • Also, the current plugin crashes in Firefox during window resize and tab switch any guidance for that? – Dipen Shah Jul 19 '16 at 04:43

1 Answers1

1

From what you've told me in the comments, your primary issue is that you're starting with a flawed example. Remember that every instance of the plugin starts up in the same process; the example you're using is a simplified one which does not use good practices for plugins. Most specifically, it uses several global variables.

In addition to that you are threading but don't seem to be doing any locks to make sure that you are totally threadsafe. You live in someone else's process, you don't own it -- the browser does. You need to be very careful with a lot of things.

Most likely your crash has to do with not shutting down cleanly or perhaps with a race condition in your threading code. The best way to troubleshoot that is to attach a debugger and find out where it's crashing, rather than just running around in circles asking "Why?? why???!?" (exagerating for effect, obviously). You'd be shocked how few people do that simple step -- attaching a debugger -- until I tell them to, but it should always be your first step in troubleshooting a crash.

Finally, it bears asking: Do you realize that you are building this on a technology which won't be available in 6 months? FireFox is removing support for NPAPI at the end of the year. I expect ActiveX to work a bit longer than that, but edge doesn't support it.

FireBreath 2 (in the 2.0 branch) is a major change from firebreath 1 but it supports Chrome via native messaging and will support FireFox as well. there are many trying to convince MS to add native messaging support to Edge, but we'll see how that goes. Feel free to follow that link and vote, since it would help you as well I suspect.

Thing is, you don't get SDL or SDL2 w/ native messaging; you'd have to use WebGL and do the dev on the javascript side, then pull data over native messaging. alternately you could look into using NaCL which does have some opengl / drawing stuff (maybe even SDL? not sure) but is sandboxed and may or may not have the networking things you need. Also, of course, it only works on Chrome.

Food for thought. Good luck.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Thank you for your comments, I'll try to find out where its getting things wrong. For debugging, though, I used debugger to solve few issues I had previously but crashing of plugin in Firefox just closes debugger and doesn't give any useful information in the crash report. – Dipen Shah Jul 19 '16 at 05:22
  • If that's the case then you don't have your debugger connected correctly -- just FYI – taxilian Jul 19 '16 at 19:26