7feaeea7551d988f629a1417e88268f89d398107
[WebKit-https.git] / Source / WebKit2 / Shared / CoordinatedGraphics / WebCoordinatedSurface.cpp
1 /*
2  Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
3  Copyright (C) 2012 Company 100, Inc.
4
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Library General Public License for more details.
14
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB.  If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "WebCoordinatedSurface.h"
23
24 #if USE(COORDINATED_GRAPHICS)
25 #include "CoordinatedGraphicsArgumentCoders.h"
26 #include "GraphicsContext.h"
27 #include "WebCoreArgumentCoders.h"
28 #include <WebCore/GraphicsSurfaceToken.h>
29
30 #if USE(TEXTURE_MAPPER)
31 #include "BitmapTextureGL.h"
32 #include "TextureMapperGL.h"
33 #endif
34
35 using namespace WebCore;
36
37 namespace WebKit {
38
39 WebCoordinatedSurface::Handle::Handle()
40 {
41 }
42
43 void WebCoordinatedSurface::Handle::encode(IPC::ArgumentEncoder& encoder) const
44 {
45     encoder << m_size << m_flags;
46 #if USE(GRAPHICS_SURFACE)
47     encoder << m_graphicsSurfaceToken;
48     if (m_graphicsSurfaceToken.isValid())
49         return;
50 #endif
51     encoder << m_bitmapHandle;
52 }
53
54 bool WebCoordinatedSurface::Handle::decode(IPC::ArgumentDecoder& decoder, Handle& handle)
55 {
56     if (!decoder.decode(handle.m_size))
57         return false;
58     if (!decoder.decode(handle.m_flags))
59         return false;
60 #if USE(GRAPHICS_SURFACE)
61     if (!decoder.decode(handle.m_graphicsSurfaceToken))
62         return false;
63     if (handle.m_graphicsSurfaceToken.isValid())
64         return true;
65 #endif
66     if (!decoder.decode(handle.m_bitmapHandle))
67         return false;
68
69     return true;
70 }
71
72 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags)
73 {
74     RefPtr<WebCoordinatedSurface> surface;
75 #if USE(GRAPHICS_SURFACE)
76     surface = createWithSurface(size, flags);
77 #endif
78
79     if (!surface)
80         surface = create(size, flags, ShareableBitmap::createShareable(size, (flags & SupportsAlpha) ? ShareableBitmap::SupportsAlpha : ShareableBitmap::NoFlags));
81
82     return surface.release();
83 }
84
85 #if USE(GRAPHICS_SURFACE)
86 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::createWithSurface(const IntSize& size, CoordinatedSurface::Flags flags)
87 {
88     GraphicsSurface::Flags surfaceFlags =
89         GraphicsSurface::SupportsSoftwareWrite
90         | GraphicsSurface::SupportsCopyToTexture
91         | GraphicsSurface::SupportsSharing;
92
93     if (flags & SupportsAlpha)
94         surfaceFlags |= GraphicsSurface::SupportsAlpha;
95
96     // This might return null, if the system is unable to provide a new graphics surface.
97     // In that case, this function would return null and allow falling back to ShareableBitmap.
98     RefPtr<GraphicsSurface> surface = GraphicsSurface::create(size, surfaceFlags);
99     if (!surface)
100         return 0;
101
102     ASSERT(surface);
103     return adoptRef(new WebCoordinatedSurface(size, flags, surface.release()));
104 }
105 #endif
106
107 std::unique_ptr<GraphicsContext> WebCoordinatedSurface::createGraphicsContext(const IntRect& rect)
108 {
109 #if USE(GRAPHICS_SURFACE)
110     if (isBackedByGraphicsSurface())
111         return m_graphicsSurface->beginPaint(rect, 0 /* Write without retaining pixels*/);
112 #endif
113
114     ASSERT(m_bitmap);
115     auto graphicsContext = m_bitmap->createGraphicsContext();
116     graphicsContext->clip(rect);
117     graphicsContext->translate(rect.x(), rect.y());
118     return graphicsContext;
119 }
120
121 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
122 {
123     return adoptRef(new WebCoordinatedSurface(size, flags, bitmap));
124 }
125
126 WebCoordinatedSurface::WebCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
127     : CoordinatedSurface(size, flags)
128     , m_bitmap(bitmap)
129 {
130 }
131
132 #if USE(GRAPHICS_SURFACE)
133 WebCoordinatedSurface::WebCoordinatedSurface(const WebCore::IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<WebCore::GraphicsSurface> surface)
134     : CoordinatedSurface(size, flags)
135     , m_graphicsSurface(surface)
136 {
137 }
138
139 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<GraphicsSurface> surface)
140 {
141     return adoptRef(new WebCoordinatedSurface(size, flags, surface));
142 }
143 #endif
144
145 WebCoordinatedSurface::~WebCoordinatedSurface()
146 {
147 }
148
149 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const Handle& handle)
150 {
151 #if USE(GRAPHICS_SURFACE)
152     if (handle.graphicsSurfaceToken().isValid()) {
153         GraphicsSurface::Flags surfaceFlags = 0;
154         if (handle.m_flags & SupportsAlpha)
155             surfaceFlags |= GraphicsSurface::SupportsAlpha;
156         RefPtr<GraphicsSurface> surface = GraphicsSurface::create(handle.m_size, surfaceFlags, handle.m_graphicsSurfaceToken);
157         if (surface)
158             return adoptRef(new WebCoordinatedSurface(handle.m_size, handle.m_flags, surface.release()));
159     }
160 #endif
161
162     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle.m_bitmapHandle);
163     if (!bitmap)
164         return 0;
165
166     return create(handle.m_size, handle.m_flags, bitmap.release());
167 }
168
169 bool WebCoordinatedSurface::createHandle(Handle& handle)
170 {
171     handle.m_size = m_size;
172     handle.m_flags = m_flags;
173
174 #if USE(GRAPHICS_SURFACE)
175     handle.m_graphicsSurfaceToken = m_graphicsSurface ? m_graphicsSurface->exportToken() : GraphicsSurfaceToken();
176     if (handle.m_graphicsSurfaceToken.isValid())
177         return true;
178 #endif
179     if (!m_bitmap->createHandle(handle.m_bitmapHandle))
180         return false;
181
182     return true;
183 }
184
185 void WebCoordinatedSurface::paintToSurface(const IntRect& rect, CoordinatedSurface::Client* client)
186 {
187     ASSERT(client);
188
189     auto context = createGraphicsContext(rect);
190     client->paintToSurfaceContext(context.get());
191 }
192
193 #if USE(TEXTURE_MAPPER)
194 void WebCoordinatedSurface::copyToTexture(PassRefPtr<WebCore::BitmapTexture> passTexture, const IntRect& target, const IntPoint& sourceOffset)
195 {
196     RefPtr<BitmapTexture> texture(passTexture);
197
198 #if USE(GRAPHICS_SURFACE)
199     if (isBackedByGraphicsSurface()) {
200         RefPtr<BitmapTextureGL> textureGL = toBitmapTextureGL(texture.get());
201         if (textureGL) {
202             uint32_t textureID = textureGL->id();
203             uint32_t textureTarget = textureGL->textureTarget();
204             m_graphicsSurface->copyToGLTexture(textureTarget, textureID, target, sourceOffset);
205             return;
206         }
207
208         RefPtr<Image> image = m_graphicsSurface->createReadOnlyImage(IntRect(sourceOffset, target.size()));
209         texture->updateContents(image.get(), target, IntPoint::zero(), BitmapTexture::UpdateCanModifyOriginalImageData);
210         return;
211     }
212 #endif
213
214     ASSERT(m_bitmap);
215     RefPtr<Image> image = m_bitmap->createImage();
216     texture->updateContents(image.get(), target, sourceOffset, BitmapTexture::UpdateCanModifyOriginalImageData);
217 }
218 #endif // USE(TEXTURE_MAPPER)
219
220 } // namespace WebKit
221 #endif // USE(COORDINATED_GRAPHICS)