mirror of
				https://bitbucket.org/chromiumembedded/cef
				synced 2025-06-05 21:39:12 +02:00 
			
		
		
		
	Make off-screen rendering example work with multi-threaded message loop mode (issue #804).
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@938 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
		| @@ -18,14 +18,13 @@ CefRefPtr<OSRWindow> OSRWindow::Create(OSRBrowserProvider* browser_provider, | ||||
|  | ||||
| // static | ||||
| CefRefPtr<OSRWindow> OSRWindow::From( | ||||
|     CefRefPtr<CefRenderHandler> renderHandler) { | ||||
|     CefRefPtr<ClientHandler::RenderHandler> renderHandler) { | ||||
|   return static_cast<OSRWindow*>(renderHandler.get()); | ||||
| } | ||||
|  | ||||
| bool OSRWindow::CreateWidget(HWND hWndParent, const RECT& rect, | ||||
|                              HINSTANCE hInst, LPCTSTR className) { | ||||
|   ASSERT(hWnd_ == NULL && hDC_ == NULL && hRC_ == NULL); | ||||
|   Reset(); | ||||
|  | ||||
|   RegisterOSRClass(hInst, className); | ||||
|   hWnd_ = ::CreateWindow(className, 0, | ||||
| @@ -37,19 +36,20 @@ bool OSRWindow::CreateWidget(HWND hWndParent, const RECT& rect, | ||||
|     return false; | ||||
|  | ||||
|   SetWindowLongPtr(hWnd_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | ||||
|  | ||||
|   // Reference released in OnDestroyed(). | ||||
|   AddRef(); | ||||
|  | ||||
|   // Enable GL drawing for the window. | ||||
|   EnableGL(); | ||||
|   return true; | ||||
| } | ||||
|  | ||||
| void OSRWindow::DestroyWidget() { | ||||
|   if (::IsWindow(hWnd_)) { | ||||
|     DisableGL(); | ||||
|   if (IsWindow(hWnd_)) | ||||
|     DestroyWindow(hWnd_); | ||||
|   } | ||||
|   Reset(); | ||||
| } | ||||
|  | ||||
| void OSRWindow::OnBeforeClose(CefRefPtr<CefBrowser> browser) { | ||||
|   DisableGL(); | ||||
| } | ||||
|  | ||||
| bool OSRWindow::GetRootScreenRect(CefRefPtr<CefBrowser> browser, | ||||
| @@ -108,6 +108,9 @@ void OSRWindow::OnPaint(CefRefPtr<CefBrowser> browser, | ||||
|                         const RectList& dirtyRects, | ||||
|                         const void* buffer, | ||||
|                         int width, int height) { | ||||
|   if (!hDC_) | ||||
|     EnableGL(); | ||||
|  | ||||
|   wglMakeCurrent(hDC_, hRC_); | ||||
|   renderer_.OnPaint(browser, type, dirtyRects, buffer, width, height); | ||||
|   renderer_.Render(); | ||||
| @@ -127,8 +130,10 @@ void OSRWindow::OnCursorChange(CefRefPtr<CefBrowser> browser, | ||||
|  | ||||
| OSRWindow::OSRWindow(OSRBrowserProvider* browser_provider, bool transparent) | ||||
|     : renderer_(transparent), | ||||
|       browser_provider_(browser_provider) { | ||||
|   Reset(); | ||||
|       browser_provider_(browser_provider), | ||||
|       hWnd_(NULL), | ||||
|       hDC_(NULL), | ||||
|       hRC_(NULL) { | ||||
| } | ||||
|  | ||||
| OSRWindow::~OSRWindow() { | ||||
| @@ -136,6 +141,8 @@ OSRWindow::~OSRWindow() { | ||||
| } | ||||
|  | ||||
| void OSRWindow::EnableGL() { | ||||
|   ASSERT(CefCurrentlyOn(TID_UI)); | ||||
|  | ||||
|   PIXELFORMATDESCRIPTOR pfd; | ||||
|   int format; | ||||
|  | ||||
| @@ -162,18 +169,29 @@ void OSRWindow::EnableGL() { | ||||
| } | ||||
|  | ||||
| void OSRWindow::DisableGL() { | ||||
|   ASSERT(CefCurrentlyOn(TID_UI)); | ||||
|  | ||||
|   if (!hDC_) | ||||
|     return; | ||||
|  | ||||
|   renderer_.Cleanup(); | ||||
|  | ||||
|   if (IsWindow(hWnd_)) { | ||||
|     wglMakeCurrent(NULL, NULL); | ||||
|     wglDeleteContext(hRC_); | ||||
|     ReleaseDC(hWnd_, hDC_); | ||||
| } | ||||
|   } | ||||
|  | ||||
| void OSRWindow::Reset() { | ||||
|   hWnd_ = NULL; | ||||
|   hDC_ = NULL; | ||||
|   hRC_ = NULL; | ||||
| } | ||||
|  | ||||
| void OSRWindow::OnDestroyed() { | ||||
|   SetWindowLongPtr(hWnd_, GWLP_USERDATA, 0L); | ||||
|   hWnd_ = NULL; | ||||
|   Release(); | ||||
| } | ||||
|  | ||||
| ATOM OSRWindow::RegisterOSRClass(HINSTANCE hInstance, LPCTSTR className) { | ||||
|   WNDCLASSEX wcex; | ||||
|  | ||||
| @@ -208,10 +226,8 @@ LRESULT CALLBACK OSRWindow::WndProc(HWND hWnd, UINT message, | ||||
|  | ||||
|   switch (message) { | ||||
|   case WM_DESTROY: | ||||
|     if (window) { | ||||
|       SetWindowLongPtr(hWnd, GWLP_USERDATA, 0L); | ||||
|       window->Release(); | ||||
|     } | ||||
|     if (window) | ||||
|       window->OnDestroyed(); | ||||
|     return 0; | ||||
|  | ||||
|   case WM_LBUTTONDOWN: | ||||
|   | ||||
| @@ -7,6 +7,7 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "include/cef_render_handler.h" | ||||
| #include "tests/cefclient/client_handler.h" | ||||
| #include "tests/cefclient/osrenderer.h" | ||||
|  | ||||
| class OSRBrowserProvider { | ||||
| @@ -17,14 +18,15 @@ class OSRBrowserProvider { | ||||
|   virtual ~OSRBrowserProvider() {} | ||||
| }; | ||||
|  | ||||
| class OSRWindow : public CefRenderHandler { | ||||
| class OSRWindow : public ClientHandler::RenderHandler { | ||||
|  public: | ||||
|   // Create a new OSRWindow instance. |browser_provider| must outlive this | ||||
|   // object. | ||||
|   static CefRefPtr<OSRWindow> Create(OSRBrowserProvider* browser_provider, | ||||
|                                      bool transparent); | ||||
|  | ||||
|   static CefRefPtr<OSRWindow> From(CefRefPtr<CefRenderHandler> renderHandler); | ||||
|   static CefRefPtr<OSRWindow> From( | ||||
|       CefRefPtr<ClientHandler::RenderHandler> renderHandler); | ||||
|  | ||||
|   // Create the underlying window. | ||||
|   bool CreateWidget(HWND hWndParent, const RECT& rect, | ||||
| @@ -37,6 +39,9 @@ class OSRWindow : public CefRenderHandler { | ||||
|     return hWnd_; | ||||
|   } | ||||
|  | ||||
|   // ClientHandler::RenderHandler methods | ||||
|   virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE; | ||||
|  | ||||
|   // CefRenderHandler methods | ||||
|   virtual bool GetRootScreenRect(CefRefPtr<CefBrowser> browser, | ||||
|                                  CefRect& rect) OVERRIDE; | ||||
| @@ -66,7 +71,7 @@ class OSRWindow : public CefRenderHandler { | ||||
|  | ||||
|   void EnableGL(); | ||||
|   void DisableGL(); | ||||
|   void Reset(); | ||||
|   void OnDestroyed(); | ||||
|   static ATOM RegisterOSRClass(HINSTANCE hInstance, LPCTSTR className); | ||||
|   static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, | ||||
|                                   LPARAM lParam); | ||||
|   | ||||
| @@ -545,12 +545,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, | ||||
|       break; | ||||
|  | ||||
|     case WM_DESTROY: | ||||
|       // The frame window has exited | ||||
|       if (g_handler->GetOSRHandler().get()) { | ||||
|         OSRWindow::From(g_handler->GetOSRHandler())->DestroyWidget(); | ||||
|         g_handler->SetOSRHandler(NULL); | ||||
|       } | ||||
|  | ||||
|       PostQuitMessage(0); | ||||
|       return 0; | ||||
|     } | ||||
|   | ||||
| @@ -281,6 +281,11 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) { | ||||
|   if (m_BrowserId == browser->GetIdentifier()) { | ||||
|     // Free the browser pointer so that the browser can be destroyed | ||||
|     m_Browser = NULL; | ||||
|  | ||||
|     if (m_OSRHandler.get()) { | ||||
|       m_OSRHandler->OnBeforeClose(browser); | ||||
|       m_OSRHandler = NULL; | ||||
|     } | ||||
|   } else if (browser->IsPopup()) { | ||||
|     // Remove the record for DevTools popup windows. | ||||
|     std::set<std::string>::iterator it = | ||||
|   | ||||
| @@ -64,6 +64,12 @@ class ClientHandler : public CefClient, | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   // Interface implemented to handle off-screen rendering. | ||||
|   class RenderHandler : public CefRenderHandler { | ||||
|    public: | ||||
|     virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) =0; | ||||
|   }; | ||||
|  | ||||
|   typedef std::set<CefRefPtr<RequestDelegate> > RequestDelegateSet; | ||||
|  | ||||
|   ClientHandler(); | ||||
| @@ -216,10 +222,10 @@ class ClientHandler : public CefClient, | ||||
|   void SetMainHwnd(CefWindowHandle hwnd); | ||||
|   CefWindowHandle GetMainHwnd() { return m_MainHwnd; } | ||||
|   void SetEditHwnd(CefWindowHandle hwnd); | ||||
|   void SetOSRHandler(CefRefPtr<CefRenderHandler> handler) { | ||||
|   void SetOSRHandler(CefRefPtr<RenderHandler> handler) { | ||||
|     m_OSRHandler = handler; | ||||
|   } | ||||
|   CefRefPtr<CefRenderHandler> GetOSRHandler() { return m_OSRHandler; } | ||||
|   CefRefPtr<RenderHandler> GetOSRHandler() { return m_OSRHandler; } | ||||
|   void SetButtonHwnds(CefWindowHandle backHwnd, | ||||
|                       CefWindowHandle forwardHwnd, | ||||
|                       CefWindowHandle reloadHwnd, | ||||
| @@ -298,7 +304,7 @@ class ClientHandler : public CefClient, | ||||
|   CefWindowHandle m_StopHwnd; | ||||
|   CefWindowHandle m_ReloadHwnd; | ||||
|  | ||||
|   CefRefPtr<CefRenderHandler> m_OSRHandler; | ||||
|   CefRefPtr<RenderHandler> m_OSRHandler; | ||||
|  | ||||
|   // Support for logging. | ||||
|   std::string m_LogFile; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user