1/*
2 * Copyright (C) 2014 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#pragma once
27
28#include <WebCore/Color.h>
29#include <WebCore/IntPoint.h>
30#include <WebCore/IntSize.h>
31#include <wtf/ListHashSet.h>
32#include <wtf/Noncopyable.h>
33#include <wtf/RefCounted.h>
34#include <wtf/text/WTFString.h>
35
36#if PLATFORM(COCOA)
37#include <WebCore/IOSurface.h>
38#endif
39
40#if PLATFORM(GTK)
41#include <WebCore/RefPtrCairo.h>
42#endif
43
44#if PLATFORM(COCOA)
45#if !defined(__OBJC__)
46typedef struct objc_object *id;
47#endif
48
49OBJC_CLASS CAContext;
50
51#if HAVE(IOSURFACE)
52namespace WebCore {
53class IOSurface;
54}
55#endif
56#endif
57
58namespace WebKit {
59
60class ViewSnapshotStore;
61class WebBackForwardListItem;
62class WebPageProxy;
63
64class ViewSnapshot : public RefCounted<ViewSnapshot> {
65public:
66#if PLATFORM(COCOA)
67#if HAVE(IOSURFACE)
68 static Ref<ViewSnapshot> create(std::unique_ptr<WebCore::IOSurface>);
69#else
70 static Ref<ViewSnapshot> create(uint32_t slotID, WebCore::IntSize, size_t imageSizeInBytes);
71#endif
72#elif PLATFORM(GTK)
73 static Ref<ViewSnapshot> create(RefPtr<cairo_surface_t>&&);
74#endif
75
76 ~ViewSnapshot();
77
78 void clearImage();
79 bool hasImage() const;
80#if PLATFORM(COCOA)
81 id asLayerContents();
82 RetainPtr<CGImageRef> asImageForTesting();
83#endif
84
85 void setRenderTreeSize(uint64_t renderTreeSize) { m_renderTreeSize = renderTreeSize; }
86 uint64_t renderTreeSize() const { return m_renderTreeSize; }
87
88 void setBackgroundColor(WebCore::Color color) { m_backgroundColor = color; }
89 WebCore::Color backgroundColor() const { return m_backgroundColor; }
90
91 void setViewScrollPosition(WebCore::IntPoint scrollPosition) { m_viewScrollPosition = scrollPosition; }
92 WebCore::IntPoint viewScrollPosition() const { return m_viewScrollPosition; }
93
94 void setDeviceScaleFactor(float deviceScaleFactor) { m_deviceScaleFactor = deviceScaleFactor; }
95 float deviceScaleFactor() const { return m_deviceScaleFactor; }
96
97#if PLATFORM(COCOA)
98#if HAVE(IOSURFACE)
99 WebCore::IOSurface* surface() const { return m_surface.get(); }
100
101 size_t imageSizeInBytes() const { return m_surface ? m_surface->totalBytes() : 0; }
102 WebCore::IntSize size() const { return m_surface ? m_surface->size() : WebCore::IntSize(); }
103
104 void setSurface(std::unique_ptr<WebCore::IOSurface>);
105
106 WebCore::IOSurface::SurfaceState setVolatile(bool);
107#else
108 WebCore::IntSize size() const { return m_size; }
109 size_t imageSizeInBytes() const { return m_imageSizeInBytes; }
110#endif
111#elif PLATFORM(GTK)
112 cairo_surface_t* surface() const { return m_surface.get(); }
113
114 size_t imageSizeInBytes() const;
115 WebCore::IntSize size() const;
116#endif
117
118private:
119#if PLATFORM(COCOA)
120#if HAVE(IOSURFACE)
121 explicit ViewSnapshot(std::unique_ptr<WebCore::IOSurface>);
122
123 std::unique_ptr<WebCore::IOSurface> m_surface;
124#else
125 explicit ViewSnapshot(uint32_t slotID, WebCore::IntSize, size_t imageSizeInBytes);
126
127 uint32_t m_slotID;
128 size_t m_imageSizeInBytes;
129 WebCore::IntSize m_size;
130#endif
131#elif PLATFORM(GTK)
132 explicit ViewSnapshot(RefPtr<cairo_surface_t>&&);
133
134 RefPtr<cairo_surface_t> m_surface { nullptr };
135#endif
136
137 uint64_t m_renderTreeSize;
138 float m_deviceScaleFactor;
139 WebCore::Color m_backgroundColor;
140 WebCore::IntPoint m_viewScrollPosition; // Scroll position at snapshot time. Integral to make comparison reliable.
141};
142
143class ViewSnapshotStore {
144 WTF_MAKE_NONCOPYABLE(ViewSnapshotStore);
145 friend class ViewSnapshot;
146public:
147 ViewSnapshotStore();
148 ~ViewSnapshotStore();
149
150 static ViewSnapshotStore& singleton();
151
152 void recordSnapshot(WebPageProxy&, WebBackForwardListItem&);
153
154 void discardSnapshotImages();
155
156 void setDisableSnapshotVolatilityForTesting(bool disable) { m_disableSnapshotVolatility = disable; }
157 bool disableSnapshotVolatilityForTesting() const { return m_disableSnapshotVolatility; }
158
159#if !HAVE(IOSURFACE) && HAVE(CORE_ANIMATION_RENDER_SERVER)
160 static CAContext *snapshottingContext();
161#endif
162
163private:
164 void didAddImageToSnapshot(ViewSnapshot&);
165 void willRemoveImageFromSnapshot(ViewSnapshot&);
166 void pruneSnapshots(WebPageProxy&);
167
168 size_t m_snapshotCacheSize { 0 };
169
170 ListHashSet<ViewSnapshot*> m_snapshotsWithImages;
171 bool m_disableSnapshotVolatility { false };
172};
173
174} // namespace WebKit
175