[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gfx / blit.cc
blob81984c802638a6fe0155dcf20272af42c1acea3b
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 #if defined(OS_IOS)
24 #include <CoreGraphics/CoreGraphics.h>
25 #else
26 #include <ApplicationServices/ApplicationServices.h>
27 #endif
29 #include "base/mac/scoped_cftyperef.h"
30 #endif
32 namespace gfx {
34 namespace {
36 // Returns true if the given canvas has any part of itself clipped out or
37 // any non-identity tranform.
38 bool HasClipOrTransform(SkCanvas& canvas) {
39 if (!canvas.getTotalMatrix().isIdentity())
40 return true;
42 if (!canvas.isClipRect())
43 return true;
45 // Now we know the clip is a regular rectangle, make sure it covers the
46 // entire canvas.
47 SkIRect clip_bounds;
48 canvas.getClipDeviceBounds(&clip_bounds);
50 SkImageInfo info;
51 size_t row_bytes;
52 void* pixels = canvas.accessTopLayerPixels(&info, &row_bytes);
53 DCHECK(pixels);
54 if (!pixels)
55 return true;
57 if (clip_bounds.fLeft != 0 || clip_bounds.fTop != 0 ||
58 clip_bounds.fRight != info.width() ||
59 clip_bounds.fBottom != info.height())
60 return true;
62 return false;
65 } // namespace
67 void BlitContextToContext(NativeDrawingContext dst_context,
68 const Rect& dst_rect,
69 NativeDrawingContext src_context,
70 const Point& src_origin) {
71 #if defined(OS_WIN)
72 BitBlt(dst_context, dst_rect.x(), dst_rect.y(),
73 dst_rect.width(), dst_rect.height(),
74 src_context, src_origin.x(), src_origin.y(), SRCCOPY);
75 #elif defined(OS_MACOSX)
76 // Only translations and/or vertical flips in the source context are
77 // supported; more complex source context transforms will be ignored.
79 // If there is a translation on the source context, we need to account for
80 // it ourselves since CGBitmapContextCreateImage will bypass it.
81 Rect src_rect(src_origin, dst_rect.size());
82 CGAffineTransform transform = CGContextGetCTM(src_context);
83 bool flipped = fabs(transform.d + 1) < 0.0001;
84 CGFloat delta_y = flipped ? CGBitmapContextGetHeight(src_context) -
85 transform.ty
86 : transform.ty;
87 src_rect.Offset(transform.tx, delta_y);
89 base::ScopedCFTypeRef<CGImageRef> src_image(
90 CGBitmapContextCreateImage(src_context));
91 base::ScopedCFTypeRef<CGImageRef> src_sub_image(
92 CGImageCreateWithImageInRect(src_image, src_rect.ToCGRect()));
93 CGContextDrawImage(dst_context, dst_rect.ToCGRect(), src_sub_image);
94 #elif defined(USE_CAIRO)
95 // Only translations in the source context are supported; more complex
96 // source context transforms will be ignored.
97 cairo_save(dst_context);
98 double surface_x = src_origin.x();
99 double surface_y = src_origin.y();
100 cairo_user_to_device(src_context, &surface_x, &surface_y);
101 cairo_set_source_surface(dst_context, cairo_get_target(src_context),
102 dst_rect.x()-surface_x, dst_rect.y()-surface_y);
103 cairo_rectangle(dst_context, dst_rect.x(), dst_rect.y(),
104 dst_rect.width(), dst_rect.height());
105 cairo_clip(dst_context);
106 cairo_paint(dst_context);
107 cairo_restore(dst_context);
108 #else
109 NOTIMPLEMENTED();
110 #endif
113 void BlitCanvasToCanvas(SkCanvas *dst_canvas,
114 const Rect& dst_rect,
115 SkCanvas *src_canvas,
116 const Point& src_origin) {
117 DCHECK(skia::SupportsPlatformPaint(dst_canvas));
118 DCHECK(skia::SupportsPlatformPaint(src_canvas));
119 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas), dst_rect,
120 skia::BeginPlatformPaint(src_canvas), src_origin);
121 skia::EndPlatformPaint(src_canvas);
122 skia::EndPlatformPaint(dst_canvas);
125 void ScrollCanvas(SkCanvas* canvas,
126 const gfx::Rect& in_clip,
127 const gfx::Vector2d& offset) {
128 DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
129 #if defined(OS_WIN)
130 // If we have a PlatformCanvas, we should use ScrollDC. Otherwise, fall
131 // through to the software implementation.
132 if (skia::SupportsPlatformPaint(canvas)) {
133 skia::ScopedPlatformPaint scoped_platform_paint(canvas);
134 HDC hdc = scoped_platform_paint.GetPlatformSurface();
136 RECT damaged_rect;
137 RECT r = in_clip.ToRECT();
138 ScrollDC(hdc, offset.x(), offset.y(), NULL, &r, NULL, &damaged_rect);
139 return;
141 #endif // defined(OS_WIN)
142 // For non-windows, always do scrolling in software.
143 // Cairo has no nice scroll function so we do our own. On Mac it's possible to
144 // use platform scroll code, but it's complex so we just use the same path
145 // here. Either way it will be software-only, so it shouldn't matter much.
146 SkBitmap& bitmap = const_cast<SkBitmap&>(
147 skia::GetTopDevice(*canvas)->accessBitmap(true));
148 SkAutoLockPixels lock(bitmap);
150 // We expect all coords to be inside the canvas, so clip here.
151 gfx::Rect clip = gfx::IntersectRects(
152 in_clip, gfx::Rect(0, 0, bitmap.width(), bitmap.height()));
154 // Compute the set of pixels we'll actually end up painting.
155 gfx::Rect dest_rect = gfx::IntersectRects(clip + offset, clip);
156 if (dest_rect.size().IsEmpty())
157 return; // Nothing to do.
159 // Compute the source pixels that will map to the dest_rect
160 gfx::Rect src_rect = dest_rect - offset;
162 size_t row_bytes = dest_rect.width() * 4;
163 if (offset.y() > 0) {
164 // Data is moving down, copy from the bottom up.
165 for (int y = dest_rect.height() - 1; y >= 0; y--) {
166 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
167 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
168 row_bytes);
170 } else if (offset.y() < 0) {
171 // Data is moving up, copy from the top down.
172 for (int y = 0; y < dest_rect.height(); y++) {
173 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
174 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
175 row_bytes);
177 } else if (offset.x() != 0) {
178 // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
179 // to-top, but have to be careful about the order for copying each row.
180 // Fortunately, memmove already handles this for us.
181 for (int y = 0; y < dest_rect.height(); y++) {
182 memmove(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
183 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
184 row_bytes);
189 } // namespace gfx