blob: fff1d7c64bbd733233d6d054aed66a1f0d824cf2 [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
20#include <SkChunkAlloc.h>
21#include <SkFlattenable.h>
22#include <SkMatrix.h>
Chet Haasea1cff502012-02-21 13:43:44 -080023#include <SkCamera.h>
Romain Guy4aa90572010-09-26 18:40:37 -070024#include <SkPaint.h>
25#include <SkPath.h>
Romain Guy4aa90572010-09-26 18:40:37 -070026#include <SkRefCnt.h>
27#include <SkTDArray.h>
28#include <SkTSearch.h>
29
Romain Guy79537452011-10-12 13:48:51 -070030#include <cutils/compiler.h>
31
Chet Haase9c1e23b2011-03-24 10:51:31 -070032#include "DisplayListLogBuffer.h"
Romain Guy4aa90572010-09-26 18:40:37 -070033#include "OpenGLRenderer.h"
34
35namespace android {
36namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Defines
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guy6d7475d2011-07-27 16:28:21 -070042#define MIN_WRITER_SIZE 4096
Romain Guy33f6beb2012-02-16 19:24:51 -080043#define OP_MAY_BE_SKIPPED_MASK 0xff000000
Romain Guy4aa90572010-09-26 18:40:37 -070044
Romain Guyffac7fc2011-01-13 17:21:49 -080045// Debug
46#if DEBUG_DISPLAY_LIST
Steve Block5baa3a62011-12-20 16:23:08 +000047 #define DISPLAY_LIST_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyffac7fc2011-01-13 17:21:49 -080048#else
49 #define DISPLAY_LIST_LOGD(...)
50#endif
51
Chet Haasea1cff502012-02-21 13:43:44 -080052// Set to 1 to enable native processing of View properties. 0 by default. Eventually this
53// will go away and we will always use this approach for accelerated apps.
Chet Haase76240da2012-03-29 09:42:34 -070054#define USE_DISPLAY_LIST_PROPERTIES 0
Chet Haasea1cff502012-02-21 13:43:44 -080055
56#define TRANSLATION 0x0001
57#define ROTATION 0x0002
58#define ROTATION_3D 0x0004
59#define SCALE 0x0008
60#define PIVOT 0x0010
61
Romain Guy4aa90572010-09-26 18:40:37 -070062///////////////////////////////////////////////////////////////////////////////
Romain Guyb051e892010-09-28 19:09:36 -070063// Display list
Romain Guy4aa90572010-09-26 18:40:37 -070064///////////////////////////////////////////////////////////////////////////////
65
Romain Guyb051e892010-09-28 19:09:36 -070066class DisplayListRenderer;
67
Romain Guy4aa90572010-09-26 18:40:37 -070068/**
Romain Guyb051e892010-09-28 19:09:36 -070069 * Replays recorded drawing commands.
Romain Guy4aa90572010-09-26 18:40:37 -070070 */
Romain Guyb051e892010-09-28 19:09:36 -070071class DisplayList {
Romain Guy4aa90572010-09-26 18:40:37 -070072public:
Romain Guyb051e892010-09-28 19:09:36 -070073 DisplayList(const DisplayListRenderer& recorder);
Romain Guy79537452011-10-12 13:48:51 -070074 ANDROID_API ~DisplayList();
Romain Guy4aa90572010-09-26 18:40:37 -070075
Romain Guyffac7fc2011-01-13 17:21:49 -080076 // IMPORTANT: Update the intialization of OP_NAMES in the .cpp file
77 // when modifying this file
Romain Guy4aa90572010-09-26 18:40:37 -070078 enum Op {
Romain Guy04c9d8c2011-08-25 14:01:48 -070079 // Non-drawing operations
Romain Guy2b1847e2011-01-26 13:43:01 -080080 Save = 0,
Romain Guy4aa90572010-09-26 18:40:37 -070081 Restore,
82 RestoreToCount,
83 SaveLayer,
Romain Guy5b3b3522010-10-27 18:57:51 -070084 SaveLayerAlpha,
Romain Guy4aa90572010-09-26 18:40:37 -070085 Translate,
86 Rotate,
87 Scale,
Romain Guy807daf72011-01-18 11:19:19 -080088 Skew,
Romain Guy4aa90572010-09-26 18:40:37 -070089 SetMatrix,
90 ConcatMatrix,
91 ClipRect,
Romain Guy04c9d8c2011-08-25 14:01:48 -070092 // Drawing operations
Romain Guy0fe478e2010-11-08 12:08:41 -080093 DrawDisplayList,
Romain Guy6c319ca2011-01-11 14:29:25 -080094 DrawLayer,
Romain Guy4aa90572010-09-26 18:40:37 -070095 DrawBitmap,
96 DrawBitmapMatrix,
97 DrawBitmapRect,
Romain Guy5a7b4662011-01-20 19:09:30 -080098 DrawBitmapMesh,
Romain Guy4aa90572010-09-26 18:40:37 -070099 DrawPatch,
100 DrawColor,
101 DrawRect,
Romain Guy01d58e42011-01-19 21:54:02 -0800102 DrawRoundRect,
103 DrawCircle,
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800104 DrawOval,
Romain Guy8b2f5262011-01-23 16:15:02 -0800105 DrawArc,
Romain Guy4aa90572010-09-26 18:40:37 -0700106 DrawPath,
107 DrawLines,
Romain Guyed6fcb02011-03-21 13:11:28 -0700108 DrawPoints,
Romain Guy4aa90572010-09-26 18:40:37 -0700109 DrawText,
Romain Guy325740f2012-02-24 16:48:34 -0800110 DrawTextOnPath,
Romain Guyeb9a5362012-01-17 17:39:26 -0800111 DrawPosText,
Romain Guy4aa90572010-09-26 18:40:37 -0700112 ResetShader,
113 SetupShader,
114 ResetColorFilter,
115 SetupColorFilter,
116 ResetShadow,
Romain Guyffac7fc2011-01-13 17:21:49 -0800117 SetupShadow,
Romain Guy5ff9df62012-01-23 17:09:05 -0800118 ResetPaintFilter,
119 SetupPaintFilter,
Chet Haasedaf98e92011-01-10 14:10:36 -0800120 DrawGLFunction,
Romain Guy4aa90572010-09-26 18:40:37 -0700121 };
122
Romain Guy33f6beb2012-02-16 19:24:51 -0800123 // See flags defined in DisplayList.java
124 enum ReplayFlag {
125 kReplayFlag_ClipChildren = 0x1
126 };
127
Romain Guyffac7fc2011-01-13 17:21:49 -0800128 static const char* OP_NAMES[];
129
Chet Haasea1cff502012-02-21 13:43:44 -0800130 void setViewProperties(OpenGLRenderer& renderer, uint32_t width, uint32_t height,
131 uint32_t level);
132 void outputViewProperties(OpenGLRenderer& renderer, char* indent);
133
Romain Guy79537452011-10-12 13:48:51 -0700134 ANDROID_API size_t getSize();
Romain Guybb0acdf2012-03-05 13:44:35 -0800135 ANDROID_API static void destroyDisplayListDeferred(DisplayList* displayList);
136 ANDROID_API static void outputLogBuffer(int fd);
137
138 void initFromDisplayListRenderer(const DisplayListRenderer& recorder, bool reusing = false);
Romain Guy65b345f2011-07-27 18:51:50 -0700139
Romain Guy65549432012-03-26 16:45:05 -0700140 status_t replay(OpenGLRenderer& renderer, uint32_t width, uint32_t height,
Chet Haasea1cff502012-02-21 13:43:44 -0800141 Rect& dirty, int32_t flags, uint32_t level = 0);
Romain Guyb051e892010-09-28 19:09:36 -0700142
Chet Haaseed30fd82011-04-22 16:18:45 -0700143 void output(OpenGLRenderer& renderer, uint32_t level = 0);
144
Romain Guy04c9d8c2011-08-25 14:01:48 -0700145 void setRenderable(bool renderable) {
146 mIsRenderable = renderable;
147 }
148
149 bool isRenderable() const {
150 return mIsRenderable;
151 }
152
Romain Guy13631f32012-01-30 17:41:55 -0800153 void setName(const char* name) {
154 if (name) {
155 mName.setTo(name);
156 }
157 }
158
Chet Haasea1cff502012-02-21 13:43:44 -0800159 void setApplicationScale(float scale) {
160 mApplicationScale = scale;
161 }
162
163 void setClipChildren(bool clipChildren) {
164 mClipChildren = clipChildren;
165 }
166
167 void setAlpha(float alpha) {
168 if (alpha != mAlpha) {
169 mAlpha = alpha;
170 mMultipliedAlpha = (int)(255 * alpha);
171 }
172 }
173
174 void setTranslationX(float translationX) {
175 if (translationX != mTranslationX) {
176 mTranslationX = translationX;
177 mMatrixDirty = true;
178 if (ALMOST_EQUAL(mTranslationX, 0) && ALMOST_EQUAL(mTranslationY, 0)) {
179 mMatrixFlags &= ~TRANSLATION;
180 } else {
181 mMatrixFlags |= TRANSLATION;
182 }
183 }
184 }
185
186 void setTranslationY(float translationY) {
187 if (translationY != mTranslationY) {
188 mTranslationY = translationY;
189 mMatrixDirty = true;
190 if (ALMOST_EQUAL(mTranslationX, 0) && ALMOST_EQUAL(mTranslationY, 0)) {
191 mMatrixFlags &= ~TRANSLATION;
192 } else {
193 mMatrixFlags |= TRANSLATION;
194 }
195 }
196 }
197
198 void setRotation(float rotation) {
199 if (rotation != mRotation) {
200 mRotation = rotation;
201 mMatrixDirty = true;
202 if (ALMOST_EQUAL(mRotation, 0)) {
203 mMatrixFlags &= ~ROTATION;
204 } else {
205 mMatrixFlags |= ROTATION;
206 }
207 }
208 }
209
210 void setRotationX(float rotationX) {
211 if (rotationX != mRotationX) {
212 mRotationX = rotationX;
213 mMatrixDirty = true;
214 if (ALMOST_EQUAL(mRotationX, 0) && ALMOST_EQUAL(mRotationY, 0)) {
215 mMatrixFlags &= ~ROTATION_3D;
216 } else {
217 mMatrixFlags |= ROTATION_3D;
218 }
219 }
220 }
221
222 void setRotationY(float rotationY) {
223 if (rotationY != mRotationY) {
224 mRotationY = rotationY;
225 mMatrixDirty = true;
226 if (ALMOST_EQUAL(mRotationX, 0) && ALMOST_EQUAL(mRotationY, 0)) {
227 mMatrixFlags &= ~ROTATION_3D;
228 } else {
229 mMatrixFlags |= ROTATION_3D;
230 }
231 }
232 }
233
234 void setScaleX(float scaleX) {
235 if (scaleX != mScaleX) {
236 mScaleX = scaleX;
237 mMatrixDirty = true;
238 if (ALMOST_EQUAL(mScaleX, 1) && ALMOST_EQUAL(mScaleY, 1)) {
239 mMatrixFlags &= ~SCALE;
240 } else {
241 mMatrixFlags |= SCALE;
242 }
243 }
244 }
245
246 void setScaleY(float scaleY) {
247 if (scaleY != mScaleY) {
248 mScaleY = scaleY;
249 mMatrixDirty = true;
250 if (ALMOST_EQUAL(mScaleX, 1) && ALMOST_EQUAL(mScaleY, 1)) {
251 mMatrixFlags &= ~SCALE;
252 } else {
253 mMatrixFlags |= SCALE;
254 }
255 }
256 }
257
258 void setPivotX(float pivotX) {
259 mPivotX = pivotX;
260 mMatrixDirty = true;
261 if (ALMOST_EQUAL(mPivotX, 0) && ALMOST_EQUAL(mPivotY, 0)) {
262 mMatrixFlags &= ~PIVOT;
263 } else {
264 mMatrixFlags |= PIVOT;
265 }
266 mPivotExplicitlySet = true;
267 }
268
269 void setPivotY(float pivotY) {
270 mPivotY = pivotY;
271 mMatrixDirty = true;
272 if (ALMOST_EQUAL(mPivotX, 0) && ALMOST_EQUAL(mPivotY, 0)) {
273 mMatrixFlags &= ~PIVOT;
274 } else {
275 mMatrixFlags |= PIVOT;
276 }
277 mPivotExplicitlySet = true;
278 }
279
280 void setCameraDistance(float distance) {
281 if (distance != mCameraDistance) {
282 mCameraDistance = distance;
283 mMatrixDirty = true;
284 if (!mTransformCamera) {
285 mTransformCamera = new Sk3DView();
286 mTransformMatrix3D = new SkMatrix();
287 }
288 mTransformCamera->setCameraLocation(0, 0, distance);
289 }
290 }
291
292 void setLeft(int left) {
293 if (left != mLeft) {
294 mLeft = left;
295 mWidth = mRight - mLeft;
296 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
297 mMatrixDirty = true;
298 }
299 }
300 }
301
302 void setTop(int top) {
303 if (top != mTop) {
304 mTop = top;
305 mHeight = mBottom - mTop;
306 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
307 mMatrixDirty = true;
308 }
309 }
310 }
311
312 void setRight(int right) {
313 if (right != mRight) {
314 mRight = right;
315 mWidth = mRight - mLeft;
316 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
317 mMatrixDirty = true;
318 }
319 }
320 }
321
322 void setBottom(int bottom) {
323 if (bottom != mBottom) {
324 mBottom = bottom;
325 mHeight = mBottom - mTop;
326 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
327 mMatrixDirty = true;
328 }
329 }
330 }
331
332 void setLeftTop(int left, int top) {
333 if (left != mLeft || top != mTop) {
334 mLeft = left;
335 mTop = top;
336 mWidth = mRight - mLeft;
337 mHeight = mBottom - mTop;
338 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
339 mMatrixDirty = true;
340 }
341 }
342 }
343
344 void setLeftTopRightBottom(int left, int top, int right, int bottom) {
345 if (left != mLeft || top != mTop || right != mRight || bottom != mBottom) {
346 mLeft = left;
347 mTop = top;
348 mRight = right;
349 mBottom = bottom;
350 mWidth = mRight - mLeft;
351 mHeight = mBottom - mTop;
352 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
353 mMatrixDirty = true;
354 }
355 }
356 }
357
358 void offsetLeftRight(int offset) {
359 if (offset != 0) {
360 mLeft += offset;
361 mRight += offset;
362 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
363 mMatrixDirty = true;
364 }
365 }
366 }
367
368 void offsetTopBottom(int offset) {
369 if (offset != 0) {
370 mTop += offset;
371 mBottom += offset;
372 if (mMatrixFlags > TRANSLATION && !mPivotExplicitlySet) {
373 mMatrixDirty = true;
374 }
375 }
376 }
377
378 void setCaching(bool caching) {
379 mCaching = caching;
380 }
381
Romain Guyb051e892010-09-28 19:09:36 -0700382private:
383 void init();
384
Chet Haasea1cff502012-02-21 13:43:44 -0800385 void initProperties();
386
Chet Haased63cbd12011-02-03 16:32:46 -0800387 void clearResources();
388
Chet Haasea1cff502012-02-21 13:43:44 -0800389 void updateMatrix();
390
Romain Guyb051e892010-09-28 19:09:36 -0700391 class TextContainer {
392 public:
393 size_t length() const {
394 return mByteLength;
395 }
396
397 const char* text() const {
398 return (const char*) mText;
399 }
400
401 size_t mByteLength;
402 const char* mText;
403 };
404
405 SkBitmap* getBitmap() {
Chet Haase5c13d892010-10-08 08:37:55 -0700406 return (SkBitmap*) getInt();
407 }
408
409 SkiaShader* getShader() {
410 return (SkiaShader*) getInt();
Romain Guyb051e892010-09-28 19:09:36 -0700411 }
412
Chet Haasead93c2b2010-10-22 16:17:12 -0700413 SkiaColorFilter* getColorFilter() {
414 return (SkiaColorFilter*) getInt();
415 }
416
Romain Guy33f6beb2012-02-16 19:24:51 -0800417 inline int32_t getIndex() {
Romain Guyb051e892010-09-28 19:09:36 -0700418 return mReader.readInt();
419 }
420
Romain Guy33f6beb2012-02-16 19:24:51 -0800421 inline int32_t getInt() {
Romain Guyb051e892010-09-28 19:09:36 -0700422 return mReader.readInt();
423 }
424
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700425 inline uint32_t getUInt() {
426 return mReader.readU32();
427 }
428
Romain Guyb051e892010-09-28 19:09:36 -0700429 SkMatrix* getMatrix() {
Chet Haase5c13d892010-10-08 08:37:55 -0700430 return (SkMatrix*) getInt();
Romain Guyb051e892010-09-28 19:09:36 -0700431 }
432
433 SkPath* getPath() {
Romain Guy2fc941e2011-02-03 15:06:05 -0800434 return (SkPath*) getInt();
Romain Guyb051e892010-09-28 19:09:36 -0700435 }
436
Romain Guy5ff9df62012-01-23 17:09:05 -0800437 SkPaint* getPaint(OpenGLRenderer& renderer) {
438 return renderer.filterPaint((SkPaint*) getInt());
Romain Guyb051e892010-09-28 19:09:36 -0700439 }
440
Romain Guy0fe478e2010-11-08 12:08:41 -0800441 DisplayList* getDisplayList() {
442 return (DisplayList*) getInt();
443 }
444
Romain Guyb051e892010-09-28 19:09:36 -0700445 inline float getFloat() {
446 return mReader.readScalar();
447 }
448
449 int32_t* getInts(uint32_t& count) {
450 count = getInt();
451 return (int32_t*) mReader.skip(count * sizeof(int32_t));
452 }
453
Romain Guy4bb94202010-10-12 15:59:26 -0700454 uint32_t* getUInts(int8_t& count) {
455 count = getInt();
456 return (uint32_t*) mReader.skip(count * sizeof(uint32_t));
457 }
458
Romain Guy33f6beb2012-02-16 19:24:51 -0800459 float* getFloats(int32_t& count) {
Romain Guyb051e892010-09-28 19:09:36 -0700460 count = getInt();
461 return (float*) mReader.skip(count * sizeof(float));
462 }
463
464 void getText(TextContainer* text) {
465 size_t length = text->mByteLength = getInt();
466 text->mText = (const char*) mReader.skip(length);
467 }
468
Chet Haase5c13d892010-10-08 08:37:55 -0700469 Vector<SkBitmap*> mBitmapResources;
Chet Haasead93c2b2010-10-22 16:17:12 -0700470 Vector<SkiaColorFilter*> mFilterResources;
Romain Guyb051e892010-09-28 19:09:36 -0700471
Chet Haased98aa2d2010-10-25 15:47:32 -0700472 Vector<SkPaint*> mPaints;
Romain Guy2fc941e2011-02-03 15:06:05 -0800473 Vector<SkPath*> mPaths;
Chet Haased98aa2d2010-10-25 15:47:32 -0700474 Vector<SkMatrix*> mMatrices;
Romain Guy24c00212011-01-14 15:31:00 -0800475 Vector<SkiaShader*> mShaders;
Chet Haased98aa2d2010-10-25 15:47:32 -0700476
Romain Guyb051e892010-09-28 19:09:36 -0700477 mutable SkFlattenableReadBuffer mReader;
Romain Guy65b345f2011-07-27 18:51:50 -0700478
479 size_t mSize;
Romain Guy04c9d8c2011-08-25 14:01:48 -0700480
481 bool mIsRenderable;
Romain Guy13631f32012-01-30 17:41:55 -0800482
483 String8 mName;
Chet Haasea1cff502012-02-21 13:43:44 -0800484
485 // View properties
486 float mApplicationScale;
487 bool mClipChildren;
488 float mAlpha;
489 int mMultipliedAlpha;
490 float mTranslationX, mTranslationY;
491 float mRotation, mRotationX, mRotationY;
492 float mScaleX, mScaleY;
493 float mPivotX, mPivotY;
494 float mCameraDistance;
495 int mLeft, mTop, mRight, mBottom;
496 int mWidth, mHeight;
497 int mPrevWidth, mPrevHeight;
498 bool mPivotExplicitlySet;
499 bool mMatrixDirty;
500 bool mMatrixIsIdentity;
501 uint32_t mMatrixFlags;
502 SkMatrix* mTransformMatrix;
503 Sk3DView* mTransformCamera;
504 SkMatrix* mTransformMatrix3D;
505 bool mCaching;
Romain Guyb051e892010-09-28 19:09:36 -0700506};
507
508///////////////////////////////////////////////////////////////////////////////
509// Renderer
510///////////////////////////////////////////////////////////////////////////////
511
512/**
513 * Records drawing commands in a display list for latter playback.
514 */
515class DisplayListRenderer: public OpenGLRenderer {
516public:
Romain Guy79537452011-10-12 13:48:51 -0700517 ANDROID_API DisplayListRenderer();
518 virtual ~DisplayListRenderer();
Romain Guyb051e892010-09-28 19:09:36 -0700519
Romain Guy79537452011-10-12 13:48:51 -0700520 ANDROID_API DisplayList* getDisplayList(DisplayList* displayList);
Chet Haase5977baa2011-01-05 18:01:22 -0800521
Romain Guy79537452011-10-12 13:48:51 -0700522 virtual void setViewport(int width, int height);
523 virtual void prepareDirty(float left, float top, float right, float bottom, bool opaque);
524 virtual void finish();
Romain Guyb051e892010-09-28 19:09:36 -0700525
Romain Guy65549432012-03-26 16:45:05 -0700526 virtual status_t callDrawGLFunction(Functor *functor, Rect& dirty);
Romain Guy4aa90572010-09-26 18:40:37 -0700527
Romain Guy79537452011-10-12 13:48:51 -0700528 virtual void interrupt();
529 virtual void resume();
Chet Haasedaf98e92011-01-10 14:10:36 -0800530
Romain Guy79537452011-10-12 13:48:51 -0700531 virtual int save(int flags);
532 virtual void restore();
533 virtual void restoreToCount(int saveCount);
Romain Guy4aa90572010-09-26 18:40:37 -0700534
Romain Guy79537452011-10-12 13:48:51 -0700535 virtual int saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700536 SkPaint* p, int flags);
Romain Guy79537452011-10-12 13:48:51 -0700537 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Romain Guy5b3b3522010-10-27 18:57:51 -0700538 int alpha, int flags);
Romain Guy4aa90572010-09-26 18:40:37 -0700539
Romain Guy79537452011-10-12 13:48:51 -0700540 virtual void translate(float dx, float dy);
541 virtual void rotate(float degrees);
542 virtual void scale(float sx, float sy);
543 virtual void skew(float sx, float sy);
Romain Guy4aa90572010-09-26 18:40:37 -0700544
Romain Guy79537452011-10-12 13:48:51 -0700545 virtual void setMatrix(SkMatrix* matrix);
546 virtual void concatMatrix(SkMatrix* matrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700547
Romain Guy79537452011-10-12 13:48:51 -0700548 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guy4aa90572010-09-26 18:40:37 -0700549
Romain Guy65549432012-03-26 16:45:05 -0700550 virtual status_t drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
Romain Guy33f6beb2012-02-16 19:24:51 -0800551 Rect& dirty, int32_t flags, uint32_t level = 0);
Romain Guy79537452011-10-12 13:48:51 -0700552 virtual void drawLayer(Layer* layer, float x, float y, SkPaint* paint);
553 virtual void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
554 virtual void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
555 virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
Romain Guy4aa90572010-09-26 18:40:37 -0700556 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chet Haase5c13d892010-10-08 08:37:55 -0700557 float dstRight, float dstBottom, SkPaint* paint);
Romain Guy79537452011-10-12 13:48:51 -0700558 virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -0800559 float* vertices, int* colors, SkPaint* paint);
Romain Guy79537452011-10-12 13:48:51 -0700560 virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700561 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700562 float left, float top, float right, float bottom, SkPaint* paint);
Romain Guy79537452011-10-12 13:48:51 -0700563 virtual void drawColor(int color, SkXfermode::Mode mode);
564 virtual void drawRect(float left, float top, float right, float bottom, SkPaint* paint);
565 virtual void drawRoundRect(float left, float top, float right, float bottom,
Romain Guy01d58e42011-01-19 21:54:02 -0800566 float rx, float ry, SkPaint* paint);
Romain Guy79537452011-10-12 13:48:51 -0700567 virtual void drawCircle(float x, float y, float radius, SkPaint* paint);
568 virtual void drawOval(float left, float top, float right, float bottom, SkPaint* paint);
569 virtual void drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -0800570 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
Romain Guy79537452011-10-12 13:48:51 -0700571 virtual void drawPath(SkPath* path, SkPaint* paint);
572 virtual void drawLines(float* points, int count, SkPaint* paint);
573 virtual void drawPoints(float* points, int count, SkPaint* paint);
574 virtual void drawText(const char* text, int bytesCount, int count, float x, float y,
Romain Guyacdd4b92012-03-15 14:36:52 -0700575 SkPaint* paint, float length = -1.0f);
Romain Guy325740f2012-02-24 16:48:34 -0800576 virtual void drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
577 float hOffset, float vOffset, SkPaint* paint);
Romain Guyeb9a5362012-01-17 17:39:26 -0800578 virtual void drawPosText(const char* text, int bytesCount, int count, const float* positions,
579 SkPaint* paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700580
Romain Guy79537452011-10-12 13:48:51 -0700581 virtual void resetShader();
582 virtual void setupShader(SkiaShader* shader);
Romain Guy4aa90572010-09-26 18:40:37 -0700583
Romain Guy79537452011-10-12 13:48:51 -0700584 virtual void resetColorFilter();
585 virtual void setupColorFilter(SkiaColorFilter* filter);
Romain Guy4aa90572010-09-26 18:40:37 -0700586
Romain Guy79537452011-10-12 13:48:51 -0700587 virtual void resetShadow();
588 virtual void setupShadow(float radius, float dx, float dy, int color);
Romain Guy4aa90572010-09-26 18:40:37 -0700589
Romain Guy5ff9df62012-01-23 17:09:05 -0800590 virtual void resetPaintFilter();
591 virtual void setupPaintFilter(int clearBits, int setBits);
592
Romain Guy79537452011-10-12 13:48:51 -0700593 ANDROID_API void reset();
Romain Guy4aa90572010-09-26 18:40:37 -0700594
Romain Guyb051e892010-09-28 19:09:36 -0700595 const SkWriter32& writeStream() const {
596 return mWriter;
597 }
598
Chet Haase5c13d892010-10-08 08:37:55 -0700599 const Vector<SkBitmap*>& getBitmapResources() const {
600 return mBitmapResources;
Romain Guyb051e892010-09-28 19:09:36 -0700601 }
602
Romain Guyd586ad92011-06-22 16:14:36 -0700603 const Vector<SkiaColorFilter*>& getFilterResources() const {
604 return mFilterResources;
605 }
606
Romain Guy24c00212011-01-14 15:31:00 -0800607 const Vector<SkiaShader*>& getShaders() const {
608 return mShaders;
Romain Guyb051e892010-09-28 19:09:36 -0700609 }
610
Chet Haased98aa2d2010-10-25 15:47:32 -0700611 const Vector<SkPaint*>& getPaints() const {
612 return mPaints;
613 }
614
Romain Guy2fc941e2011-02-03 15:06:05 -0800615 const Vector<SkPath*>& getPaths() const {
616 return mPaths;
617 }
618
Chet Haased98aa2d2010-10-25 15:47:32 -0700619 const Vector<SkMatrix*>& getMatrices() const {
620 return mMatrices;
621 }
622
Romain Guy4aa90572010-09-26 18:40:37 -0700623private:
Romain Guy27454a42011-01-23 12:01:41 -0800624 void insertRestoreToCount() {
625 if (mRestoreSaveCount >= 0) {
626 mWriter.writeInt(DisplayList::RestoreToCount);
627 addInt(mRestoreSaveCount);
628 mRestoreSaveCount = -1;
629 }
630 }
631
Romain Guy33f6beb2012-02-16 19:24:51 -0800632 void insertTranlate() {
633 if (mHasTranslate) {
634 if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
635 mWriter.writeInt(DisplayList::Translate);
636 addPoint(mTranslateX, mTranslateY);
637 mTranslateX = mTranslateY = 0.0f;
638 }
639 mHasTranslate = false;
640 }
641 }
642
643 inline void addOp(const DisplayList::Op drawOp) {
Romain Guy27454a42011-01-23 12:01:41 -0800644 insertRestoreToCount();
Romain Guy33f6beb2012-02-16 19:24:51 -0800645 insertTranlate();
Romain Guy4aa90572010-09-26 18:40:37 -0700646 mWriter.writeInt(drawOp);
Romain Guy04c9d8c2011-08-25 14:01:48 -0700647 mHasDrawOps = mHasDrawOps || drawOp >= DisplayList::DrawDisplayList;
Romain Guy4aa90572010-09-26 18:40:37 -0700648 }
649
Romain Guy33f6beb2012-02-16 19:24:51 -0800650 uint32_t* addOp(const DisplayList::Op drawOp, const bool reject) {
651 insertRestoreToCount();
652 insertTranlate();
653 mHasDrawOps = mHasDrawOps || drawOp >= DisplayList::DrawDisplayList;
654 if (reject) {
655 mWriter.writeInt(OP_MAY_BE_SKIPPED_MASK | drawOp);
Romain Guyad1daaa2012-03-01 17:33:42 -0800656 mWriter.writeInt(0xdeaddead);
Romain Guy390f8822012-03-13 18:00:10 -0700657 mBufferSize = mWriter.size();
658 return mWriter.peek32(mBufferSize - sizeof(int32_t));
Romain Guy33f6beb2012-02-16 19:24:51 -0800659 }
660 mWriter.writeInt(drawOp);
661 return NULL;
662 }
663
664 inline void addSkip(uint32_t* location) {
665 if (location) {
Romain Guy390f8822012-03-13 18:00:10 -0700666 *location = (int32_t) (mWriter.size() - mBufferSize);
Romain Guy33f6beb2012-02-16 19:24:51 -0800667 }
668 }
669
670 inline void addInt(int32_t value) {
Romain Guy4aa90572010-09-26 18:40:37 -0700671 mWriter.writeInt(value);
672 }
673
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700674 inline void addSize(uint32_t w, uint32_t h) {
675 mWriter.writeInt(w);
676 mWriter.writeInt(h);
677 }
678
Romain Guy4aa90572010-09-26 18:40:37 -0700679 void addInts(const int32_t* values, uint32_t count) {
Romain Guyb051e892010-09-28 19:09:36 -0700680 mWriter.writeInt(count);
Romain Guy4aa90572010-09-26 18:40:37 -0700681 for (uint32_t i = 0; i < count; i++) {
682 mWriter.writeInt(values[i]);
683 }
684 }
685
Romain Guy4bb94202010-10-12 15:59:26 -0700686 void addUInts(const uint32_t* values, int8_t count) {
687 mWriter.writeInt(count);
688 for (int8_t i = 0; i < count; i++) {
689 mWriter.writeInt(values[i]);
690 }
691 }
692
Romain Guy4aa90572010-09-26 18:40:37 -0700693 inline void addFloat(float value) {
694 mWriter.writeScalar(value);
695 }
696
Romain Guy33f6beb2012-02-16 19:24:51 -0800697 void addFloats(const float* values, int32_t count) {
Romain Guyb051e892010-09-28 19:09:36 -0700698 mWriter.writeInt(count);
Romain Guy33f6beb2012-02-16 19:24:51 -0800699 for (int32_t i = 0; i < count; i++) {
Romain Guy4aa90572010-09-26 18:40:37 -0700700 mWriter.writeScalar(values[i]);
701 }
702 }
703
704 inline void addPoint(float x, float y) {
705 mWriter.writeScalar(x);
706 mWriter.writeScalar(y);
707 }
708
709 inline void addBounds(float left, float top, float right, float bottom) {
710 mWriter.writeScalar(left);
711 mWriter.writeScalar(top);
712 mWriter.writeScalar(right);
713 mWriter.writeScalar(bottom);
714 }
715
716 inline void addText(const void* text, size_t byteLength) {
717 mWriter.writeInt(byteLength);
718 mWriter.writePad(text, byteLength);
719 }
720
Romain Guy2fc941e2011-02-03 15:06:05 -0800721 inline void addPath(SkPath* path) {
722 if (!path) {
723 addInt((int) NULL);
724 return;
Romain Guy4aa90572010-09-26 18:40:37 -0700725 }
Romain Guy2fc941e2011-02-03 15:06:05 -0800726
727 SkPath* pathCopy = mPathMap.valueFor(path);
728 if (pathCopy == NULL || pathCopy->getGenerationID() != path->getGenerationID()) {
Romain Guyb29cfbf2011-03-18 16:24:19 -0700729 pathCopy = new SkPath(*path);
Romain Guy4bcb7462012-02-23 17:08:38 -0800730 pathCopy->setSourcePath(path);
Romain Guy96ebc6b2012-02-21 18:32:32 -0800731 // replaceValueFor() performs an add if the entry doesn't exist
732 mPathMap.replaceValueFor(path, pathCopy);
Romain Guyb29cfbf2011-03-18 16:24:19 -0700733 mPaths.add(pathCopy);
Romain Guy2fc941e2011-02-03 15:06:05 -0800734 }
735
736 addInt((int) pathCopy);
Romain Guy4aa90572010-09-26 18:40:37 -0700737 }
738
Chet Haase5c13d892010-10-08 08:37:55 -0700739 inline void addPaint(SkPaint* paint) {
Romain Guy24c00212011-01-14 15:31:00 -0800740 if (!paint) {
Romain Guy0fe478e2010-11-08 12:08:41 -0800741 addInt((int) NULL);
Chet Haased98aa2d2010-10-25 15:47:32 -0700742 return;
743 }
Romain Guy0fe478e2010-11-08 12:08:41 -0800744
Romain Guy22d41842012-01-19 18:33:25 -0800745 SkPaint* paintCopy = mPaintMap.valueFor(paint);
Chet Haased98aa2d2010-10-25 15:47:32 -0700746 if (paintCopy == NULL || paintCopy->getGenerationID() != paint->getGenerationID()) {
747 paintCopy = new SkPaint(*paint);
Romain Guy96ebc6b2012-02-21 18:32:32 -0800748 // replaceValueFor() performs an add if the entry doesn't exist
749 mPaintMap.replaceValueFor(paint, paintCopy);
Chet Haased98aa2d2010-10-25 15:47:32 -0700750 mPaints.add(paintCopy);
751 }
Romain Guy0fe478e2010-11-08 12:08:41 -0800752
753 addInt((int) paintCopy);
754 }
755
756 inline void addDisplayList(DisplayList* displayList) {
757 // TODO: To be safe, the display list should be ref-counted in the
758 // resources cache, but we rely on the caller (UI toolkit) to
759 // do the right thing for now
760 addInt((int) displayList);
Romain Guy4aa90572010-09-26 18:40:37 -0700761 }
762
Chet Haase5c13d892010-10-08 08:37:55 -0700763 inline void addMatrix(SkMatrix* matrix) {
Chet Haased98aa2d2010-10-25 15:47:32 -0700764 // Copying the matrix is cheap and prevents against the user changing the original
765 // matrix before the operation that uses it
Romain Guyf6a63ae2011-06-22 15:13:09 -0700766 SkMatrix* copy = new SkMatrix(*matrix);
767 addInt((int) copy);
768 mMatrices.add(copy);
Romain Guy4aa90572010-09-26 18:40:37 -0700769 }
770
Chet Haase5c13d892010-10-08 08:37:55 -0700771 inline void addBitmap(SkBitmap* bitmap) {
Chet Haased98aa2d2010-10-25 15:47:32 -0700772 // Note that this assumes the bitmap is immutable. There are cases this won't handle
773 // correctly, such as creating the bitmap from scratch, drawing with it, changing its
774 // contents, and drawing again. The only fix would be to always copy it the first time,
775 // which doesn't seem worth the extra cycles for this unlikely case.
Romain Guy0fe478e2010-11-08 12:08:41 -0800776 addInt((int) bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700777 mBitmapResources.add(bitmap);
Romain Guyd586ad92011-06-22 16:14:36 -0700778 Caches::getInstance().resourceCache.incrementRefcount(bitmap);
Chet Haase5c13d892010-10-08 08:37:55 -0700779 }
Romain Guy4aa90572010-09-26 18:40:37 -0700780
Chet Haase5c13d892010-10-08 08:37:55 -0700781 inline void addShader(SkiaShader* shader) {
Romain Guy24c00212011-01-14 15:31:00 -0800782 if (!shader) {
783 addInt((int) NULL);
784 return;
785 }
786
787 SkiaShader* shaderCopy = mShaderMap.valueFor(shader);
788 // TODO: We also need to handle generation ID changes in compose shaders
Romain Guy1f1fcb72011-01-14 15:37:54 -0800789 if (shaderCopy == NULL || shaderCopy->getGenerationId() != shader->getGenerationId()) {
Romain Guy24c00212011-01-14 15:31:00 -0800790 shaderCopy = shader->copy();
Romain Guy96ebc6b2012-02-21 18:32:32 -0800791 // replaceValueFor() performs an add if the entry doesn't exist
792 mShaderMap.replaceValueFor(shader, shaderCopy);
Romain Guy1f1fcb72011-01-14 15:37:54 -0800793 mShaders.add(shaderCopy);
Romain Guy43ccf462011-01-14 18:51:01 -0800794 Caches::getInstance().resourceCache.incrementRefcount(shaderCopy);
Romain Guy24c00212011-01-14 15:31:00 -0800795 }
796
797 addInt((int) shaderCopy);
Romain Guy4aa90572010-09-26 18:40:37 -0700798 }
799
Chet Haasead93c2b2010-10-22 16:17:12 -0700800 inline void addColorFilter(SkiaColorFilter* colorFilter) {
Romain Guy0fe478e2010-11-08 12:08:41 -0800801 addInt((int) colorFilter);
Chet Haasead93c2b2010-10-22 16:17:12 -0700802 mFilterResources.add(colorFilter);
Romain Guyd586ad92011-06-22 16:14:36 -0700803 Caches::getInstance().resourceCache.incrementRefcount(colorFilter);
Chet Haasead93c2b2010-10-22 16:17:12 -0700804 }
805
Chet Haase5c13d892010-10-08 08:37:55 -0700806 Vector<SkBitmap*> mBitmapResources;
Chet Haasead93c2b2010-10-22 16:17:12 -0700807 Vector<SkiaColorFilter*> mFilterResources;
Romain Guy4aa90572010-09-26 18:40:37 -0700808
Chet Haased98aa2d2010-10-25 15:47:32 -0700809 Vector<SkPaint*> mPaints;
Romain Guy0fe478e2010-11-08 12:08:41 -0800810 DefaultKeyedVector<SkPaint*, SkPaint*> mPaintMap;
Romain Guy24c00212011-01-14 15:31:00 -0800811
Romain Guy2fc941e2011-02-03 15:06:05 -0800812 Vector<SkPath*> mPaths;
813 DefaultKeyedVector<SkPath*, SkPath*> mPathMap;
814
Romain Guy24c00212011-01-14 15:31:00 -0800815 Vector<SkiaShader*> mShaders;
816 DefaultKeyedVector<SkiaShader*, SkiaShader*> mShaderMap;
817
Chet Haased98aa2d2010-10-25 15:47:32 -0700818 Vector<SkMatrix*> mMatrices;
819
Romain Guy4aa90572010-09-26 18:40:37 -0700820 SkWriter32 mWriter;
Romain Guy390f8822012-03-13 18:00:10 -0700821 uint32_t mBufferSize;
Romain Guy4aa90572010-09-26 18:40:37 -0700822
Romain Guy27454a42011-01-23 12:01:41 -0800823 int mRestoreSaveCount;
Romain Guy33f6beb2012-02-16 19:24:51 -0800824
825 float mTranslateX;
826 float mTranslateY;
827 bool mHasTranslate;
828
Romain Guy04c9d8c2011-08-25 14:01:48 -0700829 bool mHasDrawOps;
Romain Guy27454a42011-01-23 12:01:41 -0800830
Romain Guyb051e892010-09-28 19:09:36 -0700831 friend class DisplayList;
832
Romain Guy4aa90572010-09-26 18:40:37 -0700833}; // class DisplayListRenderer
834
835}; // namespace uirenderer
836}; // namespace android
837
Romain Guy5b3b3522010-10-27 18:57:51 -0700838#endif // ANDROID_HWUI_DISPLAY_LIST_RENDERER_H