DisplayListRenderer.cpp revision cce47eb580d666ead1f6095d1e3b65233592bbaa
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 "Caches.h" 25#include "DeferredDisplayList.h" 26#include "DisplayListLogBuffer.h" 27#include "DisplayListOp.h" 28#include "DisplayListRenderer.h" 29#include "RenderNode.h" 30 31namespace android { 32namespace uirenderer { 33 34DisplayListRenderer::DisplayListRenderer() 35 : mCaches(Caches::getInstance()) 36 , mDisplayListData(0) 37 , mTranslateX(0.0f) 38 , mTranslateY(0.0f) 39 , mHasTranslate(false) 40 , mHighContrastText(false) 41 , mRestoreSaveCount(-1) { 42} 43 44DisplayListRenderer::~DisplayListRenderer() { 45 LOG_ALWAYS_FATAL_IF(mDisplayListData, 46 "Destroyed a DisplayListRenderer during a record!"); 47} 48 49/////////////////////////////////////////////////////////////////////////////// 50// Operations 51/////////////////////////////////////////////////////////////////////////////// 52 53DisplayListData* DisplayListRenderer::finishRecording() { 54 mPaintMap.clear(); 55 mRegionMap.clear(); 56 mPathMap.clear(); 57 DisplayListData* data = mDisplayListData; 58 mDisplayListData = 0; 59 return data; 60} 61 62status_t DisplayListRenderer::prepareDirty(float left, float top, 63 float right, float bottom, bool opaque) { 64 65 LOG_ALWAYS_FATAL_IF(mDisplayListData, 66 "prepareDirty called a second time during a recording!"); 67 mDisplayListData = new DisplayListData(); 68 69 initializeSaveStack(0, 0, getWidth(), getHeight()); 70 71 mDirtyClip = opaque; 72 mRestoreSaveCount = -1; 73 74 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 75} 76 77void DisplayListRenderer::finish() { 78 insertRestoreToCount(); 79 insertTranslate(); 80} 81 82void DisplayListRenderer::interrupt() { 83} 84 85void DisplayListRenderer::resume() { 86} 87 88status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { 89 // Ignore dirty during recording, it matters only when we replay 90 addDrawOp(new (alloc()) DrawFunctorOp(functor)); 91 mDisplayListData->functorCount++; 92 return DrawGlInfo::kStatusDone; // No invalidate needed at record-time 93} 94 95int DisplayListRenderer::save(int flags) { 96 addStateOp(new (alloc()) SaveOp(flags)); 97 return StatefulBaseRenderer::save(flags); 98} 99 100void DisplayListRenderer::restore() { 101 if (mRestoreSaveCount < 0) { 102 restoreToCount(getSaveCount() - 1); 103 return; 104 } 105 106 mRestoreSaveCount--; 107 insertTranslate(); 108 StatefulBaseRenderer::restore(); 109} 110 111void DisplayListRenderer::restoreToCount(int saveCount) { 112 mRestoreSaveCount = saveCount; 113 insertTranslate(); 114 StatefulBaseRenderer::restoreToCount(saveCount); 115} 116 117int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom, 118 const SkPaint* paint, int flags) { 119 paint = refPaint(paint); 120 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags)); 121 return StatefulBaseRenderer::save(flags); 122} 123 124void DisplayListRenderer::translate(float dx, float dy, float dz) { 125 // ignore dz, not used at defer time 126 mHasTranslate = true; 127 mTranslateX += dx; 128 mTranslateY += dy; 129 insertRestoreToCount(); 130 StatefulBaseRenderer::translate(dx, dy, dz); 131} 132 133void DisplayListRenderer::rotate(float degrees) { 134 addStateOp(new (alloc()) RotateOp(degrees)); 135 StatefulBaseRenderer::rotate(degrees); 136} 137 138void DisplayListRenderer::scale(float sx, float sy) { 139 addStateOp(new (alloc()) ScaleOp(sx, sy)); 140 StatefulBaseRenderer::scale(sx, sy); 141} 142 143void DisplayListRenderer::skew(float sx, float sy) { 144 addStateOp(new (alloc()) SkewOp(sx, sy)); 145 StatefulBaseRenderer::skew(sx, sy); 146} 147 148void DisplayListRenderer::setMatrix(const SkMatrix& matrix) { 149 addStateOp(new (alloc()) SetMatrixOp(matrix)); 150 StatefulBaseRenderer::setMatrix(matrix); 151} 152 153void DisplayListRenderer::concatMatrix(const SkMatrix& matrix) { 154 addStateOp(new (alloc()) ConcatMatrixOp(matrix)); 155 StatefulBaseRenderer::concatMatrix(matrix); 156} 157 158bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom, 159 SkRegion::Op op) { 160 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op)); 161 return StatefulBaseRenderer::clipRect(left, top, right, bottom, op); 162} 163 164bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) { 165 path = refPath(path); 166 addStateOp(new (alloc()) ClipPathOp(path, op)); 167 return StatefulBaseRenderer::clipPath(path, op); 168} 169 170bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) { 171 region = refRegion(region); 172 addStateOp(new (alloc()) ClipRegionOp(region, op)); 173 return StatefulBaseRenderer::clipRegion(region, op); 174} 175 176status_t DisplayListRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t flags) { 177 // dirty is an out parameter and should not be recorded, 178 // it matters only when replaying the display list 179 180 if (renderNode->stagingProperties().isProjectionReceiver()) { 181 // use staging property, since recording on UI thread 182 mDisplayListData->projectionReceiveIndex = mDisplayListData->displayListOps.size(); 183 } 184 185 DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp(renderNode, flags, *currentTransform()); 186 addDrawOp(op); 187 mDisplayListData->addChild(op); 188 return DrawGlInfo::kStatusDone; 189} 190 191status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) { 192 layer = refLayer(layer); 193 addDrawOp(new (alloc()) DrawLayerOp(layer, x, y)); 194 return DrawGlInfo::kStatusDone; 195} 196 197status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top, 198 const SkPaint* paint) { 199 bitmap = refBitmap(bitmap); 200 paint = refPaint(paint); 201 202 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint)); 203 return DrawGlInfo::kStatusDone; 204} 205 206status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix& matrix, 207 const SkPaint* paint) { 208 bitmap = refBitmap(bitmap); 209 paint = refPaint(paint); 210 211 addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint)); 212 return DrawGlInfo::kStatusDone; 213} 214 215status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 216 float srcRight, float srcBottom, float dstLeft, float dstTop, 217 float dstRight, float dstBottom, const SkPaint* paint) { 218 bitmap = refBitmap(bitmap); 219 paint = refPaint(paint); 220 221 if (srcLeft == 0 && srcTop == 0 && 222 srcRight == bitmap->width() && srcBottom == bitmap->height() && 223 (srcBottom - srcTop == dstBottom - dstTop) && 224 (srcRight - srcLeft == dstRight - dstLeft)) { 225 // transform simple rect to rect drawing case into position bitmap ops, since they merge 226 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint)); 227 return DrawGlInfo::kStatusDone; 228 } 229 230 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 231 srcLeft, srcTop, srcRight, srcBottom, 232 dstLeft, dstTop, dstRight, dstBottom, paint)); 233 return DrawGlInfo::kStatusDone; 234} 235 236status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top, 237 const SkPaint* paint) { 238 bitmap = refBitmapData(bitmap); 239 paint = refPaint(paint); 240 241 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint)); 242 return DrawGlInfo::kStatusDone; 243} 244 245status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 246 const float* vertices, const int* colors, const SkPaint* paint) { 247 int vertexCount = (meshWidth + 1) * (meshHeight + 1); 248 bitmap = refBitmap(bitmap); 249 vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex 250 paint = refPaint(paint); 251 colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex 252 253 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 254 vertices, colors, paint)); 255 return DrawGlInfo::kStatusDone; 256} 257 258status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 259 float left, float top, float right, float bottom, const SkPaint* paint) { 260 bitmap = refBitmap(bitmap); 261 patch = refPatch(patch); 262 paint = refPaint(paint); 263 264 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint)); 265 return DrawGlInfo::kStatusDone; 266} 267 268status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 269 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 270 return DrawGlInfo::kStatusDone; 271} 272 273status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 274 const SkPaint* paint) { 275 paint = refPaint(paint); 276 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 277 return DrawGlInfo::kStatusDone; 278} 279 280status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 281 float rx, float ry, const SkPaint* paint) { 282 paint = refPaint(paint); 283 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 284 return DrawGlInfo::kStatusDone; 285} 286 287status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) { 288 paint = refPaint(paint); 289 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 290 return DrawGlInfo::kStatusDone; 291} 292 293status_t DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 294 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) { 295 mDisplayListData->refProperty(x); 296 mDisplayListData->refProperty(y); 297 mDisplayListData->refProperty(radius); 298 mDisplayListData->refProperty(paint); 299 addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value, 300 &radius->value, &paint->value)); 301 return DrawGlInfo::kStatusDone; 302} 303 304status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 305 const SkPaint* paint) { 306 paint = refPaint(paint); 307 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 308 return DrawGlInfo::kStatusDone; 309} 310 311status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 312 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { 313 if (fabs(sweepAngle) >= 360.0f) { 314 return drawOval(left, top, right, bottom, paint); 315 } 316 317 paint = refPaint(paint); 318 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 319 startAngle, sweepAngle, useCenter, paint)); 320 return DrawGlInfo::kStatusDone; 321} 322 323status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) { 324 path = refPath(path); 325 paint = refPaint(paint); 326 327 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 328 return DrawGlInfo::kStatusDone; 329} 330 331status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) { 332 points = refBuffer<float>(points, count); 333 paint = refPaint(paint); 334 335 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 336 return DrawGlInfo::kStatusDone; 337} 338 339status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { 340 points = refBuffer<float>(points, count); 341 paint = refPaint(paint); 342 343 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 344 return DrawGlInfo::kStatusDone; 345} 346 347status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 348 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { 349 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 350 351 text = refText(text, bytesCount); 352 path = refPath(path); 353 paint = refPaint(paint); 354 355 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 356 hOffset, vOffset, paint); 357 addDrawOp(op); 358 return DrawGlInfo::kStatusDone; 359} 360 361status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 362 const float* positions, const SkPaint* paint) { 363 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 364 365 text = refText(text, bytesCount); 366 positions = refBuffer<float>(positions, count * 2); 367 paint = refPaint(paint); 368 369 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 370 addDrawOp(op); 371 return DrawGlInfo::kStatusDone; 372} 373 374static void simplifyPaint(int color, SkPaint* paint) { 375 paint->setColor(color); 376 paint->setShader(NULL); 377 paint->setColorFilter(NULL); 378 paint->setLooper(NULL); 379 paint->setStrokeWidth(4 + 0.04 * paint->getTextSize()); 380 paint->setStrokeJoin(SkPaint::kRound_Join); 381 paint->setLooper(NULL); 382} 383 384status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 385 float x, float y, const float* positions, const SkPaint* paint, 386 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 387 388 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 389 390 text = refText(text, bytesCount); 391 positions = refBuffer<float>(positions, count * 2); 392 393 if (CC_UNLIKELY(mHighContrastText)) { 394 // high contrast draw path 395 int color = paint->getColor(); 396 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color); 397 bool darken = channelSum < (128 * 3); 398 399 // outline 400 SkPaint* outlinePaint = copyPaint(paint); 401 simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, outlinePaint); 402 outlinePaint->setStyle(SkPaint::kStrokeAndFill_Style); 403 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 404 x, y, positions, outlinePaint, totalAdvance, bounds)); // bounds? 405 406 // inner 407 SkPaint* innerPaint = copyPaint(paint); 408 simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, innerPaint); 409 innerPaint->setStyle(SkPaint::kFill_Style); 410 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 411 x, y, positions, innerPaint, totalAdvance, bounds)); 412 } else { 413 // standard draw path 414 paint = refPaint(paint); 415 416 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 417 x, y, positions, paint, totalAdvance, bounds); 418 addDrawOp(op); 419 } 420 return DrawGlInfo::kStatusDone; 421} 422 423status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { 424 if (count <= 0) return DrawGlInfo::kStatusDone; 425 426 rects = refBuffer<float>(rects, count); 427 paint = refPaint(paint); 428 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 429 return DrawGlInfo::kStatusDone; 430} 431 432void DisplayListRenderer::resetPaintFilter() { 433 addStateOp(new (alloc()) ResetPaintFilterOp()); 434} 435 436void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 437 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 438} 439 440void DisplayListRenderer::insertRestoreToCount() { 441 if (mRestoreSaveCount >= 0) { 442 DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount); 443 mDisplayListData->displayListOps.add(op); 444 mRestoreSaveCount = -1; 445 } 446} 447 448void DisplayListRenderer::insertTranslate() { 449 if (mHasTranslate) { 450 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 451 DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY); 452 mDisplayListData->displayListOps.add(op); 453 mTranslateX = mTranslateY = 0.0f; 454 } 455 mHasTranslate = false; 456 } 457} 458 459void DisplayListRenderer::addStateOp(StateOp* op) { 460 addOpInternal(op); 461} 462 463void DisplayListRenderer::addDrawOp(DrawOp* op) { 464 Rect localBounds; 465 if (op->getLocalBounds(localBounds)) { 466 bool rejected = quickRejectConservative(localBounds.left, localBounds.top, 467 localBounds.right, localBounds.bottom); 468 op->setQuickRejected(rejected); 469 } 470 471 mDisplayListData->hasDrawOps = true; 472 addOpInternal(op); 473} 474 475}; // namespace uirenderer 476}; // namespace android 477