mac: Add the flag "-gline-tables-only" to reduce dSYM size. (attempt #2)
[chromium-blink-merge.git] / ui / gfx / blit.cc
blob065c51ec18d0b8f02f206b1ea5ded7525e039d4e
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/geometry/point.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/geometry/vector2d.h"
14 #if defined(USE_CAIRO)
15 #if defined(OS_OPENBSD)
16 #include <cairo.h>
17 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
18 #include <cairo/cairo.h>
19 #endif
20 #endif
22 #if defined(OS_MACOSX)
23 #include "base/mac/scoped_cftyperef.h"
24 #endif
26 namespace gfx {
28 namespace {
30 // Returns true if the given canvas has any part of itself clipped out or
31 // any non-identity tranform.
32 bool HasClipOrTransform(SkCanvas& canvas) {
33 if (!canvas.getTotalMatrix().isIdentity())
34 return true;
36 if (!canvas.isClipRect())
37 return true;
39 // Now we know the clip is a regular rectangle, make sure it covers the
40 // entire canvas.
41 SkIRect clip_bounds;
42 canvas.getClipDeviceBounds(&clip_bounds);
44 SkImageInfo info;
45 size_t row_bytes;
46 void* pixels = canvas.accessTopLayerPixels(&info, &row_bytes);
47 DCHECK(pixels);
48 if (!pixels)
49 return true;
51 if (clip_bounds.fLeft != 0 || clip_bounds.fTop != 0 ||
52 clip_bounds.fRight != info.width() ||
53 clip_bounds.fBottom != info.height())
54 return true;
56 return false;
59 } // namespace
61 void BlitContextToContext(NativeDrawingContext dst_context,
62 const Rect& dst_rect,
63 NativeDrawingContext src_context,
64 const Point& src_origin) {
65 #if defined(OS_WIN)
66 BitBlt(dst_context, dst_rect.x(), dst_rect.y(),
67 dst_rect.width(), dst_rect.height(),
68 src_context, src_origin.x(), src_origin.y(), SRCCOPY);
69 #elif defined(OS_MACOSX)
70 // Only translations and/or vertical flips in the source context are
71 // supported; more complex source context transforms will be ignored.
73 // If there is a translation on the source context, we need to account for
74 // it ourselves since CGBitmapContextCreateImage will bypass it.
75 Rect src_rect(src_origin, dst_rect.size());
76 CGAffineTransform transform = CGContextGetCTM(src_context);
77 bool flipped = fabs(transform.d + 1) < 0.0001;
78 CGFloat delta_y = flipped ? CGBitmapContextGetHeight(src_context) -
79 transform.ty
80 : transform.ty;
81 src_rect.Offset(transform.tx, delta_y);
83 base::ScopedCFTypeRef<CGImageRef> src_image(
84 CGBitmapContextCreateImage(src_context));
85 base::ScopedCFTypeRef<CGImageRef> src_sub_image(
86 CGImageCreateWithImageInRect(src_image, src_rect.ToCGRect()));
87 CGContextDrawImage(dst_context, dst_rect.ToCGRect(), src_sub_image);
88 #elif defined(USE_CAIRO)
89 // Only translations in the source context are supported; more complex
90 // source context transforms will be ignored.
91 cairo_save(dst_context);
92 double surface_x = src_origin.x();
93 double surface_y = src_origin.y();
94 cairo_user_to_device(src_context, &surface_x, &surface_y);
95 cairo_set_source_surface(dst_context, cairo_get_target(src_context),
96 dst_rect.x()-surface_x, dst_rect.y()-surface_y);
97 cairo_rectangle(dst_context, dst_rect.x(), dst_rect.y(),
98 dst_rect.width(), dst_rect.height());
99 cairo_clip(dst_context);
100 cairo_paint(dst_context);
101 cairo_restore(dst_context);
102 #else
103 NOTIMPLEMENTED();
104 #endif
107 void BlitContextToCanvas(SkCanvas *dst_canvas,
108 const Rect& dst_rect,
109 NativeDrawingContext src_context,
110 const Point& src_origin) {
111 DCHECK(skia::SupportsPlatformPaint(dst_canvas));
112 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas), dst_rect,
113 src_context, src_origin);
114 skia::EndPlatformPaint(dst_canvas);
117 void BlitCanvasToContext(NativeDrawingContext dst_context,
118 const Rect& dst_rect,
119 SkCanvas *src_canvas,
120 const Point& src_origin) {
121 DCHECK(skia::SupportsPlatformPaint(src_canvas));
122 BlitContextToContext(dst_context, dst_rect,
123 skia::BeginPlatformPaint(src_canvas), src_origin);
124 skia::EndPlatformPaint(src_canvas);
127 void BlitCanvasToCanvas(SkCanvas *dst_canvas,
128 const Rect& dst_rect,
129 SkCanvas *src_canvas,
130 const Point& src_origin) {
131 DCHECK(skia::SupportsPlatformPaint(dst_canvas));
132 DCHECK(skia::SupportsPlatformPaint(src_canvas));
133 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas), dst_rect,
134 skia::BeginPlatformPaint(src_canvas), src_origin);
135 skia::EndPlatformPaint(src_canvas);
136 skia::EndPlatformPaint(dst_canvas);
139 void ScrollCanvas(SkCanvas* canvas,
140 const gfx::Rect& in_clip,
141 const gfx::Vector2d& offset) {
142 DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
143 #if defined(OS_WIN)
144 // If we have a PlatformCanvas, we should use ScrollDC. Otherwise, fall
145 // through to the software implementation.
146 if (skia::SupportsPlatformPaint(canvas)) {
147 skia::ScopedPlatformPaint scoped_platform_paint(canvas);
148 HDC hdc = scoped_platform_paint.GetPlatformSurface();
150 RECT damaged_rect;
151 RECT r = in_clip.ToRECT();
152 ScrollDC(hdc, offset.x(), offset.y(), NULL, &r, NULL, &damaged_rect);
153 return;
155 #endif // defined(OS_WIN)
156 // For non-windows, always do scrolling in software.
157 // Cairo has no nice scroll function so we do our own. On Mac it's possible to
158 // use platform scroll code, but it's complex so we just use the same path
159 // here. Either way it will be software-only, so it shouldn't matter much.
160 SkBitmap& bitmap = const_cast<SkBitmap&>(
161 skia::GetTopDevice(*canvas)->accessBitmap(true));
162 SkAutoLockPixels lock(bitmap);
164 // We expect all coords to be inside the canvas, so clip here.
165 gfx::Rect clip = gfx::IntersectRects(
166 in_clip, gfx::Rect(0, 0, bitmap.width(), bitmap.height()));
168 // Compute the set of pixels we'll actually end up painting.
169 gfx::Rect dest_rect = gfx::IntersectRects(clip + offset, clip);
170 if (dest_rect.size().IsEmpty())
171 return; // Nothing to do.
173 // Compute the source pixels that will map to the dest_rect
174 gfx::Rect src_rect = dest_rect - offset;
176 size_t row_bytes = dest_rect.width() * 4;
177 if (offset.y() > 0) {
178 // Data is moving down, copy from the bottom up.
179 for (int y = dest_rect.height() - 1; y >= 0; y--) {
180 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
181 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
182 row_bytes);
184 } else if (offset.y() < 0) {
185 // Data is moving up, copy from the top down.
186 for (int y = 0; y < dest_rect.height(); y++) {
187 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
188 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
189 row_bytes);
191 } else if (offset.x() != 0) {
192 // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
193 // to-top, but have to be careful about the order for copying each row.
194 // Fortunately, memmove already handles this for us.
195 for (int y = 0; y < dest_rect.height(); y++) {
196 memmove(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
197 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
198 row_bytes);
203 } // namespace gfx