2 Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
21 #include "ShareableSurface.h"
23 #include "CoordinatedGraphicsArgumentCoders.h"
24 #include "GraphicsContext.h"
25 #include "WebCoreArgumentCoders.h"
27 #if USE(TEXTURE_MAPPER)
28 #include "TextureMapperGL.h"
31 using namespace WebCore;
35 ShareableSurface::Handle::Handle()
36 #if USE(GRAPHICS_SURFACE)
37 : m_graphicsSurfaceToken(0)
42 void ShareableSurface::Handle::encode(CoreIPC::ArgumentEncoder* encoder) const
44 encoder->encode(m_size);
45 encoder->encode(m_flags);
46 #if USE(GRAPHICS_SURFACE)
47 encoder->encode(m_graphicsSurfaceToken);
48 if (m_graphicsSurfaceToken)
51 encoder->encode(m_bitmapHandle);
54 bool ShareableSurface::Handle::decode(CoreIPC::ArgumentDecoder* decoder, Handle& handle)
56 if (!decoder->decode(handle.m_size))
58 if (!decoder->decode(handle.m_flags))
60 #if USE(GRAPHICS_SURFACE)
61 if (!decoder->decode(handle.m_graphicsSurfaceToken))
63 if (handle.m_graphicsSurfaceToken)
66 if (!decoder->decode(handle.m_bitmapHandle))
72 PassRefPtr<ShareableSurface> ShareableSurface::create(const IntSize& size, ShareableBitmap::Flags flags, Hints hints)
74 #if USE(GRAPHICS_SURFACE)
75 if (hints & SupportsGraphicsSurface) {
76 RefPtr<ShareableSurface> surface = createWithSurface(size, flags);
78 return surface.release();
82 return create(size, flags, ShareableBitmap::createShareable(size, flags));
85 #if USE(GRAPHICS_SURFACE)
86 PassRefPtr<ShareableSurface> ShareableSurface::createWithSurface(const IntSize& size, ShareableBitmap::Flags flags)
88 GraphicsSurface::Flags surfaceFlags =
89 GraphicsSurface::SupportsSoftwareWrite
90 | GraphicsSurface::SupportsCopyToTexture
91 | GraphicsSurface::SupportsSharing;
93 if (flags & ShareableBitmap::SupportsAlpha)
94 surfaceFlags |= GraphicsSurface::SupportsAlpha;
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);
103 return adoptRef(new ShareableSurface(size, flags, surface.release()));
107 PassOwnPtr<WebCore::GraphicsContext> ShareableSurface::createGraphicsContext(const IntRect& rect)
109 #if USE(GRAPHICS_SURFACE)
110 if (isBackedByGraphicsSurface())
111 return m_graphicsSurface->beginPaint(rect, 0 /* Write without retaining pixels*/);
115 OwnPtr<GraphicsContext> graphicsContext = m_bitmap->createGraphicsContext();
116 graphicsContext->clip(rect);
117 graphicsContext->translate(rect.x(), rect.y());
118 return graphicsContext.release();
121 PassRefPtr<ShareableSurface> ShareableSurface::create(const IntSize& size, ShareableBitmap::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
123 return adoptRef(new ShareableSurface(size, flags, bitmap));
126 ShareableSurface::ShareableSurface(const IntSize& size, ShareableBitmap::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
133 #if USE(GRAPHICS_SURFACE)
134 ShareableSurface::ShareableSurface(const WebCore::IntSize& size, ShareableBitmap::Flags flags, PassRefPtr<WebCore::GraphicsSurface> surface)
137 , m_graphicsSurface(surface)
141 PassRefPtr<ShareableSurface> ShareableSurface::create(const IntSize& size, ShareableBitmap::Flags flags, PassRefPtr<GraphicsSurface> surface)
143 return adoptRef(new ShareableSurface(size, flags, surface));
147 ShareableSurface::~ShareableSurface()
151 PassRefPtr<ShareableSurface> ShareableSurface::create(const Handle& handle)
153 #if USE(GRAPHICS_SURFACE)
154 if (handle.graphicsSurfaceToken()) {
155 RefPtr<GraphicsSurface> surface = GraphicsSurface::create(handle.m_size, handle.m_flags, handle.m_graphicsSurfaceToken);
157 return adoptRef(new ShareableSurface(handle.m_size, handle.m_flags, PassRefPtr<GraphicsSurface>(surface)));
161 RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle.m_bitmapHandle);
165 return create(handle.m_size, handle.m_flags, bitmap.release());
168 bool ShareableSurface::createHandle(Handle& handle)
170 handle.m_size = m_size;
171 handle.m_flags = m_flags;
173 #if USE(GRAPHICS_SURFACE)
174 handle.m_graphicsSurfaceToken = m_graphicsSurface ? m_graphicsSurface->exportToken() : 0;
175 if (handle.m_graphicsSurfaceToken)
180 if (!m_bitmap->createHandle(handle.m_bitmapHandle))
186 #if USE(TEXTURE_MAPPER)
187 void ShareableSurface::copyToTexture(PassRefPtr<WebCore::BitmapTexture> passTexture, const IntRect& target, const IntPoint& sourceOffset)
189 RefPtr<BitmapTexture> texture(passTexture);
191 #if USE(GRAPHICS_SURFACE)
192 if (isBackedByGraphicsSurface()) {
193 RefPtr<BitmapTextureGL> textureGL = toBitmapTextureGL(texture.get());
195 uint32_t textureID = textureGL->id();
196 uint32_t textureTarget = textureGL->textureTarget();
197 m_graphicsSurface->copyToGLTexture(textureTarget, textureID, target, sourceOffset);
201 RefPtr<Image> image = m_graphicsSurface->createReadOnlyImage(IntRect(sourceOffset, target.size()));
202 texture->updateContents(image.get(), target, IntPoint::zero());
207 RefPtr<Image> image = m_bitmap->createImage();
208 texture->updateContents(image.get(), target, sourceOffset);
211 #endif // USE(TEXTURE_MAPPER)
213 } // namespace WebKit