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