2013年10月14日 星期一

[W8App] D2DERR_WRONG_FACTORY (0x88990012)

最近在學習W8App的Direct2D與DirectWrite時發生了一個例外的錯誤,當呼叫D2D的EndDraw來更新繪圖結果是產生了0x88990012 (D2DERR_WRONG_FACTORY)的例外錯誤。

這個錯誤發生的原因是我的DirectWrite Render物件預先儲存了D2D factory,而透過SurfaceImageSource來連結W8App與Direct2D時,D2DContext所使用的D2D factory似乎有自已獨立的產生方式。

目前的解決方式是透過GetFactory直接從D2DContext物件中取得D2DFactory,至於是否有更好的作法,就需要再研究。

   1: IFACEMETHODIMP CustomTextRenderer::DrawGlyphRun(
   2:     _In_opt_ void* clientDrawingContext,
   3:     FLOAT baselineOriginX,
   4:     FLOAT baselineOriginY,
   5:     DWRITE_MEASURING_MODE measuringMode,
   6:     _In_ DWRITE_GLYPH_RUN const* glyphRun,
   7:     _In_ DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription,
   8:     IUnknown* clientDrawingEffect
   9:     )
  10: {
  11:     HRESULT hr = S_OK;
  12:  
  13:     Microsoft::WRL::ComPtr<ID2D1Factory> d2dFactory;
  14:     m_d2dContext->GetFactory(&d2dFactory);
  15:  
  16:     // Create the path geometry.
  17:     Microsoft::WRL::ComPtr<ID2D1PathGeometry> pathGeometry;
  18:     hr = d2dFactory->CreatePathGeometry(&pathGeometry);
  19:  
  20:     // Write to the path geometry using the geometry sink.
  21:  
  22:     Microsoft::WRL::ComPtr<ID2D1GeometrySink> sink;
  23:     if (SUCCEEDED(hr))
  24:     {
  25:         hr = pathGeometry->Open(&sink);
  26:     }
  27:  
  28:     // Get the glyph run outline geometries back from DirectWrite and place them within the
  29:     // geometry sink.
  30:     if (SUCCEEDED(hr))
  31:     {
  32:         hr = glyphRun->fontFace->GetGlyphRunOutline(
  33:             glyphRun->fontEmSize,
  34:             glyphRun->glyphIndices,
  35:             glyphRun->glyphAdvances,
  36:             glyphRun->glyphOffsets,
  37:             glyphRun->glyphCount,
  38:             glyphRun->isSideways,
  39:             glyphRun->bidiLevel %2,
  40:             sink.Get()
  41:             );
  42:     }
  43:  
  44:     // Close the geometry sink
  45:     if (SUCCEEDED(hr))
  46:     {
  47:         hr = sink.Get()->Close();
  48:     }
  49:  
  50:     // Initialize a matrix to translate the origin of the glyph run.
  51:     D2D1::Matrix3x2F const matrix = D2D1::Matrix3x2F(
  52:         1.0f, 0.0f,
  53:         0.0f, 1.0f,
  54:         baselineOriginX, baselineOriginY
  55:         );
  56:  
  57:     // Create the transformed geometry
  58:     Microsoft::WRL::ComPtr<ID2D1TransformedGeometry> transformedGeometry;
  59:     if (SUCCEEDED(hr))
  60:     {
  61:         hr = d2dFactory->CreateTransformedGeometry(
  62:             pathGeometry.Get(),
  63:             &matrix,
  64:             &transformedGeometry
  65:             );
  66:     }
  67:  
  68:     // Draw the outline of the glyph run
  69:     if(SUCCEEDED(hr))
  70:     {
  71:         m_d2dContext->DrawGeometry(transformedGeometry.Get(),m_brushOutline.Get());
  72:         m_d2dContext->FillGeometry(transformedGeometry.Get(),m_brushFill.Get());
  73:     }
  74:  
  75:     return hr;
  76: }

13-14 透過目前的D2DContext來取得D2DFactory

沒有留言:

張貼留言