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
|
/*
* Copyright 2010, The Android Open Source Project
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "DoubleBufferedTexture.h"
#include "GLUtils.h"
#define LOG_NDEBUG 1
#define LOG_TAG "DoubleBufferedTexture.cpp"
#include <utils/Log.h>
namespace WebCore {
DoubleBufferedTexture::DoubleBufferedTexture(EGLContext sharedContext)
{
// the mutex ensures that the variables are not used in any other thread
// until the constructor has the opportunity to initialize them
android::Mutex::Autolock lock(m_varLock);
m_display = eglGetCurrentDisplay();
m_pContext = EGL_NO_CONTEXT;
m_cContext = sharedContext;
m_writeableTexture = GL_NO_TEXTURE;
m_lockedConsumerTexture = GL_NO_TEXTURE;
m_supportsEGLImage = GLUtils::isEGLImageSupported();
}
SharedTexture* DoubleBufferedTexture::getWriteableTexture()
{
return (m_writeableTexture == &m_textureA) ? &m_textureA : &m_textureB;
}
SharedTexture* DoubleBufferedTexture::getReadableTexture()
{
return (m_writeableTexture != &m_textureA) ? &m_textureA : &m_textureB;
}
EGLContext DoubleBufferedTexture::producerAcquireContext()
{
// ensure that the constructor has completed and all values are initialized
android::Mutex::Autolock lock(m_varLock);
if (m_pContext != EGL_NO_CONTEXT) {
LOGV("AquireContext has previously generated a context.\n");
return m_pContext;
}
// check to see if a context already exists on this thread
EGLContext context = eglGetCurrentContext();
// if no context exists then create one
if (context == EGL_NO_CONTEXT) {
EGLContext sharedContext = m_supportsEGLImage ? EGL_NO_CONTEXT : m_cContext;
context = GLUtils::createBackgroundContext(sharedContext);
}
if (context == EGL_NO_CONTEXT) {
LOGE("eglCreateContext failed");
return EGL_NO_CONTEXT;
}
// initialize the producer's textures
m_textureA.lock();
m_textureB.lock();
m_textureA.initSourceTexture();
m_textureB.initSourceTexture();
LOGV("Initialized Textures A/B (%d:%d)", m_textureA.getSourceTextureId(),
m_textureB.getSourceTextureId());
m_textureA.unlock();
m_textureB.unlock();
// select a front buffer
m_writeableTexture = &m_textureA;
m_pContext = context;
return context;
}
TextureInfo* DoubleBufferedTexture::producerLock()
{
SharedTexture* sharedTex = getWriteableTexture();
LOGV("Acquiring P Lock (%d)", sharedTex->getSourceTextureId());
TextureInfo* texInfo = sharedTex->lockSource();
LOGV("Acquired P Lock");
return texInfo;
}
void DoubleBufferedTexture::producerRelease()
{
// get the front texture and cache the id
SharedTexture* sharedTex = getWriteableTexture();
LOGV("Releasing P Lock (%d)", sharedTex->getSourceTextureId());
sharedTex->releaseSource();
// swap the front and back buffers
m_varLock.lock();
m_writeableTexture = (sharedTex == &m_textureA) ? &m_textureB : &m_textureA;
LOGV("Released P Lock (%d)", sharedTex->getSourceTextureId());
m_varLock.unlock();
}
TextureInfo* DoubleBufferedTexture::consumerLock()
{
m_varLock.lock();
SharedTexture* sharedTex = getReadableTexture();
LOGV("Acquiring C Lock (%d)", sharedTex->getSourceTextureId());
m_lockedConsumerTexture = sharedTex;
m_varLock.unlock();
TextureInfo* texInfo = sharedTex->lockTarget();
LOGV("Acquired C Lock");
if (!texInfo)
LOGV("Released C Lock (Empty)");
return texInfo;
}
void DoubleBufferedTexture::consumerRelease()
{
android::Mutex::Autolock lock(m_varLock);
// we must check to see what texture the consumer had locked since the
// producer may have swapped out the front buffer
SharedTexture* sharedTex = m_lockedConsumerTexture;
sharedTex->releaseTarget();
LOGV("Released C Lock (%d)", sharedTex->getSourceTextureId());
}
} // namespace WebCore
|