DisplayListRenderer.cpp revision ff78583d8a73ca35ce65b5d2592570ff6fb9901b
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 21#include <private/hwui/DrawGlInfo.h> 22 23#include "DisplayList.h" 24#include "DeferredDisplayList.h" 25#include "DisplayListLogBuffer.h" 26#include "DisplayListOp.h" 27#include "DisplayListRenderer.h" 28#include "Caches.h" 29 30namespace android { 31namespace uirenderer { 32 33DisplayListRenderer::DisplayListRenderer(): 34 mCaches(Caches::getInstance()), mDisplayListData(new DisplayListData), 35 mTranslateX(0.0f), mTranslateY(0.0f), mHasTranslate(false), 36 mHasDrawOps(false), mFunctorCount(0) { 37} 38 39DisplayListRenderer::~DisplayListRenderer() { 40 reset(); 41} 42 43void DisplayListRenderer::reset() { 44 mDisplayListData = new DisplayListData(); 45 mCaches.resourceCache.lock(); 46 47 for (size_t i = 0; i < mBitmapResources.size(); i++) { 48 mCaches.resourceCache.decrementRefcountLocked(mBitmapResources.itemAt(i)); 49 } 50 51 for (size_t i = 0; i < mOwnedBitmapResources.size(); i++) { 52 mCaches.resourceCache.decrementRefcountLocked(mOwnedBitmapResources.itemAt(i)); 53 } 54 55 for (size_t i = 0; i < mFilterResources.size(); i++) { 56 mCaches.resourceCache.decrementRefcountLocked(mFilterResources.itemAt(i)); 57 } 58 59 for (size_t i = 0; i < mShaders.size(); i++) { 60 mCaches.resourceCache.decrementRefcountLocked(mShaders.itemAt(i)); 61 } 62 63 for (size_t i = 0; i < mSourcePaths.size(); i++) { 64 mCaches.resourceCache.decrementRefcountLocked(mSourcePaths.itemAt(i)); 65 } 66 67 for (size_t i = 0; i < mLayers.size(); i++) { 68 mCaches.resourceCache.decrementRefcountLocked(mLayers.itemAt(i)); 69 } 70 71 mCaches.resourceCache.unlock(); 72 73 mBitmapResources.clear(); 74 mOwnedBitmapResources.clear(); 75 mFilterResources.clear(); 76 mSourcePaths.clear(); 77 78 mShaders.clear(); 79 mShaderMap.clear(); 80 81 mPaints.clear(); 82 mPaintMap.clear(); 83 84 mRegions.clear(); 85 mRegionMap.clear(); 86 87 mPaths.clear(); 88 mPathMap.clear(); 89 90 mMatrices.clear(); 91 92 mLayers.clear(); 93 94 mHasDrawOps = false; 95 mFunctorCount = 0; 96} 97 98/////////////////////////////////////////////////////////////////////////////// 99// Operations 100/////////////////////////////////////////////////////////////////////////////// 101 102DisplayList* DisplayListRenderer::getDisplayList(DisplayList* displayList) { 103 if (!displayList) { 104 displayList = new DisplayList(*this); 105 } else { 106 displayList->initFromDisplayListRenderer(*this, true); 107 } 108 displayList->setRenderable(mHasDrawOps); 109 return displayList; 110} 111 112bool DisplayListRenderer::isDeferred() { 113 return true; 114} 115 116void DisplayListRenderer::setViewport(int width, int height) { 117 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); 118 119 mWidth = width; 120 mHeight = height; 121} 122 123status_t DisplayListRenderer::prepareDirty(float left, float top, 124 float right, float bottom, bool opaque) { 125 mSnapshot = new Snapshot(mFirstSnapshot, 126 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); 127 mSaveCount = 1; 128 129 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); 130 mDirtyClip = opaque; 131 132 mRestoreSaveCount = -1; 133 134 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 135} 136 137void DisplayListRenderer::finish() { 138 insertRestoreToCount(); 139 insertTranslate(); 140} 141 142void DisplayListRenderer::interrupt() { 143} 144 145void DisplayListRenderer::resume() { 146} 147 148status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 149 // Ignore dirty during recording, it matters only when we replay 150 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 151 mFunctorCount++; 152 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 153} 154 155int DisplayListRenderer::save(int flags) { 156 addStateOp(new (alloc()) SaveOp(flags)); 157 return OpenGLRenderer::save(flags); 158} 159 160void DisplayListRenderer::restore() { 161 if (mRestoreSaveCount < 0) { 162 restoreToCount(getSaveCount() - 1); 163 return; 164 } 165 166 mRestoreSaveCount--; 167 insertTranslate(); 168 OpenGLRenderer::restore(); 169} 170 171void DisplayListRenderer::restoreToCount(int saveCount) { 172 mRestoreSaveCount = saveCount; 173 insertTranslate(); 174 OpenGLRenderer::restoreToCount(saveCount); 175} 176 177int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 178 int alpha, SkXfermode::Mode mode, int flags) { 179 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, alpha, mode, flags)); 180 return OpenGLRenderer::save(flags); 181} 182 183void DisplayListRenderer::translate(float dx, float dy) { 184 mHasTranslate = true; 185 mTranslateX += dx; 186 mTranslateY += dy; 187 insertRestoreToCount(); 188 OpenGLRenderer::translate(dx, dy); 189} 190 191void DisplayListRenderer::rotate(float degrees) { 192 addStateOp(new (alloc()) RotateOp(degrees)); 193 OpenGLRenderer::rotate(degrees); 194} 195 196void DisplayListRenderer::scale(float sx, float sy) { 197 addStateOp(new (alloc()) ScaleOp(sx, sy)); 198 OpenGLRenderer::scale(sx, sy); 199} 200 201void DisplayListRenderer::skew(float sx, float sy) { 202 addStateOp(new (alloc()) SkewOp(sx, sy)); 203 OpenGLRenderer::skew(sx, sy); 204} 205 206void DisplayListRenderer::setMatrix(SkMatrix* matrix) { 207 matrix = refMatrix(matrix); 208 addStateOp(new (alloc()) SetMatrixOp(matrix)); 209 OpenGLRenderer::setMatrix(matrix); 210} 211 212void DisplayListRenderer::concatMatrix(SkMatrix* matrix) { 213 matrix = refMatrix(matrix); 214 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 215 OpenGLRenderer::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 addDrawOp(new (alloc()) DrawDisplayListOp(displayList, flags)); 246 return DrawGlInfo::kStatusDone; 247} 248 249status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { 250 mLayers.add(layer); 251 mCaches.resourceCache.incrementRefcount(layer); 252 paint = refPaint(paint); 253 254 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y, paint)); 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 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 282 srcLeft, srcTop, srcRight, srcBottom, 283 dstLeft, dstTop, dstRight, dstBottom, paint)); 284 return DrawGlInfo::kStatusDone; 285} 286 287status_t DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, 288 SkPaint* paint) { 289 bitmap = refBitmapData(bitmap); 290 paint = refPaint(paint); 291 292 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint)); 293 return DrawGlInfo::kStatusDone; 294} 295 296status_t DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, 297 float* vertices, int* colors, SkPaint* paint) { 298 int count = (meshWidth + 1) * (meshHeight + 1) * 2; 299 bitmap = refBitmap(bitmap); 300 vertices = refBuffer<float>(vertices, count); 301 paint = refPaint(paint); 302 colors = refBuffer<int>(colors, count); 303 304 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 305 vertices, colors, paint)); 306 return DrawGlInfo::kStatusDone; 307} 308 309status_t DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, 310 const int32_t* yDivs, const uint32_t* colors, uint32_t width, uint32_t height, 311 int8_t numColors, float left, float top, float right, float bottom, SkPaint* paint) { 312 int alpha; 313 SkXfermode::Mode mode; 314 OpenGLRenderer::getAlphaAndModeDirect(paint, &alpha, &mode); 315 316 bitmap = refBitmap(bitmap); 317 xDivs = refBuffer<int>(xDivs, width); 318 yDivs = refBuffer<int>(yDivs, height); 319 colors = refBuffer<uint32_t>(colors, numColors); 320 321 addDrawOp(new (alloc()) DrawPatchOp(bitmap, xDivs, yDivs, colors, width, height, numColors, 322 left, top, right, bottom, alpha, mode)); 323 return DrawGlInfo::kStatusDone; 324} 325 326status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 327 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 328 return DrawGlInfo::kStatusDone; 329} 330 331status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 332 SkPaint* paint) { 333 paint = refPaint(paint); 334 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 335 return DrawGlInfo::kStatusDone; 336} 337 338status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 339 float rx, float ry, SkPaint* paint) { 340 paint = refPaint(paint); 341 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 342 return DrawGlInfo::kStatusDone; 343} 344 345status_t DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { 346 paint = refPaint(paint); 347 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 348 return DrawGlInfo::kStatusDone; 349} 350 351status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 352 SkPaint* paint) { 353 paint = refPaint(paint); 354 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 355 return DrawGlInfo::kStatusDone; 356} 357 358status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 359 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { 360 paint = refPaint(paint); 361 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 362 startAngle, sweepAngle, useCenter, paint)); 363 return DrawGlInfo::kStatusDone; 364} 365 366status_t DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) { 367 path = refPath(path); 368 paint = refPaint(paint); 369 370 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 371 return DrawGlInfo::kStatusDone; 372} 373 374status_t DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) { 375 points = refBuffer<float>(points, count); 376 paint = refPaint(paint); 377 378 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 379 return DrawGlInfo::kStatusDone; 380} 381 382status_t DisplayListRenderer::drawPoints(float* points, int count, SkPaint* paint) { 383 points = refBuffer<float>(points, count); 384 paint = refPaint(paint); 385 386 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 387 return DrawGlInfo::kStatusDone; 388} 389 390status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 391 SkPath* path, float hOffset, float vOffset, SkPaint* paint) { 392 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 393 394 text = refText(text, bytesCount); 395 path = refPath(path); 396 paint = refPaint(paint); 397 398 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 399 hOffset, vOffset, paint); 400 addDrawOp(op); 401 return DrawGlInfo::kStatusDone; 402} 403 404status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 405 const float* positions, SkPaint* paint) { 406 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 407 408 text = refText(text, bytesCount); 409 positions = refBuffer<float>(positions, count * 2); 410 paint = refPaint(paint); 411 412 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 413 addDrawOp(op); 414 return DrawGlInfo::kStatusDone; 415} 416 417status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 418 float x, float y, const float* positions, SkPaint* paint, float length) { 419 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 420 421 if (length < 0.0f) length = paint->measureText(text, bytesCount); 422 423 text = refText(text, bytesCount); 424 positions = refBuffer<float>(positions, count * 2); 425 paint = refPaint(paint); 426 427 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, x, y, positions, paint, length); 428 addDrawOp(op); 429 return DrawGlInfo::kStatusDone; 430} 431 432status_t DisplayListRenderer::drawRects(const float* rects, int count, SkPaint* paint) { 433 if (count <= 0) return DrawGlInfo::kStatusDone; 434 435 rects = refBuffer<float>(rects, count); 436 paint = refPaint(paint); 437 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 438 return DrawGlInfo::kStatusDone; 439} 440 441void DisplayListRenderer::resetShader() { 442 addStateOp(new (alloc()) ResetShaderOp()); 443} 444 445void DisplayListRenderer::setupShader(SkiaShader* shader) { 446 shader = refShader(shader); 447 addStateOp(new (alloc()) SetupShaderOp(shader)); 448} 449 450void DisplayListRenderer::resetColorFilter() { 451 addStateOp(new (alloc()) ResetColorFilterOp()); 452} 453 454void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) { 455 filter = refColorFilter(filter); 456 addStateOp(new (alloc()) SetupColorFilterOp(filter)); 457} 458 459void DisplayListRenderer::resetShadow() { 460 addStateOp(new (alloc()) ResetShadowOp()); 461} 462 463void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) { 464 addStateOp(new (alloc()) SetupShadowOp(radius, dx, dy, color)); 465} 466 467void DisplayListRenderer::resetPaintFilter() { 468 addStateOp(new (alloc()) ResetPaintFilterOp()); 469} 470 471void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 472 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 473} 474 475void DisplayListRenderer::insertRestoreToCount() { 476 if (mRestoreSaveCount >= 0) { 477 DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount); 478 mDisplayListData->displayListOps.add(op); 479 mRestoreSaveCount = -1; 480 } 481} 482 483void DisplayListRenderer::insertTranslate() { 484 if (mHasTranslate) { 485 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 486 DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY); 487 mDisplayListData->displayListOps.add(op); 488 mTranslateX = mTranslateY = 0.0f; 489 } 490 mHasTranslate = false; 491 } 492} 493 494void DisplayListRenderer::addStateOp(StateOp* op) { 495 addOpInternal(op); 496} 497 498void DisplayListRenderer::addDrawOp(DrawOp* op) { 499 Rect localBounds; 500 if (op->getLocalBounds(localBounds)) { 501 bool rejected = quickRejectNoScissor(localBounds.left, localBounds.top, 502 localBounds.right, localBounds.bottom); 503 op->setQuickRejected(rejected); 504 } 505 mHasDrawOps = true; 506 addOpInternal(op); 507} 508 509}; // namespace uirenderer 510}; // namespace android 511