1/*
2 * Copyright (C) 2010, 2011, 2012, 2013 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19#ifndef SurfacePool_h
20#define SurfacePool_h
21
22#include "BackingStoreTile.h"
23#include "GraphicsContext.h"
24
25#include <BlackBerryPlatformGraphics.h>
26#include <BlackBerryPlatformPrimitives.h>
27#include <pthread.h>
28#include <set>
29#include <wtf/Vector.h>
30
31namespace BlackBerry {
32namespace WebKit {
33
34class SurfacePool {
35public:
36    static SurfacePool* globalSurfacePool();
37
38    void initialize(const BlackBerry::Platform::IntSize&);
39
40    int isActive() const { return !m_tileBufferPool.isEmpty() && !m_buffersSuspended; }
41    int isEmpty() const { return m_tileBufferPool.isEmpty(); }
42    int numberOfBackingStoreFrontBuffers() const;
43
44    PlatformGraphicsContext* createPlatformGraphicsContext(BlackBerry::Platform::Graphics::Drawable*) const;
45    void destroyPlatformGraphicsContext(PlatformGraphicsContext*) const;
46
47    // The surface pool will allocate as many back buffers as specified by
48    // Platform::Settings::instance()->numberOfBackingStoreBackBuffers() which
49    // allows for at least one back buffer to be available for drawing before
50    // swapping buffers/geometry to the front.
51    unsigned numberOfAvailableBackBuffers() const;
52    TileBuffer* takeBackBuffer();
53    void addBackBuffer(TileBuffer*);
54
55    void releaseBuffers();
56    void createBuffers();
57
58    // EGLImage synchronization between WebKit and compositing threads
59    // TODO: Figure out how to improve the BlackBerry::Platform::Graphics with API that can encapsulate
60    // this kind of synchronisation mechanism.
61
62    // WebKit thread must waitForBuffer() before rendering to EGLImage
63    void waitForBuffer(TileBuffer*);
64
65    // Compositing thread must notify the SurfacePool when EGLImages are composited
66    void notifyBuffersComposited(const WTF::Vector<TileBuffer*>& buffers);
67
68    void destroyPlatformSync(void* platformSync);
69
70private:
71    SurfacePool();
72
73    typedef WTF::Vector<TileBuffer*> TileBufferList;
74    TileBufferList m_tileBufferPool;
75    TileBufferList m_availableBackBufferPool;
76    unsigned m_numberOfFrontBuffers;
77    bool m_initialized; // SurfacePool has been set up, with or without buffers.
78    bool m_buffersSuspended; // Buffer objects exist, but pixel memory has been freed.
79
80    std::set<void*> m_garbage;
81    bool m_hasFenceExtension;
82    mutable pthread_mutex_t m_mutex;
83};
84}
85}
86
87#endif // SurfacePool_h
88