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#ifndef WebCoordinatedSurface_h
22#define WebCoordinatedSurface_h
23
24#if USE(COORDINATED_GRAPHICS)
25#include "ShareableBitmap.h"
26#include <WebCore/CoordinatedSurface.h>
27
28#if USE(GRAPHICS_SURFACE)
29#include "GraphicsSurface.h"
30#endif
31
32namespace WebCore {
33class BitmapTexture;
34class GraphicsContext;
35}
36
37namespace WebKit {
38
39class WebCoordinatedSurface : public WebCore::CoordinatedSurface {
40public:
41    class Handle {
42        WTF_MAKE_NONCOPYABLE(Handle);
43    public:
44        Handle();
45
46        void encode(IPC::ArgumentEncoder&) const;
47        static bool decode(IPC::ArgumentDecoder&, Handle&);
48
49#if USE(GRAPHICS_SURFACE)
50        WebCore::GraphicsSurfaceToken graphicsSurfaceToken() const { return m_graphicsSurfaceToken; }
51#endif
52
53    private:
54        friend class WebCoordinatedSurface;
55        mutable ShareableBitmap::Handle m_bitmapHandle;
56#if USE(GRAPHICS_SURFACE)
57        WebCore::GraphicsSurfaceToken m_graphicsSurfaceToken;
58#endif
59        WebCore::IntSize m_size;
60        WebCore::CoordinatedSurface::Flags m_flags;
61    };
62
63    // Create a new WebCoordinatedSurface, and allocate either a GraphicsSurface or a ShareableBitmap as backing.
64    static PassRefPtr<WebCoordinatedSurface> create(const WebCore::IntSize&, Flags);
65
66    // Create a shareable surface from a handle.
67    static PassRefPtr<WebCoordinatedSurface> create(const Handle&);
68
69    // Create a handle.
70    bool createHandle(Handle&);
71
72    virtual ~WebCoordinatedSurface();
73
74    virtual void paintToSurface(const WebCore::IntRect&, WebCore::CoordinatedSurface::Client*) override;
75
76#if USE(TEXTURE_MAPPER)
77    virtual void copyToTexture(PassRefPtr<WebCore::BitmapTexture>, const WebCore::IntRect& target, const WebCore::IntPoint& sourceOffset) override;
78#endif
79
80private:
81    WebCoordinatedSurface(const WebCore::IntSize&, Flags, PassRefPtr<ShareableBitmap>);
82
83    // Create a WebCoordinatedSurface referencing an existing ShareableBitmap.
84    static PassRefPtr<WebCoordinatedSurface> create(const WebCore::IntSize&, Flags, PassRefPtr<ShareableBitmap>);
85
86    std::unique_ptr<WebCore::GraphicsContext> createGraphicsContext(const WebCore::IntRect&);
87#if USE(GRAPHICS_SURFACE)
88    WebCoordinatedSurface(const WebCore::IntSize&, Flags, PassRefPtr<WebCore::GraphicsSurface>);
89    // Create a shareable bitmap backed by a graphics surface.
90    static PassRefPtr<WebCoordinatedSurface> createWithSurface(const WebCore::IntSize&, Flags);
91    // Create a WebCoordinatedSurface referencing an existing GraphicsSurface.
92    static PassRefPtr<WebCoordinatedSurface> create(const WebCore::IntSize&, Flags, PassRefPtr<WebCore::GraphicsSurface>);
93
94    bool isBackedByGraphicsSurface() const { return !!m_graphicsSurface; }
95#endif
96
97    RefPtr<ShareableBitmap> m_bitmap;
98
99#if USE(GRAPHICS_SURFACE)
100    RefPtr<WebCore::GraphicsSurface> m_graphicsSurface;
101#endif
102};
103
104} // namespace WebKit
105
106#endif // USE(COORDINATED_GRAPHICS)
107#endif // WebCoordinatedSurface_h
108