summaryrefslogtreecommitdiffstats
path: root/abs/core/mythtv/stable-29/mythtv/0287-MythUiImage-Don-t-block-UI-when-exiting-screens.patch
blob: 5bf637b33013da3645529c0ba5d0a7a9d8be3d5d (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
From 5ee658b482ad634706c21d20d4f8fd15828fc1d8 Mon Sep 17 00:00:00 2001
From: Roger Siddons <dizygotheca@ntlworld.com>
Date: Mon, 12 Oct 2015 14:54:36 +0100
Subject: [PATCH 11/13] MythUiImage: Don't block UI when exiting screens

When deleting MythUIImages the UI blocks until all image loading threads complete.

This patch removes the need for that by introducing an image proxy for the load threads that
prohibits access to a deleted image.

Note the ImageLoadEvent is unchanged but has been moved to resolve dependancies. It still
contains a (potentially dangerous) pointer to the deleted MythUIImage, which IMO is redundant and should be removed.

diff --git a/mythtv/libs/libmythui/mythuiimage.cpp b/mythtv/libs/libmythui/mythuiimage.cpp
index fc067f7..a937358 100644
--- a/mythtv/libs/libmythui/mythuiimage.cpp
+++ src/mythtv/libs/libmythui/mythuiimage.cpp
@@ -121,25 +121,104 @@ void ImageProperties::SetMaskImage(MythImage *image)
 }
 
 /*!
+ * \class ImageLoadEvent
+ */
+class ImageLoadEvent : public QEvent
+{
+  public:
+    ImageLoadEvent(const MythUIImage *parent, MythImage *image,
+                   const QString &basefile, const QString &filename,
+                   int number, bool aborted)
+        : QEvent(kEventType),
+          m_parent(parent), m_image(image), m_basefile(basefile),
+          m_filename(filename), m_number(number),
+          m_images(NULL), m_aborted(aborted) { }
+
+    ImageLoadEvent(const MythUIImage *parent, AnimationFrames *frames,
+                   const QString &basefile,
+                   const QString &filename, bool aborted)
+        : QEvent(kEventType),
+          m_parent(parent), m_image(NULL), m_basefile(basefile),
+          m_filename(filename), m_number(0),
+          m_images(frames), m_aborted(aborted) { }
+
+    const MythUIImage *GetParent() const        { return m_parent; }
+    MythImage *GetImage() const                 { return m_image; }
+    const QString GetBasefile() const           { return m_basefile; }
+    const QString GetFilename() const           { return m_filename; }
+    int GetNumber() const                       { return m_number; }
+    AnimationFrames *GetAnimationFrames() const { return m_images; }
+    bool GetAbortState() const                  { return m_aborted; }
+
+    static Type kEventType;
+
+  private:
+    const MythUIImage *m_parent;
+    MythImage         *m_image;
+    QString            m_basefile;
+    QString            m_filename;
+    int                m_number;
+
+    // Animated Images
+    AnimationFrames  *m_images;
+
+    // Image Load
+    bool             m_aborted;
+};
+
+QEvent::Type ImageLoadEvent::kEventType =
+    (QEvent::Type) QEvent::registerEventType();
+
+/*!
+   \brief Guards access to the image by Load threads. If the image is deleted
+   by the UI this proxy persists until all its load threads complete.
+ */
+class ImageProxy
+{
+public:
+    ImageProxy(MythUIImage *image) : m_mutex(), m_image(image) {}
+
+    MythUIImage *GetImage() { QMutexLocker lock(&m_mutex); return m_image; }
+    void         Orphan()   { QMutexLocker lock(&m_mutex); m_image = NULL; }
+    void         PostEvent(ImageLoadEvent *event)
+    {
+        QMutexLocker lock(&m_mutex);
+        if (m_image)
+            QCoreApplication::postEvent(m_image, event);
+        else
+            LOG(VB_GUI | VB_FILE, LOG_DEBUG,
+                QString("ImageLoadThread: Discarding load of %1")
+                .arg(event->GetFilename()));
+    }
+
+private:
+    QMutex       m_mutex;
+    MythUIImage *m_image;
+};
+
+//! The proxy is shared by the image and all its load threads.
+typedef QSharedPointer<ImageProxy> ProxyPtr;
+
+/*!
  * \class ImageLoader
  */
 class ImageLoader
 {
   public:
-    ImageLoader() { };
-   ~ImageLoader() { };
+    ImageLoader() { }
+   ~ImageLoader() { }
 
-    static QHash<QString, const MythUIImage *> m_loadingImages;
-    static QMutex                        m_loadingImagesLock;
-    static QWaitCondition                m_loadingImagesCond;
+    static QHash<QString, const ImageProxy*> m_loadingImages;
+    static QMutex                            m_loadingImagesLock;
+    static QWaitCondition                    m_loadingImagesCond;
 
-    static bool PreLoad(const QString &cacheKey, const MythUIImage *uitype)
+    static bool PreLoad(const QString &cacheKey, const ProxyPtr &proxy)
     {
         m_loadingImagesLock.lock();
 
         // Check to see if the image is being loaded by us in another thread
         if ((m_loadingImages.contains(cacheKey)) &&
-            (m_loadingImages[cacheKey] == uitype))
+            (m_loadingImages[cacheKey] == proxy.data()))
         {
             LOG(VB_GUI | VB_FILE, LOG_DEBUG,
                 QString("ImageLoader::PreLoad(%1), this "
@@ -153,7 +232,7 @@ class ImageLoader
         while (m_loadingImages.contains(cacheKey))
             m_loadingImagesCond.wait(&m_loadingImagesLock);
 
-        m_loadingImages[cacheKey] = uitype;
+        m_loadingImages[cacheKey] = proxy.data();
         m_loadingImagesLock.unlock();
 
         return true;
@@ -229,15 +308,12 @@ class ImageLoader
                                  // Must be a copy for thread safety
                                 ImageProperties imProps,
                                 ImageCacheMode cacheMode,
-                                 // Included only to check address, could be
-                                 // replaced by generating a unique value for
-                                 // each MythUIImage object?
-                                const MythUIImage *parent,
+                                const ProxyPtr &proxy,
                                 bool &aborted,
                                 MythImageReader *imageReader = NULL)
     {
         QString cacheKey = GenImageLabel(imProps);
-        if (!PreLoad(cacheKey, parent))
+        if (!PreLoad(cacheKey, proxy))
         {
             aborted = true;
             return NULL;
@@ -390,10 +466,7 @@ class ImageLoader
                                                // Must be a copy for thread safety
                                               ImageProperties imProps,
                                               ImageCacheMode cacheMode,
-                                               // Included only to check address, could be
-                                               // replaced by generating a unique value for
-                                               // each MythUIImage object?
-                                              const MythUIImage *parent,
+                                              const ProxyPtr &proxy,
                                               bool &aborted)
     {
         QString filename = QString("frame-%1-") + imProps.filename;
@@ -411,7 +484,7 @@ class ImageLoader
             ImageProperties frameProps = imProps;
             frameProps.filename = frameFilename;
 
-            MythImage *im = LoadImage(painter, frameProps, cacheMode, parent,
+            MythImage *im = LoadImage(painter, frameProps, cacheMode, proxy,
                                       aborted, imageReader);
 
             if (!im)
@@ -428,58 +501,9 @@ class ImageLoader
 
 };
 
-QHash<QString, const MythUIImage *> ImageLoader::m_loadingImages;
-QMutex                              ImageLoader::m_loadingImagesLock;
-QWaitCondition                      ImageLoader::m_loadingImagesCond;
-
-/*!
- * \class ImageLoadEvent
- */
-class ImageLoadEvent : public QEvent
-{
-  public:
-    ImageLoadEvent(const MythUIImage *parent, MythImage *image,
-                   const QString &basefile, const QString &filename,
-                   int number, bool aborted)
-        : QEvent(kEventType),
-          m_parent(parent), m_image(image), m_basefile(basefile),
-          m_filename(filename), m_number(number),
-          m_images(NULL), m_aborted(aborted) { }
-
-    ImageLoadEvent(const MythUIImage *parent, AnimationFrames *frames,
-                   const QString &basefile,
-                   const QString &filename, bool aborted)
-        : QEvent(kEventType),
-          m_parent(parent), m_image(NULL), m_basefile(basefile),
-          m_filename(filename), m_number(0),
-          m_images(frames), m_aborted(aborted) { }
-
-    const MythUIImage *GetParent() const    { return m_parent; }
-    MythImage *GetImage() const       { return m_image; }
-    const QString GetBasefile() const { return m_basefile; }
-    const QString GetFilename() const { return m_filename; }
-    int GetNumber() const             { return m_number; }
-    AnimationFrames *GetAnimationFrames() const { return m_images; }
-    bool GetAbortState() const        { return m_aborted; }
-
-    static Type kEventType;
-
-  private:
-    const MythUIImage     *m_parent;
-    MythImage       *m_image;
-    QString          m_basefile;
-    QString          m_filename;
-    int              m_number;
-
-    // Animated Images
-    AnimationFrames  *m_images;
-
-    // Image Load
-    bool             m_aborted;
-};
-
-QEvent::Type ImageLoadEvent::kEventType =
-    (QEvent::Type) QEvent::registerEventType();
+QHash<QString, const ImageProxy*> ImageLoader::m_loadingImages;
+QMutex                            ImageLoader::m_loadingImagesLock;
+QWaitCondition                    ImageLoader::m_loadingImagesCond;
 
 /*!
 * \class ImageLoadThread
@@ -487,10 +511,10 @@ QEvent::Type ImageLoadEvent::kEventType =
 class ImageLoadThread : public QRunnable
 {
   public:
-    ImageLoadThread(const MythUIImage *parent, MythPainter *painter,
+    ImageLoadThread(const ProxyPtr &proxy, MythPainter *painter,
                     const ImageProperties &imProps, const QString &basefile,
                     int number, ImageCacheMode mode) :
-        m_parent(parent), m_painter(painter), m_imageProperties(imProps),
+        m_proxy(proxy), m_painter(painter), m_imageProperties(imProps),
         m_basefile(basefile), m_number(number), m_cacheMode(mode)
     {
     }
@@ -500,6 +524,14 @@ class ImageLoadThread : public QRunnable
         bool aborted = false;
         QString filename =  m_imageProperties.filename;
 
+        // Don't even bother if parent image no longer exists
+        if (!m_proxy || !m_proxy->GetImage())
+        {
+            LOG(VB_GUI | VB_FILE, LOG_DEBUG,
+                QString("ImageLoadThread: Ignoring load of %1").arg(filename));
+            return;
+        }
+
         // NOTE Do NOT use MythImageReader::supportsAnimation here, it defeats
         // the point of caching remote images
         if (ImageLoader::SupportsAnimation(filename))
@@ -508,17 +540,16 @@ class ImageLoadThread : public QRunnable
 
              frames = ImageLoader::LoadAnimatedImage(m_painter,
                                                      m_imageProperties,
-                                                     m_cacheMode, m_parent,
+                                                     m_cacheMode, m_proxy,
                                                      aborted);
 
              if (frames && frames->count() > 1)
              {
-                ImageLoadEvent *le = new ImageLoadEvent(m_parent, frames,
+                ImageLoadEvent *le = new ImageLoadEvent(m_proxy->GetImage(), frames,
                                                         m_basefile,
                                                         m_imageProperties.filename,
                                                         aborted);
-                QCoreApplication::postEvent(const_cast<MythUIImage*>(m_parent), le);
-
+                m_proxy->PostEvent(le);
                 return;
              }
              delete frames;
@@ -526,18 +557,19 @@ class ImageLoadThread : public QRunnable
 
         MythImage *image = ImageLoader::LoadImage(m_painter,
                                                     m_imageProperties,
-                                                    m_cacheMode, m_parent,
+                                                    m_cacheMode, m_proxy,
                                                     aborted);
 
-        ImageLoadEvent *le = new ImageLoadEvent(m_parent, image, m_basefile,
+        ImageLoadEvent *le = new ImageLoadEvent(m_proxy->GetImage(), image,
+                                                m_basefile,
                                                 m_imageProperties.filename,
                                                 m_number, aborted);
-        QCoreApplication::postEvent(const_cast<MythUIImage*>(m_parent), le);
+        m_proxy->PostEvent(le);
     }
 
 private:
-    const MythUIImage    *m_parent;
-    MythPainter       *m_painter;
+    const ProxyPtr  m_proxy;
+    MythPainter    *m_painter;
     ImageProperties m_imageProperties;
     QString         m_basefile;
     int             m_number;
@@ -549,13 +581,14 @@ class MythUIImagePrivate
 {
 public:
     explicit MythUIImagePrivate(MythUIImage *p)
-        : m_parent(p),            m_UpdateLock(QReadWriteLock::Recursive)
-    { };
-    ~MythUIImagePrivate() {};
-
-    MythUIImage *m_parent;
+        : m_parent(p), m_UpdateLock(QReadWriteLock::Recursive),
+          m_proxy(ProxyPtr(new ImageProxy(p)))
+    { }
+    ~MythUIImagePrivate() {}
 
+    MythUIImage   *m_parent;
     QReadWriteLock m_UpdateLock;
+    ProxyPtr       m_proxy;
 };
 
 /////////////////////////////////////////////////////////////////
@@ -609,13 +642,8 @@ MythUIImage::MythUIImage(MythUIType *parent, const QString &name)
 
 MythUIImage::~MythUIImage()
 {
-    // Wait until all image loading threads are complete or bad things
-    // may happen if this MythUIImage disappears when a queued thread
-    // needs it.
-    if (m_runningThreads > 0)
-    {
-        GetMythUI()->GetImageThreadPool()->waitForDone();
-    }
+    // Prevent any updates from running loads
+    d->m_proxy->Orphan();
 
     Clear();
 
@@ -695,8 +723,6 @@ void MythUIImage::Init(void)
     m_animationReverse = false;
     m_animatedImage = false;
 
-    m_runningThreads = 0;
-
     m_showingRandomImage = false;
 }
 
@@ -1081,9 +1107,8 @@ bool MythUIImage::Load(bool allowLoadInBackground, bool forceStat)
             LOG(VB_GUI | VB_FILE, LOG_DEBUG, LOC +
                 QString("Load(), spawning thread to load '%1'").arg(filename));
 
-            m_runningThreads++;
             ImageLoadThread *bImgThread;
-            bImgThread = new ImageLoadThread(this, GetPainter(),
+            bImgThread = new ImageLoadThread(d->m_proxy, GetPainter(),
                                              imProps,
                                              bFilename, i,
                                              static_cast<ImageCacheMode>(cacheMode2));
@@ -1102,7 +1127,7 @@ bool MythUIImage::Load(bool allowLoadInBackground, bool forceStat)
 
                 myFrames = ImageLoader::LoadAnimatedImage(GetPainter(), imProps,
                                         static_cast<ImageCacheMode>(cacheMode2),
-                                        this, aborted);
+                                        d->m_proxy, aborted);
 
                 // TODO We might want to handle an abort here more gracefully
                 if (aborted)
@@ -1121,7 +1146,7 @@ bool MythUIImage::Load(bool allowLoadInBackground, bool forceStat)
                 image = ImageLoader::LoadImage(GetPainter(),
                                                imProps,
                                                static_cast<ImageCacheMode>(cacheMode2),
-                                               this, aborted);
+                                               d->m_proxy, aborted);
 
                 // TODO We might want to handle an abort here more gracefully
                 if (aborted)
@@ -1604,8 +1629,6 @@ void MythUIImage::customEvent(QEvent *event)
         animationFrames = le->GetAnimationFrames();
         aborted         = le->GetAbortState();
 
-        m_runningThreads--;
-
         d->m_UpdateLock.lockForRead();
         QString propFilename = m_imageProperties.filename;
         d->m_UpdateLock.unlock();
diff --git a/mythtv/libs/libmythui/mythuiimage.h b/mythtv/libs/libmythui/mythuiimage.h
index 7847c4a..e5390a1 100644
--- a/mythtv/libs/libmythui/mythuiimage.h
+++ src/mythtv/libs/libmythui/mythuiimage.h
@@ -174,8 +174,6 @@ class MUI_PUBLIC MythUIImage : public MythUIType
 
     ImageProperties m_imageProperties;
 
-    int m_runningThreads;
-
     bool m_showingRandomImage;
     QString m_imageDirectory;
 
-- 
2.1.4