Browse Source

More work on the web view

Josh Engebretson 10 years ago
parent
commit
2a8b3226a7

+ 5 - 0
Source/Atomic/Graphics/OpenGL/OGLGraphics.cpp

@@ -2615,6 +2615,11 @@ unsigned Graphics::GetRGBAFormat()
     return GL_RGBA;
 }
 
+unsigned Graphics::GetBGRAFormat()
+{
+    return GL_BGRA;
+}
+
 unsigned Graphics::GetRGBA16Format()
 {
 #ifndef GL_ES_VERSION_2_0

+ 2 - 0
Source/Atomic/Graphics/OpenGL/OGLGraphics.h

@@ -490,6 +490,8 @@ public:
     static unsigned GetRGBFormat();
     /// Return the API-specific RGBA texture format.
     static unsigned GetRGBAFormat();
+    /// Return the API-specific BGRA texture format.
+    static unsigned GetBGRAFormat();
     /// Return the API-specific RGBA 16-bit texture format.
     static unsigned GetRGBA16Format();
     /// Return the API-specific RGBA 16-bit float texture format.

+ 3 - 0
Source/Atomic/Graphics/OpenGL/OGLTexture.cpp

@@ -319,6 +319,7 @@ unsigned Texture::GetRowDataSize(int width) const
         return (unsigned)(width * 3);
 
     case GL_RGBA:
+    case GL_BGRA:
 #ifndef GL_ES_VERSION_2_0
     case GL_DEPTH24_STENCIL8_EXT:
     case GL_RG16:
@@ -409,6 +410,8 @@ unsigned Texture::GetDataType(unsigned format)
     else if (format == GL_RGBA16F_ARB || format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F ||
              format == GL_R16F || format == GL_R32F)
         return GL_FLOAT;
+    else if (format == GL_BGRA)
+        return GL_UNSIGNED_INT_8_8_8_8_REV;
     else
         return GL_UNSIGNED_BYTE;
 #else

+ 4 - 0
Source/Atomic/Graphics/OpenGL/OGLTexture2D.cpp

@@ -244,6 +244,8 @@ bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, con
 
     if (!IsCompressed())
     {
+        if (format == GL_BGRA)
+            format = GL_RGBA;
         if (wholeLevel)
             glTexImage2D(target_, level, format, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data);
         else
@@ -482,6 +484,8 @@ bool Texture2D::Create()
     if (!IsCompressed())
     {
         glGetError();
+        if (format == GL_BGRA)
+            format = GL_RGBA;
         glTexImage2D(target_, 0, format, width_, height_, 0, externalFormat, dataType, 0);
         if (glGetError())
         {

+ 10 - 5
Source/AtomicEditor/Application/AEEditorCommon.cpp

@@ -39,6 +39,7 @@
 namespace Atomic
 {
     void jsapi_init_atomicnet(JSVM* vm);
+    void jsapi_init_webview(JSVM* vm);;
 }
 
 using namespace ToolCore;
@@ -62,11 +63,6 @@ void AEEditorCommon::Start()
 {
     ValidateWindow();
 
-#ifdef ATOMIC_WEBVIEW
-    // Initialize in Start so window already exists
-    context_->RegisterSubsystem(new WebBrowserHost(context_));
-#endif
-
     Input* input = GetSubsystem<Input>();
     input->SetMouseVisible(true);
 
@@ -76,10 +72,19 @@ void AEEditorCommon::Start()
 
     jsapi_init_toolcore(vm_);
 
+#ifdef ATOMIC_WEBVIEW
+    // Initialize in Start so window already exists
+    context_->RegisterSubsystem(new WebBrowserHost(context_));
+    jsapi_init_webview(vm_);
+#endif
+
+
 #ifdef ATOMIC_DOTNET
     jsapi_init_atomicnet(vm_);
 #endif
 
+
+
 }
 
 void AEEditorCommon::Setup()

+ 9 - 1
Source/AtomicWebView/CMakeLists.txt

@@ -5,7 +5,15 @@ file (GLOB SOURCE_FILES *.cpp *.h)
 
 file (GLOB JAVASCRIPT_BINDINGS_SOURCE ${CMAKE_SOURCE_DIR}/Artifacts/Build/Source/Generated/${JAVASCRIPT_BINDINGS_PLATFORM}/Javascript/Packages/WebView/*.cpp)
 
-set (SOURCE_FILES ${SOURCE_FILES} ${JAVASCRIPT_BINDINGS_SOURCE} )
+if (APPLE)
+    if (NOT IOS)
+        set (PLATFORM_SOURCE WebBrowserHost.mm)
+    else()
+    endif()
+endif()
+
+
+set (SOURCE_FILES ${SOURCE_FILES} ${JAVASCRIPT_BINDINGS_SOURCE} ${PLATFORM_SOURCE} )
 
 add_library(AtomicWebView ${SOURCE_FILES})
 

+ 45 - 18
Source/AtomicWebView/WebBrowserHost.cpp

@@ -1,31 +1,36 @@
 
+#include <SDL/include/SDL.h>
+#include <ThirdParty/SDL/include/SDL_syswm.h>
+
 #include <ThirdParty/CEF/include/cef_app.h>
 #include <ThirdParty/CEF/include/cef_client.h>
 #include <ThirdParty/CEF/include/cef_render_handler.h>
 #include <ThirdParty/CEF/include/wrapper/cef_helpers.h>
 
-
 #include <Atomic/Core/ProcessUtils.h>
+#include <Atomic/Core/CoreEvents.h>
 #include <Atomic/IO/Log.h>
 
+#include <Atomic/Graphics/Graphics.h>
+
 #include "WebClient.h"
 #include "WebBrowserHost.h"
 
 class SimpleApp : public CefApp,
-                  public CefBrowserProcessHandler {
- public:
-  SimpleApp();
+        public CefBrowserProcessHandler {
+public:
+    SimpleApp();
 
-  // CefApp methods:
-  virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
-      OVERRIDE { return this; }
+    // CefApp methods:
+    virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
+    OVERRIDE { return this; }
 
-  // CefBrowserProcessHandler methods:
-  virtual void OnContextInitialized() OVERRIDE;
+    // CefBrowserProcessHandler methods:
+    virtual void OnContextInitialized() OVERRIDE;
 
- private:
-  // Include the default reference counting implementation.
-  IMPLEMENT_REFCOUNTING(SimpleApp);
+private:
+    // Include the default reference counting implementation.
+    IMPLEMENT_REFCOUNTING(SimpleApp);
 };
 
 SimpleApp::SimpleApp() {
@@ -33,13 +38,17 @@ SimpleApp::SimpleApp() {
 
 void SimpleApp::OnContextInitialized() {
 
-  CEF_REQUIRE_UI_THREAD();
+    CEF_REQUIRE_UI_THREAD();
 
 }
 
 namespace Atomic
 {
 
+#ifdef ATOMIC_PLATFORM_OSX
+void* GetNSWindowContentView(void* window);
+#endif
+
 WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
 {
     const Vector<String>& arguments = GetArguments();
@@ -70,11 +79,13 @@ WebBrowserHost::WebBrowserHost(Context* context) : Object (context)
         LOGERROR("CefInitialize - Error");
     }
 
+    SubscribeToEvent(E_BEGINFRAME, HANDLER(WebBrowserHost, HandleBeginFrame));
+
 }
 
 WebBrowserHost::~WebBrowserHost()
 {
-
+    CefShutdown();
 }
 
 bool WebBrowserHost::CreateBrowser(WebClient* webClient)
@@ -82,12 +93,28 @@ bool WebBrowserHost::CreateBrowser(WebClient* webClient)
     CefWindowInfo windowInfo;
     CefBrowserSettings browserSettings;
 
-    CefBrowserHost::CreateBrowser(windowInfo, (CefClient*) webClient->d_,
-                                  "http://www.atomicgameengine.com", browserSettings, nullptr);
+    Graphics* graphics = GetSubsystem<Graphics>();
 
-    return true;
-}
+    SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
+    SDL_SysWMinfo info;
+    SDL_VERSION(&info.version);
 
+    if(SDL_GetWindowWMInfo(sdlWindow, &info))
+    {
+        NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
+        windowInfo.SetAsWindowless(view, false);
 
+        return CefBrowserHost::CreateBrowser(windowInfo, (CefClient*) webClient->d_,
+                                             "https://html5test.com/", browserSettings, nullptr);
+    }
+
+    return false;
+
+}
+
+void WebBrowserHost::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
+{
+    CefDoMessageLoopWork();
+}
 
 }

+ 2 - 0
Source/AtomicWebView/WebBrowserHost.h

@@ -22,6 +22,8 @@ public:
 
 private:
 
+    void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
+
 };
 
 }

+ 11 - 0
Source/AtomicWebView/WebBrowserHost.mm

@@ -0,0 +1,11 @@
+#include <Cocoa/Cocoa.h>
+
+namespace Atomic
+{
+
+void *GetNSWindowContentView(void *window)
+{
+    return ((NSWindow*)window).contentView;
+}
+
+}

+ 10 - 0
Source/AtomicWebView/WebClient.cpp

@@ -18,6 +18,15 @@ public:
         webClient_ = client;
     }
 
+    CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE {
+
+        if (webClient_->renderHandler_.Null())
+            return nullptr;
+
+        return webClient_->renderHandler_->GetCEFRenderHandler();
+
+    }
+
     IMPLEMENT_REFCOUNTING(WebClientPrivate)
 
 private:
@@ -30,6 +39,7 @@ private:
 WebClient::WebClient(Context* context) : Object(context)
 {
     d_ = new WebClientPrivate(this);
+    d_->AddRef();
 }
 
 WebClient::~WebClient()

+ 5 - 1
Source/AtomicWebView/WebRenderHandler.h

@@ -4,6 +4,8 @@
 
 #pragma once
 
+class CefRenderHandler;
+
 namespace Atomic
 {
 
@@ -30,7 +32,9 @@ public:
     unsigned GetMaxWidth() const { return maxWidth_; }
     unsigned GetMaxHeight() const { return maxHeight_; }
 
-private:
+    virtual CefRenderHandler* GetCEFRenderHandler() = 0;
+
+protected:
 
     unsigned currentWidth_;
     unsigned currentHeight_;

+ 17 - 8
Source/AtomicWebView/WebTexture2D.cpp

@@ -43,12 +43,17 @@ private:
 WebTexture2D::WebTexture2D(Context* context, int width, int height) : WebRenderHandler(context)
 {
     d_ = new WebTexture2DPrivate(this);
+    d_->AddRef();
 
     ResourceCache* cache = GetSubsystem<ResourceCache>();
 
+    currentWidth_ = maxWidth_ = width;
+    currentHeight_ = maxHeight_ = height;
+
     texture_ = new Texture2D(context_);
-    texture_->SetSize(width, height, Graphics::GetRGBAFormat(), TEXTURE_DYNAMIC);
-    texture_->SetFilterMode(FILTER_NEAREST);
+    texture_->SetNumLevels(1);
+    texture_->SetSize(width, height, Graphics::GetBGRAFormat(), TEXTURE_DYNAMIC);
+    texture_->SetFilterMode(FILTER_BILINEAR);
 
     material_ = new Material(context_);
 
@@ -56,6 +61,16 @@ WebTexture2D::WebTexture2D(Context* context, int width, int height) : WebRenderH
     material_->SetTexture(TU_DIFFUSE, texture_);
 }
 
+WebTexture2D::~WebTexture2D()
+{
+    d_->Release();
+}
+
+CefRenderHandler* WebTexture2D::GetCEFRenderHandler()
+{
+    return d_;
+}
+
 void WebTexture2D::SetCurrentWidth(unsigned width)
 {
 
@@ -76,10 +91,4 @@ void WebTexture2D::SetMaxHeight(unsigned height)
 
 }
 
-WebTexture2D::~WebTexture2D()
-{
-    d_->Release();
-}
-
-
 }

+ 5 - 0
Source/AtomicWebView/WebTexture2D.h

@@ -30,6 +30,11 @@ public:
     virtual void SetMaxWidth(unsigned width);
     virtual void SetMaxHeight(unsigned height);
 
+    CefRenderHandler* GetCEFRenderHandler();
+
+    Texture2D* GetTexture2D() const { return texture_; }
+    Material* GetMaterial() const { return material_; }
+
 private:
 
     SharedPtr<Texture2D> texture_;

+ 47 - 0
Source/AtomicWebView/WebViewJS.cpp

@@ -0,0 +1,47 @@
+//
+// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include <AtomicJS/Javascript/JSVM.h>
+
+#include "WebBrowserHost.h"
+
+namespace Atomic
+{
+
+extern void jsb_package_webview_init(JSVM* vm);
+
+void jsapi_init_webview(JSVM* vm)
+{
+    duk_context* ctx = vm->GetJSContext();
+
+    jsb_package_webview_init(vm);
+
+    duk_get_global_string(ctx, "WebView");
+
+    js_push_class_object_instance(ctx, vm->GetSubsystem<WebBrowserHost>(), "WebBrowserHost");
+    duk_put_prop_string(ctx, -2, "browserHost");
+
+    duk_pop(ctx);
+
+}
+
+}