| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/include/fxge/fx_ge.h" | |
| 6 | |
| 7 #if defined(_SKIA_SUPPORT_) | |
| 8 #include "core/include/fxcodec/fx_codec.h" | |
| 9 | |
| 10 #include "core/src/fxge/agg/fx_agg_driver.h" | |
| 11 #include "core/src/fxge/skia/fx_skia_device.h" | |
| 12 | |
| 13 #include "SkCanvas.h" | |
| 14 #include "SkDashPathEffect.h" | |
| 15 #include "SkPaint.h" | |
| 16 #include "SkPath.h" | |
| 17 | |
| 18 static SkPath BuildPath(const CFX_PathData* pPathData, | |
| 19 const CFX_Matrix* pObject2Device) { | |
| 20 SkPath skPath; | |
| 21 const CFX_PathData* pFPath = pPathData; | |
| 22 int nPoints = pFPath->GetPointCount(); | |
| 23 FX_PATHPOINT* pPoints = pFPath->GetPoints(); | |
| 24 for (int i = 0; i < nPoints; i++) { | |
| 25 FX_FLOAT x = pPoints[i].m_PointX; | |
| 26 FX_FLOAT y = pPoints[i].m_PointY; | |
| 27 if (pObject2Device) | |
| 28 pObject2Device->Transform(x, y); | |
| 29 int point_type = pPoints[i].m_Flag & FXPT_TYPE; | |
| 30 if (point_type == FXPT_MOVETO) { | |
| 31 skPath.moveTo(x, y); | |
| 32 } else if (point_type == FXPT_LINETO) { | |
| 33 skPath.lineTo(x, y); | |
| 34 } else if (point_type == FXPT_BEZIERTO) { | |
| 35 FX_FLOAT x2 = pPoints[i + 1].m_PointX, y2 = pPoints[i + 1].m_PointY; | |
| 36 FX_FLOAT x3 = pPoints[i + 2].m_PointX, y3 = pPoints[i + 2].m_PointY; | |
| 37 if (pObject2Device) { | |
| 38 pObject2Device->Transform(x2, y2); | |
| 39 pObject2Device->Transform(x3, y3); | |
| 40 } | |
| 41 skPath.cubicTo(x, y, x2, y2, x3, y3); | |
| 42 i += 2; | |
| 43 } | |
| 44 if (pPoints[i].m_Flag & FXPT_CLOSEFIGURE) | |
| 45 skPath.close(); | |
| 46 } | |
| 47 return skPath; | |
| 48 } | |
| 49 | |
| 50 // convert a stroking path to scanlines | |
| 51 void CFX_SkiaDeviceDriver::PaintStroke(SkPaint* spaint, | |
| 52 const CFX_GraphStateData* pGraphState) { | |
| 53 SkPaint::Cap cap; | |
| 54 switch (pGraphState->m_LineCap) { | |
| 55 case CFX_GraphStateData::LineCapRound: | |
| 56 cap = SkPaint::kRound_Cap; | |
| 57 break; | |
| 58 case CFX_GraphStateData::LineCapSquare: | |
| 59 cap = SkPaint::kSquare_Cap; | |
| 60 break; | |
| 61 default: | |
| 62 cap = SkPaint::kButt_Cap; | |
| 63 break; | |
| 64 } | |
| 65 SkPaint::Join join; | |
| 66 switch (pGraphState->m_LineJoin) { | |
| 67 case CFX_GraphStateData::LineJoinRound: | |
| 68 join = SkPaint::kRound_Join; | |
| 69 break; | |
| 70 case CFX_GraphStateData::LineJoinBevel: | |
| 71 join = SkPaint::kBevel_Join; | |
| 72 break; | |
| 73 default: | |
| 74 join = SkPaint::kMiter_Join; | |
| 75 break; | |
| 76 } | |
| 77 FX_FLOAT width = pGraphState->m_LineWidth; | |
| 78 | |
| 79 if (pGraphState->m_DashArray) { | |
| 80 int count = (pGraphState->m_DashCount + 1) / 2; | |
| 81 SkScalar* intervals = FX_Alloc2D(SkScalar, count, sizeof(SkScalar)); | |
| 82 // Set dash pattern | |
| 83 for (int i = 0; i < count; i++) { | |
| 84 FX_FLOAT on = pGraphState->m_DashArray[i * 2]; | |
| 85 if (on <= 0.000001f) | |
| 86 on = 1.f / 10; | |
| 87 FX_FLOAT off = i * 2 + 1 == pGraphState->m_DashCount | |
| 88 ? on | |
| 89 : pGraphState->m_DashArray[i * 2 + 1]; | |
| 90 if (off < 0) | |
| 91 off = 0; | |
| 92 intervals[i * 2] = on; | |
| 93 intervals[i * 2 + 1] = off; | |
| 94 } | |
| 95 spaint | |
| 96 ->setPathEffect(SkDashPathEffect::Create(intervals, count * 2, | |
| 97 pGraphState->m_DashPhase)) | |
| 98 ->unref(); | |
| 99 } | |
| 100 spaint->setStyle(SkPaint::kStroke_Style); | |
| 101 spaint->setAntiAlias(TRUE); | |
| 102 spaint->setStrokeWidth(width); | |
| 103 spaint->setStrokeMiter(pGraphState->m_MiterLimit); | |
| 104 spaint->setStrokeCap(cap); | |
| 105 spaint->setStrokeJoin(join); | |
| 106 } | |
| 107 | |
| 108 CFX_SkiaDeviceDriver::CFX_SkiaDeviceDriver(CFX_DIBitmap* pBitmap, | |
| 109 int dither_bits, | |
| 110 FX_BOOL bRgbByteOrder, | |
| 111 CFX_DIBitmap* pOriDevice, | |
| 112 FX_BOOL bGroupKnockout) { | |
| 113 m_pAggDriver = new CFX_AggDeviceDriver(pBitmap, dither_bits, bRgbByteOrder, | |
| 114 pOriDevice, bGroupKnockout); | |
| 115 SkBitmap skBitmap; | |
| 116 const CFX_DIBitmap* bitmap = m_pAggDriver->m_pBitmap; | |
| 117 SkImageInfo imageInfo = | |
| 118 SkImageInfo::Make(bitmap->GetWidth(), bitmap->GetHeight(), | |
| 119 kN32_SkColorType, kOpaque_SkAlphaType); | |
| 120 skBitmap.installPixels(imageInfo, bitmap->GetBuffer(), bitmap->GetPitch(), | |
| 121 nullptr, /* to do : set color table */ | |
| 122 nullptr, nullptr); | |
| 123 m_canvas = new SkCanvas(skBitmap); | |
| 124 } | |
| 125 | |
| 126 CFX_SkiaDeviceDriver::~CFX_SkiaDeviceDriver() { | |
| 127 #if 0 // TODO(caryclark) : mismatch on allocator ? | |
| 128 delete m_canvas; | |
| 129 #endif | |
| 130 delete m_pAggDriver; | |
| 131 } | |
| 132 | |
| 133 FX_BOOL CFX_SkiaDeviceDriver::DrawDeviceText(int nChars, | |
| 134 const FXTEXT_CHARPOS* pCharPos, | |
| 135 CFX_Font* pFont, | |
| 136 CFX_FontCache* pCache, | |
| 137 const CFX_Matrix* pObject2Device, | |
| 138 FX_FLOAT font_size, | |
| 139 FX_DWORD color, | |
| 140 int alpha_flag, | |
| 141 void* pIccTransform) { | |
| 142 return m_pAggDriver->DrawDeviceText(nChars, pCharPos, pFont, pCache, | |
| 143 pObject2Device, font_size, color, | |
| 144 alpha_flag, pIccTransform); | |
| 145 } | |
| 146 | |
| 147 int CFX_SkiaDeviceDriver::GetDeviceCaps(int caps_id) { | |
| 148 return m_pAggDriver->GetDeviceCaps(caps_id); | |
| 149 } | |
| 150 | |
| 151 void CFX_SkiaDeviceDriver::SaveState() { | |
| 152 m_canvas->save(); | |
| 153 m_pAggDriver->SaveState(); | |
| 154 } | |
| 155 | |
| 156 void CFX_SkiaDeviceDriver::RestoreState(FX_BOOL bKeepSaved) { | |
| 157 m_pAggDriver->RestoreState(bKeepSaved); | |
| 158 m_canvas->restore(); | |
| 159 } | |
| 160 | |
| 161 void CFX_SkiaDeviceDriver::SetClipMask( | |
| 162 agg::rasterizer_scanline_aa& rasterizer) { | |
| 163 m_pAggDriver->SetClipMask(rasterizer); | |
| 164 } | |
| 165 | |
| 166 FX_BOOL CFX_SkiaDeviceDriver::SetClip_PathFill( | |
| 167 const CFX_PathData* pPathData, // path info | |
| 168 const CFX_Matrix* pObject2Device, // optional transformation | |
| 169 int fill_mode // fill mode, WINDING or ALTERNATE | |
| 170 ) { | |
| 171 if (!m_pAggDriver->m_pClipRgn) { | |
| 172 m_pAggDriver->m_pClipRgn = new CFX_ClipRgn( | |
| 173 GetDeviceCaps(FXDC_PIXEL_WIDTH), GetDeviceCaps(FXDC_PIXEL_HEIGHT)); | |
| 174 } | |
| 175 | |
| 176 if (pPathData->GetPointCount() == 5 || pPathData->GetPointCount() == 4) { | |
| 177 CFX_FloatRect rectf; | |
| 178 if (pPathData->IsRect(pObject2Device, &rectf)) { | |
| 179 rectf.Intersect( | |
| 180 CFX_FloatRect(0, 0, (FX_FLOAT)GetDeviceCaps(FXDC_PIXEL_WIDTH), | |
| 181 (FX_FLOAT)GetDeviceCaps(FXDC_PIXEL_HEIGHT))); | |
| 182 FX_RECT rect = rectf.GetOutterRect(); | |
| 183 m_pAggDriver->m_pClipRgn->IntersectRect(rect); | |
| 184 return TRUE; | |
| 185 } | |
| 186 } | |
| 187 SkPath clip = BuildPath(pPathData, pObject2Device); | |
| 188 clip.setFillType((fill_mode & 3) == FXFILL_WINDING | |
| 189 ? SkPath::kWinding_FillType | |
| 190 : SkPath::kEvenOdd_FillType); | |
| 191 const CFX_Matrix& m = *pObject2Device; | |
| 192 #if 0 | |
| 193 // TODO(caryclark) : don't clip quite yet | |
| 194 // need to understand how to save/restore to balance the clip | |
| 195 printf("m:(%g,%g,%g) (%g,%g,%g)\n", m.a, m.b, m.c, m.d, m.e, m.f); | |
| 196 clip.dump(); | |
| 197 SkMatrix skMatrix; | |
| 198 skMatrix.setAll(m.a, m.b, m.c, m.d, m.e, m.f, 0, 0, 1); | |
| 199 m_canvas->setMatrix(skMatrix); | |
| 200 m_canvas->clipPath(clip, SkRegion::kReplace_Op); | |
| 201 #endif | |
| 202 | |
| 203 return TRUE; | |
| 204 } | |
| 205 | |
| 206 FX_BOOL CFX_SkiaDeviceDriver::SetClip_PathStroke( | |
| 207 const CFX_PathData* pPathData, // path info | |
| 208 const CFX_Matrix* pObject2Device, // optional transformation | |
| 209 const CFX_GraphStateData* pGraphState // graphic state, for pen attributes | |
| 210 ) { | |
| 211 if (!m_pAggDriver->m_pClipRgn) { | |
| 212 m_pAggDriver->m_pClipRgn = new CFX_ClipRgn( | |
| 213 GetDeviceCaps(FXDC_PIXEL_WIDTH), GetDeviceCaps(FXDC_PIXEL_HEIGHT)); | |
| 214 } | |
| 215 | |
| 216 // build path data | |
| 217 SkPath skPath = BuildPath(pPathData, NULL); | |
| 218 skPath.setFillType(SkPath::kWinding_FillType); | |
| 219 | |
| 220 SkPaint spaint; | |
| 221 PaintStroke(&spaint, pGraphState); | |
| 222 SkPath dst_path; | |
| 223 spaint.getFillPath(skPath, &dst_path); | |
| 224 #if 01 | |
| 225 SkMatrix skMatrix; | |
| 226 const CFX_Matrix& m = *pObject2Device; | |
| 227 skMatrix.setAll(m.a, m.b, m.c, m.d, m.e, m.f, 0, 0, 1); | |
| 228 m_canvas->setMatrix(skMatrix); | |
| 229 // TODO(caryclark) : don't clip quite yet | |
| 230 // need to understand how to save/restore so that clip is later undone | |
| 231 m_canvas->clipPath(dst_path, SkRegion::kReplace_Op); | |
| 232 #endif | |
| 233 return TRUE; | |
| 234 } | |
| 235 | |
| 236 FX_BOOL CFX_SkiaDeviceDriver::RenderRasterizer( | |
| 237 agg::rasterizer_scanline_aa& rasterizer, | |
| 238 FX_DWORD color, | |
| 239 FX_BOOL bFullCover, | |
| 240 FX_BOOL bGroupKnockout, | |
| 241 int alpha_flag, | |
| 242 void* pIccTransform) { | |
| 243 return m_pAggDriver->RenderRasterizer( | |
| 244 rasterizer, color, bFullCover, bGroupKnockout, alpha_flag, pIccTransform); | |
| 245 } | |
| 246 | |
| 247 FX_BOOL CFX_SkiaDeviceDriver::DrawPath( | |
| 248 const CFX_PathData* pPathData, // path info | |
| 249 const CFX_Matrix* pObject2Device, // optional transformation | |
| 250 const CFX_GraphStateData* pGraphState, // graphic state, for pen attributes | |
| 251 FX_DWORD fill_color, // fill color | |
| 252 FX_DWORD stroke_color, // stroke color | |
| 253 int fill_mode, // fill mode, WINDING or ALTERNATE. 0 for not filled | |
| 254 int alpha_flag, | |
| 255 void* pIccTransform, | |
| 256 int blend_type) { | |
| 257 if (!GetBuffer()) | |
| 258 return TRUE; | |
| 259 SkIRect rect; | |
| 260 rect.set(0, 0, GetDeviceCaps(FXDC_PIXEL_WIDTH), | |
| 261 GetDeviceCaps(FXDC_PIXEL_HEIGHT)); | |
| 262 SkPath skPath = BuildPath(pPathData, pObject2Device); | |
| 263 SkPaint spaint; | |
| 264 spaint.setAntiAlias(TRUE); | |
| 265 if ((fill_mode & 3) && fill_color) { | |
| 266 skPath.setFillType((fill_mode & 3) == FXFILL_WINDING | |
| 267 ? SkPath::kWinding_FillType | |
| 268 : SkPath::kEvenOdd_FillType); | |
| 269 | |
| 270 spaint.setStyle(SkPaint::kFill_Style); | |
| 271 spaint.setColor(fill_color); | |
| 272 m_canvas->drawPath(skPath, spaint); | |
| 273 } | |
| 274 int stroke_alpha = FXGETFLAG_COLORTYPE(alpha_flag) | |
| 275 ? FXGETFLAG_ALPHA_STROKE(alpha_flag) | |
| 276 : FXARGB_A(stroke_color); | |
| 277 | |
| 278 if (pGraphState && stroke_alpha) { | |
| 279 spaint.setColor(stroke_color); | |
| 280 PaintStroke(&spaint, pGraphState); | |
| 281 m_canvas->drawPath(skPath, spaint); | |
| 282 } | |
| 283 | |
| 284 return TRUE; | |
| 285 } | |
| 286 | |
| 287 FX_BOOL CFX_SkiaDeviceDriver::SetPixel(int x, | |
| 288 int y, | |
| 289 FX_DWORD color, | |
| 290 int alpha_flag, | |
| 291 void* pIccTransform) { | |
| 292 return m_pAggDriver->SetPixel(x, y, color, alpha_flag, pIccTransform); | |
| 293 } | |
| 294 | |
| 295 FX_BOOL CFX_SkiaDeviceDriver::FillRect(const FX_RECT* pRect, | |
| 296 FX_DWORD fill_color, | |
| 297 int alpha_flag, | |
| 298 void* pIccTransform, | |
| 299 int blend_type) { | |
| 300 SkPaint spaint; | |
| 301 spaint.setAntiAlias(true); | |
| 302 spaint.setColor(fill_color); | |
| 303 | |
| 304 m_canvas->drawRect( | |
| 305 SkRect::MakeLTRB(pRect->left, pRect->top, pRect->right, pRect->bottom), | |
| 306 spaint); | |
| 307 return TRUE; | |
| 308 } | |
| 309 | |
| 310 FX_BOOL CFX_SkiaDeviceDriver::GetClipBox(FX_RECT* pRect) { | |
| 311 return m_pAggDriver->GetClipBox(pRect); | |
| 312 } | |
| 313 | |
| 314 FX_BOOL CFX_SkiaDeviceDriver::GetDIBits(CFX_DIBitmap* pBitmap, | |
| 315 int left, | |
| 316 int top, | |
| 317 void* pIccTransform, | |
| 318 FX_BOOL bDEdge) { | |
| 319 return m_pAggDriver->GetDIBits(pBitmap, left, top, pIccTransform, bDEdge); | |
| 320 } | |
| 321 | |
| 322 FX_BOOL CFX_SkiaDeviceDriver::SetDIBits(const CFX_DIBSource* pBitmap, | |
| 323 FX_DWORD argb, | |
| 324 const FX_RECT* pSrcRect, | |
| 325 int left, | |
| 326 int top, | |
| 327 int blend_type, | |
| 328 int alpha_flag, | |
| 329 void* pIccTransform) { | |
| 330 return m_pAggDriver->SetDIBits(pBitmap, argb, pSrcRect, left, top, blend_type, | |
| 331 alpha_flag, pIccTransform); | |
| 332 } | |
| 333 | |
| 334 FX_BOOL CFX_SkiaDeviceDriver::StretchDIBits(const CFX_DIBSource* pSource, | |
| 335 FX_DWORD argb, | |
| 336 int dest_left, | |
| 337 int dest_top, | |
| 338 int dest_width, | |
| 339 int dest_height, | |
| 340 const FX_RECT* pClipRect, | |
| 341 FX_DWORD flags, | |
| 342 int alpha_flag, | |
| 343 void* pIccTransform, | |
| 344 int blend_type) { | |
| 345 return m_pAggDriver->StretchDIBits(pSource, argb, dest_left, dest_top, | |
| 346 dest_width, dest_height, pClipRect, flags, | |
| 347 alpha_flag, pIccTransform, blend_type); | |
| 348 } | |
| 349 | |
| 350 FX_BOOL CFX_SkiaDeviceDriver::StartDIBits(const CFX_DIBSource* pSource, | |
| 351 int bitmap_alpha, | |
| 352 FX_DWORD argb, | |
| 353 const CFX_Matrix* pMatrix, | |
| 354 FX_DWORD render_flags, | |
| 355 void*& handle, | |
| 356 int alpha_flag, | |
| 357 void* pIccTransform, | |
| 358 int blend_type) { | |
| 359 return m_pAggDriver->StartDIBits(pSource, bitmap_alpha, argb, pMatrix, | |
| 360 render_flags, handle, alpha_flag, | |
| 361 pIccTransform, blend_type); | |
| 362 } | |
| 363 | |
| 364 FX_BOOL CFX_SkiaDeviceDriver::ContinueDIBits(void* pHandle, IFX_Pause* pPause) { | |
| 365 return m_pAggDriver->ContinueDIBits(pHandle, pPause); | |
| 366 } | |
| 367 | |
| 368 void CFX_SkiaDeviceDriver::CancelDIBits(void* pHandle) { | |
| 369 m_pAggDriver->CancelDIBits(pHandle); | |
| 370 } | |
| 371 | |
| 372 CFX_SkiaDevice::CFX_SkiaDevice() { | |
| 373 m_bOwnedBitmap = FALSE; | |
| 374 } | |
| 375 | |
| 376 FX_BOOL CFX_SkiaDevice::Attach(CFX_DIBitmap* pBitmap, | |
| 377 int dither_bits, | |
| 378 FX_BOOL bRgbByteOrder, | |
| 379 CFX_DIBitmap* pOriDevice, | |
| 380 FX_BOOL bGroupKnockout) { | |
| 381 if (!pBitmap) | |
| 382 return FALSE; | |
| 383 SetBitmap(pBitmap); | |
| 384 CFX_SkiaDeviceDriver* pDriver = new CFX_SkiaDeviceDriver( | |
| 385 pBitmap, dither_bits, bRgbByteOrder, pOriDevice, bGroupKnockout); | |
| 386 SetDeviceDriver(pDriver); | |
| 387 return TRUE; | |
| 388 } | |
| 389 | |
| 390 FX_BOOL CFX_SkiaDevice::Create(int width, | |
| 391 int height, | |
| 392 FXDIB_Format format, | |
| 393 int dither_bits, | |
| 394 CFX_DIBitmap* pOriDevice) { | |
| 395 m_bOwnedBitmap = TRUE; | |
| 396 CFX_DIBitmap* pBitmap = new CFX_DIBitmap; | |
| 397 if (!pBitmap->Create(width, height, format)) { | |
| 398 delete pBitmap; | |
| 399 return FALSE; | |
| 400 } | |
| 401 SetBitmap(pBitmap); | |
| 402 CFX_SkiaDeviceDriver* pDriver = | |
| 403 new CFX_SkiaDeviceDriver(pBitmap, dither_bits, FALSE, pOriDevice, FALSE); | |
| 404 SetDeviceDriver(pDriver); | |
| 405 return TRUE; | |
| 406 } | |
| 407 | |
| 408 CFX_SkiaDevice::~CFX_SkiaDevice() { | |
| 409 if (m_bOwnedBitmap && GetBitmap()) | |
| 410 delete GetBitmap(); | |
| 411 } | |
| 412 | |
| 413 #if 0 | |
| 414 #include <stdarg.h> | |
| 415 #include <stdio.h> | |
| 416 | |
| 417 void SkDebugf(const char format[], ...) { | |
| 418 va_list args; | |
| 419 va_start(args, format); | |
| 420 vfprintf(stderr, format, args); | |
| 421 va_end(args); | |
| 422 } | |
| 423 | |
| 424 #endif | |
| 425 | |
| 426 #endif | |
| OLD | NEW |