Adopt CGIOSurfaceContextCreateImageReference to avoid unnecessary readback
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 14 Dec 2015 05:27:45 +0000 (05:27 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 14 Dec 2015 05:27:45 +0000 (05:27 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150988
<rdar://problem/18993594>

Reviewed by Darin Adler.

* platform/graphics/GraphicsContext.cpp:
(WebCore::GraphicsContext::drawConsumingImageBuffer):
* platform/graphics/GraphicsContext.h:
* platform/graphics/ImageBuffer.h:
* platform/graphics/cg/ImageBufferCG.cpp:
(WebCore::createBitmapImageAfterScalingIfNeeded):
(WebCore::ImageBuffer::copyImage):
(WebCore::ImageBuffer::sinkIntoImage):
(WebCore::ImageBuffer::sinkIntoNativeImage):
(WebCore::ImageBuffer::drawConsuming):
* platform/graphics/cocoa/IOSurface.h:
* platform/graphics/cocoa/IOSurface.mm:
(IOSurface::createFromImageBuffer):
(IOSurface::sinkIntoImage):
Add sinkIntoImage, sinkIntoNativeImage, and drawConsuming to ImageBuffer,
which all consume the ImageBuffer and allow us to tell the system to
make a CGImage that references the IOSurface, which is in many cases
more efficient than making an image with a "copy" of the IOSurface.
(The copy is done lazily, but we often hit a corner case that causes
it to happen unnecessarily.)

* html/shadow/MediaControlElements.cpp:
(WebCore::MediaControlTextTrackContainerElement::createTextTrackRepresentationImage):
* page/TextIndicator.cpp:
(WebCore::takeSnapshot):
* platform/DragImage.cpp:
(WebCore::createDragImageFromSnapshot):
* platform/graphics/filters/FETile.cpp:
(WebCore::FETile::platformApplySoftware):
* platform/mac/ThemeMac.mm:
(WebCore::ThemeMac::drawCellOrFocusRingWithViewIntoContext):
* platform/mediastream/mac/AVVideoCaptureSource.mm:
(WebCore::AVVideoCaptureSource::currentFrameImage):
* rendering/RenderBoxModelObject.cpp:
(WebCore::RenderBoxModelObject::paintFillLayerExtended):
* rendering/RenderThemeMac.mm:
(WebCore::RenderThemeMac::paintProgressBar):
* rendering/svg/RenderSVGResourcePattern.cpp:
(WebCore::RenderSVGResourcePattern::buildPattern):
* svg/graphics/SVGImage.cpp:
(WebCore::SVGImage::drawPatternForContainer):
Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.

* WebCoreSupport/WebContextMenuClient.mm:
(WebContextMenuClient::imageForCurrentSharingServicePickerItem):
Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.

* WebProcess/WebPage/WebFrame.cpp:
(WebKit::WebFrame::createSelectionSnapshot):
Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@194025 268f45cc-cd09-0410-ab3c-d52691b4dbfc

22 files changed:
Source/WebCore/ChangeLog
Source/WebCore/html/shadow/MediaControlElements.cpp
Source/WebCore/page/TextIndicator.cpp
Source/WebCore/platform/DragImage.cpp
Source/WebCore/platform/graphics/GraphicsContext.cpp
Source/WebCore/platform/graphics/GraphicsContext.h
Source/WebCore/platform/graphics/ImageBuffer.h
Source/WebCore/platform/graphics/cairo/ImageBufferCairo.cpp
Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp
Source/WebCore/platform/graphics/cocoa/IOSurface.h
Source/WebCore/platform/graphics/cocoa/IOSurface.mm
Source/WebCore/platform/graphics/filters/FETile.cpp
Source/WebCore/platform/mac/ThemeMac.mm
Source/WebCore/platform/mediastream/mac/AVVideoCaptureSource.mm
Source/WebCore/rendering/RenderBoxModelObject.cpp
Source/WebCore/rendering/RenderThemeMac.mm
Source/WebCore/rendering/svg/RenderSVGResourcePattern.cpp
Source/WebCore/svg/graphics/SVGImage.cpp
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.mm
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/WebPage/WebFrame.cpp

index 79ffcef..1440c62 100644 (file)
@@ -1,3 +1,54 @@
+2015-12-13  Tim Horton  <timothy_horton@apple.com>
+
+        Adopt CGIOSurfaceContextCreateImageReference to avoid unnecessary readback
+        https://bugs.webkit.org/show_bug.cgi?id=150988
+        <rdar://problem/18993594>
+
+        Reviewed by Darin Adler.
+
+        * platform/graphics/GraphicsContext.cpp:
+        (WebCore::GraphicsContext::drawConsumingImageBuffer):
+        * platform/graphics/GraphicsContext.h:
+        * platform/graphics/ImageBuffer.h:
+        * platform/graphics/cg/ImageBufferCG.cpp:
+        (WebCore::createBitmapImageAfterScalingIfNeeded):
+        (WebCore::ImageBuffer::copyImage):
+        (WebCore::ImageBuffer::sinkIntoImage):
+        (WebCore::ImageBuffer::sinkIntoNativeImage):
+        (WebCore::ImageBuffer::drawConsuming):
+        * platform/graphics/cocoa/IOSurface.h:
+        * platform/graphics/cocoa/IOSurface.mm:
+        (IOSurface::createFromImageBuffer):
+        (IOSurface::sinkIntoImage):
+        Add sinkIntoImage, sinkIntoNativeImage, and drawConsuming to ImageBuffer,
+        which all consume the ImageBuffer and allow us to tell the system to
+        make a CGImage that references the IOSurface, which is in many cases
+        more efficient than making an image with a "copy" of the IOSurface.
+        (The copy is done lazily, but we often hit a corner case that causes
+        it to happen unnecessarily.)
+
+        * html/shadow/MediaControlElements.cpp:
+        (WebCore::MediaControlTextTrackContainerElement::createTextTrackRepresentationImage):
+        * page/TextIndicator.cpp:
+        (WebCore::takeSnapshot):
+        * platform/DragImage.cpp:
+        (WebCore::createDragImageFromSnapshot):
+        * platform/graphics/filters/FETile.cpp:
+        (WebCore::FETile::platformApplySoftware):
+        * platform/mac/ThemeMac.mm:
+        (WebCore::ThemeMac::drawCellOrFocusRingWithViewIntoContext):
+        * platform/mediastream/mac/AVVideoCaptureSource.mm:
+        (WebCore::AVVideoCaptureSource::currentFrameImage):
+        * rendering/RenderBoxModelObject.cpp:
+        (WebCore::RenderBoxModelObject::paintFillLayerExtended):
+        * rendering/RenderThemeMac.mm:
+        (WebCore::RenderThemeMac::paintProgressBar):
+        * rendering/svg/RenderSVGResourcePattern.cpp:
+        (WebCore::RenderSVGResourcePattern::buildPattern):
+        * svg/graphics/SVGImage.cpp:
+        (WebCore::SVGImage::drawPatternForContainer):
+        Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.
+
 2015-12-13  Andreas Kling  <akling@apple.com>
 
         CachedScript could have a copy-free path for all-ASCII scripts.
index 265bbd7..a1ea5ef 100644 (file)
@@ -1374,7 +1374,7 @@ RefPtr<Image> MediaControlTextTrackContainerElement::createTextTrackRepresentati
 
     layer->paint(buffer->context(), paintingRect, LayoutSize(), PaintBehaviorFlattenCompositingLayers, nullptr, RenderLayer::PaintLayerPaintingCompositingAllPhases);
 
-    return buffer->copyImage();
+    return ImageBuffer::sinkIntoImage(WTF::move(buffer));
 }
 
 void MediaControlTextTrackContainerElement::textTrackRepresentationBoundsChanged(const IntRect&)
index c00e96f..bb35b1b 100644 (file)
@@ -155,7 +155,7 @@ static RefPtr<Image> takeSnapshot(Frame& frame, IntRect rect, SnapshotOptions op
     if (!buffer)
         return nullptr;
     scaleFactor = buffer->resolutionScale();
-    return buffer->copyImage(CopyBackingStore, Unscaled);
+    return ImageBuffer::sinkIntoImage(WTF::move(buffer), Unscaled);
 }
 
 static bool takeSnapshots(TextIndicatorData& data, Frame& frame, IntRect snapshotRect)
index 8191c6f..89421c0 100644 (file)
@@ -106,7 +106,7 @@ static DragImageRef createDragImageFromSnapshot(std::unique_ptr<ImageBuffer> sna
 #else
     UNUSED_PARAM(node);
 #endif
-    RefPtr<Image> image = snapshot->copyImage(ImageBuffer::fastCopyImageMode(), Unscaled);
+    RefPtr<Image> image = ImageBuffer::sinkIntoImage(WTF::move(snapshot), Unscaled);
     if (!image)
         return nullptr;
     return createDragImageFromImage(image.get(), orientation);
index c7e8c65..c67eb52 100644 (file)
@@ -443,6 +443,33 @@ void GraphicsContext::drawImageBuffer(ImageBuffer& image, const FloatRect& desti
     image.draw(*this, destination, source, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode, imagePaintingOptions.m_useLowQualityScale);
 }
 
+void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatPoint& destination, const ImagePaintingOptions& imagePaintingOptions)
+{
+    if (!image)
+        return;
+    IntSize imageLogicalSize = image->logicalSize();
+    drawConsumingImageBuffer(WTF::move(image), FloatRect(destination, imageLogicalSize), FloatRect(FloatPoint(), imageLogicalSize), imagePaintingOptions);
+}
+
+void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatRect& destination, const ImagePaintingOptions& imagePaintingOptions)
+{
+    if (!image)
+        return;
+    IntSize imageLogicalSize = image->logicalSize();
+    drawConsumingImageBuffer(WTF::move(image), destination, FloatRect(FloatPoint(), FloatSize(imageLogicalSize)), imagePaintingOptions);
+}
+
+void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
+{
+    if (paintingDisabled() || !image)
+        return;
+    
+    // FIXME (49002): Should be InterpolationLow
+    InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_useLowQualityScale ? InterpolationNone : imageInterpolationQuality());
+
+    ImageBuffer::drawConsuming(WTF::move(image), *this, destination, source, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode, imagePaintingOptions.m_useLowQualityScale);
+}
+
 void GraphicsContext::clip(const IntRect& rect)
 {
     clip(FloatRect(rect));
index be20a73..e7b28b9 100644 (file)
@@ -315,6 +315,10 @@ public:
 
     void drawPattern(Image&, const FloatRect& srcRect, const AffineTransform&, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal);
 
+    WEBCORE_EXPORT void drawConsumingImageBuffer(std::unique_ptr<ImageBuffer>, const FloatPoint& destination, const ImagePaintingOptions& = ImagePaintingOptions());
+    void drawConsumingImageBuffer(std::unique_ptr<ImageBuffer>, const FloatRect& destination, const ImagePaintingOptions& = ImagePaintingOptions());
+    void drawConsumingImageBuffer(std::unique_ptr<ImageBuffer>, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& = ImagePaintingOptions());
+
     WEBCORE_EXPORT void setImageInterpolationQuality(InterpolationQuality);
     InterpolationQuality imageInterpolationQuality() const { return m_state.imageInterpolationQuality; }
 
index 7d58f00..ccefce0 100644 (file)
@@ -67,6 +67,7 @@ enum ScaleBehavior {
 
 class ImageBuffer {
     WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
+    friend class IOSurface;
 public:
     // Will return a null pointer on allocation failure.
     static std::unique_ptr<ImageBuffer> create(const FloatSize& size, RenderingMode renderingMode, float resolutionScale = 1, ColorSpace colorSpace = ColorSpaceSRGB)
@@ -91,6 +92,7 @@ public:
     WEBCORE_EXPORT GraphicsContext& context() const;
 
     WEBCORE_EXPORT RefPtr<Image> copyImage(BackingStoreCopy = CopyBackingStore, ScaleBehavior = Scaled) const;
+    WEBCORE_EXPORT static RefPtr<Image> sinkIntoImage(std::unique_ptr<ImageBuffer>, ScaleBehavior = Scaled);
     // Give hints on the faster copyImage Mode, return DontCopyBackingStore if it supports the DontCopyBackingStore behavior
     // or return CopyBackingStore if it doesn't.  
     static BackingStoreCopy fastCopyImageMode();
@@ -130,6 +132,7 @@ private:
     // The returned image might be larger than the internalSize(). If you want the smaller
     // image, crop the result.
     RetainPtr<CGImageRef> copyNativeImage(BackingStoreCopy = CopyBackingStore) const;
+    static RetainPtr<CGImageRef> sinkIntoNativeImage(std::unique_ptr<ImageBuffer>);
     void flushContext() const;
 #endif
     void clip(GraphicsContext&, const FloatRect&) const;
@@ -137,6 +140,8 @@ private:
     void draw(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false);
     void drawPattern(GraphicsContext&, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, const FloatSize& spacing, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal);
 
+    static void drawConsuming(std::unique_ptr<ImageBuffer>, GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), CompositeOperator = CompositeSourceOver, BlendMode = BlendModeNormal, bool useLowQualityScale = false);
+
     inline void genericConvertToLuminanceMask();
 
     friend class GraphicsContext;
index d5de76f..2372bd7 100644 (file)
@@ -195,6 +195,11 @@ GraphicsContext& ImageBuffer::context() const
     return *m_data.m_context;
 }
 
+RefPtr<Image> ImageBuffer::sinkIntoImage(std::unique_ptr<ImageBuffer> imageBuffer, ScaleBehavior scaleBehavior)
+{
+    return imageBuffer->copyImage(DontCopyBackingStore, scaleBehavior);
+}
+
 RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior) const
 {
     if (copyBehavior == CopyBackingStore)
@@ -214,6 +219,11 @@ void ImageBuffer::clip(GraphicsContext& context, const FloatRect& maskRect) cons
     context.platformContext()->pushImageMask(m_data.m_surface.get(), maskRect);
 }
 
+void ImageBuffer::drawConsuming(std::unique_ptr<ImageBuffer> imageBuffer, GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, bool useLowQualityScale)
+{
+    imageBuffer->draw(destContext, destRect, srcRect, op, blendMode, useLowQualityScale);
+}
+
 void ImageBuffer::draw(GraphicsContext& destinationContext, const FloatRect& destRect, const FloatRect& srcRect,
     CompositeOperator op, BlendMode blendMode, bool useLowQualityScale)
 {
index d5234bd..e0ef192 100644 (file)
@@ -43,6 +43,7 @@
 #include <wtf/RetainPtr.h>
 #include <wtf/text/Base64.h>
 #include <wtf/text/WTFString.h>
+
 #if PLATFORM(COCOA)
 #include "WebCoreSystemInterface.h"
 #endif
@@ -177,18 +178,15 @@ static RetainPtr<CGImageRef> createCroppedImageIfNecessary(CGImageRef image, con
     return image;
 }
 
-RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior scaleBehavior) const
+static RefPtr<Image> createBitmapImageAfterScalingIfNeeded(RetainPtr<CGImageRef>&& image, IntSize internalSize, IntSize logicalSize, IntSize backingStoreSize, float resolutionScale, ScaleBehavior scaleBehavior)
 {
-    RetainPtr<CGImageRef> image;
-    if (m_resolutionScale == 1 || scaleBehavior == Unscaled) {
-        image = copyNativeImage(copyBehavior);
-        image = createCroppedImageIfNecessary(image.get(), internalSize());
-    } else {
-        image = copyNativeImage(DontCopyBackingStore);
-        RetainPtr<CGContextRef> context = adoptCF(CGBitmapContextCreate(0, logicalSize().width(), logicalSize().height(), 8, 4 * logicalSize().width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
+    if (resolutionScale == 1 || scaleBehavior == Unscaled)
+        image = createCroppedImageIfNecessary(image.get(), internalSize);
+    else {
+        RetainPtr<CGContextRef> context = adoptCF(CGBitmapContextCreate(0, logicalSize.width(), logicalSize.height(), 8, 4 * logicalSize.width(), sRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
-        CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize()));
-        FloatSize imageSizeInUserSpace = scaleSizeToUserSpace(logicalSize(), m_data.backingStoreSize, internalSize());
+        CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize));
+        FloatSize imageSizeInUserSpace = scaleSizeToUserSpace(logicalSize, backingStoreSize, internalSize);
         CGContextDrawImage(context.get(), FloatRect(FloatPoint::zero(), imageSizeInUserSpace), image.get());
         image = adoptCF(CGBitmapContextCreateImage(context.get()));
     }
@@ -199,11 +197,44 @@ RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavio
     return BitmapImage::create(image.get());
 }
 
+RefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior scaleBehavior) const
+{
+    RetainPtr<CGImageRef> image;
+    if (m_resolutionScale == 1 || scaleBehavior == Unscaled)
+        image = copyNativeImage(copyBehavior);
+    else
+        image = copyNativeImage(DontCopyBackingStore);
+
+    return createBitmapImageAfterScalingIfNeeded(WTF::move(image), internalSize(), logicalSize(), m_data.backingStoreSize, m_resolutionScale, scaleBehavior);
+}
+
+RefPtr<Image> ImageBuffer::sinkIntoImage(std::unique_ptr<ImageBuffer> imageBuffer, ScaleBehavior scaleBehavior)
+{
+    IntSize internalSize = imageBuffer->internalSize();
+    IntSize logicalSize = imageBuffer->logicalSize();
+    IntSize backingStoreSize = imageBuffer->m_data.backingStoreSize;
+    float resolutionScale = imageBuffer->m_resolutionScale;
+
+    return createBitmapImageAfterScalingIfNeeded(sinkIntoNativeImage(WTF::move(imageBuffer)), internalSize, logicalSize, backingStoreSize, resolutionScale, scaleBehavior);
+}
+
 BackingStoreCopy ImageBuffer::fastCopyImageMode()
 {
     return DontCopyBackingStore;
 }
 
+RetainPtr<CGImageRef> ImageBuffer::sinkIntoNativeImage(std::unique_ptr<ImageBuffer> imageBuffer)
+{
+#if USE(IOSURFACE_CANVAS_BACKING_STORE)
+    if (!imageBuffer->m_data.surface)
+        return imageBuffer->copyNativeImage(DontCopyBackingStore);
+
+    return IOSurface::sinkIntoImage(IOSurface::createFromImageBuffer(WTF::move(imageBuffer)));
+#else
+    return imageBuffer->copyNativeImage(DontCopyBackingStore);
+#endif
+}
+
 RetainPtr<CGImageRef> ImageBuffer::copyNativeImage(BackingStoreCopy copyBehavior) const
 {
     RetainPtr<CGImageRef> image;
@@ -228,6 +259,29 @@ RetainPtr<CGImageRef> ImageBuffer::copyNativeImage(BackingStoreCopy copyBehavior
     return image;
 }
 
+void ImageBuffer::drawConsuming(std::unique_ptr<ImageBuffer> imageBuffer, GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, bool useLowQualityScale)
+{
+#if USE(IOSURFACE_CANVAS_BACKING_STORE)
+    if (!imageBuffer->m_data.surface) {
+        imageBuffer->draw(destContext, destRect, srcRect, op, blendMode, useLowQualityScale);
+        return;
+    }
+    
+    ASSERT(destContext.isAcceleratedContext());
+    
+    float resolutionScale = imageBuffer->m_resolutionScale;
+    IntSize backingStoreSize = imageBuffer->m_data.backingStoreSize;
+
+    RetainPtr<CGImageRef> image = IOSurface::sinkIntoImage(IOSurface::createFromImageBuffer(WTF::move(imageBuffer)));
+    
+    FloatRect adjustedSrcRect = srcRect;
+    adjustedSrcRect.scale(resolutionScale, resolutionScale);
+    destContext.drawNativeImage(image.get(), backingStoreSize, destRect, adjustedSrcRect, op, blendMode);
+#else
+    imageBuffer->draw(destContext, destRect, srcRect, op, blendMode, useLowQualityScale);
+#endif
+}
+
 void ImageBuffer::draw(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, bool)
 {
     RetainPtr<CGImageRef> image;
index cbcf56c..abf565b 100644 (file)
@@ -50,6 +50,8 @@ public:
     WEBCORE_EXPORT static std::unique_ptr<IOSurface> createFromSendRight(const MachSendRight&, ColorSpace);
     static std::unique_ptr<IOSurface> createFromSurface(IOSurfaceRef, ColorSpace);
     WEBCORE_EXPORT static std::unique_ptr<IOSurface> createFromImage(CGImageRef);
+    
+    static std::unique_ptr<IOSurface> createFromImageBuffer(std::unique_ptr<ImageBuffer>);
 
     WEBCORE_EXPORT static void moveToPool(std::unique_ptr<IOSurface>&&);
 
@@ -60,6 +62,7 @@ public:
     // Any images created from a surface need to be released before releasing
     // the surface, or an expensive GPU readback can result.
     WEBCORE_EXPORT RetainPtr<CGImageRef> createImage();
+    static RetainPtr<CGImageRef> sinkIntoImage(std::unique_ptr<IOSurface>);
 
     IOSurfaceRef surface() const { return m_surface.get(); }
     WEBCORE_EXPORT GraphicsContext& ensureGraphicsContext();
index cd4412e..e32f496 100644 (file)
 #import "GraphicsContextCG.h"
 #import "IOSurfacePool.h"
 #import "IOSurfaceSPI.h"
+#import "ImageBuffer.h"
+#import "ImageBufferDataCG.h"
 #import "MachSendRight.h"
 #import <wtf/Assertions.h>
 
 extern "C" {
 CGContextRef CGIOSurfaceContextCreate(IOSurfaceRef, size_t, size_t, size_t, size_t, CGColorSpaceRef, CGBitmapInfo);
 CGImageRef CGIOSurfaceContextCreateImage(CGContextRef);
+CGImageRef CGIOSurfaceContextCreateImageReference(CGContextRef);
 }
 
 using namespace WebCore;
@@ -98,6 +101,11 @@ void IOSurface::moveToPool(std::unique_ptr<IOSurface>&& surface)
     IOSurfacePool::sharedPool().addSurface(WTF::move(surface));
 }
 
+std::unique_ptr<IOSurface> IOSurface::createFromImageBuffer(std::unique_ptr<ImageBuffer> imageBuffer)
+{
+    return WTF::move(imageBuffer->m_data.surface);
+}
+
 IOSurface::IOSurface(IntSize size, ColorSpace colorSpace, Format format)
     : m_colorSpace(colorSpace)
     , m_size(size)
@@ -244,6 +252,15 @@ RetainPtr<CGImageRef> IOSurface::createImage()
     return adoptCF(CGIOSurfaceContextCreateImage(ensurePlatformContext()));
 }
 
+RetainPtr<CGImageRef> IOSurface::sinkIntoImage(std::unique_ptr<IOSurface> surface)
+{
+#if (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
+    return adoptCF(CGIOSurfaceContextCreateImageReference(surface->ensurePlatformContext()));
+#else
+    return surface->createImage();
+#endif
+}
+
 void IOSurface::setContextSize(IntSize contextSize)
 {
     if (contextSize == m_contextSize)
index e8803bc..8f027e1 100644 (file)
@@ -72,7 +72,7 @@ void FETile::platformApplySoftware()
     tileImageContext.translate(-inMaxEffectLocation.x(), -inMaxEffectLocation.y());
     tileImageContext.drawImageBuffer(*inBuffer, in->absolutePaintRect().location());
 
-    auto tileImageCopy = tileImage->copyImage(CopyBackingStore);
+    auto tileImageCopy = ImageBuffer::sinkIntoImage(WTF::move(tileImage));
     if (!tileImageCopy)
         return;
 
index f1e2113..d3089d9 100644 (file)
@@ -692,7 +692,7 @@ bool ThemeMac::drawCellOrFocusRingWithViewIntoContext(NSCell *cell, GraphicsCont
             LocalCurrentGraphicsContext localContext(imageBuffer->context());
             needsRepaint = drawCellOrFocusRingIntoRectWithView(cell, imageBufferDrawRect, view, drawButtonCell, drawFocusRing);
         }
-        context.drawImageBuffer(*imageBuffer, rect.location() - FloatSize(buttonFocusRectOutlineWidth, buttonFocusRectOutlineWidth));
+        context.drawConsumingImageBuffer(WTF::move(imageBuffer), rect.location() - FloatSize(buttonFocusRectOutlineWidth, buttonFocusRectOutlineWidth));
         return needsRepaint;
     }
     if (drawButtonCell)
index 1126720..57c3272 100644 (file)
@@ -334,7 +334,7 @@ RefPtr<Image> AVVideoCaptureSource::currentFrameImage()
 
     paintCurrentFrameInContext(imageBuffer->context(), imageRect);
 
-    return imageBuffer->copyImage();
+    return ImageBuffer::sinkIntoImage(WTF::move(imageBuffer));
 }
 
 RetainPtr<CGImageRef> AVVideoCaptureSource::currentFrameCGImage()
index 2ff42fd..8f72a15 100644 (file)
@@ -840,7 +840,7 @@ void RenderBoxModelObject::paintFillLayerExtended(const PaintInfo& paintInfo, co
     }
 
     if (maskImage && bgLayer->clip() == TextFillBox) {
-        context.drawImageBuffer(*maskImage, maskRect, CompositeDestinationIn);
+        context.drawConsumingImageBuffer(WTF::move(maskImage), maskRect, CompositeDestinationIn);
         context.endTransparencyLayer();
     }
 }
index b0446ba..c080458 100644 (file)
@@ -1156,7 +1156,7 @@ bool RenderThemeMac::paintProgressBar(const RenderObject& renderObject, const Pa
         paintInfo.context().scale(FloatSize(-1, 1));
     }
 
-    paintInfo.context().drawImageBuffer(*imageBuffer, inflatedRect.location());
+    paintInfo.context().drawConsumingImageBuffer(WTF::move(imageBuffer), inflatedRect.location());
     return false;
 }
 
index cc2b4d6..04c8162 100644 (file)
@@ -113,7 +113,9 @@ PatternData* RenderSVGResourcePattern::buildPattern(RenderElement& renderer, uns
     if (!tileImage)
         return nullptr;
 
-    RefPtr<Image> copiedImage = tileImage->copyImage(CopyBackingStore);
+    const IntSize tileImageSize = tileImage->logicalSize();
+
+    RefPtr<Image> copiedImage = ImageBuffer::sinkIntoImage(WTF::move(tileImage));
     if (!copiedImage)
         return nullptr;
 
@@ -122,7 +124,7 @@ PatternData* RenderSVGResourcePattern::buildPattern(RenderElement& renderer, uns
     patternData->pattern = Pattern::create(copiedImage, true, true);
 
     // Compute pattern space transformation.
-    const IntSize tileImageSize = tileImage->logicalSize();
+
     patternData->transform.translate(tileBoundaries.x(), tileBoundaries.y());
     patternData->transform.scale(tileBoundaries.width() / tileImageSize.width(), tileBoundaries.height() / tileImageSize.height());
 
index 055ae0f..a4479f1 100644 (file)
@@ -208,7 +208,7 @@ void SVGImage::drawPatternForContainer(GraphicsContext& context, const FloatSize
     if (context.drawLuminanceMask())
         buffer->convertToLuminanceMask();
 
-    RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
+    RefPtr<Image> image = ImageBuffer::sinkIntoImage(WTF::move(buffer), Unscaled);
     if (!image)
         return;
 
index 25c5054..0bb4d31 100644 (file)
@@ -1,3 +1,15 @@
+2015-12-13  Tim Horton  <timothy_horton@apple.com>
+
+        Adopt CGIOSurfaceContextCreateImageReference to avoid unnecessary readback
+        https://bugs.webkit.org/show_bug.cgi?id=150988
+        <rdar://problem/18993594>
+
+        Reviewed by Darin Adler.
+
+        * WebCoreSupport/WebContextMenuClient.mm:
+        (WebContextMenuClient::imageForCurrentSharingServicePickerItem):
+        Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.
+
 2015-12-11  Eric Carlson  <eric.carlson@apple.com>
 
         [MediaStream] Add a setting to allow the mock media capture devices to be enabled and disabled
index 30cbe89..c4afdd7 100644 (file)
@@ -220,7 +220,7 @@ RetainPtr<NSImage> WebContextMenuClient::imageForCurrentSharingServicePickerItem
     frameView->frame().selection().setSelection(oldSelection);
     frameView->setPaintBehavior(oldPaintBehavior);
 
-    RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore);
+    RefPtr<Image> image = ImageBuffer::sinkIntoImage(WTF::move(buffer));
     if (!image)
         return nil;
 
index 18309b0..6c046ce 100644 (file)
@@ -1,3 +1,15 @@
+2015-12-13  Tim Horton  <timothy_horton@apple.com>
+
+        Adopt CGIOSurfaceContextCreateImageReference to avoid unnecessary readback
+        https://bugs.webkit.org/show_bug.cgi?id=150988
+        <rdar://problem/18993594>
+
+        Reviewed by Darin Adler.
+
+        * WebProcess/WebPage/WebFrame.cpp:
+        (WebKit::WebFrame::createSelectionSnapshot):
+        Adopt sinkIntoImage and drawConsumingImageBuffer in a few places.
+
 2015-12-13  Dan Bernstein  <mitz@apple.com>
 
         Another fix after r194018.
index d653b58..c37a7f9 100644 (file)
@@ -826,7 +826,7 @@ PassRefPtr<ShareableBitmap> WebFrame::createSelectionSnapshot() const
     auto graphicsContext = sharedSnapshot->createGraphicsContext();
     float deviceScaleFactor = coreFrame()->page()->deviceScaleFactor();
     graphicsContext->scale(FloatSize(deviceScaleFactor, deviceScaleFactor));
-    graphicsContext->drawImageBuffer(*snapshot, FloatPoint());
+    graphicsContext->drawConsumingImageBuffer(WTF::move(snapshot), FloatPoint());
 
     return sharedSnapshot.release();
 }