File: PluginViewPrivateBlackBerry.cpp

package info (click to toggle)
qtwebkit-opensource-src 5.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 291,692 kB
  • ctags: 268,122
  • sloc: cpp: 1,360,420; python: 70,286; ansic: 42,986; perl: 35,476; ruby: 12,236; objc: 9,465; xml: 8,396; asm: 3,873; yacc: 2,397; sh: 1,647; makefile: 650; lex: 644; java: 110
file content (393 lines) | stat: -rw-r--r-- 12,208 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2011 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 "PluginViewPrivateBlackBerry.h"

#include "FrameView.h"
#include "HostWindow.h"
#if USE(ACCELERATED_COMPOSITING)
#include "PluginLayerWebKitThread.h"
#endif
#include "NPCallbacksBlackBerry.h"
#include <BlackBerryPlatformExecutableMessage.h>
#include <BlackBerryPlatformMessageClient.h>
#include <wtf/MainThread.h>

static unsigned s_counter = 0;

namespace WebCore {

PluginViewPrivate::PluginViewPrivate(PluginView* view)
    : m_view(view)
    , m_pluginBufferType(BlackBerry::Platform::Graphics::PluginBufferWithAlpha)
    , m_pluginFrontBuffer(0)
    , m_idlePrevented(false)
    , m_hasPendingGeometryChange(true)
    , m_sentOnLoad(false)
    , m_isFullScreen(false)
    , m_isFocused(false)
    , m_orientationLocked(false)
    , m_isBackgroundPlaying(false)
{
    m_pluginBuffers[0] = 0;
    m_pluginBuffers[1] = 0;

    pthread_mutexattr_t mutexAttributes;
    pthread_mutexattr_init(&mutexAttributes);
    pthread_mutexattr_settype(&mutexAttributes, PTHREAD_MUTEX_RECURSIVE);

    pthread_mutex_init(&m_backBufferMutex, &mutexAttributes);
    pthread_rwlock_init(&m_frontBufferRwLock, 0);

    pthread_mutexattr_destroy(&mutexAttributes);

    char uniqueId[50];
    snprintf(uniqueId, sizeof(uniqueId), "PluginViewBB-%08x%08x-", s_counter++, (int)this);
    m_pluginUniquePrefix = uniqueId;
}

PluginViewPrivate::~PluginViewPrivate()
{
#if USE(ACCELERATED_COMPOSITING)
        if (m_platformLayer)
            static_cast<PluginLayerWebKitThread*>(m_platformLayer.get())->setPluginView(0);
#endif

    destroyBuffers();

    pthread_mutex_destroy(&m_backBufferMutex);
    pthread_rwlock_destroy(&m_frontBufferRwLock);
}

BlackBerry::Platform::Graphics::BufferType PluginViewPrivate::toBufferType(NPSurfaceFormat format)
{
    switch (format) {
    case FORMAT_RGB_565:
        return BlackBerry::Platform::Graphics::PluginBuffer;
    case FORMAT_RGBA_8888:
    default:
        return BlackBerry::Platform::Graphics::PluginBufferWithAlpha;
    }
}

void PluginViewPrivate::setZoomFactor(float zoomFactor)
{
    if (((NPSetWindowCallbackStruct*)m_view->m_npWindow.ws_info)->zoomFactor != zoomFactor)
        m_hasPendingGeometryChange = true;

    ((NPSetWindowCallbackStruct*)m_view->m_npWindow.ws_info)->zoomFactor = zoomFactor;
}

void PluginViewPrivate::setVisibleRects(const NPRect rects[], int32_t count)
{
    m_keepVisibleRect = IntRect();

    if (!m_view->parent() || !count)
        return;

    for (int i = 0; i < count; i++) {
        IntRect addRect = IntRect(rects[i].left, rects[i].top, rects[i].right - rects[i].left, rects[i].bottom - rects[i].top);
        m_keepVisibleRect.unite(addRect);
    }

    // Don't cause a possible scroll if the result is an empty rectangle.
    if (m_keepVisibleRect.isEmpty())
        return;

    // Adjust the rect to the parent window and then adjust for scrolling.
    m_keepVisibleRect = m_view->convertToContainingWindow(m_keepVisibleRect);
    FrameView* frameView = toFrameView(m_view->parent());
    m_keepVisibleRect.move(frameView->scrollPosition().x(), frameView->scrollPosition().y());

    frameView->hostWindow()->platformPageClient()->ensureContentVisible();
}

void PluginViewPrivate::clearVisibleRects()
{
    if (!m_keepVisibleRect.isEmpty())
        setVisibleRects(0, 0);
}

void PluginViewPrivate::showKeyboard(bool value)
{
    FrameView* frameView = toFrameView(m_view->parent());
    frameView->hostWindow()->platformPageClient()->showVirtualKeyboard(value);
}

void PluginViewPrivate::requestFullScreen()
{
    if (FrameView* frameView = toFrameView(m_view->parent()))
        if (frameView->hostWindow()->platformPageClient()->shouldPluginEnterFullScreen(m_view, m_pluginUniquePrefix.c_str()))
            m_view->handleFullScreenAllowedEvent();
}

void PluginViewPrivate::exitFullScreen()
{
    m_view->handleFullScreenExitEvent();
}

void PluginViewPrivate::requestCenterFitZoom()
{
    FrameView* frameView = toFrameView(m_view->parent());

    if (!frameView)
        return;

    frameView->hostWindow()->platformPageClient()->zoomToContentRect(m_view->m_windowRect);
}

void PluginViewPrivate::lockOrientation(bool landscape)
{
    FrameView* frameView = toFrameView(m_view->parent());

    if (!frameView)
        return;

    frameView->hostWindow()->platformPageClient()->lockOrientation(landscape);
    m_orientationLocked = true;
}

void PluginViewPrivate::unlockOrientation()
{
    if (!m_orientationLocked)
        return;

    FrameView* frameView = toFrameView(m_view->parent());

    if (!frameView)
        return;

    frameView->hostWindow()->platformPageClient()->unlockOrientation();
    m_orientationLocked = false;
}

void PluginViewPrivate::preventIdle(bool preventIdle)
{
    if (preventIdle == m_idlePrevented)
        return;

    FrameView* frameView = toFrameView(m_view->parent());
    if (!frameView)
        return;

    frameView->hostWindow()->platformPageClient()->setPreventsScreenDimming(preventIdle);
    m_idlePrevented = preventIdle;
}

void PluginViewPrivate::setHolePunch(int x, int y, int width, int height)
{
    if (width > 0 && height > 0)
        m_holePunchRect = IntRect(x, y, width, height);
    else
        m_holePunchRect = IntRect();

    // Clip the hole punch rectangle in case a plugin is 'overzealous', or 
    // does not clean up the hole punch rectangle correctly when exiting 
    // fullscreen (will mask bugs in the plugin).
    m_holePunchRect.intersect(IntRect(IntPoint(0, 0), m_view->frameRect().size()));

#if USE(ACCELERATED_COMPOSITING)
    if (m_platformLayer) {
        IntRect rect = m_holePunchRect;

        // Translate from plugin coordinates to contents coordinates.
        if (!m_holePunchRect.isEmpty())
            rect.move(m_view->frameRect().x(), m_view->frameRect().y());

        OwnPtr<HolePunchData> hp = adoptPtr(new HolePunchData);
        hp->x = m_holePunchRect.x();
        hp->y = m_holePunchRect.y();
        hp->w = m_holePunchRect.width();
        hp->h = m_holePunchRect.height();
        hp->layer = m_platformLayer;

        // Notify compositing layer and page client in order to be able to
        // punch through composited and non-composited parts of the page.
        callOnMainThread(npSetHolePunchHandler, hp.leakPtr()); // npSetHolePunchHandler() takes ownership of hp.
    }
#endif
}

NPSurface PluginViewPrivate::lockBackBuffer()
{
    pthread_mutex_lock(&m_backBufferMutex);
    BlackBerry::Platform::Graphics::Buffer* buffer = m_pluginBuffers[(m_pluginFrontBuffer + 1) % PLUGIN_BUFFERS];

    if (!buffer || !BlackBerry::Platform::Graphics::platformBufferHandle(buffer)) {
        unlockBackBuffer();
        return 0;
    }

    return BlackBerry::Platform::Graphics::platformBufferHandle(buffer);
}

void PluginViewPrivate::unlockBackBuffer()
{
    pthread_mutex_unlock(&m_backBufferMutex);
}

BlackBerry::Platform::Graphics::Buffer* PluginViewPrivate::lockReadFrontBufferInternal()
{
    pthread_rwlock_rdlock(&m_frontBufferRwLock);
    return m_pluginBuffers[m_pluginFrontBuffer];
}

NPSurface PluginViewPrivate::lockReadFrontBuffer()
{
    BlackBerry::Platform::Graphics::Buffer* buffer = lockReadFrontBufferInternal();

    if (!buffer || !BlackBerry::Platform::Graphics::platformBufferHandle(buffer)) {
        unlockReadFrontBuffer();
        return 0;
    }

    return BlackBerry::Platform::Graphics::platformBufferHandle(buffer);
}

void PluginViewPrivate::unlockReadFrontBuffer()
{
    pthread_rwlock_unlock(&m_frontBufferRwLock);
}

void PluginViewPrivate::swapBuffers()
{
    PthreadMutexLocker backLock(&m_backBufferMutex);
    PthreadWriteLocker frontLock(&m_frontBufferRwLock);
    m_pluginFrontBuffer = (m_pluginFrontBuffer + 1) % 2;
}

bool PluginViewPrivate::createBuffers(NPSurfaceFormat format, int width, int height)
{
    bool success = true;

    PthreadMutexLocker backLock(&m_backBufferMutex);
    PthreadWriteLocker frontLock(&m_frontBufferRwLock);

    bool didDestroyBuffers = false;
    for (int i = 0; i < PLUGIN_BUFFERS; i++) {
        if (m_pluginBuffers[i]) {
            didDestroyBuffers = true;
            BlackBerry::Platform::Graphics::destroyBuffer(m_pluginBuffers[i]);
            m_pluginBuffers[i] = 0;
        }

        if (width <= 0 || height <= 0)
            success = true;
        else {
            m_pluginBuffers[i] = BlackBerry::Platform::Graphics::createBuffer(
                BlackBerry::Platform::IntSize(width, height),
                toBufferType(format));

            if (!m_pluginBuffers[i])
                success = false;
        }
    }

    if (success) {
        m_pluginBufferSize = IntSize(width, height);
        m_pluginBufferType = toBufferType(format);

        if (didDestroyBuffers) {
            BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
                BlackBerry::Platform::createFunctionCallMessage(&BlackBerry::Platform::Graphics::collectThreadSpecificGarbage));
        }
    } else {
        m_pluginBufferSize = IntSize();
        m_pluginBufferType = BlackBerry::Platform::Graphics::PluginBufferWithAlpha;
        destroyBuffers();
    }

    return success;
}

bool PluginViewPrivate::resizeBuffers(NPSurfaceFormat format, int width, int height)
{
    bool success = true;

    // If there is no buffer created, then try to create it.
    if (!m_pluginBufferSize.width() || !m_pluginBufferSize.height())
        return createBuffers(format, width, height);

    if (!width || !height)
        return destroyBuffers();

    PthreadMutexLocker backLock(&m_backBufferMutex);
    PthreadWriteLocker frontLock(&m_frontBufferRwLock);

    for (int i = 0; i < PLUGIN_BUFFERS && success; i++) {
        success &= BlackBerry::Platform::Graphics::reallocBuffer(m_pluginBuffers[i],
            BlackBerry::Platform::IntSize(width, height),
            toBufferType(format));
    }

    if (success) {
        m_pluginBufferSize = IntSize(width, height);
        m_pluginBufferType = toBufferType(format);
        return true;
    }

    // Attempt to undo if we failed to get the new size/format. We can't guarantee
    // we will get it back though.
    if (!m_pluginBufferSize.width() || !m_pluginBufferSize.height()) {
        destroyBuffers();
        return false;
    }

    bool undone = true;
    for (int i = 0; i < PLUGIN_BUFFERS; i++) {
        undone &= BlackBerry::Platform::Graphics::reallocBuffer(m_pluginBuffers[i],
            m_pluginBufferSize, m_pluginBufferType);
    }

    // If we fail to undo, delete the buffers altogether.
    if (!undone)
        destroyBuffers();

    return false;
}

bool PluginViewPrivate::destroyBuffers()
{
    bool didDestroyBuffers = false;

    {
        PthreadMutexLocker backLock(&m_backBufferMutex);
        PthreadWriteLocker frontLock(&m_frontBufferRwLock);

        for (int i = 0; i < PLUGIN_BUFFERS; i++) {
            if (m_pluginBuffers[i]) {
                didDestroyBuffers = true;
                BlackBerry::Platform::Graphics::destroyBuffer(m_pluginBuffers[i]);
                m_pluginBuffers[i] = 0;
            }
        }
        m_pluginBufferSize = IntSize();
    }

    if (didDestroyBuffers) {
        BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
            BlackBerry::Platform::createFunctionCallMessage(&BlackBerry::Platform::Graphics::collectThreadSpecificGarbage));
    }

    return true;
}

} // namespace WebCore