1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
// 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.
#include "ui/gfx/canvas_paint_mac.h"
#include "ui/gfx/geometry/size.h"
namespace gfx {
CanvasSkiaPaint::CanvasSkiaPaint(NSRect dirtyRect)
: context_(NULL),
rectangle_(dirtyRect),
composite_alpha_(false) {
Init(true);
}
CanvasSkiaPaint::CanvasSkiaPaint(NSRect dirtyRect, bool opaque)
: context_(NULL),
rectangle_(dirtyRect),
composite_alpha_(false) {
Init(opaque);
}
CanvasSkiaPaint::~CanvasSkiaPaint() {
if (!is_empty()) {
platform_canvas()->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);
}
}
void CanvasSkiaPaint::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;
gfx::Size size(NSWidth(rectangle_), NSHeight(rectangle_));
RecreateBackingCanvas(size, scale, opaque);
skia::PlatformCanvas* canvas = platform_canvas();
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_ = skia::GetBitmapContext(skia::GetTopDevice(*canvas));
}
} // namespace skia
|