blob: f0ae00fe855a1a63ecb95c00b2ce4a7ef5300190 [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"
Romain Guy4aa90572010-09-26 18:40:37 -070026#include "OpenGLRenderer.h"
John Reck113e0822014-03-18 09:22:59 -070027#include "RenderNode.h"
Romain Guy4aa90572010-09-26 18:40:37 -070028
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guyffac7fc2011-01-13 17:21:49 -080036// Debug
37#if DEBUG_DISPLAY_LIST
Steve Block5baa3a62011-12-20 16:23:08 +000038 #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyffac7fc2011-01-13 17:21:49 -080039#else
40 #define DISPLAY_LIST_LOGD(...)
41#endif
42
Romain Guy4aa90572010-09-26 18:40:37 -070043///////////////////////////////////////////////////////////////////////////////
Romain Guyb051e892010-09-28 19:09:36 -070044// Display list
Romain Guy4aa90572010-09-26 18:40:37 -070045///////////////////////////////////////////////////////////////////////////////
46
Chris Craikc3566d02013-02-04 16:16:33 -080047class DeferredDisplayList;
Romain Guyb051e892010-09-28 19:09:36 -070048class DisplayListRenderer;
Chris Craik2af46352012-11-26 18:30:17 -080049class DisplayListOp;
50class DrawOp;
51class StateOp;
52
Romain Guyb051e892010-09-28 19:09:36 -070053/**
Chris Craikd6b65f62014-01-01 14:45:21 -080054 * Records drawing commands in a display list for later playback into an OpenGLRenderer.
Romain Guyb051e892010-09-28 19:09:36 -070055 */
56class DisplayListRenderer: public OpenGLRenderer {
57public:
Romain Guy79537452011-10-12 13:48:51 -070058 ANDROID_API DisplayListRenderer();
59 virtual ~DisplayListRenderer();
Romain Guyb051e892010-09-28 19:09:36 -070060
John Reck44fd8d22014-02-26 11:00:11 -080061 ANDROID_API DisplayListData* finishRecording();
Chet Haase5977baa2011-01-05 18:01:22 -080062
Chris Craik406af022014-01-02 14:56:40 -080063 virtual bool isRecording() const { return true; }
Romain Guy49c5fc02012-05-15 11:10:01 -070064
Chris Craik14e51302013-12-30 15:32:54 -080065// ----------------------------------------------------------------------------
66// Frame state operations
67// ----------------------------------------------------------------------------
Romain Guy7c25aab2012-10-18 15:05:02 -070068 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
Romain Guy79537452011-10-12 13:48:51 -070069 virtual void finish();
Romain Guy79537452011-10-12 13:48:51 -070070 virtual void interrupt();
71 virtual void resume();
Chet Haasedaf98e92011-01-10 14:10:36 -080072
Chris Craik14e51302013-12-30 15:32:54 -080073// ----------------------------------------------------------------------------
74// Canvas state operations
75// ----------------------------------------------------------------------------
76 // Save (layer)
Romain Guy79537452011-10-12 13:48:51 -070077 virtual int save(int flags);
78 virtual void restore();
79 virtual void restoreToCount(int saveCount);
Romain Guy79537452011-10-12 13:48:51 -070080 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -050081 const SkPaint* paint, int flags);
Romain Guy4aa90572010-09-26 18:40:37 -070082
Chris Craik14e51302013-12-30 15:32:54 -080083 // Matrix
Chris Craikb4589422013-12-26 15:13:13 -080084 virtual void translate(float dx, float dy, float dz);
Romain Guy79537452011-10-12 13:48:51 -070085 virtual void rotate(float degrees);
86 virtual void scale(float sx, float sy);
87 virtual void skew(float sx, float sy);
Romain Guy4aa90572010-09-26 18:40:37 -070088
Chris Craikd218a922014-01-02 17:13:34 -080089 virtual void setMatrix(const SkMatrix* matrix);
90 virtual void concatMatrix(const SkMatrix* matrix);
Romain Guy4aa90572010-09-26 18:40:37 -070091
Chris Craik14e51302013-12-30 15:32:54 -080092 // Clip
Romain Guy79537452011-10-12 13:48:51 -070093 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Chris Craikd218a922014-01-02 17:13:34 -080094 virtual bool clipPath(const SkPath* path, SkRegion::Op op);
95 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op);
Romain Guy4aa90572010-09-26 18:40:37 -070096
Chris Craik14e51302013-12-30 15:32:54 -080097 // Misc - should be implemented with SkPaint inspection
Romain Guy5ff9df62012-01-23 17:09:05 -080098 virtual void resetPaintFilter();
99 virtual void setupPaintFilter(int clearBits, int setBits);
100
Chris Craik14e51302013-12-30 15:32:54 -0800101// ----------------------------------------------------------------------------
102// Canvas draw operations
103// ----------------------------------------------------------------------------
104 virtual status_t drawColor(int color, SkXfermode::Mode mode);
105
106 // Bitmap-based
Chris Craikd218a922014-01-02 17:13:34 -0800107 virtual status_t drawBitmap(const SkBitmap* bitmap, float left, float top,
108 const SkPaint* paint);
109 virtual status_t drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix,
110 const SkPaint* paint);
111 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);
114 virtual status_t drawBitmapData(const SkBitmap* bitmap, float left, float top,
115 const SkPaint* paint);
116 virtual status_t drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
117 const float* vertices, const int* colors, const SkPaint* paint);
118 virtual status_t drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
119 float left, float top, float right, float bottom, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800120
121 // Shapes
Chris Craikd218a922014-01-02 17:13:34 -0800122 virtual status_t drawRect(float left, float top, float right, float bottom,
123 const SkPaint* paint);
124 virtual status_t drawRects(const float* rects, int count, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800125 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800126 float rx, float ry, const SkPaint* paint);
127 virtual status_t drawCircle(float x, float y, float radius, const SkPaint* paint);
John Reck52244ff2014-05-01 21:27:37 -0700128 virtual status_t drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
129 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint);
Chris Craikd218a922014-01-02 17:13:34 -0800130 virtual status_t drawOval(float left, float top, float right, float bottom,
131 const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800132 virtual status_t drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800133 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint);
134 virtual status_t drawPath(const SkPath* path, const SkPaint* paint);
135 virtual status_t drawLines(const float* points, int count, const SkPaint* paint);
136 virtual status_t drawPoints(const float* points, int count, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800137
138 // Text
139 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -0800140 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik14e51302013-12-30 15:32:54 -0800141 DrawOpMode drawOpMode = kDrawOpMode_Immediate);
Chris Craikd218a922014-01-02 17:13:34 -0800142 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
143 float hOffset, float vOffset, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800144 virtual status_t drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800145 const float* positions, const SkPaint* paint);
Chris Craik14e51302013-12-30 15:32:54 -0800146
147// ----------------------------------------------------------------------------
148// Canvas draw operations - special
149// ----------------------------------------------------------------------------
150 virtual status_t drawLayer(Layer* layer, float x, float y);
John Recke18264b2014-03-12 13:56:30 -0700151 virtual status_t drawDisplayList(RenderNode* displayList, Rect& dirty,
Chris Craik14e51302013-12-30 15:32:54 -0800152 int32_t replayFlags);
153
154 // TODO: rename for consistency
155 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty);
156
Romain Guy4aa90572010-09-26 18:40:37 -0700157private:
Chris Craik2af46352012-11-26 18:30:17 -0800158 void insertRestoreToCount();
159 void insertTranslate();
Romain Guy27454a42011-01-23 12:01:41 -0800160
Chris Craik2af46352012-11-26 18:30:17 -0800161 LinearAllocator& alloc() { return mDisplayListData->allocator; }
162 void addStateOp(StateOp* op);
Romain Guy0f667532013-03-01 14:31:04 -0800163 void addDrawOp(DrawOp* op);
Chris Craik2af46352012-11-26 18:30:17 -0800164 void addOpInternal(DisplayListOp* op) {
Romain Guy27454a42011-01-23 12:01:41 -0800165 insertRestoreToCount();
Chris Craik2af46352012-11-26 18:30:17 -0800166 insertTranslate();
167 mDisplayListData->displayListOps.add(op);
Romain Guy4aa90572010-09-26 18:40:37 -0700168 }
169
Chris Craik2af46352012-11-26 18:30:17 -0800170 template<class T>
Chris Craikd218a922014-01-02 17:13:34 -0800171 inline const T* refBuffer(const T* srcBuffer, int32_t count) {
172 if (!srcBuffer) return NULL;
173
Chris Craik2af46352012-11-26 18:30:17 -0800174 T* dstBuffer = (T*) mDisplayListData->allocator.alloc(count * sizeof(T));
175 memcpy(dstBuffer, srcBuffer, count * sizeof(T));
176 return dstBuffer;
Romain Guy33f6beb2012-02-16 19:24:51 -0800177 }
178
Chris Craik2af46352012-11-26 18:30:17 -0800179 inline char* refText(const char* text, size_t byteLength) {
180 return (char*) refBuffer<uint8_t>((uint8_t*)text, byteLength);
Romain Guy33f6beb2012-02-16 19:24:51 -0800181 }
182
Chris Craikd218a922014-01-02 17:13:34 -0800183 inline const SkPath* refPath(const SkPath* path) {
Chris Craik2af46352012-11-26 18:30:17 -0800184 if (!path) return NULL;
Romain Guy2fc941e2011-02-03 15:06:05 -0800185
Chris Craikd218a922014-01-02 17:13:34 -0800186 const SkPath* pathCopy = mPathMap.valueFor(path);
Romain Guy2fc941e2011-02-03 15:06:05 -0800187 if (pathCopy == NULL || pathCopy->getGenerationID() != path->getGenerationID()) {
Chris Craikd218a922014-01-02 17:13:34 -0800188 SkPath* newPathCopy = new SkPath(*path);
189 newPathCopy->setSourcePath(path);
190
191 pathCopy = newPathCopy;
Romain Guy96ebc6b2012-02-21 18:32:32 -0800192 // replaceValueFor() performs an add if the entry doesn't exist
193 mPathMap.replaceValueFor(path, pathCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800194 mDisplayListData->paths.add(pathCopy);
Romain Guy2fc941e2011-02-03 15:06:05 -0800195 }
John Reck44fd8d22014-02-26 11:00:11 -0800196 if (mDisplayListData->sourcePaths.indexOf(path) < 0) {
Romain Guy58ecc202012-09-07 11:58:36 -0700197 mCaches.resourceCache.incrementRefcount(path);
John Reck44fd8d22014-02-26 11:00:11 -0800198 mDisplayListData->sourcePaths.add(path);
Chet Haased34dd712012-05-02 18:50:34 -0700199 }
Chris Craik2af46352012-11-26 18:30:17 -0800200 return pathCopy;
Romain Guy4aa90572010-09-26 18:40:37 -0700201 }
202
Chris Craikd218a922014-01-02 17:13:34 -0800203 inline const SkPaint* refPaint(const SkPaint* paint) {
Romain Guy24c00212011-01-14 15:31:00 -0800204 if (!paint) {
Chet Haasee816bae2012-08-09 13:39:02 -0700205 return paint;
Chet Haased98aa2d2010-10-25 15:47:32 -0700206 }
Romain Guy0fe478e2010-11-08 12:08:41 -0800207
Chris Craikd218a922014-01-02 17:13:34 -0800208 const SkPaint* paintCopy = mPaintMap.valueFor(paint);
Chet Haased98aa2d2010-10-25 15:47:32 -0700209 if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
210 paintCopy = new SkPaint(*paint);
Romain Guy96ebc6b2012-02-21 18:32:32 -0800211 // replaceValueFor() performs an add if the entry doesn't exist
212 mPaintMap.replaceValueFor(paint, paintCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800213 mDisplayListData->paints.add(paintCopy);
Chet Haased98aa2d2010-10-25 15:47:32 -0700214 }
Romain Guy0fe478e2010-11-08 12:08:41 -0800215
Chet Haasee816bae2012-08-09 13:39:02 -0700216 return paintCopy;
Romain Guy0fe478e2010-11-08 12:08:41 -0800217 }
218
Chris Craikd218a922014-01-02 17:13:34 -0800219 inline const SkRegion* refRegion(const SkRegion* region) {
Romain Guy735738c2012-12-03 12:34:51 -0800220 if (!region) {
Romain Guy735738c2012-12-03 12:34:51 -0800221 return region;
222 }
223
Chris Craikd218a922014-01-02 17:13:34 -0800224 const SkRegion* regionCopy = mRegionMap.valueFor(region);
Romain Guy735738c2012-12-03 12:34:51 -0800225 // TODO: Add generation ID to SkRegion
226 if (regionCopy == NULL) {
227 regionCopy = new SkRegion(*region);
228 // replaceValueFor() performs an add if the entry doesn't exist
229 mRegionMap.replaceValueFor(region, regionCopy);
John Reck44fd8d22014-02-26 11:00:11 -0800230 mDisplayListData->regions.add(regionCopy);
Romain Guy735738c2012-12-03 12:34:51 -0800231 }
232
Romain Guy735738c2012-12-03 12:34:51 -0800233 return regionCopy;
234 }
235
Chris Craikd218a922014-01-02 17:13:34 -0800236 inline const SkMatrix* refMatrix(const SkMatrix* matrix) {
Romain Guy4e7b7722013-07-16 13:47:01 -0700237 if (matrix) {
238 // Copying the matrix is cheap and prevents against the user changing
239 // the original matrix before the operation that uses it
Chris Craikd218a922014-01-02 17:13:34 -0800240 const SkMatrix* copy = new SkMatrix(*matrix);
John Reck44fd8d22014-02-26 11:00:11 -0800241 mDisplayListData->matrices.add(copy);
Romain Guy4e7b7722013-07-16 13:47:01 -0700242 return copy;
243 }
244 return matrix;
Romain Guy4aa90572010-09-26 18:40:37 -0700245 }
246
Romain Guyce4a7df2013-03-28 11:32:33 -0700247 inline Layer* refLayer(Layer* layer) {
John Reck44fd8d22014-02-26 11:00:11 -0800248 mDisplayListData->layers.add(layer);
Romain Guyce4a7df2013-03-28 11:32:33 -0700249 mCaches.resourceCache.incrementRefcount(layer);
250 return layer;
251 }
252
Chris Craikd218a922014-01-02 17:13:34 -0800253 inline const SkBitmap* refBitmap(const SkBitmap* bitmap) {
Chet Haased98aa2d2010-10-25 15:47:32 -0700254 // Note that this assumes the bitmap is immutable. There are cases this won't handle
255 // correctly, such as creating the bitmap from scratch, drawing with it, changing its
256 // contents, and drawing again. The only fix would be to always copy it the first time,
257 // which doesn't seem worth the extra cycles for this unlikely case.
John Reck44fd8d22014-02-26 11:00:11 -0800258 mDisplayListData->bitmapResources.add(bitmap);
Romain Guy58ecc202012-09-07 11:58:36 -0700259 mCaches.resourceCache.incrementRefcount(bitmap);
Chris Craik2af46352012-11-26 18:30:17 -0800260 return bitmap;
Chet Haase5c13d892010-10-08 08:37:55 -0700261 }
Romain Guy4aa90572010-09-26 18:40:37 -0700262
Chris Craikd218a922014-01-02 17:13:34 -0800263 inline const SkBitmap* refBitmapData(const SkBitmap* bitmap) {
John Reck44fd8d22014-02-26 11:00:11 -0800264 mDisplayListData->ownedBitmapResources.add(bitmap);
Romain Guy58ecc202012-09-07 11:58:36 -0700265 mCaches.resourceCache.incrementRefcount(bitmap);
Chris Craik2af46352012-11-26 18:30:17 -0800266 return bitmap;
Romain Guy49c5fc02012-05-15 11:10:01 -0700267 }
268
Chris Craikd218a922014-01-02 17:13:34 -0800269 inline const Res_png_9patch* refPatch(const Res_png_9patch* patch) {
John Reck44fd8d22014-02-26 11:00:11 -0800270 mDisplayListData->patchResources.add(patch);
Romain Guye3b0a012013-06-26 15:45:41 -0700271 mCaches.resourceCache.incrementRefcount(patch);
272 return patch;
273 }
274
Chris Craikd218a922014-01-02 17:13:34 -0800275 DefaultKeyedVector<const SkPaint*, const SkPaint*> mPaintMap;
Chris Craikd218a922014-01-02 17:13:34 -0800276 DefaultKeyedVector<const SkPath*, const SkPath*> mPathMap;
Chris Craikd218a922014-01-02 17:13:34 -0800277 DefaultKeyedVector<const SkRegion*, const SkRegion*> mRegionMap;
Romain Guy24c00212011-01-14 15:31:00 -0800278
Romain Guy58ecc202012-09-07 11:58:36 -0700279 Caches& mCaches;
John Reck44fd8d22014-02-26 11:00:11 -0800280 DisplayListData* mDisplayListData;
Romain Guy58ecc202012-09-07 11:58:36 -0700281
Romain Guy33f6beb2012-02-16 19:24:51 -0800282 float mTranslateX;
283 float mTranslateY;
284 bool mHasTranslate;
Romain Guy54c1a642012-09-27 17:55:46 -0700285
Chris Craikeea6ef92014-03-05 16:37:35 -0800286 int mRestoreSaveCount;
287
John Recke18264b2014-03-12 13:56:30 -0700288 friend class RenderNode;
Romain Guyb051e892010-09-28 19:09:36 -0700289
Romain Guy4aa90572010-09-26 18:40:37 -0700290}; // class DisplayListRenderer
291
292}; // namespace uirenderer
293}; // namespace android
294
Romain Guy5b3b3522010-10-27 18:57:51 -0700295#endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H