Commit 56c39f54 authored by yzshen@chromium.org's avatar yzshen@chromium.org

Revert 95611 - Always call the class methods to save/restore contexts.

If the current context was ever nil, sending a "restore" message would be a no-op and would likely leave an out-of-scope context as current.

BUG=90140
TEST=repeatedly select items in an open file dialog in column mode; no crash

Review URL: http://codereview.chromium.org/7572031

TBR=avi@chromium.org
Review URL: http://codereview.chromium.org/7541043

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95631 0039d316-1c4b-4281-b951-d872f2087c98
parent 54ccbd14
...@@ -56,7 +56,8 @@ const CGFloat kBorderRadius = 3.0; ...@@ -56,7 +56,8 @@ const CGFloat kBorderRadius = 3.0;
if (!themeProvider) if (!themeProvider)
return; return;
gfx::ScopedNSGraphicsContextSaveGState scopedGState; NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
// Draw the background. // Draw the background.
{ {
...@@ -102,9 +103,8 @@ const CGFloat kBorderRadius = 3.0; ...@@ -102,9 +103,8 @@ const CGFloat kBorderRadius = 3.0;
// Fade in/out the background. // Fade in/out the background.
{ {
gfx::ScopedNSGraphicsContextSaveGState bgScopedState; gfx::ScopedNSGraphicsContextSaveGState bgScopedState(context);
[border setClip]; [border setClip];
NSGraphicsContext* context = [NSGraphicsContext currentContext];
CGContextRef cgContext = (CGContextRef)[context graphicsPort]; CGContextRef cgContext = (CGContextRef)[context graphicsPort];
CGContextBeginTransparencyLayer(cgContext, NULL); CGContextBeginTransparencyLayer(cgContext, NULL);
CGContextSetAlpha(cgContext, 1 - morph); CGContextSetAlpha(cgContext, 1 - morph);
......
...@@ -406,14 +406,14 @@ const int kInterruptedAnimationDuration = 2.5; ...@@ -406,14 +406,14 @@ const int kInterruptedAnimationDuration = 2.5;
nil]; nil];
NSPoint secondaryPos = NSPoint secondaryPos =
NSMakePoint(innerFrame.origin.x + kTextPosLeft, kSecondaryTextPosTop); NSMakePoint(innerFrame.origin.x + kTextPosLeft, kSecondaryTextPosTop);
gfx::ScopedNSGraphicsContextSaveGState contextSave;
NSGraphicsContext* nsContext = [NSGraphicsContext currentContext]; NSGraphicsContext* nsContext = [NSGraphicsContext currentContext];
CGContextRef cgContext = (CGContextRef)[nsContext graphicsPort]; CGContextRef cgContext = (CGContextRef)[nsContext graphicsPort];
[nsContext saveGraphicsState];
[nsContext setCompositingOperation:NSCompositeSourceOver]; [nsContext setCompositingOperation:NSCompositeSourceOver];
CGContextSetAlpha(cgContext, statusAlpha_); CGContextSetAlpha(cgContext, statusAlpha_);
[secondaryText drawAtPoint:secondaryPos [secondaryText drawAtPoint:secondaryPos
withAttributes:secondaryTextAttributes]; withAttributes:secondaryTextAttributes];
[nsContext restoreGraphicsState];
} }
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
...@@ -579,7 +579,8 @@ const int kInterruptedAnimationDuration = 2.5; ...@@ -579,7 +579,8 @@ const int kInterruptedAnimationDuration = 2.5;
[triangle lineToPoint:p3]; [triangle lineToPoint:p3];
[triangle closePath]; [triangle closePath];
gfx::ScopedNSGraphicsContextSaveGState scopedGState; NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
[shadow.get() setShadowColor:[NSColor whiteColor]]; [shadow.get() setShadowColor:[NSColor whiteColor]];
......
...@@ -64,8 +64,8 @@ CGFloat kCurveSize = 8; ...@@ -64,8 +64,8 @@ CGFloat kCurveSize = 8;
controlPoint2:NSMakePoint(midRight2.x, topLeft.y)]; controlPoint2:NSMakePoint(midRight2.x, topLeft.y)];
{ {
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext]; NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
[path addClip]; [path addClip];
// Set the pattern phase // Set the pattern phase
......
...@@ -251,8 +251,8 @@ const CGFloat kRapidCloseDist = 2.5; ...@@ -251,8 +251,8 @@ const CGFloat kRapidCloseDist = 2.5;
- (void)drawRect:(NSRect)dirtyRect { - (void)drawRect:(NSRect)dirtyRect {
const CGFloat lineWidth = [self cr_lineWidth]; const CGFloat lineWidth = [self cr_lineWidth];
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext]; NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
ThemeService* themeProvider = ThemeService* themeProvider =
static_cast<ThemeService*>([[self window] themeProvider]); static_cast<ThemeService*>([[self window] themeProvider]);
...@@ -288,8 +288,7 @@ const CGFloat kRapidCloseDist = 2.5; ...@@ -288,8 +288,7 @@ const CGFloat kRapidCloseDist = 2.5;
[[[self window] backgroundColor] set]; [[[self window] backgroundColor] set];
[path fill]; [path fill];
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState; gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context);
NSGraphicsContext* context = [NSGraphicsContext currentContext];
CGContextRef cgContext = CGContextRef cgContext =
static_cast<CGContextRef>([context graphicsPort]); static_cast<CGContextRef>([context graphicsPort]);
CGContextBeginTransparencyLayer(cgContext, 0); CGContextBeginTransparencyLayer(cgContext, 0);
...@@ -300,6 +299,55 @@ const CGFloat kRapidCloseDist = 2.5; ...@@ -300,6 +299,55 @@ const CGFloat kRapidCloseDist = 2.5;
} }
} }
[context saveGraphicsState];
[path addClip];
// Use the same overlay for the selected state and for hover and alert glows;
// for the selected state, it's fully opaque.
CGFloat hoverAlpha = [self hoverAlpha];
CGFloat alertAlpha = [self alertAlpha];
if (selected || hoverAlpha > 0 || alertAlpha > 0) {
// Draw the selected background / glow overlay.
gfx::ScopedNSGraphicsContextSaveGState drawHoverState(context);
CGContextRef cgContext = static_cast<CGContextRef>([context graphicsPort]);
CGContextBeginTransparencyLayer(cgContext, 0);
if (!selected) {
// The alert glow overlay is like the selected state but at most at most
// 80% opaque. The hover glow brings up the overlay's opacity at most 50%.
CGFloat backgroundAlpha = 0.8 * alertAlpha;
backgroundAlpha += (1 - backgroundAlpha) * 0.5 * hoverAlpha;
CGContextSetAlpha(cgContext, backgroundAlpha);
}
[path addClip];
{
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context);
[super drawBackgroundWithOpaque:NO];
}
// Draw a mouse hover gradient for the default themes.
if (!selected && hoverAlpha > 0) {
if (themeProvider && !hasBackgroundImage) {
scoped_nsobject<NSGradient> glow([NSGradient alloc]);
[glow initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0
alpha:1.0 * hoverAlpha]
endingColor:[NSColor colorWithCalibratedWhite:1.0
alpha:0.0]];
NSPoint point = hoverPoint_;
point.y = NSHeight(rect);
[glow drawFromCenter:point
radius:0.0
toCenter:point
radius:NSWidth(rect) / 3.0
options:NSGradientDrawsBeforeStartingLocation];
[glow drawInBezierPath:path relativeCenterPosition:hoverPoint_];
}
}
CGContextEndTransparencyLayer(cgContext);
}
BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow]; BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow];
CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2; CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2;
NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha]; NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha];
...@@ -308,87 +356,35 @@ const CGFloat kRapidCloseDist = 2.5; ...@@ -308,87 +356,35 @@ const CGFloat kRapidCloseDist = 2.5;
ThemeService::COLOR_TOOLBAR_BEZEL : ThemeService::COLOR_TOOLBAR_BEZEL :
ThemeService::COLOR_TOOLBAR, true) : nil; ThemeService::COLOR_TOOLBAR, true) : nil;
{ // Draw the top inner highlight within the tab if using the default theme.
gfx::ScopedNSGraphicsContextSaveGState contextSave; if (themeProvider && themeProvider->UsingDefaultTheme()) {
[path addClip]; NSAffineTransform* highlightTransform = [NSAffineTransform transform];
[highlightTransform translateXBy:lineWidth yBy:-lineWidth];
// Use the same overlay for the selected state and for hover and alert if (selected) {
// glows; for the selected state, it's fully opaque. scoped_nsobject<NSBezierPath> highlightPath([path copy]);
CGFloat hoverAlpha = [self hoverAlpha]; [highlightPath transformUsingAffineTransform:highlightTransform];
CGFloat alertAlpha = [self alertAlpha]; [highlightColor setStroke];
if (selected || hoverAlpha > 0 || alertAlpha > 0) { [highlightPath setLineWidth:lineWidth];
// Draw the selected background / glow overlay. [highlightPath stroke];
gfx::ScopedNSGraphicsContextSaveGState drawHoverState; highlightTransform = [NSAffineTransform transform];
NSGraphicsContext* context = [NSGraphicsContext currentContext]; [highlightTransform translateXBy:-2 * lineWidth yBy:0.0];
CGContextRef cgContext = [highlightPath transformUsingAffineTransform:highlightTransform];
static_cast<CGContextRef>([context graphicsPort]); [highlightPath stroke];
CGContextBeginTransparencyLayer(cgContext, 0); } else {
if (!selected) { NSBezierPath* topHighlightPath =
// The alert glow overlay is like the selected state but at most at most [self topHighlightBezierPathForRect:[self bounds]];
// 80% opaque. The hover glow brings up the overlay's opacity at most [topHighlightPath transformUsingAffineTransform:highlightTransform];
// 50%. [highlightColor setStroke];
CGFloat backgroundAlpha = 0.8 * alertAlpha; [topHighlightPath setLineWidth:lineWidth];
backgroundAlpha += (1 - backgroundAlpha) * 0.5 * hoverAlpha; [topHighlightPath stroke];
CGContextSetAlpha(cgContext, backgroundAlpha);
}
[path addClip];
{
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState;
[super drawBackgroundWithOpaque:NO];
}
// Draw a mouse hover gradient for the default themes.
if (!selected && hoverAlpha > 0) {
if (themeProvider && !hasBackgroundImage) {
scoped_nsobject<NSGradient> glow([NSGradient alloc]);
[glow initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0
alpha:1.0 * hoverAlpha]
endingColor:[NSColor colorWithCalibratedWhite:1.0
alpha:0.0]];
NSPoint point = hoverPoint_;
point.y = NSHeight(rect);
[glow drawFromCenter:point
radius:0.0
toCenter:point
radius:NSWidth(rect) / 3.0
options:NSGradientDrawsBeforeStartingLocation];
[glow drawInBezierPath:path relativeCenterPosition:hoverPoint_];
}
}
CGContextEndTransparencyLayer(cgContext);
}
// Draw the top inner highlight within the tab if using the default theme.
if (themeProvider && themeProvider->UsingDefaultTheme()) {
NSAffineTransform* highlightTransform = [NSAffineTransform transform];
[highlightTransform translateXBy:lineWidth yBy:-lineWidth];
if (selected) {
scoped_nsobject<NSBezierPath> highlightPath([path copy]);
[highlightPath transformUsingAffineTransform:highlightTransform];
[highlightColor setStroke];
[highlightPath setLineWidth:lineWidth];
[highlightPath stroke];
highlightTransform = [NSAffineTransform transform];
[highlightTransform translateXBy:-2 * lineWidth yBy:0.0];
[highlightPath transformUsingAffineTransform:highlightTransform];
[highlightPath stroke];
} else {
NSBezierPath* topHighlightPath =
[self topHighlightBezierPathForRect:[self bounds]];
[topHighlightPath transformUsingAffineTransform:highlightTransform];
[highlightColor setStroke];
[topHighlightPath setLineWidth:lineWidth];
[topHighlightPath stroke];
}
} }
} }
[context restoreGraphicsState];
// Draw the top stroke. // Draw the top stroke.
{ {
gfx::ScopedNSGraphicsContextSaveGState drawBorderState; gfx::ScopedNSGraphicsContextSaveGState drawBorderState(context);
[borderColor set]; [borderColor set];
[path setLineWidth:lineWidth]; [path setLineWidth:lineWidth];
[path stroke]; [path stroke];
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#include "unicode/locid.h" #include "unicode/locid.h"
#include "webkit/glue/webkit_glue.h" #include "webkit/glue/webkit_glue.h"
#include "webkit/plugins/npapi/default_plugin_shared.h" #include "webkit/plugins/npapi/default_plugin_shared.h"
...@@ -140,7 +139,7 @@ void PluginInstallerImpl::DownloadCancelled() { ...@@ -140,7 +139,7 @@ void PluginInstallerImpl::DownloadCancelled() {
} }
int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) { int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) {
gfx::ScopedNSGraphicsContextSaveGState scoped_state; [NSGraphicsContext saveGraphicsState];
NSGraphicsContext* ns_context = [NSGraphicsContext NSGraphicsContext* ns_context = [NSGraphicsContext
graphicsContextWithGraphicsPort:context flipped:YES]; graphicsContextWithGraphicsPort:context flipped:YES];
[NSGraphicsContext setCurrentContext:ns_context]; [NSGraphicsContext setCurrentContext:ns_context];
...@@ -184,6 +183,7 @@ int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) { ...@@ -184,6 +183,7 @@ int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) {
label_point = NSMakePoint(roundf(label_point.x), roundf(label_point.y)); label_point = NSMakePoint(roundf(label_point.x), roundf(label_point.y));
[command_ drawAtPoint:label_point withAttributes:attributes]; [command_ drawAtPoint:label_point withAttributes:attributes];
[NSGraphicsContext restoreGraphicsState];
return 1; return 1;
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#import "third_party/mozilla/NSPasteboard+Utils.h" #import "third_party/mozilla/NSPasteboard+Utils.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/canvas_skia.h" #include "ui/gfx/canvas_skia.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#include "ui/gfx/size.h" #include "ui/gfx/size.h"
namespace ui { namespace ui {
...@@ -249,7 +248,6 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const { ...@@ -249,7 +248,6 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const {
scoped_nsobject<NSImage> image( scoped_nsobject<NSImage> image(
[[NSImage alloc] initWithPasteboard:GetPasteboard()]); [[NSImage alloc] initWithPasteboard:GetPasteboard()]);
if (image.get()) { if (image.get()) {
gfx::ScopedNSGraphicsContextSaveGState scoped_state;
[image setFlipped:YES]; [image setFlipped:YES];
int width = [image size].width; int width = [image size].width;
int height = [image size].height; int height = [image size].height;
...@@ -264,6 +262,7 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const { ...@@ -264,6 +262,7 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const {
fromRect:NSZeroRect fromRect:NSZeroRect
operation:NSCompositeCopy operation:NSCompositeCopy
fraction:1.0]; fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
return canvas.ExtractBitmap(); return canvas.ExtractBitmap();
} }
return SkBitmap(); return SkBitmap();
......
...@@ -4,27 +4,23 @@ ...@@ -4,27 +4,23 @@
#ifndef UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_ #ifndef UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_
#define UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_ #define UI_GFX_SCOPED_NS_GRAPHICS_CONTEXT_SAVE_GSTATE_MAC_H_
#pragma once
#include "base/basictypes.h"
#include "ui/ui_api.h" #include "ui/ui_api.h"
#include "base/basictypes.h"
#include "base/memory/scoped_nsobject.h"
#if defined(__OBJC__)
@class NSGraphicsContext; @class NSGraphicsContext;
#else
class NSGraphicsContext;
#endif
namespace gfx { namespace gfx {
// A class to save/restore the state of the current context.
class UI_API ScopedNSGraphicsContextSaveGState { class UI_API ScopedNSGraphicsContextSaveGState {
public: public:
ScopedNSGraphicsContextSaveGState(); // If |context| is nil, it will use the |+currentContext|.
explicit ScopedNSGraphicsContextSaveGState(NSGraphicsContext* context = nil);
~ScopedNSGraphicsContextSaveGState(); ~ScopedNSGraphicsContextSaveGState();
private: private:
NSGraphicsContext* context_; // weak scoped_nsobject<NSGraphicsContext> context_;
DISALLOW_COPY_AND_ASSIGN(ScopedNSGraphicsContextSaveGState); DISALLOW_COPY_AND_ASSIGN(ScopedNSGraphicsContextSaveGState);
}; };
......
...@@ -4,20 +4,19 @@ ...@@ -4,20 +4,19 @@
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#import <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#include "base/logging.h"
namespace gfx { namespace gfx {
ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState() ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState(
: context_([NSGraphicsContext currentContext]) { NSGraphicsContext* context) : context_([context retain]) {
[NSGraphicsContext saveGraphicsState]; if (!context_)
context_.reset([[NSGraphicsContext currentContext] retain]);
[context_ saveGraphicsState];
} }
ScopedNSGraphicsContextSaveGState::~ScopedNSGraphicsContextSaveGState() { ScopedNSGraphicsContextSaveGState::~ScopedNSGraphicsContextSaveGState() {
[NSGraphicsContext restoreGraphicsState]; [context_ restoreGraphicsState];
DCHECK_EQ(context_, [NSGraphicsContext currentContext]);
} }
} // namespace gfx } // namespace gfx
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