1/*
2 * Copyright (C) 2013 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef RemoteLayerBackingStore_h
27#define RemoteLayerBackingStore_h
28
29#include "ShareableBitmap.h"
30#include <WebCore/FloatRect.h>
31#include <WebCore/IOSurface.h>
32#include <WebCore/Region.h>
33#include <chrono>
34
35OBJC_CLASS CALayer;
36
37// FIXME: Make PlatformCALayerRemote.cpp Objective-C so we can include WebLayer.h here and share the typedef.
38namespace WebCore {
39typedef Vector<WebCore::FloatRect, 5> RepaintRectList;
40}
41
42namespace WebKit {
43
44class PlatformCALayerRemote;
45
46class RemoteLayerBackingStore {
47    WTF_MAKE_NONCOPYABLE(RemoteLayerBackingStore);
48    WTF_MAKE_FAST_ALLOCATED;
49public:
50    RemoteLayerBackingStore(PlatformCALayerRemote*);
51    ~RemoteLayerBackingStore();
52
53    void ensureBackingStore(WebCore::FloatSize, float scale, bool acceleratesDrawing, bool isOpaque);
54
55    void setNeedsDisplay(const WebCore::IntRect);
56    void setNeedsDisplay();
57
58    bool display();
59
60    WebCore::FloatSize size() const { return m_size; }
61    float scale() const { return m_scale; }
62    bool acceleratesDrawing() const { return m_acceleratesDrawing; }
63    bool isOpaque() const { return m_isOpaque; }
64
65    PlatformCALayerRemote* layer() const { return m_layer; }
66
67    void applyBackingStoreToLayer(CALayer *);
68
69    void encode(IPC::ArgumentEncoder&) const;
70    static bool decode(IPC::ArgumentDecoder&, RemoteLayerBackingStore&);
71
72    void enumerateRectsBeingDrawn(CGContextRef, void (^)(CGRect));
73
74    bool hasFrontBuffer() const
75    {
76#if USE(IOSURFACE)
77        if (m_acceleratesDrawing)
78            return !!m_frontBuffer.surface;
79#endif
80        return !!m_frontBuffer.bitmap;
81    }
82
83    RetainPtr<CGContextRef> takeFrontContextPendingFlush();
84
85    enum class BufferType {
86        Front,
87        Back,
88        SecondaryBack
89    };
90
91    bool setBufferVolatility(BufferType type, bool isVolatile);
92
93    std::chrono::steady_clock::time_point lastDisplayTime() const { return m_lastDisplayTime; }
94
95private:
96    void drawInContext(WebCore::GraphicsContext&, CGImageRef backImage);
97    void clearBackingStore();
98    void swapToValidFrontBuffer();
99
100    PlatformCALayerRemote* m_layer;
101
102    WebCore::FloatSize m_size;
103    float m_scale;
104    bool m_isOpaque;
105
106    WebCore::Region m_dirtyRegion;
107
108    struct Buffer {
109        RefPtr<ShareableBitmap> bitmap;
110#if USE(IOSURFACE)
111        RefPtr<WebCore::IOSurface> surface;
112        bool isVolatile = false;
113#endif
114
115        explicit operator bool() const
116        {
117#if USE(IOSURFACE)
118            if (surface)
119                return true;
120#endif
121            if (bitmap)
122                return true;
123
124            return false;
125        }
126
127        void discard();
128    };
129
130    Buffer m_frontBuffer;
131    Buffer m_backBuffer;
132#if USE(IOSURFACE)
133    Buffer m_secondaryBackBuffer;
134#endif
135
136    RetainPtr<CGContextRef> m_frontContextPendingFlush;
137
138    bool m_acceleratesDrawing;
139
140    WebCore::RepaintRectList m_paintingRects;
141
142    std::chrono::steady_clock::time_point m_lastDisplayTime;
143};
144
145} // namespace WebKit
146
147#endif // RemoteLayerBackingStore_h
148