1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef ShareableSurface_h
27#define ShareableSurface_h
28
29#include <WebCore/IntSize.h>
30#include <wtf/Noncopyable.h>
31#include <wtf/PassRefPtr.h>
32#include <wtf/RefCounted.h>
33#include <wtf/RetainPtr.h>
34
35typedef struct _CGLContextObject* CGLContextObj;
36typedef struct __IOSurface* IOSurfaceRef;
37
38namespace CoreIPC {
39    class ArgumentDecoder;
40    class ArgumentEncoder;
41}
42
43namespace WebKit {
44
45class ShareableSurface : public RefCounted<ShareableSurface> {
46public:
47    class Handle {
48        WTF_MAKE_NONCOPYABLE(Handle);
49
50    public:
51        Handle();
52        ~Handle();
53
54        void encode(CoreIPC::ArgumentEncoder*) const;
55        static bool decode(CoreIPC::ArgumentDecoder*, Handle&);
56
57    private:
58        friend class ShareableSurface;
59
60        mutable mach_port_t m_port;
61    };
62
63    // Create a shareable surface with the given size. Returns 0 on failure.
64    static PassRefPtr<ShareableSurface> create(CGLContextObj, const WebCore::IntSize&);
65
66    // Create a shareable surface from a handle. Returns 0 on failure.
67    static PassRefPtr<ShareableSurface> create(CGLContextObj, const Handle&);
68
69    ~ShareableSurface();
70
71    bool createHandle(Handle&);
72
73    unsigned textureID();
74
75    void attach();
76    void detach();
77
78private:
79    ShareableSurface(CGLContextObj, const WebCore::IntSize&, IOSurfaceRef);
80
81    // The OpenGL context.
82    CGLContextObj m_cglContextObj;
83
84    // The size of the surface.
85    WebCore::IntSize m_size;
86
87    // The ID of the texture.
88    unsigned m_textureID;
89
90    // The frame buffer object ID of the texture; used when the surface is used as a render destination.
91    unsigned m_frameBufferObjectID;
92
93    RetainPtr<IOSurfaceRef> m_ioSurface;
94};
95
96} // namespace WebKit
97
98#endif // ShareableSurface_h
99