DisplayListRenderer.cpp revision d44fbe55a9f434cb5bb0e34c143ba1445141990d
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 "DisplayList.h" 25#include "DeferredDisplayList.h" 26#include "DisplayListLogBuffer.h" 27#include "DisplayListOp.h" 28#include "DisplayListRenderer.h" 29#include "Caches.h" 30 31namespace android { 32namespace uirenderer { 33 34DisplayListRenderer::DisplayListRenderer(): 35 mCaches(Caches::getInstance()), mDisplayListData(new DisplayListData), 36 mTranslateX(0.0f), mTranslateY(0.0f), mHasTranslate(false), 37 mHasDrawOps(false), mFunctorCount(0) { 38} 39 40DisplayListRenderer::~DisplayListRenderer() { 41 reset(); 42} 43 44void DisplayListRenderer::reset() { 45 mDisplayListData = new DisplayListData(); 46 mCaches.resourceCache.lock(); 47 48 for (size_t i = 0; i < mBitmapResources.size(); i++) { 49 mCaches.resourceCache.decrementRefcountLocked(mBitmapResources.itemAt(i)); 50 } 51 52 for (size_t i = 0; i < mOwnedBitmapResources.size(); i++) { 53 mCaches.resourceCache.decrementRefcountLocked(mOwnedBitmapResources.itemAt(i)); 54 } 55 56 for (size_t i = 0; i < mFilterResources.size(); i++) { 57 mCaches.resourceCache.decrementRefcountLocked(mFilterResources.itemAt(i)); 58 } 59 60 for (size_t i = 0; i < mPatchResources.size(); i++) { 61 mCaches.resourceCache.decrementRefcountLocked(mPatchResources.itemAt(i)); 62 } 63 64 for (size_t i = 0; i < mShaders.size(); i++) { 65 mCaches.resourceCache.decrementRefcountLocked(mShaders.itemAt(i)); 66 } 67 68 for (size_t i = 0; i < mSourcePaths.size(); i++) { 69 mCaches.resourceCache.decrementRefcountLocked(mSourcePaths.itemAt(i)); 70 } 71 72 for (size_t i = 0; i < mLayers.size(); i++) { 73 mCaches.resourceCache.decrementRefcountLocked(mLayers.itemAt(i)); 74 } 75 76 mCaches.resourceCache.unlock(); 77 78 mBitmapResources.clear(); 79 mOwnedBitmapResources.clear(); 80 mFilterResources.clear(); 81 mPatchResources.clear(); 82 mSourcePaths.clear(); 83 84 mShaders.clear(); 85 mShaderMap.clear(); 86 87 mPaints.clear(); 88 mPaintMap.clear(); 89 90 mRegions.clear(); 91 mRegionMap.clear(); 92 93 mPaths.clear(); 94 mPathMap.clear(); 95 96 mMatrices.clear(); 97 98 mLayers.clear(); 99 100 mHasDrawOps = false; 101 mFunctorCount = 0; 102} 103 104/////////////////////////////////////////////////////////////////////////////// 105// Operations 106/////////////////////////////////////////////////////////////////////////////// 107 108DisplayList* DisplayListRenderer::getDisplayList(DisplayList* displayList) { 109 if (!displayList) { 110 displayList = new DisplayList(*this); 111 } else { 112 displayList->initFromDisplayListRenderer(*this, true); 113 } 114 // TODO: should just avoid setting the DisplayList's DisplayListData 115 displayList->setRenderable(mHasDrawOps); 116 return displayList; 117} 118 119void DisplayListRenderer::setViewport(int width, int height) { 120 // TODO: DisplayListRenderer shouldn't have a projection matrix, as it should never be used 121 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1); 122 123 initializeViewport(width, height); 124} 125 126status_t DisplayListRenderer::prepareDirty(float left, float top, 127 float right, float bottom, bool opaque) { 128 initializeSaveStack(0, 0, getWidth(), getHeight()); 129 130 mDirtyClip = opaque; 131 mRestoreSaveCount = -1; 132 133 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 134} 135 136void DisplayListRenderer::finish() { 137 insertRestoreToCount(); 138 insertTranslate(); 139} 140 141void DisplayListRenderer::interrupt() { 142} 143 144void DisplayListRenderer::resume() { 145} 146 147status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 148 // Ignore dirty during recording, it matters only when we replay 149 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 150 mFunctorCount++; 151 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 152} 153 154int DisplayListRenderer::save(int flags) { 155 addStateOp(new (alloc()) SaveOp(flags)); 156 return StatefulBaseRenderer::save(flags); 157} 158 159void DisplayListRenderer::restore() { 160 if (mRestoreSaveCount < 0) { 161 restoreToCount(getSaveCount() - 1); 162 return; 163 } 164 165 mRestoreSaveCount--; 166 insertTranslate(); 167 StatefulBaseRenderer::restore(); 168} 169 170void DisplayListRenderer::restoreToCount(int saveCount) { 171 mRestoreSaveCount = saveCount; 172 insertTranslate(); 173 StatefulBaseRenderer::restoreToCount(saveCount); 174} 175 176void DisplayListRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) { 177 bool restoreProjection = removed.flags & Snapshot::kFlagProjectionTarget; 178 if (restoreProjection) { 179 mDisplayListData->projectionIndex = mDisplayListData->displayListOps.size() - 1; 180 mDisplayListData->projectionTransform.load(*currentTransform()); 181 } 182} 183 184int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 185 const SkPaint* paint, int flags) { 186 paint = refPaint(paint); 187 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags)); 188 return StatefulBaseRenderer::save(flags); 189} 190 191void DisplayListRenderer::translate(float dx, float dy, float dz) { 192 // ignore dz, not used at defer time 193 mHasTranslate = true; 194 mTranslateX += dx; 195 mTranslateY += dy; 196 insertRestoreToCount(); 197 StatefulBaseRenderer::translate(dx, dy, dz); 198} 199 200void DisplayListRenderer::rotate(float degrees) { 201 addStateOp(new (alloc()) RotateOp(degrees)); 202 StatefulBaseRenderer::rotate(degrees); 203} 204 205void DisplayListRenderer::scale(float sx, float sy) { 206 addStateOp(new (alloc()) ScaleOp(sx, sy)); 207 StatefulBaseRenderer::scale(sx, sy); 208} 209 210void DisplayListRenderer::skew(float sx, float sy) { 211 addStateOp(new (alloc()) SkewOp(sx, sy)); 212 StatefulBaseRenderer::skew(sx, sy); 213} 214 215void DisplayListRenderer::setMatrix(const SkMatrix* matrix) { 216 matrix = refMatrix(matrix); 217 addStateOp(new (alloc()) SetMatrixOp(matrix)); 218 StatefulBaseRenderer::setMatrix(matrix); 219} 220 221void DisplayListRenderer::concatMatrix(const SkMatrix* matrix) { 222 matrix = refMatrix(matrix); 223 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 224 StatefulBaseRenderer::concatMatrix(matrix); 225} 226 227bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom, 228 SkRegion::Op op) { 229 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op)); 230 return StatefulBaseRenderer::clipRect(left, top, right, bottom, op); 231} 232 233bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) { 234 path = refPath(path); 235 addStateOp(new (alloc()) ClipPathOp(path, op)); 236 return StatefulBaseRenderer::clipPath(path, op); 237} 238 239bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { 240 region = refRegion(region); 241 addStateOp(new (alloc()) ClipRegionOp(region, op)); 242 return StatefulBaseRenderer::clipRegion(region, op); 243} 244 245status_t DisplayListRenderer::drawDisplayList(DisplayList* displayList, 246 Rect& dirty, int32_t flags) { 247 // dirty is an out parameter and should not be recorded, 248 // it matters only when replaying the display list 249 250 // TODO: To be safe, the display list should be ref-counted in the 251 // resources cache, but we rely on the caller (UI toolkit) to 252 // do the right thing for now 253 254 DrawDisplayListOp* op = new (alloc()) DrawDisplayListOp(displayList, 255 flags, *currentTransform()); 256 addDrawOp(op); 257 mDisplayListData->children.push(op); 258 259 return DrawGlInfo::kStatusDone; 260} 261 262status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) { 263 layer = refLayer(layer); 264 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y)); 265 return DrawGlInfo::kStatusDone; 266} 267 268status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top, 269 const SkPaint* paint) { 270 bitmap = refBitmap(bitmap); 271 paint = refPaint(paint); 272 273 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint)); 274 return DrawGlInfo::kStatusDone; 275} 276 277status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix, 278 const SkPaint* paint) { 279 bitmap = refBitmap(bitmap); 280 matrix = refMatrix(matrix); 281 paint = refPaint(paint); 282 283 addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint)); 284 return DrawGlInfo::kStatusDone; 285} 286 287status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 288 float srcRight, float srcBottom, float dstLeft, float dstTop, 289 float dstRight, float dstBottom, const SkPaint* paint) { 290 bitmap = refBitmap(bitmap); 291 paint = refPaint(paint); 292 293 if (srcLeft == 0 && srcTop == 0 && 294 srcRight == bitmap->width() && srcBottom == bitmap->height() && 295 (srcBottom - srcTop == dstBottom - dstTop) && 296 (srcRight - srcLeft == dstRight - dstLeft)) { 297 // transform simple rect to rect drawing case into position bitmap ops, since they merge 298 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint)); 299 return DrawGlInfo::kStatusDone; 300 } 301 302 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 303 srcLeft, srcTop, srcRight, srcBottom, 304 dstLeft, dstTop, dstRight, dstBottom, paint)); 305 return DrawGlInfo::kStatusDone; 306} 307 308status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top, 309 const SkPaint* paint) { 310 bitmap = refBitmapData(bitmap); 311 paint = refPaint(paint); 312 313 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint)); 314 return DrawGlInfo::kStatusDone; 315} 316 317status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 318 const float* vertices, const int* colors, const SkPaint* paint) { 319 int count = (meshWidth + 1) * (meshHeight + 1) * 2; 320 bitmap = refBitmap(bitmap); 321 vertices = refBuffer<float>(vertices, count); 322 paint = refPaint(paint); 323 colors = refBuffer<int>(colors, count); 324 325 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 326 vertices, colors, paint)); 327 return DrawGlInfo::kStatusDone; 328} 329 330status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 331 float left, float top, float right, float bottom, const SkPaint* paint) { 332 bitmap = refBitmap(bitmap); 333 patch = refPatch(patch); 334 paint = refPaint(paint); 335 336 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint)); 337 return DrawGlInfo::kStatusDone; 338} 339 340status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 341 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 342 return DrawGlInfo::kStatusDone; 343} 344 345status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 346 const SkPaint* paint) { 347 paint = refPaint(paint); 348 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 349 return DrawGlInfo::kStatusDone; 350} 351 352status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 353 float rx, float ry, const SkPaint* paint) { 354 paint = refPaint(paint); 355 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 356 return DrawGlInfo::kStatusDone; 357} 358 359status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) { 360 paint = refPaint(paint); 361 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 362 return DrawGlInfo::kStatusDone; 363} 364 365status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 366 const SkPaint* paint) { 367 paint = refPaint(paint); 368 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 369 return DrawGlInfo::kStatusDone; 370} 371 372status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 373 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { 374 paint = refPaint(paint); 375 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 376 startAngle, sweepAngle, useCenter, paint)); 377 return DrawGlInfo::kStatusDone; 378} 379 380status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) { 381 path = refPath(path); 382 paint = refPaint(paint); 383 384 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 385 return DrawGlInfo::kStatusDone; 386} 387 388status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) { 389 points = refBuffer<float>(points, count); 390 paint = refPaint(paint); 391 392 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 393 return DrawGlInfo::kStatusDone; 394} 395 396status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { 397 points = refBuffer<float>(points, count); 398 paint = refPaint(paint); 399 400 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 401 return DrawGlInfo::kStatusDone; 402} 403 404status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 405 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { 406 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 407 408 text = refText(text, bytesCount); 409 path = refPath(path); 410 paint = refPaint(paint); 411 412 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 413 hOffset, vOffset, paint); 414 addDrawOp(op); 415 return DrawGlInfo::kStatusDone; 416} 417 418status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 419 const float* positions, const SkPaint* paint) { 420 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 421 422 text = refText(text, bytesCount); 423 positions = refBuffer<float>(positions, count * 2); 424 paint = refPaint(paint); 425 426 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 427 addDrawOp(op); 428 return DrawGlInfo::kStatusDone; 429} 430 431status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 432 float x, float y, const float* positions, const SkPaint* paint, 433 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 434 435 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 436 437 text = refText(text, bytesCount); 438 positions = refBuffer<float>(positions, count * 2); 439 paint = refPaint(paint); 440 441 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 442 x, y, positions, paint, totalAdvance, bounds); 443 addDrawOp(op); 444 return DrawGlInfo::kStatusDone; 445} 446 447status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { 448 if (count <= 0) return DrawGlInfo::kStatusDone; 449 450 rects = refBuffer<float>(rects, count); 451 paint = refPaint(paint); 452 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 453 return DrawGlInfo::kStatusDone; 454} 455 456void DisplayListRenderer::resetShader() { 457 addStateOp(new (alloc()) ResetShaderOp()); 458} 459 460void DisplayListRenderer::setupShader(SkiaShader* shader) { 461 shader = refShader(shader); 462 addStateOp(new (alloc()) SetupShaderOp(shader)); 463} 464 465void DisplayListRenderer::resetColorFilter() { 466 addStateOp(new (alloc()) ResetColorFilterOp()); 467} 468 469void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) { 470 filter = refColorFilter(filter); 471 addStateOp(new (alloc()) SetupColorFilterOp(filter)); 472} 473 474void DisplayListRenderer::resetShadow() { 475 addStateOp(new (alloc()) ResetShadowOp()); 476 OpenGLRenderer::resetShadow(); 477} 478 479void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) { 480 addStateOp(new (alloc()) SetupShadowOp(radius, dx, dy, color)); 481 OpenGLRenderer::setupShadow(radius, dx, dy, color); 482} 483 484void DisplayListRenderer::resetPaintFilter() { 485 addStateOp(new (alloc()) ResetPaintFilterOp()); 486} 487 488void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 489 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 490} 491 492void DisplayListRenderer::insertRestoreToCount() { 493 if (mRestoreSaveCount >= 0) { 494 DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount); 495 mDisplayListData->displayListOps.add(op); 496 mRestoreSaveCount = -1; 497 } 498} 499 500void DisplayListRenderer::insertTranslate() { 501 if (mHasTranslate) { 502 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 503 DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY); 504 mDisplayListData->displayListOps.add(op); 505 mTranslateX = mTranslateY = 0.0f; 506 } 507 mHasTranslate = false; 508 } 509} 510 511void DisplayListRenderer::addStateOp(StateOp* op) { 512 addOpInternal(op); 513} 514 515void DisplayListRenderer::addDrawOp(DrawOp* op) { 516 Rect localBounds; 517 if (op->getLocalBounds(mDrawModifiers, localBounds)) { 518 bool rejected = quickRejectConservative(localBounds.left, localBounds.top, 519 localBounds.right, localBounds.bottom); 520 op->setQuickRejected(rejected); 521 } 522 523 mHasDrawOps = true; 524 addOpInternal(op); 525} 526 527}; // namespace uirenderer 528}; // namespace android 529