Changeset 170925 in webkit


Ignore:
Timestamp:
Jul 9, 2014 12:31:33 PM (10 years ago)
Author:
timothy_horton@apple.com
Message:

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.

  • 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.

Location:
trunk/Source
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r170924 r170925  
     12014-07-09  Tim Horton  <timothy_horton@apple.com>
     2
     3        Use IOSurface ViewSnapshots everywhere on Mac, remove JPEG encoding path
     4        https://bugs.webkit.org/show_bug.cgi?id=134773
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * WebCore.exp.in:
     9        * platform/graphics/cocoa/IOSurface.h:
     10        * platform/graphics/cocoa/IOSurface.mm:
     11        (IOSurface::createFromImage):
     12        Move make-an-IOSurface-from-a-CGImageRef into WebCore::IOSurface.
     13
    1142014-07-09  Enrica Casucci  <enrica@apple.com>
    215
  • trunk/Source/WebCore/WebCore.exp.in

    r170891 r170925  
    14801480__ZN7WebCore9HTMLNames9titleAttrE
    14811481__ZN7WebCore9HTMLNames9valueAttrE
     1482__ZN7WebCore9IOSurface15createFromImageEP7CGImage
    14821483__ZN7WebCore9InlineBox14adjustPositionEff
    14831484__ZN7WebCore9InlineBox14dirtyLineBoxesEv
  • trunk/Source/WebCore/platform/graphics/cocoa/IOSurface.h

    r169370 r170925  
    4141    static PassRefPtr<IOSurface> createFromMachPort(mach_port_t, ColorSpace);
    4242    static PassRefPtr<IOSurface> createFromSurface(IOSurfaceRef, ColorSpace);
     43    static PassRefPtr<IOSurface> createFromImage(CGImageRef);
    4344
    4445    static IntSize maximumSize();
  • trunk/Source/WebCore/platform/graphics/cocoa/IOSurface.mm

    r169370 r170925  
    7171}
    7272
     73PassRefPtr<IOSurface> IOSurface::createFromImage(CGImageRef image)
     74{
     75    size_t width = CGImageGetWidth(image);
     76    size_t height = CGImageGetHeight(image);
     77
     78    RefPtr<IOSurface> surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
     79    auto surfaceContext = surface->ensurePlatformContext();
     80    CGContextDrawImage(surfaceContext, CGRectMake(0, 0, width, height), image);
     81    CGContextFlush(surfaceContext);
     82
     83    return surface.release();
     84}
     85
    7386IOSurface::IOSurface(IntSize size, ColorSpace colorSpace)
    7487    : m_colorSpace(colorSpace)
  • trunk/Source/WebKit2/ChangeLog

    r170923 r170925  
     12014-07-09  Tim Horton  <timothy_horton@apple.com>
     2
     3        Use IOSurface ViewSnapshots everywhere on Mac, remove JPEG encoding path
     4        https://bugs.webkit.org/show_bug.cgi?id=134773
     5
     6        Reviewed by Anders Carlsson.
     7
     8        * UIProcess/API/mac/WKView.mm:
     9        (-[WKView _takeViewSnapshot]):
     10        * UIProcess/mac/ViewSnapshotStore.h:
     11        * UIProcess/mac/ViewSnapshotStore.mm:
     12        (WebKit::ViewSnapshotStore::ViewSnapshotStore):
     13        (WebKit::ViewSnapshotStore::~ViewSnapshotStore):
     14        (WebKit::ViewSnapshotStore::recordSnapshot):
     15        (WebKit::ViewSnapshot::clearImage):
     16        (WebKit::ViewSnapshot::asLayerContents):
     17        (WebKit::createIOSurfaceFromImage): Deleted.
     18        (WebKit::compressImageAsJPEG): Deleted.
     19        (WebKit::ViewSnapshotStore::reduceSnapshotMemoryCost): Deleted.
     20        (WebKit::ViewSnapshotStore::didCompressSnapshot): Deleted.
     21        Remove all ViewSnapshot(Store) code related to JPEG-encoded snapshots.
     22        Remove the "image" member on ViewSnapshot; Mac will always start out with an IOSurface instead.
     23        Adopt WebCore::IOSurface::createFromImage to make that happen.
     24        Add a comment noting that if a snapshot comes back empty, we should throw it away completely.
     25
    1262014-07-09  Anders Carlsson  <andersca@apple.com>
    227
  • trunk/Source/WebKit2/UIProcess/API/mac/WKView.mm

    r170774 r170925  
    31423142    auto croppedSnapshotImage = adoptCF(CGImageCreateWithImageInRect(windowSnapshotImage.get(), NSRectToCGRect([window convertRectToBacking:croppedImageRect])));
    31433143
    3144     snapshot.image = croppedSnapshotImage.get();
     3144    snapshot.surface = IOSurface::createFromImage(croppedSnapshotImage.get());
     3145    snapshot.surface->setIsVolatile(true);
    31453146
    31463147    IntSize imageSize(CGImageGetWidth(croppedSnapshotImage.get()), CGImageGetHeight(croppedSnapshotImage.get()));
  • trunk/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.h

    r170335 r170925  
    3838OBJC_CLASS CAContext;
    3939
    40 #if PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
    41 #define USE_JPEG_VIEW_SNAPSHOTS false
     40#if PLATFORM(MAC)
    4241#define USE_IOSURFACE_VIEW_SNAPSHOTS true
    4342#define USE_RENDER_SERVER_VIEW_SNAPSHOTS false
    44 #elif PLATFORM(MAC)
    45 // Mountain Lion and before do not support IOSurface purgeability.
    46 #define USE_JPEG_VIEW_SNAPSHOTS true
    47 #define USE_IOSURFACE_VIEW_SNAPSHOTS false
    48 #define USE_RENDER_SERVER_VIEW_SNAPSHOTS false
    4943#else
    50 #define USE_JPEG_VIEW_SNAPSHOTS false
    5144#define USE_IOSURFACE_VIEW_SNAPSHOTS false
    5245#define USE_RENDER_SERVER_VIEW_SNAPSHOTS true
     
    5952
    6053struct ViewSnapshot {
    61 #if USE_JPEG_VIEW_SNAPSHOTS || USE_IOSURFACE_VIEW_SNAPSHOTS
     54#if USE_IOSURFACE_VIEW_SNAPSHOTS
    6255    RefPtr<WebCore::IOSurface> surface;
    63     RetainPtr<CGImageRef> image;
    6456#endif
    6557#if USE_RENDER_SERVER_VIEW_SNAPSHOTS
     
    10294    void pruneSnapshots(WebPageProxy&);
    10395    void removeSnapshotImage(ViewSnapshot&);
    104     void reduceSnapshotMemoryCost(const String& uuid);
    105 
    106 #if USE_JPEG_VIEW_SNAPSHOTS
    107     void didCompressSnapshot(const String& uuid, RetainPtr<CGImageRef> newImage, size_t newImageSize);
    108     dispatch_queue_t m_compressionQueue;
    109 #endif
    11096
    11197    HashMap<String, ViewSnapshot> m_snapshotMap;
  • trunk/Source/WebKit2/UIProcess/mac/ViewSnapshotStore.mm

    r168689 r170925  
    4141#if USE_IOSURFACE_VIEW_SNAPSHOTS
    4242static const size_t maximumSnapshotCacheSize = 400 * (1024 * 1024);
    43 #elif USE_JPEG_VIEW_SNAPSHOTS || USE_RENDER_SERVER_VIEW_SNAPSHOTS
    44 // Because non-IOSurface snapshots are not purgeable, we should keep fewer around.
     43#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
     44// Because render server snapshots are not purgeable, we should keep fewer around.
    4545static const size_t maximumSnapshotCacheSize = 50 * (1024 * 1024);
    4646#endif
     
    5252    , m_snapshotCacheSize(0)
    5353{
    54 #if USE_JPEG_VIEW_SNAPSHOTS
    55     m_compressionQueue = dispatch_queue_create("com.apple.WebKit.ViewSnapshotStore.CompressionQueue", nullptr);
    56 #endif
    5754}
    5855
    5956ViewSnapshotStore::~ViewSnapshotStore()
    6057{
    61 #if USE_JPEG_VIEW_SNAPSHOTS
    62     dispatch_release(m_compressionQueue);
    63 #endif
    6458    discardSnapshots();
    6559}
     
    186180    m_snapshotMap.add(item->snapshotUUID(), snapshot);
    187181    m_snapshotCacheSize += snapshot.imageSizeInBytes;
    188 
    189     reduceSnapshotMemoryCost(item->snapshotUUID());
    190182}
    191183
     
    208200}
    209201
    210 #if USE_IOSURFACE_VIEW_SNAPSHOTS
    211 static RefPtr<IOSurface> createIOSurfaceFromImage(CGImageRef image)
    212 {
    213     size_t width = CGImageGetWidth(image);
    214     size_t height = CGImageGetHeight(image);
    215 
    216     RefPtr<IOSurface> surface = IOSurface::create(IntSize(width, height), ColorSpaceDeviceRGB);
    217     RetainPtr<CGContextRef> surfaceContext = surface->ensurePlatformContext();
    218     CGContextDrawImage(surfaceContext.get(), CGRectMake(0, 0, width, height), image);
    219     CGContextFlush(surfaceContext.get());
    220 
    221     surface->setIsVolatile(true);
    222 
    223     return surface;
    224 }
    225 #endif
    226 
    227 #if USE_JPEG_VIEW_SNAPSHOTS
    228 static std::pair<RetainPtr<CGImageRef>, size_t> compressImageAsJPEG(CGImageRef image)
    229 {
    230     RetainPtr<NSData> compressedData = adoptNS([[NSMutableData alloc] init]);
    231     RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData((CFMutableDataRef)compressedData.get(), kUTTypeJPEG, 1, 0));
    232 
    233     RetainPtr<NSDictionary> options = @{
    234         (NSString*)kCGImageDestinationLossyCompressionQuality: @0.9
    235     };
    236 
    237     CGImageDestinationAddImage(destination.get(), image, (CFDictionaryRef)options.get());
    238     CGImageDestinationFinalize(destination.get());
    239 
    240     RetainPtr<CGImageSourceRef> imageSourceRef = adoptCF(CGImageSourceCreateWithData((CFDataRef)compressedData.get(), 0));
    241     RetainPtr<CGImageRef> compressedSnapshot = adoptCF(CGImageSourceCreateImageAtIndex(imageSourceRef.get(), 0, 0));
    242    
    243     return std::make_pair(compressedSnapshot, [compressedData length]);
    244 }
    245 #endif
    246 
    247 void ViewSnapshotStore::reduceSnapshotMemoryCost(const String& uuid)
    248 {
    249     const auto& snapshotIterator = m_snapshotMap.find(uuid);
    250     if (snapshotIterator == m_snapshotMap.end())
    251         return;
    252     ViewSnapshot& snapshot = snapshotIterator->value;
    253     if (!snapshot.hasImage())
    254         return;
    255 
    256 #if USE_IOSURFACE_VIEW_SNAPSHOTS
    257     snapshot.surface = createIOSurfaceFromImage(snapshot.image.get());
    258     snapshot.image = nullptr;
    259 #elif USE_JPEG_VIEW_SNAPSHOTS
    260     RetainPtr<CGImageRef> originalImage = snapshot.image.get();
    261     dispatch_async(m_compressionQueue, [uuid, originalImage] {
    262         auto imageAndBytes = compressImageAsJPEG(originalImage.get());
    263         dispatch_async(dispatch_get_main_queue(), [uuid, imageAndBytes] {
    264             ViewSnapshotStore::shared().didCompressSnapshot(uuid, imageAndBytes.first, imageAndBytes.second);
    265         });
    266     });
    267 #endif
    268 }
    269 
    270 #if USE_JPEG_VIEW_SNAPSHOTS
    271 void ViewSnapshotStore::didCompressSnapshot(const String& uuid, RetainPtr<CGImageRef> newImage, size_t newImageSize)
    272 {
    273     const auto& snapshotIterator = m_snapshotMap.find(uuid);
    274     if (snapshotIterator == m_snapshotMap.end())
    275         return;
    276     ViewSnapshot& snapshot = snapshotIterator->value;
    277 
    278     size_t originalImageSize = snapshot.imageSizeInBytes;
    279     if (!newImage || newImageSize > originalImageSize)
    280         return;
    281     snapshot.image = newImage;
    282     snapshot.imageSizeInBytes = newImageSize;
    283     m_snapshotCacheSize += newImageSize;
    284     m_snapshotCacheSize -= originalImageSize;
    285 }
    286 #endif
    287 
    288202bool ViewSnapshot::hasImage() const
    289203{
     
    295209#if USE_IOSURFACE_VIEW_SNAPSHOTS
    296210    surface = nullptr;
    297 #endif
    298 #if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
    299     image = nullptr;
    300 #endif
    301 #if USE_RENDER_SERVER_VIEW_SNAPSHOTS
     211#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
    302212    if (slotID)
    303213        [ViewSnapshotStore::snapshottingContext() deleteSlot:slotID];
     
    310220{
    311221#if USE_IOSURFACE_VIEW_SNAPSHOTS
    312     if (surface) {
    313         if (surface->setIsVolatile(false) != IOSurface::SurfaceState::Valid)
    314             return nullptr;
    315 
    316         return (id)surface->surface();
    317     }
    318 #endif
    319 #if USE_IOSURFACE_VIEW_SNAPSHOTS || USE_JPEG_VIEW_SNAPSHOTS
    320     return (id)image.get();
    321 #endif
    322 #if USE_RENDER_SERVER_VIEW_SNAPSHOTS
     222    if (!surface)
     223        return nullptr;
     224
     225    // FIXME: This should destroy the surface and inform the ViewSnapshotStore to reduce m_snapshotCacheSize.
     226    if (surface->setIsVolatile(false) != IOSurface::SurfaceState::Valid)
     227        return nullptr;
     228
     229    return (id)surface->surface();
     230#elif USE_RENDER_SERVER_VIEW_SNAPSHOTS
    323231    return [CAContext objectForSlot:slotID];
    324232#endif
Note: See TracChangeset for help on using the changeset viewer.