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 "WKCACFLayerRenderer.h"
36 #include "PlatformCALayer.h"
37 #include "WKCACFContextFlusher.h"
38 #include "WebCoreInstanceHandle.h"
39 #include <WebKitSystemInterface/WebKitSystemInterface.h>
41 #include <wtf/CurrentTime.h>
42 #include <wtf/HashMap.h>
43 #include <wtf/OwnArrayPtr.h>
44 #include <wtf/OwnPtr.h>
45 #include <wtf/PassOwnPtr.h>
46 #include <wtf/StdLibExtras.h>
52 #pragma comment(lib, "d3d9")
53 #pragma comment(lib, "d3dx9")
55 #pragma comment(lib, "QuartzCore_debug")
57 #pragma comment(lib, "QuartzCore")
60 static IDirect3D9* s_d3d = 0;
61 static IDirect3D9* d3d()
66 if (!LoadLibrary(TEXT("d3d9.dll")))
69 s_d3d = Direct3DCreate9(D3D_SDK_VERSION);
74 inline static CGRect winRectToCGRect(RECT rc)
76 return CGRectMake(rc.left, rc.top, (rc.right - rc.left), (rc.bottom - rc.top));
79 inline static CGRect winRectToCGRect(RECT rc, RECT relativeToRect)
81 return CGRectMake(rc.left, (relativeToRect.bottom-rc.bottom), (rc.right - rc.left), (rc.bottom - rc.top));
86 static D3DPRESENT_PARAMETERS initialPresentationParameters()
88 D3DPRESENT_PARAMETERS parameters = {0};
89 parameters.Windowed = TRUE;
90 parameters.SwapEffect = D3DSWAPEFFECT_COPY;
91 parameters.BackBufferCount = 1;
92 parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
93 parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
98 // FIXME: <rdar://6507851> Share this code with CoreAnimation.
99 static bool hardwareCapabilitiesIndicateCoreAnimationSupport(const D3DCAPS9& caps)
101 // CoreAnimation needs two or more texture units.
102 if (caps.MaxTextureBlendStages < 2)
105 // CoreAnimation needs non-power-of-two textures.
106 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL))
109 // CoreAnimation needs vertex shader 2.0 or greater.
110 if (D3DSHADER_VERSION_MAJOR(caps.VertexShaderVersion) < 2)
113 // CoreAnimation needs pixel shader 2.0 or greater.
114 if (D3DSHADER_VERSION_MAJOR(caps.PixelShaderVersion) < 2)
120 bool WKCACFLayerRenderer::acceleratedCompositingAvailable()
122 static bool available;
130 // Initialize available to true since this function will be called from a
131 // propagation within createRenderer(). We want to be able to return true
132 // when that happens so that the test can continue.
135 HMODULE library = LoadLibrary(TEXT("d3d9.dll"));
141 FreeLibrary(library);
143 library = LoadLibrary(TEXT("QuartzCore_debug.dll"));
145 library = LoadLibrary(TEXT("QuartzCore.dll"));
152 FreeLibrary(library);
154 // Make a dummy HWND.
155 WNDCLASSEX wcex = { 0 };
156 wcex.cbSize = sizeof(WNDCLASSEX);
157 wcex.lpfnWndProc = DefWindowProc;
158 wcex.hInstance = WebCore::instanceHandle();
159 wcex.lpszClassName = L"CoreAnimationTesterWindowClass";
160 ::RegisterClassEx(&wcex);
161 HWND testWindow = ::CreateWindow(L"CoreAnimationTesterWindowClass", L"CoreAnimationTesterWindow", WS_POPUP, -500, -500, 0, 0, 0, 0, 0, 0);
168 OwnPtr<WKCACFLayerRenderer> testLayerRenderer = WKCACFLayerRenderer::create(0);
169 testLayerRenderer->setHostWindow(testWindow);
170 available = testLayerRenderer->createRenderer();
171 ::DestroyWindow(testWindow);
176 PassOwnPtr<WKCACFLayerRenderer> WKCACFLayerRenderer::create(WKCACFLayerRendererClient* client)
178 if (!acceleratedCompositingAvailable())
180 return new WKCACFLayerRenderer(client);
183 WKCACFLayerRenderer::WKCACFLayerRenderer(WKCACFLayerRendererClient* client)
185 , m_mightBeAbleToCreateDeviceLater(true)
186 , m_rootLayer(PlatformCALayer::create(PlatformCALayer::LayerTypeRootLayer, 0))
187 , m_context(wkCACFContextCreate())
189 , m_renderTimer(this, &WKCACFLayerRenderer::renderTimerFired)
190 , m_mustResetLostDeviceBeforeRendering(false)
191 , m_syncLayerChanges(false)
193 // Point the CACFContext to this
194 wkCACFContextSetUserData(m_context, this);
196 // Under the root layer, we have a clipping layer to clip the content,
197 // that contains a scroll layer that we use for scrolling the content.
198 // The root layer is the size of the client area of the window.
199 // The clipping layer is the size of the WebView client area (window less the scrollbars).
200 // The scroll layer is the size of the root child layer.
201 // Resizing the window will change the bounds of the rootLayer and the clip layer and will not
202 // cause any repositioning.
203 // Scrolling will affect only the position of the scroll layer without affecting the bounds.
205 m_rootLayer->setName("WKCACFLayerRenderer rootLayer");
206 m_rootLayer->setAnchorPoint(FloatPoint3D(0, 0, 0));
207 m_rootLayer->setGeometryFlipped(true);
210 CGColorRef debugColor = CGColorCreateGenericRGB(1, 0, 0, 0.8);
211 m_rootLayer->setBackgroundColor(debugColor);
212 CGColorRelease(debugColor);
216 wkCACFContextSetLayer(m_context, m_rootLayer->platformLayer());
219 char* printTreeFlag = getenv("CA_PRINT_TREE");
220 m_printTree = printTreeFlag && atoi(printTreeFlag);
224 WKCACFLayerRenderer::~WKCACFLayerRenderer()
227 WKCACFContextFlusher::shared().removeContext(m_context);
228 wkCACFContextDestroy(m_context);
231 PlatformCALayer* WKCACFLayerRenderer::rootLayer() const
233 return m_rootLayer.get();
236 void WKCACFLayerRenderer::addPendingAnimatedLayer(PassRefPtr<PlatformCALayer> layer)
238 m_pendingAnimatedLayers.add(layer);
241 void WKCACFLayerRenderer::setRootChildLayer(PlatformCALayer* layer)
243 m_rootLayer->removeAllSublayers();
244 m_rootChildLayer = layer;
245 if (m_rootChildLayer)
246 m_rootLayer->appendSublayer(m_rootChildLayer.get());
249 void WKCACFLayerRenderer::layerTreeDidChange()
251 WKCACFContextFlusher::shared().addContext(m_context);
255 void WKCACFLayerRenderer::setNeedsDisplay(bool sync)
258 m_rootLayer->setNeedsDisplay(0);
261 syncCompositingStateSoon();
266 bool WKCACFLayerRenderer::createRenderer()
268 if (m_d3dDevice || !m_mightBeAbleToCreateDeviceLater)
271 m_mightBeAbleToCreateDeviceLater = false;
272 D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
274 if (!d3d() || !::IsWindow(m_hostWindow))
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_hostWindow, &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_hostWindow, 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 wkCACFContextInitializeD3DDevice(m_context, m_d3dDevice.get());
332 if (IsWindow(m_hostWindow))
333 m_rootLayer->setBounds(bounds());
338 void WKCACFLayerRenderer::destroyRenderer()
340 wkCACFContextSetLayer(m_context, m_rootLayer->platformLayer());
348 m_rootChildLayer = 0;
350 m_mightBeAbleToCreateDeviceLater = true;
353 void WKCACFLayerRenderer::resize()
358 // Resetting the device might fail here. But that's OK, because if it does it we will attempt to
359 // reset the device the next time we try to render.
360 resetDevice(ChangedWindowSize);
363 m_rootLayer->setBounds(bounds());
364 WKCACFContextFlusher::shared().flushAllContexts();
368 static void getDirtyRects(HWND window, Vector<CGRect>& outRects)
370 ASSERT_ARG(outRects, outRects.isEmpty());
373 if (!GetClientRect(window, &clientRect))
376 OwnPtr<HRGN> region(CreateRectRgn(0, 0, 0, 0));
377 int regionType = GetUpdateRgn(window, region.get(), false);
378 if (regionType != COMPLEXREGION) {
380 if (GetUpdateRect(window, &dirtyRect, false))
381 outRects.append(winRectToCGRect(dirtyRect, clientRect));
385 DWORD dataSize = GetRegionData(region.get(), 0, 0);
386 OwnArrayPtr<unsigned char> regionDataBuffer(new unsigned char[dataSize]);
387 RGNDATA* regionData = reinterpret_cast<RGNDATA*>(regionDataBuffer.get());
388 if (!GetRegionData(region.get(), dataSize, regionData))
391 outRects.resize(regionData->rdh.nCount);
393 RECT* rect = reinterpret_cast<RECT*>(regionData->Buffer);
394 for (size_t i = 0; i < outRects.size(); ++i, ++rect)
395 outRects[i] = winRectToCGRect(*rect, clientRect);
398 void WKCACFLayerRenderer::renderTimerFired(Timer<WKCACFLayerRenderer>*)
403 void WKCACFLayerRenderer::paint()
407 if (m_mightBeAbleToCreateDeviceLater)
412 Vector<CGRect> dirtyRects;
413 getDirtyRects(m_hostWindow, dirtyRects);
417 void WKCACFLayerRenderer::render(const Vector<CGRect>& windowDirtyRects)
421 if (m_mustResetLostDeviceBeforeRendering && !resetDevice(LostDevice)) {
422 // We can't reset the device right now. Try again soon.
427 if (m_client && !m_client->shouldRender()) {
432 // Sync the layer if needed
433 if (m_syncLayerChanges) {
434 m_client->syncCompositingState();
435 m_syncLayerChanges = 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 WKCACFLayerRenderer::renderSoon()
522 if (!m_renderTimer.isActive())
523 m_renderTimer.startOneShot(0);
526 void WKCACFLayerRenderer::syncCompositingStateSoon()
528 m_syncLayerChanges = true;
532 CGRect WKCACFLayerRenderer::bounds() const
535 GetClientRect(m_hostWindow, &clientRect);
537 return winRectToCGRect(clientRect);
540 void WKCACFLayerRenderer::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 WKCACFLayerRenderer::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)