Separate flushing layer changes from rendering in CACFLayerTreeHost
[WebKit-https.git] / Source / WebCore / platform / graphics / ca / win / CACFLayerTreeHost.h
1 /*
2  * Copyright (C) 2009 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 COMPUTER, 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 CACFLayerTreeHost_h
27 #define CACFLayerTreeHost_h
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "COMPtr.h"
32 #include "Timer.h"
33
34 #include <wtf/HashSet.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/RetainPtr.h>
39 #include <wtf/Vector.h>
40
41 #include <CoreGraphics/CGGeometry.h>
42
43 interface IDirect3DDevice9;
44 struct WKCACFContext;
45
46 typedef struct CGImage* CGImageRef;
47
48 namespace WebCore {
49
50 class PlatformCALayer;
51
52 class CACFLayerTreeHostClient {
53 public:
54     virtual ~CACFLayerTreeHostClient() { }
55     virtual void flushPendingGraphicsLayerChanges() { }
56 };
57
58 // FIXME: Currently there is a CACFLayerTreeHost for each WebView and each
59 // has its own CARenderOGLContext and Direct3DDevice9, which is inefficient.
60 // (https://bugs.webkit.org/show_bug.cgi?id=31855)
61 class CACFLayerTreeHost : public RefCounted<CACFLayerTreeHost> {
62     friend PlatformCALayer;
63
64 public:
65     static PassRefPtr<CACFLayerTreeHost> create();
66     ~CACFLayerTreeHost();
67
68     static bool acceleratedCompositingAvailable();
69
70     void setClient(CACFLayerTreeHostClient* client) { m_client = client; }
71
72     void setRootChildLayer(PlatformCALayer*);
73     void layerTreeDidChange();
74     void setWindow(HWND);
75     void paint();
76     void resize();
77     void flushPendingGraphicsLayerChangesSoon();
78     void flushPendingLayerChangesNow();
79
80 protected:
81     PlatformCALayer* rootLayer() const;
82     void addPendingAnimatedLayer(PassRefPtr<PlatformCALayer>);
83
84 private:
85     CACFLayerTreeHost();
86
87     bool createRenderer();
88     void destroyRenderer();
89     void renderSoon();
90     void renderTimerFired(Timer<CACFLayerTreeHost>*);
91
92     CGRect bounds() const;
93
94     void initD3DGeometry();
95
96     // Call this when the device window has changed size or when IDirect3DDevice9::Present returns
97     // D3DERR_DEVICELOST. Returns true if the device was recovered, false if rendering must be
98     // aborted and reattempted soon.
99     enum ResetReason { ChangedWindowSize, LostDevice };
100     bool resetDevice(ResetReason);
101
102     void render(const Vector<CGRect>& dirtyRects = Vector<CGRect>());
103
104     CACFLayerTreeHostClient* m_client;
105     bool m_mightBeAbleToCreateDeviceLater;
106     COMPtr<IDirect3DDevice9> m_d3dDevice;
107     RefPtr<PlatformCALayer> m_rootLayer;
108     RefPtr<PlatformCALayer> m_rootChildLayer;
109     WKCACFContext* m_context;
110     HWND m_window;
111     Timer<CACFLayerTreeHost> m_renderTimer;
112     bool m_mustResetLostDeviceBeforeRendering;
113     bool m_shouldFlushPendingGraphicsLayerChanges;
114     bool m_isFlushingLayerChanges;
115     HashSet<RefPtr<PlatformCALayer> > m_pendingAnimatedLayers;
116
117 #ifndef NDEBUG
118     bool m_printTree;
119 #endif
120 };
121
122 }
123
124 #endif // USE(ACCELERATED_COMPOSITING)
125
126 #endif // CACFLayerTreeHost_h