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