DisplayListRenderer.cpp revision f57776b2d195f0937906eb88b777bb55ccc36967
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 < mPatchResources.size(); i++) {
61        mCaches.resourceCache.decrementRefcountLocked(mPatchResources.itemAt(i));
62    }
63
64    for (size_t i = 0; i < mShaders.size(); i++) {
65        mCaches.resourceCache.decrementRefcountLocked(mShaders.itemAt(i));
66    }
67
68    for (size_t i = 0; i < mSourcePaths.size(); i++) {
69        mCaches.resourceCache.decrementRefcountLocked(mSourcePaths.itemAt(i));
70    }
71
72    for (size_t i = 0; i < mLayers.size(); i++) {
73        mCaches.resourceCache.decrementRefcountLocked(mLayers.itemAt(i));
74    }
75
76    mCaches.resourceCache.unlock();
77
78    mBitmapResources.clear();
79    mOwnedBitmapResources.clear();
80    mFilterResources.clear();
81    mPatchResources.clear();
82    mSourcePaths.clear();
83
84    mShaders.clear();
85    mShaderMap.clear();
86
87    mPaints.clear();
88    mPaintMap.clear();
89
90    mRegions.clear();
91    mRegionMap.clear();
92
93    mPaths.clear();
94    mPathMap.clear();
95
96    mMatrices.clear();
97
98    mLayers.clear();
99
100    mHasDrawOps = false;
101    mFunctorCount = 0;
102}
103
104///////////////////////////////////////////////////////////////////////////////
105// Operations
106///////////////////////////////////////////////////////////////////////////////
107
108DisplayList* DisplayListRenderer::getDisplayList(DisplayList* displayList) {
109    if (!displayList) {
110        displayList = new DisplayList(*this);
111    } else {
112        displayList->initFromDisplayListRenderer(*this, true);
113    }
114    // TODO: should just avoid setting the DisplayList's DisplayListData
115    displayList->setRenderable(mHasDrawOps);
116    return displayList;
117}
118
119bool DisplayListRenderer::isDeferred() {
120    return true;
121}
122
123void DisplayListRenderer::setViewport(int width, int height) {
124    // TODO: DisplayListRenderer shouldn't have a projection matrix, as it should never be used
125    mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
126
127    mWidth = width;
128    mHeight = height;
129}
130
131status_t DisplayListRenderer::prepareDirty(float left, float top,
132        float right, float bottom, bool opaque) {
133    mSnapshot = new Snapshot(mFirstSnapshot,
134            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
135    mSaveCount = 1;
136
137    mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
138    mDirtyClip = opaque;
139
140    mRestoreSaveCount = -1;
141
142    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
143}
144
145void DisplayListRenderer::finish() {
146    insertRestoreToCount();
147    insertTranslate();
148}
149
150void DisplayListRenderer::interrupt() {
151}
152
153void DisplayListRenderer::resume() {
154}
155
156status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
157    // Ignore dirty during recording, it matters only when we replay
158    addDrawOp(new (alloc()) DrawFunctorOp(functor));
159    mFunctorCount++;
160    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
161}
162
163int DisplayListRenderer::save(int flags) {
164    addStateOp(new (alloc()) SaveOp(flags));
165    return OpenGLRenderer::save(flags);
166}
167
168void DisplayListRenderer::restore() {
169    if (mRestoreSaveCount < 0) {
170        restoreToCount(getSaveCount() - 1);
171        return;
172    }
173
174    mRestoreSaveCount--;
175    insertTranslate();
176    OpenGLRenderer::restore();
177}
178
179void DisplayListRenderer::restoreToCount(int saveCount) {
180    mRestoreSaveCount = saveCount;
181    insertTranslate();
182    OpenGLRenderer::restoreToCount(saveCount);
183}
184
185int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
186        int alpha, SkXfermode::Mode mode, int flags) {
187    addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, alpha, mode, flags));
188    return OpenGLRenderer::save(flags);
189}
190
191void DisplayListRenderer::translate(float dx, float dy) {
192    mHasTranslate = true;
193    mTranslateX += dx;
194    mTranslateY += dy;
195    insertRestoreToCount();
196    OpenGLRenderer::translate(dx, dy);
197}
198
199void DisplayListRenderer::rotate(float degrees) {
200    addStateOp(new (alloc()) RotateOp(degrees));
201    OpenGLRenderer::rotate(degrees);
202}
203
204void DisplayListRenderer::scale(float sx, float sy) {
205    addStateOp(new (alloc()) ScaleOp(sx, sy));
206    OpenGLRenderer::scale(sx, sy);
207}
208
209void DisplayListRenderer::skew(float sx, float sy) {
210    addStateOp(new (alloc()) SkewOp(sx, sy));
211    OpenGLRenderer::skew(sx, sy);
212}
213
214void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
215    matrix = refMatrix(matrix);
216    addStateOp(new (alloc()) SetMatrixOp(matrix));
217    OpenGLRenderer::setMatrix(matrix);
218}
219
220void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
221    matrix = refMatrix(matrix);
222    addStateOp(new (alloc()) ConcatMatrixOp(matrix));
223    OpenGLRenderer::concatMatrix(matrix);
224}
225
226bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
227        SkRegion::Op op) {
228    addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op));
229    return OpenGLRenderer::clipRect(left, top, right, bottom, op);
230}
231
232bool DisplayListRenderer::clipPath(SkPath* path, SkRegion::Op op) {
233    path = refPath(path);
234    addStateOp(new (alloc()) ClipPathOp(path, op));
235    return OpenGLRenderer::clipPath(path, op);
236}
237
238bool DisplayListRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
239    region = refRegion(region);
240    addStateOp(new (alloc()) ClipRegionOp(region, op));
241    return OpenGLRenderer::clipRegion(region, op);
242}
243
244status_t DisplayListRenderer::drawDisplayList(DisplayList* displayList,
245        Rect& dirty, int32_t flags) {
246    // dirty is an out parameter and should not be recorded,
247    // it matters only when replaying the display list
248
249    // TODO: To be safe, the display list should be ref-counted in the
250    //       resources cache, but we rely on the caller (UI toolkit) to
251    //       do the right thing for now
252
253    DrawDisplayListOp* op = new (alloc()) DrawDisplayListOp(displayList, flags, currentTransform());
254    addDrawOp(op);
255    mDisplayListData->children.push(op);
256
257    return DrawGlInfo::kStatusDone;
258}
259
260status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) {
261    layer = refLayer(layer);
262    addDrawOp(new (alloc()) DrawLayerOp(layer, x, y));
263    return DrawGlInfo::kStatusDone;
264}
265
266status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
267    bitmap = refBitmap(bitmap);
268    paint = refPaint(paint);
269
270    addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint));
271    return DrawGlInfo::kStatusDone;
272}
273
274status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
275    bitmap = refBitmap(bitmap);
276    matrix = refMatrix(matrix);
277    paint = refPaint(paint);
278
279    addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint));
280    return DrawGlInfo::kStatusDone;
281}
282
283status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
284        float srcRight, float srcBottom, float dstLeft, float dstTop,
285        float dstRight, float dstBottom, SkPaint* paint) {
286    bitmap = refBitmap(bitmap);
287    paint = refPaint(paint);
288
289    if (srcLeft == 0 && srcTop == 0 &&
290            srcRight == bitmap->width() && srcBottom == bitmap->height() &&
291            (srcBottom - srcTop == dstBottom - dstTop) &&
292            (srcRight - srcLeft == dstRight - dstLeft)) {
293        // transform simple rect to rect drawing case into position bitmap ops, since they merge
294        addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint));
295        return DrawGlInfo::kStatusDone;
296    }
297
298    addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap,
299                    srcLeft, srcTop, srcRight, srcBottom,
300                    dstLeft, dstTop, dstRight, dstBottom, paint));
301    return DrawGlInfo::kStatusDone;
302}
303
304status_t DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top,
305        SkPaint* paint) {
306    bitmap = refBitmapData(bitmap);
307    paint = refPaint(paint);
308
309    addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint));
310    return DrawGlInfo::kStatusDone;
311}
312
313status_t DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
314        float* vertices, int* colors, SkPaint* paint) {
315    int count = (meshWidth + 1) * (meshHeight + 1) * 2;
316    bitmap = refBitmap(bitmap);
317    vertices = refBuffer<float>(vertices, count);
318    paint = refPaint(paint);
319    colors = refBuffer<int>(colors, count);
320
321    addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight,
322                    vertices, colors, paint));
323    return DrawGlInfo::kStatusDone;
324}
325
326status_t DisplayListRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
327        float left, float top, float right, float bottom, SkPaint* paint) {
328    bitmap = refBitmap(bitmap);
329    patch = refPatch(patch);
330    paint = refPaint(paint);
331
332    addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint));
333    return DrawGlInfo::kStatusDone;
334}
335
336status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
337    addDrawOp(new (alloc()) DrawColorOp(color, mode));
338    return DrawGlInfo::kStatusDone;
339}
340
341status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
342        SkPaint* paint) {
343    paint = refPaint(paint);
344    addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint));
345    return DrawGlInfo::kStatusDone;
346}
347
348status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
349        float rx, float ry, SkPaint* paint) {
350    paint = refPaint(paint);
351    addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint));
352    return DrawGlInfo::kStatusDone;
353}
354
355status_t DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
356    paint = refPaint(paint);
357    addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint));
358    return DrawGlInfo::kStatusDone;
359}
360
361status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
362        SkPaint* paint) {
363    paint = refPaint(paint);
364    addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint));
365    return DrawGlInfo::kStatusDone;
366}
367
368status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
369        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
370    paint = refPaint(paint);
371    addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
372                    startAngle, sweepAngle, useCenter, paint));
373    return DrawGlInfo::kStatusDone;
374}
375
376status_t DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
377    path = refPath(path);
378    paint = refPaint(paint);
379
380    addDrawOp(new (alloc()) DrawPathOp(path, paint));
381    return DrawGlInfo::kStatusDone;
382}
383
384status_t DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) {
385    points = refBuffer<float>(points, count);
386    paint = refPaint(paint);
387
388    addDrawOp(new (alloc()) DrawLinesOp(points, count, paint));
389    return DrawGlInfo::kStatusDone;
390}
391
392status_t DisplayListRenderer::drawPoints(float* points, int count, SkPaint* paint) {
393    points = refBuffer<float>(points, count);
394    paint = refPaint(paint);
395
396    addDrawOp(new (alloc()) DrawPointsOp(points, count, paint));
397    return DrawGlInfo::kStatusDone;
398}
399
400status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
401        SkPath* path, float hOffset, float vOffset, SkPaint* paint) {
402    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
403
404    text = refText(text, bytesCount);
405    path = refPath(path);
406    paint = refPaint(paint);
407
408    DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path,
409            hOffset, vOffset, paint);
410    addDrawOp(op);
411    return DrawGlInfo::kStatusDone;
412}
413
414status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
415        const float* positions, SkPaint* paint) {
416    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
417
418    text = refText(text, bytesCount);
419    positions = refBuffer<float>(positions, count * 2);
420    paint = refPaint(paint);
421
422    DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint);
423    addDrawOp(op);
424    return DrawGlInfo::kStatusDone;
425}
426
427status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
428        float x, float y, const float* positions, SkPaint* paint,
429        float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) {
430
431    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
432
433    text = refText(text, bytesCount);
434    positions = refBuffer<float>(positions, count * 2);
435    paint = refPaint(paint);
436
437    DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
438            x, y, positions, paint, totalAdvance, bounds);
439    addDrawOp(op);
440    return DrawGlInfo::kStatusDone;
441}
442
443status_t DisplayListRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
444    if (count <= 0) return DrawGlInfo::kStatusDone;
445
446    rects = refBuffer<float>(rects, count);
447    paint = refPaint(paint);
448    addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
449    return DrawGlInfo::kStatusDone;
450}
451
452void DisplayListRenderer::resetShader() {
453    addStateOp(new (alloc()) ResetShaderOp());
454}
455
456void DisplayListRenderer::setupShader(SkiaShader* shader) {
457    shader = refShader(shader);
458    addStateOp(new (alloc()) SetupShaderOp(shader));
459}
460
461void DisplayListRenderer::resetColorFilter() {
462    addStateOp(new (alloc()) ResetColorFilterOp());
463}
464
465void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
466    filter = refColorFilter(filter);
467    addStateOp(new (alloc()) SetupColorFilterOp(filter));
468}
469
470void DisplayListRenderer::resetShadow() {
471    addStateOp(new (alloc()) ResetShadowOp());
472    OpenGLRenderer::resetShadow();
473}
474
475void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
476    addStateOp(new (alloc()) SetupShadowOp(radius, dx, dy, color));
477    OpenGLRenderer::setupShadow(radius, dx, dy, color);
478}
479
480void DisplayListRenderer::resetPaintFilter() {
481    addStateOp(new (alloc()) ResetPaintFilterOp());
482}
483
484void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
485    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
486}
487
488void DisplayListRenderer::insertRestoreToCount() {
489    if (mRestoreSaveCount >= 0) {
490        DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount);
491        mDisplayListData->displayListOps.add(op);
492        mRestoreSaveCount = -1;
493    }
494}
495
496void DisplayListRenderer::insertTranslate() {
497    if (mHasTranslate) {
498        if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
499            DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY);
500            mDisplayListData->displayListOps.add(op);
501            mTranslateX = mTranslateY = 0.0f;
502        }
503        mHasTranslate = false;
504    }
505}
506
507void DisplayListRenderer::addStateOp(StateOp* op) {
508    addOpInternal(op);
509}
510
511void DisplayListRenderer::addDrawOp(DrawOp* op) {
512    Rect localBounds;
513    if (op->getLocalBounds(mDrawModifiers, localBounds)) {
514        bool rejected = quickRejectConservative(localBounds.left, localBounds.top,
515                localBounds.right, localBounds.bottom);
516        op->setQuickRejected(rejected);
517    }
518
519    mHasDrawOps = true;
520    addOpInternal(op);
521}
522
523}; // namespace uirenderer
524}; // namespace android
525