SkiaCanvas.cpp revision fd92ee4b731bee39f8b100cd138fb491de9d66ee
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "CanvasProperty.h" 18#include "Layer.h" 19#include "RenderNode.h" 20#include "hwui/Canvas.h" 21 22#include <SkCanvas.h> 23#include <SkClipStack.h> 24#include <SkDrawable.h> 25#include <SkDevice.h> 26#include <SkDeque.h> 27#include <SkDrawFilter.h> 28#include <SkGraphics.h> 29#include <SkImage.h> 30#include <SkShader.h> 31#include <SkTArray.h> 32#include <SkTemplates.h> 33 34#include "VectorDrawable.h" 35 36#include <memory> 37 38namespace android { 39 40// Holds an SkCanvas reference plus additional native data. 41class SkiaCanvas : public Canvas { 42public: 43 explicit SkiaCanvas(const SkBitmap& bitmap); 44 45 /** 46 * Create a new SkiaCanvas. 47 * 48 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must 49 * not be NULL. This constructor will ref() the SkCanvas, and unref() 50 * it in its destructor. 51 */ 52 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) { 53 SkASSERT(canvas); 54 canvas->ref(); 55 } 56 57 virtual SkCanvas* asSkCanvas() override { 58 return mCanvas.get(); 59 } 60 61 virtual void resetRecording(int width, int height) override { 62 LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas"); 63 } 64 65 virtual uirenderer::DisplayList* finishRecording() override { 66 LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList"); 67 return nullptr; 68 } 69 virtual void insertReorderBarrier(bool enableReorder) override { 70 LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers"); 71 } 72 73 virtual void setBitmap(const SkBitmap& bitmap) override; 74 75 virtual bool isOpaque() override; 76 virtual int width() override; 77 virtual int height() override; 78 79 virtual void setHighContrastText(bool highContrastText) override { 80 mHighContrastText = highContrastText; 81 } 82 virtual bool isHighContrastText() override { return mHighContrastText; } 83 84 virtual int getSaveCount() const override; 85 virtual int save(SaveFlags::Flags flags) override; 86 virtual void restore() override; 87 virtual void restoreToCount(int saveCount) override; 88 89 virtual int saveLayer(float left, float top, float right, float bottom, 90 const SkPaint* paint, SaveFlags::Flags flags) override; 91 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 92 int alpha, SaveFlags::Flags flags) override; 93 94 virtual void getMatrix(SkMatrix* outMatrix) const override; 95 virtual void setMatrix(const SkMatrix& matrix) override; 96 virtual void concat(const SkMatrix& matrix) override; 97 virtual void rotate(float degrees) override; 98 virtual void scale(float sx, float sy) override; 99 virtual void skew(float sx, float sy) override; 100 virtual void translate(float dx, float dy) override; 101 102 virtual bool getClipBounds(SkRect* outRect) const override; 103 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override; 104 virtual bool quickRejectPath(const SkPath& path) const override; 105 virtual bool clipRect(float left, float top, float right, float bottom, 106 SkRegion::Op op) override; 107 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override; 108 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override; 109 110 virtual SkDrawFilter* getDrawFilter() override; 111 virtual void setDrawFilter(SkDrawFilter* drawFilter) override; 112 113 virtual void drawColor(int color, SkXfermode::Mode mode) override; 114 virtual void drawPaint(const SkPaint& paint) override; 115 116 virtual void drawPoint(float x, float y, const SkPaint& paint) override; 117 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override; 118 virtual void drawLine(float startX, float startY, float stopX, float stopY, 119 const SkPaint& paint) override; 120 virtual void drawLines(const float* points, int count, const SkPaint& paint) override; 121 virtual void drawRect(float left, float top, float right, float bottom, 122 const SkPaint& paint) override; 123 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override; 124 virtual void drawRoundRect(float left, float top, float right, float bottom, 125 float rx, float ry, const SkPaint& paint) override; 126 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override; 127 virtual void drawOval(float left, float top, float right, float bottom, 128 const SkPaint& paint) override; 129 virtual void drawArc(float left, float top, float right, float bottom, 130 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override; 131 virtual void drawPath(const SkPath& path, const SkPaint& paint) override; 132 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 133 const float* verts, const float* tex, const int* colors, 134 const uint16_t* indices, int indexCount, const SkPaint& paint) override; 135 136 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, 137 const SkPaint* paint) override; 138 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, 139 const SkPaint* paint) override; 140 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 141 float srcRight, float srcBottom, float dstLeft, float dstTop, 142 float dstRight, float dstBottom, const SkPaint* paint) override; 143 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 144 const float* vertices, const int* colors, const SkPaint* paint) override; 145 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk, 146 float dstLeft, float dstTop, float dstRight, float dstBottom, 147 const SkPaint* paint) override; 148 149 virtual bool drawTextAbsolutePos() const override { return true; } 150 virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override; 151 152 virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 153 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, 154 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, 155 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override; 156 virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x, 157 uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius, 158 uirenderer::CanvasPropertyPaint* paint) override; 159 160 virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override; 161 virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override; 162 virtual void callDrawGLFunction(Functor* functor, 163 uirenderer::GlFunctorLifecycleListener* listener) override; 164 165protected: 166 virtual void drawGlyphs(const uint16_t* text, const float* positions, int count, 167 const SkPaint& paint, float x, float y, 168 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 169 float totalAdvance) override; 170 virtual void drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path, 171 float hOffset, float vOffset, const SkPaint& paint) override; 172 173private: 174 struct SaveRec { 175 int saveCount; 176 SaveFlags::Flags saveFlags; 177 }; 178 179 bool mHighContrastText = false; 180 181 void recordPartialSave(SaveFlags::Flags flags); 182 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount); 183 void applyClips(const SkTArray<SkClipStack::Element>& clips); 184 185 void drawPoints(const float* points, int count, const SkPaint& paint, 186 SkCanvas::PointMode mode); 187 188 SkAutoTUnref<SkCanvas> mCanvas; 189 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves. 190}; 191 192Canvas* Canvas::create_canvas(const SkBitmap& bitmap) { 193 return new SkiaCanvas(bitmap); 194} 195 196Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) { 197 return new SkiaCanvas(skiaCanvas); 198} 199 200SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) { 201 mCanvas.reset(new SkCanvas(bitmap)); 202} 203 204// ---------------------------------------------------------------------------- 205// Canvas state operations: Replace Bitmap 206// ---------------------------------------------------------------------------- 207 208class ClipCopier : public SkCanvas::ClipVisitor { 209public: 210 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {} 211 212 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) { 213 m_dstCanvas->clipRect(rect, op, antialias); 214 } 215 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) { 216 m_dstCanvas->clipRRect(rrect, op, antialias); 217 } 218 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) { 219 m_dstCanvas->clipPath(path, op, antialias); 220 } 221 222private: 223 SkCanvas* m_dstCanvas; 224}; 225 226void SkiaCanvas::setBitmap(const SkBitmap& bitmap) { 227 SkCanvas* newCanvas = new SkCanvas(bitmap); 228 229 if (!bitmap.isNull()) { 230 // Copy the canvas matrix & clip state. 231 newCanvas->setMatrix(mCanvas->getTotalMatrix()); 232 233 ClipCopier copier(newCanvas); 234 mCanvas->replayClips(&copier); 235 } 236 237 // unrefs the existing canvas 238 mCanvas.reset(newCanvas); 239 240 // clean up the old save stack 241 mSaveStack.reset(NULL); 242} 243 244// ---------------------------------------------------------------------------- 245// Canvas state operations 246// ---------------------------------------------------------------------------- 247 248bool SkiaCanvas::isOpaque() { 249 return mCanvas->imageInfo().isOpaque(); 250} 251 252int SkiaCanvas::width() { 253 return mCanvas->imageInfo().width(); 254} 255 256int SkiaCanvas::height() { 257 return mCanvas->imageInfo().height(); 258} 259 260// ---------------------------------------------------------------------------- 261// Canvas state operations: Save (layer) 262// ---------------------------------------------------------------------------- 263 264int SkiaCanvas::getSaveCount() const { 265 return mCanvas->getSaveCount(); 266} 267 268int SkiaCanvas::save(SaveFlags::Flags flags) { 269 int count = mCanvas->save(); 270 recordPartialSave(flags); 271 return count; 272} 273 274// The SkiaCanvas::restore operation layers on the capability to preserve 275// either (or both) the matrix and/or clip state after a SkCanvas::restore 276// operation. It does this by explicitly saving off the clip & matrix state 277// when requested and playing it back after the SkCanvas::restore. 278void SkiaCanvas::restore() { 279 const SaveRec* rec = (NULL == mSaveStack.get()) 280 ? NULL 281 : static_cast<SaveRec*>(mSaveStack->back()); 282 int currentSaveCount = mCanvas->getSaveCount(); 283 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount); 284 285 if (NULL == rec || rec->saveCount != currentSaveCount) { 286 // Fast path - no record for this frame. 287 mCanvas->restore(); 288 return; 289 } 290 291 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix); 292 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip); 293 294 SkMatrix savedMatrix; 295 if (preserveMatrix) { 296 savedMatrix = mCanvas->getTotalMatrix(); 297 } 298 299 SkTArray<SkClipStack::Element> savedClips; 300 int topClipStackFrame = mCanvas->getClipStack()->getSaveCount(); 301 if (preserveClip) { 302 saveClipsForFrame(savedClips, topClipStackFrame); 303 } 304 305 mCanvas->restore(); 306 307 if (preserveMatrix) { 308 mCanvas->setMatrix(savedMatrix); 309 } 310 311 if (preserveClip && !savedClips.empty() && 312 topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) { 313 // Only reapply the saved clips if the top clip stack frame was actually 314 // popped by restore(). If it wasn't, it means it doesn't belong to the 315 // restored canvas frame (SkCanvas lazy save/restore kicked in). 316 applyClips(savedClips); 317 } 318 319 mSaveStack->pop_back(); 320} 321 322void SkiaCanvas::restoreToCount(int restoreCount) { 323 while (mCanvas->getSaveCount() > restoreCount) { 324 this->restore(); 325 } 326} 327 328static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) { 329 SkCanvas::SaveLayerFlags layerFlags = 0; 330 331 if (!(flags & SaveFlags::HasAlphaLayer)) { 332 layerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag; 333 } 334 335 if (!(flags & SaveFlags::ClipToLayer)) { 336 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag; 337 } 338 339 return layerFlags; 340} 341 342int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, 343 const SkPaint* paint, SaveFlags::Flags flags) { 344 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom); 345 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags)); 346 347 int count = mCanvas->saveLayer(rec); 348 recordPartialSave(flags); 349 return count; 350} 351 352int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, 353 int alpha, SaveFlags::Flags flags) { 354 if (static_cast<unsigned>(alpha) < 0xFF) { 355 SkPaint alphaPaint; 356 alphaPaint.setAlpha(alpha); 357 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags); 358 } 359 return this->saveLayer(left, top, right, bottom, nullptr, flags); 360} 361 362// ---------------------------------------------------------------------------- 363// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags) 364// ---------------------------------------------------------------------------- 365 366void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) { 367 // A partial save is a save operation which doesn't capture the full canvas state. 368 // (either SaveFlags::Matrix or SaveFlags::Clip is missing). 369 370 // Mask-out non canvas state bits. 371 flags &= SaveFlags::MatrixClip; 372 373 if (flags == SaveFlags::MatrixClip) { 374 // not a partial save. 375 return; 376 } 377 378 if (NULL == mSaveStack.get()) { 379 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8)); 380 } 381 382 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back()); 383 rec->saveCount = mCanvas->getSaveCount(); 384 rec->saveFlags = flags; 385} 386 387void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, 388 int saveCountToBackup) { 389 // Each SkClipStack::Element stores the index of the canvas save 390 // with which it is associated. Backup only those Elements that 391 // are associated with 'saveCountToBackup' 392 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(), 393 SkClipStack::Iter::kTop_IterStart); 394 while (const SkClipStack::Element* elem = clipIterator.prev()) { 395 if (elem->getSaveCount() < saveCountToBackup) { 396 // done with the target save count. 397 break; 398 } 399 SkASSERT(elem->getSaveCount() == saveCountToBackup); 400 clips.push_back(*elem); 401 } 402} 403 404void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) { 405 ClipCopier clipCopier(mCanvas); 406 407 // The clip stack stores clips in device space. 408 SkMatrix origMatrix = mCanvas->getTotalMatrix(); 409 mCanvas->resetMatrix(); 410 411 // We pushed the clips in reverse order. 412 for (int i = clips.count() - 1; i >= 0; --i) { 413 clips[i].replay(&clipCopier); 414 } 415 416 mCanvas->setMatrix(origMatrix); 417} 418 419// ---------------------------------------------------------------------------- 420// Canvas state operations: Matrix 421// ---------------------------------------------------------------------------- 422 423void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const { 424 *outMatrix = mCanvas->getTotalMatrix(); 425} 426 427void SkiaCanvas::setMatrix(const SkMatrix& matrix) { 428 mCanvas->setMatrix(matrix); 429} 430 431void SkiaCanvas::concat(const SkMatrix& matrix) { 432 mCanvas->concat(matrix); 433} 434 435void SkiaCanvas::rotate(float degrees) { 436 mCanvas->rotate(degrees); 437} 438 439void SkiaCanvas::scale(float sx, float sy) { 440 mCanvas->scale(sx, sy); 441} 442 443void SkiaCanvas::skew(float sx, float sy) { 444 mCanvas->skew(sx, sy); 445} 446 447void SkiaCanvas::translate(float dx, float dy) { 448 mCanvas->translate(dx, dy); 449} 450 451// ---------------------------------------------------------------------------- 452// Canvas state operations: Clips 453// ---------------------------------------------------------------------------- 454 455// This function is a mirror of SkCanvas::getClipBounds except that it does 456// not outset the edge of the clip to account for anti-aliasing. There is 457// a skia bug to investigate pushing this logic into back into skia. 458// (see https://code.google.com/p/skia/issues/detail?id=1303) 459bool SkiaCanvas::getClipBounds(SkRect* outRect) const { 460 SkIRect ibounds; 461 if (!mCanvas->getClipDeviceBounds(&ibounds)) { 462 return false; 463 } 464 465 SkMatrix inverse; 466 // if we can't invert the CTM, we can't return local clip bounds 467 if (!mCanvas->getTotalMatrix().invert(&inverse)) { 468 if (outRect) { 469 outRect->setEmpty(); 470 } 471 return false; 472 } 473 474 if (NULL != outRect) { 475 SkRect r = SkRect::Make(ibounds); 476 inverse.mapRect(outRect, r); 477 } 478 return true; 479} 480 481bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const { 482 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom); 483 return mCanvas->quickReject(bounds); 484} 485 486bool SkiaCanvas::quickRejectPath(const SkPath& path) const { 487 return mCanvas->quickReject(path); 488} 489 490bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { 491 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 492 mCanvas->clipRect(rect, op); 493 return !mCanvas->isClipEmpty(); 494} 495 496bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) { 497 mCanvas->clipPath(*path, op); 498 return !mCanvas->isClipEmpty(); 499} 500 501bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) { 502 SkPath rgnPath; 503 if (region->getBoundaryPath(&rgnPath)) { 504 // The region is specified in device space. 505 SkMatrix savedMatrix = mCanvas->getTotalMatrix(); 506 mCanvas->resetMatrix(); 507 mCanvas->clipPath(rgnPath, op); 508 mCanvas->setMatrix(savedMatrix); 509 } else { 510 mCanvas->clipRect(SkRect::MakeEmpty(), op); 511 } 512 return !mCanvas->isClipEmpty(); 513} 514 515// ---------------------------------------------------------------------------- 516// Canvas state operations: Filters 517// ---------------------------------------------------------------------------- 518 519SkDrawFilter* SkiaCanvas::getDrawFilter() { 520 return mCanvas->getDrawFilter(); 521} 522 523void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) { 524 mCanvas->setDrawFilter(drawFilter); 525} 526 527// ---------------------------------------------------------------------------- 528// Canvas draw operations 529// ---------------------------------------------------------------------------- 530 531void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) { 532 mCanvas->drawColor(color, mode); 533} 534 535void SkiaCanvas::drawPaint(const SkPaint& paint) { 536 mCanvas->drawPaint(paint); 537} 538 539// ---------------------------------------------------------------------------- 540// Canvas draw operations: Geometry 541// ---------------------------------------------------------------------------- 542 543void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint, 544 SkCanvas::PointMode mode) { 545 // convert the floats into SkPoints 546 count >>= 1; // now it is the number of points 547 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]); 548 for (int i = 0; i < count; i++) { 549 pts[i].set(points[0], points[1]); 550 points += 2; 551 } 552 mCanvas->drawPoints(mode, count, pts.get(), paint); 553} 554 555 556void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) { 557 mCanvas->drawPoint(x, y, paint); 558} 559 560void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) { 561 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode); 562} 563 564void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY, 565 const SkPaint& paint) { 566 mCanvas->drawLine(startX, startY, stopX, stopY, paint); 567} 568 569void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) { 570 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode); 571} 572 573void SkiaCanvas::drawRect(float left, float top, float right, float bottom, 574 const SkPaint& paint) { 575 mCanvas->drawRectCoords(left, top, right, bottom, paint); 576 577} 578 579void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { 580 SkRegion::Iterator it(region); 581 while (!it.done()) { 582 mCanvas->drawRect(SkRect::Make(it.rect()), paint); 583 it.next(); 584 } 585} 586 587void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, 588 float rx, float ry, const SkPaint& paint) { 589 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); 590 mCanvas->drawRoundRect(rect, rx, ry, paint); 591} 592 593void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) { 594 mCanvas->drawCircle(x, y, radius, paint); 595} 596 597void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) { 598 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom); 599 mCanvas->drawOval(oval, paint); 600} 601 602void SkiaCanvas::drawArc(float left, float top, float right, float bottom, 603 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) { 604 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom); 605 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint); 606} 607 608void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { 609 mCanvas->drawPath(path, paint); 610} 611 612void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount, 613 const float* verts, const float* texs, const int* colors, 614 const uint16_t* indices, int indexCount, const SkPaint& paint) { 615#ifndef SK_SCALAR_IS_FLOAT 616 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid"); 617#endif 618 const int ptCount = vertexCount >> 1; 619 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs, 620 (SkColor*)colors, NULL, indices, indexCount, paint); 621} 622 623// ---------------------------------------------------------------------------- 624// Canvas draw operations: Bitmaps 625// ---------------------------------------------------------------------------- 626 627void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) { 628 mCanvas->drawBitmap(bitmap, left, top, paint); 629} 630 631void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) { 632 SkAutoCanvasRestore acr(mCanvas, true); 633 mCanvas->concat(matrix); 634 mCanvas->drawBitmap(bitmap, 0, 0, paint); 635} 636 637void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 638 float srcRight, float srcBottom, float dstLeft, float dstTop, 639 float dstRight, float dstBottom, const SkPaint* paint) { 640 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom); 641 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); 642 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint); 643} 644 645void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 646 const float* vertices, const int* colors, const SkPaint* paint) { 647 648 const int ptCount = (meshWidth + 1) * (meshHeight + 1); 649 const int indexCount = meshWidth * meshHeight * 6; 650 651 /* Our temp storage holds 2 or 3 arrays. 652 texture points [ptCount * sizeof(SkPoint)] 653 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a 654 copy to convert from float to fixed 655 indices [ptCount * sizeof(uint16_t)] 656 */ 657 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[] 658 storageSize += indexCount * sizeof(uint16_t); // indices[] 659 660 661#ifndef SK_SCALAR_IS_FLOAT 662 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid"); 663#endif 664 std::unique_ptr<char[]> storage(new char[storageSize]); 665 SkPoint* texs = (SkPoint*)storage.get(); 666 uint16_t* indices = (uint16_t*)(texs + ptCount); 667 668 // cons up texture coordinates and indices 669 { 670 const SkScalar w = SkIntToScalar(bitmap.width()); 671 const SkScalar h = SkIntToScalar(bitmap.height()); 672 const SkScalar dx = w / meshWidth; 673 const SkScalar dy = h / meshHeight; 674 675 SkPoint* texsPtr = texs; 676 SkScalar y = 0; 677 for (int i = 0; i <= meshHeight; i++) { 678 if (i == meshHeight) { 679 y = h; // to ensure numerically we hit h exactly 680 } 681 SkScalar x = 0; 682 for (int j = 0; j < meshWidth; j++) { 683 texsPtr->set(x, y); 684 texsPtr += 1; 685 x += dx; 686 } 687 texsPtr->set(w, y); 688 texsPtr += 1; 689 y += dy; 690 } 691 SkASSERT(texsPtr - texs == ptCount); 692 } 693 694 // cons up indices 695 { 696 uint16_t* indexPtr = indices; 697 int index = 0; 698 for (int i = 0; i < meshHeight; i++) { 699 for (int j = 0; j < meshWidth; j++) { 700 // lower-left triangle 701 *indexPtr++ = index; 702 *indexPtr++ = index + meshWidth + 1; 703 *indexPtr++ = index + meshWidth + 2; 704 // upper-right triangle 705 *indexPtr++ = index; 706 *indexPtr++ = index + meshWidth + 2; 707 *indexPtr++ = index + 1; 708 // bump to the next cell 709 index += 1; 710 } 711 // bump to the next row 712 index += 1; 713 } 714 SkASSERT(indexPtr - indices == indexCount); 715 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize); 716 } 717 718 // double-check that we have legal indices 719#ifdef SK_DEBUG 720 { 721 for (int i = 0; i < indexCount; i++) { 722 SkASSERT((unsigned)indices[i] < (unsigned)ptCount); 723 } 724 } 725#endif 726 727 // cons-up a shader for the bitmap 728 SkPaint tmpPaint; 729 if (paint) { 730 tmpPaint = *paint; 731 } 732 SkShader* shader = SkShader::CreateBitmapShader(bitmap, 733 SkShader::kClamp_TileMode, 734 SkShader::kClamp_TileMode); 735 SkSafeUnref(tmpPaint.setShader(shader)); 736 737 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices, 738 texs, (const SkColor*)colors, NULL, indices, 739 indexCount, tmpPaint); 740} 741 742void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk, 743 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) { 744 SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); 745 NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr); 746} 747 748void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) { 749 vectorDrawable->drawStaging(this); 750} 751 752// ---------------------------------------------------------------------------- 753// Canvas draw operations: Text 754// ---------------------------------------------------------------------------- 755 756void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count, 757 const SkPaint& paint, float x, float y, 758 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 759 float totalAdvance) { 760 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 761 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paint); 762 drawTextDecorations(x, y, totalAdvance, paint); 763} 764 765void SkiaCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path, 766 float hOffset, float vOffset, const SkPaint& paint) { 767 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint); 768} 769 770// ---------------------------------------------------------------------------- 771// Canvas draw operations: Animations 772// ---------------------------------------------------------------------------- 773 774class AnimatedRoundRect : public SkDrawable { 775 public: 776 AnimatedRoundRect(uirenderer::CanvasPropertyPrimitive* left, 777 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, 778 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, 779 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* p) : 780 mLeft(left), mTop(top), mRight(right), mBottom(bottom), mRx(rx), mRy(ry), mPaint(p) {} 781 782 protected: 783 virtual SkRect onGetBounds() override { 784 return SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value); 785 } 786 virtual void onDraw(SkCanvas* canvas) override { 787 SkRect rect = SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value); 788 canvas->drawRoundRect(rect, mRx->value, mRy->value, mPaint->value); 789 } 790 791 private: 792 sp<uirenderer::CanvasPropertyPrimitive> mLeft; 793 sp<uirenderer::CanvasPropertyPrimitive> mTop; 794 sp<uirenderer::CanvasPropertyPrimitive> mRight; 795 sp<uirenderer::CanvasPropertyPrimitive> mBottom; 796 sp<uirenderer::CanvasPropertyPrimitive> mRx; 797 sp<uirenderer::CanvasPropertyPrimitive> mRy; 798 sp<uirenderer::CanvasPropertyPaint> mPaint; 799}; 800 801class AnimatedCircle : public SkDrawable { 802 public: 803 AnimatedCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y, 804 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) : 805 mX(x), mY(y), mRadius(radius), mPaint(paint) {} 806 807 protected: 808 virtual SkRect onGetBounds() override { 809 const float x = mX->value; 810 const float y = mY->value; 811 const float radius = mRadius->value; 812 return SkRect::MakeLTRB(x - radius, y - radius, x + radius, y + radius); 813 } 814 virtual void onDraw(SkCanvas* canvas) override { 815 canvas->drawCircle(mX->value, mY->value, mRadius->value, mPaint->value); 816 } 817 818 private: 819 sp<uirenderer::CanvasPropertyPrimitive> mX; 820 sp<uirenderer::CanvasPropertyPrimitive> mY; 821 sp<uirenderer::CanvasPropertyPrimitive> mRadius; 822 sp<uirenderer::CanvasPropertyPaint> mPaint; 823}; 824 825void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, 826 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, 827 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, 828 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) { 829 SkAutoTUnref<AnimatedRoundRect> drawable( 830 new AnimatedRoundRect(left, top, right, bottom, rx, ry, paint)); 831 mCanvas->drawDrawable(drawable.get()); 832} 833 834void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y, 835 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) { 836 SkAutoTUnref<AnimatedCircle> drawable(new AnimatedCircle(x, y, radius, paint)); 837 mCanvas->drawDrawable(drawable.get()); 838} 839 840// ---------------------------------------------------------------------------- 841// Canvas draw operations: View System 842// ---------------------------------------------------------------------------- 843 844void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layer) { } 845 846void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { } 847 848void SkiaCanvas::callDrawGLFunction(Functor* functor, 849 uirenderer::GlFunctorLifecycleListener* listener) { } 850 851} // namespace android 852