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