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