1/*
2 * Copyright (C) 2011 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#include "config.h"
27#include "LegacyCACFLayerTreeHost.h"
28
29#include "PlatformCALayer.h"
30#include <QuartzCore/CABase.h>
31#include <WebKitSystemInterface/WebKitSystemInterface.h>
32
33#ifndef NDEBUG
34#define D3D_DEBUG_INFO
35#endif
36
37#include <d3d9.h>
38#include <d3dx9.h>
39
40#pragma comment(lib, "d3d9")
41#pragma comment(lib, "d3dx9")
42
43using namespace std;
44
45namespace WebCore {
46
47static IDirect3D9* s_d3d = 0;
48static IDirect3D9* d3d()
49{
50    if (s_d3d)
51        return s_d3d;
52
53    if (!LoadLibrary(TEXT("d3d9.dll")))
54        return 0;
55
56    s_d3d = Direct3DCreate9(D3D_SDK_VERSION);
57
58    return s_d3d;
59}
60
61static D3DPRESENT_PARAMETERS initialPresentationParameters()
62{
63    D3DPRESENT_PARAMETERS parameters = {0};
64    parameters.Windowed = TRUE;
65    parameters.SwapEffect = D3DSWAPEFFECT_COPY;
66    parameters.BackBufferCount = 1;
67    parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
68    parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
69
70    return parameters;
71}
72
73// FIXME: <rdar://6507851> Share this code with CoreAnimation.
74static bool hardwareCapabilitiesIndicateCoreAnimationSupport(const D3DCAPS9& caps)
75{
76    // CoreAnimation needs two or more texture units.
77    if (caps.MaxTextureBlendStages < 2)
78        return false;
79
80    // CoreAnimation needs non-power-of-two textures.
81    if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL))
82        return false;
83
84    // CoreAnimation needs vertex shader 2.0 or greater.
85    if (D3DSHADER_VERSION_MAJOR(caps.VertexShaderVersion) < 2)
86        return false;
87
88    // CoreAnimation needs pixel shader 2.0 or greater.
89    if (D3DSHADER_VERSION_MAJOR(caps.PixelShaderVersion) < 2)
90        return false;
91
92    return true;
93}
94
95PassRefPtr<LegacyCACFLayerTreeHost> LegacyCACFLayerTreeHost::create()
96{
97    return adoptRef(new LegacyCACFLayerTreeHost);
98}
99
100LegacyCACFLayerTreeHost::LegacyCACFLayerTreeHost()
101    : m_renderTimer(this, &LegacyCACFLayerTreeHost::renderTimerFired)
102    , m_context(wkCACFContextCreate())
103    , m_mightBeAbleToCreateDeviceLater(true)
104    , m_mustResetLostDeviceBeforeRendering(false)
105{
106#ifndef NDEBUG
107    char* printTreeFlag = getenv("CA_PRINT_TREE");
108    m_printTree = printTreeFlag && atoi(printTreeFlag);
109#endif
110}
111
112LegacyCACFLayerTreeHost::~LegacyCACFLayerTreeHost()
113{
114    wkCACFContextDestroy(m_context);
115}
116
117void LegacyCACFLayerTreeHost::initializeContext(void* userData, PlatformCALayer* layer)
118{
119    wkCACFContextSetUserData(m_context, userData);
120    wkCACFContextSetLayer(m_context, layer->platformLayer());
121}
122
123bool LegacyCACFLayerTreeHost::createRenderer()
124{
125    if (m_d3dDevice || !m_mightBeAbleToCreateDeviceLater)
126        return m_d3dDevice;
127
128    m_mightBeAbleToCreateDeviceLater = false;
129    D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
130
131    if (!d3d() || !::IsWindow(window()))
132        return false;
133
134    // D3D doesn't like to make back buffers for 0 size windows. We skirt this problem if we make the
135    // passed backbuffer width and height non-zero. The window will necessarily get set to a non-zero
136    // size eventually, and then the backbuffer size will get reset.
137    RECT rect;
138    GetClientRect(window(), &rect);
139
140    if (rect.left-rect.right == 0 || rect.bottom-rect.top == 0) {
141        parameters.BackBufferWidth = 1;
142        parameters.BackBufferHeight = 1;
143    }
144
145    D3DCAPS9 d3dCaps;
146    if (FAILED(d3d()->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps)))
147        return false;
148
149    DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE;
150    if ((d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) && d3dCaps.VertexProcessingCaps)
151        behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
152    else
153        behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
154
155    COMPtr<IDirect3DDevice9> device;
156    if (FAILED(d3d()->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window(), behaviorFlags, &parameters, &device))) {
157        // In certain situations (e.g., shortly after waking from sleep), Direct3DCreate9() will
158        // return an IDirect3D9 for which IDirect3D9::CreateDevice will always fail. In case we
159        // have one of these bad IDirect3D9s, get rid of it so we'll fetch a new one the next time
160        // we want to call CreateDevice.
161        s_d3d->Release();
162        s_d3d = 0;
163
164        // Even if we don't have a bad IDirect3D9, in certain situations (e.g., shortly after
165        // waking from sleep), CreateDevice will fail, but will later succeed if called again.
166        m_mightBeAbleToCreateDeviceLater = true;
167
168        return false;
169    }
170
171    // Now that we've created the IDirect3DDevice9 based on the capabilities we
172    // got from the IDirect3D9 global object, we requery the device for its
173    // actual capabilities. The capabilities returned by the device can
174    // sometimes be more complete, for example when using software vertex
175    // processing.
176    D3DCAPS9 deviceCaps;
177    if (FAILED(device->GetDeviceCaps(&deviceCaps)))
178        return false;
179
180    if (!hardwareCapabilitiesIndicateCoreAnimationSupport(deviceCaps))
181        return false;
182
183    m_d3dDevice = device;
184
185    initD3DGeometry();
186
187    wkCACFContextSetD3DDevice(m_context, m_d3dDevice.get());
188
189    if (IsWindow(window())) {
190        rootLayer()->setBounds(bounds());
191        flushContext();
192    }
193
194    return true;
195}
196
197void LegacyCACFLayerTreeHost::destroyRenderer()
198{
199    wkCACFContextSetLayer(m_context, 0);
200
201    wkCACFContextSetD3DDevice(m_context, 0);
202    m_d3dDevice = 0;
203    if (s_d3d)
204        s_d3d->Release();
205
206    s_d3d = 0;
207    m_mightBeAbleToCreateDeviceLater = true;
208
209    CACFLayerTreeHost::destroyRenderer();
210}
211
212void LegacyCACFLayerTreeHost::resize()
213{
214    if (!m_d3dDevice)
215        return;
216
217    // Resetting the device might fail here. But that's OK, because if it does it we will attempt to
218    // reset the device the next time we try to render.
219    resetDevice(ChangedWindowSize);
220
221    if (rootLayer()) {
222        rootLayer()->setBounds(bounds());
223        flushContext();
224    }
225}
226
227void LegacyCACFLayerTreeHost::renderTimerFired(Timer<LegacyCACFLayerTreeHost>*)
228{
229    paint();
230}
231
232void LegacyCACFLayerTreeHost::paint()
233{
234    createRenderer();
235    if (!m_d3dDevice) {
236        if (m_mightBeAbleToCreateDeviceLater)
237            renderSoon();
238        return;
239    }
240
241    CACFLayerTreeHost::paint();
242}
243
244void LegacyCACFLayerTreeHost::render(const Vector<CGRect>& windowDirtyRects)
245{
246    ASSERT(m_d3dDevice);
247
248    if (m_mustResetLostDeviceBeforeRendering && !resetDevice(LostDevice)) {
249        // We can't reset the device right now. Try again soon.
250        renderSoon();
251        return;
252    }
253
254    CGRect bounds = this->bounds();
255
256    // Give the renderer some space to use. This needs to be valid until the
257    // wkCACFContextFinishUpdate() call below.
258    char space[4096];
259    if (!wkCACFContextBeginUpdate(m_context, space, sizeof(space), CACurrentMediaTime(), bounds, windowDirtyRects.data(), windowDirtyRects.size()))
260        return;
261
262    HRESULT err = S_OK;
263    CFTimeInterval timeToNextRender = numeric_limits<CFTimeInterval>::infinity();
264
265    do {
266        // FIXME: don't need to clear dirty region if layer tree is opaque.
267
268        WKCACFUpdateRectEnumerator* e = wkCACFContextCopyUpdateRectEnumerator(m_context);
269        if (!e)
270            break;
271
272        Vector<D3DRECT, 64> rects;
273        for (const CGRect* r = wkCACFUpdateRectEnumeratorNextRect(e); r; r = wkCACFUpdateRectEnumeratorNextRect(e)) {
274            D3DRECT rect;
275            rect.x1 = r->origin.x;
276            rect.x2 = rect.x1 + r->size.width;
277            rect.y1 = bounds.origin.y + bounds.size.height - (r->origin.y + r->size.height);
278            rect.y2 = rect.y1 + r->size.height;
279
280            rects.append(rect);
281        }
282        wkCACFUpdateRectEnumeratorRelease(e);
283
284        timeToNextRender = wkCACFContextGetNextUpdateTime(m_context);
285
286        if (rects.isEmpty())
287            break;
288
289        m_d3dDevice->Clear(rects.size(), rects.data(), D3DCLEAR_TARGET, 0, 1.0f, 0);
290
291        m_d3dDevice->BeginScene();
292        wkCACFContextRenderUpdate(m_context);
293        m_d3dDevice->EndScene();
294
295        err = m_d3dDevice->Present(0, 0, 0, 0);
296
297        if (err == D3DERR_DEVICELOST) {
298            wkCACFContextAddUpdateRect(m_context, bounds);
299            if (!resetDevice(LostDevice)) {
300                // We can't reset the device right now. Try again soon.
301                renderSoon();
302                return;
303            }
304        }
305    } while (err == D3DERR_DEVICELOST);
306
307    wkCACFContextFinishUpdate(m_context);
308
309#ifndef NDEBUG
310    if (m_printTree)
311        rootLayer()->printTree();
312#endif
313
314    // If timeToNextRender is not infinity, it means animations are running, so queue up to render again
315    if (timeToNextRender != numeric_limits<CFTimeInterval>::infinity())
316        renderSoon();
317}
318
319void LegacyCACFLayerTreeHost::renderSoon()
320{
321    if (!m_renderTimer.isActive())
322        m_renderTimer.startOneShot(0);
323}
324
325void LegacyCACFLayerTreeHost::flushContext()
326{
327    wkCACFContextFlush(m_context);
328    contextDidChange();
329}
330
331void LegacyCACFLayerTreeHost::contextDidChange()
332{
333    renderSoon();
334    CACFLayerTreeHost::contextDidChange();
335}
336
337CFTimeInterval LegacyCACFLayerTreeHost::lastCommitTime() const
338{
339    return wkCACFContextGetLastCommitTime(m_context);
340}
341
342void LegacyCACFLayerTreeHost::initD3DGeometry()
343{
344    ASSERT(m_d3dDevice);
345
346    CGRect bounds = this->bounds();
347
348    float x0 = bounds.origin.x;
349    float y0 = bounds.origin.y;
350    float x1 = x0 + bounds.size.width;
351    float y1 = y0 + bounds.size.height;
352
353    D3DXMATRIXA16 projection;
354    D3DXMatrixOrthoOffCenterRH(&projection, x0, x1, y0, y1, -1.0f, 1.0f);
355
356    m_d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
357}
358
359bool LegacyCACFLayerTreeHost::resetDevice(ResetReason reason)
360{
361    ASSERT(m_d3dDevice);
362    ASSERT(m_context);
363
364    HRESULT hr = m_d3dDevice->TestCooperativeLevel();
365
366    if (hr == D3DERR_DEVICELOST || hr == D3DERR_DRIVERINTERNALERROR) {
367        // The device cannot be reset at this time. Try again soon.
368        m_mustResetLostDeviceBeforeRendering = true;
369        return false;
370    }
371
372    m_mustResetLostDeviceBeforeRendering = false;
373
374    if (reason == LostDevice && hr == D3D_OK) {
375        // The device wasn't lost after all.
376        return true;
377    }
378
379    // We can reset the device.
380
381    // We have to release the context's D3D resrouces whenever we reset the IDirect3DDevice9 in order to
382    // destroy any D3DPOOL_DEFAULT resources that Core Animation has allocated (e.g., textures used
383    // for mask layers). See <http://msdn.microsoft.com/en-us/library/bb174425(v=VS.85).aspx>.
384    wkCACFContextReleaseD3DResources(m_context);
385
386    D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
387    hr = m_d3dDevice->Reset(&parameters);
388
389    // TestCooperativeLevel told us the device may be reset now, so we should
390    // not be told here that the device is lost.
391    ASSERT(hr != D3DERR_DEVICELOST);
392
393    initD3DGeometry();
394
395    return true;
396}
397
398} // namespace WebCore
399