Mac: Fix rendering of the background color (issue #1161)

git-svn-id: https://chromiumembedded.googlecode.com/svn/branches/1547@1554 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2014-01-03 21:39:18 +00:00
parent b18102df04
commit 55ee7d4dd6
6 changed files with 288 additions and 91 deletions

View File

@ -75,7 +75,7 @@ typedef int int32;
typedef unsigned int uint32;
#endif
// UTF-16 character type
// UTF-16 character type.
#ifndef char16
#if defined(WIN32)
typedef wchar_t char16;
@ -84,6 +84,27 @@ typedef unsigned short char16;
#endif
#endif
// 32-bit ARGB color value, not premultiplied. The color components are always
// in a known order. Equivalent to the SkColor type.
typedef uint32 cef_color_t;
// Return the alpha byte from a cef_color_t value.
#define CefColorGetA(color) (((color) >> 24) & 0xFF)
// Return the red byte from a cef_color_t value.
#define CefColorGetR(color) (((color) >> 16) & 0xFF)
// Return the green byte from a cef_color_t value.
#define CefColorGetG(color) (((color) >> 8) & 0xFF)
// Return the blue byte from a cef_color_t value.
#define CefColorGetB(color) (((color) >> 0) & 0xFF)
// Return an cef_color_t value with the specified byte component values.
#define CefColorSetARGB(a, r, g, b) \
static_cast<uint32_t>( \
(static_cast<unsigned>(a) << 24) | \
(static_cast<unsigned>(r) << 16) | \
(static_cast<unsigned>(g) << 8) | \
(static_cast<unsigned>(b) << 0))
#ifdef __cplusplus
extern "C" {
#endif
@ -335,6 +356,12 @@ typedef struct _cef_settings_t {
// "ignore-certificate-errors" command-line switch.
///
bool ignore_certificate_errors;
///
// Used on Mac OS X to specify the background color for hardware accelerated
// content.
///
cef_color_t background_color;
} cef_settings_t;
///

View File

@ -376,6 +376,7 @@ struct CefSettingsTraits {
target->uncaught_exception_stack_size = src->uncaught_exception_stack_size;
target->context_safety_implementation = src->context_safety_implementation;
target->ignore_certificate_errors = src->ignore_certificate_errors;
target->background_color = src->background_color;
}
};

View File

@ -3,8 +3,21 @@
// found in the LICENSE file.
#include "libcef/browser/browser_main.h"
#include "libcef/browser/context.h"
#include "content/browser/renderer_host/compositing_iosurface_shader_programs_mac.h"
void CefBrowserMainParts::PlatformInitialize() {
const CefSettings& settings = _Context->settings();
if (CefColorGetA(settings.background_color) != 0) {
const float r =
static_cast<float>(CefColorGetR(settings.background_color)) / 255.0f;
const float g =
static_cast<float>(CefColorGetG(settings.background_color)) / 255.0f;
const float b =
static_cast<float>(CefColorGetB(settings.background_color)) / 255.0f;
content::CompositingIOSurfaceShaderPrograms::SetBackgroundColor(r, g, b);
}
}
void CefBrowserMainParts::PlatformCleanup() {

View File

@ -65,7 +65,9 @@ patches = [
{
# Fix crash on 10.6 and older 10.7 versions when building with the 10.7 SDK.
# http://code.google.com/p/chromiumembedded/issues/detail?id=1026
'name': 'renderer_host_1026',
# Support customization of the background color on OS X.
# http://code.google.com/p/chromiumembedded/issues/detail?id=1161
'name': 'renderer_host_1026_1161',
'path': '../content/browser/renderer_host/',
},
{

View File

@ -1,89 +0,0 @@
Index: render_widget_host_view_mac.mm
===================================================================
--- render_widget_host_view_mac.mm (revision 214871)
+++ render_widget_host_view_mac.mm (working copy)
@@ -4,6 +4,7 @@
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
+#import <objc/runtime.h>
#include <QuartzCore/QuartzCore.h>
#include "base/bind.h"
@@ -129,6 +130,29 @@
return modifiers;
}
+// This method will return YES for OS X versions 10.7.3 and later, and NO
+// otherwise.
+// Used to prevent a crash when building with the 10.7 SDK and accessing the
+// notification below. See: http://crbug.com/260595.
+static BOOL SupportsBackingPropertiesChangedNotification() {
+ // windowDidChangeBackingProperties: method has been added to the
+ // NSWindowDelegate protocol in 10.7.3, at the same time as the
+ // NSWindowDidChangeBackingPropertiesNotification notification was added.
+ // If the protocol contains this method description, the notification should
+ // be supported as well.
+ Protocol* windowDelegateProtocol = NSProtocolFromString(@"NSWindowDelegate");
+ struct objc_method_description methodDescription =
+ protocol_getMethodDescription(
+ windowDelegateProtocol,
+ @selector(windowDidChangeBackingProperties:),
+ NO,
+ YES);
+
+ // If the protocol does not contain the method, the returned method
+ // description is {NULL, NULL}
+ return methodDescription.name != NULL || methodDescription.types != NULL;
+}
+
static float ScaleFactor(NSView* view) {
return ui::GetScaleFactorScale(ui::GetScaleFactorForNativeView(view));
}
@@ -2395,13 +2419,21 @@
NSNotificationCenter* notificationCenter =
[NSNotificationCenter defaultCenter];
+
+ // Backing property notifications crash on 10.6 when building with the 10.7
+ // SDK, see http://crbug.com/260595.
+ static BOOL supportsBackingPropertiesNotification =
+ SupportsBackingPropertiesChangedNotification();
+
if (oldWindow) {
+ if (supportsBackingPropertiesNotification) {
+ [notificationCenter
+ removeObserver:self
+ name:NSWindowDidChangeBackingPropertiesNotification
+ object:oldWindow];
+ }
[notificationCenter
removeObserver:self
- name:NSWindowDidChangeBackingPropertiesNotification
- object:oldWindow];
- [notificationCenter
- removeObserver:self
name:NSWindowDidMoveNotification
object:oldWindow];
[notificationCenter
@@ -2410,13 +2442,15 @@
object:oldWindow];
}
if (newWindow) {
+ if (supportsBackingPropertiesNotification) {
+ [notificationCenter
+ addObserver:self
+ selector:@selector(windowDidChangeBackingProperties:)
+ name:NSWindowDidChangeBackingPropertiesNotification
+ object:newWindow];
+ }
[notificationCenter
addObserver:self
- selector:@selector(windowDidChangeBackingProperties:)
- name:NSWindowDidChangeBackingPropertiesNotification
- object:newWindow];
- [notificationCenter
- addObserver:self
selector:@selector(windowChangedGlobalFrame:)
name:NSWindowDidMoveNotification
object:newWindow];

View File

@ -0,0 +1,243 @@
Index: compositing_iosurface_mac.mm
===================================================================
--- compositing_iosurface_mac.mm (revision 226504)
+++ compositing_iosurface_mac.mm (working copy)
@@ -515,7 +515,7 @@
glUseProgram(0); CHECK_AND_SAVE_GL_ERROR();
} else {
// Should match the clear color of RenderWidgetHostViewMac.
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
+ glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
}
Index: compositing_iosurface_shader_programs_mac.cc
===================================================================
--- compositing_iosurface_shader_programs_mac.cc (revision 226504)
+++ compositing_iosurface_shader_programs_mac.cc (working copy)
@@ -11,6 +11,7 @@
#include "base/debug/trace_event.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
+#include "base/strings/stringprintf.h"
namespace content {
@@ -23,6 +24,7 @@
// latest version supported by MacOS 10.6.
#define GLSL_PROGRAM_AS_STRING(shader_code) "#version 120\n" #shader_code
+float bgcolor[] = {1.0f, 1.0f, 1.0f};
// Only the bare-bones calculations here for speed.
const char kvsBlit[] = GLSL_PROGRAM_AS_STRING(
@@ -229,6 +231,16 @@
const GLuint shader = glCreateShader(shader_type);
DCHECK_NE(shader, 0u);
+
+ // Support customization of the background color.
+ std::string bg_shader;
+ if (shader_program == SHADER_PROGRAM_SOLID_WHITE &&
+ shader_type == GL_FRAGMENT_SHADER) {
+ bg_shader = base::StringPrintf(
+ "void main() { gl_FragColor = vec4(%f, %f, %f, 1.0); }",
+ bgcolor[0], bgcolor[1], bgcolor[2]);
+ kFragmentShaderSourceCodeMap[shader_program] = bg_shader.c_str();
+ }
// Select and compile the shader program source code.
glShaderSource(shader,
@@ -371,6 +383,14 @@
return true;
}
+// static
+void CompositingIOSurfaceShaderPrograms::SetBackgroundColor(
+ float r, float g, float b) {
+ bgcolor[0] = r;
+ bgcolor[1] = g;
+ bgcolor[2] = b;
+}
+
GLuint CompositingIOSurfaceShaderPrograms::GetShaderProgram(int which) {
if (shader_programs_[which] == 0u) {
shader_programs_[which] =
Index: compositing_iosurface_shader_programs_mac.h
===================================================================
--- compositing_iosurface_shader_programs_mac.h (revision 226504)
+++ compositing_iosurface_shader_programs_mac.h (working copy)
@@ -39,6 +39,8 @@
// Returns false on error.
bool UseRGBToYV12Program(int pass_number, float texel_scale_x);
+ static void SetBackgroundColor(float r, float g, float b);
+
private:
enum { kNumShaderPrograms = 4 };
Index: compositing_iosurface_layer_mac.mm
===================================================================
--- compositing_iosurface_layer_mac.mm (revision 226504)
+++ compositing_iosurface_layer_mac.mm (working copy)
@@ -23,7 +23,7 @@
renderWidgetHostView_ = r;
ScopedCAActionDisabler disabler;
- [self setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
+ [self setBackgroundColor:CGColorGetConstantColor(kCGColorClear)];
[self setAutoresizingMask:kCALayerWidthSizable | kCALayerHeightSizable];
[self setContentsGravity:kCAGravityTopLeft];
[self setFrame:NSRectToCGRect(
@@ -102,7 +102,7 @@
displayTime:(const CVTimeStamp*)timeStamp {
if (!context_ || !renderWidgetHostView_ ||
!renderWidgetHostView_->compositing_iosurface_) {
- glClearColor(1, 1, 1, 1);
+ glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
return;
}
Index: render_widget_host_view_mac.mm
===================================================================
--- render_widget_host_view_mac.mm (revision 226504)
+++ render_widget_host_view_mac.mm (working copy)
@@ -4,6 +4,7 @@
#include "content/browser/renderer_host/render_widget_host_view_mac.h"
+#import <objc/runtime.h>
#include <QuartzCore/QuartzCore.h>
#include "base/bind.h"
@@ -129,6 +130,29 @@
return modifiers;
}
+// This method will return YES for OS X versions 10.7.3 and later, and NO
+// otherwise.
+// Used to prevent a crash when building with the 10.7 SDK and accessing the
+// notification below. See: http://crbug.com/260595.
+static BOOL SupportsBackingPropertiesChangedNotification() {
+ // windowDidChangeBackingProperties: method has been added to the
+ // NSWindowDelegate protocol in 10.7.3, at the same time as the
+ // NSWindowDidChangeBackingPropertiesNotification notification was added.
+ // If the protocol contains this method description, the notification should
+ // be supported as well.
+ Protocol* windowDelegateProtocol = NSProtocolFromString(@"NSWindowDelegate");
+ struct objc_method_description methodDescription =
+ protocol_getMethodDescription(
+ windowDelegateProtocol,
+ @selector(windowDidChangeBackingProperties:),
+ NO,
+ YES);
+
+ // If the protocol does not contain the method, the returned method
+ // description is {NULL, NULL}
+ return methodDescription.name != NULL || methodDescription.types != NULL;
+}
+
static float ScaleFactor(NSView* view) {
return ui::GetScaleFactorScale(ui::GetScaleFactorForNativeView(view));
}
@@ -465,7 +489,7 @@
software_layer_.reset([[CALayer alloc] init]);
if (!software_layer_)
LOG(ERROR) << "Failed to create CALayer for software rendering";
- [software_layer_ setBackgroundColor:CGColorGetConstantColor(kCGColorWhite)];
+ [software_layer_ setBackgroundColor:CGColorGetConstantColor(kCGColorClear)];
[software_layer_ setDelegate:cocoa_view_];
[software_layer_ setAutoresizingMask:kCALayerWidthSizable |
kCALayerHeightSizable];
@@ -2395,13 +2419,21 @@
NSNotificationCenter* notificationCenter =
[NSNotificationCenter defaultCenter];
+
+ // Backing property notifications crash on 10.6 when building with the 10.7
+ // SDK, see http://crbug.com/260595.
+ static BOOL supportsBackingPropertiesNotification =
+ SupportsBackingPropertiesChangedNotification();
+
if (oldWindow) {
+ if (supportsBackingPropertiesNotification) {
+ [notificationCenter
+ removeObserver:self
+ name:NSWindowDidChangeBackingPropertiesNotification
+ object:oldWindow];
+ }
[notificationCenter
removeObserver:self
- name:NSWindowDidChangeBackingPropertiesNotification
- object:oldWindow];
- [notificationCenter
- removeObserver:self
name:NSWindowDidMoveNotification
object:oldWindow];
[notificationCenter
@@ -2410,13 +2442,15 @@
object:oldWindow];
}
if (newWindow) {
+ if (supportsBackingPropertiesNotification) {
+ [notificationCenter
+ addObserver:self
+ selector:@selector(windowDidChangeBackingProperties:)
+ name:NSWindowDidChangeBackingPropertiesNotification
+ object:newWindow];
+ }
[notificationCenter
addObserver:self
- selector:@selector(windowDidChangeBackingProperties:)
- name:NSWindowDidChangeBackingPropertiesNotification
- object:newWindow];
- [notificationCenter
- addObserver:self
selector:@selector(windowChangedGlobalFrame:)
name:NSWindowDidMoveNotification
object:newWindow];
@@ -2528,7 +2562,7 @@
NSRect r = [self flipRectToNSRect:gfx::Rect(x, y, width, height)];
CGContextSetFillColorWithColor(context,
- CGColorGetConstantColor(kCGColorWhite));
+ CGColorGetConstantColor(kCGColorClear));
CGContextFillRect(context, NSRectToCGRect(r));
}
if (damagedRect.bottom() > rect.bottom()) {
@@ -2550,7 +2584,7 @@
NSRect r = [self flipRectToNSRect:gfx::Rect(x, y, width, height)];
CGContextSetFillColorWithColor(context,
- CGColorGetConstantColor(kCGColorWhite));
+ CGColorGetConstantColor(kCGColorClear));
CGContextFillRect(context, NSRectToCGRect(r));
}
}
@@ -2561,7 +2595,7 @@
if (!renderWidgetHostView_->render_widget_host_) {
// TODO(shess): Consider using something more noticable?
- [[NSColor whiteColor] set];
+ [[NSColor clearColor] set];
NSRectFill(dirtyRect);
return;
}
@@ -2675,7 +2709,7 @@
}
} else {
CGContextSetFillColorWithColor(context,
- CGColorGetConstantColor(kCGColorWhite));
+ CGColorGetConstantColor(kCGColorClear));
CGContextFillRect(context, dirtyRect);
if (renderWidgetHostView_->whiteout_start_time_.is_null())
renderWidgetHostView_->whiteout_start_time_ = base::TimeTicks::Now();
@@ -3676,7 +3710,7 @@
if (!renderWidgetHostView_->render_widget_host_ ||
renderWidgetHostView_->is_hidden()) {
CGContextSetFillColorWithColor(context,
- CGColorGetConstantColor(kCGColorWhite));
+ CGColorGetConstantColor(kCGColorClear));
CGContextFillRect(context, clipRect);
return;
}