[EFL] Remove non Coordinated Graphics code path from cmake build system after r142169
[WebKit-https.git] / Source / WebCore / platform / graphics / surfaces / efl / GraphicsSurfaceCommon.cpp
1 /*
2  * Copyright (C) 2013 Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "GraphicsSurface.h"
28 #include "GLPlatformContext.h"
29 #include "GLTransportSurface.h"
30 #include "NotImplemented.h"
31 #include "TextureMapperGL.h"
32
33 namespace WebCore {
34
35 struct GraphicsSurfacePrivate {
36
37     GraphicsSurfacePrivate()
38     {
39     }
40
41     GraphicsSurfacePrivate(PlatformBufferHandle winId, const IntSize& size, GraphicsSurface::Flags flags)
42         : m_flags(0)
43         , m_rect(FloatPoint::zero(), size)
44         , m_size(size)
45         , m_sharedHandle(winId)
46     {
47         if (flags & GraphicsSurface::SupportsAlpha)
48             m_flags |= TextureMapperGL::ShouldBlend;
49     }
50
51     ~GraphicsSurfacePrivate()
52     {
53     }
54
55     void destroy()
56     {
57         if (m_client)
58             m_client->destroy();
59
60         if (m_sharedContext && m_sharedContext->handle() && m_sharedSurface)
61             makeContextCurrent();
62
63         if (m_sharedSurface)
64             m_sharedSurface->destroy();
65
66         if (m_sharedContext) {
67             m_sharedContext->destroy();
68             m_sharedContext->releaseCurrent();
69         }
70     }
71
72     bool initializeTransportSurface(const IntSize& size, GraphicsSurface::Flags flags, const PlatformGraphicsContext3D shareContext)
73     {
74         GLPlatformSurface::SurfaceAttributes sharedSurfaceAttributes = GLPlatformSurface::Default;
75         m_size = size;
76
77         if (flags & GraphicsSurface::SupportsAlpha)
78             sharedSurfaceAttributes = GLPlatformSurface::SupportAlpha;
79
80         m_sharedSurface = GLTransportSurface::createTransportSurface(size, sharedSurfaceAttributes);
81         if (!m_sharedSurface)
82             return false;
83
84         m_sharedContext = GLPlatformContext::createContext(GraphicsContext3D::RenderOffscreen);
85         if (!m_sharedContext)
86             return false;
87
88         if (!m_sharedContext->initialize(m_sharedSurface.get(), static_cast<PlatformContext>(shareContext)))
89             return false;
90
91         if (!makeContextCurrent())
92             return false;
93
94         return true;
95     }
96
97     bool makeContextCurrent() const
98     {
99         return m_sharedContext->makeCurrent(m_sharedSurface.get());
100     }
101
102     void copyFromTexture(GLuint textureId)
103     {
104         if (!makeContextCurrent())
105             return;
106
107         m_sharedSurface->updateContents(textureId);
108     }
109
110     PlatformBufferHandle handle() const
111     {
112         return m_sharedSurface->handle();
113     }
114
115     // Client
116     void updateClientBuffer()
117     {
118         if (!m_client)
119             return;
120
121         m_client->prepareTexture();
122     }
123
124     TextureMapperGL::Flags flags() const { return m_flags; }
125
126     const FloatRect& rect() const { return m_rect; }
127     const IntSize& size() const { return m_size; }
128
129     GLuint textureId() const
130     {
131         if (!m_client)
132             const_cast<GraphicsSurfacePrivate*>(this)->initializeClient();
133
134         return m_client ? m_client->texture() : 0;
135     }
136
137 private:
138     void initializeClient()
139     {
140         m_client = GLTransportSurfaceClient::createTransportSurfaceClient(m_sharedHandle, m_size, m_flags & TextureMapperGL::ShouldBlend);
141
142         if (!m_client)
143             return;
144     }
145
146     TextureMapperGL::Flags m_flags;
147     FloatRect m_rect;
148     IntSize m_size;
149     PlatformBufferHandle m_sharedHandle;
150     OwnPtr<GLTransportSurfaceClient> m_client;
151     std::unique_ptr<GLPlatformContext> m_sharedContext;
152     OwnPtr<GLTransportSurface> m_sharedSurface;
153 };
154
155 GraphicsSurfaceToken GraphicsSurface::platformExport()
156 {
157     return m_private->handle();
158 }
159
160 uint32_t GraphicsSurface::platformGetTextureID()
161 {
162     return m_private->textureId();
163 }
164
165 void GraphicsSurface::platformCopyToGLTexture(uint32_t /*target*/, uint32_t /*id*/, const IntRect& /*targetRect*/, const IntPoint& /*offset*/)
166 {
167     notImplemented();
168 }
169
170 void GraphicsSurface::platformCopyFromTexture(uint32_t textureId, const IntRect&)
171 {
172     if (!m_private)
173         return;
174
175     m_private->copyFromTexture(textureId);
176 }
177
178 void GraphicsSurface::platformPaintToTextureMapper(TextureMapper* textureMapper, const FloatRect& targetRect, const TransformationMatrix& transform, float opacity)
179 {
180     uint32_t texture = platformGetTextureID();
181     if (!texture)
182         return;
183
184     TransformationMatrix adjustedTransform = transform;
185     adjustedTransform.multiply(TransformationMatrix::rectToRect(m_private->rect(), targetRect));
186     static_cast<TextureMapperGL*>(textureMapper)->drawTexture(texture, m_private->flags(), m_private->size(), m_private->rect(), adjustedTransform, opacity);
187 }
188
189 uint32_t GraphicsSurface::platformFrontBuffer() const
190 {
191     return 0;
192 }
193
194 uint32_t GraphicsSurface::platformSwapBuffers()
195 {
196     m_private->updateClientBuffer();
197     return 0;
198 }
199
200 IntSize GraphicsSurface::platformSize() const
201 {
202     return m_private->size();
203 }
204
205 PassRefPtr<GraphicsSurface> GraphicsSurface::platformCreate(const IntSize& size, Flags flags, const PlatformGraphicsContext3D shareContext)
206 {
207     // GraphicsSurface doesn't yet support copyToTexture or singlebuffered surface.
208     if (flags & SupportsCopyToTexture || flags & SupportsSingleBuffered)
209         return PassRefPtr<GraphicsSurface>();
210
211     RefPtr<GraphicsSurface> surface = adoptRef(new GraphicsSurface(size, flags));
212     surface->m_private = new GraphicsSurfacePrivate();
213
214     if (surface->m_private->initializeTransportSurface(size, flags, shareContext))
215         return surface;
216
217     return PassRefPtr<GraphicsSurface>();
218 }
219
220 PassRefPtr<GraphicsSurface> GraphicsSurface::platformImport(const IntSize& size, Flags flags, const GraphicsSurfaceToken& token)
221 {
222     // GraphicsSurface doesn't yet support copyToTexture or singlebuffered surface.
223     if (flags & SupportsCopyToTexture || flags & SupportsSingleBuffered)
224         return PassRefPtr<GraphicsSurface>();
225
226     RefPtr<GraphicsSurface> surface = adoptRef(new GraphicsSurface(size, flags));
227     surface->m_private = new GraphicsSurfacePrivate(token.frontBufferHandle, size, flags);
228     return surface;
229 }
230
231 char* GraphicsSurface::platformLock(const IntRect&, int* /*outputStride*/, LockOptions)
232 {
233     // GraphicsSurface is currently only being used for WebGL, which does not require this locking mechanism.
234     return 0;
235 }
236
237 void GraphicsSurface::platformUnlock()
238 {
239     // GraphicsSurface is currently only being used for WebGL, which does not require this locking mechanism.
240 }
241
242 void GraphicsSurface::platformDestroy()
243 {
244     if (!m_private)
245         return;
246
247     m_private->destroy();
248
249     delete m_private;
250     m_private = 0;
251 }
252
253 std::unique_ptr<GraphicsContext> GraphicsSurface::platformBeginPaint(const IntSize&, char*, int)
254 {
255     notImplemented();
256     return nullptr;
257 }
258
259 PassRefPtr<Image> GraphicsSurface::createReadOnlyImage(const IntRect&)
260 {
261     notImplemented();
262     return 0;
263 }
264
265 }
266