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