diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-17 18:05:42 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-17 18:05:42 +0000 |
commit | cc854e87cd67d488da2dc066ad743a12c2b108c6 (patch) | |
tree | 6dd39a9a7b1d71ffed07842d5247e5ece297277d /skia/ext/canvas_paint_mac.h | |
parent | 9cd458d48d4ed44370505d533e8393f01089d0f2 (diff) | |
download | chromium_src-cc854e87cd67d488da2dc066ad743a12c2b108c6.zip chromium_src-cc854e87cd67d488da2dc066ad743a12c2b108c6.tar.gz chromium_src-cc854e87cd67d488da2dc066ad743a12c2b108c6.tar.bz2 |
A canvas_paint for OS X.
Makes it possible to use skia to paint something into a bitmap and then blit that bitmap to screen. I need this so that I can call download_util::PaintDownloadProgress(), which needs an SkCanvas.
To be used like this:
{
gfx::CanvasPaint canvas(myRect);
canvas.set_composite_alpha(true);
download_util::PaintDownloadprogres(&canvas, ...);
}
BUG=18902
TEST=none
Review URL: http://codereview.chromium.org/172046
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23551 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/canvas_paint_mac.h')
-rw-r--r-- | skia/ext/canvas_paint_mac.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/skia/ext/canvas_paint_mac.h b/skia/ext/canvas_paint_mac.h new file mode 100644 index 0000000..9f4ae84 --- /dev/null +++ b/skia/ext/canvas_paint_mac.h @@ -0,0 +1,104 @@ + +// Copyright (c) 2009 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/platform_canvas.h" + +#import <Cocoa/Cocoa.h> + +namespace skia { + +// A class designed to translate skia painting into a region to the current +// graphics context. This class has been adapted from the class with the same +// name in platform_canvas_win.h. On construction, it will set up a context for +// painting into, and on destruction, it will commit it to the current context. +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()) { + T::restoreToCount(1); + + // Blit the dirty rect to the current context. + CGImageRef image = CGBitmapContextCreateImage(context_); + CGRect destRect = NSRectToCGRect(rectangle_); + + CGContextRef context = + (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; + CGContextSaveGState(context); + CGContextSetBlendMode( + context, composite_alpha_ ? kCGBlendModeNormal : kCGBlendModeCopy); + CGContextDrawImage(context, destRect, image); + CGContextRestoreGState(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 rectangle_.size.width == 0 || rectangle_.size.height == 0; + } + + const NSRect& rectangle() const { + return rectangle_; + } + + private: + void init(bool opaque) { + if (!T::initialize(rectangle_.size.width, rectangle_.size.height, + opaque, NULL)) { + // Cause a deliberate crash; + *(char*) 0 = 0; + } + + // Need to translate so that the dirty region appears at the origin of the + // surface. + T::translate(-SkIntToScalar(rectangle_.origin.x), + -SkIntToScalar(rectangle_.origin.y)); + + context_ = T::getTopPlatformDevice().GetBitmapContext(); + } + + 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_ |