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 @@
#define MOJO_SERVICES_NATIVE_VIEWPORT_NATIVE_VIEWPORT_H_
#include "base/memory/scoped_ptr.h"
namespace gfx {
class Size;
}
namespace gpu {
namespace gles2 {
class GLES2Interface;
}
}
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/size.h"
namespace ui {
class Event;
......@@ -32,11 +24,10 @@ class NativeViewportDelegate {
public:
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 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.
......@@ -44,6 +35,7 @@ class NativeViewport {
public:
virtual ~NativeViewport() {}
virtual gfx::Size GetSize() = 0;
virtual void Close() = 0;
static scoped_ptr<NativeViewport> Create(shell::Context* context,
......
......@@ -5,8 +5,6 @@
#include "mojo/services/native_viewport/native_viewport_android.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/shell/context.h"
......@@ -27,19 +25,7 @@ NativeViewportAndroid::~NativeViewportAndroid() {
void NativeViewportAndroid::OnNativeWindowCreated(ANativeWindow* window) {
DCHECK(!window_);
window_ = 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();
delegate_->OnAcceleratedWidgetAvailable(window_);
}
void NativeViewportAndroid::OnNativeWindowDestroyed() {
......@@ -53,11 +39,14 @@ void NativeViewportAndroid::OnResized(const gfx::Size& size) {
}
void NativeViewportAndroid::ReleaseWindow() {
gl_context_.reset();
ANativeWindow_release(window_);
window_ = NULL;
}
gfx::Size NativeViewportAndroid::GetSize() {
return size_;
}
void NativeViewportAndroid::Close() {
// TODO(beng): close activity containing MojoView?
......
......@@ -33,15 +33,14 @@ class NativeViewportAndroid : public NativeViewport {
private:
// Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE;
virtual void Close() OVERRIDE;
void OnGLContextLost();
void ReleaseWindow();
NativeViewportDelegate* delegate_;
ANativeWindow* window_;
gfx::Size size_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
base::WeakPtrFactory<NativeViewportAndroid> weak_factory_;
......
......@@ -4,9 +4,11 @@
#include "mojo/services/native_viewport/native_viewport_controller.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.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 "ui/events/event.h"
......@@ -34,10 +36,18 @@ bool NativeViewportController::OnEvent(ui::Event* event) {
return false;
}
void NativeViewportController::OnGLContextAvailable(
gpu::gles2::GLES2Interface* gl) {
void NativeViewportController::OnAcceleratedWidgetAvailable(
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
// pipe_ somehow.
gpu::gles2::GLES2Interface* gl = gl_context_->GetImplementation();
gl->ClearColor(0, 1, 0, 0);
gl->Clear(GL_COLOR_BUFFER_BIT);
gl->SwapBuffers();
......
......@@ -10,6 +10,10 @@
#include "mojo/public/system/core.h"
#include "mojo/services/native_viewport/native_viewport.h"
namespace gpu {
class GLInProcessContext;
}
namespace mojo {
namespace services {
......@@ -26,16 +30,19 @@ class NativeViewportController : public services::NativeViewportDelegate {
private:
// 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 void OnDestroyed() OVERRIDE;
virtual void OnGLContextAvailable(gpu::gles2::GLES2Interface*) OVERRIDE;
virtual void OnGLContextLost() OVERRIDE;
virtual void OnResized(const gfx::Size& size) OVERRIDE;
void OnGLContextLost();
void SendString(const std::string& string);
Handle pipe_;
scoped_ptr<NativeViewport> native_viewport_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportController);
};
......
......@@ -9,9 +9,6 @@
#import <AppKit/NSWindow.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"
namespace mojo {
......@@ -31,15 +28,7 @@ class NativeViewportMac : public NativeViewport {
backing:NSBackingStoreBuffered
defer:NO];
[window_ orderFront:nil];
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());
delegate_->OnAcceleratedWidgetAvailable([window_ contentView]);
}
virtual ~NativeViewportMac() {
......@@ -49,20 +38,18 @@ class NativeViewportMac : public NativeViewport {
private:
// Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return rect_.size();
}
virtual void Close() OVERRIDE {
// TODO(beng): perform this in response to NSWindow destruction.
delegate_->OnDestroyed();
}
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_;
NSWindow* window_;
gfx::Rect rect_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportMac);
};
......
......@@ -19,6 +19,9 @@ class NativeViewportStub : public NativeViewport {
private:
// Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return gfx::Size();
}
virtual void Close() OVERRIDE {
delegate_->OnDestroyed();
}
......
......@@ -4,9 +4,6 @@
#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/gfx/win/window_impl.h"
......@@ -29,6 +26,11 @@ class NativeViewportWin : public gfx::WindowImpl,
private:
// 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 {
DestroyWindow(hwnd());
}
......@@ -51,16 +53,7 @@ class NativeViewportWin : public gfx::WindowImpl,
return 0;
}
LRESULT OnCreate(CREATESTRUCT* create_struct) {
RECT cr;
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());
delegate_->OnAcceleratedWidgetAvailable(hwnd());
return 0;
}
void OnPaint(HDC) {
......@@ -83,13 +76,7 @@ class NativeViewportWin : public gfx::WindowImpl,
delegate_->OnDestroyed();
}
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
DISALLOW_COPY_AND_ASSIGN(NativeViewportWin);
};
......
......@@ -6,12 +6,8 @@
#include <X11/Xlib.h>
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.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/x/x11_types.h"
......@@ -44,14 +40,7 @@ class NativeViewportX11 : public NativeViewport,
XMapWindow(display, window_);
XFlush(display);
gpu::GLInProcessContextAttribs attribs;
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());
delegate_->OnAcceleratedWidgetAvailable(window_);
}
virtual ~NativeViewportX11() {
......@@ -63,6 +52,9 @@ class NativeViewportX11 : public NativeViewport,
private:
// Overridden from NativeViewport:
virtual gfx::Size GetSize() OVERRIDE {
return bounds_.size();
}
virtual void Close() OVERRIDE {
// TODO(beng): perform this in response to XWindow destruction.
delegate_->OnDestroyed();
......@@ -73,15 +65,9 @@ class NativeViewportX11 : public NativeViewport,
return true;
}
void OnGLContextLost() {
gl_context_.reset();
delegate_->OnGLContextLost();
}
NativeViewportDelegate* delegate_;
gfx::Rect bounds_;
XID window_;
scoped_ptr<gpu::GLInProcessContext> gl_context_;
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