This source file includes following definitions.
- RoundUpSurfaceDimension
- AddBooleanValue
- AddIntegerValue
- did_unschedule_
- Initialize
- Destroy
- DeferDraws
- IsOffscreen
- OnMakeCurrent
- GetBackingFrameBufferObject
- SetBackbufferAllocation
- SetFrontbufferAllocation
- AdjustBufferAllocation
- SwapBuffers
- PostSubBuffer
- SupportsPostSubBuffer
- GetSize
- OnBufferPresented
- OnResizeViewACK
- OnResize
- SetLatencyInfo
- WakeUpGpu
- OnWillDestroyStub
- UnrefIOSurface
- CreateIOSurface
- SwapBuffers
- CreateNativeSurface
- SetAllowOSMesaForTesting
#include "content/common/gpu/image_transport_surface.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/gpu/gpu_command_buffer_stub.h"
#include "content/common/gpu/gpu_messages.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_context.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/gl_surface_cgl.h"
#include "ui/gl/gl_surface_osmesa.h"
#include "ui/gl/io_surface_support_mac.h"
namespace content {
namespace {
const uint32 kIOSurfaceDimensionRoundup = 64;
int RoundUpSurfaceDimension(int number) {
DCHECK(number >= 0);
uint32 unsigned_number = static_cast<uint32>(number);
uint32 roundup_sub_1 = kIOSurfaceDimensionRoundup - 1;
unsigned_number = (unsigned_number + roundup_sub_1) & ~roundup_sub_1;
return static_cast<int>(unsigned_number);
}
class IOSurfaceImageTransportSurface
: public gfx::NoOpGLSurfaceCGL,
public ImageTransportSurface,
public GpuCommandBufferStub::DestructionObserver {
public:
IOSurfaceImageTransportSurface(GpuChannelManager* manager,
GpuCommandBufferStub* stub,
gfx::PluginWindowHandle handle);
virtual bool Initialize() OVERRIDE;
virtual void Destroy() OVERRIDE;
virtual bool DeferDraws() OVERRIDE;
virtual bool IsOffscreen() OVERRIDE;
virtual bool SwapBuffers() OVERRIDE;
virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE;
virtual bool SupportsPostSubBuffer() OVERRIDE;
virtual gfx::Size GetSize() OVERRIDE;
virtual bool OnMakeCurrent(gfx::GLContext* context) OVERRIDE;
virtual unsigned int GetBackingFrameBufferObject() OVERRIDE;
virtual bool SetBackbufferAllocation(bool allocated) OVERRIDE;
virtual void SetFrontbufferAllocation(bool allocated) OVERRIDE;
protected:
virtual void OnBufferPresented(
const AcceleratedSurfaceMsg_BufferPresented_Params& params) OVERRIDE;
virtual void OnResizeViewACK() OVERRIDE;
virtual void OnResize(gfx::Size size, float scale_factor) OVERRIDE;
virtual void SetLatencyInfo(
const std::vector<ui::LatencyInfo>&) OVERRIDE;
virtual void WakeUpGpu() OVERRIDE;
virtual void OnWillDestroyStub() OVERRIDE;
private:
virtual ~IOSurfaceImageTransportSurface() OVERRIDE;
void AdjustBufferAllocation();
void UnrefIOSurface();
void CreateIOSurface();
bool backbuffer_suggested_allocation_;
bool frontbuffer_suggested_allocation_;
uint32 fbo_id_;
GLuint texture_id_;
GLuint depth_stencil_renderbuffer_id_;
base::ScopedCFTypeRef<CFTypeRef> io_surface_;
uint64 io_surface_handle_;
gfx::GLContext* context_;
gfx::Size size_;
gfx::Size rounded_size_;
float scale_factor_;
bool made_current_;
bool is_swap_buffers_pending_;
bool did_unschedule_;
std::vector<ui::LatencyInfo> latency_info_;
scoped_ptr<ImageTransportHelper> helper_;
DISALLOW_COPY_AND_ASSIGN(IOSurfaceImageTransportSurface);
};
void AddBooleanValue(CFMutableDictionaryRef dictionary,
const CFStringRef key,
bool value) {
CFDictionaryAddValue(dictionary, key,
(value ? kCFBooleanTrue : kCFBooleanFalse));
}
void AddIntegerValue(CFMutableDictionaryRef dictionary,
const CFStringRef key,
int32 value) {
base::ScopedCFTypeRef<CFNumberRef> number(
CFNumberCreate(NULL, kCFNumberSInt32Type, &value));
CFDictionaryAddValue(dictionary, key, number.get());
}
IOSurfaceImageTransportSurface::IOSurfaceImageTransportSurface(
GpuChannelManager* manager,
GpuCommandBufferStub* stub,
gfx::PluginWindowHandle handle)
: gfx::NoOpGLSurfaceCGL(gfx::Size(1, 1)),
backbuffer_suggested_allocation_(true),
frontbuffer_suggested_allocation_(true),
fbo_id_(0),
texture_id_(0),
depth_stencil_renderbuffer_id_(0),
io_surface_handle_(0),
context_(NULL),
scale_factor_(1.f),
made_current_(false),
is_swap_buffers_pending_(false),
did_unschedule_(false) {
helper_.reset(new ImageTransportHelper(this, manager, stub, handle));
}
IOSurfaceImageTransportSurface::~IOSurfaceImageTransportSurface() {
}
bool IOSurfaceImageTransportSurface::Initialize() {
if (gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL &&
gfx::GetGLImplementation() != gfx::kGLImplementationAppleGL)
return false;
if (!helper_->Initialize())
return false;
if (!NoOpGLSurfaceCGL::Initialize()) {
helper_->Destroy();
return false;
}
helper_->stub()->AddDestructionObserver(this);
return true;
}
void IOSurfaceImageTransportSurface::Destroy() {
UnrefIOSurface();
helper_->Destroy();
NoOpGLSurfaceCGL::Destroy();
}
bool IOSurfaceImageTransportSurface::DeferDraws() {
if(did_unschedule_)
return true;
if (is_swap_buffers_pending_) {
did_unschedule_ = true;
helper_->SetScheduled(false);
return true;
}
return false;
}
bool IOSurfaceImageTransportSurface::IsOffscreen() {
return false;
}
bool IOSurfaceImageTransportSurface::OnMakeCurrent(gfx::GLContext* context) {
context_ = context;
if (made_current_)
return true;
OnResize(gfx::Size(1, 1), 1.f);
made_current_ = true;
return true;
}
unsigned int IOSurfaceImageTransportSurface::GetBackingFrameBufferObject() {
return fbo_id_;
}
bool IOSurfaceImageTransportSurface::SetBackbufferAllocation(bool allocation) {
if (backbuffer_suggested_allocation_ == allocation)
return true;
backbuffer_suggested_allocation_ = allocation;
AdjustBufferAllocation();
return true;
}
void IOSurfaceImageTransportSurface::SetFrontbufferAllocation(bool allocation) {
if (frontbuffer_suggested_allocation_ == allocation)
return;
frontbuffer_suggested_allocation_ = allocation;
AdjustBufferAllocation();
}
void IOSurfaceImageTransportSurface::AdjustBufferAllocation() {
if (!backbuffer_suggested_allocation_ &&
!frontbuffer_suggested_allocation_ &&
io_surface_.get()) {
UnrefIOSurface();
helper_->Suspend();
} else if (backbuffer_suggested_allocation_ && !io_surface_) {
CreateIOSurface();
}
}
bool IOSurfaceImageTransportSurface::SwapBuffers() {
DCHECK(backbuffer_suggested_allocation_);
if (!frontbuffer_suggested_allocation_)
return true;
glFlush();
GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params params;
params.surface_handle = io_surface_handle_;
params.size = GetSize();
params.scale_factor = scale_factor_;
params.latency_info.swap(latency_info_);
helper_->SendAcceleratedSurfaceBuffersSwapped(params);
DCHECK(!is_swap_buffers_pending_);
is_swap_buffers_pending_ = true;
return true;
}
bool IOSurfaceImageTransportSurface::PostSubBuffer(
int x, int y, int width, int height) {
DCHECK(backbuffer_suggested_allocation_);
if (!frontbuffer_suggested_allocation_)
return true;
glFlush();
GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params params;
params.surface_handle = io_surface_handle_;
params.x = x;
params.y = y;
params.width = width;
params.height = height;
params.surface_size = GetSize();
params.surface_scale_factor = scale_factor_;
params.latency_info.swap(latency_info_);
helper_->SendAcceleratedSurfacePostSubBuffer(params);
DCHECK(!is_swap_buffers_pending_);
is_swap_buffers_pending_ = true;
return true;
}
bool IOSurfaceImageTransportSurface::SupportsPostSubBuffer() {
return true;
}
gfx::Size IOSurfaceImageTransportSurface::GetSize() {
return size_;
}
void IOSurfaceImageTransportSurface::OnBufferPresented(
const AcceleratedSurfaceMsg_BufferPresented_Params& params) {
DCHECK(is_swap_buffers_pending_);
context_->share_group()->SetRendererID(params.renderer_id);
is_swap_buffers_pending_ = false;
if (did_unschedule_) {
did_unschedule_ = false;
helper_->SetScheduled(true);
}
}
void IOSurfaceImageTransportSurface::OnResizeViewACK() {
NOTREACHED();
}
void IOSurfaceImageTransportSurface::OnResize(gfx::Size size,
float scale_factor) {
TRACE_EVENT2("gpu", "IOSurfaceImageTransportSurface::OnResize",
"old_width", size_.width(), "new_width", size.width());
DCHECK(context_->IsCurrent(this));
size_ = size;
scale_factor_ = scale_factor;
CreateIOSurface();
}
void IOSurfaceImageTransportSurface::SetLatencyInfo(
const std::vector<ui::LatencyInfo>& latency_info) {
for (size_t i = 0; i < latency_info.size(); i++)
latency_info_.push_back(latency_info[i]);
}
void IOSurfaceImageTransportSurface::WakeUpGpu() {
NOTIMPLEMENTED();
}
void IOSurfaceImageTransportSurface::OnWillDestroyStub() {
helper_->stub()->RemoveDestructionObserver(this);
Destroy();
}
void IOSurfaceImageTransportSurface::UnrefIOSurface() {
if (context_ || fbo_id_ || texture_id_ || depth_stencil_renderbuffer_id_) {
DCHECK(gfx::GLContext::GetCurrent() == context_);
DCHECK(context_->IsCurrent(this));
DCHECK(CGLGetCurrentContext());
}
if (fbo_id_) {
glDeleteFramebuffersEXT(1, &fbo_id_);
fbo_id_ = 0;
}
if (texture_id_) {
glDeleteTextures(1, &texture_id_);
texture_id_ = 0;
}
if (depth_stencil_renderbuffer_id_) {
glDeleteRenderbuffersEXT(1, &depth_stencil_renderbuffer_id_);
depth_stencil_renderbuffer_id_ = 0;
}
io_surface_.reset();
io_surface_handle_ = 0;
}
void IOSurfaceImageTransportSurface::CreateIOSurface() {
gfx::Size new_rounded_size(RoundUpSurfaceDimension(size_.width()),
RoundUpSurfaceDimension(size_.height()));
if (io_surface_.get() && new_rounded_size == rounded_size_)
return;
TRACE_EVENT2("gpu", "IOSurfaceImageTransportSurface::CreateIOSurface",
"width", new_rounded_size.width(),
"height", new_rounded_size.height());
rounded_size_ = new_rounded_size;
GLint previous_texture_id = 0;
glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &previous_texture_id);
UnrefIOSurface();
glGenFramebuffersEXT(1, &fbo_id_);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo_id_);
IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize();
glGenTextures(1, &texture_id_);
GLenum target = GL_TEXTURE_RECTANGLE_ARB;
glBindTexture(target, texture_id_);
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
GL_COLOR_ATTACHMENT0_EXT,
target,
texture_id_,
0);
int32 stencil_bits =
helper_->stub()->GetRequestedAttribute(EGL_STENCIL_SIZE);
if (stencil_bits > 0) {
bool has_packed_depth_stencil =
GLSurface::ExtensionsContain(
reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)),
"GL_EXT_packed_depth_stencil");
if (has_packed_depth_stencil) {
glGenRenderbuffersEXT(1, &depth_stencil_renderbuffer_id_);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT,
depth_stencil_renderbuffer_id_);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT,
rounded_size_.width(), rounded_size_.height());
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
GL_STENCIL_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT,
depth_stencil_renderbuffer_id_);
}
}
base::ScopedCFTypeRef<CFMutableDictionaryRef> properties;
properties.reset(CFDictionaryCreateMutable(kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
AddIntegerValue(properties,
io_surface_support->GetKIOSurfaceWidth(),
rounded_size_.width());
AddIntegerValue(properties,
io_surface_support->GetKIOSurfaceHeight(),
rounded_size_.height());
AddIntegerValue(properties,
io_surface_support->GetKIOSurfaceBytesPerElement(), 4);
AddBooleanValue(properties,
io_surface_support->GetKIOSurfaceIsGlobal(), true);
io_surface_.reset(io_surface_support->IOSurfaceCreate(properties));
io_surface_handle_ = io_surface_support->IOSurfaceGetID(io_surface_);
GLuint plane = 0;
CGLError cglerror =
io_surface_support->CGLTexImageIOSurface2D(
static_cast<CGLContextObj>(context_->GetHandle()),
target,
GL_RGBA,
rounded_size_.width(),
rounded_size_.height(),
GL_BGRA,
GL_UNSIGNED_INT_8_8_8_8_REV,
io_surface_.get(),
plane);
if (cglerror != kCGLNoError) {
UnrefIOSurface();
return;
}
glFlush();
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
DLOG(ERROR) << "Framebuffer was incomplete: " << status;
UnrefIOSurface();
return;
}
glBindTexture(target, previous_texture_id);
}
class DRTSurfaceOSMesa : public gfx::GLSurfaceOSMesa {
public:
DRTSurfaceOSMesa() : GLSurfaceOSMesa(GL_RGBA, gfx::Size(1, 1)) {}
virtual bool SwapBuffers() OVERRIDE;
private:
virtual ~DRTSurfaceOSMesa() {}
DISALLOW_COPY_AND_ASSIGN(DRTSurfaceOSMesa);
};
bool DRTSurfaceOSMesa::SwapBuffers() {
return true;
}
bool g_allow_os_mesa = false;
}
scoped_refptr<gfx::GLSurface> ImageTransportSurface::CreateNativeSurface(
GpuChannelManager* manager,
GpuCommandBufferStub* stub,
const gfx::GLSurfaceHandle& surface_handle) {
DCHECK(surface_handle.transport_type == gfx::NATIVE_TRANSPORT);
IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize();
switch (gfx::GetGLImplementation()) {
case gfx::kGLImplementationDesktopGL:
case gfx::kGLImplementationAppleGL:
if (!io_surface_support) {
DLOG(WARNING) << "No IOSurface support";
return scoped_refptr<gfx::GLSurface>();
}
return scoped_refptr<gfx::GLSurface>(new IOSurfaceImageTransportSurface(
manager, stub, surface_handle.handle));
default:
if (!g_allow_os_mesa) {
NOTREACHED();
return scoped_refptr<gfx::GLSurface>();
}
scoped_refptr<gfx::GLSurface> surface(new DRTSurfaceOSMesa());
if (!surface.get() || !surface->Initialize())
return surface;
return scoped_refptr<gfx::GLSurface>(new PassThroughImageTransportSurface(
manager, stub, surface.get(), false));
}
}
void ImageTransportSurface::SetAllowOSMesaForTesting(bool allow) {
g_allow_os_mesa = allow;
}
}