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