2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if USE(ACCELERATED_COMPOSITING)
31 #define D3D_DEBUG_INFO
34 #include "CACFLayerTreeHost.h"
36 #include "PlatformCALayer.h"
37 #include "WebCoreInstanceHandle.h"
38 #include <WebKitSystemInterface/WebKitSystemInterface.h>
40 #include <wtf/CurrentTime.h>
41 #include <wtf/HashMap.h>
42 #include <wtf/OwnArrayPtr.h>
43 #include <wtf/OwnPtr.h>
44 #include <wtf/PassOwnPtr.h>
45 #include <wtf/StdLibExtras.h>
51 #pragma comment(lib, "d3d9")
52 #pragma comment(lib, "d3dx9")
54 #pragma comment(lib, "QuartzCore_debug")
56 #pragma comment(lib, "QuartzCore")
59 static IDirect3D9* s_d3d = 0;
60 static IDirect3D9* d3d()
65 if (!LoadLibrary(TEXT("d3d9.dll")))
68 s_d3d = Direct3DCreate9(D3D_SDK_VERSION);
73 inline static CGRect winRectToCGRect(RECT rc)
75 return CGRectMake(rc.left, rc.top, (rc.right - rc.left), (rc.bottom - rc.top));
78 inline static CGRect winRectToCGRect(RECT rc, RECT relativeToRect)
80 return CGRectMake(rc.left, (relativeToRect.bottom-rc.bottom), (rc.right - rc.left), (rc.bottom - rc.top));
85 static D3DPRESENT_PARAMETERS initialPresentationParameters()
87 D3DPRESENT_PARAMETERS parameters = {0};
88 parameters.Windowed = TRUE;
89 parameters.SwapEffect = D3DSWAPEFFECT_COPY;
90 parameters.BackBufferCount = 1;
91 parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
92 parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
97 // FIXME: <rdar://6507851> Share this code with CoreAnimation.
98 static bool hardwareCapabilitiesIndicateCoreAnimationSupport(const D3DCAPS9& caps)
100 // CoreAnimation needs two or more texture units.
101 if (caps.MaxTextureBlendStages < 2)
104 // CoreAnimation needs non-power-of-two textures.
105 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL))
108 // CoreAnimation needs vertex shader 2.0 or greater.
109 if (D3DSHADER_VERSION_MAJOR(caps.VertexShaderVersion) < 2)
112 // CoreAnimation needs pixel shader 2.0 or greater.
113 if (D3DSHADER_VERSION_MAJOR(caps.PixelShaderVersion) < 2)
119 bool CACFLayerTreeHost::acceleratedCompositingAvailable()
121 static bool available;
129 // Initialize available to true since this function will be called from a
130 // propagation within createRenderer(). We want to be able to return true
131 // when that happens so that the test can continue.
134 HMODULE library = LoadLibrary(TEXT("d3d9.dll"));
140 FreeLibrary(library);
142 library = LoadLibrary(TEXT("QuartzCore_debug.dll"));
144 library = LoadLibrary(TEXT("QuartzCore.dll"));
151 FreeLibrary(library);
153 // Make a dummy HWND.
154 WNDCLASSEX wcex = { 0 };
155 wcex.cbSize = sizeof(WNDCLASSEX);
156 wcex.lpfnWndProc = DefWindowProc;
157 wcex.hInstance = WebCore::instanceHandle();
158 wcex.lpszClassName = L"CoreAnimationTesterWindowClass";
159 ::RegisterClassEx(&wcex);
160 HWND testWindow = ::CreateWindow(L"CoreAnimationTesterWindowClass", L"CoreAnimationTesterWindow", WS_POPUP, -500, -500, 0, 0, 0, 0, 0, 0);
167 RefPtr<CACFLayerTreeHost> host = CACFLayerTreeHost::create();
168 host->setWindow(testWindow);
169 available = host->createRenderer();
170 ::DestroyWindow(testWindow);
175 PassRefPtr<CACFLayerTreeHost> CACFLayerTreeHost::create()
177 if (!acceleratedCompositingAvailable())
179 return adoptRef(new CACFLayerTreeHost());
182 CACFLayerTreeHost::CACFLayerTreeHost()
184 , m_mightBeAbleToCreateDeviceLater(true)
185 , m_rootLayer(PlatformCALayer::create(PlatformCALayer::LayerTypeRootLayer, 0))
186 , m_context(wkCACFContextCreate())
188 , m_renderTimer(this, &CACFLayerTreeHost::renderTimerFired)
189 , m_mustResetLostDeviceBeforeRendering(false)
190 , m_shouldFlushPendingGraphicsLayerChanges(false)
192 // Point the CACFContext to this
193 wkCACFContextSetUserData(m_context, this);
195 // Under the root layer, we have a clipping layer to clip the content,
196 // that contains a scroll layer that we use for scrolling the content.
197 // The root layer is the size of the client area of the window.
198 // The clipping layer is the size of the WebView client area (window less the scrollbars).
199 // The scroll layer is the size of the root child layer.
200 // Resizing the window will change the bounds of the rootLayer and the clip layer and will not
201 // cause any repositioning.
202 // Scrolling will affect only the position of the scroll layer without affecting the bounds.
204 m_rootLayer->setName("CACFLayerTreeHost rootLayer");
205 m_rootLayer->setAnchorPoint(FloatPoint3D(0, 0, 0));
206 m_rootLayer->setGeometryFlipped(true);
209 CGColorRef debugColor = CGColorCreateGenericRGB(1, 0, 0, 0.8);
210 m_rootLayer->setBackgroundColor(debugColor);
211 CGColorRelease(debugColor);
215 wkCACFContextSetLayer(m_context, m_rootLayer->platformLayer());
218 char* printTreeFlag = getenv("CA_PRINT_TREE");
219 m_printTree = printTreeFlag && atoi(printTreeFlag);
223 CACFLayerTreeHost::~CACFLayerTreeHost()
226 wkCACFContextDestroy(m_context);
229 void CACFLayerTreeHost::setWindow(HWND window)
231 if (window == m_window)
243 PlatformCALayer* CACFLayerTreeHost::rootLayer() const
245 return m_rootLayer.get();
248 void CACFLayerTreeHost::addPendingAnimatedLayer(PassRefPtr<PlatformCALayer> layer)
250 m_pendingAnimatedLayers.add(layer);
253 void CACFLayerTreeHost::setRootChildLayer(PlatformCALayer* layer)
255 m_rootLayer->removeAllSublayers();
256 m_rootChildLayer = layer;
257 if (m_rootChildLayer)
258 m_rootLayer->appendSublayer(m_rootChildLayer.get());
261 void CACFLayerTreeHost::layerTreeDidChange()
266 bool CACFLayerTreeHost::createRenderer()
268 if (m_d3dDevice || !m_mightBeAbleToCreateDeviceLater)
271 m_mightBeAbleToCreateDeviceLater = false;
272 D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
274 if (!d3d() || !::IsWindow(m_window))
277 // D3D doesn't like to make back buffers for 0 size windows. We skirt this problem if we make the
278 // passed backbuffer width and height non-zero. The window will necessarily get set to a non-zero
279 // size eventually, and then the backbuffer size will get reset.
281 GetClientRect(m_window, &rect);
283 if (rect.left-rect.right == 0 || rect.bottom-rect.top == 0) {
284 parameters.BackBufferWidth = 1;
285 parameters.BackBufferHeight = 1;
289 if (FAILED(d3d()->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps)))
292 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE;
293 if ((d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) && d3dCaps.VertexProcessingCaps)
294 behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
296 behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
298 COMPtr<IDirect3DDevice9> device;
299 if (FAILED(d3d()->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_window, behaviorFlags, ¶meters, &device))) {
300 // In certain situations (e.g., shortly after waking from sleep), Direct3DCreate9() will
301 // return an IDirect3D9 for which IDirect3D9::CreateDevice will always fail. In case we
302 // have one of these bad IDirect3D9s, get rid of it so we'll fetch a new one the next time
303 // we want to call CreateDevice.
307 // Even if we don't have a bad IDirect3D9, in certain situations (e.g., shortly after
308 // waking from sleep), CreateDevice will fail, but will later succeed if called again.
309 m_mightBeAbleToCreateDeviceLater = true;
314 // Now that we've created the IDirect3DDevice9 based on the capabilities we
315 // got from the IDirect3D9 global object, we requery the device for its
316 // actual capabilities. The capabilities returned by the device can
317 // sometimes be more complete, for example when using software vertex
320 if (FAILED(device->GetDeviceCaps(&deviceCaps)))
323 if (!hardwareCapabilitiesIndicateCoreAnimationSupport(deviceCaps))
326 m_d3dDevice = device;
330 wkCACFContextSetD3DDevice(m_context, m_d3dDevice.get());
332 if (IsWindow(m_window))
333 m_rootLayer->setBounds(bounds());
338 void CACFLayerTreeHost::destroyRenderer()
340 wkCACFContextSetLayer(m_context, m_rootLayer->platformLayer());
342 wkCACFContextSetD3DDevice(m_context, 0);
349 m_rootChildLayer = 0;
351 m_mightBeAbleToCreateDeviceLater = true;
354 void CACFLayerTreeHost::resize()
359 // Resetting the device might fail here. But that's OK, because if it does it we will attempt to
360 // reset the device the next time we try to render.
361 resetDevice(ChangedWindowSize);
364 m_rootLayer->setBounds(bounds());
365 wkCACFContextFlush(m_context);
369 static void getDirtyRects(HWND window, Vector<CGRect>& outRects)
371 ASSERT_ARG(outRects, outRects.isEmpty());
374 if (!GetClientRect(window, &clientRect))
377 OwnPtr<HRGN> region(CreateRectRgn(0, 0, 0, 0));
378 int regionType = GetUpdateRgn(window, region.get(), false);
379 if (regionType != COMPLEXREGION) {
381 if (GetUpdateRect(window, &dirtyRect, false))
382 outRects.append(winRectToCGRect(dirtyRect, clientRect));
386 DWORD dataSize = GetRegionData(region.get(), 0, 0);
387 OwnArrayPtr<unsigned char> regionDataBuffer(new unsigned char[dataSize]);
388 RGNDATA* regionData = reinterpret_cast<RGNDATA*>(regionDataBuffer.get());
389 if (!GetRegionData(region.get(), dataSize, regionData))
392 outRects.resize(regionData->rdh.nCount);
394 RECT* rect = reinterpret_cast<RECT*>(regionData->Buffer);
395 for (size_t i = 0; i < outRects.size(); ++i, ++rect)
396 outRects[i] = winRectToCGRect(*rect, clientRect);
399 void CACFLayerTreeHost::renderTimerFired(Timer<CACFLayerTreeHost>*)
404 void CACFLayerTreeHost::paint()
408 if (m_mightBeAbleToCreateDeviceLater)
413 Vector<CGRect> dirtyRects;
414 getDirtyRects(m_window, dirtyRects);
418 void CACFLayerTreeHost::render(const Vector<CGRect>& windowDirtyRects)
422 if (m_mustResetLostDeviceBeforeRendering && !resetDevice(LostDevice)) {
423 // We can't reset the device right now. Try again soon.
428 if (m_client && !m_client->shouldRender()) {
433 if (m_shouldFlushPendingGraphicsLayerChanges) {
434 m_client->flushPendingGraphicsLayerChanges();
435 m_shouldFlushPendingGraphicsLayerChanges = false;
438 // Flush the root layer to the render tree.
439 wkCACFContextFlush(m_context);
441 // All pending animations will have been started with the flush. Fire the animationStarted calls
442 double currentTime = WTF::currentTime();
443 double currentMediaTime = CACurrentMediaTime();
444 double t = currentTime + wkCACFContextGetLastCommitTime(m_context) - currentMediaTime;
445 ASSERT(t <= currentTime);
447 HashSet<RefPtr<PlatformCALayer> >::iterator end = m_pendingAnimatedLayers.end();
448 for (HashSet<RefPtr<PlatformCALayer> >::iterator it = m_pendingAnimatedLayers.begin(); it != end; ++it) {
449 PlatformCALayerClient* owner = (*it)->owner();
450 owner->platformCALayerAnimationStarted(t);
453 m_pendingAnimatedLayers.clear();
455 CGRect bounds = this->bounds();
457 // Give the renderer some space to use. This needs to be valid until the
458 // wkCACFContextFinishUpdate() call below.
460 if (!wkCACFContextBeginUpdate(m_context, space, sizeof(space), currentMediaTime, bounds, windowDirtyRects.data(), windowDirtyRects.size()))
464 CFTimeInterval timeToNextRender = numeric_limits<CFTimeInterval>::infinity();
467 // FIXME: don't need to clear dirty region if layer tree is opaque.
469 WKCACFUpdateRectEnumerator* e = wkCACFContextCopyUpdateRectEnumerator(m_context);
473 Vector<D3DRECT, 64> rects;
474 for (const CGRect* r = wkCACFUpdateRectEnumeratorNextRect(e); r; r = wkCACFUpdateRectEnumeratorNextRect(e)) {
476 rect.x1 = r->origin.x;
477 rect.x2 = rect.x1 + r->size.width;
478 rect.y1 = bounds.origin.y + bounds.size.height - (r->origin.y + r->size.height);
479 rect.y2 = rect.y1 + r->size.height;
483 wkCACFUpdateRectEnumeratorRelease(e);
485 timeToNextRender = wkCACFContextGetNextUpdateTime(m_context);
490 m_d3dDevice->Clear(rects.size(), rects.data(), D3DCLEAR_TARGET, 0, 1.0f, 0);
492 m_d3dDevice->BeginScene();
493 wkCACFContextRenderUpdate(m_context);
494 m_d3dDevice->EndScene();
496 err = m_d3dDevice->Present(0, 0, 0, 0);
498 if (err == D3DERR_DEVICELOST) {
499 wkCACFContextAddUpdateRect(m_context, bounds);
500 if (!resetDevice(LostDevice)) {
501 // We can't reset the device right now. Try again soon.
506 } while (err == D3DERR_DEVICELOST);
508 wkCACFContextFinishUpdate(m_context);
512 m_rootLayer->printTree();
515 // If timeToNextRender is not infinity, it means animations are running, so queue up to render again
516 if (timeToNextRender != numeric_limits<CFTimeInterval>::infinity())
520 void CACFLayerTreeHost::renderSoon()
522 if (!m_renderTimer.isActive())
523 m_renderTimer.startOneShot(0);
526 void CACFLayerTreeHost::flushPendingGraphicsLayerChangesSoon()
528 m_shouldFlushPendingGraphicsLayerChanges = true;
532 CGRect CACFLayerTreeHost::bounds() const
535 GetClientRect(m_window, &clientRect);
537 return winRectToCGRect(clientRect);
540 void CACFLayerTreeHost::initD3DGeometry()
544 CGRect bounds = this->bounds();
546 float x0 = bounds.origin.x;
547 float y0 = bounds.origin.y;
548 float x1 = x0 + bounds.size.width;
549 float y1 = y0 + bounds.size.height;
551 D3DXMATRIXA16 projection;
552 D3DXMatrixOrthoOffCenterRH(&projection, x0, x1, y0, y1, -1.0f, 1.0f);
554 m_d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
557 bool CACFLayerTreeHost::resetDevice(ResetReason reason)
562 HRESULT hr = m_d3dDevice->TestCooperativeLevel();
564 if (hr == D3DERR_DEVICELOST || hr == D3DERR_DRIVERINTERNALERROR) {
565 // The device cannot be reset at this time. Try again soon.
566 m_mustResetLostDeviceBeforeRendering = true;
570 m_mustResetLostDeviceBeforeRendering = false;
572 if (reason == LostDevice && hr == D3D_OK) {
573 // The device wasn't lost after all.
577 // We can reset the device.
579 // We have to release the context's D3D resrouces whenever we reset the IDirect3DDevice9 in order to
580 // destroy any D3DPOOL_DEFAULT resources that Core Animation has allocated (e.g., textures used
581 // for mask layers). See <http://msdn.microsoft.com/en-us/library/bb174425(v=VS.85).aspx>.
582 wkCACFContextReleaseD3DResources(m_context);
584 D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
585 hr = m_d3dDevice->Reset(¶meters);
587 // TestCooperativeLevel told us the device may be reset now, so we should
588 // not be told here that the device is lost.
589 ASSERT(hr != D3DERR_DEVICELOST);
598 #endif // USE(ACCELERATED_COMPOSITING)