OSDN Git Service

Merge "Ensure Dequeue/Queue duration is always set" into nyc-mr1-dev
[android-x86/frameworks-base.git] / libs / hwui / DisplayListCanvas.h
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 "CanvasState.h"
21 #include "DisplayList.h"
22 #include "RenderNode.h"
23 #include "ResourceCache.h"
24 #include "SkiaCanvasProxy.h"
25 #include "hwui/Canvas.h"
26 #include "utils/Macros.h"
27
28 #include <SkDrawFilter.h>
29 #include <SkMatrix.h>
30 #include <SkPaint.h>
31 #include <SkPath.h>
32 #include <SkRegion.h>
33 #include <SkTLazy.h>
34 #include <cutils/compiler.h>
35
36 namespace android {
37 namespace uirenderer {
38
39 ///////////////////////////////////////////////////////////////////////////////
40 // Defines
41 ///////////////////////////////////////////////////////////////////////////////
42
43 // Debug
44 #if DEBUG_DISPLAY_LIST
45     #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__)
46 #else
47     #define DISPLAY_LIST_LOGD(...)
48 #endif
49
50 ///////////////////////////////////////////////////////////////////////////////
51 // Display list
52 ///////////////////////////////////////////////////////////////////////////////
53
54 class DeferredDisplayList;
55 class DeferredLayerUpdater;
56 class DisplayListOp;
57 class DrawOp;
58 class DrawRenderNodeOp;
59 class RenderNode;
60 class StateOp;
61
62 /**
63  * Records drawing commands in a display list for later playback into an OpenGLRenderer.
64  */
65 class ANDROID_API DisplayListCanvas: public Canvas, public CanvasStateClient {
66 public:
67     DisplayListCanvas(int width, int height);
68     virtual ~DisplayListCanvas();
69
70     virtual void resetRecording(int width, int height) override;
71     virtual WARN_UNUSED_RESULT DisplayList* finishRecording() override;
72
73 // ----------------------------------------------------------------------------
74 // HWUI Canvas state operations
75 // ----------------------------------------------------------------------------
76
77     virtual void insertReorderBarrier(bool enableReorder) override;
78
79 // ----------------------------------------------------------------------------
80 // HWUI Canvas draw operations
81 // ----------------------------------------------------------------------------
82
83     // Shapes
84     virtual void drawRoundRect(CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
85                 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
86                 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
87                 CanvasPropertyPaint* paint) override;
88     virtual void drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
89                 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) override;
90
91 // ----------------------------------------------------------------------------
92 // HWUI Canvas draw operations - special
93 // ----------------------------------------------------------------------------
94     virtual void drawLayer(DeferredLayerUpdater* layerHandle) override;
95     virtual void drawRenderNode(RenderNode* renderNode) override;
96     virtual void callDrawGLFunction(Functor* functor,
97             GlFunctorLifecycleListener* listener) override;
98
99 // ----------------------------------------------------------------------------
100 // CanvasStateClient interface
101 // ----------------------------------------------------------------------------
102     virtual void onViewportInitialized() override { }
103     virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override { }
104     virtual GLuint getTargetFbo() const override { return -1; }
105
106 // ----------------------------------------------------------------------------
107 // android/graphics/Canvas interface
108 // ----------------------------------------------------------------------------
109     virtual SkCanvas* asSkCanvas() override;
110
111     virtual void setBitmap(const SkBitmap& bitmap) override {
112         LOG_ALWAYS_FATAL("DisplayListCanvas is not backed by a bitmap.");
113     }
114
115     virtual bool isOpaque() override { return false; }
116     virtual int width() override { return mState.getWidth(); }
117     virtual int height() override { return mState.getHeight(); }
118
119     virtual void setHighContrastText(bool highContrastText) override {
120         mHighContrastText = highContrastText;
121     }
122     virtual bool isHighContrastText() override { return mHighContrastText; }
123
124 // ----------------------------------------------------------------------------
125 // android/graphics/Canvas state operations
126 // ----------------------------------------------------------------------------
127     // Save (layer)
128     virtual int getSaveCount() const override { return mState.getSaveCount(); }
129     virtual int save(SaveFlags::Flags flags) override;
130     virtual void restore() override;
131     virtual void restoreToCount(int saveCount) override;
132
133     virtual int saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
134         SaveFlags::Flags flags) override;
135     virtual int saveLayerAlpha(float left, float top, float right, float bottom,
136             int alpha, SaveFlags::Flags flags) override {
137         SkPaint paint;
138         paint.setAlpha(alpha);
139         return saveLayer(left, top, right, bottom, &paint, flags);
140     }
141
142     // Matrix
143     virtual void getMatrix(SkMatrix* outMatrix) const override { mState.getMatrix(outMatrix); }
144     virtual void setMatrix(const SkMatrix& matrix) override;
145
146     virtual void concat(const SkMatrix& matrix) override;
147     virtual void rotate(float degrees) override;
148     virtual void scale(float sx, float sy) override;
149     virtual void skew(float sx, float sy) override;
150     virtual void translate(float dx, float dy) override;
151
152     // Clip
153     virtual bool getClipBounds(SkRect* outRect) const override;
154     virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
155     virtual bool quickRejectPath(const SkPath& path) const override;
156
157     virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op) override;
158     virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
159     virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
160
161     // Misc
162     virtual SkDrawFilter* getDrawFilter() override { return mDrawFilter.get(); }
163     virtual void setDrawFilter(SkDrawFilter* filter) override;
164
165 // ----------------------------------------------------------------------------
166 // android/graphics/Canvas draw operations
167 // ----------------------------------------------------------------------------
168     virtual void drawColor(int color, SkXfermode::Mode mode) override;
169     virtual void drawPaint(const SkPaint& paint) override;
170
171     // Geometry
172     virtual void drawPoint(float x, float y, const SkPaint& paint) override {
173         float points[2] = { x, y };
174         drawPoints(points, 2, paint);
175     }
176     virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
177     virtual void drawLine(float startX, float startY, float stopX, float stopY,
178             const SkPaint& paint) override {
179         float points[4] = { startX, startY, stopX, stopY };
180         drawLines(points, 4, paint);
181     }
182     virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
183     virtual void drawRect(float left, float top, float right, float bottom, const SkPaint& paint) override;
184     virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
185     virtual void drawRoundRect(float left, float top, float right, float bottom,
186             float rx, float ry, const SkPaint& paint) override;
187     virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
188     virtual void drawOval(float left, float top, float right, float bottom, const SkPaint& paint) override;
189     virtual void drawArc(float left, float top, float right, float bottom,
190             float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
191     virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
192     virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
193             const float* verts, const float* tex, const int* colors,
194             const uint16_t* indices, int indexCount, const SkPaint& paint) override
195         { /* DisplayListCanvas does not support drawVertices(); ignore */ }
196
197     // Bitmap-based
198     virtual void drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) override;
199     virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
200                             const SkPaint* paint) override;
201     virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
202             float srcRight, float srcBottom, float dstLeft, float dstTop,
203             float dstRight, float dstBottom, const SkPaint* paint) override;
204     virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
205             const float* vertices, const int* colors, const SkPaint* paint) override;
206     virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
207             float dstLeft, float dstTop, float dstRight, float dstBottom,
208             const SkPaint* paint) override;
209
210     virtual void drawVectorDrawable(VectorDrawableRoot* tree) override;
211
212     // Text
213     virtual void drawGlyphs(const uint16_t* glyphs, const float* positions, int count,
214             const SkPaint& paint, float x, float y, float boundsLeft, float boundsTop,
215             float boundsRight, float boundsBottom, float totalAdvance) override;
216     virtual void drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path,
217             float hOffset, float vOffset, const SkPaint& paint) override;
218     virtual bool drawTextAbsolutePos() const override { return false; }
219
220 private:
221
222     CanvasState mState;
223     std::unique_ptr<SkiaCanvasProxy> mSkiaCanvasProxy;
224
225     enum DeferredBarrierType {
226         kBarrier_None,
227         kBarrier_InOrder,
228         kBarrier_OutOfOrder,
229     };
230
231     void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint);
232     void drawRects(const float* rects, int count, const SkPaint* paint);
233
234     void flushRestoreToCount();
235     void flushTranslate();
236     void flushReorderBarrier();
237
238     LinearAllocator& alloc() { return mDisplayList->allocator; }
239
240     // Each method returns final index of op
241     size_t addOpAndUpdateChunk(DisplayListOp* op);
242     // flushes any deferred operations, and appends the op
243     size_t flushAndAddOp(DisplayListOp* op);
244
245     size_t addStateOp(StateOp* op);
246     size_t addDrawOp(DrawOp* op);
247     size_t addRenderNodeOp(DrawRenderNodeOp* op);
248
249     void refBitmapsInShader(const SkShader* shader);
250
251     template<class T>
252     inline const T* refBuffer(const T* srcBuffer, int32_t count) {
253         if (!srcBuffer) return nullptr;
254
255         T* dstBuffer = (T*) mDisplayList->allocator.alloc<T>(count * sizeof(T));
256         memcpy(dstBuffer, srcBuffer, count * sizeof(T));
257         return dstBuffer;
258     }
259
260     inline const SkPath* refPath(const SkPath* path) {
261         if (!path) return nullptr;
262
263         // The points/verbs within the path are refcounted so this copy operation
264         // is inexpensive and maintains the generationID of the original path.
265         const SkPath* cachedPath = new SkPath(*path);
266         mDisplayList->pathResources.push_back(cachedPath);
267         return cachedPath;
268     }
269
270     inline const SkPaint* refPaint(const SkPaint* paint) {
271         if (!paint) return nullptr;
272
273         // If there is a draw filter apply it here and store the modified paint
274         // so that we don't need to modify the paint every time we access it.
275         SkTLazy<SkPaint> filteredPaint;
276         if (mDrawFilter.get()) {
277             filteredPaint.set(*paint);
278             mDrawFilter->filter(filteredPaint.get(), SkDrawFilter::kPaint_Type);
279             paint = filteredPaint.get();
280         }
281
282         // compute the hash key for the paint and check the cache.
283         const uint32_t key = paint->getHash();
284         const SkPaint* cachedPaint = mPaintMap.valueFor(key);
285         // In the unlikely event that 2 unique paints have the same hash we do a
286         // object equality check to ensure we don't erroneously dedup them.
287         if (cachedPaint == nullptr || *cachedPaint != *paint) {
288             cachedPaint = new SkPaint(*paint);
289             std::unique_ptr<const SkPaint> copy(cachedPaint);
290             mDisplayList->paints.push_back(std::move(copy));
291
292             // replaceValueFor() performs an add if the entry doesn't exist
293             mPaintMap.replaceValueFor(key, cachedPaint);
294             refBitmapsInShader(cachedPaint->getShader());
295         }
296
297         return cachedPaint;
298     }
299
300     inline const SkRegion* refRegion(const SkRegion* region) {
301         if (!region) {
302             return region;
303         }
304
305         const SkRegion* cachedRegion = mRegionMap.valueFor(region);
306         // TODO: Add generation ID to SkRegion
307         if (cachedRegion == nullptr) {
308             std::unique_ptr<const SkRegion> copy(new SkRegion(*region));
309             cachedRegion = copy.get();
310             mDisplayList->regions.push_back(std::move(copy));
311
312             // replaceValueFor() performs an add if the entry doesn't exist
313             mRegionMap.replaceValueFor(region, cachedRegion);
314         }
315
316         return cachedRegion;
317     }
318
319     inline const SkBitmap* refBitmap(const SkBitmap& bitmap) {
320         // Note that this assumes the bitmap is immutable. There are cases this won't handle
321         // correctly, such as creating the bitmap from scratch, drawing with it, changing its
322         // contents, and drawing again. The only fix would be to always copy it the first time,
323         // which doesn't seem worth the extra cycles for this unlikely case.
324         SkBitmap* localBitmap = alloc().create<SkBitmap>(bitmap);
325         mDisplayList->bitmapResources.push_back(localBitmap);
326         return localBitmap;
327     }
328
329     inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) {
330         mDisplayList->patchResources.push_back(patch);
331         mResourceCache.incrementRefcount(patch);
332         return patch;
333     }
334
335     DefaultKeyedVector<uint32_t, const SkPaint*> mPaintMap;
336     DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap;
337     DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap;
338
339     ResourceCache& mResourceCache;
340     DisplayList* mDisplayList;
341
342     float mTranslateX;
343     float mTranslateY;
344     bool mHasDeferredTranslate;
345     DeferredBarrierType mDeferredBarrierType;
346     bool mHighContrastText;
347
348     int mRestoreSaveCount;
349
350     SkAutoTUnref<SkDrawFilter> mDrawFilter;
351
352     friend class RenderNode;
353
354 }; // class DisplayListCanvas
355
356 }; // namespace uirenderer
357 }; // namespace android
358
359 #endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H