DisplayListRenderer.cpp revision 139088228faa7f3c446af7387e017933998a5570
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    paint = refPaint(paint);
312    addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
313                    startAngle, sweepAngle, useCenter, paint));
314    return DrawGlInfo::kStatusDone;
315}
316
317status_t DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
318    path = refPath(path);
319    paint = refPaint(paint);
320
321    addDrawOp(new (alloc()) DrawPathOp(path, paint));
322    return DrawGlInfo::kStatusDone;
323}
324
325status_t DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
326    points = refBuffer<float>(points, count);
327    paint = refPaint(paint);
328
329    addDrawOp(new (alloc()) DrawLinesOp(points, count, paint));
330    return DrawGlInfo::kStatusDone;
331}
332
333status_t DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
334    points = refBuffer<float>(points, count);
335    paint = refPaint(paint);
336
337    addDrawOp(new (alloc()) DrawPointsOp(points, count, paint));
338    return DrawGlInfo::kStatusDone;
339}
340
341status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
342        const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
343    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
344
345    text = refText(text, bytesCount);
346    path = refPath(path);
347    paint = refPaint(paint);
348
349    DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path,
350            hOffset, vOffset, paint);
351    addDrawOp(op);
352    return DrawGlInfo::kStatusDone;
353}
354
355status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
356        const float* positions, const SkPaint* paint) {
357    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
358
359    text = refText(text, bytesCount);
360    positions = refBuffer<float>(positions, count * 2);
361    paint = refPaint(paint);
362
363    DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint);
364    addDrawOp(op);
365    return DrawGlInfo::kStatusDone;
366}
367
368status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
369        float x, float y, const float* positions, const SkPaint* paint,
370        float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) {
371
372    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
373
374    text = refText(text, bytesCount);
375    positions = refBuffer<float>(positions, count * 2);
376    paint = refPaint(paint);
377
378    DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
379            x, y, positions, paint, totalAdvance, bounds);
380    addDrawOp(op);
381    return DrawGlInfo::kStatusDone;
382}
383
384status_t DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
385    if (count <= 0) return DrawGlInfo::kStatusDone;
386
387    rects = refBuffer<float>(rects, count);
388    paint = refPaint(paint);
389    addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
390    return DrawGlInfo::kStatusDone;
391}
392
393void DisplayListRenderer::resetPaintFilter() {
394    addStateOp(new (alloc()) ResetPaintFilterOp());
395}
396
397void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
398    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
399}
400
401void DisplayListRenderer::insertRestoreToCount() {
402    if (mRestoreSaveCount >= 0) {
403        DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount);
404        mDisplayListData->displayListOps.add(op);
405        mRestoreSaveCount = -1;
406    }
407}
408
409void DisplayListRenderer::insertTranslate() {
410    if (mHasTranslate) {
411        if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
412            DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY);
413            mDisplayListData->displayListOps.add(op);
414            mTranslateX = mTranslateY = 0.0f;
415        }
416        mHasTranslate = false;
417    }
418}
419
420void DisplayListRenderer::addStateOp(StateOp* op) {
421    addOpInternal(op);
422}
423
424void DisplayListRenderer::addDrawOp(DrawOp* op) {
425    Rect localBounds;
426    if (op->getLocalBounds(mDrawModifiers, localBounds)) {
427        bool rejected = quickRejectConservative(localBounds.left, localBounds.top,
428                localBounds.right, localBounds.bottom);
429        op->setQuickRejected(rejected);
430    }
431
432    mDisplayListData->hasDrawOps = true;
433    addOpInternal(op);
434}
435
436}; // namespace uirenderer
437}; // namespace android
438