VirtualBox

Changeset 22813 in vbox


Ignore:
Timestamp:
Sep 7, 2009 2:27:59 PM (15 years ago)
Author:
vboxsync
Message:

FE/Qt4-OSX: Make sure that CGImageRef's remains valid for there life time when they are created out of QPixmaps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/darwin/VBoxUtils-darwin.cpp

    r22313 r22813  
    2222#include "VBoxUtils-darwin.h"
    2323
    24 #include <iprt/assert.h>
     24#include <iprt/mem.h>
    2525
    2626#include <QApplication>
     
    222222CGImageRef darwinToCGImageRef (const QPixmap *aPixmap)
    223223{
    224     return aPixmap->toMacCGImageRef();
     224    /* It seems Qt releases the memory to an returned CGImageRef when the
     225     * associated QPixmap is destroyed. This shouldn't happen as long a
     226     * CGImageRef has a retrain count. As a workaround we make a real copy. */
     227    int bitmapBytesPerRow = aPixmap->width() * 4;
     228    int bitmapByteCount = (bitmapBytesPerRow * aPixmap->height());
     229    /* Create a memory block for the temporary image. It is initialized by zero
     230     * which means black & zero alpha. */
     231    void *pBitmapData = RTMemAllocZ (bitmapByteCount);
     232    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
     233    /* Create a context to paint on */
     234    CGContextRef ctx = CGBitmapContextCreate (pBitmapData,
     235                                              aPixmap->width(),
     236                                              aPixmap->height(),
     237                                              8,
     238                                              bitmapBytesPerRow,
     239                                              cs,
     240                                              kCGImageAlphaPremultipliedFirst);
     241    /* Get the CGImageRef from Qt */
     242    CGImageRef qtPixmap = aPixmap->toMacCGImageRef();
     243    /* Draw the image from Qt & convert the context back to a new CGImageRef. */
     244    CGContextDrawImage (ctx, CGRectMake (0, 0, aPixmap->width(), aPixmap->height()), qtPixmap);
     245    CGImageRef newImage = CGBitmapContextCreateImage (ctx);
     246    /* Now release all used resources */
     247    CGImageRelease (qtPixmap);
     248    CGContextRelease (ctx);
     249    CGColorSpaceRelease (cs);
     250    RTMemFree (pBitmapData);
     251
     252    /* Return the new CGImageRef */
     253    return newImage;
    225254}
    226255
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette