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