summaryrefslogtreecommitdiffstats
path: root/tools/qmeegographicssystemhelper/mlivepixmap.cpp
blob: 2f7abd7709812f2d375d0935ef7c03be52ef2198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "mlivepixmap.h"
#include "../private/qimage_p.h"
#include "../private/qpixmap_raster_p.h"
#include "mlivepixmap_p.h"
#include "mliveimage_p.h"
#include <QSharedMemory>

/* MLivePixmapPrivate */

MLivePixmapPrivate::MLivePixmapPrivate() : shm(0), shmSerial(0), owns(true), parentImage(0)
{
}

void MLivePixmapPrivate::copyBackFrom(const void *raw)
{
    Q_Q(MLivePixmap);
    
    q->detach();
    shm->lock();
    uchar *dest = ((uchar *) shm->data()) + (2 * sizeof(int));
    memcpy(dest, raw, q->width() * q->height() * 4);
    shm->unlock();
}

MLivePixmapPrivate::~MLivePixmapPrivate()
{
    Q_Q(MLivePixmap);
    
    if (parentImage)
        parentImage->d_ptr->detachPixmap(q);

    if (shm)
        shm->detach();
        
    if (owns)
        delete shm;        
}

/* MLivePixmap */

MLivePixmap::MLivePixmap(QPixmapData *p) : QPixmap(p), d_ptr(new MLivePixmapPrivate())
{
    Q_D(MLivePixmap);
    d->q_ptr = this;
}

MLivePixmap* MLivePixmap::fromLiveImage(MLiveImage *liveImage)
{
    static int counter = 100;
    QSharedMemory *shm = NULL;
    uchar* imgData = NULL;
    int *header = NULL;
    int w = liveImage->width();
    int h = liveImage->height();
    
    counter++;
    shm = new QSharedMemory(QString("MLivePixmap%1").arg(counter));
    shm->create((w * h * 4) + 2 * sizeof(int)); // +2 to store width & height
    shm->attach();
    
    imgData = ((uchar *) shm->data()) + (2 * sizeof(int));
    header = (int *) shm->data();
    
    header[0] = w;
    header[1] = h;
    
    QImage img(imgData, w, h, QImage::Format_ARGB32_Premultiplied);
    
    QPixmapData *pmd = new QRasterPixmapData(QPixmapData::PixmapType);
    pmd->fromImage(img, Qt::NoOpaqueDetection);
    
    MLivePixmap *livePixmap = new MLivePixmap(pmd);
    livePixmap->d_ptr->shm = shm;
    livePixmap->d_ptr->owns = true;
    livePixmap->d_ptr->shmSerial = counter;
    livePixmap->d_ptr->parentImage = liveImage;
    
    liveImage->d_ptr->attachPixmap(livePixmap);

    return livePixmap;    
}

MLivePixmap* MLivePixmap::fromHandle(Qt::HANDLE handle)
{
    QSharedMemory *shm = NULL;
    int *header;
    int width;
    int height;
    uchar* imgData;
    
    shm = new QSharedMemory(QString("MLivePixmap%1").arg(handle));
    shm->attach();
    
    shm->lock();
    header = (int *) shm->data();
    width = header[0];
    height = header[1];
    shm->unlock();
    
    imgData = ((uchar *) shm->data()) + (2 * sizeof(int));
    QImage img(imgData, width, height, QImage::Format_ARGB32_Premultiplied);
    
    QPixmapData *pmd = new QRasterPixmapData(QPixmapData::PixmapType);
    pmd->fromImage(img, Qt::NoOpaqueDetection);

    MLivePixmap *livePixmap = new MLivePixmap(pmd);
    livePixmap->d_ptr->shm = shm;
    livePixmap->d_ptr->owns = false;
    livePixmap->d_ptr->shmSerial = handle;

    return livePixmap;
}

MLivePixmap::~MLivePixmap()
{
}

Qt::HANDLE MLivePixmap::handle()
{
    Q_D(MLivePixmap);
    return d->shmSerial;
}