blob: 7350082bdad7d543d92cb172571085feb59e1e34 [file] [log] [blame]
Romain Guy4aa90572010-09-26 18:40:37 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
18#define ANDROID_HWUI_DISPLAY_LIST_RENDERER_H
Romain Guy4aa90572010-09-26 18:40:37 -070019
Romain Guy4aa90572010-09-26 18:40:37 -070020#include <SkMatrix.h>
21#include <SkPaint.h>
22#include <SkPath.h>
Romain Guy79537452011-10-12 13:48:51 -070023#include <cutils/compiler.h>
24
Chet Haase9c1e23b2011-03-24 10:51:31 -070025#include "DisplayListLogBuffer.h"
John Reck113e0822014-03-18 09:22:59 -070026#include "RenderNode.h"
Romain Guy4aa90572010-09-26 18:40:37 -070027
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Defines
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guyffac7fc2011-01-13 17:21:49 -080035// Debug
36#if DEBUG_DISPLAY_LIST
Steve Block5baa3a62011-12-20 16:23:08 +000037 #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyffac7fc2011-01-13 17:21:49 -080038#else
39 #define DISPLAY_LIST_LOGD(...)
40#endif
41
Romain Guy4aa90572010-09-26 18:40:37 -070042///////////////////////////////////////////////////////////////////////////////
Romain Guyb051e892010-09-28 19:09:36 -070043// Display list
Romain Guy4aa90572010-09-26 18:40:37 -070044///////////////////////////////////////////////////////////////////////////////
45
Chris Craikc3566d02013-02-04 16:16:33 -080046class DeferredDisplayList;
Romain Guyb051e892010-09-28 19:09:36 -070047class DisplayListRenderer;
Chris Craik2af46352012-11-26 18:30:17 -080048class DisplayListOp;
49class DrawOp;
50class StateOp;
51
Romain Guyb051e892010-09-28 19:09:36 -070052/**
Chris Craikd6b65f62014-01-01 14:45:21 -080053 * Records drawing commands in a display list for later playback into an OpenGLRenderer.
Romain Guyb051e892010-09-28 19:09:36 -070054 */
John Reck3b202512014-06-23 13:13:08 -070055class ANDROID_API DisplayListRenderer: public StatefulBaseRenderer {
Romain Guyb051e892010-09-28 19:09:36 -070056public:
John Reck3b202512014-06-23 13:13:08 -070057 DisplayListRenderer();
Romain Guy79537452011-10-12 13:48:51 -070058 virtual ~DisplayListRenderer();
Romain Guyb051e892010-09-28 19:09:36 -070059
Chris Craik8afd0f22014-08-21 17:41:57 -070060 void insertReorderBarrier(bool enableReorder);
61
John Reck3b202512014-06-23 13:13:08 -070062 DisplayListData* finishRecording();
Chet Haase5977baa2011-01-05 18:01:22 -080063
Chris Craik14e51302013-12-30 15:32:54 -080064// ----------------------------------------------------------------------------
65// Frame state operations
66// ----------------------------------------------------------------------------
Romain Guy7c25aab2012-10-18 15:05:02 -070067 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
Romain Guy79537452011-10-12 13:48:51 -070068 virtual void finish();
Romain Guy79537452011-10-12 13:48:51 -070069 virtual void interrupt();
70 virtual void resume();
Chet Haasedaf98e92011-01-10 14:10:36 -080071
Chris Craik14e51302013-12-30 15:32:54 -080072// ----------------------------------------------------------------------------
73// Canvas state operations
74// ----------------------------------------------------------------------------
75 // Save (layer)
Romain Guy79537452011-10-12 13:48:51 -070076 virtual int save(int flags);
77 virtual void restore();
78 virtual void restoreToCount(int saveCount);
Romain Guy79537452011-10-12 13:48:51 -070079 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -050080 const SkPaint* paint, int flags);
Romain Guy4aa90572010-09-26 18:40:37 -070081
Chris Craik14e51302013-12-30 15:32:54 -080082 // Matrix
John Reck3b202512014-06-23 13:13:08 -070083 virtual void translate(float dx, float dy, float dz = 0.0f);
Romain Guy79537452011-10-12 13:48:51 -070084 virtual void rotate(float degrees);
85 virtual void scale(float sx, float sy);
86 virtual void skew(float sx, float sy);
Romain Guy4aa90572010-09-26 18:40:37 -070087
Derek Sollenberger13908822013-12-10 12:28:58 -050088 virtual void setMatrix(const SkMatrix& matrix);
89 virtual void concatMatrix(const SkMatrix& matrix);
Romain Guy4aa90572010-09-26 18:40:37 -070090
Chris Craik14e51302013-12-30 15:32:54 -080091 // Clip
Romain Guy79537452011-10-12 13:48:51 -070092 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Chris Craikd218a922014-01-02 17:13:34 -080093 virtual bool clipPath(const SkPath* path, SkRegion::Op op);
94 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op);
Romain Guy4aa90572010-09-26 18:40:37 -070095
Chris Craik14e51302013-12-30 15:32:54 -080096 // Misc - should be implemented with SkPaint inspection
Romain Guy5ff9df62012-01-23 17:09:05 -080097 virtual void resetPaintFilter();
98 virtual void setupPaintFilter(int clearBits, int setBits);
99
John Reck3b202512014-06-23 13:13:08 -0700100 bool isCurrentTransformSimple() {
101 return currentTransform()->isSimple();
102 }
103
Chris Craik14e51302013-12-30 15:32:54 -0800104// ----------------------------------------------------------------------------
105// Canvas draw operations
106// ----------------------------------------------------------------------------
107 virtual status_t drawColor(int color, SkXfermode::Mode mode);
108
109 // Bitmap-based
Chris Craik79647502014-08-06 13:42:24 -0700110 virtual status_t drawBitmap(const SkBitmap* bitmap, const SkPaint* paint);
Chris Craikd218a922014-01-02 17:13:34 -0800111 virtual status_t drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
Chris Craik14e51302013-12-30 15:32:54 -0800112 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chris Craikd218a922014-01-02 17:13:34 -0800113 float dstRight, float dstBottom, const SkPaint* paint);
Chris Craik79647502014-08-06 13:42:24 -0700114 virtual status_t drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint);
Chris Craikd218a922014-01-02 17:13:34 -0800115 virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
116 const float* vertices, const int* colors, const SkPaint* paint);
117 virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
118 float left, float top, float right, float bottom, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800119
120 // Shapes
Chris Craikd218a922014-01-02 17:13:34 -0800121 virtual status_t drawRect(float left, float top, float right, float bottom,
122 const SkPaint* paint);
123 virtual status_t drawRects(const float* rects, int count, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800124 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800125 float rx, float ry, const SkPaint* paint);
126 virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint);
John Reck52244ff2014-05-01 21:27:37 -0700127 virtual status_t drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
128 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint);
Chris Craikd218a922014-01-02 17:13:34 -0800129 virtual status_t drawOval(float left, float top, float right, float bottom,
130 const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800131 virtual status_t drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800132 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint);
133 virtual status_t drawPath(const SkPath* path, const SkPaint* paint);
134 virtual status_t drawLines(const float* points, int count, const SkPaint* paint);
135 virtual status_t drawPoints(const float* points, int count, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800136
137 // Text
138 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -0800139 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik14e51302013-12-30 15:32:54 -0800140 DrawOpMode drawOpMode = kDrawOpMode_Immediate);
Chris Craikd218a922014-01-02 17:13:34 -0800141 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
142 float hOffset, float vOffset, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800143 virtual status_t drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800144 const float* positions, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800145
146// ----------------------------------------------------------------------------
147// Canvas draw operations - special
148// ----------------------------------------------------------------------------
149 virtual status_t drawLayer(Layer* layer, float x, float y);
Chris Craika7090e02014-06-20 16:01:00 -0700150 virtual status_t drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags);
Chris Craik14e51302013-12-30 15:32:54 -0800151
152 // TODO: rename for consistency
153 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty);
154
Chris Craikcce47eb2014-07-16 15:12:15 -0700155 void setHighContrastText(bool highContrastText) {
156 mHighContrastText = highContrastText;
157 }
Romain Guy4aa90572010-09-26 18:40:37 -0700158private:
Chris Craik8afd0f22014-08-21 17:41:57 -0700159 enum DeferredBarrierType {
160 kBarrier_None,
161 kBarrier_InOrder,
162 kBarrier_OutOfOrder,
163 };
164
165 void flushRestoreToCount();
166 void flushTranslate();
167 void flushReorderBarrier();
Romain Guy27454a42011-01-23 12:01:41 -0800168
Chris Craik2af46352012-11-26 18:30:17 -0800169 LinearAllocator& alloc() { return mDisplayListData->allocator; }
Chris Craikb3cca872014-08-08 18:42:51 -0700170
171 // Each method returns final index of op
Chris Craik8afd0f22014-08-21 17:41:57 -0700172 size_t addOpAndUpdateChunk(DisplayListOp* op);
173 // flushes any deferred operations, and appends the op
174 size_t flushAndAddOp(DisplayListOp* op);
175
176 size_t addStateOp(StateOp* op);
177 size_t addDrawOp(DrawOp* op);
178 size_t addRenderNodeOp(DrawRenderNodeOp* op);
179
Romain Guy4aa90572010-09-26 18:40:37 -0700180
Chris Craik2af46352012-11-26 18:30:17 -0800181 template<class T>
Chris Craikd218a922014-01-02 17:13:34 -0800182 inline const T* refBuffer(const T* srcBuffer, int32_t count) {
183 if (!srcBuffer) return NULL;
184
Chris Craik2af46352012-11-26 18:30:17 -0800185 T* dstBuffer = (T*) mDisplayListData->allocator.alloc(count * sizeof(T));
186 memcpy(dstBuffer, srcBuffer, count * sizeof(T));
187 return dstBuffer;
Romain Guy33f6beb2012-02-16 19:24:51 -0800188 }
189
Chris Craik2af46352012-11-26 18:30:17 -0800190 inline char* refText(const char* text, size_t byteLength) {
191 return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength);
Romain Guy33f6beb2012-02-16 19:24:51 -0800192 }
193
Chris Craikd218a922014-01-02 17:13:34 -0800194 inline const SkPath* refPath(const SkPath* path) {
Chris Craik2af46352012-11-26 18:30:17 -0800195 if (!path) return NULL;
Romain Guy2fc941e2011-02-03 15:06:05 -0800196
Chris Craikd218a922014-01-02 17:13:34 -0800197 const SkPath* pathCopy = mPathMap.valueFor(path);
Romain Guy2fc941e2011-02-03 15:06:05 -0800198 if (pathCopy == NULL || pathCopy->getGenerationID() != path->getGenerationID()) {
Chris Craikd218a922014-01-02 17:13:34 -0800199 SkPath* newPathCopy = new SkPath(*path);
200 newPathCopy->setSourcePath(path);
201
202 pathCopy = newPathCopy;
Romain Guy96ebc6b2012-02-21 18:32:32 -0800203 // replaceValueFor() performs an add if the entry doesn't exist
204 mPathMap.replaceValueFor(path, pathCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800205 mDisplayListData->paths.add(pathCopy);
Romain Guy2fc941e2011-02-03 15:06:05 -0800206 }
John Reck44fd8d22014-02-26 11:00:11 -0800207 if (mDisplayListData->sourcePaths.indexOf(path) < 0) {
Romain Guy58ecc202012-09-07 11:58:36 -0700208 mCaches.resourceCache.incrementRefcount(path);
John Reck44fd8d22014-02-26 11:00:11 -0800209 mDisplayListData->sourcePaths.add(path);
Chet Haased34dd712012-05-02 18:50:34 -0700210 }
Chris Craik2af46352012-11-26 18:30:17 -0800211 return pathCopy;
Romain Guy4aa90572010-09-26 18:40:37 -0700212 }
213
Chris Craikd218a922014-01-02 17:13:34 -0800214 inline const SkPaint* refPaint(const SkPaint* paint) {
Chris Craikcce47eb2014-07-16 15:12:15 -0700215 if (!paint) return NULL;
Romain Guy0fe478e2010-11-08 12:08:41 -0800216
Chris Craikd218a922014-01-02 17:13:34 -0800217 const SkPaint* paintCopy = mPaintMap.valueFor(paint);
Chet Haased98aa2d2010-10-25 15:47:32 -0700218 if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
219 paintCopy = new SkPaint(*paint);
Romain Guy96ebc6b2012-02-21 18:32:32 -0800220 // replaceValueFor() performs an add if the entry doesn't exist
221 mPaintMap.replaceValueFor(paint, paintCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800222 mDisplayListData->paints.add(paintCopy);
Chet Haased98aa2d2010-10-25 15:47:32 -0700223 }
Romain Guy0fe478e2010-11-08 12:08:41 -0800224
Chet Haasee816bae2012-08-09 13:39:02 -0700225 return paintCopy;
Romain Guy0fe478e2010-11-08 12:08:41 -0800226 }
227
Chris Craikcce47eb2014-07-16 15:12:15 -0700228 inline SkPaint* copyPaint(const SkPaint* paint) {
229 if (!paint) return NULL;
230 SkPaint* paintCopy = new SkPaint(*paint);
231 mDisplayListData->paints.add(paintCopy);
232
233 return paintCopy;
234 }
235
Chris Craikd218a922014-01-02 17:13:34 -0800236 inline const SkRegion* refRegion(const SkRegion* region) {
Romain Guy735738c2012-12-03 12:34:51 -0800237 if (!region) {
Romain Guy735738c2012-12-03 12:34:51 -0800238 return region;
239 }
240
Chris Craikd218a922014-01-02 17:13:34 -0800241 const SkRegion* regionCopy = mRegionMap.valueFor(region);
Romain Guy735738c2012-12-03 12:34:51 -0800242 // TODO: Add generation ID to SkRegion
243 if (regionCopy == NULL) {
244 regionCopy = new SkRegion(*region);
245 // replaceValueFor() performs an add if the entry doesn't exist
246 mRegionMap.replaceValueFor(region, regionCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800247 mDisplayListData->regions.add(regionCopy);
Romain Guy735738c2012-12-03 12:34:51 -0800248 }
249
Romain Guy735738c2012-12-03 12:34:51 -0800250 return regionCopy;
251 }
252
Romain Guyce4a7df2013-03-28 11:32:33 -0700253 inline Layer* refLayer(Layer* layer) {
John Reck44fd8d22014-02-26 11:00:11 -0800254 mDisplayListData->layers.add(layer);
Romain Guyce4a7df2013-03-28 11:32:33 -0700255 mCaches.resourceCache.incrementRefcount(layer);
256 return layer;
257 }
258
Chris Craikd218a922014-01-02 17:13:34 -0800259 inline const SkBitmap* refBitmap(const SkBitmap* bitmap) {
Chet Haased98aa2d2010-10-25 15:47:32 -0700260 // Note that this assumes the bitmap is immutable. There are cases this won't handle
261 // correctly, such as creating the bitmap from scratch, drawing with it, changing its
262 // contents, and drawing again. The only fix would be to always copy it the first time,
263 // which doesn't seem worth the extra cycles for this unlikely case.
John Reck44fd8d22014-02-26 11:00:11 -0800264 mDisplayListData->bitmapResources.add(bitmap);
Romain Guy58ecc202012-09-07 11:58:36 -0700265 mCaches.resourceCache.incrementRefcount(bitmap);
Chris Craik2af46352012-11-26 18:30:17 -0800266 return bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700267 }
Romain Guy4aa90572010-09-26 18:40:37 -0700268
Chris Craikd218a922014-01-02 17:13:34 -0800269 inline const SkBitmap* refBitmapData(const SkBitmap* bitmap) {
John Reck44fd8d22014-02-26 11:00:11 -0800270 mDisplayListData->ownedBitmapResources.add(bitmap);
Romain Guy58ecc202012-09-07 11:58:36 -0700271 mCaches.resourceCache.incrementRefcount(bitmap);
Chris Craik2af46352012-11-26 18:30:17 -0800272 return bitmap;
Romain Guy49c5fc02012-05-15 11:10:01 -0700273 }
274
Chris Craikd218a922014-01-02 17:13:34 -0800275 inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) {
John Reck44fd8d22014-02-26 11:00:11 -0800276 mDisplayListData->patchResources.add(patch);
Romain Guye3b0a012013-06-26 15:45:41 -0700277 mCaches.resourceCache.incrementRefcount(patch);
278 return patch;
279 }
280
Chris Craikd218a922014-01-02 17:13:34 -0800281 DefaultKeyedVector<const SkPaint*, const SkPaint*> mPaintMap;
Chris Craikd218a922014-01-02 17:13:34 -0800282 DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap;
Chris Craikd218a922014-01-02 17:13:34 -0800283 DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap;
Romain Guy24c00212011-01-14 15:31:00 -0800284
Romain Guy58ecc202012-09-07 11:58:36 -0700285 Caches& mCaches;
John Reck44fd8d22014-02-26 11:00:11 -0800286 DisplayListData* mDisplayListData;
Romain Guy58ecc202012-09-07 11:58:36 -0700287
Romain Guy33f6beb2012-02-16 19:24:51 -0800288 float mTranslateX;
289 float mTranslateY;
Chris Craik8afd0f22014-08-21 17:41:57 -0700290 bool mHasDeferredTranslate;
291 DeferredBarrierType mDeferredBarrierType;
Chris Craikcce47eb2014-07-16 15:12:15 -0700292 bool mHighContrastText;
Romain Guy54c1a642012-09-27 17:55:46 -0700293
Chris Craikeea6ef92014-03-05 16:37:35 -0800294 int mRestoreSaveCount;
295
John Recke18264b2014-03-12 13:56:30 -0700296 friend class RenderNode;
Romain Guyb051e892010-09-28 19:09:36 -0700297
Romain Guy4aa90572010-09-26 18:40:37 -0700298}; // class DisplayListRenderer
299
300}; // namespace uirenderer
301}; // namespace android
302
Romain Guy5b3b3522010-10-27 18:57:51 -0700303#endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H