DisplayListRenderer.cpp revision b3cca876c2e11b865cb4f83abe2c48a60b95af5b
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->functors.add(functor); 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 DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp(renderNode, flags, *currentTransform()); 180 int opIndex = addDrawOp(op); 181 mDisplayListData->addChild(op); 182 183 if (renderNode->stagingProperties().isProjectionReceiver()) { 184 // use staging property, since recording on UI thread 185 mDisplayListData->projectionReceiveIndex = opIndex; 186 } 187 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, const SkPaint* paint) { 198 bitmap = refBitmap(bitmap); 199 paint = refPaint(paint); 200 201 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, paint)); 202 return DrawGlInfo::kStatusDone; 203} 204 205status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop, 206 float srcRight, float srcBottom, float dstLeft, float dstTop, 207 float dstRight, float dstBottom, const SkPaint* paint) { 208 if (srcLeft == 0 && srcTop == 0 209 && srcRight == bitmap->width() && srcBottom == bitmap->height() 210 && (srcBottom - srcTop == dstBottom - dstTop) 211 && (srcRight - srcLeft == dstRight - dstLeft)) { 212 // transform simple rect to rect drawing case into position bitmap ops, since they merge 213 save(SkCanvas::kMatrix_SaveFlag); 214 translate(dstLeft, dstTop); 215 drawBitmap(bitmap, paint); 216 restore(); 217 } else { 218 bitmap = refBitmap(bitmap); 219 paint = refPaint(paint); 220 221 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap, 222 srcLeft, srcTop, srcRight, srcBottom, 223 dstLeft, dstTop, dstRight, dstBottom, paint)); 224 } 225 return DrawGlInfo::kStatusDone; 226} 227 228status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) { 229 bitmap = refBitmapData(bitmap); 230 paint = refPaint(paint); 231 232 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, paint)); 233 return DrawGlInfo::kStatusDone; 234} 235 236status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight, 237 const float* vertices, const int* colors, const SkPaint* paint) { 238 int vertexCount = (meshWidth + 1) * (meshHeight + 1); 239 bitmap = refBitmap(bitmap); 240 vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex 241 paint = refPaint(paint); 242 colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex 243 244 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight, 245 vertices, colors, paint)); 246 return DrawGlInfo::kStatusDone; 247} 248 249status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch, 250 float left, float top, float right, float bottom, const SkPaint* paint) { 251 bitmap = refBitmap(bitmap); 252 patch = refPatch(patch); 253 paint = refPaint(paint); 254 255 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint)); 256 return DrawGlInfo::kStatusDone; 257} 258 259status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) { 260 addDrawOp(new (alloc()) DrawColorOp(color, mode)); 261 return DrawGlInfo::kStatusDone; 262} 263 264status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom, 265 const SkPaint* paint) { 266 paint = refPaint(paint); 267 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint)); 268 return DrawGlInfo::kStatusDone; 269} 270 271status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom, 272 float rx, float ry, const SkPaint* paint) { 273 paint = refPaint(paint); 274 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint)); 275 return DrawGlInfo::kStatusDone; 276} 277 278status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) { 279 paint = refPaint(paint); 280 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint)); 281 return DrawGlInfo::kStatusDone; 282} 283 284status_t DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, 285 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) { 286 mDisplayListData->refProperty(x); 287 mDisplayListData->refProperty(y); 288 mDisplayListData->refProperty(radius); 289 mDisplayListData->refProperty(paint); 290 addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value, 291 &radius->value, &paint->value)); 292 return DrawGlInfo::kStatusDone; 293} 294 295status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom, 296 const SkPaint* paint) { 297 paint = refPaint(paint); 298 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint)); 299 return DrawGlInfo::kStatusDone; 300} 301 302status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom, 303 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) { 304 if (fabs(sweepAngle) >= 360.0f) { 305 return drawOval(left, top, right, bottom, paint); 306 } 307 308 paint = refPaint(paint); 309 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom, 310 startAngle, sweepAngle, useCenter, paint)); 311 return DrawGlInfo::kStatusDone; 312} 313 314status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) { 315 path = refPath(path); 316 paint = refPaint(paint); 317 318 addDrawOp(new (alloc()) DrawPathOp(path, paint)); 319 return DrawGlInfo::kStatusDone; 320} 321 322status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) { 323 points = refBuffer<float>(points, count); 324 paint = refPaint(paint); 325 326 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint)); 327 return DrawGlInfo::kStatusDone; 328} 329 330status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) { 331 points = refBuffer<float>(points, count); 332 paint = refPaint(paint); 333 334 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint)); 335 return DrawGlInfo::kStatusDone; 336} 337 338status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count, 339 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) { 340 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 341 342 text = refText(text, bytesCount); 343 path = refPath(path); 344 paint = refPaint(paint); 345 346 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path, 347 hOffset, vOffset, paint); 348 addDrawOp(op); 349 return DrawGlInfo::kStatusDone; 350} 351 352status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count, 353 const float* positions, const SkPaint* paint) { 354 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 355 356 text = refText(text, bytesCount); 357 positions = refBuffer<float>(positions, count * 2); 358 paint = refPaint(paint); 359 360 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint); 361 addDrawOp(op); 362 return DrawGlInfo::kStatusDone; 363} 364 365static void simplifyPaint(int color, SkPaint* paint) { 366 paint->setColor(color); 367 paint->setShader(NULL); 368 paint->setColorFilter(NULL); 369 paint->setLooper(NULL); 370 paint->setStrokeWidth(4 + 0.04 * paint->getTextSize()); 371 paint->setStrokeJoin(SkPaint::kRound_Join); 372 paint->setLooper(NULL); 373} 374 375status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count, 376 float x, float y, const float* positions, const SkPaint* paint, 377 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) { 378 379 if (!text || count <= 0) return DrawGlInfo::kStatusDone; 380 381 text = refText(text, bytesCount); 382 positions = refBuffer<float>(positions, count * 2); 383 384 if (CC_UNLIKELY(mHighContrastText)) { 385 // high contrast draw path 386 int color = paint->getColor(); 387 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color); 388 bool darken = channelSum < (128 * 3); 389 390 // outline 391 SkPaint* outlinePaint = copyPaint(paint); 392 simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, outlinePaint); 393 outlinePaint->setStyle(SkPaint::kStrokeAndFill_Style); 394 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 395 x, y, positions, outlinePaint, totalAdvance, bounds)); // bounds? 396 397 // inner 398 SkPaint* innerPaint = copyPaint(paint); 399 simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, innerPaint); 400 innerPaint->setStyle(SkPaint::kFill_Style); 401 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count, 402 x, y, positions, innerPaint, totalAdvance, bounds)); 403 } else { 404 // standard draw path 405 paint = refPaint(paint); 406 407 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count, 408 x, y, positions, paint, totalAdvance, bounds); 409 addDrawOp(op); 410 } 411 return DrawGlInfo::kStatusDone; 412} 413 414status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) { 415 if (count <= 0) return DrawGlInfo::kStatusDone; 416 417 rects = refBuffer<float>(rects, count); 418 paint = refPaint(paint); 419 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint)); 420 return DrawGlInfo::kStatusDone; 421} 422 423void DisplayListRenderer::resetPaintFilter() { 424 addStateOp(new (alloc()) ResetPaintFilterOp()); 425} 426 427void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) { 428 addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits)); 429} 430 431void DisplayListRenderer::insertRestoreToCount() { 432 if (mRestoreSaveCount >= 0) { 433 DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount); 434 mDisplayListData->displayListOps.add(op); 435 mRestoreSaveCount = -1; 436 } 437} 438 439void DisplayListRenderer::insertTranslate() { 440 if (mHasTranslate) { 441 if (mTranslateX != 0.0f || mTranslateY != 0.0f) { 442 DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY); 443 mDisplayListData->displayListOps.add(op); 444 mTranslateX = mTranslateY = 0.0f; 445 } 446 mHasTranslate = false; 447 } 448} 449 450int DisplayListRenderer::addStateOp(StateOp* op) { 451 return addOpInternal(op); 452} 453 454int DisplayListRenderer::addDrawOp(DrawOp* op) { 455 Rect localBounds; 456 if (op->getLocalBounds(localBounds)) { 457 bool rejected = quickRejectConservative(localBounds.left, localBounds.top, 458 localBounds.right, localBounds.bottom); 459 op->setQuickRejected(rejected); 460 } 461 462 mDisplayListData->hasDrawOps = true; 463 return addOpInternal(op); 464} 465 466}; // namespace uirenderer 467}; // namespace android 468