Compress snapshots on iOS
authorbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 5 Oct 2015 20:35:28 +0000 (20:35 +0000)
committerbdakin@apple.com <bdakin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 5 Oct 2015 20:35:28 +0000 (20:35 +0000)
https://bugs.webkit.org/show_bug.cgi?id=149814
-and corresponding-
rdar://problem/22976230

Reviewed by Simon Fraser.

Source/WebCore:

Though the default is still RGBA, it is now possible to create an IOSurface
that uses the YUV422 pixel format.
* platform/graphics/cocoa/IOSurface.h:
* platform/graphics/cocoa/IOSurface.mm:
(IOSurface::surfaceFromPool):
(IOSurface::create):
(IOSurface::createFromImage):
(IOSurface::IOSurface):
(IOSurface::releaseGraphicsContext):

In order to have a YUV IOSurface, we actually have to create an RGBA surface
first and then convert it to YUV, so this class method will handle that.
(IOSurface::convertToFormat):

Necessary SPI.
* platform/spi/cocoa/IOSurfaceSPI.h:

Source/WebKit2:

Compress the snapshot.
* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _takeViewSnapshot]):

Since the snapshots are converted to YUV asynchronously, it is possible to
get here and have a snapshot that does not yet have an image, so we have to
relax this constraint.
* UIProcess/mac/ViewSnapshotStore.mm:
(WebKit::ViewSnapshotStore::recordSnapshot):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/cocoa/IOSurface.h
Source/WebCore/platform/graphics/cocoa/IOSurface.mm
Source/WebCore/platform/spi/cocoa/IOSurfaceSPI.h
Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm

index 79800c7..834e166 100644 (file)
@@ -1,3 +1,29 @@
+2015-10-05  Beth Dakin  <bdakin@apple.com>
+
+        Compress snapshots on iOS
+        https://bugs.webkit.org/show_bug.cgi?id=149814
+        -and corresponding-
+        rdar://problem/22976230
+
+        Reviewed by Simon Fraser.
+
+        Though the default is still RGBA, it is now possible to create an IOSurface 
+        that uses the YUV422 pixel format.
+        * platform/graphics/cocoa/IOSurface.h:
+        * platform/graphics/cocoa/IOSurface.mm:
+        (IOSurface::surfaceFromPool):
+        (IOSurface::create):
+        (IOSurface::createFromImage):
+        (IOSurface::IOSurface):
+        (IOSurface::releaseGraphicsContext):
+
+        In order to have a YUV IOSurface, we actually have to create an RGBA surface 
+        first and then convert it to YUV, so this class method will handle that.
+        (IOSurface::convertToFormat):
+
+        Necessary SPI.
+        * platform/spi/cocoa/IOSurfaceSPI.h:
+
 2015-10-05  Zalan Bujtas  <zalan@apple.com>
 
         Mark the line dirty when RenderQuote's text changes.
index 186db96..7857645 100644 (file)
@@ -38,7 +38,14 @@ class MachSendRight;
 
 class IOSurface final {
 public:
-    WEBCORE_EXPORT static std::unique_ptr<IOSurface> create(IntSize, ColorSpace);
+    enum class Format {
+        RGBA,
+#if PLATFORM(IOS)
+        YUV422
+#endif
+    };
+
+    WEBCORE_EXPORT static std::unique_ptr<IOSurface> create(IntSize, ColorSpace, Format = Format::RGBA);
     WEBCORE_EXPORT static std::unique_ptr<IOSurface> create(IntSize, IntSize contextSize, ColorSpace);
     WEBCORE_EXPORT static std::unique_ptr<IOSurface> createFromSendRight(const MachSendRight&, ColorSpace);
     static std::unique_ptr<IOSurface> createFromSurface(IOSurfaceRef, ColorSpace);
@@ -73,6 +80,7 @@ public:
     IntSize size() const { return m_size; }
     size_t totalBytes() const { return m_totalBytes; }
     ColorSpace colorSpace() const { return m_colorSpace; }
+    Format format() const { return m_format; }
 
     WEBCORE_EXPORT bool isInUse() const;
 
@@ -80,8 +88,12 @@ public:
     // an accurate result from isInUse(), it needs to be released.
     WEBCORE_EXPORT void releaseGraphicsContext();
 
+#if PLATFORM(IOS)
+    WEBCORE_EXPORT static void convertToFormat(std::unique_ptr<WebCore::IOSurface>&& inSurface, Format, std::function<void(std::unique_ptr<WebCore::IOSurface>)>);
+#endif
+
 private:
-    IOSurface(IntSize, ColorSpace);
+    IOSurface(IntSize, ColorSpace, Format);
     IOSurface(IntSize, IntSize contextSize, ColorSpace);
     IOSurface(IOSurfaceRef, ColorSpace);
 
@@ -90,6 +102,7 @@ private:
     void setContextSize(IntSize);
 
     ColorSpace m_colorSpace;
+    Format m_format;
     IntSize m_size;
     IntSize m_contextSize;
     size_t m_totalBytes;
index cbd442a..1414a5d 100644 (file)
@@ -51,11 +51,15 @@ inline std::unique_ptr<IOSurface> IOSurface::surfaceFromPool(IntSize size, IntSi
     return cachedSurface;
 }
 
-std::unique_ptr<IOSurface> IOSurface::create(IntSize size, ColorSpace colorSpace)
+std::unique_ptr<IOSurface> IOSurface::create(IntSize size, ColorSpace colorSpace, Format pixelFormat)
 {
-    if (auto cachedSurface = surfaceFromPool(size, size, colorSpace))
-        return cachedSurface;
-    return std::unique_ptr<IOSurface>(new IOSurface(size, colorSpace));
+    // YUV422 IOSurfaces do not go in the pool.
+    if (pixelFormat == Format::RGBA) {
+        if (auto cachedSurface = surfaceFromPool(size, size, colorSpace))
+            return cachedSurface;
+    }
+
+    return std::unique_ptr<IOSurface>(new IOSurface(size, colorSpace, pixelFormat));
 }
 
 std::unique_ptr<IOSurface> IOSurface::create(IntSize size, IntSize contextSize, ColorSpace colorSpace)
@@ -92,17 +96,30 @@ std::unique_ptr<IOSurface> IOSurface::createFromImage(CGImageRef image)
     return surface;
 }
 
-IOSurface::IOSurface(IntSize size, ColorSpace colorSpace)
+IOSurface::IOSurface(IntSize size, ColorSpace colorSpace, Format format)
     : m_colorSpace(colorSpace)
+    , m_format(format)
     , m_size(size)
     , m_contextSize(size)
 {
     unsigned pixelFormat = 'BGRA';
+    unsigned bytesPerPixel = 4;
     unsigned bytesPerElement = 4;
+    unsigned elementWidth = 1;
+
+#if PLATFORM(IOS)
+    if (format == Format::YUV422) {
+        pixelFormat = 'yuvf';
+        bytesPerPixel = 2;
+        elementWidth = 2;
+        bytesPerElement = 4;
+    }
+#endif
+
     int width = size.width();
     int height = size.height();
 
-    size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, width * bytesPerElement);
+    size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, width * bytesPerPixel);
     ASSERT(bytesPerRow);
 
     m_totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize, height * bytesPerRow);
@@ -116,15 +133,17 @@ IOSurface::IOSurface(IntSize size, ColorSpace colorSpace)
         (id)kIOSurfaceBytesPerRow: @(bytesPerRow),
         (id)kIOSurfaceAllocSize: @(m_totalBytes),
 #if PLATFORM(IOS)
-        (id)kIOSurfaceCacheMode: @(kIOMapWriteCombineCache)
+        (id)kIOSurfaceCacheMode: @(kIOMapWriteCombineCache),
 #endif
+        (id)kIOSurfaceElementWidth: @(elementWidth),
+        (id)kIOSurfaceElementHeight: @(1)
     };
 
     m_surface = adoptCF(IOSurfaceCreate((CFDictionaryRef)options));
 }
 
 IOSurface::IOSurface(IntSize size, IntSize contextSize, ColorSpace colorSpace)
-    : IOSurface(size, colorSpace)
+    : IOSurface(size, colorSpace, Format::RGBA)
 {
     ASSERT(contextSize.width() <= size.width());
     ASSERT(contextSize.height() <= size.height());
@@ -133,6 +152,7 @@ IOSurface::IOSurface(IntSize size, IntSize contextSize, ColorSpace colorSpace)
 
 IOSurface::IOSurface(IOSurfaceRef surface, ColorSpace colorSpace)
     : m_colorSpace(colorSpace)
+    , m_format(Format::RGBA)
     , m_surface(surface)
 {
     m_size = IntSize(IOSurfaceGetWidth(surface), IOSurfaceGetHeight(surface));
@@ -228,4 +248,39 @@ void IOSurface::releaseGraphicsContext()
     m_cgContext = nullptr;
 }
 
+#if PLATFORM(IOS)
+ void IOSurface::convertToFormat(std::unique_ptr<WebCore::IOSurface>&& inSurface, Format format, std::function<void(std::unique_ptr<WebCore::IOSurface>)> callback)
+{
+    static IOSurfaceAcceleratorRef accelerator;
+    if (!accelerator) {
+        IOSurfaceAcceleratorCreate(nullptr, nullptr, &accelerator);
+
+        auto runLoopSource = IOSurfaceAcceleratorGetRunLoopSource(accelerator);
+        CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopDefaultMode);
+    }
+
+    if (inSurface->format() == format) {
+        callback(WTF::move(inSurface));
+        return;
+    }
+
+    auto destinationSurface = IOSurface::create(inSurface->size(), inSurface->colorSpace(), format);
+    IOSurfaceRef destinationIOSurfaceRef = destinationSurface->surface();
+
+    IOSurfaceAcceleratorCompletion completion;
+    completion.completionRefCon = new std::function<void(std::unique_ptr<IOSurface>)> (WTF::move(callback));
+    completion.completionRefCon2 = destinationSurface.release();
+    completion.completionCallback = [](void *completionRefCon, IOReturn, void * completionRefCon2) {
+        auto* callback = static_cast<std::function<void(std::unique_ptr<WebCore::IOSurface>)>*>(completionRefCon);
+        auto destinationSurface = std::unique_ptr<IOSurface>(static_cast<IOSurface*>(completionRefCon2));
+        
+        (*callback)(WTF::move(destinationSurface));
+        delete callback;
+    };
+
+    IOReturn ret = IOSurfaceAcceleratorTransformSurface(accelerator, inSurface->surface(), destinationIOSurfaceRef, nullptr, nullptr, &completion, nullptr, nullptr);
+    ASSERT_UNUSED(ret, ret == kIOReturnSuccess);
+}
+#endif // PLATFORM(IOS)
+
 #endif // USE(IOSURFACE)
index f641fea..e5d4f79 100644 (file)
@@ -94,6 +94,35 @@ IOReturn IOSurfaceSetPurgeable(IOSurfaceRef buffer, uint32_t newState, uint32_t
 
 WTF_EXTERN_C_END
 
-#endif
+#if PLATFORM(IOS)
+#if USE(APPLE_INTERNAL_SDK)
+
+#import <IOSurfaceAccelerator/IOSurfaceAccelerator.h>
+
+#else
+
+typedef struct __IOSurfaceAccelerator *IOSurfaceAcceleratorRef;
+
+WTF_EXTERN_C_BEGIN
+
+IOReturn IOSurfaceAcceleratorCreate(CFAllocatorRef allocator, CFDictionaryRef properties, IOSurfaceAcceleratorRef* acceleratorOut);
+CFRunLoopSourceRef IOSurfaceAcceleratorGetRunLoopSource(IOSurfaceAcceleratorRef accelerator);
+
+typedef void (*IOSurfaceAcceleratorCompletionCallback)(void* completionRefCon, IOReturn status, void* completionRefCon2);
+
+typedef struct IOSurfaceAcceleratorCompletion {
+    IOSurfaceAcceleratorCompletionCallback completionCallback;
+    void* completionRefCon;
+    void* completionRefCon2;
+} IOSurfaceAcceleratorCompletion;
+
+IOReturn IOSurfaceAcceleratorTransformSurface(IOSurfaceAcceleratorRef accelerator, IOSurfaceRef sourceBuffer, IOSurfaceRef destinationBuffer, CFDictionaryRef options, void* pCropRectangles, IOSurfaceAcceleratorCompletion* pCompletion, void* pSwap, uint32_t* pCommandID);
+
+WTF_EXTERN_C_END
+
+#endif // USE(APPLE_INTERNAL_SDK)
+#endif // PLATFORM(IOS)
+
+#endif // !PLATFORM(IOS_SIMULATOR)
 
 #endif // IOSurfaceSPI_h
index 7f9928d..7e0e8c0 100644 (file)
@@ -1,3 +1,22 @@
+2015-10-05  Beth Dakin  <bdakin@apple.com>
+
+        Compress snapshots on iOS
+        https://bugs.webkit.org/show_bug.cgi?id=149814
+        -and corresponding-
+        rdar://problem/22976230
+
+        Reviewed by Simon Fraser.
+
+        Compress the snapshot.
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView _takeViewSnapshot]):
+
+        Since the snapshots are converted to YUV asynchronously, it is possible to 
+        get here and have a snapshot that does not yet have an image, so we have to 
+        relax this constraint.
+        * UIProcess/mac/ViewSnapshotStore.mm:
+        (WebKit::ViewSnapshotStore::recordSnapshot):
+
 2015-10-05  Myles C. Maxfield  <mmaxfield@apple.com>
 
         Allow WKRetainPtrs to be used as keys in hashing data structures
index 19a025b..c2fb8c0 100644 (file)
@@ -1126,7 +1126,12 @@ static inline bool areEssentiallyEqualAsFloat(float a, float b)
     auto surface = WebCore::IOSurface::create(WebCore::expandedIntSize(snapshotSize), WebCore::ColorSpaceDeviceRGB);
     CARenderServerRenderLayerWithTransform(MACH_PORT_NULL, self.layer.context.contextId, reinterpret_cast<uint64_t>(self.layer), surface->surface(), 0, 0, &transform);
 
-    return WebKit::ViewSnapshot::create(WTF::move(surface));
+    RefPtr<WebKit::ViewSnapshot> viewSnapshot = WebKit::ViewSnapshot::create(nullptr);
+    WebCore::IOSurface::convertToFormat(WTF::move(surface), WebCore::IOSurface::Format::YUV422, [viewSnapshot](std::unique_ptr<WebCore::IOSurface> convertedSurface) {
+        viewSnapshot->setSurface(WTF::move(convertedSurface));
+    });
+
+    return viewSnapshot;
 #else
     uint32_t slotID = [WebKit::ViewSnapshotStore::snapshottingContext() createImageSlot:snapshotSize hasAlpha:YES];
 
index 95afdb8..722b1b1 100644 (file)
@@ -115,7 +115,7 @@ void ViewSnapshotStore::recordSnapshot(WebPageProxy& webPageProxy, WebBackForwar
     webPageProxy.willRecordNavigationSnapshot(item);
 
     RefPtr<ViewSnapshot> snapshot = webPageProxy.takeViewSnapshot();
-    if (!snapshot || !snapshot->hasImage())
+    if (!snapshot)
         return;
 
     snapshot->setRenderTreeSize(webPageProxy.renderTreeSize());