1/*
2 * Copyright (C) 2015 Igalia S.L.
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#pragma once
27
28#if USE(COORDINATED_GRAPHICS)
29
30#include "TextureMapperGLHeaders.h"
31#include <wtf/Condition.h>
32#include <wtf/Function.h>
33#include <wtf/Lock.h>
34#include <wtf/RunLoop.h>
35#include <wtf/ThreadSafeRefCounted.h>
36#include <wtf/Vector.h>
37
38#ifndef NDEBUG
39#include <wtf/Threading.h>
40#endif
41
42namespace WebCore {
43
44class IntSize;
45class TextureMapperGL;
46class TextureMapperLayer;
47class TextureMapperPlatformLayerBuffer;
48
49class TextureMapperPlatformLayerProxy : public ThreadSafeRefCounted<TextureMapperPlatformLayerProxy> {
50 WTF_MAKE_FAST_ALLOCATED();
51public:
52 class Compositor {
53 public:
54 virtual void onNewBufferAvailable() = 0;
55 };
56
57 TextureMapperPlatformLayerProxy();
58 virtual ~TextureMapperPlatformLayerProxy();
59
60 // To avoid multiple lock/release situation to update a single frame,
61 // the implementation of TextureMapperPlatformLayerProxyProvider should
62 // aquire / release the lock explicitly to use below methods.
63 Lock& lock() { return m_lock; }
64 std::unique_ptr<TextureMapperPlatformLayerBuffer> getAvailableBuffer(const IntSize&, GLint internalFormat);
65 void pushNextBuffer(std::unique_ptr<TextureMapperPlatformLayerBuffer>);
66 bool isActive();
67
68 WEBCORE_EXPORT void activateOnCompositingThread(Compositor*, TextureMapperLayer*);
69 WEBCORE_EXPORT void invalidate();
70
71 WEBCORE_EXPORT void swapBuffer();
72 void dropCurrentBufferWhilePreservingTexture(bool shouldWait = false);
73
74 bool scheduleUpdateOnCompositorThread(Function<void()>&&);
75
76private:
77 void appendToUnusedBuffers(std::unique_ptr<TextureMapperPlatformLayerBuffer>);
78 void scheduleReleaseUnusedBuffers();
79 void releaseUnusedBuffersTimerFired();
80
81 Compositor* m_compositor;
82 TextureMapperLayer* m_targetLayer;
83
84 std::unique_ptr<TextureMapperPlatformLayerBuffer> m_currentBuffer;
85 std::unique_ptr<TextureMapperPlatformLayerBuffer> m_pendingBuffer;
86
87 Lock m_lock;
88
89 Lock m_wasBufferDroppedLock;
90 Condition m_wasBufferDroppedCondition;
91 bool m_wasBufferDropped { false };
92
93 Vector<std::unique_ptr<TextureMapperPlatformLayerBuffer>> m_usedBuffers;
94 std::unique_ptr<RunLoop::Timer<TextureMapperPlatformLayerProxy>> m_releaseUnusedBuffersTimer;
95
96#ifndef NDEBUG
97 RefPtr<Thread> m_compositorThread;
98#endif
99
100 void compositorThreadUpdateTimerFired();
101 std::unique_ptr<RunLoop::Timer<TextureMapperPlatformLayerProxy>> m_compositorThreadUpdateTimer;
102 Function<void()> m_compositorThreadUpdateFunction;
103};
104
105} // namespace WebCore
106
107#endif // USE(COORDINATED_GRAPHICS)
108