void WKCACFLayerRenderer::createRenderer()
{
    if (m_triedToCreateD3DRenderer)
        return;

    m_triedToCreateD3DRenderer = true;
    D3DPRESENT_PARAMETERS parameters = initialPresentationParameters();

    if (!d3d() || !::IsWindow(m_hostWindow))
        return;

    // D3D doesn't like to make back buffers for 0 size windows. We skirt this problem if we make the
    // passed backbuffer width and height non-zero. The window will necessarily get set to a non-zero
    // size eventually, and then the backbuffer size will get reset.
    RECT rect;
    GetClientRect(m_hostWindow, &rect);

    if (rect.left-rect.right == 0 || rect.bottom-rect.top == 0) {
        parameters.BackBufferWidth = 1;
        parameters.BackBufferHeight = 1;
    }

    if (FAILED(d3d()->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hostWindow, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_FPU_PRESERVE, &parameters, &m_d3dDevice)))
        return;

    D3DXMATRIXA16 projection;
    D3DXMatrixOrthoOffCenterRH(&projection, rect.left, rect.right, rect.top, rect.bottom, -1.0f, 1.0f);

    m_d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);

    m_context.adoptCF(CACFContextCreate(0));
    windowsForContexts().set(m_context.get(), this);

    m_renderContext = static_cast<CARenderContext*>(CACFContextGetRenderContext(m_context.get()));
    m_renderer = CARenderOGLNew(wkqcCARenderOGLCallbacks(wkqckCARenderDX9Callbacks), m_d3dDevice.get(), 0);

    // Create the root hierarchy
    m_rootLayer = WKCACFLayer::create(WKCACFLayer::Layer);
    m_rootLayer->setName("WKCACFLayerRenderer rootLayer");
    m_scrollLayer = WKCACFLayer::create(WKCACFLayer::Layer);
    m_scrollLayer->setName("WKCACFLayerRenderer scrollLayer");

    m_rootLayer->addSublayer(m_scrollLayer);
    m_scrollLayer->setMasksToBounds(true);
    m_scrollLayer->setAnchorPoint(CGPointMake(0, 1));

#ifndef NDEBUG
    CGColorRef debugColor = createCGColor(Color(255, 0, 0, 204));
    m_rootLayer->setBackgroundColor(debugColor);
    CGColorRelease(debugColor);
#endif

    if (IsWindow(m_hostWindow))
        m_rootLayer->setFrame(bounds());

    if (m_context)
        m_rootLayer->becomeRootLayerForContext(m_context.get());
}
Exemplo n.º 2
0
WKCACFLayerRenderer::WKCACFLayerRenderer(WKCACFLayerRendererClient* client)
    : m_client(client)
    , m_mightBeAbleToCreateDeviceLater(true)
    , m_rootLayer(WKCACFRootLayer::create(this))
    , m_scrollLayer(WKCACFLayer::create(WKCACFLayer::Layer))
    , m_clipLayer(WKCACFLayer::create(WKCACFLayer::Layer))
    , m_context(AdoptCF, CACFContextCreate(0))
    , m_renderContext(static_cast<CARenderContext*>(CACFContextGetRenderContext(m_context.get())))
    , m_renderer(0)
    , m_hostWindow(0)
    , m_renderTimer(this, &WKCACFLayerRenderer::renderTimerFired)
    , m_scrollPosition(0, 0)
    , m_scrollSize(1, 1)
    , m_backingStoreDirty(false)
    , m_mustResetLostDeviceBeforeRendering(false)
{
    windowsForContexts().set(m_context.get(), this);

    // Under the root layer, we have a clipping layer to clip the content,
    // that contains a scroll layer that we use for scrolling the content.
    // The root layer is the size of the client area of the window.
    // The clipping layer is the size of the WebView client area (window less the scrollbars).
    // The scroll layer is the size of the root child layer.
    // Resizing the window will change the bounds of the rootLayer and the clip layer and will not
    // cause any repositioning.
    // Scrolling will affect only the position of the scroll layer without affecting the bounds.

    m_rootLayer->setName("WKCACFLayerRenderer rootLayer");
    m_clipLayer->setName("WKCACFLayerRenderer clipLayer");
    m_scrollLayer->setName("WKCACFLayerRenderer scrollLayer");

    m_rootLayer->addSublayer(m_clipLayer);
    m_clipLayer->addSublayer(m_scrollLayer);
    m_clipLayer->setMasksToBounds(true);
    m_rootLayer->setAnchorPoint(CGPointMake(0, 0));
    m_scrollLayer->setAnchorPoint(CGPointMake(0, 1));
    m_clipLayer->setAnchorPoint(CGPointMake(0, 1));

#ifndef NDEBUG
    CGColorRef debugColor = createCGColor(Color(255, 0, 0, 204));
    m_rootLayer->setBackgroundColor(debugColor);
    CGColorRelease(debugColor);
#endif

    if (m_context)
        m_rootLayer->becomeRootLayerForContext(m_context.get());

#ifndef NDEBUG
    char* printTreeFlag = getenv("CA_PRINT_TREE");
    m_printTree = printTreeFlag && atoi(printTreeFlag);
#endif
}