already_AddRefed<Path> ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget) { #ifdef USE_SKIA if (aTarget->GetBackendType() == BackendType::SKIA) { SkPath path = GetSkiaPathForGlyphs(aBuffer); return MakeAndAddRef<PathSkia>(path, FillRule::FILL_WINDING); } #endif #ifdef USE_CAIRO if (aTarget->GetBackendType() == BackendType::CAIRO) { MOZ_ASSERT(mScaledFont); DrawTarget *dt = const_cast<DrawTarget*>(aTarget); cairo_t *ctx = static_cast<cairo_t*>(dt->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT)); bool isNewContext = !ctx; if (!ctx) { ctx = cairo_create(DrawTargetCairo::GetDummySurface()); cairo_matrix_t mat; GfxMatrixToCairoMatrix(aTarget->GetTransform(), mat); cairo_set_matrix(ctx, &mat); } cairo_set_scaled_font(ctx, mScaledFont); // Convert our GlyphBuffer into an array of Cairo glyphs. std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs); for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) { glyphs[i].index = aBuffer.mGlyphs[i].mIndex; glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x; glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y; } cairo_new_path(ctx); cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs); RefPtr<PathCairo> newPath = new PathCairo(ctx); if (isNewContext) { cairo_destroy(ctx); } return newPath.forget(); } #endif return nullptr; }
HDC gfxWindowsNativeDrawing::BeginNativeDrawing() { if (mRenderState == RENDER_STATE_INIT) { RefPtr<gfxASurface> surf; DrawTarget* drawTarget = mContext->GetDrawTarget(); cairo_t* cairo = nullptr; if (drawTarget->GetBackendType() == BackendType::CAIRO) { cairo = static_cast<cairo_t*> (drawTarget->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT)); if (cairo) { cairo_surface_t* s = cairo_get_target(cairo); if (s) { mDeviceOffset = mContext->GetDeviceOffset(); surf = gfxASurface::Wrap(s); } } } if (surf && surf->CairoStatus() != 0) return nullptr; gfxMatrix m = mContext->CurrentMatrix(); if (!m.HasNonTranslation()) mTransformType = TRANSLATION_ONLY; else if (m.HasNonAxisAlignedTransform()) mTransformType = COMPLEX; else mTransformType = AXIS_ALIGNED_SCALE; // if this is a native win32 surface, we don't have to // redirect rendering to our own HDC; in some cases, // we may be able to use the HDC from the surface directly. if (surf && ((surf->GetType() == gfxSurfaceType::Win32 || surf->GetType() == gfxSurfaceType::Win32Printing) && (surf->GetContentType() == gfxContentType::COLOR || (surf->GetContentType() == gfxContentType::COLOR_ALPHA && (mNativeDrawFlags & CAN_DRAW_TO_COLOR_ALPHA))))) { // grab the DC. This can fail if there is a complex clipping path, // in which case we'll have to fall back. mWinSurface = static_cast<gfxWindowsSurface*>(static_cast<gfxASurface*>(surf.get())); mDC = cairo_win32_get_dc_with_clip(cairo); if (mDC) { if (mTransformType == TRANSLATION_ONLY) { mRenderState = RENDER_STATE_NATIVE_DRAWING; mTranslation = m.GetTranslation(); } else if (((mTransformType == AXIS_ALIGNED_SCALE) && (mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) || (mNativeDrawFlags & CAN_COMPLEX_TRANSFORM)) { mWorldTransform.eM11 = (FLOAT) m._11; mWorldTransform.eM12 = (FLOAT) m._12; mWorldTransform.eM21 = (FLOAT) m._21; mWorldTransform.eM22 = (FLOAT) m._22; mWorldTransform.eDx = (FLOAT) m._31; mWorldTransform.eDy = (FLOAT) m._32; mRenderState = RENDER_STATE_NATIVE_DRAWING; } } } // If we couldn't do native drawing, then we have to do two-buffer drawing // and do alpha recovery if (mRenderState == RENDER_STATE_INIT) { mRenderState = RENDER_STATE_ALPHA_RECOVERY_BLACK; // We round out our native rect here, that way the snapping will // happen correctly. mNativeRect.RoundOut(); // we only do the scale bit if we can do an axis aligned // scale; otherwise we scale (if necessary) after // rendering with cairo. Note that if we're doing alpha recovery, // we cannot do a full complex transform with win32 (I mean, we could, but // it would require more code that's not here.) if (mTransformType == TRANSLATION_ONLY || !(mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) { mScale = gfxSize(1.0, 1.0); // Add 1 to the surface size; it's guaranteed to not be incorrect, // and it fixes bug 382458 // There's probably a better fix, but I haven't figured out // the root cause of the problem. mTempSurfaceSize = IntSize((int32_t) ceil(mNativeRect.Width() + 1), (int32_t) ceil(mNativeRect.Height() + 1)); } else { // figure out the scale factors mScale = m.ScaleFactors(true); mWorldTransform.eM11 = (FLOAT) mScale.width; mWorldTransform.eM12 = 0.0f; mWorldTransform.eM21 = 0.0f; mWorldTransform.eM22 = (FLOAT) mScale.height; mWorldTransform.eDx = 0.0f; mWorldTransform.eDy = 0.0f; // See comment above about "+1" mTempSurfaceSize = IntSize((int32_t) ceil(mNativeRect.Width() * mScale.width + 1), (int32_t) ceil(mNativeRect.Height() * mScale.height + 1)); } } } if (mRenderState == RENDER_STATE_NATIVE_DRAWING) { // we can just do native drawing directly to the context's surface // do we need to use SetWorldTransform? if (mTransformType != TRANSLATION_ONLY) { SetGraphicsMode(mDC, GM_ADVANCED); GetWorldTransform(mDC, &mOldWorldTransform); SetWorldTransform(mDC, &mWorldTransform); } GetViewportOrgEx(mDC, &mOrigViewportOrigin); SetViewportOrgEx(mDC, mOrigViewportOrigin.x - (int)mDeviceOffset.x, mOrigViewportOrigin.y - (int)mDeviceOffset.y, nullptr); return mDC; } else if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK || mRenderState == RENDER_STATE_ALPHA_RECOVERY_WHITE) { // we're going to use mWinSurface to create our temporary surface here // get us a RGB24 DIB; DIB is important, because // we can later call GetImageSurface on it. mWinSurface = new gfxWindowsSurface(mTempSurfaceSize); mDC = mWinSurface->GetDC(); RECT r = { 0, 0, mTempSurfaceSize.width, mTempSurfaceSize.height }; if (mRenderState == RENDER_STATE_ALPHA_RECOVERY_BLACK) FillRect(mDC, &r, (HBRUSH)GetStockObject(BLACK_BRUSH)); else FillRect(mDC, &r, (HBRUSH)GetStockObject(WHITE_BRUSH)); if ((mTransformType != TRANSLATION_ONLY) && (mNativeDrawFlags & CAN_AXIS_ALIGNED_SCALE)) { SetGraphicsMode(mDC, GM_ADVANCED); SetWorldTransform(mDC, &mWorldTransform); } return mDC; } else { NS_ERROR("Bogus render state!"); return nullptr; } }
void gfxXlibNativeRenderer::Draw(gfxContext* ctx, nsIntSize size, uint32_t flags, Screen *screen, Visual *visual) { gfxMatrix matrix = ctx->CurrentMatrix(); // We can only draw direct or onto a copied background if pixels align and // native drawing is compatible with the current operator. (The matrix is // actually also pixel-exact for flips and right-angle rotations, which // would permit copying the background but not drawing direct.) bool matrixIsIntegerTranslation = !matrix.HasNonIntegerTranslation(); bool canDrawOverBackground = matrixIsIntegerTranslation && ctx->CurrentOperator() == gfxContext::OPERATOR_OVER; // The padding of 0.5 for non-pixel-exact transformations used here is // the same as what _cairo_pattern_analyze_filter uses. const gfxFloat filterRadius = 0.5; gfxRect affectedRect(0.0, 0.0, size.width, size.height); if (!matrixIsIntegerTranslation) { // The filter footprint means that the affected rectangle is a // little larger than the drawingRect; affectedRect.Inflate(filterRadius); NATIVE_DRAWING_NOTE("FALLBACK: matrix not integer translation"); } else if (!canDrawOverBackground) { NATIVE_DRAWING_NOTE("FALLBACK: unsupported operator"); } // Clipping to the region affected by drawing allows us to consider only // the portions of the clip region that will be affected by drawing. gfxRect clipExtents; { gfxContextAutoSaveRestore autoSR(ctx); ctx->Clip(affectedRect); clipExtents = ctx->GetClipExtents(); if (clipExtents.IsEmpty()) return; // nothing to do if (canDrawOverBackground && DrawDirect(ctx, size, flags, screen, visual)) return; } IntRect drawingRect(IntPoint(0, 0), size); // Drawing need only be performed within the clip extents // (and padding for the filter). if (!matrixIsIntegerTranslation) { // The source surface may need to be a little larger than the clip // extents due to the filter footprint. clipExtents.Inflate(filterRadius); } clipExtents.RoundOut(); IntRect intExtents(int32_t(clipExtents.X()), int32_t(clipExtents.Y()), int32_t(clipExtents.Width()), int32_t(clipExtents.Height())); drawingRect.IntersectRect(drawingRect, intExtents); gfxPoint offset(drawingRect.x, drawingRect.y); DrawingMethod method; DrawTarget* drawTarget = ctx->GetDrawTarget(); Matrix dtTransform = drawTarget->GetTransform(); gfxPoint deviceTranslation = gfxPoint(dtTransform._31, dtTransform._32); cairo_surface_t* cairoTarget = static_cast<cairo_surface_t*> (drawTarget->GetNativeSurface(NativeSurfaceType::CAIRO_SURFACE)); cairo_surface_t* tempXlibSurface = CreateTempXlibSurface(cairoTarget, drawTarget, size, canDrawOverBackground, flags, screen, visual, &method); if (!tempXlibSurface) return; bool drawIsOpaque = (flags & DRAW_IS_OPAQUE) != 0; if (!drawIsOpaque) { cairo_t* tmpCtx = cairo_create(tempXlibSurface); if (method == eCopyBackground) { NS_ASSERTION(cairoTarget, "eCopyBackground only used when there's a cairoTarget"); cairo_set_operator(tmpCtx, CAIRO_OPERATOR_SOURCE); gfxPoint pt = -(offset + deviceTranslation); cairo_set_source_surface(tmpCtx, cairoTarget, pt.x, pt.y); // The copy from the tempXlibSurface to the target context should // use operator SOURCE, but that would need a mask to bound the // operation. Here we only copy opaque backgrounds so operator // OVER will behave like SOURCE masked by the surface. NS_ASSERTION(cairo_surface_get_content(tempXlibSurface) == CAIRO_CONTENT_COLOR, "Don't copy background with a transparent surface"); } else { cairo_set_operator(tmpCtx, CAIRO_OPERATOR_CLEAR); } cairo_paint(tmpCtx); cairo_destroy(tmpCtx); } if (!DrawOntoTempSurface(tempXlibSurface, -drawingRect.TopLeft())) { cairo_surface_destroy(tempXlibSurface); return; } SurfaceFormat moz2DFormat = cairo_surface_get_content(tempXlibSurface) == CAIRO_CONTENT_COLOR ? SurfaceFormat::B8G8R8A8 : SurfaceFormat::B8G8R8X8; if (method != eAlphaExtraction) { if (drawTarget) { NativeSurface native; native.mFormat = moz2DFormat; native.mType = NativeSurfaceType::CAIRO_SURFACE; native.mSurface = tempXlibSurface; native.mSize = size; RefPtr<SourceSurface> sourceSurface = drawTarget->CreateSourceSurfaceFromNativeSurface(native); if (sourceSurface) { drawTarget->DrawSurface(sourceSurface, Rect(offset.x, offset.y, size.width, size.height), Rect(0, 0, size.width, size.height)); } } else { nsRefPtr<gfxASurface> tmpSurf = gfxASurface::Wrap(tempXlibSurface); ctx->SetSource(tmpSurf, offset); ctx->Paint(); } cairo_surface_destroy(tempXlibSurface); return; } nsRefPtr<gfxImageSurface> blackImage = CopyXlibSurfaceToImage(tempXlibSurface, size, gfxImageFormat::ARGB32); cairo_t* tmpCtx = cairo_create(tempXlibSurface); cairo_set_source_rgba(tmpCtx, 1.0, 1.0, 1.0, 1.0); cairo_set_operator(tmpCtx, CAIRO_OPERATOR_SOURCE); cairo_paint(tmpCtx); cairo_destroy(tmpCtx); DrawOntoTempSurface(tempXlibSurface, -drawingRect.TopLeft()); nsRefPtr<gfxImageSurface> whiteImage = CopyXlibSurfaceToImage(tempXlibSurface, size, gfxImageFormat::RGB24); if (blackImage->CairoStatus() == CAIRO_STATUS_SUCCESS && whiteImage->CairoStatus() == CAIRO_STATUS_SUCCESS) { if (!gfxAlphaRecovery::RecoverAlpha(blackImage, whiteImage)) { cairo_surface_destroy(tempXlibSurface); return; } gfxASurface* paintSurface = blackImage; if (drawTarget) { NativeSurface native; native.mFormat = moz2DFormat; native.mType = NativeSurfaceType::CAIRO_SURFACE; native.mSurface = paintSurface->CairoSurface(); native.mSize = size; RefPtr<SourceSurface> sourceSurface = drawTarget->CreateSourceSurfaceFromNativeSurface(native); if (sourceSurface) { drawTarget->DrawSurface(sourceSurface, Rect(offset.x, offset.y, size.width, size.height), Rect(0, 0, size.width, size.height)); } } else { ctx->SetSource(paintSurface, offset); ctx->Paint(); } } cairo_surface_destroy(tempXlibSurface); }
TemporaryRef<Path> ScaledFontBase::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget) { #ifdef USE_SKIA if (aTarget->GetType() == BACKEND_SKIA) { SkPaint paint; paint.setTypeface(GetSkTypeface()); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); paint.setTextSize(SkFloatToScalar(mSize)); std::vector<uint16_t> indices; std::vector<SkPoint> offsets; indices.resize(aBuffer.mNumGlyphs); offsets.resize(aBuffer.mNumGlyphs); for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) { indices[i] = aBuffer.mGlyphs[i].mIndex; offsets[i].fX = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.x); offsets[i].fY = SkFloatToScalar(aBuffer.mGlyphs[i].mPosition.y); } SkPath path; paint.getPosTextPath(&indices.front(), aBuffer.mNumGlyphs*2, &offsets.front(), &path); return new PathSkia(path, FILL_WINDING); } #endif #ifdef USE_CAIRO if (aTarget->GetType() == BACKEND_CAIRO) { MOZ_ASSERT(mScaledFont); DrawTarget *dt = const_cast<DrawTarget*>(aTarget); cairo_t *ctx = static_cast<cairo_t*>(dt->GetNativeSurface(NATIVE_SURFACE_CAIRO_CONTEXT)); bool isNewContext = !ctx; if (!ctx) { ctx = cairo_create(DrawTargetCairo::GetDummySurface()); cairo_matrix_t mat; GfxMatrixToCairoMatrix(aTarget->GetTransform(), mat); cairo_set_matrix(ctx, &mat); } cairo_set_scaled_font(ctx, mScaledFont); // Convert our GlyphBuffer into an array of Cairo glyphs. std::vector<cairo_glyph_t> glyphs(aBuffer.mNumGlyphs); for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) { glyphs[i].index = aBuffer.mGlyphs[i].mIndex; glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x; glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y; } cairo_glyph_path(ctx, &glyphs[0], aBuffer.mNumGlyphs); RefPtr<PathCairo> newPath = new PathCairo(ctx); if (isNewContext) { cairo_destroy(ctx); } return newPath; } #endif return nullptr; }