DisplayListCanvas.cpp revision 1d8e194661085f9a18ab1b3cd12f9e19d3a86be5
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 mDisplayList->pushStagingFunctors.push_back(tree->getFunctor()); 419 addDrawOp(new (alloc()) DrawVectorDrawableOp(tree, tree->stagingProperties()->getBounds())); 420} 421 422void DisplayListCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int count, 423 const SkPath& path, float hOffset, float vOffset, const SkPaint& paint) { 424 if (!glyphs || count <= 0) return; 425 426 int bytesCount = 2 * count; 427 DrawOp* op = new (alloc()) DrawTextOnPathOp(refBuffer<glyph_t>(glyphs, count), 428 bytesCount, count, refPath(&path), 429 hOffset, vOffset, refPaint(&paint)); 430 addDrawOp(op); 431} 432 433void DisplayListCanvas::drawGlyphs(const uint16_t* glyphs, const float* positions, 434 int count, const SkPaint& paint, float x, float y, 435 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom, 436 float totalAdvance) { 437 438 if (!glyphs || count <= 0 || PaintUtils::paintWillNotDrawText(paint)) return; 439 440 int bytesCount = count * 2; 441 positions = refBuffer<float>(positions, count * 2); 442 Rect bounds(boundsLeft, boundsTop, boundsRight, boundsBottom); 443 444 DrawOp* op = new (alloc()) DrawTextOp(refBuffer<glyph_t>(glyphs, count), bytesCount, count, 445 x, y, positions, refPaint(&paint), totalAdvance, bounds); 446 addDrawOp(op); 447 drawTextDecorations(x, y, totalAdvance, paint); 448} 449 450void DisplayListCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { 451 if (paint.getStyle() != SkPaint::kFill_Style || 452 (paint.isAntiAlias() && !mState.currentTransform()->isSimple())) { 453 SkRegion::Iterator it(region); 454 while (!it.done()) { 455 const SkIRect& r = it.rect(); 456 drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint); 457 it.next(); 458 } 459 } else { 460 int count = 0; 461 Vector<float> rects; 462 SkRegion::Iterator it(region); 463 while (!it.done()) { 464 const SkIRect& r = it.rect(); 465 rects.push(r.fLeft); 466 rects.push(r.fTop); 467 rects.push(r.fRight); 468 rects.push(r.fBottom); 469 count += 4; 470 it.next(); 471 } 472 drawRects(rects.array(), count, &paint); 473 } 474} 475 476void DisplayListCanvas::drawRects(const float* rects, int count, const SkPaint* paint) { 477 if (count <= 0) return; 478 479 rects = refBuffer<float>(rects, count); 480 paint = refPaint(paint); 481 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 482} 483 484void DisplayListCanvas::setDrawFilter(SkDrawFilter* filter) { 485 mDrawFilter.reset(SkSafeRef(filter)); 486} 487 488void DisplayListCanvas::insertReorderBarrier(bool enableReorder) { 489 flushRestoreToCount(); 490 flushTranslate(); 491 mDeferredBarrierType = enableReorder ? kBarrier_OutOfOrder : kBarrier_InOrder; 492} 493 494void DisplayListCanvas::flushRestoreToCount() { 495 if (mRestoreSaveCount >= 0) { 496 addOpAndUpdateChunk(new (alloc()) RestoreToCountOp(mRestoreSaveCount)); 497 mRestoreSaveCount = -1; 498 } 499} 500 501void DisplayListCanvas::flushTranslate() { 502 if (mHasDeferredTranslate) { 503 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 504 addOpAndUpdateChunk(new (alloc()) TranslateOp(mTranslateX, mTranslateY)); 505 mTranslateX = mTranslateY = 0.0f; 506 } 507 mHasDeferredTranslate = false; 508 } 509} 510 511size_t DisplayListCanvas::addOpAndUpdateChunk(DisplayListOp* op) { 512 int insertIndex = mDisplayList->ops.size(); 513#if HWUI_NEW_OPS 514 LOG_ALWAYS_FATAL("unsupported"); 515#else 516 mDisplayList->ops.push_back(op); 517#endif 518 if (mDeferredBarrierType != kBarrier_None) { 519 // op is first in new chunk 520 mDisplayList->chunks.emplace_back(); 521 DisplayList::Chunk& newChunk = mDisplayList->chunks.back(); 522 newChunk.beginOpIndex = insertIndex; 523 newChunk.endOpIndex = insertIndex + 1; 524 newChunk.reorderChildren = (mDeferredBarrierType == kBarrier_OutOfOrder); 525 526 int nextChildIndex = mDisplayList->children.size(); 527 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex; 528 mDeferredBarrierType = kBarrier_None; 529 } else { 530 // standard case - append to existing chunk 531 mDisplayList->chunks.back().endOpIndex = insertIndex + 1; 532 } 533 return insertIndex; 534} 535 536size_t DisplayListCanvas::flushAndAddOp(DisplayListOp* op) { 537 flushRestoreToCount(); 538 flushTranslate(); 539 return addOpAndUpdateChunk(op); 540} 541 542size_t DisplayListCanvas::addStateOp(StateOp* op) { 543 return flushAndAddOp(op); 544} 545 546size_t DisplayListCanvas::addDrawOp(DrawOp* op) { 547 Rect localBounds; 548 if (op->getLocalBounds(localBounds)) { 549 bool rejected = quickRejectRect(localBounds.left, localBounds.top, 550 localBounds.right, localBounds.bottom); 551 op->setQuickRejected(rejected); 552 } 553 554 mDisplayList->hasDrawOps = true; 555 return flushAndAddOp(op); 556} 557 558size_t DisplayListCanvas::addRenderNodeOp(DrawRenderNodeOp* op) { 559 int opIndex = addDrawOp(op); 560#if !HWUI_NEW_OPS 561 int childIndex = mDisplayList->addChild(op); 562 563 // update the chunk's child indices 564 DisplayList::Chunk& chunk = mDisplayList->chunks.back(); 565 chunk.endChildIndex = childIndex + 1; 566 567 if (op->renderNode->stagingProperties().isProjectionReceiver()) { 568 // use staging property, since recording on UI thread 569 mDisplayList->projectionReceiveIndex = opIndex; 570 } 571#endif 572 return opIndex; 573} 574 575void DisplayListCanvas::refBitmapsInShader(const SkShader* shader) { 576 if (!shader) return; 577 578 // If this paint has an SkShader that has an SkBitmap add 579 // it to the bitmap pile 580 SkBitmap bitmap; 581 SkShader::TileMode xy[2]; 582 if (shader->isABitmap(&bitmap, nullptr, xy)) { 583 refBitmap(bitmap); 584 return; 585 } 586 SkShader::ComposeRec rec; 587 if (shader->asACompose(&rec)) { 588 refBitmapsInShader(rec.fShaderA); 589 refBitmapsInShader(rec.fShaderB); 590 return; 591 } 592} 593 594}; // namespace uirenderer 595}; // namespace android 596