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