+
+ /* If we're copying from a bitmap to a bitmap, and there's nothing funny
+ going on with clipping masks or depths or anything, optimize it by
+ just doing a memcpy instead of going through a CGI.
+ */
+ if (bitmap_context_p (src)) {
+
+ if (bitmap_context_p (dst) &&
+ gc->gcv.function == GXcopy &&
+ !gc->gcv.clip_mask &&
+ drawable_depth (src) == drawable_depth (dst)) {
+
+ Assert(!(int)src_frame.origin.x &&
+ !(int)src_frame.origin.y &&
+ !(int)dst_frame.origin.x &&
+ !(int)dst_frame.origin.y,
+ "unexpected non-zero origin");
+
+ char *src_data = CGBitmapContextGetData(src->cgc);
+ char *dst_data = CGBitmapContextGetData(dst->cgc);
+ size_t src_pitch = CGBitmapContextGetBytesPerRow(src->cgc);
+ size_t dst_pitch = CGBitmapContextGetBytesPerRow(dst->cgc);
+
+ // Int to float and back again. It's not very safe, but it seems to work.
+ int src_x0 = src_rect.origin.x;
+ int dst_x0 = dst_rect.origin.x;
+
+ // Flip the Y-axis a second time.
+ int src_y0 = (src_frame.origin.y + src_frame.size.height -
+ src_rect.size.height - src_rect.origin.y);
+ int dst_y0 = (dst_frame.origin.y + dst_frame.size.height -
+ dst_rect.size.height - dst_rect.origin.y);
+
+ unsigned width0 = (int) src_rect.size.width;
+ unsigned height0 = (int) src_rect.size.height;
+
+ Assert((int)src_rect.size.width == (int)dst_rect.size.width ||
+ (int)src_rect.size.height == (int)dst_rect.size.height,
+ "size mismatch");
+ {
+ char *src_data0 = seek_xy(src_data, src_pitch, src_x0, src_y0);
+ char *dst_data0 = seek_xy(dst_data, dst_pitch, dst_x0, dst_y0);
+ size_t src_pitch0 = src_pitch;
+ size_t dst_pitch0 = dst_pitch;
+ size_t bytes = width0 * 4;
+
+ if (src == dst && dst_y0 > src_y0) {
+ // Copy upwards if the areas might overlap.
+ src_data0 += src_pitch0 * (height0 - 1);
+ dst_data0 += dst_pitch0 * (height0 - 1);
+ src_pitch0 = -src_pitch0;
+ dst_pitch0 = -dst_pitch0;
+ }
+
+ size_t lines0 = height0;
+ while (lines0) {
+ // memcpy is an alias for memmove on OS X.
+ memmove(dst_data0, src_data0, bytes);
+ src_data0 += src_pitch0;
+ dst_data0 += dst_pitch0;
+ --lines0;
+ }
+ }
+
+ if (clipped) {
+ int orig_dst_x = orig_dst_rect.origin.x;
+ int orig_dst_y = (dst_frame.origin.y + dst_frame.size.height -
+ orig_dst_rect.origin.y - orig_dst_rect.size.height);
+ int orig_width = orig_dst_rect.size.width;
+ int orig_height = orig_dst_rect.size.height;
+
+ Assert (orig_dst_x >= 0 &&
+ orig_dst_x + orig_width <= (int) dst_frame.size.width &&
+ orig_dst_y >= 0 &&
+ orig_dst_y + orig_height <= (int) dst_frame.size.height,
+ "wrong dimensions");
+
+ if (orig_dst_y < dst_y0) {
+ fill_rect_memset (seek_xy (dst_data, dst_pitch,
+ orig_dst_x, orig_dst_y), dst_pitch,
+ (uint32_t) gc->gcv.background, orig_width,
+ dst_y0 - orig_dst_y);
+ }
+
+ if (orig_dst_y + orig_height > dst_y0 + height0) {
+ fill_rect_memset (seek_xy (dst_data, dst_pitch, orig_dst_x,
+ dst_y0 + height0),
+ dst_pitch,
+ (uint32_t) gc->gcv.background, orig_width,
+ orig_dst_y + orig_height - dst_y0 - height0);
+ }
+
+ if (orig_dst_x < dst_x0) {
+ fill_rect_memset (seek_xy (dst_data, dst_pitch, orig_dst_x, dst_y0),
+ dst_pitch, (uint32_t) gc->gcv.background,
+ dst_x0 - orig_dst_x, height0);
+ }
+
+ if (dst_x0 + width0 < orig_dst_x + orig_width) {
+ fill_rect_memset (seek_xy (dst_data, dst_pitch, dst_x0 + width0,
+ dst_y0),
+ dst_pitch, (uint32_t) gc->gcv.background,
+ orig_dst_x + orig_width - dst_x0 - width0,
+ height0);
+ }
+ }
+
+ invalidate_drawable_cache (dst);
+ return 0;
+ }
+