Changeset 54616 in webkit


Ignore:
Timestamp:
Feb 10, 2010 1:05:55 PM (14 years ago)
Author:
Adam Roben
Message:

Optimize ImageBuffer::toDataURL's CG implementation

There was some unused code that could have been removed in r31830 when
this function was changed not to flip the CG image anymore.

Fixes <http://webkit.org/b/34808> ImageBuffer::toDataURL allocates
unnecessary memory under CoreGraphics

Reviewed by Sam Weinig.

  • platform/graphics/cg/ImageBufferCG.cpp:

(WebCore::ImageBuffer::toDataURL): Don't allocate an unused CGImageRef
and buffer, and switch to the new overload of base64Encode that
doesn't require us to copy all the image data into a Vector first.

  • platform/text/Base64.cpp:

(WebCore::base64Encode):

  • platform/text/Base64.h:

Added an overload that takes a raw data pointer and length, just like
we have for base64Decode. The overload that takes a Vector as input
just calls through to the new overload.

Location:
trunk/WebCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r54615 r54616  
     12010-02-10  Adam Roben  <aroben@apple.com>
     2
     3        Optimize ImageBuffer::toDataURL's CG implementation
     4
     5        There was some unused code that could have been removed in r31830 when
     6        this function was changed not to flip the CG image anymore.
     7
     8        Fixes <http://webkit.org/b/34808> ImageBuffer::toDataURL allocates
     9        unnecessary memory under CoreGraphics
     10
     11        Reviewed by Sam Weinig.
     12
     13        * platform/graphics/cg/ImageBufferCG.cpp:
     14        (WebCore::ImageBuffer::toDataURL): Don't allocate an unused CGImageRef
     15        and buffer, and switch to the new overload of base64Encode that
     16        doesn't require us to copy all the image data into a Vector first.
     17
     18        * platform/text/Base64.cpp:
     19        (WebCore::base64Encode):
     20        * platform/text/Base64.h:
     21        Added an overload that takes a raw data pointer and length, just like
     22        we have for base64Decode. The overload that takes a Vector as input
     23        just calls through to the new overload.
     24
    1252010-02-10  Dan Bernstein  <mitz@apple.com>
    226
  • trunk/WebCore/platform/graphics/cg/ImageBufferCG.cpp

    r47585 r54616  
    281281        return "data:,";
    282282
    283     size_t width = CGImageGetWidth(image.get());
    284     size_t height = CGImageGetHeight(image.get());
    285 
    286     OwnArrayPtr<uint32_t> imageData(new uint32_t[width * height]);
    287     if (!imageData)
     283    RetainPtr<CFMutableDataRef> data(AdoptCF, CFDataCreateMutable(kCFAllocatorDefault, 0));
     284    if (!data)
    288285        return "data:,";
    289    
    290     RetainPtr<CGImageRef> transformedImage(AdoptCF, CGBitmapContextCreateImage(context()->platformContext()));
    291     if (!transformedImage)
     286
     287    RetainPtr<CGImageDestinationRef> destination(AdoptCF, CGImageDestinationCreateWithData(data.get(), utiFromMIMEType(mimeType).get(), 1, 0));
     288    if (!destination)
    292289        return "data:,";
    293290
    294     RetainPtr<CFMutableDataRef> transformedImageData(AdoptCF, CFDataCreateMutable(kCFAllocatorDefault, 0));
    295     if (!transformedImageData)
    296         return "data:,";
    297 
    298     RetainPtr<CGImageDestinationRef> imageDestination(AdoptCF, CGImageDestinationCreateWithData(transformedImageData.get(),
    299         utiFromMIMEType(mimeType).get(), 1, 0));
    300     if (!imageDestination)
    301         return "data:,";
    302 
    303     CGImageDestinationAddImage(imageDestination.get(), transformedImage.get(), 0);
    304     CGImageDestinationFinalize(imageDestination.get());
    305 
    306     Vector<char> in;
    307     in.append(CFDataGetBytePtr(transformedImageData.get()), CFDataGetLength(transformedImageData.get()));
     291    CGImageDestinationAddImage(destination.get(), image.get(), 0);
     292    CGImageDestinationFinalize(destination.get());
    308293
    309294    Vector<char> out;
    310     base64Encode(in, out);
     295    base64Encode(reinterpret_cast<const char*>(CFDataGetBytePtr(data.get())), CFDataGetLength(data.get()), out);
    311296    out.append('\0');
    312297
  • trunk/WebCore/platform/text/Base64.cpp

    r42002 r54616  
    6363void base64Encode(const Vector<char>& in, Vector<char>& out, bool insertLFs)
    6464{
     65    base64Encode(in.data(), in.size(), out, insertLFs);
     66}
     67
     68void base64Encode(const char* data, unsigned len, Vector<char>& out, bool insertLFs)
     69{
    6570    out.clear();
    66     if (in.isEmpty())
     71    if (!len)
    6772        return;
    6873
     
    7176    // Rather than being perfectly precise, this is a bit conservative.
    7277    const unsigned maxInputBufferSize = UINT_MAX / 77 * 76 / 4 * 3 - 2;
    73     if (in.size() > maxInputBufferSize)
     78    if (len > maxInputBufferSize)
    7479        return;
    7580
    7681    unsigned sidx = 0;
    7782    unsigned didx = 0;
    78     const char* data = in.data();
    79     const unsigned len = in.size();
    8083
    8184    unsigned out_len = ((len + 2) / 3) * 4;
  • trunk/WebCore/platform/text/Base64.h

    r28234 r54616  
    3232
    3333void base64Encode(const Vector<char>&, Vector<char>&, bool insertLFs = false);
     34void base64Encode(const char*, unsigned, Vector<char>&, bool insertLFs = false);
    3435
    3536// this decoder is not general purpose - it returns an error if it encounters a linefeed, as needed for window.atob
Note: See TracChangeset for help on using the changeset viewer.