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