DisplayListRenderer.cpp revision 797b95b26bbb7557678af78b9a2a61830158920f
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    mShaderMap.clear();
51    mPaintMap.clear();
52    mRegionMap.clear();
53    mPathMap.clear();
54    DisplayListData* data = mDisplayListData;
55    mDisplayListData = 0;
56    return data;
57}
58
59status_t DisplayListRenderer::prepareDirty(float left, float top,
60        float right, float bottom, bool opaque) {
61
62    LOG_ALWAYS_FATAL_IF(mDisplayListData,
63            "prepareDirty called a second time during a recording!");
64    mDisplayListData = new DisplayListData();
65
66    initializeSaveStack(0, 0, getWidth(), getHeight());
67
68    mDirtyClip = opaque;
69    mRestoreSaveCount = -1;
70
71    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
72}
73
74void DisplayListRenderer::finish() {
75    insertRestoreToCount();
76    insertTranslate();
77}
78
79void DisplayListRenderer::interrupt() {
80}
81
82void DisplayListRenderer::resume() {
83}
84
85status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
86    // Ignore dirty during recording, it matters only when we replay
87    addDrawOp(new (alloc()) DrawFunctorOp(functor));
88    mDisplayListData->functorCount++;
89    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
90}
91
92int DisplayListRenderer::save(int flags) {
93    addStateOp(new (alloc()) SaveOp(flags));
94    return StatefulBaseRenderer::save(flags);
95}
96
97void DisplayListRenderer::restore() {
98    if (mRestoreSaveCount < 0) {
99        restoreToCount(getSaveCount() - 1);
100        return;
101    }
102
103    mRestoreSaveCount--;
104    insertTranslate();
105    StatefulBaseRenderer::restore();
106}
107
108void DisplayListRenderer::restoreToCount(int saveCount) {
109    mRestoreSaveCount = saveCount;
110    insertTranslate();
111    StatefulBaseRenderer::restoreToCount(saveCount);
112}
113
114int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
115        const SkPaint* paint, int flags) {
116    paint = refPaint(paint);
117    addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags));
118    return StatefulBaseRenderer::save(flags);
119}
120
121void DisplayListRenderer::translate(float dx, float dy, float dz) {
122    // ignore dz, not used at defer time
123    mHasTranslate = true;
124    mTranslateX += dx;
125    mTranslateY += dy;
126    insertRestoreToCount();
127    StatefulBaseRenderer::translate(dx, dy, dz);
128}
129
130void DisplayListRenderer::rotate(float degrees) {
131    addStateOp(new (alloc()) RotateOp(degrees));
132    StatefulBaseRenderer::rotate(degrees);
133}
134
135void DisplayListRenderer::scale(float sx, float sy) {
136    addStateOp(new (alloc()) ScaleOp(sx, sy));
137    StatefulBaseRenderer::scale(sx, sy);
138}
139
140void DisplayListRenderer::skew(float sx, float sy) {
141    addStateOp(new (alloc()) SkewOp(sx, sy));
142    StatefulBaseRenderer::skew(sx, sy);
143}
144
145void DisplayListRenderer::setMatrix(const SkMatrix* matrix) {
146    matrix = refMatrix(matrix);
147    addStateOp(new (alloc()) SetMatrixOp(matrix));
148    StatefulBaseRenderer::setMatrix(matrix);
149}
150
151void DisplayListRenderer::concatMatrix(const SkMatrix* matrix) {
152    matrix = refMatrix(matrix);
153    addStateOp(new (alloc()) ConcatMatrixOp(matrix));
154    StatefulBaseRenderer::concatMatrix(matrix);
155}
156
157bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
158        SkRegion::Op op) {
159    addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op));
160    return StatefulBaseRenderer::clipRect(left, top, right, bottom, op);
161}
162
163bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
164    path = refPath(path);
165    addStateOp(new (alloc()) ClipPathOp(path, op));
166    return StatefulBaseRenderer::clipPath(path, op);
167}
168
169bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
170    region = refRegion(region);
171    addStateOp(new (alloc()) ClipRegionOp(region, op));
172    return StatefulBaseRenderer::clipRegion(region, op);
173}
174
175status_t DisplayListRenderer::drawDisplayList(RenderNode* displayList,
176        Rect& dirty, int32_t flags) {
177    // dirty is an out parameter and should not be recorded,
178    // it matters only when replaying the display list
179
180    if (displayList->stagingProperties().isProjectionReceiver()) {
181        // use staging property, since recording on UI thread
182        mDisplayListData->projectionReceiveIndex = mDisplayListData->displayListOps.size();
183    }
184
185    DrawDisplayListOp* op = new (alloc()) DrawDisplayListOp(displayList,
186            flags, *currentTransform());
187    addDrawOp(op);
188    mDisplayListData->addChild(op);
189    return DrawGlInfo::kStatusDone;
190}
191
192status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) {
193    layer = refLayer(layer);
194    addDrawOp(new (alloc()) DrawLayerOp(layer, x, y));
195    return DrawGlInfo::kStatusDone;
196}
197
198status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top,
199        const SkPaint* paint) {
200    bitmap = refBitmap(bitmap);
201    paint = refPaint(paint);
202
203    addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint));
204    return DrawGlInfo::kStatusDone;
205}
206
207status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix,
208        const SkPaint* paint) {
209    bitmap = refBitmap(bitmap);
210    matrix = refMatrix(matrix);
211    paint = refPaint(paint);
212
213    addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint));
214    return DrawGlInfo::kStatusDone;
215}
216
217status_t DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
218        float srcRight, float srcBottom, float dstLeft, float dstTop,
219        float dstRight, float dstBottom, const SkPaint* paint) {
220    bitmap = refBitmap(bitmap);
221    paint = refPaint(paint);
222
223    if (srcLeft == 0 && srcTop == 0 &&
224            srcRight == bitmap->width() && srcBottom == bitmap->height() &&
225            (srcBottom - srcTop == dstBottom - dstTop) &&
226            (srcRight - srcLeft == dstRight - dstLeft)) {
227        // transform simple rect to rect drawing case into position bitmap ops, since they merge
228        addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint));
229        return DrawGlInfo::kStatusDone;
230    }
231
232    addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap,
233                    srcLeft, srcTop, srcRight, srcBottom,
234                    dstLeft, dstTop, dstRight, dstBottom, paint));
235    return DrawGlInfo::kStatusDone;
236}
237
238status_t DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top,
239        const SkPaint* paint) {
240    bitmap = refBitmapData(bitmap);
241    paint = refPaint(paint);
242
243    addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint));
244    return DrawGlInfo::kStatusDone;
245}
246
247status_t DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
248        const float* vertices, const int* colors, const SkPaint* paint) {
249    int vertexCount = (meshWidth + 1) * (meshHeight + 1);
250    bitmap = refBitmap(bitmap);
251    vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex
252    paint = refPaint(paint);
253    colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex
254
255    addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight,
256                    vertices, colors, paint));
257    return DrawGlInfo::kStatusDone;
258}
259
260status_t DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
261        float left, float top, float right, float bottom, const SkPaint* paint) {
262    bitmap = refBitmap(bitmap);
263    patch = refPatch(patch);
264    paint = refPaint(paint);
265
266    addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint));
267    return DrawGlInfo::kStatusDone;
268}
269
270status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
271    addDrawOp(new (alloc()) DrawColorOp(color, mode));
272    return DrawGlInfo::kStatusDone;
273}
274
275status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
276        const SkPaint* paint) {
277    paint = refPaint(paint);
278    addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint));
279    return DrawGlInfo::kStatusDone;
280}
281
282status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
283        float rx, float ry, const SkPaint* paint) {
284    paint = refPaint(paint);
285    addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint));
286    return DrawGlInfo::kStatusDone;
287}
288
289status_t DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) {
290    paint = refPaint(paint);
291    addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint));
292    return DrawGlInfo::kStatusDone;
293}
294
295status_t DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
296        CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
297    mDisplayListData->refProperty(x);
298    mDisplayListData->refProperty(y);
299    mDisplayListData->refProperty(radius);
300    mDisplayListData->refProperty(paint);
301    addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value,
302            &radius->value, &paint->value));
303    return DrawGlInfo::kStatusDone;
304}
305
306status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
307        const SkPaint* paint) {
308    paint = refPaint(paint);
309    addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint));
310    return DrawGlInfo::kStatusDone;
311}
312
313status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
314        float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
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::resetShader() {
398    addStateOp(new (alloc()) ResetShaderOp());
399}
400
401void DisplayListRenderer::setupShader(SkiaShader* shader) {
402    shader = refShader(shader);
403    addStateOp(new (alloc()) SetupShaderOp(shader));
404}
405
406void DisplayListRenderer::resetPaintFilter() {
407    addStateOp(new (alloc()) ResetPaintFilterOp());
408}
409
410void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
411    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
412}
413
414void DisplayListRenderer::insertRestoreToCount() {
415    if (mRestoreSaveCount >= 0) {
416        DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount);
417        mDisplayListData->displayListOps.add(op);
418        mRestoreSaveCount = -1;
419    }
420}
421
422void DisplayListRenderer::insertTranslate() {
423    if (mHasTranslate) {
424        if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
425            DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY);
426            mDisplayListData->displayListOps.add(op);
427            mTranslateX = mTranslateY = 0.0f;
428        }
429        mHasTranslate = false;
430    }
431}
432
433void DisplayListRenderer::addStateOp(StateOp* op) {
434    addOpInternal(op);
435}
436
437void DisplayListRenderer::addDrawOp(DrawOp* op) {
438    Rect localBounds;
439    if (op->getLocalBounds(mDrawModifiers, localBounds)) {
440        bool rejected = quickRejectConservative(localBounds.left, localBounds.top,
441                localBounds.right, localBounds.bottom);
442        op->setQuickRejected(rejected);
443    }
444
445    mDisplayListData->hasDrawOps = true;
446    addOpInternal(op);
447}
448
449}; // namespace uirenderer
450}; // namespace android
451