2 Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
3 Copyright (C) 2012 Company 100, Inc.
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.
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.
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.
22 #include "WebCoordinatedSurface.h"
24 #if USE(COORDINATED_GRAPHICS)
25 #include "CoordinatedGraphicsArgumentCoders.h"
26 #include "GraphicsContext.h"
27 #include "WebCoreArgumentCoders.h"
28 #include <WebCore/GraphicsSurfaceToken.h>
30 #if USE(TEXTURE_MAPPER)
31 #include "TextureMapperGL.h"
34 using namespace WebCore;
38 WebCoordinatedSurface::Handle::Handle()
42 void WebCoordinatedSurface::Handle::encode(CoreIPC::ArgumentEncoder& encoder) const
44 encoder << m_size << m_flags;
45 #if USE(GRAPHICS_SURFACE)
46 encoder << m_graphicsSurfaceToken;
47 if (m_graphicsSurfaceToken.isValid())
50 encoder << m_bitmapHandle;
53 bool WebCoordinatedSurface::Handle::decode(CoreIPC::ArgumentDecoder& decoder, Handle& handle)
55 if (!decoder.decode(handle.m_size))
57 if (!decoder.decode(handle.m_flags))
59 #if USE(GRAPHICS_SURFACE)
60 if (!decoder.decode(handle.m_graphicsSurfaceToken))
62 if (handle.m_graphicsSurfaceToken.isValid())
65 if (!decoder.decode(handle.m_bitmapHandle))
71 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags)
73 RefPtr<WebCoordinatedSurface> surface;
74 #if USE(GRAPHICS_SURFACE)
75 surface = createWithSurface(size, flags);
79 surface = create(size, flags, ShareableBitmap::createShareable(size, (flags & SupportsAlpha) ? ShareableBitmap::SupportsAlpha : ShareableBitmap::NoFlags));
81 return surface.release();
84 #if USE(GRAPHICS_SURFACE)
85 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::createWithSurface(const IntSize& size, CoordinatedSurface::Flags flags)
87 GraphicsSurface::Flags surfaceFlags =
88 GraphicsSurface::SupportsSoftwareWrite
89 | GraphicsSurface::SupportsCopyToTexture
90 | GraphicsSurface::SupportsSharing;
92 if (flags & SupportsAlpha)
93 surfaceFlags |= GraphicsSurface::SupportsAlpha;
95 // This might return null, if the system is unable to provide a new graphics surface.
96 // In that case, this function would return null and allow falling back to ShareableBitmap.
97 RefPtr<GraphicsSurface> surface = GraphicsSurface::create(size, surfaceFlags);
102 return adoptRef(new WebCoordinatedSurface(size, flags, surface.release()));
106 PassOwnPtr<WebCore::GraphicsContext> WebCoordinatedSurface::createGraphicsContext(const IntRect& rect)
108 #if USE(GRAPHICS_SURFACE)
109 if (isBackedByGraphicsSurface())
110 return m_graphicsSurface->beginPaint(rect, 0 /* Write without retaining pixels*/);
114 OwnPtr<GraphicsContext> graphicsContext = m_bitmap->createGraphicsContext();
115 graphicsContext->clip(rect);
116 graphicsContext->translate(rect.x(), rect.y());
117 return graphicsContext.release();
120 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
122 return adoptRef(new WebCoordinatedSurface(size, flags, bitmap));
125 WebCoordinatedSurface::WebCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<ShareableBitmap> bitmap)
132 #if USE(GRAPHICS_SURFACE)
133 WebCoordinatedSurface::WebCoordinatedSurface(const WebCore::IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<WebCore::GraphicsSurface> surface)
136 , m_graphicsSurface(surface)
140 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags, PassRefPtr<GraphicsSurface> surface)
142 return adoptRef(new WebCoordinatedSurface(size, flags, surface));
146 WebCoordinatedSurface::~WebCoordinatedSurface()
150 PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const Handle& handle)
152 #if USE(GRAPHICS_SURFACE)
153 if (handle.graphicsSurfaceToken().isValid()) {
154 GraphicsSurface::Flags surfaceFlags = 0;
155 if (handle.m_flags & SupportsAlpha)
156 surfaceFlags |= GraphicsSurface::SupportsAlpha;
157 RefPtr<GraphicsSurface> surface = GraphicsSurface::create(handle.m_size, surfaceFlags, handle.m_graphicsSurfaceToken);
159 return adoptRef(new WebCoordinatedSurface(handle.m_size, handle.m_flags, PassRefPtr<GraphicsSurface>(surface)));
163 RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle.m_bitmapHandle);
167 return create(handle.m_size, handle.m_flags, bitmap.release());
170 bool WebCoordinatedSurface::createHandle(Handle& handle)
172 handle.m_size = m_size;
173 handle.m_flags = m_flags;
175 #if USE(GRAPHICS_SURFACE)
176 handle.m_graphicsSurfaceToken = m_graphicsSurface ? m_graphicsSurface->exportToken() : GraphicsSurfaceToken();
177 if (handle.m_graphicsSurfaceToken.isValid())
180 if (!m_bitmap->createHandle(handle.m_bitmapHandle))
186 #if USE(TEXTURE_MAPPER)
187 void WebCoordinatedSurface::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(), BitmapTexture::UpdateCanModifyOriginalImageData);
208 RefPtr<Image> image = m_bitmap->createImage();
209 texture->updateContents(image.get(), target, sourceOffset, BitmapTexture::UpdateCanModifyOriginalImageData);
211 #endif // USE(TEXTURE_MAPPER)
213 } // namespace WebKit
214 #endif // USE(COORDINATED_GRAPHICS)