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 #include "DisplayListCanvas.h"
18 
19 #include "ResourceCache.h"
20 #include "DeferredDisplayList.h"
21 #include "DeferredLayerUpdater.h"
22 #include "DisplayListOp.h"
23 #include "RenderNode.h"
24 #include "utils/PaintUtils.h"
25 
26 #include <SkCamera.h>
27 #include <SkCanvas.h>
28 
29 #include <private/hwui/DrawGlInfo.h>
30 
31 namespace android {
32 namespace uirenderer {
33 
DisplayListCanvas()34 DisplayListCanvas::DisplayListCanvas()
35     : mState(*this)
36     , mResourceCache(ResourceCache::getInstance())
37     , mDisplayListData(nullptr)
38     , mTranslateX(0.0f)
39     , mTranslateY(0.0f)
40     , mHasDeferredTranslate(false)
41     , mDeferredBarrierType(kBarrier_None)
42     , mHighContrastText(false)
43     , mRestoreSaveCount(-1) {
44 }
45 
~DisplayListCanvas()46 DisplayListCanvas::~DisplayListCanvas() {
47     LOG_ALWAYS_FATAL_IF(mDisplayListData,
48             "Destroyed a DisplayListCanvas during a record!");
49 }
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 // Operations
53 ///////////////////////////////////////////////////////////////////////////////
54 
finishRecording()55 DisplayListData* DisplayListCanvas::finishRecording() {
56     mPaintMap.clear();
57     mRegionMap.clear();
58     mPathMap.clear();
59     DisplayListData* data = mDisplayListData;
60     mDisplayListData = nullptr;
61     mSkiaCanvasProxy.reset(nullptr);
62     return data;
63 }
64 
prepareDirty(float left,float top,float right,float bottom)65 void DisplayListCanvas::prepareDirty(float left, float top,
66         float right, float bottom) {
67 
68     LOG_ALWAYS_FATAL_IF(mDisplayListData,
69             "prepareDirty called a second time during a recording!");
70     mDisplayListData = new DisplayListData();
71 
72     mState.initializeSaveStack(0, 0, mState.getWidth(), mState.getHeight(), Vector3());
73 
74     mDeferredBarrierType = kBarrier_InOrder;
75     mState.setDirtyClip(false);
76     mRestoreSaveCount = -1;
77 }
78 
finish()79 bool DisplayListCanvas::finish() {
80     flushRestoreToCount();
81     flushTranslate();
82     return false;
83 }
84 
interrupt()85 void DisplayListCanvas::interrupt() {
86 }
87 
resume()88 void DisplayListCanvas::resume() {
89 }
90 
callDrawGLFunction(Functor * functor)91 void DisplayListCanvas::callDrawGLFunction(Functor *functor) {
92     addDrawOp(new (alloc()) DrawFunctorOp(functor));
93     mDisplayListData->functors.add(functor);
94 }
95 
asSkCanvas()96 SkCanvas* DisplayListCanvas::asSkCanvas() {
97     LOG_ALWAYS_FATAL_IF(!mDisplayListData,
98             "attempting to get an SkCanvas when we are not recording!");
99     if (!mSkiaCanvasProxy) {
100         mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this));
101     }
102 
103     // SkCanvas instances default to identity transform, but should inherit
104     // the state of this Canvas; if this code was in the SkiaCanvasProxy
105     // constructor, we couldn't cache mSkiaCanvasProxy.
106     SkMatrix parentTransform;
107     getMatrix(&parentTransform);
108     mSkiaCanvasProxy.get()->setMatrix(parentTransform);
109 
110     return mSkiaCanvasProxy.get();
111 }
112 
save(SkCanvas::SaveFlags flags)113 int DisplayListCanvas::save(SkCanvas::SaveFlags flags) {
114     addStateOp(new (alloc()) SaveOp((int) flags));
115     return mState.save((int) flags);
116 }
117 
restore()118 void DisplayListCanvas::restore() {
119     if (mRestoreSaveCount < 0) {
120         restoreToCount(getSaveCount() - 1);
121         return;
122     }
123 
124     mRestoreSaveCount--;
125     flushTranslate();
126     mState.restore();
127 }
128 
restoreToCount(int saveCount)129 void DisplayListCanvas::restoreToCount(int saveCount) {
130     mRestoreSaveCount = saveCount;
131     flushTranslate();
132     mState.restoreToCount(saveCount);
133 }
134 
saveLayer(float left,float top,float right,float bottom,const SkPaint * paint,SkCanvas::SaveFlags flags)135 int DisplayListCanvas::saveLayer(float left, float top, float right, float bottom,
136         const SkPaint* paint, SkCanvas::SaveFlags flags) {
137     // force matrix/clip isolation for layer
138     flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
139 
140     paint = refPaint(paint);
141     addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, (int) flags));
142     return mState.save((int) flags);
143 }
144 
translate(float dx,float dy)145 void DisplayListCanvas::translate(float dx, float dy) {
146     if (dx == 0.0f && dy == 0.0f) return;
147 
148     mHasDeferredTranslate = true;
149     mTranslateX += dx;
150     mTranslateY += dy;
151     flushRestoreToCount();
152     mState.translate(dx, dy, 0.0f);
153 }
154 
rotate(float degrees)155 void DisplayListCanvas::rotate(float degrees) {
156     if (degrees == 0.0f) return;
157 
158     addStateOp(new (alloc()) RotateOp(degrees));
159     mState.rotate(degrees);
160 }
161 
scale(float sx,float sy)162 void DisplayListCanvas::scale(float sx, float sy) {
163     if (sx == 1.0f && sy == 1.0f) return;
164 
165     addStateOp(new (alloc()) ScaleOp(sx, sy));
166     mState.scale(sx, sy);
167 }
168 
skew(float sx,float sy)169 void DisplayListCanvas::skew(float sx, float sy) {
170     addStateOp(new (alloc()) SkewOp(sx, sy));
171     mState.skew(sx, sy);
172 }
173 
setMatrix(const SkMatrix & matrix)174 void DisplayListCanvas::setMatrix(const SkMatrix& matrix) {
175     addStateOp(new (alloc()) SetMatrixOp(matrix));
176     mState.setMatrix(matrix);
177 }
178 
setLocalMatrix(const SkMatrix & matrix)179 void DisplayListCanvas::setLocalMatrix(const SkMatrix& matrix) {
180     addStateOp(new (alloc()) SetLocalMatrixOp(matrix));
181     mState.setMatrix(matrix);
182 }
183 
concat(const SkMatrix & matrix)184 void DisplayListCanvas::concat(const SkMatrix& matrix) {
185     addStateOp(new (alloc()) ConcatMatrixOp(matrix));
186     mState.concatMatrix(matrix);
187 }
188 
getClipBounds(SkRect * outRect) const189 bool DisplayListCanvas::getClipBounds(SkRect* outRect) const {
190     Rect bounds = mState.getLocalClipBounds();
191     *outRect = SkRect::MakeLTRB(bounds.left, bounds.top, bounds.right, bounds.bottom);
192     return !(outRect->isEmpty());
193 }
194 
quickRejectRect(float left,float top,float right,float bottom) const195 bool DisplayListCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
196     return mState.quickRejectConservative(left, top, right, bottom);
197 }
198 
quickRejectPath(const SkPath & path) const199 bool DisplayListCanvas::quickRejectPath(const SkPath& path) const {
200     SkRect bounds = path.getBounds();
201     return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom);
202 }
203 
204 
clipRect(float left,float top,float right,float bottom,SkRegion::Op op)205 bool DisplayListCanvas::clipRect(float left, float top, float right, float bottom,
206         SkRegion::Op op) {
207     addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op));
208     return mState.clipRect(left, top, right, bottom, op);
209 }
210 
clipPath(const SkPath * path,SkRegion::Op op)211 bool DisplayListCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
212     path = refPath(path);
213     addStateOp(new (alloc()) ClipPathOp(path, op));
214     return mState.clipPath(path, op);
215 }
216 
clipRegion(const SkRegion * region,SkRegion::Op op)217 bool DisplayListCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
218     region = refRegion(region);
219     addStateOp(new (alloc()) ClipRegionOp(region, op));
220     return mState.clipRegion(region, op);
221 }
222 
drawRenderNode(RenderNode * renderNode)223 void DisplayListCanvas::drawRenderNode(RenderNode* renderNode) {
224     LOG_ALWAYS_FATAL_IF(!renderNode, "missing rendernode");
225     DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp(
226             renderNode,
227             *mState.currentTransform(),
228             mState.clipIsSimple());
229     addRenderNodeOp(op);
230 }
231 
drawLayer(DeferredLayerUpdater * layerHandle,float x,float y)232 void DisplayListCanvas::drawLayer(DeferredLayerUpdater* layerHandle, float x, float y) {
233     // We ref the DeferredLayerUpdater due to its thread-safe ref-counting
234     // semantics.
235     mDisplayListData->ref(layerHandle);
236     addDrawOp(new (alloc()) DrawLayerOp(layerHandle->backingLayer(), x, y));
237 }
238 
drawBitmap(const SkBitmap * bitmap,const SkPaint * paint)239 void DisplayListCanvas::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
240     bitmap = refBitmap(*bitmap);
241     paint = refPaint(paint);
242 
243     addDrawOp(new (alloc()) DrawBitmapOp(bitmap, paint));
244 }
245 
drawBitmap(const SkBitmap & bitmap,float left,float top,const SkPaint * paint)246 void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top,
247         const SkPaint* paint) {
248     save(SkCanvas::kMatrix_SaveFlag);
249     translate(left, top);
250     drawBitmap(&bitmap, paint);
251     restore();
252 }
253 
drawBitmap(const SkBitmap & bitmap,const SkMatrix & matrix,const SkPaint * paint)254 void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
255         const SkPaint* paint) {
256     if (matrix.isIdentity()) {
257         drawBitmap(&bitmap, paint);
258     } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask))
259             && MathUtils::isPositive(matrix.getScaleX())
260             && MathUtils::isPositive(matrix.getScaleY())) {
261         // SkMatrix::isScaleTranslate() not available in L
262         SkRect src;
263         SkRect dst;
264         bitmap.getBounds(&src);
265         matrix.mapRect(&dst, src);
266         drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
267                    dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
268     } else {
269         save(SkCanvas::kMatrix_SaveFlag);
270         concat(matrix);
271         drawBitmap(&bitmap, paint);
272         restore();
273     }
274 }
275 
drawBitmap(const SkBitmap & bitmap,float srcLeft,float srcTop,float srcRight,float srcBottom,float dstLeft,float dstTop,float dstRight,float dstBottom,const SkPaint * paint)276 void DisplayListCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
277         float srcRight, float srcBottom, float dstLeft, float dstTop,
278         float dstRight, float dstBottom, const SkPaint* paint) {
279     if (srcLeft == 0 && srcTop == 0
280             && srcRight == bitmap.width()
281             && 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         save(SkCanvas::kMatrix_SaveFlag);
286         translate(dstLeft, dstTop);
287         drawBitmap(&bitmap, paint);
288         restore();
289     } else {
290         paint = refPaint(paint);
291 
292         if (paint && paint->getShader()) {
293             float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
294             float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
295             if (!MathUtils::areEqual(scaleX, 1.0f) || !MathUtils::areEqual(scaleY, 1.0f)) {
296                 // Apply the scale transform on the canvas, so that the shader
297                 // effectively calculates positions relative to src rect space
298 
299                 save(SkCanvas::kMatrix_SaveFlag);
300                 translate(dstLeft, dstTop);
301                 scale(scaleX, scaleY);
302 
303                 dstLeft = 0.0f;
304                 dstTop = 0.0f;
305                 dstRight = srcRight - srcLeft;
306                 dstBottom = srcBottom - srcTop;
307 
308                 addDrawOp(new (alloc()) DrawBitmapRectOp(refBitmap(bitmap),
309                         srcLeft, srcTop, srcRight, srcBottom,
310                         dstLeft, dstTop, dstRight, dstBottom, paint));
311                 restore();
312                 return;
313             }
314         }
315 
316         addDrawOp(new (alloc()) DrawBitmapRectOp(refBitmap(bitmap),
317                 srcLeft, srcTop, srcRight, srcBottom,
318                 dstLeft, dstTop, dstRight, dstBottom, paint));
319     }
320 }
321 
drawBitmapMesh(const SkBitmap & bitmap,int meshWidth,int meshHeight,const float * vertices,const int * colors,const SkPaint * paint)322 void DisplayListCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
323         const float* vertices, const int* colors, const SkPaint* paint) {
324     int vertexCount = (meshWidth + 1) * (meshHeight + 1);
325     vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex
326     paint = refPaint(paint);
327     colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex
328 
329     addDrawOp(new (alloc()) DrawBitmapMeshOp(refBitmap(bitmap), meshWidth, meshHeight,
330            vertices, colors, paint));
331 }
332 
drawPatch(const SkBitmap & bitmap,const Res_png_9patch * patch,float left,float top,float right,float bottom,const SkPaint * paint)333 void DisplayListCanvas::drawPatch(const SkBitmap& bitmap, const Res_png_9patch* patch,
334         float left, float top, float right, float bottom, const SkPaint* paint) {
335     const SkBitmap* bitmapPtr = refBitmap(bitmap);
336     patch = refPatch(patch);
337     paint = refPaint(paint);
338 
339     addDrawOp(new (alloc()) DrawPatchOp(bitmapPtr, patch, left, top, right, bottom, paint));
340 }
341 
drawColor(int color,SkXfermode::Mode mode)342 void DisplayListCanvas::drawColor(int color, SkXfermode::Mode mode) {
343     addDrawOp(new (alloc()) DrawColorOp(color, mode));
344 }
345 
drawPaint(const SkPaint & paint)346 void DisplayListCanvas::drawPaint(const SkPaint& paint) {
347     SkRect bounds;
348     if (getClipBounds(&bounds)) {
349         drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint);
350     }
351 }
352 
353 
drawRect(float left,float top,float right,float bottom,const SkPaint & paint)354 void DisplayListCanvas::drawRect(float left, float top, float right, float bottom,
355         const SkPaint& paint) {
356     addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, refPaint(&paint)));
357 }
358 
drawRoundRect(float left,float top,float right,float bottom,float rx,float ry,const SkPaint & paint)359 void DisplayListCanvas::drawRoundRect(float left, float top, float right, float bottom,
360         float rx, float ry, const SkPaint& paint) {
361     addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, refPaint(&paint)));
362 }
363 
drawRoundRect(CanvasPropertyPrimitive * left,CanvasPropertyPrimitive * top,CanvasPropertyPrimitive * right,CanvasPropertyPrimitive * bottom,CanvasPropertyPrimitive * rx,CanvasPropertyPrimitive * ry,CanvasPropertyPaint * paint)364 void DisplayListCanvas::drawRoundRect(
365         CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
366         CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
367         CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
368         CanvasPropertyPaint* paint) {
369     mDisplayListData->ref(left);
370     mDisplayListData->ref(top);
371     mDisplayListData->ref(right);
372     mDisplayListData->ref(bottom);
373     mDisplayListData->ref(rx);
374     mDisplayListData->ref(ry);
375     mDisplayListData->ref(paint);
376     refBitmapsInShader(paint->value.getShader());
377     addDrawOp(new (alloc()) DrawRoundRectPropsOp(&left->value, &top->value,
378             &right->value, &bottom->value, &rx->value, &ry->value, &paint->value));
379 }
380 
drawCircle(float x,float y,float radius,const SkPaint & paint)381 void DisplayListCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
382     addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, refPaint(&paint)));
383 }
384 
drawCircle(CanvasPropertyPrimitive * x,CanvasPropertyPrimitive * y,CanvasPropertyPrimitive * radius,CanvasPropertyPaint * paint)385 void DisplayListCanvas::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
386         CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
387     mDisplayListData->ref(x);
388     mDisplayListData->ref(y);
389     mDisplayListData->ref(radius);
390     mDisplayListData->ref(paint);
391     refBitmapsInShader(paint->value.getShader());
392     addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value,
393             &radius->value, &paint->value));
394 }
395 
drawOval(float left,float top,float right,float bottom,const SkPaint & paint)396 void DisplayListCanvas::drawOval(float left, float top, float right, float bottom,
397         const SkPaint& paint) {
398     addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, refPaint(&paint)));
399 }
400 
drawArc(float left,float top,float right,float bottom,float startAngle,float sweepAngle,bool useCenter,const SkPaint & paint)401 void DisplayListCanvas::drawArc(float left, float top, float right, float bottom,
402         float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
403     if (fabs(sweepAngle) >= 360.0f) {
404         drawOval(left, top, right, bottom, paint);
405     } else {
406         addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
407                         startAngle, sweepAngle, useCenter, refPaint(&paint)));
408     }
409 }
410 
drawPath(const SkPath & path,const SkPaint & paint)411 void DisplayListCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
412     addDrawOp(new (alloc()) DrawPathOp(refPath(&path), refPaint(&paint)));
413 }
414 
drawLines(const float * points,int count,const SkPaint & paint)415 void DisplayListCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
416     points = refBuffer<float>(points, count);
417 
418     addDrawOp(new (alloc()) DrawLinesOp(points, count, refPaint(&paint)));
419 }
420 
drawPoints(const float * points,int count,const SkPaint & paint)421 void DisplayListCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
422     points = refBuffer<float>(points, count);
423 
424     addDrawOp(new (alloc()) DrawPointsOp(points, count, refPaint(&paint)));
425 }
426 
drawTextOnPath(const uint16_t * glyphs,int count,const SkPath & path,float hOffset,float vOffset,const SkPaint & paint)427 void DisplayListCanvas::drawTextOnPath(const uint16_t* glyphs, int count,
428         const SkPath& path, float hOffset, float vOffset, const SkPaint& paint) {
429     if (!glyphs || count <= 0) return;
430 
431     int bytesCount = 2 * count;
432     DrawOp* op = new (alloc()) DrawTextOnPathOp(refText((const char*) glyphs, bytesCount),
433             bytesCount, count, refPath(&path),
434             hOffset, vOffset, refPaint(&paint));
435     addDrawOp(op);
436 }
437 
drawPosText(const uint16_t * text,const float * positions,int count,int posCount,const SkPaint & paint)438 void DisplayListCanvas::drawPosText(const uint16_t* text, const float* positions,
439         int count, int posCount, const SkPaint& paint) {
440     if (!text || count <= 0) return;
441 
442     int bytesCount = 2 * count;
443     positions = refBuffer<float>(positions, count * 2);
444 
445     DrawOp* op = new (alloc()) DrawPosTextOp(refText((const char*) text, bytesCount),
446                                              bytesCount, count, positions, refPaint(&paint));
447     addDrawOp(op);
448 }
449 
simplifyPaint(int color,SkPaint * paint)450 static void simplifyPaint(int color, SkPaint* paint) {
451     paint->setColor(color);
452     paint->setShader(nullptr);
453     paint->setColorFilter(nullptr);
454     paint->setLooper(nullptr);
455     paint->setStrokeWidth(4 + 0.04 * paint->getTextSize());
456     paint->setStrokeJoin(SkPaint::kRound_Join);
457     paint->setLooper(nullptr);
458 }
459 
drawText(const uint16_t * glyphs,const float * positions,int count,const SkPaint & paint,float x,float y,float boundsLeft,float boundsTop,float boundsRight,float boundsBottom,float totalAdvance)460 void DisplayListCanvas::drawText(const uint16_t* glyphs, const float* positions,
461         int count, const SkPaint& paint, float x, float y,
462         float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
463         float totalAdvance) {
464 
465     if (!glyphs || count <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
466 
467     int bytesCount = count * 2;
468     const char* text = refText((const char*) glyphs, bytesCount);
469     positions = refBuffer<float>(positions, count * 2);
470     Rect bounds(boundsLeft, boundsTop, boundsRight, boundsBottom);
471 
472     if (CC_UNLIKELY(mHighContrastText)) {
473         // high contrast draw path
474         int color = paint.getColor();
475         int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color);
476         bool darken = channelSum < (128 * 3);
477 
478         // outline
479         SkPaint* outlinePaint = copyPaint(&paint);
480         simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, outlinePaint);
481         outlinePaint->setStyle(SkPaint::kStrokeAndFill_Style);
482         addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count,
483                 x, y, positions, outlinePaint, totalAdvance, bounds)); // bounds?
484 
485         // inner
486         SkPaint* innerPaint = copyPaint(&paint);
487         simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, innerPaint);
488         innerPaint->setStyle(SkPaint::kFill_Style);
489         addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count,
490                 x, y, positions, innerPaint, totalAdvance, bounds));
491     } else {
492         // standard draw path
493         DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
494                 x, y, positions, refPaint(&paint), totalAdvance, bounds);
495         addDrawOp(op);
496     }
497 }
498 
drawRects(const float * rects,int count,const SkPaint * paint)499 void DisplayListCanvas::drawRects(const float* rects, int count, const SkPaint* paint) {
500     if (count <= 0) return;
501 
502     rects = refBuffer<float>(rects, count);
503     paint = refPaint(paint);
504     addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
505 }
506 
setDrawFilter(SkDrawFilter * filter)507 void DisplayListCanvas::setDrawFilter(SkDrawFilter* filter) {
508     mDrawFilter.reset(SkSafeRef(filter));
509 }
510 
insertReorderBarrier(bool enableReorder)511 void DisplayListCanvas::insertReorderBarrier(bool enableReorder) {
512     flushRestoreToCount();
513     flushTranslate();
514     mDeferredBarrierType = enableReorder ? kBarrier_OutOfOrder : kBarrier_InOrder;
515 }
516 
flushRestoreToCount()517 void DisplayListCanvas::flushRestoreToCount() {
518     if (mRestoreSaveCount >= 0) {
519         addOpAndUpdateChunk(new (alloc()) RestoreToCountOp(mRestoreSaveCount));
520         mRestoreSaveCount = -1;
521     }
522 }
523 
flushTranslate()524 void DisplayListCanvas::flushTranslate() {
525     if (mHasDeferredTranslate) {
526         if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
527             addOpAndUpdateChunk(new (alloc()) TranslateOp(mTranslateX, mTranslateY));
528             mTranslateX = mTranslateY = 0.0f;
529         }
530         mHasDeferredTranslate = false;
531     }
532 }
533 
addOpAndUpdateChunk(DisplayListOp * op)534 size_t DisplayListCanvas::addOpAndUpdateChunk(DisplayListOp* op) {
535     int insertIndex = mDisplayListData->displayListOps.add(op);
536     if (mDeferredBarrierType != kBarrier_None) {
537         // op is first in new chunk
538         mDisplayListData->chunks.push();
539         DisplayListData::Chunk& newChunk = mDisplayListData->chunks.editTop();
540         newChunk.beginOpIndex = insertIndex;
541         newChunk.endOpIndex = insertIndex + 1;
542         newChunk.reorderChildren = (mDeferredBarrierType == kBarrier_OutOfOrder);
543 
544         int nextChildIndex = mDisplayListData->children().size();
545         newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex;
546         mDeferredBarrierType = kBarrier_None;
547     } else {
548         // standard case - append to existing chunk
549         mDisplayListData->chunks.editTop().endOpIndex = insertIndex + 1;
550     }
551     return insertIndex;
552 }
553 
flushAndAddOp(DisplayListOp * op)554 size_t DisplayListCanvas::flushAndAddOp(DisplayListOp* op) {
555     flushRestoreToCount();
556     flushTranslate();
557     return addOpAndUpdateChunk(op);
558 }
559 
addStateOp(StateOp * op)560 size_t DisplayListCanvas::addStateOp(StateOp* op) {
561     return flushAndAddOp(op);
562 }
563 
addDrawOp(DrawOp * op)564 size_t DisplayListCanvas::addDrawOp(DrawOp* op) {
565     Rect localBounds;
566     if (op->getLocalBounds(localBounds)) {
567         bool rejected = quickRejectRect(localBounds.left, localBounds.top,
568                 localBounds.right, localBounds.bottom);
569         op->setQuickRejected(rejected);
570     }
571 
572     mDisplayListData->hasDrawOps = true;
573     return flushAndAddOp(op);
574 }
575 
addRenderNodeOp(DrawRenderNodeOp * op)576 size_t DisplayListCanvas::addRenderNodeOp(DrawRenderNodeOp* op) {
577     int opIndex = addDrawOp(op);
578     int childIndex = mDisplayListData->addChild(op);
579 
580     // update the chunk's child indices
581     DisplayListData::Chunk& chunk = mDisplayListData->chunks.editTop();
582     chunk.endChildIndex = childIndex + 1;
583 
584     if (op->renderNode()->stagingProperties().isProjectionReceiver()) {
585         // use staging property, since recording on UI thread
586         mDisplayListData->projectionReceiveIndex = opIndex;
587     }
588     return opIndex;
589 }
590 
refBitmapsInShader(const SkShader * shader)591 void DisplayListCanvas::refBitmapsInShader(const SkShader* shader) {
592     if (!shader) return;
593 
594     // If this paint has an SkShader that has an SkBitmap add
595     // it to the bitmap pile
596     SkBitmap bitmap;
597     SkShader::TileMode xy[2];
598     if (shader->asABitmap(&bitmap, nullptr, xy) == SkShader::kDefault_BitmapType) {
599         refBitmap(bitmap);
600         return;
601     }
602     SkShader::ComposeRec rec;
603     if (shader->asACompose(&rec)) {
604         refBitmapsInShader(rec.fShaderA);
605         refBitmapsInShader(rec.fShaderB);
606         return;
607     }
608 }
609 
610 }; // namespace uirenderer
611 }; // namespace android
612