DisplayListRenderer.cpp revision 41541825bc90dac740e424cdd41a8c997e15cdb7
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 < mShaders.size(); i++) { 61 mCaches.resourceCache.decrementRefcountLocked(mShaders.itemAt(i)); 62 } 63 64 for (size_t i = 0; i < mSourcePaths.size(); i++) { 65 mCaches.resourceCache.decrementRefcountLocked(mSourcePaths.itemAt(i)); 66 } 67 68 for (size_t i = 0; i < mLayers.size(); i++) { 69 mCaches.resourceCache.decrementRefcountLocked(mLayers.itemAt(i)); 70 } 71 72 mCaches.resourceCache.unlock(); 73 74 mBitmapResources.clear(); 75 mOwnedBitmapResources.clear(); 76 mFilterResources.clear(); 77 mSourcePaths.clear(); 78 79 mShaders.clear(); 80 mShaderMap.clear(); 81 82 mPaints.clear(); 83 mPaintMap.clear(); 84 85 mRegions.clear(); 86 mRegionMap.clear(); 87 88 mPaths.clear(); 89 mPathMap.clear(); 90 91 mMatrices.clear(); 92 93 mLayers.clear(); 94 95 mHasDrawOps = false; 96 mFunctorCount = 0; 97} 98 99/////////////////////////////////////////////////////////////////////////////// 100// Operations 101/////////////////////////////////////////////////////////////////////////////// 102 103DisplayList* DisplayListRenderer::getDisplayList(DisplayList* displayList) { 104 if (!displayList) { 105 displayList = new DisplayList(*this); 106 } else { 107 displayList->initFromDisplayListRenderer(*this, true); 108 } 109 displayList->setRenderable(mHasDrawOps); 110 return displayList; 111} 112 113bool DisplayListRenderer::isDeferred() { 114 return true; 115} 116 117void DisplayListRenderer::setViewport(int width, int height) { 118 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 119 120 mWidth = width; 121 mHeight = height; 122} 123 124status_t DisplayListRenderer::prepareDirty(float left, float top, 125 float right, float bottom, bool opaque) { 126 mSnapshot = new Snapshot(mFirstSnapshot, 127 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 128 mSaveCount = 1; 129 130 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); 131 mDirtyClip = opaque; 132 133 mRestoreSaveCount = -1; 134 135 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 136} 137 138void DisplayListRenderer::finish() { 139 insertRestoreToCount(); 140 insertTranslate(); 141} 142 143void DisplayListRenderer::interrupt() { 144} 145 146void DisplayListRenderer::resume() { 147} 148 149status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 150 // Ignore dirty during recording, it matters only when we replay 151 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 152 mFunctorCount++; 153 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 154} 155 156int DisplayListRenderer::save(int flags) { 157 addStateOp(new (alloc()) SaveOp(flags)); 158 return OpenGLRenderer::save(flags); 159} 160 161void DisplayListRenderer::restore() { 162 if (mRestoreSaveCount < 0) { 163 restoreToCount(getSaveCount() - 1); 164 return; 165 } 166 167 mRestoreSaveCount--; 168 insertTranslate(); 169 OpenGLRenderer::restore(); 170} 171 172void DisplayListRenderer::restoreToCount(int saveCount) { 173 mRestoreSaveCount = saveCount; 174 insertTranslate(); 175 OpenGLRenderer::restoreToCount(saveCount); 176} 177 178int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 179 int alpha, SkXfermode::Mode mode, int flags) { 180 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, alpha, mode, flags)); 181 return OpenGLRenderer::save(flags); 182} 183 184void DisplayListRenderer::translate(float dx, float dy) { 185 mHasTranslate = true; 186 mTranslateX += dx; 187 mTranslateY += dy; 188 insertRestoreToCount(); 189 OpenGLRenderer::translate(dx, dy); 190} 191 192void DisplayListRenderer::rotate(float degrees) { 193 addStateOp(new (alloc()) RotateOp(degrees)); 194 OpenGLRenderer::rotate(degrees); 195} 196 197void DisplayListRenderer::scale(float sx, float sy) { 198 addStateOp(new (alloc()) ScaleOp(sx, sy)); 199 OpenGLRenderer::scale(sx, sy); 200} 201 202void DisplayListRenderer::skew(float sx, float sy) { 203 addStateOp(new (alloc()) SkewOp(sx, sy)); 204 OpenGLRenderer::skew(sx, sy); 205} 206 207void DisplayListRenderer::setMatrix(SkMatrix* matrix) { 208 matrix = refMatrix(matrix); 209 addStateOp(new (alloc()) SetMatrixOp(matrix)); 210 OpenGLRenderer::setMatrix(matrix); 211} 212 213void DisplayListRenderer::concatMatrix(SkMatrix* matrix) { 214 matrix = refMatrix(matrix); 215 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 216 OpenGLRenderer::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 OpenGLRenderer::clipRect(left, top, right, bottom, op); 223} 224 225bool DisplayListRenderer::clipPath(SkPath* path, SkRegion::Op op) { 226 path = refPath(path); 227 addStateOp(new (alloc()) ClipPathOp(path, op)); 228 return OpenGLRenderer::clipPath(path, op); 229} 230 231bool DisplayListRenderer::clipRegion(SkRegion* region, SkRegion::Op op) { 232 region = refRegion(region); 233 addStateOp(new (alloc()) ClipRegionOp(region, op)); 234 return OpenGLRenderer::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 addDrawOp(new (alloc()) DrawDisplayListOp(displayList, flags)); 247 return DrawGlInfo::kStatusDone; 248} 249 250status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) { 251 layer = refLayer(layer); 252 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y)); 253 return DrawGlInfo::kStatusDone; 254} 255 256status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { 257 bitmap = refBitmap(bitmap); 258 paint = refPaint(paint); 259 260 const AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap); 261 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint, entry)); 262 return DrawGlInfo::kStatusDone; 263} 264 265status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { 266 bitmap = refBitmap(bitmap); 267 matrix = refMatrix(matrix); 268 paint = refPaint(paint); 269 270 addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint)); 271 return DrawGlInfo::kStatusDone; 272} 273 274status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 275 float srcRight, float srcBottom, float dstLeft, float dstTop, 276 float dstRight, float dstBottom, SkPaint* paint) { 277 bitmap = refBitmap(bitmap); 278 paint = refPaint(paint); 279 280 if (srcLeft == 0 && srcTop == 0 && 281 srcRight == bitmap->width() && srcBottom == bitmap->height() && 282 (srcBottom - srcTop == dstBottom - dstTop) && 283 (srcRight - srcLeft == dstRight - dstLeft)) { 284 // transform simple rect to rect drawing case into position bitmap ops, since they merge 285 const AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap); 286 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint, entry)); 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 int alpha; 321 SkXfermode::Mode mode; 322 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); 323 324 bitmap = refBitmap(bitmap); 325 326 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, alpha, mode)); 327 return DrawGlInfo::kStatusDone; 328} 329 330status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 331 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 332 return DrawGlInfo::kStatusDone; 333} 334 335status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 336 SkPaint* paint) { 337 paint = refPaint(paint); 338 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 339 return DrawGlInfo::kStatusDone; 340} 341 342status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 343 float rx, float ry, SkPaint* paint) { 344 paint = refPaint(paint); 345 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 346 return DrawGlInfo::kStatusDone; 347} 348 349status_t DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 350 paint = refPaint(paint); 351 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 352 return DrawGlInfo::kStatusDone; 353} 354 355status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 356 SkPaint* paint) { 357 paint = refPaint(paint); 358 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 359 return DrawGlInfo::kStatusDone; 360} 361 362status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 363 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 364 paint = refPaint(paint); 365 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 366 startAngle, sweepAngle, useCenter, paint)); 367 return DrawGlInfo::kStatusDone; 368} 369 370status_t DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) { 371 path = refPath(path); 372 paint = refPaint(paint); 373 374 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 375 return DrawGlInfo::kStatusDone; 376} 377 378status_t DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) { 379 points = refBuffer<float>(points, count); 380 paint = refPaint(paint); 381 382 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 383 return DrawGlInfo::kStatusDone; 384} 385 386status_t DisplayListRenderer::drawPoints(float* points, int count, SkPaint* paint) { 387 points = refBuffer<float>(points, count); 388 paint = refPaint(paint); 389 390 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 391 return DrawGlInfo::kStatusDone; 392} 393 394status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 395 SkPath* path, float hOffset, float vOffset, SkPaint* paint) { 396 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 397 398 text = refText(text, bytesCount); 399 path = refPath(path); 400 paint = refPaint(paint); 401 402 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 403 hOffset, vOffset, paint); 404 addDrawOp(op); 405 return DrawGlInfo::kStatusDone; 406} 407 408status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 409 const float* positions, SkPaint* paint) { 410 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 411 412 text = refText(text, bytesCount); 413 positions = refBuffer<float>(positions, count * 2); 414 paint = refPaint(paint); 415 416 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 417 addDrawOp(op); 418 return DrawGlInfo::kStatusDone; 419} 420 421status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 422 float x, float y, const float* positions, SkPaint* paint, 423 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 424 425 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 426 427 text = refText(text, bytesCount); 428 positions = refBuffer<float>(positions, count * 2); 429 paint = refPaint(paint); 430 431 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 432 x, y, positions, paint, totalAdvance, bounds); 433 addDrawOp(op); 434 return DrawGlInfo::kStatusDone; 435} 436 437status_t DisplayListRenderer::drawRects(const float* rects, int count, SkPaint* paint) { 438 if (count <= 0) return DrawGlInfo::kStatusDone; 439 440 rects = refBuffer<float>(rects, count); 441 paint = refPaint(paint); 442 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 443 return DrawGlInfo::kStatusDone; 444} 445 446void DisplayListRenderer::resetShader() { 447 addStateOp(new (alloc()) ResetShaderOp()); 448} 449 450void DisplayListRenderer::setupShader(SkiaShader* shader) { 451 shader = refShader(shader); 452 addStateOp(new (alloc()) SetupShaderOp(shader)); 453} 454 455void DisplayListRenderer::resetColorFilter() { 456 addStateOp(new (alloc()) ResetColorFilterOp()); 457} 458 459void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) { 460 filter = refColorFilter(filter); 461 addStateOp(new (alloc()) SetupColorFilterOp(filter)); 462} 463 464void DisplayListRenderer::resetShadow() { 465 addStateOp(new (alloc()) ResetShadowOp()); 466} 467 468void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) { 469 addStateOp(new (alloc()) SetupShadowOp(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(localBounds)) { 506 bool rejected = quickRejectNoScissor(localBounds.left, localBounds.top, 507 localBounds.right, localBounds.bottom); 508 op->setQuickRejected(rejected); 509 } 510 mHasDrawOps = true; 511 addOpInternal(op); 512} 513 514}; // namespace uirenderer 515}; // namespace android 516