Title: [138356] trunk/Source/WebKit2
Revision
138356
Author
[email protected]
Date
2012-12-21 01:54:09 -0800 (Fri, 21 Dec 2012)

Log Message

Reducing print preview scale significantly causes a crash
https://bugs.webkit.org/show_bug.cgi?id=105519
<rdar://problem/12807090>

Reviewed by Alexey Proskuryakov.

The bitmap image used for print previews was being created respecting the print preview scale.
This doesn't make any sense, as the scale does not affect the size of the image required to
represent the previewed page. Instead, we should not scale the size, creating the buffer at a
size that is constant regardless of scale, and do the scaling when drawing *into* the buffer instead.

In some cases, this could cause an out-of-memory crash.

* UIProcess/API/mac/WKPrintingView.mm:
(-[WKPrintingView _drawPreview:]): Rename rect to scaledPrintingRect for clarity. Compute print
preview bitmap image size from the original non-scaled drawing size and the device scale factor.
Hand the image size to drawRectToImage. Draw the image without scaling, as it's always created
at the correct size. Don't compute the scale factor from the size of the image, we can know it from WebPageProxy.
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::drawRectToImage): Add a parameter to drawRectForImage, imageSize.
* UIProcess/WebPageProxy.h:
(WebPageProxy): Add a parameter to drawRectForImage, imageSize.
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::drawRectToImage): Use imageSize to size our buffer, and scale up to make the printing rect
fit in the bitmap's bounds.
* WebProcess/WebPage/WebPage.h:
(WebPage): Add a parameter to drawRectForImage, imageSize.
* WebProcess/WebPage/WebPage.messages.in: Add a parameter to drawRectForImage, imageSize.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (138355 => 138356)


--- trunk/Source/WebKit2/ChangeLog	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/ChangeLog	2012-12-21 09:54:09 UTC (rev 138356)
@@ -1,3 +1,34 @@
+2012-12-21  Tim Horton  <[email protected]>
+
+        Reducing print preview scale significantly causes a crash
+        https://bugs.webkit.org/show_bug.cgi?id=105519
+        <rdar://problem/12807090>
+
+        Reviewed by Alexey Proskuryakov.
+
+        The bitmap image used for print previews was being created respecting the print preview scale.
+        This doesn't make any sense, as the scale does not affect the size of the image required to
+        represent the previewed page. Instead, we should not scale the size, creating the buffer at a
+        size that is constant regardless of scale, and do the scaling when drawing *into* the buffer instead.
+
+        In some cases, this could cause an out-of-memory crash.
+
+        * UIProcess/API/mac/WKPrintingView.mm:
+        (-[WKPrintingView _drawPreview:]): Rename rect to scaledPrintingRect for clarity. Compute print
+        preview bitmap image size from the original non-scaled drawing size and the device scale factor.
+        Hand the image size to drawRectToImage. Draw the image without scaling, as it's always created
+        at the correct size. Don't compute the scale factor from the size of the image, we can know it from WebPageProxy.
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::drawRectToImage): Add a parameter to drawRectForImage, imageSize.
+        * UIProcess/WebPageProxy.h:
+        (WebPageProxy): Add a parameter to drawRectForImage, imageSize.
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::drawRectToImage): Use imageSize to size our buffer, and scale up to make the printing rect
+        fit in the bitmap's bounds.
+        * WebProcess/WebPage/WebPage.h:
+        (WebPage): Add a parameter to drawRectForImage, imageSize.
+        * WebProcess/WebPage/WebPage.messages.in: Add a parameter to drawRectForImage, imageSize.
+
 2012-12-20  Helder Correia  <[email protected]>
 
         [CoordGfx] Make single-argument AreaAllocator constructors explicit

Modified: trunk/Source/WebKit2/UIProcess/API/mac/WKPrintingView.mm (138355 => 138356)


--- trunk/Source/WebKit2/UIProcess/API/mac/WKPrintingView.mm	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/UIProcess/API/mac/WKPrintingView.mm	2012-12-21 09:54:09 UTC (rev 138356)
@@ -472,13 +472,15 @@
 {
     ASSERT(isMainThread());
 
-    IntRect rect(nsRect);
-    rect.scale(1 / _totalScaleFactorForPrinting);
-    HashMap<WebCore::IntRect, RefPtr<ShareableBitmap> >::iterator pagePreviewIterator = _pagePreviews.find(rect);
+    IntRect scaledPrintingRect(nsRect);
+    scaledPrintingRect.scale(1 / _totalScaleFactorForPrinting);
+    IntSize imageSize(nsRect.size);
+    imageSize.scale(_webFrame->page()->deviceScaleFactor());
+    HashMap<WebCore::IntRect, RefPtr<ShareableBitmap> >::iterator pagePreviewIterator = _pagePreviews.find(scaledPrintingRect);
     if (pagePreviewIterator == _pagePreviews.end())  {
         // It's too early to ask for page preview if we don't even know page size and scale.
         if ([self _hasPageRects]) {
-            if (uint64_t existingCallback = [self _expectedPreviewCallbackForRect:rect]) {
+            if (uint64_t existingCallback = [self _expectedPreviewCallbackForRect:scaledPrintingRect]) {
                 // We've already asked for a preview of this page, and are waiting for response.
                 // There is no need to ask again.
                 _latestExpectedPreviewCallback = existingCallback;
@@ -493,12 +495,12 @@
                 IPCCallbackContext* context = new IPCCallbackContext;
                 RefPtr<ImageCallback> callback = ImageCallback::create(context, pageDidDrawToImage);
                 _latestExpectedPreviewCallback = callback->callbackID();
-                _expectedPreviewCallbacks.add(_latestExpectedPreviewCallback, rect);
+                _expectedPreviewCallbacks.add(_latestExpectedPreviewCallback, scaledPrintingRect);
 
                 context->view = self;
                 context->callbackID = callback->callbackID();
 
-                _webFrame->page()->drawRectToImage(_webFrame.get(), PrintInfo([_printOperation printInfo]), rect, callback.get());
+                _webFrame->page()->drawRectToImage(_webFrame.get(), PrintInfo([_printOperation printInfo]), scaledPrintingRect, imageSize, callback.get());
                 return;
             }
         }
@@ -513,14 +515,7 @@
     GraphicsContext context(cgContext);
     GraphicsContextStateSaver stateSaver(context);
 
-    context.translate(nsRect.origin.x, nsRect.origin.y);
-    context.scale(FloatSize(_totalScaleFactorForPrinting, _totalScaleFactorForPrinting));
-
-    // FIXME: Should we get the WebPage's deviceScaleFactor from here instead?
-    const IntRect& imageBounds = bitmap->bounds();
-    float imageScaleFactor = imageBounds.width() / rect.width();
-
-    bitmap->paint(context, imageScaleFactor, IntPoint(), imageBounds);
+    bitmap->paint(context, _webFrame->page()->deviceScaleFactor(), IntPoint(nsRect.origin), bitmap->bounds());
 }
 
 - (void)drawRect:(NSRect)nsRect

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp (138355 => 138356)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.cpp	2012-12-21 09:54:09 UTC (rev 138356)
@@ -4049,7 +4049,7 @@
 }
 
 #if PLATFORM(MAC) || PLATFORM(WIN)
-void WebPageProxy::drawRectToImage(WebFrameProxy* frame, const PrintInfo& printInfo, const IntRect& rect, PassRefPtr<ImageCallback> prpCallback)
+void WebPageProxy::drawRectToImage(WebFrameProxy* frame, const PrintInfo& printInfo, const IntRect& rect, const WebCore::IntSize& imageSize, PassRefPtr<ImageCallback> prpCallback)
 {
     RefPtr<ImageCallback> callback = prpCallback;
     if (!isValid()) {
@@ -4059,7 +4059,7 @@
     
     uint64_t callbackID = callback->callbackID();
     m_imageCallbacks.set(callbackID, callback.get());
-    m_process->send(Messages::WebPage::DrawRectToImage(frame->frameID(), printInfo, rect, callbackID), m_pageID, m_isPerformingDOMPrintOperation ? CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply : 0);
+    m_process->send(Messages::WebPage::DrawRectToImage(frame->frameID(), printInfo, rect, imageSize, callbackID), m_pageID, m_isPerformingDOMPrintOperation ? CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply : 0);
 }
 
 void WebPageProxy::drawPagesToPDF(WebFrameProxy* frame, const PrintInfo& printInfo, uint32_t first, uint32_t count, PassRefPtr<DataCallback> prpCallback)

Modified: trunk/Source/WebKit2/UIProcess/WebPageProxy.h (138355 => 138356)


--- trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/UIProcess/WebPageProxy.h	2012-12-21 09:54:09 UTC (rev 138356)
@@ -700,7 +700,7 @@
     void endPrinting();
     void computePagesForPrinting(WebFrameProxy*, const PrintInfo&, PassRefPtr<ComputedPagesCallback>);
 #if PLATFORM(MAC) || PLATFORM(WIN)
-    void drawRectToImage(WebFrameProxy*, const PrintInfo&, const WebCore::IntRect&, PassRefPtr<ImageCallback>);
+    void drawRectToImage(WebFrameProxy*, const PrintInfo&, const WebCore::IntRect&, const WebCore::IntSize&, PassRefPtr<ImageCallback>);
     void drawPagesToPDF(WebFrameProxy*, const PrintInfo&, uint32_t first, uint32_t count, PassRefPtr<DataCallback>);
 #elif PLATFORM(GTK)
     void drawPagesForPrinting(WebFrameProxy*, const PrintInfo&, PassRefPtr<PrintFinishedCallback>);

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp (138355 => 138356)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp	2012-12-21 09:54:09 UTC (rev 138356)
@@ -3262,7 +3262,7 @@
 }
 
 #if PLATFORM(MAC) || PLATFORM(WIN)
-void WebPage::drawRectToImage(uint64_t frameID, const PrintInfo& printInfo, const WebCore::IntRect& rect, uint64_t callbackID)
+void WebPage::drawRectToImage(uint64_t frameID, const PrintInfo& printInfo, const WebCore::IntRect& rect, const WebCore::IntSize& imageSize, uint64_t callbackID)
 {
     WebFrame* frame = WebProcess::shared().webFrame(frameID);
     Frame* coreFrame = frame ? frame->coreFrame() : 0;
@@ -3277,9 +3277,12 @@
         ASSERT(coreFrame->document()->printing());
 #endif
 
-        RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(rect.size(), ShareableBitmap::SupportsAlpha);
+        RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(imageSize, ShareableBitmap::SupportsAlpha);
         OwnPtr<GraphicsContext> graphicsContext = bitmap->createGraphicsContext();
 
+        float printingScale = static_cast<float>(imageSize.width()) / rect.width();
+        graphicsContext->scale(FloatSize(printingScale, printingScale));
+
 #if PLATFORM(MAC)
         if (RetainPtr<PDFDocument> pdfDocument = pdfDocumentForPrintingFrame(coreFrame)) {
             ASSERT(!m_printContext);

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h (138355 => 138356)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h	2012-12-21 09:54:09 UTC (rev 138356)
@@ -520,7 +520,7 @@
     void endPrinting();
     void computePagesForPrinting(uint64_t frameID, const PrintInfo&, uint64_t callbackID);
 #if PLATFORM(MAC) || PLATFORM(WIN)
-    void drawRectToImage(uint64_t frameID, const PrintInfo&, const WebCore::IntRect&, uint64_t callbackID);
+    void drawRectToImage(uint64_t frameID, const PrintInfo&, const WebCore::IntRect&, const WebCore::IntSize&, uint64_t callbackID);
     void drawPagesToPDF(uint64_t frameID, const PrintInfo&, uint32_t first, uint32_t count, uint64_t callbackID);
 #elif PLATFORM(GTK)
     void drawPagesForPrinting(uint64_t frameID, const PrintInfo&, uint64_t callbackID);

Modified: trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in (138355 => 138356)


--- trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2012-12-21 09:33:03 UTC (rev 138355)
+++ trunk/Source/WebKit2/WebProcess/WebPage/WebPage.messages.in	2012-12-21 09:54:09 UTC (rev 138356)
@@ -217,7 +217,7 @@
     EndPrinting()
     ComputePagesForPrinting(uint64_t frameID, WebKit::PrintInfo printInfo, uint64_t callbackID)
 #if PLATFORM(MAC) || PLATFORM(WIN)
-    DrawRectToImage(uint64_t frameID, WebKit::PrintInfo printInfo, WebCore::IntRect rect, uint64_t callbackID)
+    DrawRectToImage(uint64_t frameID, WebKit::PrintInfo printInfo, WebCore::IntRect rect, WebCore::IntSize imageSize, uint64_t callbackID)
     DrawPagesToPDF(uint64_t frameID, WebKit::PrintInfo printInfo, uint32_t first, uint32_t count, uint64_t callbackID)
 #endif
 #if PLATFORM(GTK)
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to