Commit e74c9bc2 authored by abarth@chromium.org's avatar abarth@chromium.org

Factor common code into native_viewport_controller.cc

The platform-specific implementations of NativeViewport have a bunch of
duplicated code. This CL centralizes that code in the cross-platform
controller class.

R=ben@chromium.org

Review URL: https://codereview.chromium.org/59383011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233511 0039d316-1c4b-4281-b951-d872f2087c98
parent ad7f3920
...@@ -6,16 +6,8 @@ ...@@ -6,16 +6,8 @@
#define MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_H_ #define MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_H_
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "ui/gfx/native_widget_types.h"
namespace gfx { #include "ui/gfx/size.h"
class Size;
}
namespace gpu {
namespace gles2 {
class GLES2Interface;
}
}
namespace ui { namespace ui {
class Event; class Event;
...@@ -32,11 +24,10 @@ class NativeViewportDelegate { ...@@ -32,11 +24,10 @@ class NativeViewportDelegate {
public: public:
virtual ~NativeViewportDelegate() {} virtual ~NativeViewportDelegate() {}
virtual void OnResized(const gfx::Size& size) = 0;
virtual void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) = 0;
virtual bool OnEvent(ui::Event* event) = 0; virtual bool OnEvent(ui::Event* event) = 0;
virtual void OnDestroyed() = 0; virtual void OnDestroyed() = 0;
virtual void OnGLContextAvailable(gpu::gles2::GLES2Interface* gl) = 0;
virtual void OnGLContextLost() = 0;
virtual void OnResized(const gfx::Size& size) = 0;
}; };
// Encapsulation of platform-specific Viewport. // Encapsulation of platform-specific Viewport.
...@@ -44,6 +35,7 @@ class NativeViewport { ...@@ -44,6 +35,7 @@ class NativeViewport {
public: public:
virtual ~NativeViewport() {} virtual ~NativeViewport() {}
virtual gfx::Size GetSize() = 0;
virtual void Close() = 0; virtual void Close() = 0;
static scoped_ptr<NativeViewport> Create(shell::Context* context, static scoped_ptr<NativeViewport> Create(shell::Context* context,
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#include "mojo/services/native_viewport/native_viewport_android.h" #include "mojo/services/native_viewport/native_viewport_android.h"
#include <android/native_window_jni.h> #include <android/native_window_jni.h>
#include "gpu/command_buffer/client/gl_in_process_context.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "mojo/services/native_viewport/android/mojo_viewport.h" #include "mojo/services/native_viewport/android/mojo_viewport.h"
#include "mojo/shell/context.h" #include "mojo/shell/context.h"
...@@ -27,19 +25,7 @@ NativeViewportAndroid::~NativeViewportAndroid() { ...@@ -27,19 +25,7 @@ NativeViewportAndroid::~NativeViewportAndroid() {
void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) { void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) {
DCHECK(!window_); DCHECK(!window_);
window_ = window; window_ = window;
delegate_->OnAcceleratedWidgetAvailable(window_);
gpu::GLInProcessContextAttribs attribs;
gl_context_.reset(gpu::GLInProcessContext::CreateContext(
false, window_, size_, false, attribs, gfx::PreferDiscreteGpu));
gl_context_->SetContextLostCallback(base::Bind(
&NativeViewportAndroid::OnGLContextLost, base::Unretained(this)));
delegate_->OnGLContextAvailable(gl_context_->GetImplementation());
}
void NativeViewportAndroid::OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
} }
void NativeViewportAndroid::OnNativeWindowDestroyed() { void NativeViewportAndroid::OnNativeWindowDestroyed() {
...@@ -53,11 +39,14 @@ void NativeViewportAndroid::OnResized(const gfx::Size& size) { ...@@ -53,11 +39,14 @@ void NativeViewportAndroid::OnResized(const gfx::Size& size) {
} }
void NativeViewportAndroid::ReleaseWindow() { void NativeViewportAndroid::ReleaseWindow() {
gl_context_.reset();
ANativeWindow_release(window_); ANativeWindow_release(window_);
window_ = NULL; window_ = NULL;
} }
gfx::Size NativeViewportAndroid::GetSize() {
return size_;
}
void NativeViewportAndroid::Close() { void NativeViewportAndroid::Close() {
// TODO(beng): close activity containing MojoView? // TODO(beng): close activity containing MojoView?
......
...@@ -33,15 +33,14 @@ class NativeViewportAndroid : public NativeViewport { ...@@ -33,15 +33,14 @@ class NativeViewportAndroid : public NativeViewport {
private: private:
// Overridden from NativeViewport: // Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE;
virtual void Close() OVERRIDE; virtual void Close() OVERRIDE;
void OnGLContextLost();
void ReleaseWindow(); void ReleaseWindow();
NativeViewportDelegate* delegate_; NativeViewportDelegate* delegate_;
ANativeWindow* window_; ANativeWindow* window_;
gfx::Size size_; gfx::Size size_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
base::WeakPtrFactory<NativeViewportAndroid> weak_factory_; base::WeakPtrFactory<NativeViewportAndroid> weak_factory_;
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
#include "mojo/services/native_viewport/native_viewport_controller.h" #include "mojo/services/native_viewport/native_viewport_controller.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "gpu/command_buffer/client/gles2_interface.h" #include "gpu/command_buffer/client/gl_in_process_context.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "mojo/services/native_viewport/native_viewport.h" #include "mojo/services/native_viewport/native_viewport.h"
#include "ui/events/event.h" #include "ui/events/event.h"
...@@ -34,10 +36,18 @@ bool NativeViewportController::OnEvent(ui::Event* event) { ...@@ -34,10 +36,18 @@ bool NativeViewportController::OnEvent(ui::Event* event) {
return false; return false;
} }
void NativeViewportController::OnGLContextAvailable( void NativeViewportController::OnAcceleratedWidgetAvailable(
gpu::gles2::GLES2Interface* gl) { gfx::AcceleratedWidget widget) {
gfx::Size size = native_viewport_->GetSize();
gpu::GLInProcessContextAttribs attribs;
gl_context_.reset(gpu::GLInProcessContext::CreateContext(
false, widget, size, false, attribs, gfx::PreferDiscreteGpu));
gl_context_->SetContextLostCallback(base::Bind(
&NativeViewportController::OnGLContextLost, base::Unretained(this)));
// TODO(abarth): Instead of drawing green, we want to send the context over // TODO(abarth): Instead of drawing green, we want to send the context over
// pipe_ somehow. // pipe_ somehow.
gpu::gles2::GLES2Interface* gl = gl_context_->GetImplementation();
gl->ClearColor(0, 1, 0, 0); gl->ClearColor(0, 1, 0, 0);
gl->Clear(GL_COLOR_BUFFER_BIT); gl->Clear(GL_COLOR_BUFFER_BIT);
gl->SwapBuffers(); gl->SwapBuffers();
......
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
#include "mojo/public/system/core.h" #include "mojo/public/system/core.h"
#include "mojo/services/native_viewport/native_viewport.h" #include "mojo/services/native_viewport/native_viewport.h"
namespace gpu {
class GLInProcessContext;
}
namespace mojo { namespace mojo {
namespace services { namespace services {
...@@ -26,16 +30,19 @@ class NativeViewportController : public services::NativeViewportDelegate { ...@@ -26,16 +30,19 @@ class NativeViewportController : public services::NativeViewportDelegate {
private: private:
// Overridden from services::NativeViewportDelegate: // Overridden from services::NativeViewportDelegate:
virtual void OnResized(const gfx::Size& size) OVERRIDE;
virtual void OnAcceleratedWidgetAvailable(
gfx::AcceleratedWidget widget) OVERRIDE;
virtual bool OnEvent(ui::Event* event) OVERRIDE; virtual bool OnEvent(ui::Event* event) OVERRIDE;
virtual void OnDestroyed() OVERRIDE; virtual void OnDestroyed() OVERRIDE;
virtual void OnGLContextAvailable(gpu::gles2::GLES2Interface*) OVERRIDE;
virtual void OnGLContextLost() OVERRIDE; void OnGLContextLost();
virtual void OnResized(const gfx::Size& size) OVERRIDE;
void SendString(const std::string& string); void SendString(const std::string& string);
Handle pipe_; Handle pipe_;
scoped_ptr<NativeViewport> native_viewport_; scoped_ptr<NativeViewport> native_viewport_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportController); DISALLOW_COPY_AND_ASSIGN(NativeViewportController);
}; };
......
...@@ -9,9 +9,6 @@ ...@@ -9,9 +9,6 @@
#import <AppKit/NSWindow.h> #import <AppKit/NSWindow.h>
#include "base/bind.h" #include "base/bind.h"
#include "base/mac/scoped_nsobject.h"
#include "gpu/command_buffer/client/gl_in_process_context.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
namespace mojo { namespace mojo {
...@@ -31,15 +28,7 @@ class NativeViewportMac : public NativeViewport { ...@@ -31,15 +28,7 @@ class NativeViewportMac : public NativeViewport {
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:NO]; defer:NO];
[window_ orderFront:nil]; [window_ orderFront:nil];
delegate_->OnAcceleratedWidgetAvailable([window_ contentView]);
gpu::GLInProcessContextAttribs attribs;
gl_context_.reset(gpu::GLInProcessContext::CreateContext(
false, [window_ contentView], rect_.size(), false,
attribs, gfx::PreferDiscreteGpu));
gl_context_->SetContextLostCallback(base::Bind(
&NativeViewportMac::OnGLContextLost, base::Unretained(this)));
delegate_->OnGLContextAvailable(gl_context_->GetImplementation());
} }
virtual ~NativeViewportMac() { virtual ~NativeViewportMac() {
...@@ -49,20 +38,18 @@ class NativeViewportMac : public NativeViewport { ...@@ -49,20 +38,18 @@ class NativeViewportMac : public NativeViewport {
private: private:
// Overridden from NativeViewport: // Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return rect_.size();
}
virtual void Close() OVERRIDE { virtual void Close() OVERRIDE {
// TODO(beng): perform this in response to NSWindow destruction. // TODO(beng): perform this in response to NSWindow destruction.
delegate_->OnDestroyed(); delegate_->OnDestroyed();
} }
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_; NativeViewportDelegate* delegate_;
NSWindow* window_; NSWindow* window_;
gfx::Rect rect_; gfx::Rect rect_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportMac); DISALLOW_COPY_AND_ASSIGN(NativeViewportMac);
}; };
......
...@@ -19,6 +19,9 @@ class NativeViewportStub : public NativeViewport { ...@@ -19,6 +19,9 @@ class NativeViewportStub : public NativeViewport {
private: private:
// Overridden from NativeViewport: // Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return gfx::Size();
}
virtual void Close() OVERRIDE { virtual void Close() OVERRIDE {
delegate_->OnDestroyed(); delegate_->OnDestroyed();
} }
......
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
#include "mojo/services/native_viewport/native_viewport.h" #include "mojo/services/native_viewport/native_viewport.h"
#include "base/bind.h"
#include "gpu/command_buffer/client/gl_in_process_context.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "ui/events/event.h" #include "ui/events/event.h"
#include "ui/gfx/win/window_impl.h" #include "ui/gfx/win/window_impl.h"
...@@ -29,6 +26,11 @@ class NativeViewportWin : public gfx::WindowImpl, ...@@ -29,6 +26,11 @@ class NativeViewportWin : public gfx::WindowImpl,
private: private:
// Overridden from NativeViewport: // Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
RECT cr;
GetClientRect(hwnd(), &cr);
return gfx::Size(cr.right - cr.left, cr.bottom - cr.top);
}
virtual void Close() OVERRIDE { virtual void Close() OVERRIDE {
DestroyWindow(hwnd()); DestroyWindow(hwnd());
} }
...@@ -51,16 +53,7 @@ class NativeViewportWin : public gfx::WindowImpl, ...@@ -51,16 +53,7 @@ class NativeViewportWin : public gfx::WindowImpl,
return 0; return 0;
} }
LRESULT OnCreate(CREATESTRUCT* create_struct) { LRESULT OnCreate(CREATESTRUCT* create_struct) {
RECT cr; delegate_->OnAcceleratedWidgetAvailable(hwnd());
GetClientRect(hwnd(), &cr);
gpu::GLInProcessContextAttribs attribs;
gl_context_.reset(gpu::GLInProcessContext::CreateContext(
false, hwnd(), gfx::Size(cr.right - cr.left, cr.bottom - cr.top),
false, attribs, gfx::PreferDiscreteGpu));
gl_context_->SetContextLostCallback(base::Bind(
&NativeViewportWin::OnGLContextLost, base::Unretained(this)));
delegate_->OnGLContextAvailable(gl_context_->GetImplementation());
return 0; return 0;
} }
void OnPaint(HDC) { void OnPaint(HDC) {
...@@ -83,13 +76,7 @@ class NativeViewportWin : public gfx::WindowImpl, ...@@ -83,13 +76,7 @@ class NativeViewportWin : public gfx::WindowImpl,
delegate_->OnDestroyed(); delegate_->OnDestroyed();
} }
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_; NativeViewportDelegate* delegate_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportWin); DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
}; };
......
...@@ -6,12 +6,8 @@ ...@@ -6,12 +6,8 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/message_loop/message_pump_x11.h" #include "base/message_loop/message_pump_x11.h"
#include "gpu/command_buffer/client/gl_in_process_context.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
#include "ui/gfx/x/x11_types.h" #include "ui/gfx/x/x11_types.h"
...@@ -44,14 +40,7 @@ class NativeViewportX11 : public NativeViewport, ...@@ -44,14 +40,7 @@ class NativeViewportX11 : public NativeViewport,
XMapWindow(display, window_); XMapWindow(display, window_);
XFlush(display); XFlush(display);
gpu::GLInProcessContextAttribs attribs; delegate_->OnAcceleratedWidgetAvailable(window_);
gl_context_.reset(gpu::GLInProcessContext::CreateContext(
false, window_, bounds_.size(), false,
attribs, gfx::PreferDiscreteGpu));
gl_context_->SetContextLostCallback(base::Bind(
&NativeViewportX11::OnGLContextLost, base::Unretained(this)));
delegate_->OnGLContextAvailable(gl_context_->GetImplementation());
} }
virtual ~NativeViewportX11() { virtual ~NativeViewportX11() {
...@@ -63,6 +52,9 @@ class NativeViewportX11 : public NativeViewport, ...@@ -63,6 +52,9 @@ class NativeViewportX11 : public NativeViewport,
private: private:
// Overridden from NativeViewport: // Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return bounds_.size();
}
virtual void Close() OVERRIDE { virtual void Close() OVERRIDE {
// TODO(beng): perform this in response to XWindow destruction. // TODO(beng): perform this in response to XWindow destruction.
delegate_->OnDestroyed(); delegate_->OnDestroyed();
...@@ -73,15 +65,9 @@ class NativeViewportX11 : public NativeViewport, ...@@ -73,15 +65,9 @@ class NativeViewportX11 : public NativeViewport,
return true; return true;
} }
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_; NativeViewportDelegate* delegate_;
gfx::Rect bounds_; gfx::Rect bounds_;
XID window_; XID window_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportX11); DISALLOW_COPY_AND_ASSIGN(NativeViewportX11);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment