summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/SharedTexture.cpp
blob: a7d43b5d227dee99f2b4b608c379369a3b6e134e (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
/*
 * 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 "SharedTexture.h"

#include "GLUtils.h"
#include <android/native_window.h>
#include <gui/SurfaceTexture.h>
#include <gui/SurfaceTextureClient.h>

#define LOG_NDEBUG 1
#define LOG_TAG "SharedTexture.cpp"
#include <utils/Log.h>

namespace WebCore {

SharedTexture::SharedTexture(SharedTextureMode mode)
{
    m_sharedTextureMode = mode;

    m_sourceTexture = new TextureInfo(m_sharedTextureMode);
    m_targetTexture = 0;

    if (m_sharedTextureMode == EglImageMode) {
        m_targetTexture = new TextureInfo(m_sharedTextureMode);
        m_eglImage = EGL_NO_IMAGE_KHR;
        m_isNewImage = true;
        m_syncObject = EGL_NO_SYNC_KHR;

        // Defer initialization of these values until we initialize the source
        // texture. This ensures that this initialization happens in the appropriate
        // thread.
        m_display = 0;
        m_supportsEGLImage = false;
        m_supportsEGLFenceSyncKHR = false;
    } else if (m_sharedTextureMode == SurfaceTextureMode) {
        glGenTextures(1, &m_sourceTexture->m_textureId);

        m_sourceTexture->m_surfaceTexture =
            new android::SurfaceTexture(m_sourceTexture->m_textureId, false);
        m_sourceTexture->m_ANW =
            new android::SurfaceTextureClient(m_sourceTexture->m_surfaceTexture);
    }
}

// called by the consumer when it no longer wants to consume and after it has
// terminated all providers. If EGLImages are used, the deletion of the
// source texture and EGLImage is the responsibility of the caller.
SharedTexture::~SharedTexture()
{
    if (m_sharedTextureMode == EglImageMode)
        deleteTargetTexture();
    else if (m_sharedTextureMode == SurfaceTextureMode) {
        m_sourceTexture->m_surfaceTexture.clear();
        m_sourceTexture->m_ANW.clear();
        GLUtils::deleteTexture(&m_sourceTexture->m_textureId);
    }
    delete m_sourceTexture;
    delete m_targetTexture;
}


void SharedTexture::initSourceTexture()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return;

    m_display = eglGetCurrentDisplay();
    m_supportsEGLImage = GLUtils::isEGLImageSupported();
    m_supportsEGLFenceSyncKHR = GLUtils::isEGLFenceSyncSupported();

    // TODO temporarily disable fence sync until the EGL framework fixes
    // performance issues that result from consistently adding/removing fences.
    m_supportsEGLFenceSyncKHR = false;

    LOGI("imageEGL: %d syncKHR: %d", m_supportsEGLImage, m_supportsEGLFenceSyncKHR);

    glGenTextures(1, &m_sourceTexture->m_textureId);

}

// For MediaTexture only
void SharedTexture::deleteSourceTexture()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return;
    // We need to delete the source texture and EGLImage in the thread in which
    // it was created. In theory we should be able to delete the EGLImage
    // from either thread, but it currently throws an error if not deleted
    // in the same EGLContext from which it was created.
    if (m_supportsEGLImage) {
        GLUtils::deleteTexture(&m_sourceTexture->m_textureId);
        if (m_eglImage != EGL_NO_IMAGE_KHR) {
            eglDestroyImageKHR(eglGetCurrentDisplay(), m_eglImage);
            m_eglImage = EGL_NO_IMAGE_KHR;
            m_isNewImage = true;
        }
        LOGI("Deleted Source Texture and EGLImage");
    }
}

// For MediaTexture only
void SharedTexture::deleteTargetTexture()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return;

    if (m_supportsEGLImage)
        GLUtils::deleteTexture(&m_targetTexture->m_textureId);
    else
        GLUtils::deleteTexture(&m_sourceTexture->m_textureId);
}

TextureInfo* SharedTexture::lockSource()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return m_sourceTexture;

    m_lock.lock();

    if (m_supportsEGLFenceSyncKHR && m_syncObject != EGL_NO_SYNC_KHR) {

        EGLint status = eglClientWaitSyncKHR(m_display, m_syncObject, 0, 1000000);

        if (status == EGL_TIMEOUT_EXPIRED_KHR)
            LOGE("Sync timeout for shared texture (%d)", m_sourceTexture->m_textureId);

        eglDestroySyncKHR(m_display, m_syncObject);
        m_syncObject = EGL_NO_SYNC_KHR;
    }
    return m_sourceTexture;
}

void SharedTexture::releaseSource()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return;

    if (m_supportsEGLImage) {
        // delete the existing image if needed
        if (!m_sourceTexture->equalsAttributes(m_targetTexture)) {
            if (m_eglImage != EGL_NO_IMAGE_KHR) {
                eglDestroyImageKHR(m_display, m_eglImage);
                m_eglImage = EGL_NO_IMAGE_KHR;
                m_isNewImage = true;
            }
            m_targetTexture->copyAttributes(m_sourceTexture);
        }

        // create an image from the texture, only when the texture is valid
        if (m_eglImage == EGL_NO_IMAGE_KHR && m_sourceTexture->m_width
            && m_sourceTexture->m_height) {
            GLUtils::createEGLImageFromTexture(m_sourceTexture->m_textureId, &m_eglImage);
            LOGV("Generating Image (%d) 0x%x", m_sourceTexture->m_textureId, m_eglImage);

            glFinish(); // ensures the texture is ready to be used by the consumer
        }

    } else {

        m_targetTexture = m_sourceTexture;

        // in the case of shared contexts we must flush the texture edits to the
        // GPU. This ensures the edits complete prior to allowing the texture to
        // be bound on the producers context.
        glFlush();
    }

    m_lock.unlock();
}

TextureInfo* SharedTexture::lockTarget()
{
    // Note that the source and targe are the same when using Surface Texture.
    if (m_sharedTextureMode == SurfaceTextureMode) {
        m_sourceTexture->m_surfaceTexture->updateTexImage();
        return m_sourceTexture;
    }

    m_lock.lock();

    if ((!m_supportsEGLImage && m_targetTexture->m_textureId == GL_NO_TEXTURE)
        || (m_supportsEGLImage && m_eglImage == EGL_NO_IMAGE_KHR)) {
        m_lock.unlock();
        return 0;
    }

    if (m_supportsEGLImage && (m_isNewImage || m_targetTexture->m_textureId == GL_NO_TEXTURE)) {
        if (m_targetTexture->m_textureId == GL_NO_TEXTURE)
            glGenTextures(1, &m_targetTexture->m_textureId);

        GLUtils::createTextureFromEGLImage(m_targetTexture->m_textureId, m_eglImage);
        LOGV("Generating Consumer Texture from 0x%x", m_eglImage);
        m_isNewImage = false;
    }
    return m_targetTexture;
}

void SharedTexture::releaseTarget()
{
    if (m_sharedTextureMode == SurfaceTextureMode)
        return;

    if (m_supportsEGLFenceSyncKHR) {
        if (m_syncObject != EGL_NO_SYNC_KHR)
            eglDestroySyncKHR(m_display, m_syncObject);
        m_syncObject = eglCreateSyncKHR(m_display, EGL_SYNC_FENCE_KHR, 0);
    } else {
        // TODO the flush currently prevents the screen from getting partial
        // updates but the only way to guarantee this is to call glFinish. Until
        // we support an EGL sync we will leave flush enable in order to test
        // with modest performance.
        glFlush();
    }

    m_lock.unlock();
}

} // namespace WebCore