Skip to content

Commit c879c2c

Browse files
committed
fixing too much memory consumption in MacOSGrabber
1 parent 1378bea commit c879c2c

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

Software/grab/MacOSGrabber.cpp

+32-13
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@
3434
MacOSGrabber::MacOSGrabber(QObject *parent, QList<QRgb> *grabResult, QList<GrabWidget *> *grabAreasGeometry):
3535
TimeredGrabber(parent, grabResult, grabAreasGeometry)
3636
{
37+
_imageBuf = NULL;
38+
_imageBufSize = 0;
39+
_colorSpace = CGColorSpaceCreateDeviceRGB();
40+
_context = NULL;
41+
_contextHeight = 0;
42+
_contextWidth = 0;
3743
}
3844

3945
MacOSGrabber::~MacOSGrabber()
4046
{
47+
CGColorSpaceRelease(_colorSpace);
48+
if(_imageBuf)
49+
free(_imageBuf);
50+
if(_context)
51+
CGContextRelease(_context);
4152
}
4253

4354
const char * MacOSGrabber::getName()
@@ -50,28 +61,36 @@ void MacOSGrabber::updateGrabMonitor(QWidget *widget)
5061
Q_UNUSED(widget);
5162
}
5263

53-
void imageCleanup(void *data) {
54-
free(data);
55-
}
56-
5764
QImage * MacOSGrabber::toImage(CGImageRef imageRef)
5865
{
5966
size_t width = CGImageGetWidth(imageRef);
6067
size_t height = CGImageGetHeight(imageRef);
61-
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
62-
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
68+
size_t new_buf_size = height * width * 4;
69+
if (new_buf_size > _imageBufSize) {
70+
DEBUG_LOW_LEVEL << Q_FUNC_INFO << "new width = " << width << " new height = " << height;
71+
if (_imageBuf)
72+
free(_imageBuf);
73+
_imageBuf = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
74+
_imageBufSize = new_buf_size;
75+
}
76+
6377
size_t bytesPerPixel = 4;
6478
size_t bytesPerRow = bytesPerPixel * width;
6579
size_t bitsPerComponent = 8;
66-
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
67-
bitsPerComponent, bytesPerRow, colorSpace,
68-
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
69-
CGColorSpaceRelease(colorSpace);
80+
if (width != _contextWidth || height != _contextHeight) {
81+
if(_context)
82+
CGContextRelease(_context);
83+
84+
_context = CGBitmapContextCreate(_imageBuf, width, height,
85+
bitsPerComponent, bytesPerRow, _colorSpace,
86+
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
87+
_contextWidth = width;
88+
_contextHeight = height;
89+
}
7090

71-
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
72-
CGContextRelease(context);
91+
CGContextDrawImage(_context, CGRectMake(0, 0, _contextWidth, _contextHeight), imageRef);
7392

74-
QImage * result = new QImage(rawData, width, height, QImage::Format_ARGB32, imageCleanup);
93+
QImage * result = new QImage(_imageBuf, _contextWidth, _contextHeight, QImage::Format_RGB32);
7594

7695
return result;
7796
}

Software/grab/include/MacOSGrabber.hpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131

3232
#include "TimeredGrabber.hpp"
3333

34-
struct CGImage;
35-
typedef CGImage *CGImageRef;
34+
#include <CoreGraphics/CGColorSpace.h>
35+
#include <CoreGraphics/CGContext.h>
36+
#include <CoreGraphics/CGImage.h>
37+
3638

3739
class MacOSGrabber : public TimeredGrabber
3840
{
@@ -53,6 +55,13 @@ public slots:
5355
QRgb getColor(QImage * image, const QWidget * grabme);
5456
QRgb getColor(QImage * image, int x, int y, int width, int height);
5557
QImage * toImage(CGImageRef);
58+
59+
unsigned char *_imageBuf;
60+
size_t _imageBufSize;
61+
size_t _contextWidth;
62+
size_t _contextHeight;
63+
CGColorSpaceRef _colorSpace;
64+
CGContextRef _context;
5665
};
5766

5867
#endif // MAC_OS_CG_GRAB_SUPPORT

0 commit comments

Comments
 (0)