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
|
/*
* Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "SurfacePool.h"
#include "PlatformContextSkia.h"
#if USE(ACCELERATED_COMPOSITING)
#include "BackingStoreCompositingSurface.h"
#endif
#include <BlackBerryPlatformGraphics.h>
#include <BlackBerryPlatformLog.h>
#include <BlackBerryPlatformMisc.h>
#include <BlackBerryPlatformScreen.h>
#include <BlackBerryPlatformSettings.h>
#include <BlackBerryPlatformThreading.h>
#include <errno.h>
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL
#include <EGL/eglext.h>
#endif
#define SHARED_PIXMAP_GROUP "webkit_backingstore_group"
namespace BlackBerry {
namespace WebKit {
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL
static PFNEGLCREATESYNCKHRPROC eglCreateSyncKHR;
static PFNEGLDESTROYSYNCKHRPROC eglDestroySyncKHR;
static PFNEGLCLIENTWAITSYNCKHRPROC eglClientWaitSyncKHR;
#endif
#if USE(ACCELERATED_COMPOSITING) && ENABLE_COMPOSITING_SURFACE
static PassRefPtr<BackingStoreCompositingSurface> createCompositingSurface()
{
BlackBerry::Platform::IntSize screenSize = BlackBerry::Platform::Graphics::Screen::primaryScreen()->size();
return BackingStoreCompositingSurface::create(screenSize, false /*doubleBuffered*/);
}
#endif
SurfacePool* SurfacePool::globalSurfacePool()
{
static SurfacePool* s_instance = 0;
if (!s_instance)
s_instance = new SurfacePool;
return s_instance;
}
SurfacePool::SurfacePool()
: m_visibleTileBuffer(0)
#if USE(ACCELERATED_COMPOSITING)
, m_compositingSurface(0)
#endif
, m_tileRenderingSurface(0)
, m_backBuffer(0)
, m_initialized(false)
, m_buffersSuspended(false)
, m_hasFenceExtension(false)
{
}
void SurfacePool::initialize(const BlackBerry::Platform::IntSize& tileSize)
{
if (m_initialized)
return;
m_initialized = true;
const unsigned numberOfTiles = BlackBerry::Platform::Settings::instance()->numberOfBackingStoreTiles();
const unsigned maxNumberOfTiles = BlackBerry::Platform::Settings::instance()->maximumNumberOfBackingStoreTilesAcrossProcesses();
if (numberOfTiles) { // Only allocate if we actually use a backingstore.
unsigned byteLimit = (maxNumberOfTiles /*pool*/ + 2 /*visible tile buffer, backbuffer*/) * tileSize.width() * tileSize.height() * 4;
bool success = BlackBerry::Platform::Graphics::createPixmapGroup(SHARED_PIXMAP_GROUP, byteLimit);
if (!success) {
BlackBerry::Platform::log(BlackBerry::Platform::LogLevelWarn,
"Shared buffer pool could not be set up, using regular memory allocation instead.");
}
}
m_tileRenderingSurface = BlackBerry::Platform::Graphics::drawingSurface();
#if USE(ACCELERATED_COMPOSITING) && ENABLE_COMPOSITING_SURFACE
m_compositingSurface = createCompositingSurface();
#endif
if (!numberOfTiles)
return; // we only use direct rendering when 0 tiles are specified.
// Create the shared backbuffer.
m_backBuffer = reinterpret_cast<unsigned>(new TileBuffer(tileSize));
for (size_t i = 0; i < numberOfTiles; ++i)
m_tilePool.append(BackingStoreTile::create(tileSize, BackingStoreTile::DoubleBuffered));
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL
const char* extensions = eglQueryString(Platform::Graphics::eglDisplay(), EGL_EXTENSIONS);
if (strstr(extensions, "EGL_KHR_fence_sync")) {
// We assume GL_OES_EGL_sync is present, but we don't check for it because
// no GL context is current at this point.
// TODO: check for it
eglCreateSyncKHR = (PFNEGLCREATESYNCKHRPROC) eglGetProcAddress("eglCreateSyncKHR");
eglDestroySyncKHR = (PFNEGLDESTROYSYNCKHRPROC) eglGetProcAddress("eglDestroySyncKHR");
eglClientWaitSyncKHR = (PFNEGLCLIENTWAITSYNCKHRPROC) eglGetProcAddress("eglClientWaitSyncKHR");
m_hasFenceExtension = eglCreateSyncKHR && eglDestroySyncKHR && eglClientWaitSyncKHR;
}
#endif
// m_mutex must be recursive because destroyPlatformSync may be called indirectly
// from notifyBuffersComposited
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_mutex, &attr);
pthread_mutexattr_destroy(&attr);
}
PlatformGraphicsContext* SurfacePool::createPlatformGraphicsContext(BlackBerry::Platform::Graphics::Drawable* drawable) const
{
return new WebCore::PlatformContextSkia(drawable);
}
PlatformGraphicsContext* SurfacePool::lockTileRenderingSurface() const
{
if (!m_tileRenderingSurface)
return 0;
return createPlatformGraphicsContext(BlackBerry::Platform::Graphics::lockBufferDrawable(m_tileRenderingSurface));
}
void SurfacePool::releaseTileRenderingSurface(PlatformGraphicsContext* context) const
{
if (!m_tileRenderingSurface)
return;
delete context;
BlackBerry::Platform::Graphics::releaseBufferDrawable(m_tileRenderingSurface);
}
void SurfacePool::initializeVisibleTileBuffer(const BlackBerry::Platform::IntSize& visibleSize)
{
if (!m_visibleTileBuffer || m_visibleTileBuffer->size() != visibleSize) {
delete m_visibleTileBuffer;
m_visibleTileBuffer = BackingStoreTile::create(visibleSize, BackingStoreTile::SingleBuffered);
}
}
TileBuffer* SurfacePool::backBuffer() const
{
ASSERT(m_backBuffer);
return reinterpret_cast<TileBuffer*>(m_backBuffer);
}
#if USE(ACCELERATED_COMPOSITING)
BackingStoreCompositingSurface* SurfacePool::compositingSurface() const
{
return m_compositingSurface.get();
}
#endif
void SurfacePool::notifyScreenRotated()
{
#if USE(ACCELERATED_COMPOSITING) && ENABLE_COMPOSITING_SURFACE
// Recreate compositing surface at new screen resolution.
m_compositingSurface = createCompositingSurface();
#endif
}
std::string SurfacePool::sharedPixmapGroup() const
{
return SHARED_PIXMAP_GROUP;
}
void SurfacePool::createBuffers()
{
if (!m_initialized || m_tilePool.isEmpty() || !m_buffersSuspended)
return;
// Create the tile pool.
for (size_t i = 0; i < m_tilePool.size(); ++i)
BlackBerry::Platform::Graphics::createPixmapBuffer(m_tilePool[i]->frontBuffer()->nativeBuffer());
if (m_visibleTileBuffer)
BlackBerry::Platform::Graphics::createPixmapBuffer(m_visibleTileBuffer->frontBuffer()->nativeBuffer());
if (backBuffer())
BlackBerry::Platform::Graphics::createPixmapBuffer(backBuffer()->nativeBuffer());
m_buffersSuspended = false;
}
void SurfacePool::releaseBuffers()
{
if (!m_initialized || m_tilePool.isEmpty() || m_buffersSuspended)
return;
m_buffersSuspended = true;
// Release the tile pool.
for (size_t i = 0; i < m_tilePool.size(); ++i) {
m_tilePool[i]->frontBuffer()->clearRenderedRegion();
// Clear the buffer to prevent accidental leakage of (possibly sensitive) pixel data.
BlackBerry::Platform::Graphics::clearBuffer(m_tilePool[i]->frontBuffer()->nativeBuffer(), 0, 0, 0, 0);
BlackBerry::Platform::Graphics::destroyPixmapBuffer(m_tilePool[i]->frontBuffer()->nativeBuffer());
}
if (m_visibleTileBuffer) {
m_visibleTileBuffer->frontBuffer()->clearRenderedRegion();
BlackBerry::Platform::Graphics::clearBuffer(m_visibleTileBuffer->frontBuffer()->nativeBuffer(), 0, 0, 0, 0);
BlackBerry::Platform::Graphics::destroyPixmapBuffer(m_visibleTileBuffer->frontBuffer()->nativeBuffer());
}
if (backBuffer()) {
backBuffer()->clearRenderedRegion();
BlackBerry::Platform::Graphics::clearBuffer(backBuffer()->nativeBuffer(), 0, 0, 0, 0);
BlackBerry::Platform::Graphics::destroyPixmapBuffer(backBuffer()->nativeBuffer());
}
}
void SurfacePool::waitForBuffer(TileBuffer* tileBuffer)
{
if (!m_hasFenceExtension)
return;
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL
EGLSyncKHR platformSync;
{
Platform::MutexLocker locker(&m_mutex);
platformSync = tileBuffer->fence()->takePlatformSync();
}
if (!platformSync)
return;
if (!eglClientWaitSyncKHR(Platform::Graphics::eglDisplay(), platformSync, 0, 100000000LL))
Platform::logAlways(Platform::LogLevelWarn, "Failed to wait for EGLSyncKHR object!\n");
// Instead of assuming eglDestroySyncKHR is thread safe, we add it to
// a garbage list for later collection on the thread that created it.
{
Platform::MutexLocker locker(&m_mutex);
m_garbage.insert(platformSync);
}
#endif
}
void SurfacePool::notifyBuffersComposited(const Vector<TileBuffer*>& tileBuffers)
{
if (!m_hasFenceExtension)
return;
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL
Platform::MutexLocker locker(&m_mutex);
EGLDisplay display = Platform::Graphics::eglDisplay();
// The EGL_KHR_fence_sync spec is nice enough to specify that the sync object
// is not actually deleted until everyone has stopped using it.
for (std::set<void*>::const_iterator it = m_garbage.begin(); it != m_garbage.end(); ++it)
eglDestroySyncKHR(display, *it);
m_garbage.clear();
// If we didn't blit anything, we don't need to create a new fence.
if (tileBuffers.isEmpty())
return;
// Create a new fence and assign to the tiles that were blit. Invalidate any previous
// fence that may be active among these tiles and add its sync object to the garbage set
// for later destruction to make sure it doesn't leak.
RefPtr<Fence> fence = Fence::create(eglCreateSyncKHR(display, EGL_SYNC_FENCE_KHR, 0));
for (unsigned int i = 0; i < tileBuffers.size(); ++i)
tileBuffers[i]->setFence(fence);
#endif
}
void SurfacePool::destroyPlatformSync(void* platformSync)
{
#if BLACKBERRY_PLATFORM_GRAPHICS_EGL && USE(SKIA)
Platform::MutexLocker locker(&m_mutex);
m_garbage.insert(platformSync);
#endif
}
}
}
|