DisplayListRenderer.cpp revision 8afd0f245cc0c4a0366f39f41b5f78e47ee83be3
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#define LOG_TAG "OpenGLRenderer" 18 19#include <SkCamera.h> 20#include <SkCanvas.h> 21 22#include <private/hwui/DrawGlInfo.h> 23 24#include "Caches.h" 25#include "DeferredDisplayList.h" 26#include "DisplayListLogBuffer.h" 27#include "DisplayListOp.h" 28#include "DisplayListRenderer.h" 29#include "RenderNode.h" 30 31namespace android { 32namespace uirenderer { 33 34DisplayListRenderer::DisplayListRenderer() 35 : mCaches(Caches::getInstance()) 36 , mDisplayListData(NULL) 37 , mTranslateX(0.0f) 38 , mTranslateY(0.0f) 39 , mDeferredBarrierType(kBarrier_None) 40 , mHighContrastText(false) 41 , mRestoreSaveCount(-1) { 42} 43 44DisplayListRenderer::~DisplayListRenderer() { 45 LOG_ALWAYS_FATAL_IF(mDisplayListData, 46 "Destroyed a DisplayListRenderer during a record!"); 47} 48 49/////////////////////////////////////////////////////////////////////////////// 50// Operations 51/////////////////////////////////////////////////////////////////////////////// 52 53DisplayListData* DisplayListRenderer::finishRecording() { 54 mPaintMap.clear(); 55 mRegionMap.clear(); 56 mPathMap.clear(); 57 DisplayListData* data = mDisplayListData; 58 mDisplayListData = 0; 59 return data; 60} 61 62status_t DisplayListRenderer::prepareDirty(float left, float top, 63 float right, float bottom, bool opaque) { 64 65 LOG_ALWAYS_FATAL_IF(mDisplayListData, 66 "prepareDirty called a second time during a recording!"); 67 mDisplayListData = new DisplayListData(); 68 69 initializeSaveStack(0, 0, getWidth(), getHeight(), Vector3()); 70 71 mDeferredBarrierType = kBarrier_InOrder; 72 mDirtyClip = opaque; 73 mRestoreSaveCount = -1; 74 75 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 76} 77 78void DisplayListRenderer::finish() { 79 flushRestoreToCount(); 80 flushTranslate(); 81} 82 83void DisplayListRenderer::interrupt() { 84} 85 86void DisplayListRenderer::resume() { 87} 88 89status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 90 // Ignore dirty during recording, it matters only when we replay 91 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 92 mDisplayListData->functors.add(functor); 93 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 94} 95 96int DisplayListRenderer::save(int flags) { 97 addStateOp(new (alloc()) SaveOp(flags)); 98 return StatefulBaseRenderer::save(flags); 99} 100 101void DisplayListRenderer::restore() { 102 if (mRestoreSaveCount < 0) { 103 restoreToCount(getSaveCount() - 1); 104 return; 105 } 106 107 mRestoreSaveCount--; 108 flushTranslate(); 109 StatefulBaseRenderer::restore(); 110} 111 112void DisplayListRenderer::restoreToCount(int saveCount) { 113 mRestoreSaveCount = saveCount; 114 flushTranslate(); 115 StatefulBaseRenderer::restoreToCount(saveCount); 116} 117 118int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 119 const SkPaint* paint, int flags) { 120 paint = refPaint(paint); 121 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags)); 122 return StatefulBaseRenderer::save(flags); 123} 124 125void DisplayListRenderer::translate(float dx, float dy, float dz) { 126 // ignore dz, not used at defer time 127 mHasDeferredTranslate = true; 128 mTranslateX += dx; 129 mTranslateY += dy; 130 flushRestoreToCount(); 131 StatefulBaseRenderer::translate(dx, dy, dz); 132} 133 134void DisplayListRenderer::rotate(float degrees) { 135 addStateOp(new (alloc()) RotateOp(degrees)); 136 StatefulBaseRenderer::rotate(degrees); 137} 138 139void DisplayListRenderer::scale(float sx, float sy) { 140 addStateOp(new (alloc()) ScaleOp(sx, sy)); 141 StatefulBaseRenderer::scale(sx, sy); 142} 143 144void DisplayListRenderer::skew(float sx, float sy) { 145 addStateOp(new (alloc()) SkewOp(sx, sy)); 146 StatefulBaseRenderer::skew(sx, sy); 147} 148 149void DisplayListRenderer::setMatrix(const SkMatrix& matrix) { 150 addStateOp(new (alloc()) SetMatrixOp(matrix)); 151 StatefulBaseRenderer::setMatrix(matrix); 152} 153 154void DisplayListRenderer::concatMatrix(const SkMatrix& matrix) { 155 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 156 StatefulBaseRenderer::concatMatrix(matrix); 157} 158 159bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom, 160 SkRegion::Op op) { 161 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op)); 162 return StatefulBaseRenderer::clipRect(left, top, right, bottom, op); 163} 164 165bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) { 166 path = refPath(path); 167 addStateOp(new (alloc()) ClipPathOp(path, op)); 168 return StatefulBaseRenderer::clipPath(path, op); 169} 170 171bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { 172 region = refRegion(region); 173 addStateOp(new (alloc()) ClipRegionOp(region, op)); 174 return StatefulBaseRenderer::clipRegion(region, op); 175} 176 177status_t DisplayListRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t flags) { 178 LOG_ALWAYS_FATAL_IF(!renderNode, "missing rendernode"); 179 180 // dirty is an out parameter and should not be recorded, 181 // it matters only when replaying the display list 182 DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp(renderNode, flags, *currentTransform()); 183 addRenderNodeOp(op); 184 185 return DrawGlInfo::kStatusDone; 186} 187 188status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) { 189 layer = refLayer(layer); 190 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y)); 191 return DrawGlInfo::kStatusDone; 192} 193 194status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) { 195 bitmap = refBitmap(bitmap); 196 paint = refPaint(paint); 197 198 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, paint)); 199 return DrawGlInfo::kStatusDone; 200} 201 202status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 203 float srcRight, float srcBottom, float dstLeft, float dstTop, 204 float dstRight, float dstBottom, const SkPaint* paint) { 205 if (srcLeft == 0 && srcTop == 0 206 && srcRight == bitmap->width() && srcBottom == bitmap->height() 207 && (srcBottom - srcTop == dstBottom - dstTop) 208 && (srcRight - srcLeft == dstRight - dstLeft)) { 209 // transform simple rect to rect drawing case into position bitmap ops, since they merge 210 save(SkCanvas::kMatrix_SaveFlag); 211 translate(dstLeft, dstTop); 212 drawBitmap(bitmap, paint); 213 restore(); 214 } else { 215 bitmap = refBitmap(bitmap); 216 paint = refPaint(paint); 217 218 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 219 srcLeft, srcTop, srcRight, srcBottom, 220 dstLeft, dstTop, dstRight, dstBottom, paint)); 221 } 222 return DrawGlInfo::kStatusDone; 223} 224 225status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) { 226 bitmap = refBitmapData(bitmap); 227 paint = refPaint(paint); 228 229 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, paint)); 230 return DrawGlInfo::kStatusDone; 231} 232 233status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 234 const float* vertices, const int* colors, const SkPaint* paint) { 235 int vertexCount = (meshWidth + 1) * (meshHeight + 1); 236 bitmap = refBitmap(bitmap); 237 vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex 238 paint = refPaint(paint); 239 colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex 240 241 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 242 vertices, colors, paint)); 243 return DrawGlInfo::kStatusDone; 244} 245 246status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 247 float left, float top, float right, float bottom, const SkPaint* paint) { 248 bitmap = refBitmap(bitmap); 249 patch = refPatch(patch); 250 paint = refPaint(paint); 251 252 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint)); 253 return DrawGlInfo::kStatusDone; 254} 255 256status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 257 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 258 return DrawGlInfo::kStatusDone; 259} 260 261status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 262 const SkPaint* paint) { 263 paint = refPaint(paint); 264 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 265 return DrawGlInfo::kStatusDone; 266} 267 268status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 269 float rx, float ry, const SkPaint* paint) { 270 paint = refPaint(paint); 271 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 272 return DrawGlInfo::kStatusDone; 273} 274 275status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) { 276 paint = refPaint(paint); 277 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 278 return DrawGlInfo::kStatusDone; 279} 280 281status_t DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 282 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) { 283 mDisplayListData->refProperty(x); 284 mDisplayListData->refProperty(y); 285 mDisplayListData->refProperty(radius); 286 mDisplayListData->refProperty(paint); 287 addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value, 288 &radius->value, &paint->value)); 289 return DrawGlInfo::kStatusDone; 290} 291 292status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 293 const SkPaint* paint) { 294 paint = refPaint(paint); 295 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 296 return DrawGlInfo::kStatusDone; 297} 298 299status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 300 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { 301 if (fabs(sweepAngle) >= 360.0f) { 302 return drawOval(left, top, right, bottom, paint); 303 } 304 305 paint = refPaint(paint); 306 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 307 startAngle, sweepAngle, useCenter, paint)); 308 return DrawGlInfo::kStatusDone; 309} 310 311status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) { 312 path = refPath(path); 313 paint = refPaint(paint); 314 315 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 316 return DrawGlInfo::kStatusDone; 317} 318 319status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) { 320 points = refBuffer<float>(points, count); 321 paint = refPaint(paint); 322 323 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 324 return DrawGlInfo::kStatusDone; 325} 326 327status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { 328 points = refBuffer<float>(points, count); 329 paint = refPaint(paint); 330 331 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 332 return DrawGlInfo::kStatusDone; 333} 334 335status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 336 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { 337 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 338 339 text = refText(text, bytesCount); 340 path = refPath(path); 341 paint = refPaint(paint); 342 343 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 344 hOffset, vOffset, paint); 345 addDrawOp(op); 346 return DrawGlInfo::kStatusDone; 347} 348 349status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 350 const float* positions, const SkPaint* paint) { 351 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 352 353 text = refText(text, bytesCount); 354 positions = refBuffer<float>(positions, count * 2); 355 paint = refPaint(paint); 356 357 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 358 addDrawOp(op); 359 return DrawGlInfo::kStatusDone; 360} 361 362static void simplifyPaint(int color, SkPaint* paint) { 363 paint->setColor(color); 364 paint->setShader(NULL); 365 paint->setColorFilter(NULL); 366 paint->setLooper(NULL); 367 paint->setStrokeWidth(4 + 0.04 * paint->getTextSize()); 368 paint->setStrokeJoin(SkPaint::kRound_Join); 369 paint->setLooper(NULL); 370} 371 372status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 373 float x, float y, const float* positions, const SkPaint* paint, 374 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 375 376 if (!text || count <= 0 || paintWillNotDrawText(*paint)) return DrawGlInfo::kStatusDone; 377 378 text = refText(text, bytesCount); 379 positions = refBuffer<float>(positions, count * 2); 380 381 if (CC_UNLIKELY(mHighContrastText)) { 382 // high contrast draw path 383 int color = paint->getColor(); 384 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color); 385 bool darken = channelSum < (128 * 3); 386 387 // outline 388 SkPaint* outlinePaint = copyPaint(paint); 389 simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, outlinePaint); 390 outlinePaint->setStyle(SkPaint::kStrokeAndFill_Style); 391 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 392 x, y, positions, outlinePaint, totalAdvance, bounds)); // bounds? 393 394 // inner 395 SkPaint* innerPaint = copyPaint(paint); 396 simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, innerPaint); 397 innerPaint->setStyle(SkPaint::kFill_Style); 398 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 399 x, y, positions, innerPaint, totalAdvance, bounds)); 400 } else { 401 // standard draw path 402 paint = refPaint(paint); 403 404 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 405 x, y, positions, paint, totalAdvance, bounds); 406 addDrawOp(op); 407 } 408 return DrawGlInfo::kStatusDone; 409} 410 411status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { 412 if (count <= 0) return DrawGlInfo::kStatusDone; 413 414 rects = refBuffer<float>(rects, count); 415 paint = refPaint(paint); 416 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 417 return DrawGlInfo::kStatusDone; 418} 419 420void DisplayListRenderer::resetPaintFilter() { 421 addStateOp(new (alloc()) ResetPaintFilterOp()); 422} 423 424void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 425 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 426} 427 428void DisplayListRenderer::insertReorderBarrier(bool enableReorder) { 429 flushRestoreToCount(); 430 flushTranslate(); 431 mDeferredBarrierType = enableReorder ? kBarrier_OutOfOrder : kBarrier_InOrder; 432} 433 434void DisplayListRenderer::flushRestoreToCount() { 435 if (mRestoreSaveCount >= 0) { 436 addOpAndUpdateChunk(new (alloc()) RestoreToCountOp(mRestoreSaveCount)); 437 mRestoreSaveCount = -1; 438 } 439} 440 441void DisplayListRenderer::flushTranslate() { 442 if (mHasDeferredTranslate) { 443 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 444 addOpAndUpdateChunk(new (alloc()) TranslateOp(mTranslateX, mTranslateY)); 445 mTranslateX = mTranslateY = 0.0f; 446 } 447 mHasDeferredTranslate = false; 448 } 449} 450 451size_t DisplayListRenderer::addOpAndUpdateChunk(DisplayListOp* op) { 452 int insertIndex = mDisplayListData->displayListOps.add(op); 453 if (mDeferredBarrierType != kBarrier_None) { 454 // op is first in new chunk 455 mDisplayListData->chunks.push(); 456 DisplayListData::Chunk& newChunk = mDisplayListData->chunks.editTop(); 457 newChunk.beginOpIndex = insertIndex; 458 newChunk.endOpIndex = insertIndex + 1; 459 newChunk.reorderChildren = (mDeferredBarrierType == kBarrier_OutOfOrder); 460 461 int nextChildIndex = mDisplayListData->children().size(); 462 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex; 463 mDeferredBarrierType = kBarrier_None; 464 } else { 465 // standard case - append to existing chunk 466 mDisplayListData->chunks.editTop().endOpIndex = insertIndex + 1; 467 } 468 return insertIndex; 469} 470 471size_t DisplayListRenderer::flushAndAddOp(DisplayListOp* op) { 472 flushRestoreToCount(); 473 flushTranslate(); 474 return addOpAndUpdateChunk(op); 475} 476 477size_t DisplayListRenderer::addStateOp(StateOp* op) { 478 return flushAndAddOp(op); 479} 480 481size_t DisplayListRenderer::addDrawOp(DrawOp* op) { 482 Rect localBounds; 483 if (op->getLocalBounds(localBounds)) { 484 bool rejected = quickRejectConservative(localBounds.left, localBounds.top, 485 localBounds.right, localBounds.bottom); 486 op->setQuickRejected(rejected); 487 } 488 489 mDisplayListData->hasDrawOps = true; 490 return flushAndAddOp(op); 491} 492 493size_t DisplayListRenderer::addRenderNodeOp(DrawRenderNodeOp* op) { 494 int opIndex = addDrawOp(op); 495 int childIndex = mDisplayListData->addChild(op); 496 497 // update the chunk's child indices 498 DisplayListData::Chunk& chunk = mDisplayListData->chunks.editTop(); 499 chunk.endChildIndex = childIndex + 1; 500 501 if (op->renderNode()->stagingProperties().isProjectionReceiver()) { 502 // use staging property, since recording on UI thread 503 mDisplayListData->projectionReceiveIndex = opIndex; 504 } 505 return opIndex; 506} 507 508}; // namespace uirenderer 509}; // namespace android 510