• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 namespace android {
30 namespace 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 
50 class DeferredDisplayList;
51 class DisplayListRenderer;
52 class DisplayListOp;
53 class DrawOp;
54 class StateOp;
55 
56 /**
57  * Records drawing commands in a display list for latter playback.
58  */
59 class DisplayListRenderer: public OpenGLRenderer {
60 public:
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, Res_png_9patch* patch,
107             float left, float top, float right, float bottom, SkPaint* paint);
108     virtual status_t drawColor(int color, SkXfermode::Mode mode);
109     virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint);
110     virtual status_t drawRoundRect(float left, float top, float right, float bottom,
111             float rx, float ry, SkPaint* paint);
112     virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint);
113     virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint);
114     virtual status_t drawArc(float left, float top, float right, float bottom,
115             float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
116     virtual status_t drawPath(SkPath* path, SkPaint* paint);
117     virtual status_t drawLines(float* points, int count, SkPaint* paint);
118     virtual status_t drawPoints(float* points, int count, SkPaint* paint);
119     virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
120             float hOffset, float vOffset, SkPaint* paint);
121     virtual status_t drawPosText(const char* text, int bytesCount, int count,
122             const float* positions, SkPaint* paint);
123     virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
124             const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
125             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 
getDisplayListData()143     sp<DisplayListData> getDisplayListData() const {
144         return mDisplayListData;
145     }
146 
getBitmapResources()147     const Vector<SkBitmap*>& getBitmapResources() const {
148         return mBitmapResources;
149     }
150 
getOwnedBitmapResources()151     const Vector<SkBitmap*>& getOwnedBitmapResources() const {
152         return mOwnedBitmapResources;
153     }
154 
getFilterResources()155     const Vector<SkiaColorFilter*>& getFilterResources() const {
156         return mFilterResources;
157     }
158 
getPatchResources()159     const Vector<Res_png_9patch*>& getPatchResources() const {
160         return mPatchResources;
161     }
162 
getShaders()163     const Vector<SkiaShader*>& getShaders() const {
164         return mShaders;
165     }
166 
getPaints()167     const Vector<SkPaint*>& getPaints() const {
168         return mPaints;
169     }
170 
getPaths()171     const Vector<SkPath*>& getPaths() const {
172         return mPaths;
173     }
174 
getSourcePaths()175     const SortedVector<SkPath*>& getSourcePaths() const {
176         return mSourcePaths;
177     }
178 
getRegions()179     const Vector<SkRegion*>& getRegions() const {
180         return mRegions;
181     }
182 
getLayers()183     const Vector<Layer*>& getLayers() const {
184         return mLayers;
185     }
186 
getMatrices()187     const Vector<SkMatrix*>& getMatrices() const {
188         return mMatrices;
189     }
190 
getFunctorCount()191     uint32_t getFunctorCount() const {
192         return mFunctorCount;
193     }
194 
195 private:
196     void insertRestoreToCount();
197     void insertTranslate();
198 
alloc()199     LinearAllocator& alloc() { return mDisplayListData->allocator; }
200     void addStateOp(StateOp* op);
201     void addDrawOp(DrawOp* op);
addOpInternal(DisplayListOp * op)202     void addOpInternal(DisplayListOp* op) {
203         insertRestoreToCount();
204         insertTranslate();
205         mDisplayListData->displayListOps.add(op);
206     }
207 
208     template<class T>
refBuffer(const T * srcBuffer,int32_t count)209     inline T* refBuffer(const T* srcBuffer, int32_t count) {
210         if (srcBuffer == NULL) return NULL;
211         T* dstBuffer = (T*) mDisplayListData->allocator.alloc(count * sizeof(T));
212         memcpy(dstBuffer, srcBuffer, count * sizeof(T));
213         return dstBuffer;
214     }
215 
refText(const char * text,size_t byteLength)216     inline char* refText(const char* text, size_t byteLength) {
217         return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength);
218     }
219 
refPath(SkPath * path)220     inline SkPath* refPath(SkPath* path) {
221         if (!path) return NULL;
222 
223         SkPath* pathCopy = mPathMap.valueFor(path);
224         if (pathCopy == NULL || pathCopy->getGenerationID() != path->getGenerationID()) {
225             pathCopy = new SkPath(*path);
226             pathCopy->setSourcePath(path);
227             // replaceValueFor() performs an add if the entry doesn't exist
228             mPathMap.replaceValueFor(path, pathCopy);
229             mPaths.add(pathCopy);
230         }
231         if (mSourcePaths.indexOf(path) < 0) {
232             mCaches.resourceCache.incrementRefcount(path);
233             mSourcePaths.add(path);
234         }
235         return pathCopy;
236     }
237 
refPaint(SkPaint * paint)238     inline SkPaint* refPaint(SkPaint* paint) {
239         if (!paint) {
240             return paint;
241         }
242 
243         SkPaint* paintCopy = mPaintMap.valueFor(paint);
244         if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
245             paintCopy = new SkPaint(*paint);
246             // replaceValueFor() performs an add if the entry doesn't exist
247             mPaintMap.replaceValueFor(paint, paintCopy);
248             mPaints.add(paintCopy);
249         }
250 
251         return paintCopy;
252     }
253 
refRegion(SkRegion * region)254     inline SkRegion* refRegion(SkRegion* region) {
255         if (!region) {
256             return region;
257         }
258 
259         SkRegion* regionCopy = mRegionMap.valueFor(region);
260         // TODO: Add generation ID to SkRegion
261         if (regionCopy == NULL) {
262             regionCopy = new SkRegion(*region);
263             // replaceValueFor() performs an add if the entry doesn't exist
264             mRegionMap.replaceValueFor(region, regionCopy);
265             mRegions.add(regionCopy);
266         }
267 
268         return regionCopy;
269     }
270 
refMatrix(SkMatrix * matrix)271     inline SkMatrix* refMatrix(SkMatrix* matrix) {
272         if (matrix) {
273             // Copying the matrix is cheap and prevents against the user changing
274             // the original matrix before the operation that uses it
275             SkMatrix* copy = new SkMatrix(*matrix);
276             mMatrices.add(copy);
277             return copy;
278         }
279         return matrix;
280     }
281 
refLayer(Layer * layer)282     inline Layer* refLayer(Layer* layer) {
283         mLayers.add(layer);
284         mCaches.resourceCache.incrementRefcount(layer);
285         return layer;
286     }
287 
refBitmap(SkBitmap * bitmap)288     inline SkBitmap* refBitmap(SkBitmap* bitmap) {
289         // Note that this assumes the bitmap is immutable. There are cases this won't handle
290         // correctly, such as creating the bitmap from scratch, drawing with it, changing its
291         // contents, and drawing again. The only fix would be to always copy it the first time,
292         // which doesn't seem worth the extra cycles for this unlikely case.
293         mBitmapResources.add(bitmap);
294         mCaches.resourceCache.incrementRefcount(bitmap);
295         return bitmap;
296     }
297 
refBitmapData(SkBitmap * bitmap)298     inline SkBitmap* refBitmapData(SkBitmap* bitmap) {
299         mOwnedBitmapResources.add(bitmap);
300         mCaches.resourceCache.incrementRefcount(bitmap);
301         return bitmap;
302     }
303 
refShader(SkiaShader * shader)304     inline SkiaShader* refShader(SkiaShader* shader) {
305         if (!shader) return NULL;
306 
307         SkiaShader* shaderCopy = mShaderMap.valueFor(shader);
308         // TODO: We also need to handle generation ID changes in compose shaders
309         if (shaderCopy == NULL || shaderCopy->getGenerationId() != shader->getGenerationId()) {
310             shaderCopy = shader->copy();
311             // replaceValueFor() performs an add if the entry doesn't exist
312             mShaderMap.replaceValueFor(shader, shaderCopy);
313             mShaders.add(shaderCopy);
314             mCaches.resourceCache.incrementRefcount(shaderCopy);
315         }
316         return shaderCopy;
317     }
318 
refColorFilter(SkiaColorFilter * colorFilter)319     inline SkiaColorFilter* refColorFilter(SkiaColorFilter* colorFilter) {
320         mFilterResources.add(colorFilter);
321         mCaches.resourceCache.incrementRefcount(colorFilter);
322         return colorFilter;
323     }
324 
refPatch(Res_png_9patch * patch)325     inline Res_png_9patch* refPatch(Res_png_9patch* patch) {
326         mPatchResources.add(patch);
327         mCaches.resourceCache.incrementRefcount(patch);
328         return patch;
329     }
330 
331     Vector<SkBitmap*> mBitmapResources;
332     Vector<SkBitmap*> mOwnedBitmapResources;
333     Vector<SkiaColorFilter*> mFilterResources;
334     Vector<Res_png_9patch*> mPatchResources;
335 
336     Vector<SkPaint*> mPaints;
337     DefaultKeyedVector<SkPaint*, SkPaint*> mPaintMap;
338 
339     Vector<SkPath*> mPaths;
340     DefaultKeyedVector<SkPath*, SkPath*> mPathMap;
341 
342     SortedVector<SkPath*> mSourcePaths;
343 
344     Vector<SkRegion*> mRegions;
345     DefaultKeyedVector<SkRegion*, SkRegion*> mRegionMap;
346 
347     Vector<SkiaShader*> mShaders;
348     DefaultKeyedVector<SkiaShader*, SkiaShader*> mShaderMap;
349 
350     Vector<SkMatrix*> mMatrices;
351 
352     Vector<Layer*> mLayers;
353 
354     int mRestoreSaveCount;
355 
356     Caches& mCaches;
357     sp<DisplayListData> mDisplayListData;
358 
359     float mTranslateX;
360     float mTranslateY;
361     bool mHasTranslate;
362     bool mHasDrawOps;
363 
364     uint32_t mFunctorCount;
365 
366     friend class DisplayList;
367 
368 }; // class DisplayListRenderer
369 
370 }; // namespace uirenderer
371 }; // namespace android
372 
373 #endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
374