DisplayListRenderer.cpp revision 6ac174b97246ed40fe780b29561603b61770fa17
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::drawDisplayList(RenderNode* displayList,
173        Rect& dirty, int32_t flags) {
174    // dirty is an out parameter and should not be recorded,
175    // it matters only when replaying the display list
176
177    if (displayList->stagingProperties().isProjectionReceiver()) {
178        // use staging property, since recording on UI thread
179        mDisplayListData->projectionReceiveIndex = mDisplayListData->displayListOps.size();
180    }
181
182    DrawDisplayListOp* op = new (alloc()) DrawDisplayListOp(displayList,
183            flags, *currentTransform());
184    addDrawOp(op);
185    mDisplayListData->addChild(op);
186    return DrawGlInfo::kStatusDone;
187}
188
189status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) {
190    layer = refLayer(layer);
191    addDrawOp(new (alloc()) DrawLayerOp(layer, x, y));
192    return DrawGlInfo::kStatusDone;
193}
194
195status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top,
196        const SkPaint* paint) {
197    bitmap = refBitmap(bitmap);
198    paint = refPaint(paint);
199
200    addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint));
201    return DrawGlInfo::kStatusDone;
202}
203
204status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix& matrix,
205        const SkPaint* paint) {
206    bitmap = refBitmap(bitmap);
207    paint = refPaint(paint);
208
209    addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint));
210    return DrawGlInfo::kStatusDone;
211}
212
213status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
214        float srcRight, float srcBottom, float dstLeft, float dstTop,
215        float dstRight, float dstBottom, const SkPaint* paint) {
216    bitmap = refBitmap(bitmap);
217    paint = refPaint(paint);
218
219    if (srcLeft == 0 && srcTop == 0 &&
220            srcRight == bitmap->width() && srcBottom == bitmap->height() &&
221            (srcBottom - srcTop == dstBottom - dstTop) &&
222            (srcRight - srcLeft == dstRight - dstLeft)) {
223        // transform simple rect to rect drawing case into position bitmap ops, since they merge
224        addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint));
225        return DrawGlInfo::kStatusDone;
226    }
227
228    addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap,
229                    srcLeft, srcTop, srcRight, srcBottom,
230                    dstLeft, dstTop, dstRight, dstBottom, paint));
231    return DrawGlInfo::kStatusDone;
232}
233
234status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top,
235        const SkPaint* paint) {
236    bitmap = refBitmapData(bitmap);
237    paint = refPaint(paint);
238
239    addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint));
240    return DrawGlInfo::kStatusDone;
241}
242
243status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
244        const float* vertices, const int* colors, const SkPaint* paint) {
245    int vertexCount = (meshWidth + 1) * (meshHeight + 1);
246    bitmap = refBitmap(bitmap);
247    vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex
248    paint = refPaint(paint);
249    colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex
250
251    addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight,
252                    vertices, colors, paint));
253    return DrawGlInfo::kStatusDone;
254}
255
256status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
257        float left, float top, float right, float bottom, const SkPaint* paint) {
258    bitmap = refBitmap(bitmap);
259    patch = refPatch(patch);
260    paint = refPaint(paint);
261
262    addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint));
263    return DrawGlInfo::kStatusDone;
264}
265
266status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
267    addDrawOp(new (alloc()) DrawColorOp(color, mode));
268    return DrawGlInfo::kStatusDone;
269}
270
271status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
272        const SkPaint* paint) {
273    paint = refPaint(paint);
274    addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint));
275    return DrawGlInfo::kStatusDone;
276}
277
278status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
279        float rx, float ry, const SkPaint* paint) {
280    paint = refPaint(paint);
281    addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint));
282    return DrawGlInfo::kStatusDone;
283}
284
285status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) {
286    paint = refPaint(paint);
287    addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint));
288    return DrawGlInfo::kStatusDone;
289}
290
291status_t DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
292        CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
293    mDisplayListData->refProperty(x);
294    mDisplayListData->refProperty(y);
295    mDisplayListData->refProperty(radius);
296    mDisplayListData->refProperty(paint);
297    addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value,
298            &radius->value, &paint->value));
299    return DrawGlInfo::kStatusDone;
300}
301
302status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
303        const SkPaint* paint) {
304    paint = refPaint(paint);
305    addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint));
306    return DrawGlInfo::kStatusDone;
307}
308
309status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
310        float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
311    if (fabs(sweepAngle) > 360.0f) {
312        return drawOval(left, top, right, bottom, paint);
313    }
314
315    paint = refPaint(paint);
316    addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
317                    startAngle, sweepAngle, useCenter, paint));
318    return DrawGlInfo::kStatusDone;
319}
320
321status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
322    path = refPath(path);
323    paint = refPaint(paint);
324
325    addDrawOp(new (alloc()) DrawPathOp(path, paint));
326    return DrawGlInfo::kStatusDone;
327}
328
329status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
330    points = refBuffer<float>(points, count);
331    paint = refPaint(paint);
332
333    addDrawOp(new (alloc()) DrawLinesOp(points, count, paint));
334    return DrawGlInfo::kStatusDone;
335}
336
337status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
338    points = refBuffer<float>(points, count);
339    paint = refPaint(paint);
340
341    addDrawOp(new (alloc()) DrawPointsOp(points, count, paint));
342    return DrawGlInfo::kStatusDone;
343}
344
345status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
346        const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
347    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
348
349    text = refText(text, bytesCount);
350    path = refPath(path);
351    paint = refPaint(paint);
352
353    DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path,
354            hOffset, vOffset, paint);
355    addDrawOp(op);
356    return DrawGlInfo::kStatusDone;
357}
358
359status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
360        const float* positions, const SkPaint* paint) {
361    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
362
363    text = refText(text, bytesCount);
364    positions = refBuffer<float>(positions, count * 2);
365    paint = refPaint(paint);
366
367    DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint);
368    addDrawOp(op);
369    return DrawGlInfo::kStatusDone;
370}
371
372status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
373        float x, float y, const float* positions, const SkPaint* paint,
374        float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) {
375
376    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
377
378    text = refText(text, bytesCount);
379    positions = refBuffer<float>(positions, count * 2);
380    paint = refPaint(paint);
381
382    DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
383            x, y, positions, paint, totalAdvance, bounds);
384    addDrawOp(op);
385    return DrawGlInfo::kStatusDone;
386}
387
388status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
389    if (count <= 0) return DrawGlInfo::kStatusDone;
390
391    rects = refBuffer<float>(rects, count);
392    paint = refPaint(paint);
393    addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
394    return DrawGlInfo::kStatusDone;
395}
396
397void DisplayListRenderer::resetPaintFilter() {
398    addStateOp(new (alloc()) ResetPaintFilterOp());
399}
400
401void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
402    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
403}
404
405void DisplayListRenderer::insertRestoreToCount() {
406    if (mRestoreSaveCount >= 0) {
407        DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount);
408        mDisplayListData->displayListOps.add(op);
409        mRestoreSaveCount = -1;
410    }
411}
412
413void DisplayListRenderer::insertTranslate() {
414    if (mHasTranslate) {
415        if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
416            DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY);
417            mDisplayListData->displayListOps.add(op);
418            mTranslateX = mTranslateY = 0.0f;
419        }
420        mHasTranslate = false;
421    }
422}
423
424void DisplayListRenderer::addStateOp(StateOp* op) {
425    addOpInternal(op);
426}
427
428void DisplayListRenderer::addDrawOp(DrawOp* op) {
429    Rect localBounds;
430    if (op->getLocalBounds(mDrawModifiers, localBounds)) {
431        bool rejected = quickRejectConservative(localBounds.left, localBounds.top,
432                localBounds.right, localBounds.bottom);
433        op->setQuickRejected(rejected);
434    }
435
436    mDisplayListData->hasDrawOps = true;
437    addOpInternal(op);
438}
439
440}; // namespace uirenderer
441}; // namespace android
442