summaryrefslogtreecommitdiff
path: root/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.cpp
blob: 0ad1e7f860f715cee8990f608876436c8b5250be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
 Copyright (C) 2012 Company 100, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebCoordinatedSurface.h"

#if USE(COORDINATED_GRAPHICS)
#include "CoordinatedGraphicsArgumentCoders.h"
#include "GraphicsContext.h"
#include "WebCoreArgumentCoders.h"
#include <WebCore/GraphicsSurfaceToken.h>

#if USE(TEXTURE_MAPPER)
#include "BitmapTextureGL.h"
#include "TextureMapperGL.h"
#endif

using namespace WebCore;

namespace WebKit {

WebCoordinatedSurface::Handle::Handle()
{
}

void WebCoordinatedSurface::Handle::encode(IPC::Encoder& encoder) const
{
    encoder << m_size << m_flags;
    encoder << m_bitmapHandle;
}

bool WebCoordinatedSurface::Handle::decode(IPC::Decoder& decoder, Handle& handle)
{
    if (!decoder.decode(handle.m_size))
        return false;
    if (!decoder.decode(handle.m_flags))
        return false;
    if (!decoder.decode(handle.m_bitmapHandle))
        return false;

    return true;
}

RefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags)
{
    if (auto bitmap = ShareableBitmap::createShareable(size, (flags & SupportsAlpha) ? ShareableBitmap::SupportsAlpha : ShareableBitmap::NoFlags))
        return create(size, flags, WTFMove(bitmap));

    return nullptr;
}

std::unique_ptr<GraphicsContext> WebCoordinatedSurface::createGraphicsContext(const IntRect& rect)
{
    ASSERT(m_bitmap);
    auto graphicsContext = m_bitmap->createGraphicsContext();
    graphicsContext->clip(rect);
    graphicsContext->translate(rect.x(), rect.y());
    return graphicsContext;
}

Ref<WebCoordinatedSurface> WebCoordinatedSurface::create(const IntSize& size, CoordinatedSurface::Flags flags, RefPtr<ShareableBitmap> bitmap)
{
    return adoptRef(*new WebCoordinatedSurface(size, flags, bitmap));
}

WebCoordinatedSurface::WebCoordinatedSurface(const IntSize& size, CoordinatedSurface::Flags flags, RefPtr<ShareableBitmap> bitmap)
    : CoordinatedSurface(size, flags)
    , m_bitmap(bitmap)
{
}

WebCoordinatedSurface::~WebCoordinatedSurface()
{
}

RefPtr<WebCoordinatedSurface> WebCoordinatedSurface::create(const Handle& handle)
{
    RefPtr<ShareableBitmap> bitmap = ShareableBitmap::create(handle.m_bitmapHandle);
    if (!bitmap)
        return nullptr;

    return create(handle.m_size, handle.m_flags, WTFMove(bitmap));
}

bool WebCoordinatedSurface::createHandle(Handle& handle)
{
    handle.m_size = m_size;
    handle.m_flags = m_flags;

    if (!m_bitmap->createHandle(handle.m_bitmapHandle))
        return false;

    return true;
}

void WebCoordinatedSurface::paintToSurface(const IntRect& rect, CoordinatedSurface::Client& client)
{
    auto context = createGraphicsContext(rect);
    client.paintToSurfaceContext(*context);
}

#if USE(TEXTURE_MAPPER)
void WebCoordinatedSurface::copyToTexture(RefPtr<WebCore::BitmapTexture> passTexture, const IntRect& target, const IntPoint& sourceOffset)
{
    RefPtr<BitmapTexture> texture(passTexture);

    ASSERT(m_bitmap);
    RefPtr<Image> image = m_bitmap->createImage();
    texture->updateContents(image.get(), target, sourceOffset, BitmapTexture::UpdateCanModifyOriginalImageData);
}
#endif // USE(TEXTURE_MAPPER)

} // namespace WebKit
#endif // USE(COORDINATED_GRAPHICS)