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;
if (!themeProvider)
return;
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
// Draw the background.
{
......@@ -102,9 +103,8 @@ const CGFloat kBorderRadius = 3.0;
// Fade in/out the background.
{
gfx::ScopedNSGraphicsContextSaveGState bgScopedState;
gfx::ScopedNSGraphicsContextSaveGState bgScopedState(context);
[border setClip];
NSGraphicsContext* context = [NSGraphicsContext currentContext];
CGContextRef cgContext = (CGContextRef)[context graphicsPort];
CGContextBeginTransparencyLayer(cgContext, NULL);
CGContextSetAlpha(cgContext, 1 - morph);
......
......@@ -406,14 +406,14 @@ const int kInterruptedAnimationDuration = 2.5;
nil];
NSPoint secondaryPos =
NSMakePoint(innerFrame.origin.x + kTextPosLeft, kSecondaryTextPosTop);
gfx::ScopedNSGraphicsContextSaveGState contextSave;
NSGraphicsContext* nsContext = [NSGraphicsContext currentContext];
CGContextRef cgContext = (CGContextRef)[nsContext graphicsPort];
[nsContext saveGraphicsState];
[nsContext setCompositingOperation:NSCompositeSourceOver];
CGContextSetAlpha(cgContext, statusAlpha_);
[secondaryText drawAtPoint:secondaryPos
withAttributes:secondaryTextAttributes];
[nsContext restoreGraphicsState];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
......@@ -579,7 +579,8 @@ const int kInterruptedAnimationDuration = 2.5;
[triangle lineToPoint:p3];
[triangle closePath];
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
[shadow.get() setShadowColor:[NSColor whiteColor]];
......
......@@ -64,8 +64,8 @@ CGFloat kCurveSize = 8;
controlPoint2:NSMakePoint(midRight2.x, topLeft.y)];
{
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
[path addClip];
// Set the pattern phase
......
......@@ -251,8 +251,8 @@ const CGFloat kRapidCloseDist = 2.5;
- (void)drawRect:(NSRect)dirtyRect {
const CGFloat lineWidth = [self cr_lineWidth];
gfx::ScopedNSGraphicsContextSaveGState scopedGState;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState scopedGState(context);
ThemeService* themeProvider =
static_cast<ThemeService*>([[self window] themeProvider]);
......@@ -288,8 +288,7 @@ const CGFloat kRapidCloseDist = 2.5;
[[[self window] backgroundColor] set];
[path fill];
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context);
CGContextRef cgContext =
static_cast<CGContextRef>([context graphicsPort]);
CGContextBeginTransparencyLayer(cgContext, 0);
......@@ -300,40 +299,28 @@ const CGFloat kRapidCloseDist = 2.5;
}
}
BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow];
CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2;
NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha];
NSColor* highlightColor = themeProvider ? themeProvider->GetNSColor(
themeProvider->UsingDefaultTheme() ?
ThemeService::COLOR_TOOLBAR_BEZEL :
ThemeService::COLOR_TOOLBAR, true) : nil;
{
gfx::ScopedNSGraphicsContextSaveGState contextSave;
[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.
// 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;
NSGraphicsContext* context = [NSGraphicsContext currentContext];
CGContextRef cgContext =
static_cast<CGContextRef>([context graphicsPort]);
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%.
// 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;
gfx::ScopedNSGraphicsContextSaveGState drawBackgroundState(context);
[super drawBackgroundWithOpaque:NO];
}
......@@ -361,6 +348,14 @@ const CGFloat kRapidCloseDist = 2.5;
CGContextEndTransparencyLayer(cgContext);
}
BOOL active = [[self window] isKeyWindow] || [[self window] isMainWindow];
CGFloat borderAlpha = selected ? (active ? 0.3 : 0.2) : 0.2;
NSColor* borderColor = [NSColor colorWithDeviceWhite:0.0 alpha:borderAlpha];
NSColor* highlightColor = themeProvider ? themeProvider->GetNSColor(
themeProvider->UsingDefaultTheme() ?
ThemeService::COLOR_TOOLBAR_BEZEL :
ThemeService::COLOR_TOOLBAR, true) : nil;
// Draw the top inner highlight within the tab if using the default theme.
if (themeProvider && themeProvider->UsingDefaultTheme()) {
NSAffineTransform* highlightTransform = [NSAffineTransform transform];
......@@ -384,11 +379,12 @@ const CGFloat kRapidCloseDist = 2.5;
[topHighlightPath stroke];
}
}
}
[context restoreGraphicsState];
// Draw the top stroke.
{
gfx::ScopedNSGraphicsContextSaveGState drawBorderState;
gfx::ScopedNSGraphicsContextSaveGState drawBorderState(context);
[borderColor set];
[path setLineWidth:lineWidth];
[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
// found in the LICENSE file.
......@@ -17,7 +17,6 @@
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#include "unicode/locid.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/plugins/npapi/default_plugin_shared.h"
......@@ -140,7 +139,7 @@ void PluginInstallerImpl::DownloadCancelled() {
}
int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) {
gfx::ScopedNSGraphicsContextSaveGState scoped_state;
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext* ns_context = [NSGraphicsContext
graphicsContextWithGraphicsPort:context flipped:YES];
[NSGraphicsContext setCurrentContext:ns_context];
......@@ -184,6 +183,7 @@ int16 PluginInstallerImpl::OnDrawRect(CGContextRef context, CGRect dirty_rect) {
label_point = NSMakePoint(roundf(label_point.x), roundf(label_point.y));
[command_ drawAtPoint:label_point withAttributes:attributes];
[NSGraphicsContext restoreGraphicsState];
return 1;
}
......
......@@ -16,7 +16,6 @@
#import "third_party/mozilla/NSPasteboard+Utils.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#include "ui/gfx/size.h"
namespace ui {
......@@ -249,7 +248,6 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const {
scoped_nsobject<NSImage> image(
[[NSImage alloc] initWithPasteboard:GetPasteboard()]);
if (image.get()) {
gfx::ScopedNSGraphicsContextSaveGState scoped_state;
[image setFlipped:YES];
int width = [image size].width;
int height = [image size].height;
......@@ -264,6 +262,7 @@ SkBitmap Clipboard::ReadImage(Buffer buffer) const {
fromRect:NSZeroRect
operation:NSCompositeCopy
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
return canvas.ExtractBitmap();
}
return SkBitmap();
......
......@@ -4,27 +4,23 @@
#ifndef 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 "base/basictypes.h"
#include "base/memory/scoped_nsobject.h"
#if defined(__OBJC__)
@class NSGraphicsContext;
#else
class NSGraphicsContext;
#endif
namespace gfx {
// A class to save/restore the state of the current context.
class UI_API ScopedNSGraphicsContextSaveGState {
public:
ScopedNSGraphicsContextSaveGState();
// If |context| is nil, it will use the |+currentContext|.
explicit ScopedNSGraphicsContextSaveGState(NSGraphicsContext* context = nil);
~ScopedNSGraphicsContextSaveGState();
private:
NSGraphicsContext* context_; // weak
scoped_nsobject<NSGraphicsContext> context_;
DISALLOW_COPY_AND_ASSIGN(ScopedNSGraphicsContextSaveGState);
};
......
......@@ -4,20 +4,19 @@
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
#import <AppKit/AppKit.h>
#include "base/logging.h"
#include <AppKit/AppKit.h>
namespace gfx {
ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState()
: context_([NSGraphicsContext currentContext]) {
[NSGraphicsContext saveGraphicsState];
ScopedNSGraphicsContextSaveGState::ScopedNSGraphicsContextSaveGState(
NSGraphicsContext* context) : context_([context retain]) {
if (!context_)
context_.reset([[NSGraphicsContext currentContext] retain]);
[context_ saveGraphicsState];
}
ScopedNSGraphicsContextSaveGState::~ScopedNSGraphicsContextSaveGState() {
[NSGraphicsContext restoreGraphicsState];
DCHECK_EQ(context_, [NSGraphicsContext currentContext]);
[context_ restoreGraphicsState];
}
} // 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