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