// Copyright 2014 PDFium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "core/include/fxge/fx_ge.h" #if defined(_SKIA_SUPPORT_) #include "core/include/fxcodec/fx_codec.h" #include "core/fxge/agg/fx_agg_driver.h" #include "core/fxge/skia/fx_skia_device.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkColorPriv.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkPath.h" #include "third_party/skia/include/core/SkPictureRecorder.h" #include "third_party/skia/include/core/SkStream.h" #include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/effects/SkDashPathEffect.h" #define SHOW_SKIA_PATH 0 // set to 1 to print the path contents #define DRAW_SKIA_CLIP 0 // set to 1 to draw a green rectangle around the clip static void DebugShowSkiaPath(const SkPath& path) { #if SHOW_SKIA_PATH char buffer[4096]; sk_bzero(buffer, sizeof(buffer)); SkMemoryWStream stream(buffer, sizeof(buffer)); path.dump(&stream, false, false); printf("%s\n", buffer); #endif // SHOW_SKIA_PATH } #if DRAW_SKIA_CLIP static SkPaint DebugClipPaint() { SkPaint paint; paint.setAntiAlias(true); paint.setColor(SK_ColorGREEN); paint.setStyle(SkPaint::kStroke_Style); return paint; } static void DebugDrawSkiaClipRect(SkCanvas* canvas, const SkRect& rect) { SkPaint paint = DebugClipPaint(); canvas->drawRect(rect, paint); } static void DebugDrawSkiaClipPath(SkCanvas* canvas, const SkPath& path) { SkPaint paint = DebugClipPaint(); canvas->drawPath(path, paint); } #else // DRAW_SKIA_CLIP static void DebugDrawSkiaClipRect(SkCanvas* canvas, const SkRect& rect) {} static void DebugDrawSkiaClipPath(SkCanvas* canvas, const SkPath& path) {} #endif // DRAW_SKIA_CLIP #undef SHOW_SKIA_PATH #undef DRAW_SKIA_CLIP static SkPath BuildPath(const CFX_PathData* pPathData, const CFX_Matrix* pObject2Device) { SkPath skPath; const CFX_PathData* pFPath = pPathData; int nPoints = pFPath->GetPointCount(); FX_PATHPOINT* pPoints = pFPath->GetPoints(); for (int i = 0; i < nPoints; i++) { FX_FLOAT x = pPoints[i].m_PointX; FX_FLOAT y = pPoints[i].m_PointY; if (pObject2Device) pObject2Device->Transform(x, y); int point_type = pPoints[i].m_Flag & FXPT_TYPE; if (point_type == FXPT_MOVETO) { skPath.moveTo(x, y); } else if (point_type == FXPT_LINETO) { skPath.lineTo(x, y); } else if (point_type == FXPT_BEZIERTO) { FX_FLOAT x2 = pPoints[i + 1].m_PointX, y2 = pPoints[i + 1].m_PointY; FX_FLOAT x3 = pPoints[i + 2].m_PointX, y3 = pPoints[i + 2].m_PointY; if (pObject2Device) { pObject2Device->Transform(x2, y2); pObject2Device->Transform(x3, y3); } skPath.cubicTo(x, y, x2, y2, x3, y3); i += 2; } if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE) skPath.close(); } return skPath; } // convert a stroking path to scanlines void CFX_SkiaDeviceDriver::PaintStroke(SkPaint* spaint, const CFX_GraphStateData* pGraphState) { SkPaint::Cap cap; switch (pGraphState->m_LineCap) { case CFX_GraphStateData::LineCapRound: cap = SkPaint::kRound_Cap; break; case CFX_GraphStateData::LineCapSquare: cap = SkPaint::kSquare_Cap; break; default: cap = SkPaint::kButt_Cap; break; } SkPaint::Join join; switch (pGraphState->m_LineJoin) { case CFX_GraphStateData::LineJoinRound: join = SkPaint::kRound_Join; break; case CFX_GraphStateData::LineJoinBevel: join = SkPaint::kBevel_Join; break; default: join = SkPaint::kMiter_Join; break; } FX_FLOAT width = pGraphState->m_LineWidth; if (pGraphState->m_DashArray) { int count = (pGraphState->m_DashCount + 1) / 2; SkScalar* intervals = FX_Alloc2D(SkScalar, count, sizeof(SkScalar)); // Set dash pattern for (int i = 0; i < count; i++) { FX_FLOAT on = pGraphState->m_DashArray[i * 2]; if (on <= 0.000001f) on = 1.f / 10; FX_FLOAT off = i * 2 + 1 == pGraphState->m_DashCount ? on : pGraphState->m_DashArray[i * 2 + 1]; if (off < 0) off = 0; intervals[i * 2] = on; intervals[i * 2 + 1] = off; } spaint->setPathEffect(SkDashPathEffect::Create(intervals, count * 2, pGraphState->m_DashPhase)) ->unref(); } spaint->setStyle(SkPaint::kStroke_Style); spaint->setAntiAlias(true); spaint->setStrokeWidth(width); spaint->setStrokeMiter(pGraphState->m_MiterLimit); spaint->setStrokeCap(cap); spaint->setStrokeJoin(join); } CFX_SkiaDeviceDriver::CFX_SkiaDeviceDriver(CFX_DIBitmap* pBitmap, int dither_bits, FX_BOOL bRgbByteOrder, CFX_DIBitmap* pOriDevice, FX_BOOL bGroupKnockout) : m_pRecorder(nullptr) { m_pAggDriver = new CFX_AggDeviceDriver(pBitmap, dither_bits, bRgbByteOrder, pOriDevice, bGroupKnockout); SkBitmap skBitmap; const CFX_DIBitmap* bitmap = m_pAggDriver->GetBitmap(); SkImageInfo imageInfo = SkImageInfo::Make(bitmap->GetWidth(), bitmap->GetHeight(), kN32_SkColorType, kOpaque_SkAlphaType); skBitmap.installPixels(imageInfo, bitmap->GetBuffer(), bitmap->GetPitch(), nullptr, /* to do : set color table */ nullptr, nullptr); m_pCanvas = new SkCanvas(skBitmap); m_ditherBits = dither_bits; } CFX_SkiaDeviceDriver::CFX_SkiaDeviceDriver(int size_x, int size_y) : m_pRecorder(new SkPictureRecorder) { m_pAggDriver = nullptr; m_pRecorder->beginRecording(SkIntToScalar(size_x), SkIntToScalar(size_y)); m_pCanvas = m_pRecorder->getRecordingCanvas(); m_ditherBits = 0; } CFX_SkiaDeviceDriver::CFX_SkiaDeviceDriver(SkPictureRecorder* recorder) : m_pRecorder(recorder) { m_pAggDriver = nullptr; m_pCanvas = m_pRecorder->getRecordingCanvas(); m_ditherBits = 0; } CFX_SkiaDeviceDriver::~CFX_SkiaDeviceDriver() { if (!m_pRecorder) delete m_pCanvas; delete m_pAggDriver; } FX_BOOL CFX_SkiaDeviceDriver::DrawDeviceText(int nChars, const FXTEXT_CHARPOS* pCharPos, CFX_Font* pFont, CFX_FontCache* pCache, const CFX_Matrix* pObject2Device, FX_FLOAT font_size, FX_DWORD color, int alpha_flag, void* pIccTransform) { SkAutoTUnref typeface(SkTypeface::CreateFromStream( new SkMemoryStream(pFont->GetFontData(), pFont->GetSize()))); SkPaint paint; paint.setAntiAlias(true); paint.setColor(color); paint.setTypeface(typeface); paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); paint.setTextSize(font_size); m_pCanvas->save(); SkMatrix skMatrix; const CFX_Matrix& m = *pObject2Device; // note that PDF's y-axis goes up; Skia's y-axis goes down skMatrix.setAll(m.a, m.b, m.e, -m.c, -m.d, m.f, 0, 0, 1); m_pCanvas->concat(skMatrix); for (int index = 0; index < nChars; ++index) { const FXTEXT_CHARPOS& cp = pCharPos[index]; uint16_t glyph = (uint16_t)cp.m_GlyphIndex; m_pCanvas->drawText(&glyph, 2, cp.m_OriginX, cp.m_OriginY, paint); } m_pCanvas->restore(); return TRUE; } int CFX_SkiaDeviceDriver::GetDeviceCaps(int caps_id) { switch (caps_id) { case FXDC_DEVICE_CLASS: return FXDC_DISPLAY; case FXDC_PIXEL_WIDTH: return m_pCanvas->imageInfo().width(); case FXDC_PIXEL_HEIGHT: return m_pCanvas->imageInfo().height(); case FXDC_BITS_PIXEL: return 32; case FXDC_HORZ_SIZE: case FXDC_VERT_SIZE: return 0; case FXDC_RENDER_CAPS: return FXRC_GET_BITS | FXRC_ALPHA_PATH | FXRC_ALPHA_IMAGE | FXRC_BLEND_MODE | FXRC_SOFT_CLIP | FXRC_ALPHA_OUTPUT; case FXDC_DITHER_BITS: return m_ditherBits; } return 0; } void CFX_SkiaDeviceDriver::SaveState() { m_pCanvas->save(); if (m_pAggDriver) m_pAggDriver->SaveState(); } void CFX_SkiaDeviceDriver::RestoreState(FX_BOOL bKeepSaved) { if (m_pAggDriver) m_pAggDriver->RestoreState(bKeepSaved); m_pCanvas->restore(); if (bKeepSaved) m_pCanvas->save(); } void CFX_SkiaDeviceDriver::SetClipMask( agg::rasterizer_scanline_aa& rasterizer) { if (m_pAggDriver) m_pAggDriver->SetClipMask(rasterizer); } FX_BOOL CFX_SkiaDeviceDriver::SetClip_PathFill( const CFX_PathData* pPathData, // path info const CFX_Matrix* pObject2Device, // flips object's y-axis int fill_mode // fill mode, WINDING or ALTERNATE ) { if (pPathData->GetPointCount() == 5 || pPathData->GetPointCount() == 4) { CFX_FloatRect rectf; if (pPathData->IsRect(pObject2Device, &rectf)) { rectf.Intersect( CFX_FloatRect(0, 0, (FX_FLOAT)GetDeviceCaps(FXDC_PIXEL_WIDTH), (FX_FLOAT)GetDeviceCaps(FXDC_PIXEL_HEIGHT))); // note that PDF's y-axis goes up; Skia's y-axis goes down SkRect skClipRect = SkRect::MakeLTRB(rectf.left, rectf.bottom, rectf.right, rectf.top); DebugDrawSkiaClipRect(m_pCanvas, skClipRect); m_pCanvas->clipRect(skClipRect); return TRUE; } } SkPath skClipPath = BuildPath(pPathData, pObject2Device); skClipPath.setFillType((fill_mode & 3) == FXFILL_WINDING ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType); DebugShowSkiaPath(skClipPath); DebugDrawSkiaClipPath(m_pCanvas, skClipPath); m_pCanvas->clipPath(skClipPath); return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::SetClip_PathStroke( const CFX_PathData* pPathData, // path info const CFX_Matrix* pObject2Device, // optional transformation const CFX_GraphStateData* pGraphState // graphic state, for pen attributes ) { // build path data SkPath skPath = BuildPath(pPathData, NULL); skPath.setFillType(SkPath::kWinding_FillType); SkPaint spaint; PaintStroke(&spaint, pGraphState); SkPath dst_path; spaint.getFillPath(skPath, &dst_path); DebugDrawSkiaClipPath(m_pCanvas, dst_path); m_pCanvas->clipPath(dst_path); return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::RenderRasterizer( agg::rasterizer_scanline_aa& rasterizer, FX_DWORD color, FX_BOOL bFullCover, FX_BOOL bGroupKnockout, int alpha_flag, void* pIccTransform) { return m_pAggDriver && m_pAggDriver->RenderRasterizer(rasterizer, color, bFullCover, bGroupKnockout, alpha_flag, pIccTransform); } FX_BOOL CFX_SkiaDeviceDriver::DrawPath( const CFX_PathData* pPathData, // path info const CFX_Matrix* pObject2Device, // optional transformation const CFX_GraphStateData* pGraphState, // graphic state, for pen attributes FX_DWORD fill_color, // fill color FX_DWORD stroke_color, // stroke color int fill_mode, // fill mode, WINDING or ALTERNATE. 0 for not filled int alpha_flag, void* pIccTransform, int blend_type) { SkIRect rect; rect.set(0, 0, GetDeviceCaps(FXDC_PIXEL_WIDTH), GetDeviceCaps(FXDC_PIXEL_HEIGHT)); SkPath skPath = BuildPath(pPathData, pObject2Device); SkPaint spaint; spaint.setAntiAlias(true); if ((fill_mode & 3) && fill_color) { skPath.setFillType((fill_mode & 3) == FXFILL_WINDING ? SkPath::kWinding_FillType : SkPath::kEvenOdd_FillType); spaint.setStyle(SkPaint::kFill_Style); spaint.setColor(fill_color); m_pCanvas->drawPath(skPath, spaint); } int stroke_alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_STROKE(alpha_flag) : FXARGB_A(stroke_color); if (pGraphState && stroke_alpha) { spaint.setColor(stroke_color); PaintStroke(&spaint, pGraphState); m_pCanvas->drawPath(skPath, spaint); } return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::SetPixel(int x, int y, FX_DWORD color, int alpha_flag, void* pIccTransform) { return m_pAggDriver && m_pAggDriver->SetPixel(x, y, color, alpha_flag, pIccTransform); } FX_BOOL CFX_SkiaDeviceDriver::FillRect(const FX_RECT* pRect, FX_DWORD fill_color, int alpha_flag, void* pIccTransform, int blend_type) { SkPaint spaint; spaint.setAntiAlias(true); spaint.setColor(fill_color); m_pCanvas->drawRect( SkRect::MakeLTRB(pRect->left, pRect->top, pRect->right, pRect->bottom), spaint); return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::GetClipBox(FX_RECT* pRect) { // TODO(caryclark) call m_canvas->getClipDeviceBounds() instead pRect->left = 0; pRect->top = 0; const SkImageInfo& canvasSize = m_pCanvas->imageInfo(); pRect->right = canvasSize.width(); pRect->bottom = canvasSize.height(); return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::GetDIBits(CFX_DIBitmap* pBitmap, int left, int top, void* pIccTransform, FX_BOOL bDEdge) { return m_pAggDriver && m_pAggDriver->GetDIBits(pBitmap, left, top, pIccTransform, bDEdge); } FX_BOOL CFX_SkiaDeviceDriver::SetDIBits(const CFX_DIBSource* pBitmap, FX_DWORD argb, const FX_RECT* pSrcRect, int left, int top, int blend_type, int alpha_flag, void* pIccTransform) { return m_pAggDriver && m_pAggDriver->SetDIBits(pBitmap, argb, pSrcRect, left, top, blend_type, alpha_flag, pIccTransform); } FX_BOOL CFX_SkiaDeviceDriver::StretchDIBits(const CFX_DIBSource* pSource, FX_DWORD argb, int dest_left, int dest_top, int dest_width, int dest_height, const FX_RECT* pClipRect, FX_DWORD flags, int alpha_flag, void* pIccTransform, int blend_type) { return m_pAggDriver && m_pAggDriver->StretchDIBits(pSource, argb, dest_left, dest_top, dest_width, dest_height, pClipRect, flags, alpha_flag, pIccTransform, blend_type); } FX_BOOL CFX_SkiaDeviceDriver::StartDIBits(const CFX_DIBSource* pSource, int bitmap_alpha, FX_DWORD argb, const CFX_Matrix* pMatrix, FX_DWORD render_flags, void*& handle, int alpha_flag, void* pIccTransform, int blend_type) { SkColorType colorType; void* buffer = pSource->GetBuffer(); std::unique_ptr dst8Storage; std::unique_ptr dst32Storage; int width = pSource->GetWidth(); int height = pSource->GetHeight(); int rowBytes = pSource->GetPitch(); switch (pSource->GetBPP()) { case 1: { dst8Storage.reset(FX_Alloc2D(uint8_t, width, height)); uint8_t* dst8Pixels = dst8Storage.get(); for (int y = 0; y < height; ++y) { const uint8_t* srcRow = static_cast(buffer) + y * rowBytes; uint8_t* dstRow = dst8Pixels + y * width; for (int x = 0; x < width; ++x) dstRow[x] = srcRow[x >> 3] & (1 << (~x & 0x07)) ? 0xFF : 0x00; } buffer = dst8Storage.get(); rowBytes = width; colorType = SkColorType::kGray_8_SkColorType; } break; case 24: { dst32Storage.reset(FX_Alloc2D(uint32_t, width, height)); uint32_t* dst32Pixels = dst32Storage.get(); for (int y = 0; y < height; ++y) { const uint8_t* srcRow = static_cast(buffer) + y * rowBytes; uint32_t* dstRow = dst32Pixels + y * width; for (int x = 0; x < width; ++x) dstRow[x] = SkPackARGB32(0xFF, srcRow[x * 3 + 2], srcRow[x * 3 + 1], srcRow[x * 3 + 0]); } buffer = dst32Storage.get(); rowBytes = width * sizeof(uint32_t); colorType = SkColorType::kN32_SkColorType; } break; case 32: colorType = SkColorType::kN32_SkColorType; break; default: colorType = SkColorType::kUnknown_SkColorType; } SkImageInfo imageInfo = SkImageInfo::Make(width, height, colorType, kOpaque_SkAlphaType); SkBitmap skBitmap; skBitmap.installPixels(imageInfo, buffer, rowBytes, nullptr, /* TODO(caryclark) : set color table */ nullptr, nullptr); m_pCanvas->save(); bool landscape = !pMatrix->a; if (landscape) m_pCanvas->translate(m_pCanvas->imageInfo().width(), 0); else m_pCanvas->translate(pMatrix->e, pMatrix->f + pMatrix->d); SkMatrix skMatrix = SkMatrix::MakeScale(1.f / width, 1.f / height); m_pCanvas->concat(skMatrix); const CFX_Matrix& m = *pMatrix; // note that PDF's y-axis goes up; Skia's y-axis goes down if (landscape) skMatrix.setAll(-m.a, -m.b, m.e, m.c, m.d, m.f, 0, 0, 1); else skMatrix.setAll(m.a, m.b, 0, -m.c, -m.d, 0, 0, 0, 1); m_pCanvas->concat(skMatrix); SkPaint paint; paint.setAntiAlias(true); paint.setFilterQuality(kLow_SkFilterQuality); m_pCanvas->drawBitmap(skBitmap, 0, 0, &paint); m_pCanvas->restore(); return TRUE; } FX_BOOL CFX_SkiaDeviceDriver::ContinueDIBits(void* pHandle, IFX_Pause* pPause) { return m_pAggDriver && m_pAggDriver->ContinueDIBits(pHandle, pPause); } void CFX_SkiaDeviceDriver::CancelDIBits(void* pHandle) { if (m_pAggDriver) m_pAggDriver->CancelDIBits(pHandle); } CFX_SkiaDevice::CFX_SkiaDevice() { m_bOwnedBitmap = FALSE; } SkPictureRecorder* CFX_SkiaDevice::CreateRecorder(int size_x, int size_y) { CFX_SkiaDeviceDriver* skDriver = new CFX_SkiaDeviceDriver(size_x, size_y); SetDeviceDriver(skDriver); return skDriver->GetRecorder(); } FX_BOOL CFX_SkiaDevice::Attach(CFX_DIBitmap* pBitmap, int dither_bits, FX_BOOL bRgbByteOrder, CFX_DIBitmap* pOriDevice, FX_BOOL bGroupKnockout) { if (!pBitmap) return FALSE; SetBitmap(pBitmap); SetDeviceDriver(new CFX_SkiaDeviceDriver(pBitmap, dither_bits, bRgbByteOrder, pOriDevice, bGroupKnockout)); return TRUE; } FX_BOOL CFX_SkiaDevice::AttachRecorder(SkPictureRecorder* recorder) { if (!recorder) return FALSE; SetDeviceDriver(new CFX_SkiaDeviceDriver(recorder)); return TRUE; } FX_BOOL CFX_SkiaDevice::Create(int width, int height, FXDIB_Format format, int dither_bits, CFX_DIBitmap* pOriDevice) { m_bOwnedBitmap = TRUE; CFX_DIBitmap* pBitmap = new CFX_DIBitmap; if (!pBitmap->Create(width, height, format)) { delete pBitmap; return FALSE; } SetBitmap(pBitmap); CFX_SkiaDeviceDriver* pDriver = new CFX_SkiaDeviceDriver(pBitmap, dither_bits, FALSE, pOriDevice, FALSE); SetDeviceDriver(pDriver); return TRUE; } CFX_SkiaDevice::~CFX_SkiaDevice() { if (m_bOwnedBitmap && GetBitmap()) delete GetBitmap(); } #endif