blob: bf1182ca4fb7fef519549cf9121804e8fec0a36a [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include "DisplayListRenderer.h"
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
Romain Guyb051e892010-09-28 19:09:36 -070025// Display list
26///////////////////////////////////////////////////////////////////////////////
27
Romain Guyffac7fc2011-01-13 17:21:49 -080028const char* DisplayList::OP_NAMES[] = {
Romain Guyffac7fc2011-01-13 17:21:49 -080029 "Save",
30 "Restore",
31 "RestoreToCount",
32 "SaveLayer",
33 "SaveLayerAlpha",
34 "Translate",
35 "Rotate",
36 "Scale",
Romain Guy4cf6e2f2011-01-23 11:35:13 -080037 "Skew",
Romain Guyffac7fc2011-01-13 17:21:49 -080038 "SetMatrix",
39 "ConcatMatrix",
40 "ClipRect",
41 "DrawDisplayList",
42 "DrawLayer",
43 "DrawBitmap",
44 "DrawBitmapMatrix",
45 "DrawBitmapRect",
Romain Guy5a7b4662011-01-20 19:09:30 -080046 "DrawBitmapMesh",
Romain Guyffac7fc2011-01-13 17:21:49 -080047 "DrawPatch",
48 "DrawColor",
49 "DrawRect",
Romain Guy01d58e42011-01-19 21:54:02 -080050 "DrawRoundRect",
51 "DrawCircle",
Romain Guyc1cd9ba32011-01-23 14:18:41 -080052 "DrawOval",
Romain Guy8b2f5262011-01-23 16:15:02 -080053 "DrawArc",
Romain Guyffac7fc2011-01-13 17:21:49 -080054 "DrawPath",
55 "DrawLines",
Romain Guyed6fcb02011-03-21 13:11:28 -070056 "DrawPoints",
Romain Guyffac7fc2011-01-13 17:21:49 -080057 "DrawText",
58 "ResetShader",
59 "SetupShader",
60 "ResetColorFilter",
61 "SetupColorFilter",
62 "ResetShadow",
Chet Haasedaf98e92011-01-10 14:10:36 -080063 "SetupShadow",
64 "DrawGLFunction"
Romain Guyffac7fc2011-01-13 17:21:49 -080065};
66
Romain Guyb051e892010-09-28 19:09:36 -070067DisplayList::DisplayList(const DisplayListRenderer& recorder) {
Chet Haase5977baa2011-01-05 18:01:22 -080068 initFromDisplayListRenderer(recorder);
69}
70
71DisplayList::~DisplayList() {
Chet Haased63cbd12011-02-03 16:32:46 -080072 clearResources();
73}
74
75void DisplayList::clearResources() {
Chet Haase5977baa2011-01-05 18:01:22 -080076 sk_free((void*) mReader.base());
77
78 Caches& caches = Caches::getInstance();
79
80 for (size_t i = 0; i < mBitmapResources.size(); i++) {
81 caches.resourceCache.decrementRefcount(mBitmapResources.itemAt(i));
82 }
83 mBitmapResources.clear();
84
Romain Guy24c00212011-01-14 15:31:00 -080085 for (size_t i = 0; i < mShaders.size(); i++) {
Romain Guy43ccf462011-01-14 18:51:01 -080086 caches.resourceCache.decrementRefcount(mShaders.itemAt(i));
Chet Haase5977baa2011-01-05 18:01:22 -080087 }
Romain Guy24c00212011-01-14 15:31:00 -080088 mShaders.clear();
Chet Haase5977baa2011-01-05 18:01:22 -080089
90 for (size_t i = 0; i < mPaints.size(); i++) {
91 delete mPaints.itemAt(i);
92 }
93 mPaints.clear();
94
Romain Guy2fc941e2011-02-03 15:06:05 -080095 for (size_t i = 0; i < mPaths.size(); i++) {
96 delete mPaths.itemAt(i);
97 }
98 mPaths.clear();
99
Chet Haase5977baa2011-01-05 18:01:22 -0800100 for (size_t i = 0; i < mMatrices.size(); i++) {
101 delete mMatrices.itemAt(i);
102 }
103 mMatrices.clear();
Chet Haase5977baa2011-01-05 18:01:22 -0800104}
105
Chet Haased63cbd12011-02-03 16:32:46 -0800106void DisplayList::initFromDisplayListRenderer(const DisplayListRenderer& recorder, bool reusing) {
Romain Guyb051e892010-09-28 19:09:36 -0700107 const SkWriter32& writer = recorder.writeStream();
108 init();
109
110 if (writer.size() == 0) {
111 return;
112 }
113
Chet Haased63cbd12011-02-03 16:32:46 -0800114 if (reusing) {
115 // re-using display list - clear out previous allocations
116 clearResources();
117 }
118
Romain Guyb051e892010-09-28 19:09:36 -0700119 size_t size = writer.size();
120 void* buffer = sk_malloc_throw(size);
121 writer.flatten(buffer);
122 mReader.setMemory(buffer, size);
123
Chet Haase5c13d892010-10-08 08:37:55 -0700124 Caches& caches = Caches::getInstance();
Romain Guyb051e892010-09-28 19:09:36 -0700125
Chet Haase5c13d892010-10-08 08:37:55 -0700126 const Vector<SkBitmap*> &bitmapResources = recorder.getBitmapResources();
127 for (size_t i = 0; i < bitmapResources.size(); i++) {
128 SkBitmap* resource = bitmapResources.itemAt(i);
129 mBitmapResources.add(resource);
130 caches.resourceCache.incrementRefcount(resource);
Romain Guyb051e892010-09-28 19:09:36 -0700131 }
Chet Haased98aa2d2010-10-25 15:47:32 -0700132
Romain Guy24c00212011-01-14 15:31:00 -0800133 const Vector<SkiaShader*> &shaders = recorder.getShaders();
134 for (size_t i = 0; i < shaders.size(); i++) {
Romain Guy43ccf462011-01-14 18:51:01 -0800135 SkiaShader* shader = shaders.itemAt(i);
136 mShaders.add(shader);
137 caches.resourceCache.incrementRefcount(shader);
Romain Guyb051e892010-09-28 19:09:36 -0700138 }
139
Chet Haased98aa2d2010-10-25 15:47:32 -0700140 const Vector<SkPaint*> &paints = recorder.getPaints();
141 for (size_t i = 0; i < paints.size(); i++) {
142 mPaints.add(paints.itemAt(i));
143 }
144
Romain Guy2fc941e2011-02-03 15:06:05 -0800145 const Vector<SkPath*> &paths = recorder.getPaths();
146 for (size_t i = 0; i < paths.size(); i++) {
147 mPaths.add(paths.itemAt(i));
148 }
149
Chet Haased98aa2d2010-10-25 15:47:32 -0700150 const Vector<SkMatrix*> &matrices = recorder.getMatrices();
151 for (size_t i = 0; i < matrices.size(); i++) {
152 mMatrices.add(matrices.itemAt(i));
153 }
Romain Guyb051e892010-09-28 19:09:36 -0700154}
155
Romain Guyb051e892010-09-28 19:09:36 -0700156void DisplayList::init() {
Romain Guyb051e892010-09-28 19:09:36 -0700157}
158
Romain Guycabfcc12011-03-07 18:06:46 -0800159bool DisplayList::replay(OpenGLRenderer& renderer, Rect& dirty, uint32_t level) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800160 bool needsInvalidate = false;
Romain Guyb051e892010-09-28 19:09:36 -0700161 TextContainer text;
162 mReader.rewind();
163
Romain Guyffac7fc2011-01-13 17:21:49 -0800164#if DEBUG_DISPLAY_LIST
165 uint32_t count = (level + 1) * 2;
166 char indent[count + 1];
167 for (uint32_t i = 0; i < count; i++) {
168 indent[i] = ' ';
169 }
170 indent[count] = '\0';
171 DISPLAY_LIST_LOGD("%sStart display list (%p)", (char*) indent + 2, this);
172#endif
Romain Guyb051e892010-09-28 19:09:36 -0700173
Romain Guyffac7fc2011-01-13 17:21:49 -0800174 int saveCount = renderer.getSaveCount() - 1;
Romain Guyb051e892010-09-28 19:09:36 -0700175 while (!mReader.eof()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700176 int op = mReader.readInt();
Romain Guyffac7fc2011-01-13 17:21:49 -0800177
Romain Guy5b3b3522010-10-27 18:57:51 -0700178 switch (op) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800179 case DrawGLFunction: {
180 Functor *functor = (Functor *) getInt();
181 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], functor);
Romain Guycabfcc12011-03-07 18:06:46 -0800182 needsInvalidate |= renderer.callDrawGLFunction(functor, dirty);
Chet Haasedaf98e92011-01-10 14:10:36 -0800183 }
184 break;
Romain Guyb051e892010-09-28 19:09:36 -0700185 case Save: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800186 int rendererNum = getInt();
187 DISPLAY_LIST_LOGD("%s%s %d", (char*) indent, OP_NAMES[op], rendererNum);
188 renderer.save(rendererNum);
Romain Guyb051e892010-09-28 19:09:36 -0700189 }
190 break;
191 case Restore: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800192 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700193 renderer.restore();
194 }
195 break;
196 case RestoreToCount: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800197 int restoreCount = saveCount + getInt();
198 DISPLAY_LIST_LOGD("%s%s %d", (char*) indent, OP_NAMES[op], restoreCount);
199 renderer.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -0700200 }
201 break;
202 case SaveLayer: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800203 float f1 = getFloat();
204 float f2 = getFloat();
205 float f3 = getFloat();
206 float f4 = getFloat();
207 SkPaint* paint = getPaint();
208 int flags = getInt();
209 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p, 0x%x", (char*) indent,
210 OP_NAMES[op], f1, f2, f3, f4, paint, flags);
211 renderer.saveLayer(f1, f2, f3, f4, paint, flags);
Romain Guyb051e892010-09-28 19:09:36 -0700212 }
213 break;
Romain Guy5b3b3522010-10-27 18:57:51 -0700214 case SaveLayerAlpha: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800215 float f1 = getFloat();
216 float f2 = getFloat();
217 float f3 = getFloat();
218 float f4 = getFloat();
219 int alpha = getInt();
220 int flags = getInt();
221 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %d, 0x%x", (char*) indent,
222 OP_NAMES[op], f1, f2, f3, f4, alpha, flags);
223 renderer.saveLayerAlpha(f1, f2, f3, f4, alpha, flags);
Romain Guy5b3b3522010-10-27 18:57:51 -0700224 }
225 break;
Romain Guyb051e892010-09-28 19:09:36 -0700226 case Translate: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800227 float f1 = getFloat();
228 float f2 = getFloat();
229 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], f1, f2);
230 renderer.translate(f1, f2);
Romain Guyb051e892010-09-28 19:09:36 -0700231 }
232 break;
233 case Rotate: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800234 float rotation = getFloat();
235 DISPLAY_LIST_LOGD("%s%s %.2f", (char*) indent, OP_NAMES[op], rotation);
236 renderer.rotate(rotation);
Romain Guyb051e892010-09-28 19:09:36 -0700237 }
238 break;
239 case Scale: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800240 float sx = getFloat();
241 float sy = getFloat();
242 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], sx, sy);
243 renderer.scale(sx, sy);
Romain Guyb051e892010-09-28 19:09:36 -0700244 }
245 break;
Romain Guy807daf72011-01-18 11:19:19 -0800246 case Skew: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800247 float sx = getFloat();
248 float sy = getFloat();
249 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], sx, sy);
250 renderer.skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -0800251 }
252 break;
Romain Guyb051e892010-09-28 19:09:36 -0700253 case SetMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800254 SkMatrix* matrix = getMatrix();
255 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], matrix);
256 renderer.setMatrix(matrix);
Romain Guyb051e892010-09-28 19:09:36 -0700257 }
258 break;
259 case ConcatMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800260 SkMatrix* matrix = getMatrix();
261 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], matrix);
262 renderer.concatMatrix(matrix);
Romain Guyb051e892010-09-28 19:09:36 -0700263 }
264 break;
265 case ClipRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800266 float f1 = getFloat();
267 float f2 = getFloat();
268 float f3 = getFloat();
269 float f4 = getFloat();
270 int regionOp = getInt();
271 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %d", (char*) indent, OP_NAMES[op],
272 f1, f2, f3, f4, regionOp);
273 renderer.clipRect(f1, f2, f3, f4, (SkRegion::Op) regionOp);
Romain Guyb051e892010-09-28 19:09:36 -0700274 }
275 break;
Romain Guy0fe478e2010-11-08 12:08:41 -0800276 case DrawDisplayList: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800277 DisplayList* displayList = getDisplayList();
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700278 uint32_t width = getUInt();
279 uint32_t height = getUInt();
280 DISPLAY_LIST_LOGD("%s%s %p, %dx%d, %d", (char*) indent, OP_NAMES[op],
281 displayList, width, height, level + 1);
282 needsInvalidate |= renderer.drawDisplayList(displayList, width, height,
283 dirty, level + 1);
Romain Guy0fe478e2010-11-08 12:08:41 -0800284 }
285 break;
Romain Guy6c319ca2011-01-11 14:29:25 -0800286 case DrawLayer: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800287 Layer* layer = (Layer*) getInt();
288 float x = getFloat();
289 float y = getFloat();
290 SkPaint* paint = getPaint();
291 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
292 layer, x, y, paint);
293 renderer.drawLayer(layer, x, y, paint);
Romain Guy6c319ca2011-01-11 14:29:25 -0800294 }
295 break;
Romain Guyb051e892010-09-28 19:09:36 -0700296 case DrawBitmap: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800297 SkBitmap* bitmap = getBitmap();
298 float x = getFloat();
299 float y = getFloat();
300 SkPaint* paint = getPaint();
301 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
302 bitmap, x, y, paint);
303 renderer.drawBitmap(bitmap, x, y, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700304 }
305 break;
306 case DrawBitmapMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800307 SkBitmap* bitmap = getBitmap();
308 SkMatrix* matrix = getMatrix();
309 SkPaint* paint = getPaint();
310 DISPLAY_LIST_LOGD("%s%s %p, %p, %p", (char*) indent, OP_NAMES[op],
311 bitmap, matrix, paint);
312 renderer.drawBitmap(bitmap, matrix, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700313 }
314 break;
315 case DrawBitmapRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800316 SkBitmap* bitmap = getBitmap();
317 float f1 = getFloat();
318 float f2 = getFloat();
319 float f3 = getFloat();
320 float f4 = getFloat();
321 float f5 = getFloat();
322 float f6 = getFloat();
323 float f7 = getFloat();
324 float f8 = getFloat();
325 SkPaint* paint = getPaint();
326 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p",
327 (char*) indent, OP_NAMES[op], bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
328 renderer.drawBitmap(bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700329 }
330 break;
Romain Guy5a7b4662011-01-20 19:09:30 -0800331 case DrawBitmapMesh: {
332 int verticesCount = 0;
333 uint32_t colorsCount = 0;
334
335 SkBitmap* bitmap = getBitmap();
336 uint32_t meshWidth = getInt();
337 uint32_t meshHeight = getInt();
338 float* vertices = getFloats(verticesCount);
339 bool hasColors = getInt();
340 int* colors = hasColors ? getInts(colorsCount) : NULL;
341
Chet Haasedaf98e92011-01-10 14:10:36 -0800342 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guy5a7b4662011-01-20 19:09:30 -0800343 renderer.drawBitmapMesh(bitmap, meshWidth, meshHeight, vertices, colors, getPaint());
344 }
Romain Guya566b7c2011-01-23 16:36:11 -0800345 break;
Romain Guyb051e892010-09-28 19:09:36 -0700346 case DrawPatch: {
347 int32_t* xDivs = NULL;
348 int32_t* yDivs = NULL;
Romain Guy4bb94202010-10-12 15:59:26 -0700349 uint32_t* colors = NULL;
Romain Guyb051e892010-09-28 19:09:36 -0700350 uint32_t xDivsCount = 0;
351 uint32_t yDivsCount = 0;
Romain Guy4bb94202010-10-12 15:59:26 -0700352 int8_t numColors = 0;
Romain Guyb051e892010-09-28 19:09:36 -0700353
354 SkBitmap* bitmap = getBitmap();
355
356 xDivs = getInts(xDivsCount);
357 yDivs = getInts(yDivsCount);
Romain Guy4bb94202010-10-12 15:59:26 -0700358 colors = getUInts(numColors);
Romain Guyb051e892010-09-28 19:09:36 -0700359
Chet Haasedaf98e92011-01-10 14:10:36 -0800360 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guy4bb94202010-10-12 15:59:26 -0700361 renderer.drawPatch(bitmap, xDivs, yDivs, colors, xDivsCount, yDivsCount,
362 numColors, getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
Romain Guyb051e892010-09-28 19:09:36 -0700363 }
364 break;
365 case DrawColor: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800366 int color = getInt();
367 int xferMode = getInt();
368 DISPLAY_LIST_LOGD("%s%s 0x%x %d", (char*) indent, OP_NAMES[op], color, xferMode);
369 renderer.drawColor(color, (SkXfermode::Mode) xferMode);
Romain Guyb051e892010-09-28 19:09:36 -0700370 }
371 break;
372 case DrawRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800373 float f1 = getFloat();
374 float f2 = getFloat();
375 float f3 = getFloat();
376 float f4 = getFloat();
377 SkPaint* paint = getPaint();
378 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
379 f1, f2, f3, f4, paint);
380 renderer.drawRect(f1, f2, f3, f4, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700381 }
382 break;
Romain Guy01d58e42011-01-19 21:54:02 -0800383 case DrawRoundRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800384 float f1 = getFloat();
385 float f2 = getFloat();
386 float f3 = getFloat();
387 float f4 = getFloat();
388 float f5 = getFloat();
389 float f6 = getFloat();
390 SkPaint* paint = getPaint();
391 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p",
392 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, f5, f6, paint);
393 renderer.drawRoundRect(f1, f2, f3, f4, f5, f6, paint);
Romain Guy01d58e42011-01-19 21:54:02 -0800394 }
395 break;
396 case DrawCircle: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800397 float f1 = getFloat();
398 float f2 = getFloat();
399 float f3 = getFloat();
400 SkPaint* paint = getPaint();
401 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %p",
402 (char*) indent, OP_NAMES[op], f1, f2, f3, paint);
403 renderer.drawCircle(f1, f2, f3, paint);
Romain Guy01d58e42011-01-19 21:54:02 -0800404 }
405 break;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800406 case DrawOval: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800407 float f1 = getFloat();
408 float f2 = getFloat();
409 float f3 = getFloat();
410 float f4 = getFloat();
411 SkPaint* paint = getPaint();
412 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p",
413 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, paint);
414 renderer.drawOval(f1, f2, f3, f4, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800415 }
416 break;
Romain Guy8b2f5262011-01-23 16:15:02 -0800417 case DrawArc: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800418 float f1 = getFloat();
419 float f2 = getFloat();
420 float f3 = getFloat();
421 float f4 = getFloat();
422 float f5 = getFloat();
423 float f6 = getFloat();
424 int i1 = getInt();
425 SkPaint* paint = getPaint();
426 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p",
427 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, f5, f6, i1, paint);
428 renderer.drawArc(f1, f2, f3, f4, f5, f6, i1 == 1, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -0800429 }
430 break;
Romain Guyb051e892010-09-28 19:09:36 -0700431 case DrawPath: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800432 SkPath* path = getPath();
433 SkPaint* paint = getPaint();
434 DISPLAY_LIST_LOGD("%s%s %p, %p", (char*) indent, OP_NAMES[op], path, paint);
435 renderer.drawPath(path, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700436 }
437 break;
438 case DrawLines: {
439 int count = 0;
440 float* points = getFloats(count);
Chet Haasedaf98e92011-01-10 14:10:36 -0800441 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700442 renderer.drawLines(points, count, getPaint());
443 }
444 break;
Romain Guyed6fcb02011-03-21 13:11:28 -0700445 case DrawPoints: {
446 int count = 0;
447 float* points = getFloats(count);
448 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
449 renderer.drawPoints(points, count, getPaint());
450 }
451 break;
Romain Guyb051e892010-09-28 19:09:36 -0700452 case DrawText: {
453 getText(&text);
Chet Haasedaf98e92011-01-10 14:10:36 -0800454 int count = getInt();
455 float x = getFloat();
456 float y = getFloat();
457 SkPaint* paint = getPaint();
458 DISPLAY_LIST_LOGD("%s%s %s, %d, %d, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
459 text.text(), text.length(), count, x, y, paint);
460 renderer.drawText(text.text(), text.length(), count, x, y, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700461 }
462 break;
463 case ResetShader: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800464 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700465 renderer.resetShader();
466 }
467 break;
468 case SetupShader: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800469 SkiaShader* shader = getShader();
470 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], shader);
471 renderer.setupShader(shader);
Romain Guyb051e892010-09-28 19:09:36 -0700472 }
473 break;
474 case ResetColorFilter: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800475 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700476 renderer.resetColorFilter();
477 }
478 break;
479 case SetupColorFilter: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800480 SkiaColorFilter *colorFilter = getColorFilter();
481 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], colorFilter);
482 renderer.setupColorFilter(colorFilter);
Romain Guyb051e892010-09-28 19:09:36 -0700483 }
484 break;
485 case ResetShadow: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800486 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700487 renderer.resetShadow();
488 }
489 break;
490 case SetupShadow: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800491 float radius = getFloat();
492 float dx = getFloat();
493 float dy = getFloat();
494 int color = getInt();
495 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, 0x%x", (char*) indent, OP_NAMES[op],
496 radius, dx, dy, color);
497 renderer.setupShadow(radius, dx, dy, color);
Romain Guyb051e892010-09-28 19:09:36 -0700498 }
499 break;
Chet Haasedaf98e92011-01-10 14:10:36 -0800500 default:
501 DISPLAY_LIST_LOGD("Display List error: op not handled: %s%s",
502 (char*) indent, OP_NAMES[op]);
503 break;
Romain Guyb051e892010-09-28 19:09:36 -0700504 }
505 }
Romain Guyffac7fc2011-01-13 17:21:49 -0800506
Chet Haasedaf98e92011-01-10 14:10:36 -0800507 DISPLAY_LIST_LOGD("%sDone, returning %d", (char*) indent + 2, needsInvalidate);
508 return needsInvalidate;
Romain Guyb051e892010-09-28 19:09:36 -0700509}
510
511///////////////////////////////////////////////////////////////////////////////
Romain Guy4aa90572010-09-26 18:40:37 -0700512// Base structure
513///////////////////////////////////////////////////////////////////////////////
514
Romain Guy2fc941e2011-02-03 15:06:05 -0800515DisplayListRenderer::DisplayListRenderer(): mWriter(MIN_WRITER_SIZE) {
Chet Haase5977baa2011-01-05 18:01:22 -0800516 mDisplayList = NULL;
Romain Guy4aa90572010-09-26 18:40:37 -0700517}
518
519DisplayListRenderer::~DisplayListRenderer() {
520 reset();
521}
522
523void DisplayListRenderer::reset() {
Romain Guy4aa90572010-09-26 18:40:37 -0700524 mWriter.reset();
Chet Haase5c13d892010-10-08 08:37:55 -0700525
526 Caches& caches = Caches::getInstance();
527 for (size_t i = 0; i < mBitmapResources.size(); i++) {
528 SkBitmap* resource = mBitmapResources.itemAt(i);
529 caches.resourceCache.decrementRefcount(resource);
530 }
531 mBitmapResources.clear();
Chet Haased98aa2d2010-10-25 15:47:32 -0700532
Romain Guy43ccf462011-01-14 18:51:01 -0800533 for (size_t i = 0; i < mShaders.size(); i++) {
534 caches.resourceCache.decrementRefcount(mShaders.itemAt(i));
535 }
Romain Guy24c00212011-01-14 15:31:00 -0800536 mShaders.clear();
537 mShaderMap.clear();
Romain Guy43ccf462011-01-14 18:51:01 -0800538
539 mPaints.clear();
540 mPaintMap.clear();
Romain Guy2fc941e2011-02-03 15:06:05 -0800541 mPaths.clear();
542 mPathMap.clear();
Chet Haased98aa2d2010-10-25 15:47:32 -0700543 mMatrices.clear();
Romain Guy4aa90572010-09-26 18:40:37 -0700544}
545
546///////////////////////////////////////////////////////////////////////////////
547// Operations
548///////////////////////////////////////////////////////////////////////////////
549
Chet Haase5977baa2011-01-05 18:01:22 -0800550DisplayList* DisplayListRenderer::getDisplayList() {
551 if (mDisplayList == NULL) {
552 mDisplayList = new DisplayList(*this);
553 } else {
Chet Haased63cbd12011-02-03 16:32:46 -0800554 mDisplayList->initFromDisplayListRenderer(*this, true);
Chet Haase5977baa2011-01-05 18:01:22 -0800555 }
556 return mDisplayList;
557}
558
Romain Guyb051e892010-09-28 19:09:36 -0700559void DisplayListRenderer::setViewport(int width, int height) {
560 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
561
562 mWidth = width;
563 mHeight = height;
564}
565
Romain Guy7d7b5492011-01-24 16:33:45 -0800566void DisplayListRenderer::prepareDirty(float left, float top,
567 float right, float bottom, bool opaque) {
Romain Guyb051e892010-09-28 19:09:36 -0700568 mSnapshot = new Snapshot(mFirstSnapshot,
569 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
570 mSaveCount = 1;
571 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guy27454a42011-01-23 12:01:41 -0800572 mRestoreSaveCount = -1;
573}
574
575void DisplayListRenderer::finish() {
576 insertRestoreToCount();
577 OpenGLRenderer::finish();
Romain Guyb051e892010-09-28 19:09:36 -0700578}
579
Chet Haasedaf98e92011-01-10 14:10:36 -0800580void DisplayListRenderer::interrupt() {
Chet Haasedaf98e92011-01-10 14:10:36 -0800581}
Romain Guy2b1847e2011-01-26 13:43:01 -0800582
Chet Haasedaf98e92011-01-10 14:10:36 -0800583void DisplayListRenderer::resume() {
Romain Guy4aa90572010-09-26 18:40:37 -0700584}
585
Romain Guycabfcc12011-03-07 18:06:46 -0800586bool DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
587 // Ignore dirty during recording, it matters only when we replay
Chet Haasedaf98e92011-01-10 14:10:36 -0800588 addOp(DisplayList::DrawGLFunction);
589 addInt((int) functor);
590 return false; // No invalidate needed at record-time
591}
592
Romain Guy4aa90572010-09-26 18:40:37 -0700593int DisplayListRenderer::save(int flags) {
Romain Guyb051e892010-09-28 19:09:36 -0700594 addOp(DisplayList::Save);
Romain Guy4aa90572010-09-26 18:40:37 -0700595 addInt(flags);
596 return OpenGLRenderer::save(flags);
597}
598
599void DisplayListRenderer::restore() {
Romain Guyb051e892010-09-28 19:09:36 -0700600 addOp(DisplayList::Restore);
Romain Guy4aa90572010-09-26 18:40:37 -0700601 OpenGLRenderer::restore();
602}
603
604void DisplayListRenderer::restoreToCount(int saveCount) {
Romain Guy27454a42011-01-23 12:01:41 -0800605 mRestoreSaveCount = saveCount;
Romain Guy4aa90572010-09-26 18:40:37 -0700606 OpenGLRenderer::restoreToCount(saveCount);
607}
608
609int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700610 SkPaint* p, int flags) {
Romain Guyb051e892010-09-28 19:09:36 -0700611 addOp(DisplayList::SaveLayer);
Romain Guy4aa90572010-09-26 18:40:37 -0700612 addBounds(left, top, right, bottom);
613 addPaint(p);
614 addInt(flags);
Romain Guyb051e892010-09-28 19:09:36 -0700615 return OpenGLRenderer::save(flags);
Romain Guy4aa90572010-09-26 18:40:37 -0700616}
617
Romain Guy5b3b3522010-10-27 18:57:51 -0700618int DisplayListRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
619 int alpha, int flags) {
620 addOp(DisplayList::SaveLayerAlpha);
621 addBounds(left, top, right, bottom);
622 addInt(alpha);
623 addInt(flags);
624 return OpenGLRenderer::save(flags);
625}
626
Romain Guy4aa90572010-09-26 18:40:37 -0700627void DisplayListRenderer::translate(float dx, float dy) {
Romain Guyb051e892010-09-28 19:09:36 -0700628 addOp(DisplayList::Translate);
Romain Guy4aa90572010-09-26 18:40:37 -0700629 addPoint(dx, dy);
630 OpenGLRenderer::translate(dx, dy);
631}
632
633void DisplayListRenderer::rotate(float degrees) {
Romain Guyb051e892010-09-28 19:09:36 -0700634 addOp(DisplayList::Rotate);
Romain Guy4aa90572010-09-26 18:40:37 -0700635 addFloat(degrees);
636 OpenGLRenderer::rotate(degrees);
637}
638
639void DisplayListRenderer::scale(float sx, float sy) {
Romain Guyb051e892010-09-28 19:09:36 -0700640 addOp(DisplayList::Scale);
Romain Guy4aa90572010-09-26 18:40:37 -0700641 addPoint(sx, sy);
642 OpenGLRenderer::scale(sx, sy);
643}
644
Romain Guy807daf72011-01-18 11:19:19 -0800645void DisplayListRenderer::skew(float sx, float sy) {
646 addOp(DisplayList::Skew);
647 addPoint(sx, sy);
648 OpenGLRenderer::skew(sx, sy);
649}
650
Romain Guy4aa90572010-09-26 18:40:37 -0700651void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
Romain Guyb051e892010-09-28 19:09:36 -0700652 addOp(DisplayList::SetMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700653 addMatrix(matrix);
654 OpenGLRenderer::setMatrix(matrix);
655}
656
657void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guyb051e892010-09-28 19:09:36 -0700658 addOp(DisplayList::ConcatMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700659 addMatrix(matrix);
660 OpenGLRenderer::concatMatrix(matrix);
661}
662
663bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
664 SkRegion::Op op) {
Romain Guyb051e892010-09-28 19:09:36 -0700665 addOp(DisplayList::ClipRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700666 addBounds(left, top, right, bottom);
667 addInt(op);
668 return OpenGLRenderer::clipRect(left, top, right, bottom, op);
669}
670
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700671bool DisplayListRenderer::drawDisplayList(DisplayList* displayList,
672 uint32_t width, uint32_t height, Rect& dirty, uint32_t level) {
Romain Guycabfcc12011-03-07 18:06:46 -0800673 // dirty is an out parameter and should not be recorded,
674 // it matters only when replaying the display list
Romain Guy0fe478e2010-11-08 12:08:41 -0800675 addOp(DisplayList::DrawDisplayList);
676 addDisplayList(displayList);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700677 addSize(width, height);
Chet Haasedaf98e92011-01-10 14:10:36 -0800678 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -0800679}
680
Romain Guyada830f2011-01-13 12:13:20 -0800681void DisplayListRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy6c319ca2011-01-11 14:29:25 -0800682 addOp(DisplayList::DrawLayer);
Romain Guyada830f2011-01-13 12:13:20 -0800683 addInt((int) layer);
684 addPoint(x, y);
Romain Guy6c319ca2011-01-11 14:29:25 -0800685 addPaint(paint);
686}
687
Romain Guy4aa90572010-09-26 18:40:37 -0700688void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top,
Chet Haase5c13d892010-10-08 08:37:55 -0700689 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700690 addOp(DisplayList::DrawBitmap);
Romain Guy4aa90572010-09-26 18:40:37 -0700691 addBitmap(bitmap);
692 addPoint(left, top);
693 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700694}
695
Chet Haase5c13d892010-10-08 08:37:55 -0700696void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix,
697 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700698 addOp(DisplayList::DrawBitmapMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700699 addBitmap(bitmap);
700 addMatrix(matrix);
701 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700702}
703
704void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
705 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chet Haase5c13d892010-10-08 08:37:55 -0700706 float dstRight, float dstBottom, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700707 addOp(DisplayList::DrawBitmapRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700708 addBitmap(bitmap);
709 addBounds(srcLeft, srcTop, srcRight, srcBottom);
710 addBounds(dstLeft, dstTop, dstRight, dstBottom);
711 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700712}
713
Romain Guy5a7b4662011-01-20 19:09:30 -0800714void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
715 float* vertices, int* colors, SkPaint* paint) {
716 addOp(DisplayList::DrawBitmapMesh);
717 addBitmap(bitmap);
718 addInt(meshWidth);
719 addInt(meshHeight);
720 addFloats(vertices, (meshWidth + 1) * (meshHeight + 1) * 2);
721 if (colors) {
722 addInt(1);
723 addInts(colors, (meshWidth + 1) * (meshHeight + 1));
724 } else {
725 addInt(0);
726 }
727 addPaint(paint);
728}
729
Romain Guy4aa90572010-09-26 18:40:37 -0700730void DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700731 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700732 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700733 addOp(DisplayList::DrawPatch);
Romain Guy4aa90572010-09-26 18:40:37 -0700734 addBitmap(bitmap);
735 addInts(xDivs, width);
736 addInts(yDivs, height);
Romain Guy4bb94202010-10-12 15:59:26 -0700737 addUInts(colors, numColors);
Romain Guy4aa90572010-09-26 18:40:37 -0700738 addBounds(left, top, right, bottom);
739 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700740}
741
742void DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyb051e892010-09-28 19:09:36 -0700743 addOp(DisplayList::DrawColor);
Romain Guy4aa90572010-09-26 18:40:37 -0700744 addInt(color);
745 addInt(mode);
Romain Guy4aa90572010-09-26 18:40:37 -0700746}
747
748void DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700749 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700750 addOp(DisplayList::DrawRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700751 addBounds(left, top, right, bottom);
752 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700753}
754
Romain Guy01d58e42011-01-19 21:54:02 -0800755void DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
756 float rx, float ry, SkPaint* paint) {
757 addOp(DisplayList::DrawRoundRect);
758 addBounds(left, top, right, bottom);
759 addPoint(rx, ry);
760 addPaint(paint);
761}
762
763void DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
764 addOp(DisplayList::DrawCircle);
765 addPoint(x, y);
766 addFloat(radius);
767 addPaint(paint);
768}
769
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800770void DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
771 SkPaint* paint) {
772 addOp(DisplayList::DrawOval);
773 addBounds(left, top, right, bottom);
774 addPaint(paint);
775}
776
Romain Guy8b2f5262011-01-23 16:15:02 -0800777void DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
778 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Romain Guy82d41a52011-01-24 21:53:42 -0800779 addOp(DisplayList::DrawArc);
Romain Guy8b2f5262011-01-23 16:15:02 -0800780 addBounds(left, top, right, bottom);
781 addPoint(startAngle, sweepAngle);
782 addInt(useCenter ? 1 : 0);
783 addPaint(paint);
784}
785
Romain Guy4aa90572010-09-26 18:40:37 -0700786void DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700787 addOp(DisplayList::DrawPath);
Romain Guy4aa90572010-09-26 18:40:37 -0700788 addPath(path);
789 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700790}
791
Chet Haase5c13d892010-10-08 08:37:55 -0700792void DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700793 addOp(DisplayList::DrawLines);
Romain Guy4aa90572010-09-26 18:40:37 -0700794 addFloats(points, count);
795 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700796}
797
Romain Guyed6fcb02011-03-21 13:11:28 -0700798void DisplayListRenderer::drawPoints(float* points, int count, SkPaint* paint) {
799 addOp(DisplayList::DrawPoints);
800 addFloats(points, count);
801 addPaint(paint);
802}
803
Romain Guy4aa90572010-09-26 18:40:37 -0700804void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
805 float x, float y, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700806 addOp(DisplayList::DrawText);
Romain Guy4aa90572010-09-26 18:40:37 -0700807 addText(text, bytesCount);
808 addInt(count);
809 addPoint(x, y);
810 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700811}
812
813void DisplayListRenderer::resetShader() {
Romain Guyb051e892010-09-28 19:09:36 -0700814 addOp(DisplayList::ResetShader);
Romain Guy4aa90572010-09-26 18:40:37 -0700815}
816
817void DisplayListRenderer::setupShader(SkiaShader* shader) {
Chet Haase5c13d892010-10-08 08:37:55 -0700818 addOp(DisplayList::SetupShader);
819 addShader(shader);
Romain Guy4aa90572010-09-26 18:40:37 -0700820}
821
822void DisplayListRenderer::resetColorFilter() {
Romain Guyb051e892010-09-28 19:09:36 -0700823 addOp(DisplayList::ResetColorFilter);
Romain Guy4aa90572010-09-26 18:40:37 -0700824}
825
826void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chet Haasead93c2b2010-10-22 16:17:12 -0700827 addOp(DisplayList::SetupColorFilter);
828 addColorFilter(filter);
Romain Guy4aa90572010-09-26 18:40:37 -0700829}
830
831void DisplayListRenderer::resetShadow() {
Romain Guyb051e892010-09-28 19:09:36 -0700832 addOp(DisplayList::ResetShadow);
Romain Guy4aa90572010-09-26 18:40:37 -0700833}
834
835void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
Romain Guyb051e892010-09-28 19:09:36 -0700836 addOp(DisplayList::SetupShadow);
Romain Guy4aa90572010-09-26 18:40:37 -0700837 addFloat(radius);
838 addPoint(dx, dy);
839 addInt(color);
Romain Guy4aa90572010-09-26 18:40:37 -0700840}
841
Romain Guy4aa90572010-09-26 18:40:37 -0700842}; // namespace uirenderer
843}; // namespace android