1 //
2 // Copyright (c) 2016 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Stream.h: Defines the egl::Stream class, representing the stream
8 // where frames are streamed in. Implements EGLStreanKHR.
9 
10 #ifndef LIBANGLE_STREAM_H_
11 #define LIBANGLE_STREAM_H_
12 
13 #include <array>
14 
15 #include <EGL/egl.h>
16 #include <EGL/eglext.h>
17 
18 #include "common/angleutils.h"
19 #include "libANGLE/AttributeMap.h"
20 
21 namespace rx
22 {
23 class StreamProducerImpl;
24 }
25 
26 namespace gl
27 {
28 class Context;
29 class Texture;
30 }
31 
32 namespace egl
33 {
34 class Display;
35 class Error;
36 class Thread;
37 
38 class Stream final : angle::NonCopyable
39 {
40   public:
41     Stream(Display *display, const AttributeMap &attribs);
42     ~Stream();
43 
44     enum class ConsumerType
45     {
46         NoConsumer,
47         GLTextureRGB,
48         GLTextureYUV,
49     };
50 
51     enum class ProducerType
52     {
53         NoProducer,
54         D3D11TextureNV12,
55     };
56 
57     // A GL texture interpretation of a part of a producer frame. For use with GL texture consumers
58     struct GLTextureDescription
59     {
60         unsigned int width;
61         unsigned int height;
62         unsigned int internalFormat;
63         unsigned int mipLevels;
64     };
65 
66     EGLenum getState() const;
67 
68     void setConsumerLatency(EGLint latency);
69     EGLint getConsumerLatency() const;
70 
71     EGLuint64KHR getProducerFrame() const;
72     EGLuint64KHR getConsumerFrame() const;
73 
74     void setConsumerAcquireTimeout(EGLint timeout);
75     EGLint getConsumerAcquireTimeout() const;
76 
77     ConsumerType getConsumerType() const;
78     ProducerType getProducerType() const;
79 
80     EGLint getPlaneCount() const;
81 
82     rx::StreamProducerImpl *getImplementation();
83 
84     // Consumer creation methods
85     Error createConsumerGLTextureExternal(const AttributeMap &attributes, gl::Context *context);
86 
87     // Producer creation methods
88     Error createProducerD3D11TextureNV12(const AttributeMap &attributes);
89 
90     // Consumer methods
91     Error consumerAcquire(const gl::Context *context);
92     Error consumerRelease(const gl::Context *context);
93 
94     // Some consumers are bound to GL contexts. This validates that a given context is bound to the
95     // stream's consumer
96     bool isConsumerBoundToContext(const gl::Context *context) const;
97 
98     // Producer methods
99     Error validateD3D11NV12Texture(void *texture) const;
100     Error postD3D11NV12Texture(void *texture, const AttributeMap &attributes);
101 
102   private:
103     // Associated display
104     Display *mDisplay;
105 
106     // Producer Implementation
107     rx::StreamProducerImpl *mProducerImplementation;
108 
109     // Associated GL context. Note that this is a weak pointer used for validation purposes only,
110     // and should never be arbitrarily dereferenced without knowing the context still exists as it
111     // can become dangling at any time.
112     gl::Context *mContext;
113 
114     // EGL defined attributes
115     EGLint mState;
116     EGLuint64KHR mProducerFrame;
117     EGLuint64KHR mConsumerFrame;
118     EGLint mConsumerLatency;
119 
120     // EGL gltexture consumer attributes
121     EGLint mConsumerAcquireTimeout;
122 
123     // EGL gltexture yuv consumer attributes
124     EGLint mPlaneCount;
125     struct PlaneTexture
126     {
127         EGLint textureUnit;
128         gl::Texture *texture;
129     };
130     // Texture units and textures for all the planes
131     std::array<PlaneTexture, 3> mPlanes;
132 
133     // Consumer and producer types
134     ConsumerType mConsumerType;
135     ProducerType mProducerType;
136 
137     // ANGLE-only method, used internally
138     friend class gl::Texture;
139     void releaseTextures();
140 };
141 }  // namespace egl
142 
143 #endif  // LIBANGLE_STREAM_H_
144