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