| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtGui module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qimagepixmapcleanuphooks_p.h" | 
|---|
| 43 | #include "private/qpixmapdata_p.h" | 
|---|
| 44 | #include "private/qimage_p.h" | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | QT_BEGIN_NAMESPACE | 
|---|
| 48 |  | 
|---|
| 49 | // Legacy, single instance hooks: ### Qt 5: remove | 
|---|
| 50 | typedef void (*_qt_pixmap_cleanup_hook)(int); | 
|---|
| 51 | typedef void (*_qt_pixmap_cleanup_hook_64)(qint64); | 
|---|
| 52 | typedef void (*_qt_image_cleanup_hook)(int); | 
|---|
| 53 | Q_GUI_EXPORT _qt_pixmap_cleanup_hook qt_pixmap_cleanup_hook = 0; | 
|---|
| 54 | Q_GUI_EXPORT _qt_pixmap_cleanup_hook_64 qt_pixmap_cleanup_hook_64 = 0; | 
|---|
| 55 | Q_GUI_EXPORT _qt_image_cleanup_hook qt_image_cleanup_hook = 0; | 
|---|
| 56 | Q_GUI_EXPORT _qt_image_cleanup_hook_64 qt_image_cleanup_hook_64 = 0; | 
|---|
| 57 |  | 
|---|
| 58 | Q_GLOBAL_STATIC(QImagePixmapCleanupHooks, qt_image_and_pixmap_cleanup_hooks) | 
|---|
| 59 |  | 
|---|
| 60 | QImagePixmapCleanupHooks *QImagePixmapCleanupHooks::instance() | 
|---|
| 61 | { | 
|---|
| 62 | return qt_image_and_pixmap_cleanup_hooks(); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void QImagePixmapCleanupHooks::addPixmapDataModificationHook(_qt_pixmap_cleanup_hook_pmd hook) | 
|---|
| 66 | { | 
|---|
| 67 | pixmapModificationHooks.append(hook); | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | void QImagePixmapCleanupHooks::addPixmapDataDestructionHook(_qt_pixmap_cleanup_hook_pmd hook) | 
|---|
| 71 | { | 
|---|
| 72 | pixmapDestructionHooks.append(hook); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | void QImagePixmapCleanupHooks::addImageHook(_qt_image_cleanup_hook_64 hook) | 
|---|
| 77 | { | 
|---|
| 78 | imageHooks.append(hook); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | void QImagePixmapCleanupHooks::removePixmapDataModificationHook(_qt_pixmap_cleanup_hook_pmd hook) | 
|---|
| 82 | { | 
|---|
| 83 | pixmapModificationHooks.removeAll(hook); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void QImagePixmapCleanupHooks::removePixmapDataDestructionHook(_qt_pixmap_cleanup_hook_pmd hook) | 
|---|
| 87 | { | 
|---|
| 88 | pixmapDestructionHooks.removeAll(hook); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void QImagePixmapCleanupHooks::removeImageHook(_qt_image_cleanup_hook_64 hook) | 
|---|
| 92 | { | 
|---|
| 93 | imageHooks.removeAll(hook); | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void QImagePixmapCleanupHooks::executePixmapDataModificationHooks(QPixmapData* pmd) | 
|---|
| 97 | { | 
|---|
| 98 | QImagePixmapCleanupHooks *h = qt_image_and_pixmap_cleanup_hooks(); | 
|---|
| 99 | // the global destructor for the pixmap and image hooks might have | 
|---|
| 100 | // been called already if the app is "leaking" global | 
|---|
| 101 | // pixmaps/images | 
|---|
| 102 | if (!h) | 
|---|
| 103 | return; | 
|---|
| 104 | for (int i = 0; i < h->pixmapModificationHooks.count(); ++i) | 
|---|
| 105 | h->pixmapModificationHooks[i](pmd); | 
|---|
| 106 |  | 
|---|
| 107 | if (qt_pixmap_cleanup_hook_64) | 
|---|
| 108 | qt_pixmap_cleanup_hook_64(pmd->cacheKey()); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | void QImagePixmapCleanupHooks::executePixmapDataDestructionHooks(QPixmapData* pmd) | 
|---|
| 112 | { | 
|---|
| 113 | QImagePixmapCleanupHooks *h = qt_image_and_pixmap_cleanup_hooks(); | 
|---|
| 114 | // the global destructor for the pixmap and image hooks might have | 
|---|
| 115 | // been called already if the app is "leaking" global | 
|---|
| 116 | // pixmaps/images | 
|---|
| 117 | if (!h) | 
|---|
| 118 | return; | 
|---|
| 119 | for (int i = 0; i < h->pixmapDestructionHooks.count(); ++i) | 
|---|
| 120 | h->pixmapDestructionHooks[i](pmd); | 
|---|
| 121 |  | 
|---|
| 122 | if (qt_pixmap_cleanup_hook_64) | 
|---|
| 123 | qt_pixmap_cleanup_hook_64(pmd->cacheKey()); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | void QImagePixmapCleanupHooks::executeImageHooks(qint64 key) | 
|---|
| 127 | { | 
|---|
| 128 | for (int i = 0; i < qt_image_and_pixmap_cleanup_hooks()->imageHooks.count(); ++i) | 
|---|
| 129 | qt_image_and_pixmap_cleanup_hooks()->imageHooks[i](key); | 
|---|
| 130 |  | 
|---|
| 131 | if (qt_image_cleanup_hook_64) | 
|---|
| 132 | qt_image_cleanup_hook_64(key); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | void QImagePixmapCleanupHooks::enableCleanupHooks(QPixmapData *pixmapData) | 
|---|
| 137 | { | 
|---|
| 138 | pixmapData->is_cached = true; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void QImagePixmapCleanupHooks::enableCleanupHooks(const QPixmap &pixmap) | 
|---|
| 142 | { | 
|---|
| 143 | enableCleanupHooks(const_cast<QPixmap &>(pixmap).data_ptr().data()); | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void QImagePixmapCleanupHooks::enableCleanupHooks(const QImage &image) | 
|---|
| 147 | { | 
|---|
| 148 | const_cast<QImage &>(image).data_ptr()->is_cached = true; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | bool QImagePixmapCleanupHooks::isImageCached(const QImage &image) | 
|---|
| 152 | { | 
|---|
| 153 | return const_cast<QImage &>(image).data_ptr()->is_cached; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | bool QImagePixmapCleanupHooks::isPixmapCached(const QPixmap &pixmap) | 
|---|
| 157 | { | 
|---|
| 158 | return const_cast<QPixmap&>(pixmap).data_ptr().data()->is_cached; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 | QT_END_NAMESPACE | 
|---|