DisplayListRenderer.cpp revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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 "DisplayListRenderer.h"
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Defines
26///////////////////////////////////////////////////////////////////////////////
27
28#define PATH_HEAP_SIZE 64
29
30///////////////////////////////////////////////////////////////////////////////
31// Helpers
32///////////////////////////////////////////////////////////////////////////////
33
34PathHeap::PathHeap(): mHeap(PATH_HEAP_SIZE * sizeof(SkPath)) {
35}
36
37PathHeap::PathHeap(SkFlattenableReadBuffer& buffer): mHeap(PATH_HEAP_SIZE * sizeof(SkPath)) {
38    int count = buffer.readS32();
39
40    mPaths.setCount(count);
41    SkPath** ptr = mPaths.begin();
42    SkPath* p = (SkPath*) mHeap.allocThrow(count * sizeof(SkPath));
43
44    for (int i = 0; i < count; i++) {
45        new (p) SkPath;
46        p->unflatten(buffer);
47        *ptr++ = p;
48        p++;
49    }
50}
51
52PathHeap::~PathHeap() {
53    SkPath** iter = mPaths.begin();
54    SkPath** stop = mPaths.end();
55    while (iter < stop) {
56        (*iter)->~SkPath();
57        iter++;
58    }
59}
60
61int PathHeap::append(const SkPath& path) {
62    SkPath* p = (SkPath*) mHeap.allocThrow(sizeof(SkPath));
63    new (p) SkPath(path);
64    *mPaths.append() = p;
65    return mPaths.count();
66}
67
68void PathHeap::flatten(SkFlattenableWriteBuffer& buffer) const {
69    int count = mPaths.count();
70
71    buffer.write32(count);
72    SkPath** iter = mPaths.begin();
73    SkPath** stop = mPaths.end();
74    while (iter < stop) {
75        (*iter)->flatten(buffer);
76        iter++;
77    }
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Display list
82///////////////////////////////////////////////////////////////////////////////
83
84DisplayList::DisplayList(const DisplayListRenderer& recorder) {
85    const SkWriter32& writer = recorder.writeStream();
86    init();
87
88    if (writer.size() == 0) {
89        return;
90    }
91
92    size_t size = writer.size();
93    void* buffer = sk_malloc_throw(size);
94    writer.flatten(buffer);
95    mReader.setMemory(buffer, size);
96
97    mRCPlayback.reset(&recorder.mRCRecorder);
98    mRCPlayback.setupBuffer(mReader);
99
100    mTFPlayback.reset(&recorder.mTFRecorder);
101    mTFPlayback.setupBuffer(mReader);
102
103    Caches& caches = Caches::getInstance();
104
105    const Vector<SkBitmap*> &bitmapResources = recorder.getBitmapResources();
106    for (size_t i = 0; i < bitmapResources.size(); i++) {
107        SkBitmap* resource = bitmapResources.itemAt(i);
108        mBitmapResources.add(resource);
109        caches.resourceCache.incrementRefcount(resource);
110    }
111
112    const Vector<SkiaShader*> &shaderResources = recorder.getShaderResources();
113    for (size_t i = 0; i < shaderResources.size(); i++) {
114        SkiaShader* resource = shaderResources.itemAt(i);
115        mShaderResources.add(resource);
116        caches.resourceCache.incrementRefcount(resource);
117    }
118
119    const Vector<SkPaint*> &paints = recorder.getPaints();
120    for (size_t i = 0; i < paints.size(); i++) {
121        mPaints.add(paints.itemAt(i));
122    }
123
124    const Vector<SkMatrix*> &matrices = recorder.getMatrices();
125    for (size_t i = 0; i < matrices.size(); i++) {
126        mMatrices.add(matrices.itemAt(i));
127    }
128
129    mPathHeap = recorder.mPathHeap;
130    mPathHeap->safeRef();
131}
132
133DisplayList::~DisplayList() {
134    sk_free((void*) mReader.base());
135
136    Caches& caches = Caches::getInstance();
137
138    for (size_t i = 0; i < mBitmapResources.size(); i++) {
139        caches.resourceCache.decrementRefcount(mBitmapResources.itemAt(i));
140    }
141    mBitmapResources.clear();
142
143    for (size_t i = 0; i < mShaderResources.size(); i++) {
144        caches.resourceCache.decrementRefcount(mShaderResources.itemAt(i));
145    }
146    mShaderResources.clear();
147
148    for (size_t i = 0; i < mPaints.size(); i++) {
149        delete mPaints.itemAt(i);
150    }
151    mPaints.clear();
152
153    for (size_t i = 0; i < mMatrices.size(); i++) {
154        delete  mMatrices.itemAt(i);
155    }
156    mMatrices.clear();
157
158    mPathHeap->safeUnref();
159}
160
161void DisplayList::init() {
162    mPathHeap = NULL;
163}
164
165void DisplayList::replay(OpenGLRenderer& renderer) {
166    TextContainer text;
167    mReader.rewind();
168
169    int saveCount = renderer.getSaveCount() - 1;
170
171    while (!mReader.eof()) {
172        int op = mReader.readInt();
173        switch (op) {
174            case AcquireContext: {
175                renderer.acquireContext();
176            }
177            break;
178            case ReleaseContext: {
179                renderer.releaseContext();
180            }
181            break;
182            case Save: {
183                renderer.save(getInt());
184            }
185            break;
186            case Restore: {
187                renderer.restore();
188            }
189            break;
190            case RestoreToCount: {
191                renderer.restoreToCount(saveCount + getInt());
192            }
193            break;
194            case SaveLayer: {
195                renderer.saveLayer(getFloat(), getFloat(), getFloat(), getFloat(),
196                        getPaint(), getInt());
197            }
198            break;
199            case SaveLayerAlpha: {
200                renderer.saveLayerAlpha(getFloat(), getFloat(), getFloat(), getFloat(),
201                        getInt(), getInt());
202            }
203            break;
204            case Translate: {
205                renderer.translate(getFloat(), getFloat());
206            }
207            break;
208            case Rotate: {
209                renderer.rotate(getFloat());
210            }
211            break;
212            case Scale: {
213                renderer.scale(getFloat(), getFloat());
214            }
215            break;
216            case SetMatrix: {
217                renderer.setMatrix(getMatrix());
218            }
219            break;
220            case ConcatMatrix: {
221                renderer.concatMatrix(getMatrix());
222            }
223            break;
224            case ClipRect: {
225                renderer.clipRect(getFloat(), getFloat(), getFloat(), getFloat(),
226                        (SkRegion::Op) getInt());
227            }
228            break;
229            case DrawBitmap: {
230                renderer.drawBitmap(getBitmap(), getFloat(), getFloat(), getPaint());
231            }
232            break;
233            case DrawBitmapMatrix: {
234                renderer.drawBitmap(getBitmap(), getMatrix(), getPaint());
235            }
236            break;
237            case DrawBitmapRect: {
238                renderer.drawBitmap(getBitmap(), getFloat(), getFloat(), getFloat(), getFloat(),
239                        getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
240            }
241            break;
242            case DrawPatch: {
243                int32_t* xDivs = NULL;
244                int32_t* yDivs = NULL;
245                uint32_t* colors = NULL;
246                uint32_t xDivsCount = 0;
247                uint32_t yDivsCount = 0;
248                int8_t numColors = 0;
249
250                SkBitmap* bitmap = getBitmap();
251
252                xDivs = getInts(xDivsCount);
253                yDivs = getInts(yDivsCount);
254                colors = getUInts(numColors);
255
256                renderer.drawPatch(bitmap, xDivs, yDivs, colors, xDivsCount, yDivsCount,
257                        numColors, getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
258            }
259            break;
260            case DrawColor: {
261                renderer.drawColor(getInt(), (SkXfermode::Mode) getInt());
262            }
263            break;
264            case DrawRect: {
265                renderer.drawRect(getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
266            }
267            break;
268            case DrawPath: {
269                renderer.drawPath(getPath(), getPaint());
270            }
271            break;
272            case DrawLines: {
273                int count = 0;
274                float* points = getFloats(count);
275                renderer.drawLines(points, count, getPaint());
276            }
277            break;
278            case DrawText: {
279                getText(&text);
280                renderer.drawText(text.text(), text.length(), getInt(),
281                        getFloat(), getFloat(), getPaint());
282            }
283            break;
284            case ResetShader: {
285                renderer.resetShader();
286            }
287            break;
288            case SetupShader: {
289                renderer.setupShader(getShader());
290            }
291            break;
292            case ResetColorFilter: {
293                renderer.resetColorFilter();
294            }
295            break;
296            case SetupColorFilter: {
297                renderer.setupColorFilter(getColorFilter());
298            }
299            break;
300            case ResetShadow: {
301                renderer.resetShadow();
302            }
303            break;
304            case SetupShadow: {
305                renderer.setupShadow(getFloat(), getFloat(), getFloat(), getInt());
306            }
307            break;
308        }
309    }
310}
311
312///////////////////////////////////////////////////////////////////////////////
313// Base structure
314///////////////////////////////////////////////////////////////////////////////
315
316DisplayListRenderer::DisplayListRenderer():
317        mHeap(HEAP_BLOCK_SIZE), mWriter(MIN_WRITER_SIZE) {
318    mPathHeap = NULL;
319}
320
321DisplayListRenderer::~DisplayListRenderer() {
322    reset();
323}
324
325void DisplayListRenderer::reset() {
326    if (mPathHeap) {
327        mPathHeap->unref();
328        mPathHeap = NULL;
329    }
330
331    mWriter.reset();
332    mHeap.reset();
333
334    mRCRecorder.reset();
335    mTFRecorder.reset();
336
337    Caches& caches = Caches::getInstance();
338    for (size_t i = 0; i < mBitmapResources.size(); i++) {
339        SkBitmap* resource = mBitmapResources.itemAt(i);
340        caches.resourceCache.decrementRefcount(resource);
341    }
342    mBitmapResources.clear();
343
344    for (size_t i = 0; i < mShaderResources.size(); i++) {
345        SkiaShader* resource = mShaderResources.itemAt(i);
346        caches.resourceCache.decrementRefcount(resource);
347    }
348    mShaderResources.clear();
349
350    mPaints.clear();
351    mPaintMap.clear();
352    mMatrices.clear();
353}
354
355///////////////////////////////////////////////////////////////////////////////
356// Operations
357///////////////////////////////////////////////////////////////////////////////
358
359void DisplayListRenderer::setViewport(int width, int height) {
360    mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
361
362    mWidth = width;
363    mHeight = height;
364}
365
366void DisplayListRenderer::prepare(bool opaque) {
367    mSnapshot = new Snapshot(mFirstSnapshot,
368            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
369    mSaveCount = 1;
370    mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
371}
372
373void DisplayListRenderer::acquireContext() {
374    addOp(DisplayList::AcquireContext);
375    OpenGLRenderer::acquireContext();
376}
377
378void DisplayListRenderer::releaseContext() {
379    addOp(DisplayList::ReleaseContext);
380    OpenGLRenderer::releaseContext();
381}
382
383int DisplayListRenderer::save(int flags) {
384    addOp(DisplayList::Save);
385    addInt(flags);
386    return OpenGLRenderer::save(flags);
387}
388
389void DisplayListRenderer::restore() {
390    addOp(DisplayList::Restore);
391    OpenGLRenderer::restore();
392}
393
394void DisplayListRenderer::restoreToCount(int saveCount) {
395    addOp(DisplayList::RestoreToCount);
396    addInt(saveCount);
397    OpenGLRenderer::restoreToCount(saveCount);
398}
399
400int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
401        SkPaint* p, int flags) {
402    addOp(DisplayList::SaveLayer);
403    addBounds(left, top, right, bottom);
404    addPaint(p);
405    addInt(flags);
406    return OpenGLRenderer::save(flags);
407}
408
409int DisplayListRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
410        int alpha, int flags) {
411    addOp(DisplayList::SaveLayerAlpha);
412    addBounds(left, top, right, bottom);
413    addInt(alpha);
414    addInt(flags);
415    return OpenGLRenderer::save(flags);
416}
417
418void DisplayListRenderer::translate(float dx, float dy) {
419    addOp(DisplayList::Translate);
420    addPoint(dx, dy);
421    OpenGLRenderer::translate(dx, dy);
422}
423
424void DisplayListRenderer::rotate(float degrees) {
425    addOp(DisplayList::Rotate);
426    addFloat(degrees);
427    OpenGLRenderer::rotate(degrees);
428}
429
430void DisplayListRenderer::scale(float sx, float sy) {
431    addOp(DisplayList::Scale);
432    addPoint(sx, sy);
433    OpenGLRenderer::scale(sx, sy);
434}
435
436void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
437    addOp(DisplayList::SetMatrix);
438    addMatrix(matrix);
439    OpenGLRenderer::setMatrix(matrix);
440}
441
442void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
443    addOp(DisplayList::ConcatMatrix);
444    addMatrix(matrix);
445    OpenGLRenderer::concatMatrix(matrix);
446}
447
448bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
449        SkRegion::Op op) {
450    addOp(DisplayList::ClipRect);
451    addBounds(left, top, right, bottom);
452    addInt(op);
453    return OpenGLRenderer::clipRect(left, top, right, bottom, op);
454}
455
456void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top,
457        SkPaint* paint) {
458    addOp(DisplayList::DrawBitmap);
459    addBitmap(bitmap);
460    addPoint(left, top);
461    addPaint(paint);
462}
463
464void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix,
465        SkPaint* paint) {
466    addOp(DisplayList::DrawBitmapMatrix);
467    addBitmap(bitmap);
468    addMatrix(matrix);
469    addPaint(paint);
470}
471
472void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
473        float srcRight, float srcBottom, float dstLeft, float dstTop,
474        float dstRight, float dstBottom, SkPaint* paint) {
475    addOp(DisplayList::DrawBitmapRect);
476    addBitmap(bitmap);
477    addBounds(srcLeft, srcTop, srcRight, srcBottom);
478    addBounds(dstLeft, dstTop, dstRight, dstBottom);
479    addPaint(paint);
480}
481
482void DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
483        const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
484        float left, float top, float right, float bottom, SkPaint* paint) {
485    addOp(DisplayList::DrawPatch);
486    addBitmap(bitmap);
487    addInts(xDivs, width);
488    addInts(yDivs, height);
489    addUInts(colors, numColors);
490    addBounds(left, top, right, bottom);
491    addPaint(paint);
492}
493
494void DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
495    addOp(DisplayList::DrawColor);
496    addInt(color);
497    addInt(mode);
498}
499
500void DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
501        SkPaint* paint) {
502    addOp(DisplayList::DrawRect);
503    addBounds(left, top, right, bottom);
504    addPaint(paint);
505}
506
507void DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
508    addOp(DisplayList::DrawPath);
509    addPath(path);
510    addPaint(paint);
511}
512
513void DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) {
514    addOp(DisplayList::DrawLines);
515    addFloats(points, count);
516    addPaint(paint);
517}
518
519void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
520        float x, float y, SkPaint* paint) {
521    addOp(DisplayList::DrawText);
522    addText(text, bytesCount);
523    addInt(count);
524    addPoint(x, y);
525    addPaint(paint);
526}
527
528void DisplayListRenderer::resetShader() {
529    addOp(DisplayList::ResetShader);
530}
531
532void DisplayListRenderer::setupShader(SkiaShader* shader) {
533    addOp(DisplayList::SetupShader);
534    addShader(shader);
535}
536
537void DisplayListRenderer::resetColorFilter() {
538    addOp(DisplayList::ResetColorFilter);
539}
540
541void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
542    addOp(DisplayList::SetupColorFilter);
543    addColorFilter(filter);
544}
545
546void DisplayListRenderer::resetShadow() {
547    addOp(DisplayList::ResetShadow);
548}
549
550void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
551    addOp(DisplayList::SetupShadow);
552    addFloat(radius);
553    addPoint(dx, dy);
554    addInt(color);
555}
556
557}; // namespace uirenderer
558}; // namespace android
559