forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert 164660 - Change CanvasPaintT from a template to just a subclas…
…s of gfx::Canvas. Greatly simplifies source-code-readability, and will allow us to move the impl into a .cc file, rather than carry it to every call-site. This is also a necessary precursor to a subsequent change to remove initialize() from PlatformCanvas, and make it just a typedef for SkCanvas. Review URL: https://codereview.chromium.org/11193037 TBR=reed@google.com Review URL: https://codereview.chromium.org/11347017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@164666 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
sergeyu@chromium.org
committed
Oct 29, 2012
1 parent
2208a74
commit ac14bd6
Showing
15 changed files
with
342 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2012 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. | ||
|
||
#ifndef SKIA_EXT_CANVAS_PAINT_H_ | ||
#define SKIA_EXT_CANVAS_PAINT_H_ | ||
|
||
// This file provides an easy way to include the appropriate CanvasPaint | ||
// header file on your platform. | ||
|
||
#if defined(WIN32) | ||
#include "skia/ext/canvas_paint_win.h" | ||
#elif defined(__APPLE__) | ||
#include "skia/ext/canvas_paint_mac.h" | ||
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__sun) | ||
#if defined(TOOLKIT_GTK) | ||
#include "skia/ext/canvas_paint_gtk.h" | ||
#else | ||
#error "No canvas paint for this platform" | ||
#endif | ||
#endif | ||
|
||
#endif // SKIA_EXT_CANVAS_PAINT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2012 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. | ||
|
||
#ifndef SKIA_EXT_CANVAS_PAINT_COMMON_H_ | ||
#define SKIA_EXT_CANVAS_PAINT_COMMON_H_ | ||
|
||
namespace skia { | ||
class PlatformCanvas; | ||
|
||
template<class T> inline PlatformCanvas* GetPlatformCanvas(T* t) { | ||
return t; | ||
} | ||
|
||
// TODO(pkotwicz): Push scale into PlatformCanvas such that this function | ||
// is not needed. | ||
template<class T> inline void RecreateBackingCanvas(T* t, | ||
int width, int height, float scale, bool opaque) { | ||
} | ||
|
||
} // namespace skia | ||
|
||
#endif // SKIA_EXT_CANVAS_PAINT_COMMON_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
// Copyright (c) 2011 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. | ||
|
||
#ifndef SKIA_EXT_CANVAS_PAINT_LINUX_H_ | ||
#define SKIA_EXT_CANVAS_PAINT_LINUX_H_ | ||
|
||
#include "base/logging.h" | ||
#include "skia/ext/canvas_paint_common.h" | ||
#include "skia/ext/platform_canvas.h" | ||
|
||
#include <gdk/gdk.h> | ||
|
||
namespace skia { | ||
|
||
// A class designed to translate skia painting into a region in a GdkWindow. | ||
// On construction, it will set up a context for painting into, and on | ||
// destruction, it will commit it to the GdkWindow. | ||
// Note: The created context is always inialized to (0, 0, 0, 0). | ||
template <class T> | ||
class CanvasPaintT : public T { | ||
public: | ||
// This constructor assumes the result is opaque. | ||
explicit CanvasPaintT(GdkEventExpose* event) | ||
: context_(NULL), | ||
window_(event->window), | ||
region_(gdk_region_copy(event->region)), | ||
composite_alpha_(false) { | ||
init(true); | ||
} | ||
|
||
CanvasPaintT(GdkEventExpose* event, bool opaque) | ||
: context_(NULL), | ||
window_(event->window), | ||
region_(gdk_region_copy(event->region)), | ||
composite_alpha_(false) { | ||
init(opaque); | ||
} | ||
|
||
virtual ~CanvasPaintT() { | ||
if (!is_empty()) { | ||
GetPlatformCanvas(this)->restoreToCount(1); | ||
|
||
// Blit the dirty rect to the window. | ||
CHECK(window_); | ||
cairo_t* cr = gdk_cairo_create(window_); | ||
CHECK(cr); | ||
if (composite_alpha_) | ||
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); | ||
cairo_surface_t* source_surface = cairo_get_target(context_); | ||
CHECK(source_surface); | ||
// Flush cairo's cache of the surface. | ||
cairo_surface_mark_dirty(source_surface); | ||
GdkRectangle bounds = rectangle(); | ||
cairo_set_source_surface(cr, source_surface, bounds.x, bounds.y); | ||
gdk_cairo_region(cr, region_); | ||
cairo_fill(cr); | ||
cairo_destroy(cr); | ||
} | ||
|
||
gdk_region_destroy(region_); | ||
} | ||
|
||
// Sets whether the bitmap is composited in such a way that the alpha channel | ||
// is honored. This is only useful if you've enabled an RGBA colormap on the | ||
// widget. The default is false. | ||
void set_composite_alpha(bool composite_alpha) { | ||
composite_alpha_ = composite_alpha; | ||
} | ||
|
||
// Returns true if the invalid region is empty. The caller should call this | ||
// function to determine if anything needs painting. | ||
bool is_empty() const { | ||
return gdk_region_empty(region_); | ||
} | ||
|
||
GdkRectangle rectangle() const { | ||
GdkRectangle bounds; | ||
gdk_region_get_clipbox(region_, &bounds); | ||
return bounds; | ||
} | ||
|
||
private: | ||
void init(bool opaque) { | ||
GdkRectangle bounds = rectangle(); | ||
PlatformCanvas* canvas = GetPlatformCanvas(this); | ||
if (!canvas->initialize(bounds.width, bounds.height, opaque, NULL)) { | ||
// Cause a deliberate crash; | ||
CHECK(false); | ||
} | ||
// No need to clear the canvas, because cairo automatically performs the | ||
// clear. | ||
|
||
// Need to translate so that the dirty region appears at the origin of the | ||
// surface. | ||
canvas->translate(-SkIntToScalar(bounds.x), -SkIntToScalar(bounds.y)); | ||
|
||
context_ = BeginPlatformPaint(canvas); | ||
} | ||
|
||
cairo_t* context_; | ||
GdkWindow* window_; | ||
GdkRegion* region_; | ||
// See description above setter. | ||
bool composite_alpha_; | ||
|
||
// Disallow copy and assign. | ||
CanvasPaintT(const CanvasPaintT&); | ||
CanvasPaintT& operator=(const CanvasPaintT&); | ||
}; | ||
|
||
typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; | ||
|
||
} // namespace skia | ||
|
||
#endif // SKIA_EXT_CANVAS_PAINT_LINUX_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
|
||
// Copyright (c) 2012 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. | ||
|
||
#ifndef SKIA_EXT_CANVAS_PAINT_MAC_H_ | ||
#define SKIA_EXT_CANVAS_PAINT_MAC_H_ | ||
|
||
#include "skia/ext/canvas_paint_common.h" | ||
#include "skia/ext/platform_canvas.h" | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
namespace skia { | ||
|
||
// A class designed to translate skia painting into a region to the current | ||
// graphics context. On construction, it will set up a context for painting | ||
// into, and on destruction, it will commit it to the current context. | ||
// Note: The created context is always inialized to (0, 0, 0, 0). | ||
template <class T> | ||
class CanvasPaintT : public T { | ||
public: | ||
// This constructor assumes the result is opaque. | ||
explicit CanvasPaintT(NSRect dirtyRect) | ||
: context_(NULL), | ||
rectangle_(dirtyRect), | ||
composite_alpha_(false) { | ||
init(true); | ||
} | ||
|
||
CanvasPaintT(NSRect dirtyRect, bool opaque) | ||
: context_(NULL), | ||
rectangle_(dirtyRect), | ||
composite_alpha_(false) { | ||
init(opaque); | ||
} | ||
|
||
virtual ~CanvasPaintT() { | ||
if (!is_empty()) { | ||
GetPlatformCanvas(this)->restoreToCount(1); | ||
|
||
// Blit the dirty rect to the current context. | ||
CGImageRef image = CGBitmapContextCreateImage(context_); | ||
CGRect dest_rect = NSRectToCGRect(rectangle_); | ||
|
||
CGContextRef destination_context = | ||
(CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; | ||
CGContextSaveGState(destination_context); | ||
CGContextSetBlendMode( | ||
destination_context, | ||
composite_alpha_ ? kCGBlendModeNormal : kCGBlendModeCopy); | ||
|
||
if ([[NSGraphicsContext currentContext] isFlipped]) { | ||
// Mirror context on the target's rect middle scanline. | ||
CGContextTranslateCTM(destination_context, 0.0, NSMidY(rectangle_)); | ||
CGContextScaleCTM(destination_context, 1.0, -1.0); | ||
CGContextTranslateCTM(destination_context, 0.0, -NSMidY(rectangle_)); | ||
} | ||
|
||
CGContextDrawImage(destination_context, dest_rect, image); | ||
CGContextRestoreGState(destination_context); | ||
|
||
CFRelease(image); | ||
} | ||
} | ||
|
||
// If true, the data painted into the CanvasPaintT is blended onto the current | ||
// context, else it is copied. | ||
void set_composite_alpha(bool composite_alpha) { | ||
composite_alpha_ = composite_alpha; | ||
} | ||
|
||
// Returns true if the invalid region is empty. The caller should call this | ||
// function to determine if anything needs painting. | ||
bool is_empty() const { | ||
return NSIsEmptyRect(rectangle_); | ||
} | ||
|
||
const NSRect& rectangle() const { | ||
return rectangle_; | ||
} | ||
|
||
private: | ||
void init(bool opaque) { | ||
CGContextRef destination_context = | ||
(CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; | ||
CGRect scaled_unit_rect = CGContextConvertRectToDeviceSpace( | ||
destination_context, CGRectMake(0, 0, 1, 1)); | ||
// Assume that the x scale and the y scale are the same. | ||
CGFloat scale = scaled_unit_rect.size.width; | ||
|
||
RecreateBackingCanvas(this, | ||
NSWidth(rectangle_), NSHeight(rectangle_), scale, opaque); | ||
PlatformCanvas* canvas = GetPlatformCanvas(this); | ||
canvas->clear(SkColorSetARGB(0, 0, 0, 0)); | ||
|
||
// Need to translate so that the dirty region appears at the origin of the | ||
// surface. | ||
canvas->translate(-SkDoubleToScalar(NSMinX(rectangle_)), | ||
-SkDoubleToScalar(NSMinY(rectangle_))); | ||
|
||
context_ = GetBitmapContext(GetTopDevice(*canvas)); | ||
} | ||
|
||
CGContext* context_; | ||
NSRect rectangle_; | ||
// See description above setter. | ||
bool composite_alpha_; | ||
|
||
// Disallow copy and assign. | ||
CanvasPaintT(const CanvasPaintT&); | ||
CanvasPaintT& operator=(const CanvasPaintT&); | ||
}; | ||
|
||
typedef CanvasPaintT<PlatformCanvas> PlatformCanvasPaint; | ||
|
||
} // namespace skia | ||
|
||
|
||
#endif // SKIA_EXT_CANVAS_PAINT_MAC_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.