Bug 60757

Summary: [CAIRO] Don't copy the surface before drawing it into the context in ShareableBitmap::paint()
Product: WebKit Reporter: Carlos Garcia Campos <cgarcia>
Component: WebKit2Assignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: mrobinson
Priority: P2 Keywords: Cairo, Gtk
Version: 528+ (Nightly build)   
Hardware: PC   
OS: Linux   
Attachments:
Description Flags
Patch mrobinson: review+

Description Carlos Garcia Campos 2011-05-13 04:55:24 PDT
cairo_image_surface_create_for_data() is already a shallow copy of the image, so we don't need to copy it again.
Comment 1 Carlos Garcia Campos 2011-05-13 04:58:47 PDT
Created attachment 93434 [details]
Patch
Comment 2 Martin Robinson 2011-05-13 08:38:22 PDT
(In reply to comment #0)
> cairo_image_surface_create_for_data() is already a shallow copy of the image, so we don't need to copy it again.

Notice that in ShareableBitmapCG.cpp (the CG version of this file), the code does a copy of the image. Perhaps that this is just to support self copies though. If that's the case, it would be better to support self-copies by detecting when the target of the context is the same as the ShareableBitmap. Please contect the Apple team to see what the correct behavior is here.
Comment 3 Martin Robinson 2011-05-13 08:38:48 PDT
Comment on attachment 93434 [details]
Patch

Removing review flag until we get some input from the authors of the CG version of ShareableBitmap.
Comment 4 Carlos Garcia Campos 2011-05-13 11:58:06 PDT
(In reply to comment #2)
> (In reply to comment #0)
> > cairo_image_surface_create_for_data() is already a shallow copy of the image, so we don't need to copy it again.
> 
> Notice that in ShareableBitmapCG.cpp (the CG version of this file), the code does a copy of the image. Perhaps that this is just to support self copies though. If that's the case, it would be better to support self-copies by detecting when the target of the context is the same as the ShareableBitmap. Please contect the Apple team to see what the correct behavior is here.

But we are creating a new surface and copying that new surface, CG copies the already existing image, I think, which is the same than simply create a new surface with the image data.
Comment 5 Martin Robinson 2011-05-13 12:19:43 PDT
(In reply to comment #4)
> > Notice that in ShareableBitmapCG.cpp (the CG version of this file), the code does a copy of the image. Perhaps that this is just to support self copies though. If that's the case, it would be better to support self-copies by detecting when the target of the context is the same as the ShareableBitmap. Please contect the Apple team to see what the correct behavior is here.
> 
> But we are creating a new surface and copying that new surface, CG copies the already existing image, I think, which is the same than simply create a new surface with the image data.

The documentation for CGBitmapContextCreateImage states: 

The CGImage object returned by this function is created by a copy operation. Subsequent changes to the bitmap graphics context do not affect the contents of the returned image. In some cases the copy operation actually follows copy-on-write semantics, so that the actual physical copy of the bits occur only if the underlying data in the bitmap graphics context is modified. As a consequence, you may want to use the resulting image and release it before you perform additional drawing into the bitmap graphics context. In this way, you can avoid the actual physical copy of the data.

In the case of Cairo, unless you explicitly create a copy of the original surface, all surfaces will be backed by the same block of data. Thus it seems the semantics of the blit could deviate if the ShareableBitmap is painting onto itself.

In my opinion, we should check with the authors of the CG version. If the bitmap will never do a self-paint we can make your optimization, otherwise we can optimize only when the backing surface of the GraphicsContext does not contain the same bytes as the ShareableBitmap.
Comment 6 Carlos Garcia Campos 2011-05-27 09:14:56 PDT
Committed r87517: <http://trac.webkit.org/changeset/87517>