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

#ifndef SharedTexture_h
#define SharedTexture_h

#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <utils/threads.h>

namespace WebCore {

static const GLuint GL_NO_TEXTURE = 0;

/**
 * TextureInfo is a class that stores both the texture and metadata about the
 * texture.
 */
class TextureInfo {
public:

    TextureInfo();

    bool equalsAttributes(const TextureInfo* otherTexture);
    void copyAttributes(const TextureInfo* sourceTexture);

    bool operator==(const TextureInfo& otherTexture);

    GLuint m_textureId;
    int32_t m_width;
    int32_t m_height;
    GLenum m_internalFormat;
};

/**
 * SharedTexture is a class that encapsulates all the necessary variables
 * needed to share a single texture across threads. In the case that threads
 * communicate using EGL's sharedContext mechanism or do not support the proper
 * EGLImage extensions the targetTexture, eglImage, and isNewImage variables are
 * not used.
 */
class SharedTexture {
public:
    // consumer thread functions
    SharedTexture();
    ~SharedTexture();

    TextureInfo* lockSource();
    void releaseSource();

    TextureInfo* lockTarget();
    void releaseTarget();

    // these locks are only used for the methods below
    void lock() { m_lock.lock(); }
    void unlock() { m_lock.unlock(); }

    void initSourceTexture(); // producer thread only
    GLuint getSourceTextureId() { return m_sourceTexture.m_textureId; }
    GLuint getTargetTextureId() { return m_targetTexture.m_textureId; }
    EGLImageKHR getEGLImage() { return m_eglImage; }

private:
    /**
     * The mutex is used to ensure that the contents of the struct are current across
     * threads and that only one thread is manipulating the texture at a given time.
     */
    android::Mutex m_lock;
    /**
     * The texture and its associated metadata that is used by the producer. The
     * texture is created in the producer's thread and can only be read by the
     * consumer when the consumer shares the same context as the producer. The
     * metadata is used to track changes to the texture that would orphan the
     * target texture and require a new EGLImage to be constructed.
     */
    TextureInfo m_sourceTexture;
    /**
     * The target texture stores the id and metadata of the texture that is to be
     * used by the consumer. In the case where EGLImages are supported this hold
     * the current eglImage target.
     */
    TextureInfo m_targetTexture;
    /**
     * The EGLImage is used to share the texture between EGLContexts on two
     * different threads. This serves as an alternative to sharing the contexts
     * but is only used if GL and EGL support the required extensions.
     */
    EGLImageKHR m_eglImage;
    /**
     * This flag is used to determine if the eglImage has been updated. This
     * signals the consumer thread to rebind the targetTexture to the new image.
     */
    bool m_isNewImage;
    /**
     * The sync allows the consumer to release the lock prior to the commands
     * executing on the GPU. Prior to releasing the lock the consumer creates
     * a sync object and stores it here. After locking the texture the client
     * must check that the sync has completed prior to manipulating the texture.
     * This value is only used if the proper EGL extensions are supported.
     */
    EGLSyncKHR m_syncObject;

    EGLDisplay m_display;

    bool m_supportsEGLImage;
    bool m_supportsEGLFenceSyncKHR;
};

} // namespace WebCore

#endif // SharedTexture_h