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 "WKCACFContextFlusher.h"
37 #include "WKCACFLayer.h"
38 #include "WebCoreInstanceHandle.h"
39 #include <WebKitSystemInterface/WebKitSystemInterface.h>
40 #include <wtf/HashMap.h>
41 #include <wtf/OwnArrayPtr.h>
42 #include <wtf/OwnPtr.h>
43 #include <wtf/PassOwnPtr.h>
44 #include <wtf/StdLibExtras.h>
48 #pragma comment(lib, "d3d9")
49 #pragma comment(lib, "d3dx9")
51 #pragma comment(lib, "QuartzCore_debug")
53 #pragma comment(lib, "QuartzCore")
56 static IDirect3D9* s_d3d = 0;
57 static IDirect3D9* d3d()
62 if (!LoadLibrary(TEXT("d3d9.dll")))
65 s_d3d = Direct3DCreate9(D3D_SDK_VERSION);
70 inline static CGRect winRectToCGRect(RECT rc)
72 return CGRectMake(rc.left, rc.top, (rc.right - rc.left), (rc.bottom - rc.top));
75 inline static CGRect winRectToCGRect(RECT rc, RECT relativeToRect)
77 return CGRectMake(rc.left, (relativeToRect.bottom-rc.bottom), (rc.right - rc.left), (rc.bottom - rc.top));
82 // Subclass of WKCACFLayer to allow the root layer to have a back pointer to the layer renderer
84 class WKCACFRootLayer : public WKCACFLayer {
86 WKCACFRootLayer(WKCACFLayerRenderer* renderer)
87 : WKCACFLayer(WKCACFLayer::Layer)
89 m_renderer = renderer;
92 static PassRefPtr<WKCACFRootLayer> create(WKCACFLayerRenderer* renderer)
94 if (!WKCACFLayerRenderer::acceleratedCompositingAvailable())
96 return adoptRef(new WKCACFRootLayer(renderer));
99 virtual void setNeedsRender() { m_renderer->layerTreeDidChange(); }
101 // Overload this to avoid calling setNeedsDisplay on the layer, which would override the contents
102 // we have placed on the root layer.
103 virtual void setNeedsDisplay(const CGRect* dirtyRect) { setNeedsCommit(); }
106 WKCACFLayerRenderer* m_renderer;
109 typedef HashMap<WKCACFContext*, WKCACFLayerRenderer*> ContextToWindowMap;
111 static ContextToWindowMap& windowsForContexts()
113 DEFINE_STATIC_LOCAL(ContextToWindowMap, map, ());
117 static D3DPRESENT_PARAMETERS initialPresentationParameters()
119 D3DPRESENT_PARAMETERS parameters = {0};
120 parameters.Windowed = TRUE;
121 parameters.SwapEffect = D3DSWAPEFFECT_COPY;
122 parameters.BackBufferCount = 1;
123 parameters.BackBufferFormat = D3DFMT_A8R8G8B8;
124 parameters.MultiSampleType = D3DMULTISAMPLE_NONE;
129 // FIXME: <rdar://6507851> Share this code with CoreAnimation.
130 static bool hardwareCapabilitiesIndicateCoreAnimationSupport(const D3DCAPS9& caps)
132 // CoreAnimation needs two or more texture units.
133 if (caps.MaxTextureBlendStages < 2)
136 // CoreAnimation needs non-power-of-two textures.
137 if ((caps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(caps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL))
140 // CoreAnimation needs vertex shader 2.0 or greater.
141 if (D3DSHADER_VERSION_MAJOR(caps.VertexShaderVersion) < 2)
144 // CoreAnimation needs pixel shader 2.0 or greater.
145 if (D3DSHADER_VERSION_MAJOR(caps.PixelShaderVersion) < 2)
151 bool WKCACFLayerRenderer::acceleratedCompositingAvailable()
153 static bool available;
161 // Initialize available to true since this function will be called from a
162 // propagation within createRenderer(). We want to be able to return true
163 // when that happens so that the test can continue.
166 HMODULE library = LoadLibrary(TEXT("d3d9.dll"));
172 FreeLibrary(library);
174 library = LoadLibrary(TEXT("QuartzCore_debug.dll"));
176 library = LoadLibrary(TEXT("QuartzCore.dll"));
183 FreeLibrary(library);
185 // Make a dummy HWND.
186 WNDCLASSEX wcex = { 0 };
187 wcex.cbSize = sizeof(WNDCLASSEX);
188 wcex.lpfnWndProc = DefWindowProc;
189 wcex.hInstance = WebCore::instanceHandle();
190 wcex.lpszClassName = L"CoreAnimationTesterWindowClass";
191 ::RegisterClassEx(&wcex);
192 HWND testWindow = ::CreateWindow(L"CoreAnimationTesterWindowClass", L"CoreAnimationTesterWindow", WS_POPUP, -500, -500, 0, 0, 0, 0, 0, 0);
199 OwnPtr<WKCACFLayerRenderer> testLayerRenderer = WKCACFLayerRenderer::create(0);
200 testLayerRenderer->setHostWindow(testWindow);
201 available = testLayerRenderer->createRenderer();
202 ::DestroyWindow(testWindow);
207 void WKCACFLayerRenderer::didFlushContext(WKCACFContext* context)
209 WKCACFLayerRenderer* window = windowsForContexts().get(context);
213 window->renderSoon();
216 PassOwnPtr<WKCACFLayerRenderer> WKCACFLayerRenderer::create(WKCACFLayerRendererClient* client)
218 if (!acceleratedCompositingAvailable())
220 return new WKCACFLayerRenderer(client);
223 WKCACFLayerRenderer::WKCACFLayerRenderer(WKCACFLayerRendererClient* client)
225 , m_mightBeAbleToCreateDeviceLater(true)
226 , m_rootLayer(WKCACFRootLayer::create(this))
227 , m_scrollLayer(WKCACFLayer::create(WKCACFLayer::Layer))
228 , m_clipLayer(WKCACFLayer::create(WKCACFLayer::Layer))
229 , m_context(wkCACFContextCreate())
231 , m_renderTimer(this, &WKCACFLayerRenderer::renderTimerFired)
232 , m_scrollPosition(0, 0)
234 , m_backingStoreDirty(false)
235 , m_mustResetLostDeviceBeforeRendering(false)
237 windowsForContexts().set(m_context, this);
239 // Under the root layer, we have a clipping layer to clip the content,
240 // that contains a scroll layer that we use for scrolling the content.
241 // The root layer is the size of the client area of the window.
242 // The clipping layer is the size of the WebView client area (window less the scrollbars).
243 // The scroll layer is the size of the root child layer.
244 // Resizing the window will change the bounds of the rootLayer and the clip layer and will not
245 // cause any repositioning.
246 // Scrolling will affect only the position of the scroll layer without affecting the bounds.
248 m_rootLayer->setName("WKCACFLayerRenderer rootLayer");
249 m_clipLayer->setName("WKCACFLayerRenderer clipLayer");
250 m_scrollLayer->setName("WKCACFLayerRenderer scrollLayer");
252 m_rootLayer->addSublayer(m_clipLayer);
253 m_clipLayer->addSublayer(m_scrollLayer);
254 m_clipLayer->setMasksToBounds(true);
255 m_rootLayer->setAnchorPoint(CGPointMake(0, 0));
256 m_scrollLayer->setAnchorPoint(CGPointMake(0, 1));
257 m_clipLayer->setAnchorPoint(CGPointMake(0, 1));
260 CGColorRef debugColor = createCGColor(Color(255, 0, 0, 204));
261 m_rootLayer->setBackgroundColor(debugColor);
262 CGColorRelease(debugColor);
266 m_rootLayer->becomeRootLayerForContext(m_context);
269 char* printTreeFlag = getenv("CA_PRINT_TREE");
270 m_printTree = printTreeFlag && atoi(printTreeFlag);
274 WKCACFLayerRenderer::~WKCACFLayerRenderer()
277 wkCACFContextDestroy(m_context);
280 WKCACFLayer* WKCACFLayerRenderer::rootLayer() const
282 return m_rootLayer.get();
285 void WKCACFLayerRenderer::setScrollFrame(const IntPoint& position, const IntSize& size)
288 m_scrollPosition = position;
293 void WKCACFLayerRenderer::updateScrollFrame()
295 CGRect frameBounds = bounds();
296 m_clipLayer->setBounds(CGRectMake(0, 0, m_scrollSize.width(), m_scrollSize.height()));
297 m_clipLayer->setPosition(CGPointMake(0, frameBounds.size.height));
298 if (m_rootChildLayer) {
299 CGRect rootBounds = m_rootChildLayer->bounds();
300 m_scrollLayer->setBounds(rootBounds);
302 m_scrollLayer->setPosition(CGPointMake(-m_scrollPosition.x(), m_scrollPosition.y() + m_scrollSize.height()));
305 void WKCACFLayerRenderer::setRootContents(CGImageRef image)
308 m_rootLayer->setContents(image);
312 void WKCACFLayerRenderer::setRootContentsAndDisplay(CGImageRef image)
315 m_rootLayer->setContents(image);
319 void WKCACFLayerRenderer::setRootChildLayer(WKCACFLayer* layer)
324 m_scrollLayer->removeAllSublayers();
325 m_rootChildLayer = layer;
327 m_scrollLayer->addSublayer(layer);
328 // Adjust the scroll frame accordingly
333 void WKCACFLayerRenderer::layerTreeDidChange()
335 WKCACFContextFlusher::shared().addContext(m_context);
339 void WKCACFLayerRenderer::setNeedsDisplay()
342 m_rootLayer->setNeedsDisplay(0);
346 bool WKCACFLayerRenderer::createRenderer()
348 if (m_d3dDevice || !m_mightBeAbleToCreateDeviceLater)
351 m_mightBeAbleToCreateDeviceLater = false;
352 D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
354 if (!d3d() || !::IsWindow(m_hostWindow))
357 // D3D doesn't like to make back buffers for 0 size windows. We skirt this problem if we make the
358 // passed backbuffer width and height non-zero. The window will necessarily get set to a non-zero
359 // size eventually, and then the backbuffer size will get reset.
361 GetClientRect(m_hostWindow, &rect);
363 if (rect.left-rect.right == 0 || rect.bottom-rect.top == 0) {
364 parameters.BackBufferWidth = 1;
365 parameters.BackBufferHeight = 1;
369 if (FAILED(d3d()->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps)))
372 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE;
373 if ((d3dCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) && d3dCaps.VertexProcessingCaps)
374 behaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
376 behaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
378 COMPtr<IDirect3DDevice9> device;
379 if (FAILED(d3d()->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hostWindow, behaviorFlags, ¶meters, &device))) {
380 // In certain situations (e.g., shortly after waking from sleep), Direct3DCreate9() will
381 // return an IDirect3D9 for which IDirect3D9::CreateDevice will always fail. In case we
382 // have one of these bad IDirect3D9s, get rid of it so we'll fetch a new one the next time
383 // we want to call CreateDevice.
387 // Even if we don't have a bad IDirect3D9, in certain situations (e.g., shortly after
388 // waking from sleep), CreateDevice will fail, but will later succeed if called again.
389 m_mightBeAbleToCreateDeviceLater = true;
394 // Now that we've created the IDirect3DDevice9 based on the capabilities we
395 // got from the IDirect3D9 global object, we requery the device for its
396 // actual capabilities. The capabilities returned by the device can
397 // sometimes be more complete, for example when using software vertex
400 if (FAILED(device->GetDeviceCaps(&deviceCaps)))
403 if (!hardwareCapabilitiesIndicateCoreAnimationSupport(deviceCaps))
406 m_d3dDevice = device;
408 D3DXMATRIXA16 projection;
409 D3DXMatrixOrthoOffCenterRH(&projection, rect.left, rect.right, rect.top, rect.bottom, -1.0f, 1.0f);
411 m_d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
413 wkCACFContextInitializeD3DDevice(m_context, m_d3dDevice.get());
415 if (IsWindow(m_hostWindow))
416 m_rootLayer->setBounds(bounds());
421 void WKCACFLayerRenderer::destroyRenderer()
424 windowsForContexts().remove(m_context);
425 WKCACFContextFlusher::shared().removeContext(m_context);
436 m_rootChildLayer = 0;
438 m_mightBeAbleToCreateDeviceLater = true;
441 void WKCACFLayerRenderer::resize()
446 // Resetting the device might fail here. But that's OK, because if it does it we will attempt to
447 // reset the device the next time we try to render.
448 resetDevice(ChangedWindowSize);
451 m_rootLayer->setBounds(bounds());
452 WKCACFContextFlusher::shared().flushAllContexts();
457 static void getDirtyRects(HWND window, Vector<CGRect>& outRects)
459 ASSERT_ARG(outRects, outRects.isEmpty());
462 if (!GetClientRect(window, &clientRect))
465 OwnPtr<HRGN> region(CreateRectRgn(0, 0, 0, 0));
466 int regionType = GetUpdateRgn(window, region.get(), false);
467 if (regionType != COMPLEXREGION) {
469 if (GetUpdateRect(window, &dirtyRect, false))
470 outRects.append(winRectToCGRect(dirtyRect, clientRect));
474 DWORD dataSize = GetRegionData(region.get(), 0, 0);
475 OwnArrayPtr<unsigned char> regionDataBuffer(new unsigned char[dataSize]);
476 RGNDATA* regionData = reinterpret_cast<RGNDATA*>(regionDataBuffer.get());
477 if (!GetRegionData(region.get(), dataSize, regionData))
480 outRects.resize(regionData->rdh.nCount);
482 RECT* rect = reinterpret_cast<RECT*>(regionData->Buffer);
483 for (size_t i = 0; i < outRects.size(); ++i, ++rect)
484 outRects[i] = winRectToCGRect(*rect, clientRect);
487 void WKCACFLayerRenderer::renderTimerFired(Timer<WKCACFLayerRenderer>*)
492 void WKCACFLayerRenderer::paint()
496 if (m_mightBeAbleToCreateDeviceLater)
501 if (m_backingStoreDirty) {
502 // If the backing store is still dirty when we are about to draw the
503 // composited content, we need to force the window to paint into the
504 // backing store. The paint will only paint the dirty region that
505 // if being tracked in WebView.
506 UpdateWindow(m_hostWindow);
510 Vector<CGRect> dirtyRects;
511 getDirtyRects(m_hostWindow, dirtyRects);
515 void WKCACFLayerRenderer::render(const Vector<CGRect>& windowDirtyRects)
519 if (m_mustResetLostDeviceBeforeRendering && !resetDevice(LostDevice)) {
520 // We can't reset the device right now. Try again soon.
525 if (m_client && !m_client->shouldRender()) {
530 // Flush the root layer to the render tree.
531 WKCACFContextFlusher::shared().flushAllContexts();
533 CGRect bounds = this->bounds();
535 CFTimeInterval t = CACurrentMediaTime();
537 // Give the renderer some space to use. This needs to be valid until the
538 // wkCACFContextFinishUpdate() call below.
540 if (!wkCACFContextBeginUpdate(m_context, space, sizeof(space), t, bounds, windowDirtyRects.data(), windowDirtyRects.size()))
545 // FIXME: don't need to clear dirty region if layer tree is opaque.
547 WKCACFUpdateRectEnumerator* e = wkCACFContextCopyUpdateRectEnumerator(m_context);
551 Vector<D3DRECT, 64> rects;
552 for (const CGRect* r = wkCACFUpdateRectEnumeratorNextRect(e); r; r = wkCACFUpdateRectEnumeratorNextRect(e)) {
554 rect.x1 = r->origin.x;
555 rect.x2 = rect.x1 + r->size.width;
556 rect.y1 = bounds.origin.y + bounds.size.height - (r->origin.y + r->size.height);
557 rect.y2 = rect.y1 + r->size.height;
561 wkCACFUpdateRectEnumeratorRelease(e);
566 m_d3dDevice->Clear(rects.size(), rects.data(), D3DCLEAR_TARGET, 0, 1.0f, 0);
568 m_d3dDevice->BeginScene();
569 wkCACFContextRenderUpdate(m_context);
570 m_d3dDevice->EndScene();
572 err = m_d3dDevice->Present(0, 0, 0, 0);
574 if (err == D3DERR_DEVICELOST) {
575 wkCACFContextAddUpdateRect(m_context, bounds);
576 if (!resetDevice(LostDevice)) {
577 // We can't reset the device right now. Try again soon.
582 } while (err == D3DERR_DEVICELOST);
584 wkCACFContextFinishUpdate(m_context);
588 m_rootLayer->printTree();
592 void WKCACFLayerRenderer::renderSoon()
594 if (!m_renderTimer.isActive())
595 m_renderTimer.startOneShot(0);
598 CGRect WKCACFLayerRenderer::bounds() const
601 GetClientRect(m_hostWindow, &clientRect);
603 return winRectToCGRect(clientRect);
606 void WKCACFLayerRenderer::initD3DGeometry()
610 CGRect bounds = this->bounds();
612 float x0 = bounds.origin.x;
613 float y0 = bounds.origin.y;
614 float x1 = x0 + bounds.size.width;
615 float y1 = y0 + bounds.size.height;
617 D3DXMATRIXA16 projection;
618 D3DXMatrixOrthoOffCenterRH(&projection, x0, x1, y0, y1, -1.0f, 1.0f);
620 m_d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
623 bool WKCACFLayerRenderer::resetDevice(ResetReason reason)
628 HRESULT hr = m_d3dDevice->TestCooperativeLevel();
630 if (hr == D3DERR_DEVICELOST || hr == D3DERR_DRIVERINTERNALERROR) {
631 // The device cannot be reset at this time. Try again soon.
632 m_mustResetLostDeviceBeforeRendering = true;
636 m_mustResetLostDeviceBeforeRendering = false;
638 if (reason == LostDevice && hr == D3D_OK) {
639 // The device wasn't lost after all.
643 // We can reset the device.
645 // We have to release the context's D3D resrouces whenever we reset the IDirect3DDevice9 in order to
646 // destroy any D3DPOOL_DEFAULT resources that Core Animation has allocated (e.g., textures used
647 // for mask layers). See <http://msdn.microsoft.com/en-us/library/bb174425(v=VS.85).aspx>.
648 wkCACFContextReleaseD3DResources(m_context);
650 D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();
651 hr = m_d3dDevice->Reset(¶meters);
653 // TestCooperativeLevel told us the device may be reset now, so we should
654 // not be told here that the device is lost.
655 ASSERT(hr != D3DERR_DEVICELOST);
664 #endif // USE(ACCELERATED_COMPOSITING)