DisplayListRenderer.h revision 5c13d89c1332fcc499379b9064b891187b75ca32
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#ifndef ANDROID_UI_DISPLAY_LIST_RENDERER_H
18#define ANDROID_UI_DISPLAY_LIST_RENDERER_H
19
20#include <SkChunkAlloc.h>
21#include <SkFlattenable.h>
22#include <SkMatrix.h>
23#include <SkPaint.h>
24#include <SkPath.h>
25#include <SkPictureFlat.h>
26#include <SkRefCnt.h>
27#include <SkTDArray.h>
28#include <SkTSearch.h>
29
30#include "OpenGLRenderer.h"
31
32namespace android {
33namespace uirenderer {
34
35///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
39#define MIN_WRITER_SIZE 16384
40#define HEAP_BLOCK_SIZE 4096
41
42///////////////////////////////////////////////////////////////////////////////
43// Helpers
44///////////////////////////////////////////////////////////////////////////////
45
46class PathHeap: public SkRefCnt {
47public:
48    PathHeap();
49    PathHeap(SkFlattenableReadBuffer& buffer);
50    ~PathHeap();
51
52    int append(const SkPath& path);
53
54    int count() const { return mPaths.count(); }
55
56    SkPath& operator[](int index) const {
57        return *mPaths[index];
58    }
59
60    void flatten(SkFlattenableWriteBuffer& buffer) const;
61
62private:
63    SkChunkAlloc mHeap;
64    SkTDArray<SkPath*> mPaths;
65};
66
67///////////////////////////////////////////////////////////////////////////////
68// Display list
69///////////////////////////////////////////////////////////////////////////////
70
71class DisplayListRenderer;
72
73/**
74 * Replays recorded drawing commands.
75 */
76class DisplayList {
77public:
78    DisplayList(const DisplayListRenderer& recorder);
79    ~DisplayList();
80
81    enum Op {
82        AcquireContext,
83        ReleaseContext,
84        Save,
85        Restore,
86        RestoreToCount,
87        SaveLayer,
88        Translate,
89        Rotate,
90        Scale,
91        SetMatrix,
92        ConcatMatrix,
93        ClipRect,
94        DrawBitmap,
95        DrawBitmapMatrix,
96        DrawBitmapRect,
97        DrawPatch,
98        DrawColor,
99        DrawRect,
100        DrawPath,
101        DrawLines,
102        DrawText,
103        ResetShader,
104        SetupShader,
105        ResetColorFilter,
106        SetupColorFilter,
107        ResetShadow,
108        SetupShadow
109    };
110
111    void replay(OpenGLRenderer& renderer);
112
113private:
114    void init();
115
116    class TextContainer {
117    public:
118        size_t length() const {
119            return mByteLength;
120        }
121
122        const char* text() const {
123            return (const char*) mText;
124        }
125
126        size_t mByteLength;
127        const char* mText;
128    };
129
130    SkBitmap* getBitmap() {
131        return (SkBitmap*) getInt();
132    }
133
134    SkiaShader* getShader() {
135        return (SkiaShader*) getInt();
136    }
137
138    inline int getIndex() {
139        return mReader.readInt();
140    }
141
142    inline int getInt() {
143        return mReader.readInt();
144    }
145
146    SkMatrix* getMatrix() {
147        return (SkMatrix*) getInt();
148    }
149
150    SkPath* getPath() {
151        return &(*mPathHeap)[getInt() - 1];
152    }
153
154    SkPaint* getPaint() {
155        return (SkPaint*) getInt();
156    }
157
158    inline float getFloat() {
159        return mReader.readScalar();
160    }
161
162    int32_t* getInts(uint32_t& count) {
163        count = getInt();
164        return (int32_t*) mReader.skip(count * sizeof(int32_t));
165    }
166
167    uint32_t* getUInts(int8_t& count) {
168        count = getInt();
169        return (uint32_t*) mReader.skip(count * sizeof(uint32_t));
170    }
171
172    float* getFloats(int& count) {
173        count = getInt();
174        return (float*) mReader.skip(count * sizeof(float));
175    }
176
177    void getText(TextContainer* text) {
178        size_t length = text->mByteLength = getInt();
179        text->mText = (const char*) mReader.skip(length);
180    }
181
182    PathHeap* mPathHeap;
183
184    Vector<SkBitmap*> mBitmapResources;
185    Vector<SkMatrix*> mMatrixResources;
186    Vector<SkPaint*> mPaintResources;
187    Vector<SkiaShader*> mShaderResources;
188
189    mutable SkFlattenableReadBuffer mReader;
190
191    SkRefCntPlayback mRCPlayback;
192    SkTypefacePlayback mTFPlayback;
193};
194
195///////////////////////////////////////////////////////////////////////////////
196// Renderer
197///////////////////////////////////////////////////////////////////////////////
198
199/**
200 * Records drawing commands in a display list for latter playback.
201 */
202class DisplayListRenderer: public OpenGLRenderer {
203public:
204    DisplayListRenderer();
205    ~DisplayListRenderer();
206
207    void setViewport(int width, int height);
208    void prepare(bool opaque);
209
210    void acquireContext();
211    void releaseContext();
212
213    int save(int flags);
214    void restore();
215    void restoreToCount(int saveCount);
216
217    int saveLayer(float left, float top, float right, float bottom,
218            SkPaint* p, int flags);
219
220    void translate(float dx, float dy);
221    void rotate(float degrees);
222    void scale(float sx, float sy);
223
224    void setMatrix(SkMatrix* matrix);
225    void concatMatrix(SkMatrix* matrix);
226
227    bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
228
229    void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
230    void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
231    void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
232            float srcRight, float srcBottom, float dstLeft, float dstTop,
233            float dstRight, float dstBottom, SkPaint* paint);
234    void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
235            const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
236            float left, float top, float right, float bottom, SkPaint* paint);
237    void drawColor(int color, SkXfermode::Mode mode);
238    void drawRect(float left, float top, float right, float bottom, SkPaint* paint);
239    void drawPath(SkPath* path, SkPaint* paint);
240    void drawLines(float* points, int count, SkPaint* paint);
241    void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint);
242
243    void resetShader();
244    void setupShader(SkiaShader* shader);
245
246    void resetColorFilter();
247    void setupColorFilter(SkiaColorFilter* filter);
248
249    void resetShadow();
250    void setupShadow(float radius, float dx, float dy, int color);
251
252    void reset();
253
254    DisplayList* getDisplayList() const {
255        return new DisplayList(*this);
256    }
257
258    const SkWriter32& writeStream() const {
259        return mWriter;
260    }
261
262    const Vector<SkBitmap*>& getBitmapResources() const {
263        return mBitmapResources;
264    }
265
266    const Vector<SkMatrix*>& getMatrixResources() const {
267        return mMatrixResources;
268    }
269
270    const Vector<SkPaint*>& getPaintResources() const {
271        return mPaintResources;
272    }
273
274    const Vector<SkiaShader*>& getShaderResources() const {
275        return mShaderResources;
276    }
277
278private:
279    inline void addOp(DisplayList::Op drawOp) {
280        mWriter.writeInt(drawOp);
281    }
282
283    inline void addInt(int value) {
284        mWriter.writeInt(value);
285    }
286
287    void addInts(const int32_t* values, uint32_t count) {
288        mWriter.writeInt(count);
289        for (uint32_t i = 0; i < count; i++) {
290            mWriter.writeInt(values[i]);
291        }
292    }
293
294    void addUInts(const uint32_t* values, int8_t count) {
295        mWriter.writeInt(count);
296        for (int8_t i = 0; i < count; i++) {
297            mWriter.writeInt(values[i]);
298        }
299    }
300
301    inline void addFloat(float value) {
302        mWriter.writeScalar(value);
303    }
304
305    void addFloats(const float* values, int count) {
306        mWriter.writeInt(count);
307        for (int i = 0; i < count; i++) {
308            mWriter.writeScalar(values[i]);
309        }
310    }
311
312    inline void addPoint(float x, float y) {
313        mWriter.writeScalar(x);
314        mWriter.writeScalar(y);
315    }
316
317    inline void addBounds(float left, float top, float right, float bottom) {
318        mWriter.writeScalar(left);
319        mWriter.writeScalar(top);
320        mWriter.writeScalar(right);
321        mWriter.writeScalar(bottom);
322    }
323
324    inline void addText(const void* text, size_t byteLength) {
325        mWriter.writeInt(byteLength);
326        mWriter.writePad(text, byteLength);
327    }
328
329    inline void addPath(const SkPath* path) {
330        if (mPathHeap == NULL) {
331            mPathHeap = new PathHeap();
332        }
333        addInt(mPathHeap->append(*path));
334    }
335
336    inline void addPaint(SkPaint* paint) {
337        addInt((int)paint);
338        mPaintResources.add(paint);
339        Caches& caches = Caches::getInstance();
340        caches.resourceCache.incrementRefcount(paint);
341    }
342
343    inline void addMatrix(SkMatrix* matrix) {
344        addInt((int)matrix);
345        mMatrixResources.add(matrix);
346        Caches& caches = Caches::getInstance();
347        caches.resourceCache.incrementRefcount(matrix);
348    }
349
350    inline void addBitmap(SkBitmap* bitmap) {
351        addInt((int)bitmap);
352        mBitmapResources.add(bitmap);
353        Caches& caches = Caches::getInstance();
354        caches.resourceCache.incrementRefcount(bitmap);
355    }
356
357    inline void addShader(SkiaShader* shader) {
358        addInt((int)shader);
359        mShaderResources.add(shader);
360        Caches& caches = Caches::getInstance();
361        caches.resourceCache.incrementRefcount(shader);
362    }
363
364    SkChunkAlloc mHeap;
365
366    Vector<SkBitmap*> mBitmapResources;
367    Vector<SkMatrix*> mMatrixResources;
368    Vector<SkPaint*> mPaintResources;
369    Vector<SkiaShader*> mShaderResources;
370
371    PathHeap* mPathHeap;
372    SkWriter32 mWriter;
373
374    SkRefCntRecorder mRCRecorder;
375    SkRefCntRecorder mTFRecorder;
376
377    friend class DisplayList;
378
379}; // class DisplayListRenderer
380
381}; // namespace uirenderer
382}; // namespace android
383
384#endif // ANDROID_UI_DISPLAY_LIST_RENDERER_H
385