DisplayListCanvas.cpp revision a7f6bba1a3565c19715e878dfe7f0e01022944ff
1/* 2 * Copyright (C) 2010 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 "DisplayListCanvas.h" 18 19#include "DeferredDisplayList.h" 20#include "DeferredLayerUpdater.h" 21#include "DisplayListOp.h" 22#include "ResourceCache.h" 23#include "RenderNode.h" 24#include "VectorDrawable.h" 25#include "utils/PaintUtils.h" 26 27#include <SkCamera.h> 28#include <SkCanvas.h> 29 30#include <private/hwui/DrawGlInfo.h> 31 32namespace android { 33namespace uirenderer { 34 35DisplayListCanvas::DisplayListCanvas(int width, int height) 36 : mState(*this) 37 , mResourceCache(ResourceCache::getInstance()) 38 , mDisplayList(nullptr) 39 , mTranslateX(0.0f) 40 , mTranslateY(0.0f) 41 , mHasDeferredTranslate(false) 42 , mDeferredBarrierType(kBarrier_None) 43 , mHighContrastText(false) 44 , mRestoreSaveCount(-1) { 45 resetRecording(width, height); 46} 47 48DisplayListCanvas::~DisplayListCanvas() { 49 LOG_ALWAYS_FATAL_IF(mDisplayList, 50 "Destroyed a DisplayListCanvas during a record!"); 51} 52 53void DisplayListCanvas::resetRecording(int width, int height) { 54 LOG_ALWAYS_FATAL_IF(mDisplayList, 55 "prepareDirty called a second time during a recording!"); 56 mDisplayList = new DisplayList(); 57 58 mState.initializeSaveStack(width, height, 59 0, 0, width, height, Vector3()); 60 61 mDeferredBarrierType = kBarrier_InOrder; 62 mState.setDirtyClip(false); 63 mRestoreSaveCount = -1; 64} 65 66 67/////////////////////////////////////////////////////////////////////////////// 68// Operations 69/////////////////////////////////////////////////////////////////////////////// 70 71DisplayList* DisplayListCanvas::finishRecording() { 72 flushRestoreToCount(); 73 flushTranslate(); 74 75 mPaintMap.clear(); 76 mRegionMap.clear(); 77 mPathMap.clear(); 78 DisplayList* displayList = mDisplayList; 79 mDisplayList = nullptr; 80 mSkiaCanvasProxy.reset(nullptr); 81 return displayList; 82} 83 84void DisplayListCanvas::callDrawGLFunction(Functor *functor) { 85 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 86 mDisplayList->functors.push_back(functor); 87} 88 89SkCanvas* DisplayListCanvas::asSkCanvas() { 90 LOG_ALWAYS_FATAL_IF(!mDisplayList, 91 "attempting to get an SkCanvas when we are not recording!"); 92 if (!mSkiaCanvasProxy) { 93 mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this)); 94 } 95 96 // SkCanvas instances default to identity transform, but should inherit 97 // the state of this Canvas; if this code was in the SkiaCanvasProxy 98 // constructor, we couldn't cache mSkiaCanvasProxy. 99 SkMatrix parentTransform; 100 getMatrix(&parentTransform); 101 mSkiaCanvasProxy.get()->setMatrix(parentTransform); 102 103 return mSkiaCanvasProxy.get(); 104} 105 106int DisplayListCanvas::save(SaveFlags::Flags flags) { 107 addStateOp(new (alloc()) SaveOp((int) flags)); 108 return mState.save((int) flags); 109} 110 111void DisplayListCanvas::restore() { 112 if (mRestoreSaveCount < 0) { 113 restoreToCount(getSaveCount() - 1); 114 return; 115 } 116 117 mRestoreSaveCount--; 118 flushTranslate(); 119 mState.restore(); 120} 121 122void DisplayListCanvas::restoreToCount(int saveCount) { 123 mRestoreSaveCount = saveCount; 124 flushTranslate(); 125 mState.restoreToCount(saveCount); 126} 127 128int DisplayListCanvas::saveLayer(float left, float top, float right, float bottom, 129 const SkPaint* paint, SaveFlags::Flags flags) { 130 // force matrix/clip isolation for layer 131 flags |= SaveFlags::MatrixClip; 132 133 paint = refPaint(paint); 134 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, (int) flags)); 135 return mState.save((int) flags); 136} 137 138void DisplayListCanvas::translate(float dx, float dy) { 139 if (dx == 0.0f && dy == 0.0f) return; 140 141 mHasDeferredTranslate = true; 142 mTranslateX += dx; 143 mTranslateY += dy; 144 flushRestoreToCount(); 145 mState.translate(dx, dy, 0.0f); 146} 147 148void DisplayListCanvas::rotate(float degrees) { 149 if (degrees == 0.0f) return; 150 151 addStateOp(new (alloc()) RotateOp(degrees)); 152 mState.rotate(degrees); 153} 154 155void DisplayListCanvas::scale(float sx, float sy) { 156 if (sx == 1.0f && sy == 1.0f) return; 157 158 addStateOp(new (alloc()) ScaleOp(sx, sy)); 159 mState.scale(sx, sy); 160} 161 162void DisplayListCanvas::skew(float sx, float sy) { 163 addStateOp(new (alloc()) SkewOp(sx, sy)); 164 mState.skew(sx, sy); 165} 166 167void DisplayListCanvas::setMatrix(const SkMatrix& matrix) { 168 addStateOp(new (alloc()) SetMatrixOp(matrix)); 169 mState.setMatrix(matrix); 170} 171 172void DisplayListCanvas::concat(const SkMatrix& matrix) { 173 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 174 mState.concatMatrix(matrix); 175} 176 177bool DisplayListCanvas::getClipBounds(SkRect* outRect) const { 178 Rect bounds = mState.getLocalClipBounds(); 179 *outRect = SkRect::MakeLTRB(bounds.left, bounds.top, bounds.right, bounds.bottom); 180 return !(outRect->isEmpty()); 181} 182 183bool DisplayListCanvas::quickRejectRect(float left, float top, float right, float bottom) const { 184 return mState.quickRejectConservative(left, top, right, bottom); 185} 186 187bool DisplayListCanvas::quickRejectPath(const SkPath& path) const { 188 SkRect bounds = path.getBounds(); 189 return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); 190} 191 192 193bool DisplayListCanvas::clipRect(float left, float top, float right, float bottom, 194 SkRegion::Op op) { 195 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op)); 196 return mState.clipRect(left, top, right, bottom, op); 197} 198 199bool DisplayListCanvas::clipPath(const SkPath* path, SkRegion::Op op) { 200 path = refPath(path); 201 addStateOp(new (alloc()) ClipPathOp(path, op)); 202 return mState.clipPath(path, op); 203} 204 205bool DisplayListCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) { 206 region = refRegion(region); 207 addStateOp(new (alloc()) ClipRegionOp(region, op)); 208 return mState.clipRegion(region, op); 209} 210 211void DisplayListCanvas::drawRenderNode(RenderNode* renderNode) { 212 LOG_ALWAYS_FATAL_IF(!renderNode, "missing rendernode"); 213 DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp( 214 renderNode, 215 *mState.currentTransform(), 216 mState.clipIsSimple()); 217 addRenderNodeOp(op); 218} 219 220void DisplayListCanvas::drawLayer(DeferredLayerUpdater* layerHandle) { 221 // We ref the DeferredLayerUpdater due to its thread-safe ref-counting 222 // semantics. 223 mDisplayList->ref(layerHandle); 224 addDrawOp(new (alloc()) DrawLayerOp(layerHandle->backingLayer())); 225} 226 227void DisplayListCanvas::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) { 228 bitmap = refBitmap(*bitmap); 229 paint = refPaint(paint); 230 231 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, paint)); 232} 233 234void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, 235 const SkPaint* paint) { 236 save(SaveFlags::Matrix); 237 translate(left, top); 238 drawBitmap(&bitmap, paint); 239 restore(); 240} 241 242void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, 243 const SkPaint* paint) { 244 if (matrix.isIdentity()) { 245 drawBitmap(&bitmap, paint); 246 } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) 247 && MathUtils::isPositive(matrix.getScaleX()) 248 && MathUtils::isPositive(matrix.getScaleY())) { 249 // SkMatrix::isScaleTranslate() not available in L 250 SkRect src; 251 SkRect dst; 252 bitmap.getBounds(&src); 253 matrix.mapRect(&dst, src); 254 drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, 255 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); 256 } else { 257 save(SaveFlags::Matrix); 258 concat(matrix); 259 drawBitmap(&bitmap, paint); 260 restore(); 261 } 262} 263 264void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop, 265 float srcRight, float srcBottom, float dstLeft, float dstTop, 266 float dstRight, float dstBottom, const SkPaint* paint) { 267 if (srcLeft == 0 && srcTop == 0 268 && srcRight == bitmap.width() 269 && srcBottom == bitmap.height() 270 && (srcBottom - srcTop == dstBottom - dstTop) 271 && (srcRight - srcLeft == dstRight - dstLeft)) { 272 // transform simple rect to rect drawing case into position bitmap ops, since they merge 273 save(SaveFlags::Matrix); 274 translate(dstLeft, dstTop); 275 drawBitmap(&bitmap, paint); 276 restore(); 277 } else { 278 paint = refPaint(paint); 279 280 if (paint && paint->getShader()) { 281 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft); 282 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop); 283 if (!MathUtils::areEqual(scaleX, 1.0f) || !MathUtils::areEqual(scaleY, 1.0f)) { 284 // Apply the scale transform on the canvas, so that the shader 285 // effectively calculates positions relative to src rect space 286 287 save(SaveFlags::Matrix); 288 translate(dstLeft, dstTop); 289 scale(scaleX, scaleY); 290 291 dstLeft = 0.0f; 292 dstTop = 0.0f; 293 dstRight = srcRight - srcLeft; 294 dstBottom = srcBottom - srcTop; 295 296 addDrawOp(new (alloc()) DrawBitmapRectOp(refBitmap(bitmap), 297 srcLeft, srcTop, srcRight, srcBottom, 298 dstLeft, dstTop, dstRight, dstBottom, paint)); 299 restore(); 300 return; 301 } 302 } 303 304 addDrawOp(new (alloc()) DrawBitmapRectOp(refBitmap(bitmap), 305 srcLeft, srcTop, srcRight, srcBottom, 306 dstLeft, dstTop, dstRight, dstBottom, paint)); 307 } 308} 309 310void DisplayListCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight, 311 const float* vertices, const int* colors, const SkPaint* paint) { 312 int vertexCount = (meshWidth + 1) * (meshHeight + 1); 313 vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex 314 paint = refPaint(paint); 315 colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex 316 317 addDrawOp(new (alloc()) DrawBitmapMeshOp(refBitmap(bitmap), meshWidth, meshHeight, 318 vertices, colors, paint)); 319} 320 321void DisplayListCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& patch, 322 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) { 323 const SkBitmap* bitmapPtr = refBitmap(bitmap); 324 const Res_png_9patch* patchPtr = refPatch(&patch); 325 paint = refPaint(paint); 326 327 addDrawOp(new (alloc()) DrawPatchOp(bitmapPtr, patchPtr, 328 dstLeft, dstTop, dstRight, dstBottom, paint)); 329} 330 331void DisplayListCanvas::drawColor(int color, SkXfermode::Mode mode) { 332 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 333} 334 335void DisplayListCanvas::drawPaint(const SkPaint& paint) { 336 SkRect bounds; 337 if (getClipBounds(&bounds)) { 338 drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint); 339 } 340} 341 342 343void DisplayListCanvas::drawRect(float left, float top, float right, float bottom, 344 const SkPaint& paint) { 345 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, refPaint(&paint))); 346} 347 348void DisplayListCanvas::drawRoundRect(float left, float top, float right, float bottom, 349 float rx, float ry, const SkPaint& paint) { 350 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, refPaint(&paint))); 351} 352 353void DisplayListCanvas::drawRoundRect( 354 CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top, 355 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom, 356 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry, 357 CanvasPropertyPaint* paint) { 358 mDisplayList->ref(left); 359 mDisplayList->ref(top); 360 mDisplayList->ref(right); 361 mDisplayList->ref(bottom); 362 mDisplayList->ref(rx); 363 mDisplayList->ref(ry); 364 mDisplayList->ref(paint); 365 refBitmapsInShader(paint->value.getShader()); 366 addDrawOp(new (alloc()) DrawRoundRectPropsOp(&left->value, &top->value, 367 &right->value, &bottom->value, &rx->value, &ry->value, &paint->value)); 368} 369 370void DisplayListCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) { 371 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, refPaint(&paint))); 372} 373 374void DisplayListCanvas::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 375 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) { 376 mDisplayList->ref(x); 377 mDisplayList->ref(y); 378 mDisplayList->ref(radius); 379 mDisplayList->ref(paint); 380 refBitmapsInShader(paint->value.getShader()); 381 addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value, 382 &radius->value, &paint->value)); 383} 384 385void DisplayListCanvas::drawOval(float left, float top, float right, float bottom, 386 const SkPaint& paint) { 387 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, refPaint(&paint))); 388} 389 390void DisplayListCanvas::drawArc(float left, float top, float right, float bottom, 391 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) { 392 if (fabs(sweepAngle) >= 360.0f) { 393 drawOval(left, top, right, bottom, paint); 394 } else { 395 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 396 startAngle, sweepAngle, useCenter, refPaint(&paint))); 397 } 398} 399 400void DisplayListCanvas::drawPath(const SkPath& path, const SkPaint& paint) { 401 addDrawOp(new (alloc()) DrawPathOp(refPath(&path), refPaint(&paint))); 402} 403 404void DisplayListCanvas::drawLines(const float* points, int count, const SkPaint& paint) { 405 points = refBuffer<float>(points, count); 406 407 addDrawOp(new (alloc()) DrawLinesOp(points, count, refPaint(&paint))); 408} 409 410void DisplayListCanvas::drawPoints(const float* points, int count, const SkPaint& paint) { 411 points = refBuffer<float>(points, count); 412 413 addDrawOp(new (alloc()) DrawPointsOp(points, count, refPaint(&paint))); 414} 415 416void DisplayListCanvas::drawVectorDrawable(VectorDrawableRoot* tree) { 417 mDisplayList->ref(tree); 418 addDrawOp(new (alloc()) DrawVectorDrawableOp(tree)); 419} 420 421void DisplayListCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int count, 422 const SkPath& path, float hOffset, float vOffset, const SkPaint& paint) { 423 if (!glyphs || count <= 0) return; 424 425 int bytesCount = 2 * count; 426 DrawOp* op = new (alloc()) DrawTextOnPathOp(refBuffer<glyph_t>(glyphs, count), 427 bytesCount, count, refPath(&path), 428 hOffset, vOffset, refPaint(&paint)); 429 addDrawOp(op); 430} 431 432void DisplayListCanvas::drawGlyphs(const uint16_t* glyphs, const float* positions, 433 int count, const SkPaint& paint, float x, float y, 434 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 435 float totalAdvance) { 436 437 if (!glyphs || count <= 0 || PaintUtils::paintWillNotDrawText(paint)) return; 438 439 int bytesCount = count * 2; 440 positions = refBuffer<float>(positions, count * 2); 441 Rect bounds(boundsLeft, boundsTop, boundsRight, boundsBottom); 442 443 DrawOp* op = new (alloc()) DrawTextOp(refBuffer<glyph_t>(glyphs, count), bytesCount, count, 444 x, y, positions, refPaint(&paint), totalAdvance, bounds); 445 addDrawOp(op); 446 drawTextDecorations(x, y, totalAdvance, paint); 447} 448 449void DisplayListCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { 450 if (paint.getStyle() != SkPaint::kFill_Style || 451 (paint.isAntiAlias() && !mState.currentTransform()->isSimple())) { 452 SkRegion::Iterator it(region); 453 while (!it.done()) { 454 const SkIRect& r = it.rect(); 455 drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint); 456 it.next(); 457 } 458 } else { 459 int count = 0; 460 Vector<float> rects; 461 SkRegion::Iterator it(region); 462 while (!it.done()) { 463 const SkIRect& r = it.rect(); 464 rects.push(r.fLeft); 465 rects.push(r.fTop); 466 rects.push(r.fRight); 467 rects.push(r.fBottom); 468 count += 4; 469 it.next(); 470 } 471 drawRects(rects.array(), count, &paint); 472 } 473} 474 475void DisplayListCanvas::drawRects(const float* rects, int count, const SkPaint* paint) { 476 if (count <= 0) return; 477 478 rects = refBuffer<float>(rects, count); 479 paint = refPaint(paint); 480 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 481} 482 483void DisplayListCanvas::setDrawFilter(SkDrawFilter* filter) { 484 mDrawFilter.reset(SkSafeRef(filter)); 485} 486 487void DisplayListCanvas::insertReorderBarrier(bool enableReorder) { 488 flushRestoreToCount(); 489 flushTranslate(); 490 mDeferredBarrierType = enableReorder ? kBarrier_OutOfOrder : kBarrier_InOrder; 491} 492 493void DisplayListCanvas::flushRestoreToCount() { 494 if (mRestoreSaveCount >= 0) { 495 addOpAndUpdateChunk(new (alloc()) RestoreToCountOp(mRestoreSaveCount)); 496 mRestoreSaveCount = -1; 497 } 498} 499 500void DisplayListCanvas::flushTranslate() { 501 if (mHasDeferredTranslate) { 502 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 503 addOpAndUpdateChunk(new (alloc()) TranslateOp(mTranslateX, mTranslateY)); 504 mTranslateX = mTranslateY = 0.0f; 505 } 506 mHasDeferredTranslate = false; 507 } 508} 509 510size_t DisplayListCanvas::addOpAndUpdateChunk(DisplayListOp* op) { 511 int insertIndex = mDisplayList->ops.size(); 512#if HWUI_NEW_OPS 513 LOG_ALWAYS_FATAL("unsupported"); 514#else 515 mDisplayList->ops.push_back(op); 516#endif 517 if (mDeferredBarrierType != kBarrier_None) { 518 // op is first in new chunk 519 mDisplayList->chunks.emplace_back(); 520 DisplayList::Chunk& newChunk = mDisplayList->chunks.back(); 521 newChunk.beginOpIndex = insertIndex; 522 newChunk.endOpIndex = insertIndex + 1; 523 newChunk.reorderChildren = (mDeferredBarrierType == kBarrier_OutOfOrder); 524 525 int nextChildIndex = mDisplayList->children.size(); 526 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex; 527 mDeferredBarrierType = kBarrier_None; 528 } else { 529 // standard case - append to existing chunk 530 mDisplayList->chunks.back().endOpIndex = insertIndex + 1; 531 } 532 return insertIndex; 533} 534 535size_t DisplayListCanvas::flushAndAddOp(DisplayListOp* op) { 536 flushRestoreToCount(); 537 flushTranslate(); 538 return addOpAndUpdateChunk(op); 539} 540 541size_t DisplayListCanvas::addStateOp(StateOp* op) { 542 return flushAndAddOp(op); 543} 544 545size_t DisplayListCanvas::addDrawOp(DrawOp* op) { 546 Rect localBounds; 547 if (op->getLocalBounds(localBounds)) { 548 bool rejected = quickRejectRect(localBounds.left, localBounds.top, 549 localBounds.right, localBounds.bottom); 550 op->setQuickRejected(rejected); 551 } 552 553 mDisplayList->hasDrawOps = true; 554 return flushAndAddOp(op); 555} 556 557size_t DisplayListCanvas::addRenderNodeOp(DrawRenderNodeOp* op) { 558 int opIndex = addDrawOp(op); 559#if !HWUI_NEW_OPS 560 int childIndex = mDisplayList->addChild(op); 561 562 // update the chunk's child indices 563 DisplayList::Chunk& chunk = mDisplayList->chunks.back(); 564 chunk.endChildIndex = childIndex + 1; 565 566 if (op->renderNode->stagingProperties().isProjectionReceiver()) { 567 // use staging property, since recording on UI thread 568 mDisplayList->projectionReceiveIndex = opIndex; 569 } 570#endif 571 return opIndex; 572} 573 574void DisplayListCanvas::refBitmapsInShader(const SkShader* shader) { 575 if (!shader) return; 576 577 // If this paint has an SkShader that has an SkBitmap add 578 // it to the bitmap pile 579 SkBitmap bitmap; 580 SkShader::TileMode xy[2]; 581 if (shader->isABitmap(&bitmap, nullptr, xy)) { 582 refBitmap(bitmap); 583 return; 584 } 585 SkShader::ComposeRec rec; 586 if (shader->asACompose(&rec)) { 587 refBitmapsInShader(rec.fShaderA); 588 refBitmapsInShader(rec.fShaderB); 589 return; 590 } 591} 592 593}; // namespace uirenderer 594}; // namespace android 595