DisplayListRenderer.cpp revision 8852ab4357ffb653bafb36f3b9272866834f7a72
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 176int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 177 const SkPaint* paint, int flags) { 178 paint = refPaint(paint); 179 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags)); 180 return StatefulBaseRenderer::save(flags); 181} 182 183void DisplayListRenderer::translate(float dx, float dy, float dz) { 184 // ignore dz, not used at defer time 185 mHasTranslate = true; 186 mTranslateX += dx; 187 mTranslateY += dy; 188 insertRestoreToCount(); 189 StatefulBaseRenderer::translate(dx, dy, dz); 190} 191 192void DisplayListRenderer::rotate(float degrees) { 193 addStateOp(new (alloc()) RotateOp(degrees)); 194 StatefulBaseRenderer::rotate(degrees); 195} 196 197void DisplayListRenderer::scale(float sx, float sy) { 198 addStateOp(new (alloc()) ScaleOp(sx, sy)); 199 StatefulBaseRenderer::scale(sx, sy); 200} 201 202void DisplayListRenderer::skew(float sx, float sy) { 203 addStateOp(new (alloc()) SkewOp(sx, sy)); 204 StatefulBaseRenderer::skew(sx, sy); 205} 206 207void DisplayListRenderer::setMatrix(const SkMatrix* matrix) { 208 matrix = refMatrix(matrix); 209 addStateOp(new (alloc()) SetMatrixOp(matrix)); 210 StatefulBaseRenderer::setMatrix(matrix); 211} 212 213void DisplayListRenderer::concatMatrix(const SkMatrix* matrix) { 214 matrix = refMatrix(matrix); 215 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 216 StatefulBaseRenderer::concatMatrix(matrix); 217} 218 219bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom, 220 SkRegion::Op op) { 221 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op)); 222 return StatefulBaseRenderer::clipRect(left, top, right, bottom, op); 223} 224 225bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) { 226 path = refPath(path); 227 addStateOp(new (alloc()) ClipPathOp(path, op)); 228 return StatefulBaseRenderer::clipPath(path, op); 229} 230 231bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { 232 region = refRegion(region); 233 addStateOp(new (alloc()) ClipRegionOp(region, op)); 234 return StatefulBaseRenderer::clipRegion(region, op); 235} 236 237status_t DisplayListRenderer::drawDisplayList(DisplayList* displayList, 238 Rect& dirty, int32_t flags) { 239 // dirty is an out parameter and should not be recorded, 240 // it matters only when replaying the display list 241 242 // TODO: To be safe, the display list should be ref-counted in the 243 // resources cache, but we rely on the caller (UI toolkit) to 244 // do the right thing for now 245 246 DrawDisplayListOp* op = new (alloc()) DrawDisplayListOp(displayList, 247 flags, *currentTransform()); 248 addDrawOp(op); 249 mDisplayListData->children.push(op); 250 if (displayList->isProjectionReceiver()) { 251 mDisplayListData->projectionReceiveIndex = mDisplayListData->displayListOps.size() - 1; 252 } 253 254 return DrawGlInfo::kStatusDone; 255} 256 257status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) { 258 layer = refLayer(layer); 259 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y)); 260 return DrawGlInfo::kStatusDone; 261} 262 263status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top, 264 const SkPaint* paint) { 265 bitmap = refBitmap(bitmap); 266 paint = refPaint(paint); 267 268 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint)); 269 return DrawGlInfo::kStatusDone; 270} 271 272status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix, 273 const SkPaint* paint) { 274 bitmap = refBitmap(bitmap); 275 matrix = refMatrix(matrix); 276 paint = refPaint(paint); 277 278 addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint)); 279 return DrawGlInfo::kStatusDone; 280} 281 282status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 283 float srcRight, float srcBottom, float dstLeft, float dstTop, 284 float dstRight, float dstBottom, const SkPaint* paint) { 285 bitmap = refBitmap(bitmap); 286 paint = refPaint(paint); 287 288 if (srcLeft == 0 && srcTop == 0 && 289 srcRight == bitmap->width() && srcBottom == bitmap->height() && 290 (srcBottom - srcTop == dstBottom - dstTop) && 291 (srcRight - srcLeft == dstRight - dstLeft)) { 292 // transform simple rect to rect drawing case into position bitmap ops, since they merge 293 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint)); 294 return DrawGlInfo::kStatusDone; 295 } 296 297 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 298 srcLeft, srcTop, srcRight, srcBottom, 299 dstLeft, dstTop, dstRight, dstBottom, paint)); 300 return DrawGlInfo::kStatusDone; 301} 302 303status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top, 304 const SkPaint* paint) { 305 bitmap = refBitmapData(bitmap); 306 paint = refPaint(paint); 307 308 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint)); 309 return DrawGlInfo::kStatusDone; 310} 311 312status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 313 const float* vertices, const int* colors, const SkPaint* paint) { 314 int count = (meshWidth + 1) * (meshHeight + 1) * 2; 315 bitmap = refBitmap(bitmap); 316 vertices = refBuffer<float>(vertices, count); 317 paint = refPaint(paint); 318 colors = refBuffer<int>(colors, count); 319 320 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 321 vertices, colors, paint)); 322 return DrawGlInfo::kStatusDone; 323} 324 325status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 326 float left, float top, float right, float bottom, const SkPaint* paint) { 327 bitmap = refBitmap(bitmap); 328 patch = refPatch(patch); 329 paint = refPaint(paint); 330 331 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint)); 332 return DrawGlInfo::kStatusDone; 333} 334 335status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 336 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 337 return DrawGlInfo::kStatusDone; 338} 339 340status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 341 const SkPaint* paint) { 342 paint = refPaint(paint); 343 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 344 return DrawGlInfo::kStatusDone; 345} 346 347status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 348 float rx, float ry, const SkPaint* paint) { 349 paint = refPaint(paint); 350 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 351 return DrawGlInfo::kStatusDone; 352} 353 354status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) { 355 paint = refPaint(paint); 356 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 357 return DrawGlInfo::kStatusDone; 358} 359 360status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 361 const SkPaint* paint) { 362 paint = refPaint(paint); 363 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 364 return DrawGlInfo::kStatusDone; 365} 366 367status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 368 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { 369 paint = refPaint(paint); 370 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 371 startAngle, sweepAngle, useCenter, paint)); 372 return DrawGlInfo::kStatusDone; 373} 374 375status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) { 376 path = refPath(path); 377 paint = refPaint(paint); 378 379 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 380 return DrawGlInfo::kStatusDone; 381} 382 383status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) { 384 points = refBuffer<float>(points, count); 385 paint = refPaint(paint); 386 387 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 388 return DrawGlInfo::kStatusDone; 389} 390 391status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { 392 points = refBuffer<float>(points, count); 393 paint = refPaint(paint); 394 395 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 396 return DrawGlInfo::kStatusDone; 397} 398 399status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 400 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { 401 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 402 403 text = refText(text, bytesCount); 404 path = refPath(path); 405 paint = refPaint(paint); 406 407 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 408 hOffset, vOffset, paint); 409 addDrawOp(op); 410 return DrawGlInfo::kStatusDone; 411} 412 413status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 414 const float* positions, const SkPaint* paint) { 415 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 416 417 text = refText(text, bytesCount); 418 positions = refBuffer<float>(positions, count * 2); 419 paint = refPaint(paint); 420 421 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 422 addDrawOp(op); 423 return DrawGlInfo::kStatusDone; 424} 425 426status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 427 float x, float y, const float* positions, const SkPaint* paint, 428 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 429 430 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 431 432 text = refText(text, bytesCount); 433 positions = refBuffer<float>(positions, count * 2); 434 paint = refPaint(paint); 435 436 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 437 x, y, positions, paint, totalAdvance, bounds); 438 addDrawOp(op); 439 return DrawGlInfo::kStatusDone; 440} 441 442status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { 443 if (count <= 0) return DrawGlInfo::kStatusDone; 444 445 rects = refBuffer<float>(rects, count); 446 paint = refPaint(paint); 447 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 448 return DrawGlInfo::kStatusDone; 449} 450 451void DisplayListRenderer::resetShader() { 452 addStateOp(new (alloc()) ResetShaderOp()); 453} 454 455void DisplayListRenderer::setupShader(SkiaShader* shader) { 456 shader = refShader(shader); 457 addStateOp(new (alloc()) SetupShaderOp(shader)); 458} 459 460void DisplayListRenderer::resetColorFilter() { 461 addStateOp(new (alloc()) ResetColorFilterOp()); 462} 463 464void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) { 465 filter = refColorFilter(filter); 466 addStateOp(new (alloc()) SetupColorFilterOp(filter)); 467} 468 469void DisplayListRenderer::resetShadow() { 470 addStateOp(new (alloc()) ResetShadowOp()); 471 OpenGLRenderer::resetShadow(); 472} 473 474void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) { 475 addStateOp(new (alloc()) SetupShadowOp(radius, dx, dy, color)); 476 OpenGLRenderer::setupShadow(radius, dx, dy, color); 477} 478 479void DisplayListRenderer::resetPaintFilter() { 480 addStateOp(new (alloc()) ResetPaintFilterOp()); 481} 482 483void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 484 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 485} 486 487void DisplayListRenderer::insertRestoreToCount() { 488 if (mRestoreSaveCount >= 0) { 489 DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount); 490 mDisplayListData->displayListOps.add(op); 491 mRestoreSaveCount = -1; 492 } 493} 494 495void DisplayListRenderer::insertTranslate() { 496 if (mHasTranslate) { 497 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 498 DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY); 499 mDisplayListData->displayListOps.add(op); 500 mTranslateX = mTranslateY = 0.0f; 501 } 502 mHasTranslate = false; 503 } 504} 505 506void DisplayListRenderer::addStateOp(StateOp* op) { 507 addOpInternal(op); 508} 509 510void DisplayListRenderer::addDrawOp(DrawOp* op) { 511 Rect localBounds; 512 if (op->getLocalBounds(mDrawModifiers, localBounds)) { 513 bool rejected = quickRejectConservative(localBounds.left, localBounds.top, 514 localBounds.right, localBounds.bottom); 515 op->setQuickRejected(rejected); 516 } 517 518 mHasDrawOps = true; 519 addOpInternal(op); 520} 521 522}; // namespace uirenderer 523}; // namespace android 524