1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/blit.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
9 #include "skia/ext/platform_canvas.h"
10 #include "ui/gfx/point.h"
11 #include "ui/gfx/rect.h"
12 #include "ui/gfx/vector2d.h"
14 #if defined(USE_CAIRO)
15 #if defined(OS_OPENBSD)
17 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
18 #include <cairo/cairo.h>
22 #if defined(OS_MACOSX)
23 #include "base/mac/scoped_cftyperef.h"
30 // Returns true if the given canvas has any part of itself clipped out or
31 // any non-identity tranform.
32 bool HasClipOrTransform(const SkCanvas
& canvas
) {
33 if (!canvas
.getTotalMatrix().isIdentity())
36 const SkRegion
& clip_region
= canvas
.getTotalClip();
37 if (clip_region
.isEmpty() || clip_region
.isComplex())
40 // Now we know the clip is a regular rectangle, make sure it covers the
42 const SkBitmap
& bitmap
= skia::GetTopDevice(canvas
)->accessBitmap(false);
43 const SkIRect
& clip_bounds
= clip_region
.getBounds();
44 if (clip_bounds
.fLeft
!= 0 || clip_bounds
.fTop
!= 0 ||
45 clip_bounds
.fRight
!= bitmap
.width() ||
46 clip_bounds
.fBottom
!= bitmap
.height())
54 void BlitContextToContext(NativeDrawingContext dst_context
,
56 NativeDrawingContext src_context
,
57 const Point
& src_origin
) {
59 BitBlt(dst_context
, dst_rect
.x(), dst_rect
.y(),
60 dst_rect
.width(), dst_rect
.height(),
61 src_context
, src_origin
.x(), src_origin
.y(), SRCCOPY
);
62 #elif defined(OS_MACOSX)
63 // Only translations and/or vertical flips in the source context are
64 // supported; more complex source context transforms will be ignored.
66 // If there is a translation on the source context, we need to account for
67 // it ourselves since CGBitmapContextCreateImage will bypass it.
68 Rect
src_rect(src_origin
, dst_rect
.size());
69 CGAffineTransform transform
= CGContextGetCTM(src_context
);
70 bool flipped
= fabs(transform
.d
+ 1) < 0.0001;
71 CGFloat delta_y
= flipped
? CGBitmapContextGetHeight(src_context
) -
74 src_rect
.Offset(transform
.tx
, delta_y
);
76 base::ScopedCFTypeRef
<CGImageRef
> src_image(
77 CGBitmapContextCreateImage(src_context
));
78 base::ScopedCFTypeRef
<CGImageRef
> src_sub_image(
79 CGImageCreateWithImageInRect(src_image
, src_rect
.ToCGRect()));
80 CGContextDrawImage(dst_context
, dst_rect
.ToCGRect(), src_sub_image
);
81 #elif defined(USE_CAIRO)
82 // Only translations in the source context are supported; more complex
83 // source context transforms will be ignored.
84 cairo_save(dst_context
);
85 double surface_x
= src_origin
.x();
86 double surface_y
= src_origin
.y();
87 cairo_user_to_device(src_context
, &surface_x
, &surface_y
);
88 cairo_set_source_surface(dst_context
, cairo_get_target(src_context
),
89 dst_rect
.x()-surface_x
, dst_rect
.y()-surface_y
);
90 cairo_rectangle(dst_context
, dst_rect
.x(), dst_rect
.y(),
91 dst_rect
.width(), dst_rect
.height());
92 cairo_clip(dst_context
);
93 cairo_paint(dst_context
);
94 cairo_restore(dst_context
);
100 void BlitContextToCanvas(SkCanvas
*dst_canvas
,
101 const Rect
& dst_rect
,
102 NativeDrawingContext src_context
,
103 const Point
& src_origin
) {
104 DCHECK(skia::SupportsPlatformPaint(dst_canvas
));
105 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas
), dst_rect
,
106 src_context
, src_origin
);
107 skia::EndPlatformPaint(dst_canvas
);
110 void BlitCanvasToContext(NativeDrawingContext dst_context
,
111 const Rect
& dst_rect
,
112 SkCanvas
*src_canvas
,
113 const Point
& src_origin
) {
114 DCHECK(skia::SupportsPlatformPaint(src_canvas
));
115 BlitContextToContext(dst_context
, dst_rect
,
116 skia::BeginPlatformPaint(src_canvas
), src_origin
);
117 skia::EndPlatformPaint(src_canvas
);
120 void BlitCanvasToCanvas(SkCanvas
*dst_canvas
,
121 const Rect
& dst_rect
,
122 SkCanvas
*src_canvas
,
123 const Point
& src_origin
) {
124 DCHECK(skia::SupportsPlatformPaint(dst_canvas
));
125 DCHECK(skia::SupportsPlatformPaint(src_canvas
));
126 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas
), dst_rect
,
127 skia::BeginPlatformPaint(src_canvas
), src_origin
);
128 skia::EndPlatformPaint(src_canvas
);
129 skia::EndPlatformPaint(dst_canvas
);
132 void ScrollCanvas(SkCanvas
* canvas
,
133 const gfx::Rect
& in_clip
,
134 const gfx::Vector2d
& offset
) {
135 DCHECK(!HasClipOrTransform(*canvas
)); // Don't support special stuff.
137 // If we have a PlatformCanvas, we should use ScrollDC. Otherwise, fall
138 // through to the software implementation.
139 if (skia::SupportsPlatformPaint(canvas
)) {
140 skia::ScopedPlatformPaint
scoped_platform_paint(canvas
);
141 HDC hdc
= scoped_platform_paint
.GetPlatformSurface();
144 RECT r
= in_clip
.ToRECT();
145 ScrollDC(hdc
, offset
.x(), offset
.y(), NULL
, &r
, NULL
, &damaged_rect
);
148 #endif // defined(OS_WIN)
149 // For non-windows, always do scrolling in software.
150 // Cairo has no nice scroll function so we do our own. On Mac it's possible to
151 // use platform scroll code, but it's complex so we just use the same path
152 // here. Either way it will be software-only, so it shouldn't matter much.
153 SkBitmap
& bitmap
= const_cast<SkBitmap
&>(
154 skia::GetTopDevice(*canvas
)->accessBitmap(true));
155 SkAutoLockPixels
lock(bitmap
);
157 // We expect all coords to be inside the canvas, so clip here.
158 gfx::Rect clip
= gfx::IntersectRects(
159 in_clip
, gfx::Rect(0, 0, bitmap
.width(), bitmap
.height()));
161 // Compute the set of pixels we'll actually end up painting.
162 gfx::Rect dest_rect
= gfx::IntersectRects(clip
+ offset
, clip
);
163 if (dest_rect
.size().IsEmpty())
164 return; // Nothing to do.
166 // Compute the source pixels that will map to the dest_rect
167 gfx::Rect src_rect
= dest_rect
- offset
;
169 size_t row_bytes
= dest_rect
.width() * 4;
170 if (offset
.y() > 0) {
171 // Data is moving down, copy from the bottom up.
172 for (int y
= dest_rect
.height() - 1; y
>= 0; y
--) {
173 memcpy(bitmap
.getAddr32(dest_rect
.x(), dest_rect
.y() + y
),
174 bitmap
.getAddr32(src_rect
.x(), src_rect
.y() + y
),
177 } else if (offset
.y() < 0) {
178 // Data is moving up, copy from the top down.
179 for (int y
= 0; y
< dest_rect
.height(); y
++) {
180 memcpy(bitmap
.getAddr32(dest_rect
.x(), dest_rect
.y() + y
),
181 bitmap
.getAddr32(src_rect
.x(), src_rect
.y() + y
),
184 } else if (offset
.x() != 0) {
185 // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
186 // to-top, but have to be careful about the order for copying each row.
187 // Fortunately, memmove already handles this for us.
188 for (int y
= 0; y
< dest_rect
.height(); y
++) {
189 memmove(bitmap
.getAddr32(dest_rect
.x(), dest_rect
.y() + y
),
190 bitmap
.getAddr32(src_rect
.x(), src_rect
.y() + y
),