Use IOSurface ViewSnapshots everywhere on Mac, remove JPEG encoding path
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 Jul 2014 19:31:33 +0000 (19:31 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 9 Jul 2014 19:31:33 +0000 (19:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=134773

Reviewed by Anders Carlsson.

* UIProcess/API/mac/WKView.mm:
(-[WKView _takeViewSnapshot]):
* UIProcess/mac/ViewSnapshotStore.h:
* UIProcess/mac/ViewSnapshotStore.mm:
(WebKit::ViewSnapshotStore::ViewSnapshotStore):
(WebKit::ViewSnapshotStore::~ViewSnapshotStore):
(WebKit::ViewSnapshotStore::recordSnapshot):
(WebKit::ViewSnapshot::clearImage):
(WebKit::ViewSnapshot::asLayerContents):
(WebKit::createIOSurfaceFromImage): Deleted.
(WebKit::compressImageAsJPEG): Deleted.
(WebKit::ViewSnapshotStore::reduceSnapshotMemoryCost): Deleted.
(WebKit::ViewSnapshotStore::didCompressSnapshot): Deleted.
Remove all ViewSnapshot(Store) code related to JPEG-encoded snapshots.
Remove the "image" member on ViewSnapshot; Mac will always start out with an IOSurface instead.
Adopt WebCore::IOSurface::createFromImage to make that happen.
Add a comment noting that if a snapshot comes back empty, we should throw it away completely.

* WebCore.exp.in:
* platform/graphics/cocoa/IOSurface.h:
* platform/graphics/cocoa/IOSurface.mm:
(IOSurface::createFromImage):
Move make-an-IOSurface-from-a-CGImageRef into WebCore::IOSurface.

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

Source/WebCore/ChangeLog
Source/WebCore/WebCore.exp.in
Source/WebCore/platform/graphics/cocoa/IOSurface.h
Source/WebCore/platform/graphics/cocoa/IOSurface.mm
Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/mac/WKView.mm
Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h
Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm

index fa63a20e2f902d8226e03e103ab6d9c3a23b1122..0b3f6d21c5163d152f75a208cefc2fbdc30e199e 100644 (file)
@@ -1,3 +1,16 @@
+2014-07-09  Tim Horton  <timothy_horton@apple.com>
+
+        Use IOSurface ViewSnapshots everywhere on Mac, remove JPEG encoding path
+        https://bugs.webkit.org/show_bug.cgi?id=134773
+
+        Reviewed by Anders Carlsson.
+
+        * WebCore.exp.in:
+        * platform/graphics/cocoa/IOSurface.h:
+        * platform/graphics/cocoa/IOSurface.mm:
+        (IOSurface::createFromImage):
+        Move make-an-IOSurface-from-a-CGImageRef into WebCore::IOSurface.
+
 2014-07-09  Enrica Casucci  <enrica@apple.com>
 
         Implement Editor::fontAttributesForSelectionStart() or iOS.
index 61c37785dc927b3282243bfd0eed9ac7b1732c88..9b3b001afcd694f43573fae21d96f0f7a44ca03e 100644 (file)
@@ -1479,6 +1479,7 @@ __ZN7WebCore9HTMLNames9selectTagE
 __ZN7WebCore9HTMLNames9styleAttrE
 __ZN7WebCore9HTMLNames9titleAttrE
 __ZN7WebCore9HTMLNames9valueAttrE
+__ZN7WebCore9IOSurface15createFromImageEP7CGImage
 __ZN7WebCore9InlineBox14adjustPositionEff
 __ZN7WebCore9InlineBox14dirtyLineBoxesEv
 __ZN7WebCore9InlineBox14selectionStateEv
index 540a87d5e494f346c6c8b9ada4b2ab3099b1cb5a..7f808ca21709924808c8a58928ececc48f30fd81 100644 (file)
@@ -40,6 +40,7 @@ public:
     static PassRefPtr<IOSurface> create(IntSize, ColorSpace);
     static PassRefPtr<IOSurface> createFromMachPort(mach_port_t, ColorSpace);
     static PassRefPtr<IOSurface> createFromSurface(IOSurfaceRef, ColorSpace);
+    static PassRefPtr<IOSurface> createFromImage(CGImageRef);
 
     static IntSize maximumSize();
 
index f6601e3c647a9fc59f2af06850cdab0007417741..b72f3bd1538de1bb23831f3268dd51475984541f 100644 (file)
@@ -70,6 +70,19 @@ PassRefPtr<IOSurface> IOSurface::createFromSurface(IOSurfaceRef surface, ColorSp
     return adoptRef(new IOSurface(surface, colorSpace));
 }
 
+PassRefPtr<IOSurface> IOSurface::createFromImage(CGImageRef image)
+{
+    size_t width = CGImageGetWidth(image);
+    size_t height = CGImageGetHeight(image);
+
+    RefPtr<IOSurface> surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
+    auto surfaceContext = surface->ensurePlatformContext();
+    CGContextDrawImage(surfaceContext, CGRectMake(0, 0, width, height), image);
+    CGContextFlush(surfaceContext);
+
+    return surface.release();
+}
+
 IOSurface::IOSurface(IntSize size, ColorSpace colorSpace)
     : m_colorSpace(colorSpace)
     , m_size(size)
index 160f2ba786d1ca65771a7d24ebe5681f601f8e40..8664b67d0c34f793ce9d165064c6373cba7f2a73 100644 (file)
@@ -1,3 +1,28 @@
+2014-07-09  Tim Horton  <timothy_horton@apple.com>
+
+        Use IOSurface ViewSnapshots everywhere on Mac, remove JPEG encoding path
+        https://bugs.webkit.org/show_bug.cgi?id=134773
+
+        Reviewed by Anders Carlsson.
+
+        * UIProcess/API/mac/WKView.mm:
+        (-[WKView _takeViewSnapshot]):
+        * UIProcess/mac/ViewSnapshotStore.h:
+        * UIProcess/mac/ViewSnapshotStore.mm:
+        (WebKit::ViewSnapshotStore::ViewSnapshotStore):
+        (WebKit::ViewSnapshotStore::~ViewSnapshotStore):
+        (WebKit::ViewSnapshotStore::recordSnapshot):
+        (WebKit::ViewSnapshot::clearImage):
+        (WebKit::ViewSnapshot::asLayerContents):
+        (WebKit::createIOSurfaceFromImage): Deleted.
+        (WebKit::compressImageAsJPEG): Deleted.
+        (WebKit::ViewSnapshotStore::reduceSnapshotMemoryCost): Deleted.
+        (WebKit::ViewSnapshotStore::didCompressSnapshot): Deleted.
+        Remove all ViewSnapshot(Store) code related to JPEG-encoded snapshots.
+        Remove the "image" member on ViewSnapshot; Mac will always start out with an IOSurface instead.
+        Adopt WebCore::IOSurface::createFromImage to make that happen.
+        Add a comment noting that if a snapshot comes back empty, we should throw it away completely.
+
 2014-07-09  Anders Carlsson  <andersca@apple.com>
 
         RemoteLayerBackingStore::ensureBackingStore should ensure that the entire backing store gets redrawn
index 7b3fae4abfc8de2f33daf81fd95f1a2734524168..e578a07968b3ce0f1398894551c1eb3405bbda27 100644 (file)
@@ -3141,7 +3141,8 @@ static void* keyValueObservingContext = &keyValueObservingContext;
 
     auto croppedSnapshotImage = adoptCF(CGImageCreateWithImageInRect(windowSnapshotImage.get(), NSRectToCGRect([window convertRectToBacking:croppedImageRect])));
 
-    snapshot.image = croppedSnapshotImage.get();
+    snapshot.surface = IOSurface::createFromImage(croppedSnapshotImage.get());
+    snapshot.surface->setIsVolatile(true);
 
     IntSize imageSize(CGImageGetWidth(croppedSnapshotImage.get()), CGImageGetHeight(croppedSnapshotImage.get()));
     snapshot.size = imageSize;
index 3fbaa4e2ac32aa31855372767f13acb1f6a3eca2..2bc77df0d56c5a593c2b351aba11bcf39e50299d 100644 (file)
 
 OBJC_CLASS CAContext;
 
-#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
-#define USE_JPEG_VIEW_SNAPSHOTS false
+#if PLATFORM(MAC)
 #define USE_IOSURFACE_VIEW_SNAPSHOTS true
 #define USE_RENDER_SERVER_VIEW_SNAPSHOTS false
-#elif PLATFORM(MAC)
-// Mountain Lion and before do not support IOSurface purgeability.
-#define USE_JPEG_VIEW_SNAPSHOTS true
-#define USE_IOSURFACE_VIEW_SNAPSHOTS false
-#define USE_RENDER_SERVER_VIEW_SNAPSHOTS false
 #else
-#define USE_JPEG_VIEW_SNAPSHOTS false
 #define USE_IOSURFACE_VIEW_SNAPSHOTS false
 #define USE_RENDER_SERVER_VIEW_SNAPSHOTS true
 #endif
@@ -58,9 +51,8 @@ class WebBackForwardListItem;
 class WebPageProxy;
 
 struct ViewSnapshot {
-#if USE_JPEG_VIEW_SNAPSHOTS || USE_IOSURFACE_VIEW_SNAPSHOTS
+#if USE_IOSURFACE_VIEW_SNAPSHOTS
     RefPtr<WebCore::IOSurface> surface;
-    RetainPtr<CGImageRef> image;
 #endif
 #if USE_RENDER_SERVER_VIEW_SNAPSHOTS
     uint32_t slotID = 0;
@@ -101,12 +93,6 @@ public:
 private:
     void pruneSnapshots(WebPageProxy&);
     void removeSnapshotImage(ViewSnapshot&);
-    void reduceSnapshotMemoryCost(const String& uuid);
-
-#if USE_JPEG_VIEW_SNAPSHOTS
-    void didCompressSnapshot(const String& uuid, RetainPtr<CGImageRef> newImage, size_t newImageSize);
-    dispatch_queue_t m_compressionQueue;
-#endif
 
     HashMap<String, ViewSnapshot> m_snapshotMap;
 
index f9992f239d6536f9d5099c190c1a3adbbf74c56b..971bff4ea72a927232d7f8cf24f3d880197b666d 100644 (file)
@@ -40,8 +40,8 @@ using namespace WebCore;
 
 #if USE_IOSURFACE_VIEW_SNAPSHOTS
 static const size_t maximumSnapshotCacheSize = 400 * (1024 * 1024);
-#elif USE_JPEG_VIEW_SNAPSHOTS || USE_RENDER_SERVER_VIEW_SNAPSHOTS
-// Because non-IOSurface snapshots are not purgeable, we should keep fewer around.
+#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
+// Because render server snapshots are not purgeable, we should keep fewer around.
 static const size_t maximumSnapshotCacheSize = 50 * (1024 * 1024);
 #endif
 
@@ -51,16 +51,10 @@ ViewSnapshotStore::ViewSnapshotStore()
     : m_enabled(true)
     , m_snapshotCacheSize(0)
 {
-#if USE_JPEG_VIEW_SNAPSHOTS
-    m_compressionQueue = dispatch_queue_create("com.apple.WebKit.ViewSnapshotStore.CompressionQueue", nullptr);
-#endif
 }
 
 ViewSnapshotStore::~ViewSnapshotStore()
 {
-#if USE_JPEG_VIEW_SNAPSHOTS
-    dispatch_release(m_compressionQueue);
-#endif
     discardSnapshots();
 }
 
@@ -185,8 +179,6 @@ void ViewSnapshotStore::recordSnapshot(WebPageProxy& webPageProxy)
 
     m_snapshotMap.add(item->snapshotUUID(), snapshot);
     m_snapshotCacheSize += snapshot.imageSizeInBytes;
-
-    reduceSnapshotMemoryCost(item->snapshotUUID());
 }
 
 bool ViewSnapshotStore::getSnapshot(WebBackForwardListItem* item, ViewSnapshot& snapshot)
@@ -207,84 +199,6 @@ void ViewSnapshotStore::discardSnapshots()
         removeSnapshotImage(snapshot);
 }
 
-#if USE_IOSURFACE_VIEW_SNAPSHOTS
-static RefPtr<IOSurface> createIOSurfaceFromImage(CGImageRef image)
-{
-    size_t width = CGImageGetWidth(image);
-    size_t height = CGImageGetHeight(image);
-
-    RefPtr<IOSurface> surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
-    RetainPtr<CGContextRef> surfaceContext = surface->ensurePlatformContext();
-    CGContextDrawImage(surfaceContext.get(), CGRectMake(0, 0, width, height), image);
-    CGContextFlush(surfaceContext.get());
-
-    surface->setIsVolatile(true);
-
-    return surface;
-}
-#endif
-
-#if USE_JPEG_VIEW_SNAPSHOTS
-static std::pair<RetainPtr<CGImageRef>, size_t> compressImageAsJPEG(CGImageRef image)
-{
-    RetainPtr<NSData> compressedData = adoptNS([[NSMutableData alloc] init]);
-    RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData((CFMutableDataRef)compressedData.get(), kUTTypeJPEG, 1, 0));
-
-    RetainPtr<NSDictionary> options = @{
-        (NSString*)kCGImageDestinationLossyCompressionQuality: @0.9
-    };
-
-    CGImageDestinationAddImage(destination.get(), image, (CFDictionaryRef)options.get());
-    CGImageDestinationFinalize(destination.get());
-
-    RetainPtr<CGImageSourceRef> imageSourceRef = adoptCF(CGImageSourceCreateWithData((CFDataRef)compressedData.get(), 0));
-    RetainPtr<CGImageRef> compressedSnapshot = adoptCF(CGImageSourceCreateImageAtIndex(imageSourceRef.get(), 0, 0));
-    
-    return std::make_pair(compressedSnapshot, [compressedData length]);
-}
-#endif
-
-void ViewSnapshotStore::reduceSnapshotMemoryCost(const String& uuid)
-{
-    const auto& snapshotIterator = m_snapshotMap.find(uuid);
-    if (snapshotIterator == m_snapshotMap.end())
-        return;
-    ViewSnapshot& snapshot = snapshotIterator->value;
-    if (!snapshot.hasImage())
-        return;
-
-#if USE_IOSURFACE_VIEW_SNAPSHOTS
-    snapshot.surface = createIOSurfaceFromImage(snapshot.image.get());
-    snapshot.image = nullptr;
-#elif USE_JPEG_VIEW_SNAPSHOTS
-    RetainPtr<CGImageRef> originalImage = snapshot.image.get();
-    dispatch_async(m_compressionQueue, [uuid, originalImage] {
-        auto imageAndBytes = compressImageAsJPEG(originalImage.get());
-        dispatch_async(dispatch_get_main_queue(), [uuid, imageAndBytes] {
-            ViewSnapshotStore::shared().didCompressSnapshot(uuid, imageAndBytes.first, imageAndBytes.second);
-        });
-    });
-#endif
-}
-
-#if USE_JPEG_VIEW_SNAPSHOTS
-void ViewSnapshotStore::didCompressSnapshot(const String& uuid, RetainPtr<CGImageRef> newImage, size_t newImageSize)
-{
-    const auto& snapshotIterator = m_snapshotMap.find(uuid);
-    if (snapshotIterator == m_snapshotMap.end())
-        return;
-    ViewSnapshot& snapshot = snapshotIterator->value;
-
-    size_t originalImageSize = snapshot.imageSizeInBytes;
-    if (!newImage || newImageSize > originalImageSize)
-        return;
-    snapshot.image = newImage;
-    snapshot.imageSizeInBytes = newImageSize;
-    m_snapshotCacheSize += newImageSize;
-    m_snapshotCacheSize -= originalImageSize;
-}
-#endif
-
 bool ViewSnapshot::hasImage() const
 {
     return imageSizeInBytes;
@@ -294,11 +208,7 @@ void ViewSnapshot::clearImage()
 {
 #if USE_IOSURFACE_VIEW_SNAPSHOTS
     surface = nullptr;
-#endif
-#if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
-    image = nullptr;
-#endif
-#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
+#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
     if (slotID)
         [ViewSnapshotStore::snapshottingContext() deleteSlot:slotID];
     slotID = 0;
@@ -309,17 +219,15 @@ void ViewSnapshot::clearImage()
 id ViewSnapshot::asLayerContents()
 {
 #if USE_IOSURFACE_VIEW_SNAPSHOTS
-    if (surface) {
-        if (surface->setIsVolatile(false) != IOSurface::SurfaceState::Valid)
-            return nullptr;
+    if (!surface)
+        return nullptr;
 
-        return (id)surface->surface();
-    }
-#endif
-#if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
-    return (id)image.get();
-#endif
-#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
+    // FIXME: This should destroy the surface and inform the ViewSnapshotStore to reduce m_snapshotCacheSize.
+    if (surface->setIsVolatile(false) != IOSurface::SurfaceState::Valid)
+        return nullptr;
+
+    return (id)surface->surface();
+#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
     return [CAContext objectForSlot:slotID];
 #endif
 }