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_HWUI_DISPLAY_LIST_RENDERER_H
18#define ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
19
20#include <SkMatrix.h>
21#include <SkPaint.h>
22#include <SkPath.h>
23#include <cutils/compiler.h>
24
25#include "DisplayList.h"
26#include "DisplayListLogBuffer.h"
27#include "OpenGLRenderer.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
36#define MIN_WRITER_SIZE 4096
37#define OP_MAY_BE_SKIPPED_MASK 0xff000000
38
39// Debug
40#if DEBUG_DISPLAY_LIST
41    #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__)
42#else
43    #define DISPLAY_LIST_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
47// Display list
48///////////////////////////////////////////////////////////////////////////////
49
50class DeferredDisplayList;
51class DisplayListRenderer;
52class DisplayListOp;
53class DrawOp;
54class StateOp;
55
56/**
57 * Records drawing commands in a display list for latter playback.
58 */
59class DisplayListRenderer: public OpenGLRenderer {
60public:
61    ANDROID_API DisplayListRenderer();
62    virtual ~DisplayListRenderer();
63
64    ANDROID_API DisplayList* getDisplayList(DisplayList* displayList);
65
66    virtual bool isDeferred();
67
68    virtual void setViewport(int width, int height);
69    virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
70    virtual void finish();
71
72    virtual status_t callDrawGLFunction(Functor *functor, Rect& dirty);
73
74    virtual void interrupt();
75    virtual void resume();
76
77    virtual int save(int flags);
78    virtual void restore();
79    virtual void restoreToCount(int saveCount);
80
81    virtual int saveLayer(float left, float top, float right, float bottom,
82            int alpha, SkXfermode::Mode mode, int flags);
83
84    virtual void translate(float dx, float dy);
85    virtual void rotate(float degrees);
86    virtual void scale(float sx, float sy);
87    virtual void skew(float sx, float sy);
88
89    virtual void setMatrix(SkMatrix* matrix);
90    virtual void concatMatrix(SkMatrix* matrix);
91
92    virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
93    virtual bool clipPath(SkPath* path, SkRegion::Op op);
94    virtual bool clipRegion(SkRegion* region, SkRegion::Op op);
95
96    virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags);
97    virtual status_t drawLayer(Layer* layer, float x, float y);
98    virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
99    virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
100    virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
101            float srcRight, float srcBottom, float dstLeft, float dstTop,
102            float dstRight, float dstBottom, SkPaint* paint);
103    virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
104    virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
105            float* vertices, int* colors, SkPaint* paint);
106    virtual status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
107            const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
108            float left, float top, float right, float bottom, SkPaint* paint);
109    virtual status_t drawColor(int color, SkXfermode::Mode mode);
110    virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint);
111    virtual status_t drawRoundRect(float left, float top, float right, float bottom,
112            float rx, float ry, SkPaint* paint);
113    virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint);
114    virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint);
115    virtual status_t drawArc(float left, float top, float right, float bottom,
116            float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
117    virtual status_t drawPath(SkPath* path, SkPaint* paint);
118    virtual status_t drawLines(float* points, int count, SkPaint* paint);
119    virtual status_t drawPoints(float* points, int count, SkPaint* paint);
120    virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
121            float hOffset, float vOffset, SkPaint* paint);
122    virtual status_t drawPosText(const char* text, int bytesCount, int count,
123            const float* positions, SkPaint* paint);
124    virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
125            const float* positions, SkPaint* paint, float length, DrawOpMode drawOpMode);
126
127    virtual status_t drawRects(const float* rects, int count, SkPaint* paint);
128
129    virtual void resetShader();
130    virtual void setupShader(SkiaShader* shader);
131
132    virtual void resetColorFilter();
133    virtual void setupColorFilter(SkiaColorFilter* filter);
134
135    virtual void resetShadow();
136    virtual void setupShadow(float radius, float dx, float dy, int color);
137
138    virtual void resetPaintFilter();
139    virtual void setupPaintFilter(int clearBits, int setBits);
140
141    ANDROID_API void reset();
142
143    sp<DisplayListData> getDisplayListData() const {
144        return mDisplayListData;
145    }
146
147    const Vector<SkBitmap*>& getBitmapResources() const {
148        return mBitmapResources;
149    }
150
151    const Vector<SkBitmap*>& getOwnedBitmapResources() const {
152        return mOwnedBitmapResources;
153    }
154
155    const Vector<SkiaColorFilter*>& getFilterResources() const {
156        return mFilterResources;
157    }
158
159    const Vector<SkiaShader*>& getShaders() const {
160        return mShaders;
161    }
162
163    const Vector<SkPaint*>& getPaints() const {
164        return mPaints;
165    }
166
167    const Vector<SkPath*>& getPaths() const {
168        return mPaths;
169    }
170
171    const SortedVector<SkPath*>& getSourcePaths() const {
172        return mSourcePaths;
173    }
174
175    const Vector<SkRegion*>& getRegions() const {
176        return mRegions;
177    }
178
179    const Vector<Layer*>& getLayers() const {
180        return mLayers;
181    }
182
183    const Vector<SkMatrix*>& getMatrices() const {
184        return mMatrices;
185    }
186
187    uint32_t getFunctorCount() const {
188        return mFunctorCount;
189    }
190
191private:
192    void insertRestoreToCount();
193    void insertTranslate();
194
195    LinearAllocator& alloc() { return mDisplayListData->allocator; }
196    void addStateOp(StateOp* op);
197    void addDrawOp(DrawOp* op);
198    void addOpInternal(DisplayListOp* op) {
199        insertRestoreToCount();
200        insertTranslate();
201        mDisplayListData->displayListOps.add(op);
202    }
203
204    template<class T>
205    inline T* refBuffer(const T* srcBuffer, int32_t count) {
206        if (srcBuffer == NULL) return NULL;
207        T* dstBuffer = (T*) mDisplayListData->allocator.alloc(count * sizeof(T));
208        memcpy(dstBuffer, srcBuffer, count * sizeof(T));
209        return dstBuffer;
210    }
211
212    inline char* refText(const char* text, size_t byteLength) {
213        return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength);
214    }
215
216    inline SkPath* refPath(SkPath* path) {
217        if (!path) return NULL;
218
219        SkPath* pathCopy = mPathMap.valueFor(path);
220        if (pathCopy == NULL || pathCopy->getGenerationID() != path->getGenerationID()) {
221            pathCopy = new SkPath(*path);
222            pathCopy->setSourcePath(path);
223            // replaceValueFor() performs an add if the entry doesn't exist
224            mPathMap.replaceValueFor(path, pathCopy);
225            mPaths.add(pathCopy);
226        }
227        if (mSourcePaths.indexOf(path) < 0) {
228            mCaches.resourceCache.incrementRefcount(path);
229            mSourcePaths.add(path);
230        }
231        return pathCopy;
232    }
233
234    inline SkPaint* refPaint(SkPaint* paint) {
235        if (!paint) {
236            return paint;
237        }
238
239        SkPaint* paintCopy = mPaintMap.valueFor(paint);
240        if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
241            paintCopy = new SkPaint(*paint);
242            // replaceValueFor() performs an add if the entry doesn't exist
243            mPaintMap.replaceValueFor(paint, paintCopy);
244            mPaints.add(paintCopy);
245        }
246
247        return paintCopy;
248    }
249
250    inline SkRegion* refRegion(SkRegion* region) {
251        if (!region) {
252            return region;
253        }
254
255        SkRegion* regionCopy = mRegionMap.valueFor(region);
256        // TODO: Add generation ID to SkRegion
257        if (regionCopy == NULL) {
258            regionCopy = new SkRegion(*region);
259            // replaceValueFor() performs an add if the entry doesn't exist
260            mRegionMap.replaceValueFor(region, regionCopy);
261            mRegions.add(regionCopy);
262        }
263
264        return regionCopy;
265    }
266
267    inline SkMatrix* refMatrix(SkMatrix* matrix) {
268        // Copying the matrix is cheap and prevents against the user changing the original
269        // matrix before the operation that uses it
270        SkMatrix* copy = new SkMatrix(*matrix);
271        mMatrices.add(copy);
272        return copy;
273    }
274
275    inline Layer* refLayer(Layer* layer) {
276        mLayers.add(layer);
277        mCaches.resourceCache.incrementRefcount(layer);
278        return layer;
279    }
280
281    inline SkBitmap* refBitmap(SkBitmap* bitmap) {
282        // Note that this assumes the bitmap is immutable. There are cases this won't handle
283        // correctly, such as creating the bitmap from scratch, drawing with it, changing its
284        // contents, and drawing again. The only fix would be to always copy it the first time,
285        // which doesn't seem worth the extra cycles for this unlikely case.
286        mBitmapResources.add(bitmap);
287        mCaches.resourceCache.incrementRefcount(bitmap);
288        return bitmap;
289    }
290
291    inline SkBitmap* refBitmapData(SkBitmap* bitmap) {
292        mOwnedBitmapResources.add(bitmap);
293        mCaches.resourceCache.incrementRefcount(bitmap);
294        return bitmap;
295    }
296
297    inline SkiaShader* refShader(SkiaShader* shader) {
298        if (!shader) return NULL;
299
300        SkiaShader* shaderCopy = mShaderMap.valueFor(shader);
301        // TODO: We also need to handle generation ID changes in compose shaders
302        if (shaderCopy == NULL || shaderCopy->getGenerationId() != shader->getGenerationId()) {
303            shaderCopy = shader->copy();
304            // replaceValueFor() performs an add if the entry doesn't exist
305            mShaderMap.replaceValueFor(shader, shaderCopy);
306            mShaders.add(shaderCopy);
307            mCaches.resourceCache.incrementRefcount(shaderCopy);
308        }
309        return shaderCopy;
310    }
311
312    inline SkiaColorFilter* refColorFilter(SkiaColorFilter* colorFilter) {
313        mFilterResources.add(colorFilter);
314        mCaches.resourceCache.incrementRefcount(colorFilter);
315        return colorFilter;
316    }
317
318    Vector<SkBitmap*> mBitmapResources;
319    Vector<SkBitmap*> mOwnedBitmapResources;
320    Vector<SkiaColorFilter*> mFilterResources;
321
322    Vector<SkPaint*> mPaints;
323    DefaultKeyedVector<SkPaint*, SkPaint*> mPaintMap;
324
325    Vector<SkPath*> mPaths;
326    DefaultKeyedVector<SkPath*, SkPath*> mPathMap;
327
328    SortedVector<SkPath*> mSourcePaths;
329
330    Vector<SkRegion*> mRegions;
331    DefaultKeyedVector<SkRegion*, SkRegion*> mRegionMap;
332
333    Vector<SkiaShader*> mShaders;
334    DefaultKeyedVector<SkiaShader*, SkiaShader*> mShaderMap;
335
336    Vector<SkMatrix*> mMatrices;
337
338    Vector<Layer*> mLayers;
339
340    int mRestoreSaveCount;
341
342    Caches& mCaches;
343    sp<DisplayListData> mDisplayListData;
344
345    float mTranslateX;
346    float mTranslateY;
347    bool mHasTranslate;
348    bool mHasDrawOps;
349
350    uint32_t mFunctorCount;
351
352    friend class DisplayList;
353
354}; // class DisplayListRenderer
355
356}; // namespace uirenderer
357}; // namespace android
358
359#endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
360