Make launcherSearchProvider API white-listed on stable and public on dev.
[chromium-blink-merge.git] / ui / gfx / blit.cc
blobce82e42cf12780ad9cbaf33a5c717ccfa14c80f5
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 BlitContextToCanvas(SkCanvas *dst_canvas,
114 const Rect& dst_rect,
115 NativeDrawingContext src_context,
116 const Point& src_origin) {
117 DCHECK(skia::SupportsPlatformPaint(dst_canvas));
118 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas), dst_rect,
119 src_context, src_origin);
120 skia::EndPlatformPaint(dst_canvas);
123 void BlitCanvasToContext(NativeDrawingContext dst_context,
124 const Rect& dst_rect,
125 SkCanvas *src_canvas,
126 const Point& src_origin) {
127 DCHECK(skia::SupportsPlatformPaint(src_canvas));
128 BlitContextToContext(dst_context, dst_rect,
129 skia::BeginPlatformPaint(src_canvas), src_origin);
130 skia::EndPlatformPaint(src_canvas);
133 void BlitCanvasToCanvas(SkCanvas *dst_canvas,
134 const Rect& dst_rect,
135 SkCanvas *src_canvas,
136 const Point& src_origin) {
137 DCHECK(skia::SupportsPlatformPaint(dst_canvas));
138 DCHECK(skia::SupportsPlatformPaint(src_canvas));
139 BlitContextToContext(skia::BeginPlatformPaint(dst_canvas), dst_rect,
140 skia::BeginPlatformPaint(src_canvas), src_origin);
141 skia::EndPlatformPaint(src_canvas);
142 skia::EndPlatformPaint(dst_canvas);
145 void ScrollCanvas(SkCanvas* canvas,
146 const gfx::Rect& in_clip,
147 const gfx::Vector2d& offset) {
148 DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff.
149 #if defined(OS_WIN)
150 // If we have a PlatformCanvas, we should use ScrollDC. Otherwise, fall
151 // through to the software implementation.
152 if (skia::SupportsPlatformPaint(canvas)) {
153 skia::ScopedPlatformPaint scoped_platform_paint(canvas);
154 HDC hdc = scoped_platform_paint.GetPlatformSurface();
156 RECT damaged_rect;
157 RECT r = in_clip.ToRECT();
158 ScrollDC(hdc, offset.x(), offset.y(), NULL, &r, NULL, &damaged_rect);
159 return;
161 #endif // defined(OS_WIN)
162 // For non-windows, always do scrolling in software.
163 // Cairo has no nice scroll function so we do our own. On Mac it's possible to
164 // use platform scroll code, but it's complex so we just use the same path
165 // here. Either way it will be software-only, so it shouldn't matter much.
166 SkBitmap& bitmap = const_cast<SkBitmap&>(
167 skia::GetTopDevice(*canvas)->accessBitmap(true));
168 SkAutoLockPixels lock(bitmap);
170 // We expect all coords to be inside the canvas, so clip here.
171 gfx::Rect clip = gfx::IntersectRects(
172 in_clip, gfx::Rect(0, 0, bitmap.width(), bitmap.height()));
174 // Compute the set of pixels we'll actually end up painting.
175 gfx::Rect dest_rect = gfx::IntersectRects(clip + offset, clip);
176 if (dest_rect.size().IsEmpty())
177 return; // Nothing to do.
179 // Compute the source pixels that will map to the dest_rect
180 gfx::Rect src_rect = dest_rect - offset;
182 size_t row_bytes = dest_rect.width() * 4;
183 if (offset.y() > 0) {
184 // Data is moving down, copy from the bottom up.
185 for (int y = dest_rect.height() - 1; y >= 0; y--) {
186 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
187 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
188 row_bytes);
190 } else if (offset.y() < 0) {
191 // Data is moving up, copy from the top down.
192 for (int y = 0; y < dest_rect.height(); y++) {
193 memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
194 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
195 row_bytes);
197 } else if (offset.x() != 0) {
198 // Horizontal-only scroll. We can do it in either top-to-bottom or bottom-
199 // to-top, but have to be careful about the order for copying each row.
200 // Fortunately, memmove already handles this for us.
201 for (int y = 0; y < dest_rect.height(); y++) {
202 memmove(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y),
203 bitmap.getAddr32(src_rect.x(), src_rect.y() + y),
204 row_bytes);
209 } // namespace gfx