DisplayListRenderer.cpp revision 55b6f95ee4ace96c97508bcd14483fb4e9dbeaa0
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 "DisplayList.h"
25#include "DeferredDisplayList.h"
26#include "DisplayListLogBuffer.h"
27#include "DisplayListOp.h"
28#include "DisplayListRenderer.h"
29#include "Caches.h"
30
31namespace android {
32namespace uirenderer {
33
34DisplayListRenderer::DisplayListRenderer():
35        mCaches(Caches::getInstance()), mDisplayListData(new DisplayListData),
36        mTranslateX(0.0f), mTranslateY(0.0f), mHasTranslate(false),
37        mHasDrawOps(false), mFunctorCount(0) {
38}
39
40DisplayListRenderer::~DisplayListRenderer() {
41    reset();
42}
43
44void DisplayListRenderer::reset() {
45    mDisplayListData = new DisplayListData();
46    mCaches.resourceCache.lock();
47
48    for (size_t i = 0; i < mBitmapResources.size(); i++) {
49        mCaches.resourceCache.decrementRefcountLocked(mBitmapResources.itemAt(i));
50    }
51
52    for (size_t i = 0; i < mOwnedBitmapResources.size(); i++) {
53        mCaches.resourceCache.decrementRefcountLocked(mOwnedBitmapResources.itemAt(i));
54    }
55
56    for (size_t i = 0; i < mFilterResources.size(); i++) {
57        mCaches.resourceCache.decrementRefcountLocked(mFilterResources.itemAt(i));
58    }
59
60    for (size_t i = 0; i < mPatchResources.size(); i++) {
61        mCaches.resourceCache.decrementRefcountLocked(mPatchResources.itemAt(i));
62    }
63
64    for (size_t i = 0; i < mShaders.size(); i++) {
65        mCaches.resourceCache.decrementRefcountLocked(mShaders.itemAt(i));
66    }
67
68    for (size_t i = 0; i < mSourcePaths.size(); i++) {
69        mCaches.resourceCache.decrementRefcountLocked(mSourcePaths.itemAt(i));
70    }
71
72    for (size_t i = 0; i < mLayers.size(); i++) {
73        mCaches.resourceCache.decrementRefcountLocked(mLayers.itemAt(i));
74    }
75
76    mCaches.resourceCache.unlock();
77
78    mBitmapResources.clear();
79    mOwnedBitmapResources.clear();
80    mFilterResources.clear();
81    mPatchResources.clear();
82    mSourcePaths.clear();
83
84    mShaders.clear();
85    mShaderMap.clear();
86
87    mPaints.clear();
88    mPaintMap.clear();
89
90    mRegions.clear();
91    mRegionMap.clear();
92
93    mPaths.clear();
94    mPathMap.clear();
95
96    mMatrices.clear();
97
98    mLayers.clear();
99
100    mHasDrawOps = false;
101    mFunctorCount = 0;
102}
103
104///////////////////////////////////////////////////////////////////////////////
105// Operations
106///////////////////////////////////////////////////////////////////////////////
107
108DisplayList* DisplayListRenderer::getDisplayList(DisplayList* displayList) {
109    if (!displayList) {
110        displayList = new DisplayList(*this);
111    } else {
112        displayList->initFromDisplayListRenderer(*this, true);
113    }
114    displayList->setRenderable(mHasDrawOps);
115    return displayList;
116}
117
118bool DisplayListRenderer::isDeferred() {
119    return true;
120}
121
122void DisplayListRenderer::setViewport(int width, int height) {
123    mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
124
125    mWidth = width;
126    mHeight = height;
127}
128
129status_t DisplayListRenderer::prepareDirty(float left, float top,
130        float right, float bottom, bool opaque) {
131    mSnapshot = new Snapshot(mFirstSnapshot,
132            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
133    mSaveCount = 1;
134
135    mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
136    mDirtyClip = opaque;
137
138    mRestoreSaveCount = -1;
139
140    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
141}
142
143void DisplayListRenderer::finish() {
144    insertRestoreToCount();
145    insertTranslate();
146}
147
148void DisplayListRenderer::interrupt() {
149}
150
151void DisplayListRenderer::resume() {
152}
153
154status_t DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
155    // Ignore dirty during recording, it matters only when we replay
156    addDrawOp(new (alloc()) DrawFunctorOp(functor));
157    mFunctorCount++;
158    return DrawGlInfo::kStatusDone; // No invalidate needed at record-time
159}
160
161int DisplayListRenderer::save(int flags) {
162    addStateOp(new (alloc()) SaveOp(flags));
163    return OpenGLRenderer::save(flags);
164}
165
166void DisplayListRenderer::restore() {
167    if (mRestoreSaveCount < 0) {
168        restoreToCount(getSaveCount() - 1);
169        return;
170    }
171
172    mRestoreSaveCount--;
173    insertTranslate();
174    OpenGLRenderer::restore();
175}
176
177void DisplayListRenderer::restoreToCount(int saveCount) {
178    mRestoreSaveCount = saveCount;
179    insertTranslate();
180    OpenGLRenderer::restoreToCount(saveCount);
181}
182
183int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
184        int alpha, SkXfermode::Mode mode, int flags) {
185    addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, alpha, mode, flags));
186    return OpenGLRenderer::save(flags);
187}
188
189void DisplayListRenderer::translate(float dx, float dy) {
190    mHasTranslate = true;
191    mTranslateX += dx;
192    mTranslateY += dy;
193    insertRestoreToCount();
194    OpenGLRenderer::translate(dx, dy);
195}
196
197void DisplayListRenderer::rotate(float degrees) {
198    addStateOp(new (alloc()) RotateOp(degrees));
199    OpenGLRenderer::rotate(degrees);
200}
201
202void DisplayListRenderer::scale(float sx, float sy) {
203    addStateOp(new (alloc()) ScaleOp(sx, sy));
204    OpenGLRenderer::scale(sx, sy);
205}
206
207void DisplayListRenderer::skew(float sx, float sy) {
208    addStateOp(new (alloc()) SkewOp(sx, sy));
209    OpenGLRenderer::skew(sx, sy);
210}
211
212void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
213    matrix = refMatrix(matrix);
214    addStateOp(new (alloc()) SetMatrixOp(matrix));
215    OpenGLRenderer::setMatrix(matrix);
216}
217
218void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
219    matrix = refMatrix(matrix);
220    addStateOp(new (alloc()) ConcatMatrixOp(matrix));
221    OpenGLRenderer::concatMatrix(matrix);
222}
223
224bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
225        SkRegion::Op op) {
226    addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op));
227    return OpenGLRenderer::clipRect(left, top, right, bottom, op);
228}
229
230bool DisplayListRenderer::clipPath(SkPath* path, SkRegion::Op op) {
231    path = refPath(path);
232    addStateOp(new (alloc()) ClipPathOp(path, op));
233    return OpenGLRenderer::clipPath(path, op);
234}
235
236bool DisplayListRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
237    region = refRegion(region);
238    addStateOp(new (alloc()) ClipRegionOp(region, op));
239    return OpenGLRenderer::clipRegion(region, op);
240}
241
242status_t DisplayListRenderer::drawDisplayList(DisplayList* displayList,
243        Rect& dirty, int32_t flags) {
244    // dirty is an out parameter and should not be recorded,
245    // it matters only when replaying the display list
246
247    // TODO: To be safe, the display list should be ref-counted in the
248    //       resources cache, but we rely on the caller (UI toolkit) to
249    //       do the right thing for now
250
251    addDrawOp(new (alloc()) DrawDisplayListOp(displayList, flags));
252    return DrawGlInfo::kStatusDone;
253}
254
255status_t DisplayListRenderer::drawLayer(Layer* layer, float x, float y) {
256    layer = refLayer(layer);
257    addDrawOp(new (alloc()) DrawLayerOp(layer, x, y));
258    return DrawGlInfo::kStatusDone;
259}
260
261status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
262    bitmap = refBitmap(bitmap);
263    paint = refPaint(paint);
264
265    addDrawOp(new (alloc()) DrawBitmapOp(bitmap, left, top, paint));
266    return DrawGlInfo::kStatusDone;
267}
268
269status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
270    bitmap = refBitmap(bitmap);
271    matrix = refMatrix(matrix);
272    paint = refPaint(paint);
273
274    addDrawOp(new (alloc()) DrawBitmapMatrixOp(bitmap, matrix, paint));
275    return DrawGlInfo::kStatusDone;
276}
277
278status_t DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
279        float srcRight, float srcBottom, float dstLeft, float dstTop,
280        float dstRight, float dstBottom, SkPaint* paint) {
281    bitmap = refBitmap(bitmap);
282    paint = refPaint(paint);
283
284    if (srcLeft == 0 && srcTop == 0 &&
285            srcRight == bitmap->width() && srcBottom == bitmap->height() &&
286            (srcBottom - srcTop == dstBottom - dstTop) &&
287            (srcRight - srcLeft == dstRight - dstLeft)) {
288        // transform simple rect to rect drawing case into position bitmap ops, since they merge
289        addDrawOp(new (alloc()) DrawBitmapOp(bitmap, dstLeft, dstTop, paint));
290        return DrawGlInfo::kStatusDone;
291    }
292
293    addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap,
294                    srcLeft, srcTop, srcRight, srcBottom,
295                    dstLeft, dstTop, dstRight, dstBottom, paint));
296    return DrawGlInfo::kStatusDone;
297}
298
299status_t DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top,
300        SkPaint* paint) {
301    bitmap = refBitmapData(bitmap);
302    paint = refPaint(paint);
303
304    addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, left, top, paint));
305    return DrawGlInfo::kStatusDone;
306}
307
308status_t DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
309        float* vertices, int* colors, SkPaint* paint) {
310    int count = (meshWidth + 1) * (meshHeight + 1) * 2;
311    bitmap = refBitmap(bitmap);
312    vertices = refBuffer<float>(vertices, count);
313    paint = refPaint(paint);
314    colors = refBuffer<int>(colors, count);
315
316    addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight,
317                    vertices, colors, paint));
318    return DrawGlInfo::kStatusDone;
319}
320
321status_t DisplayListRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
322        float left, float top, float right, float bottom, SkPaint* paint) {
323    bitmap = refBitmap(bitmap);
324    patch = refPatch(patch);
325    paint = refPaint(paint);
326
327    addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint));
328    return DrawGlInfo::kStatusDone;
329}
330
331status_t DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
332    addDrawOp(new (alloc()) DrawColorOp(color, mode));
333    return DrawGlInfo::kStatusDone;
334}
335
336status_t DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
337        SkPaint* paint) {
338    paint = refPaint(paint);
339    addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint));
340    return DrawGlInfo::kStatusDone;
341}
342
343status_t DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
344        float rx, float ry, SkPaint* paint) {
345    paint = refPaint(paint);
346    addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint));
347    return DrawGlInfo::kStatusDone;
348}
349
350status_t DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
351    paint = refPaint(paint);
352    addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint));
353    return DrawGlInfo::kStatusDone;
354}
355
356status_t DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
357        SkPaint* paint) {
358    paint = refPaint(paint);
359    addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint));
360    return DrawGlInfo::kStatusDone;
361}
362
363status_t DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
364        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
365    paint = refPaint(paint);
366    addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
367                    startAngle, sweepAngle, useCenter, paint));
368    return DrawGlInfo::kStatusDone;
369}
370
371status_t DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
372    path = refPath(path);
373    paint = refPaint(paint);
374
375    addDrawOp(new (alloc()) DrawPathOp(path, paint));
376    return DrawGlInfo::kStatusDone;
377}
378
379status_t DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) {
380    points = refBuffer<float>(points, count);
381    paint = refPaint(paint);
382
383    addDrawOp(new (alloc()) DrawLinesOp(points, count, paint));
384    return DrawGlInfo::kStatusDone;
385}
386
387status_t DisplayListRenderer::drawPoints(float* points, int count, SkPaint* paint) {
388    points = refBuffer<float>(points, count);
389    paint = refPaint(paint);
390
391    addDrawOp(new (alloc()) DrawPointsOp(points, count, paint));
392    return DrawGlInfo::kStatusDone;
393}
394
395status_t DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
396        SkPath* path, float hOffset, float vOffset, SkPaint* paint) {
397    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
398
399    text = refText(text, bytesCount);
400    path = refPath(path);
401    paint = refPaint(paint);
402
403    DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path,
404            hOffset, vOffset, paint);
405    addDrawOp(op);
406    return DrawGlInfo::kStatusDone;
407}
408
409status_t DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
410        const float* positions, SkPaint* paint) {
411    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
412
413    text = refText(text, bytesCount);
414    positions = refBuffer<float>(positions, count * 2);
415    paint = refPaint(paint);
416
417    DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint);
418    addDrawOp(op);
419    return DrawGlInfo::kStatusDone;
420}
421
422status_t DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
423        float x, float y, const float* positions, SkPaint* paint,
424        float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) {
425
426    if (!text || count <= 0) return DrawGlInfo::kStatusDone;
427
428    text = refText(text, bytesCount);
429    positions = refBuffer<float>(positions, count * 2);
430    paint = refPaint(paint);
431
432    DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
433            x, y, positions, paint, totalAdvance, bounds);
434    addDrawOp(op);
435    return DrawGlInfo::kStatusDone;
436}
437
438status_t DisplayListRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
439    if (count <= 0) return DrawGlInfo::kStatusDone;
440
441    rects = refBuffer<float>(rects, count);
442    paint = refPaint(paint);
443    addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
444    return DrawGlInfo::kStatusDone;
445}
446
447void DisplayListRenderer::resetShader() {
448    addStateOp(new (alloc()) ResetShaderOp());
449}
450
451void DisplayListRenderer::setupShader(SkiaShader* shader) {
452    shader = refShader(shader);
453    addStateOp(new (alloc()) SetupShaderOp(shader));
454}
455
456void DisplayListRenderer::resetColorFilter() {
457    addStateOp(new (alloc()) ResetColorFilterOp());
458}
459
460void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
461    filter = refColorFilter(filter);
462    addStateOp(new (alloc()) SetupColorFilterOp(filter));
463}
464
465void DisplayListRenderer::resetShadow() {
466    addStateOp(new (alloc()) ResetShadowOp());
467}
468
469void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
470    addStateOp(new (alloc()) SetupShadowOp(radius, dx, dy, color));
471}
472
473void DisplayListRenderer::resetPaintFilter() {
474    addStateOp(new (alloc()) ResetPaintFilterOp());
475}
476
477void DisplayListRenderer::setupPaintFilter(int clearBits, int setBits) {
478    addStateOp(new (alloc()) SetupPaintFilterOp(clearBits, setBits));
479}
480
481void DisplayListRenderer::insertRestoreToCount() {
482    if (mRestoreSaveCount >= 0) {
483        DisplayListOp* op = new (alloc()) RestoreToCountOp(mRestoreSaveCount);
484        mDisplayListData->displayListOps.add(op);
485        mRestoreSaveCount = -1;
486    }
487}
488
489void DisplayListRenderer::insertTranslate() {
490    if (mHasTranslate) {
491        if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
492            DisplayListOp* op = new (alloc()) TranslateOp(mTranslateX, mTranslateY);
493            mDisplayListData->displayListOps.add(op);
494            mTranslateX = mTranslateY = 0.0f;
495        }
496        mHasTranslate = false;
497    }
498}
499
500void DisplayListRenderer::addStateOp(StateOp* op) {
501    addOpInternal(op);
502}
503
504void DisplayListRenderer::addDrawOp(DrawOp* op) {
505    Rect localBounds;
506    if (op->getLocalBounds(localBounds)) {
507        bool rejected = quickRejectNoScissor(localBounds.left, localBounds.top,
508                localBounds.right, localBounds.bottom);
509        op->setQuickRejected(rejected);
510    }
511    mHasDrawOps = true;
512    addOpInternal(op);
513}
514
515}; // namespace uirenderer
516}; // namespace android
517