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