blob: d5d2ba07154c08875f76cf183c86428f20c046b8 [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",
56 "DrawText",
57 "ResetShader",
58 "SetupShader",
59 "ResetColorFilter",
60 "SetupColorFilter",
61 "ResetShadow",
Chet Haasedaf98e92011-01-10 14:10:36 -080062 "SetupShadow",
63 "DrawGLFunction"
Romain Guyffac7fc2011-01-13 17:21:49 -080064};
65
Romain Guyb051e892010-09-28 19:09:36 -070066DisplayList::DisplayList(const DisplayListRenderer& recorder) {
Chet Haase5977baa2011-01-05 18:01:22 -080067 initFromDisplayListRenderer(recorder);
68}
69
70DisplayList::~DisplayList() {
Chet Haased63cbd12011-02-03 16:32:46 -080071 clearResources();
72}
73
74void DisplayList::clearResources() {
Chet Haase5977baa2011-01-05 18:01:22 -080075 sk_free((void*) mReader.base());
76
77 Caches& caches = Caches::getInstance();
78
79 for (size_t i = 0; i < mBitmapResources.size(); i++) {
80 caches.resourceCache.decrementRefcount(mBitmapResources.itemAt(i));
81 }
82 mBitmapResources.clear();
83
Romain Guy24c00212011-01-14 15:31:00 -080084 for (size_t i = 0; i < mShaders.size(); i++) {
Romain Guy43ccf462011-01-14 18:51:01 -080085 caches.resourceCache.decrementRefcount(mShaders.itemAt(i));
Chet Haase5977baa2011-01-05 18:01:22 -080086 }
Romain Guy24c00212011-01-14 15:31:00 -080087 mShaders.clear();
Chet Haase5977baa2011-01-05 18:01:22 -080088
89 for (size_t i = 0; i < mPaints.size(); i++) {
90 delete mPaints.itemAt(i);
91 }
92 mPaints.clear();
93
Romain Guy2fc941e2011-02-03 15:06:05 -080094 for (size_t i = 0; i < mPaths.size(); i++) {
95 delete mPaths.itemAt(i);
96 }
97 mPaths.clear();
Chet Haase5a7e8282011-02-04 12:50:55 -080098 for (size_t i = 0; i < mOriginalPaths.size(); i++) {
99 caches.resourceCache.decrementRefcount(mOriginalPaths.itemAt(i));
100 }
101 mOriginalPaths.clear();
Romain Guy2fc941e2011-02-03 15:06:05 -0800102
Chet Haase5977baa2011-01-05 18:01:22 -0800103 for (size_t i = 0; i < mMatrices.size(); i++) {
104 delete mMatrices.itemAt(i);
105 }
106 mMatrices.clear();
Chet Haase5977baa2011-01-05 18:01:22 -0800107}
108
Chet Haased63cbd12011-02-03 16:32:46 -0800109void DisplayList::initFromDisplayListRenderer(const DisplayListRenderer& recorder, bool reusing) {
Romain Guyb051e892010-09-28 19:09:36 -0700110 const SkWriter32& writer = recorder.writeStream();
111 init();
112
113 if (writer.size() == 0) {
114 return;
115 }
116
Chet Haased63cbd12011-02-03 16:32:46 -0800117 if (reusing) {
118 // re-using display list - clear out previous allocations
119 clearResources();
120 }
121
Romain Guyb051e892010-09-28 19:09:36 -0700122 size_t size = writer.size();
123 void* buffer = sk_malloc_throw(size);
124 writer.flatten(buffer);
125 mReader.setMemory(buffer, size);
126
Chet Haase5c13d892010-10-08 08:37:55 -0700127 Caches& caches = Caches::getInstance();
Romain Guyb051e892010-09-28 19:09:36 -0700128
Chet Haase5c13d892010-10-08 08:37:55 -0700129 const Vector<SkBitmap*> &bitmapResources = recorder.getBitmapResources();
130 for (size_t i = 0; i < bitmapResources.size(); i++) {
131 SkBitmap* resource = bitmapResources.itemAt(i);
132 mBitmapResources.add(resource);
133 caches.resourceCache.incrementRefcount(resource);
Romain Guyb051e892010-09-28 19:09:36 -0700134 }
Chet Haased98aa2d2010-10-25 15:47:32 -0700135
Romain Guy24c00212011-01-14 15:31:00 -0800136 const Vector<SkiaShader*> &shaders = recorder.getShaders();
137 for (size_t i = 0; i < shaders.size(); i++) {
Romain Guy43ccf462011-01-14 18:51:01 -0800138 SkiaShader* shader = shaders.itemAt(i);
139 mShaders.add(shader);
140 caches.resourceCache.incrementRefcount(shader);
Romain Guyb051e892010-09-28 19:09:36 -0700141 }
142
Chet Haased98aa2d2010-10-25 15:47:32 -0700143 const Vector<SkPaint*> &paints = recorder.getPaints();
144 for (size_t i = 0; i < paints.size(); i++) {
145 mPaints.add(paints.itemAt(i));
146 }
147
Romain Guy2fc941e2011-02-03 15:06:05 -0800148 const Vector<SkPath*> &paths = recorder.getPaths();
149 for (size_t i = 0; i < paths.size(); i++) {
150 mPaths.add(paths.itemAt(i));
151 }
152
Chet Haase5a7e8282011-02-04 12:50:55 -0800153 const Vector<SkPath*> &originalPaths = recorder.getOriginalPaths();
154 for (size_t i = 0; i < originalPaths.size(); i++) {
155 SkPath* path = originalPaths.itemAt(i);
156 mOriginalPaths.add(path);
157 caches.resourceCache.incrementRefcount(path);
158 }
159
Chet Haased98aa2d2010-10-25 15:47:32 -0700160 const Vector<SkMatrix*> &matrices = recorder.getMatrices();
161 for (size_t i = 0; i < matrices.size(); i++) {
162 mMatrices.add(matrices.itemAt(i));
163 }
Romain Guyb051e892010-09-28 19:09:36 -0700164}
165
Romain Guyb051e892010-09-28 19:09:36 -0700166void DisplayList::init() {
Romain Guyb051e892010-09-28 19:09:36 -0700167}
168
Chet Haasedaf98e92011-01-10 14:10:36 -0800169bool DisplayList::replay(OpenGLRenderer& renderer, uint32_t level) {
170 bool needsInvalidate = false;
Romain Guyb051e892010-09-28 19:09:36 -0700171 TextContainer text;
172 mReader.rewind();
173
Romain Guyffac7fc2011-01-13 17:21:49 -0800174#if DEBUG_DISPLAY_LIST
175 uint32_t count = (level + 1) * 2;
176 char indent[count + 1];
177 for (uint32_t i = 0; i < count; i++) {
178 indent[i] = ' ';
179 }
180 indent[count] = '\0';
181 DISPLAY_LIST_LOGD("%sStart display list (%p)", (char*) indent + 2, this);
182#endif
Romain Guyb051e892010-09-28 19:09:36 -0700183
Romain Guyffac7fc2011-01-13 17:21:49 -0800184 int saveCount = renderer.getSaveCount() - 1;
Romain Guyb051e892010-09-28 19:09:36 -0700185 while (!mReader.eof()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700186 int op = mReader.readInt();
Romain Guyffac7fc2011-01-13 17:21:49 -0800187
Romain Guy5b3b3522010-10-27 18:57:51 -0700188 switch (op) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800189 case DrawGLFunction: {
190 Functor *functor = (Functor *) getInt();
191 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], functor);
192 needsInvalidate |= renderer.callDrawGLFunction(functor);
193 }
194 break;
Romain Guyb051e892010-09-28 19:09:36 -0700195 case Save: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800196 int rendererNum = getInt();
197 DISPLAY_LIST_LOGD("%s%s %d", (char*) indent, OP_NAMES[op], rendererNum);
198 renderer.save(rendererNum);
Romain Guyb051e892010-09-28 19:09:36 -0700199 }
200 break;
201 case Restore: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800202 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700203 renderer.restore();
204 }
205 break;
206 case RestoreToCount: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800207 int restoreCount = saveCount + getInt();
208 DISPLAY_LIST_LOGD("%s%s %d", (char*) indent, OP_NAMES[op], restoreCount);
209 renderer.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -0700210 }
211 break;
212 case SaveLayer: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800213 float f1 = getFloat();
214 float f2 = getFloat();
215 float f3 = getFloat();
216 float f4 = getFloat();
217 SkPaint* paint = getPaint();
218 int flags = getInt();
219 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p, 0x%x", (char*) indent,
220 OP_NAMES[op], f1, f2, f3, f4, paint, flags);
221 renderer.saveLayer(f1, f2, f3, f4, paint, flags);
Romain Guyb051e892010-09-28 19:09:36 -0700222 }
223 break;
Romain Guy5b3b3522010-10-27 18:57:51 -0700224 case SaveLayerAlpha: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800225 float f1 = getFloat();
226 float f2 = getFloat();
227 float f3 = getFloat();
228 float f4 = getFloat();
229 int alpha = getInt();
230 int flags = getInt();
231 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %d, 0x%x", (char*) indent,
232 OP_NAMES[op], f1, f2, f3, f4, alpha, flags);
233 renderer.saveLayerAlpha(f1, f2, f3, f4, alpha, flags);
Romain Guy5b3b3522010-10-27 18:57:51 -0700234 }
235 break;
Romain Guyb051e892010-09-28 19:09:36 -0700236 case Translate: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800237 float f1 = getFloat();
238 float f2 = getFloat();
239 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], f1, f2);
240 renderer.translate(f1, f2);
Romain Guyb051e892010-09-28 19:09:36 -0700241 }
242 break;
243 case Rotate: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800244 float rotation = getFloat();
245 DISPLAY_LIST_LOGD("%s%s %.2f", (char*) indent, OP_NAMES[op], rotation);
246 renderer.rotate(rotation);
Romain Guyb051e892010-09-28 19:09:36 -0700247 }
248 break;
249 case Scale: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800250 float sx = getFloat();
251 float sy = getFloat();
252 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], sx, sy);
253 renderer.scale(sx, sy);
Romain Guyb051e892010-09-28 19:09:36 -0700254 }
255 break;
Romain Guy807daf72011-01-18 11:19:19 -0800256 case Skew: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800257 float sx = getFloat();
258 float sy = getFloat();
259 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f", (char*) indent, OP_NAMES[op], sx, sy);
260 renderer.skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -0800261 }
262 break;
Romain Guyb051e892010-09-28 19:09:36 -0700263 case SetMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800264 SkMatrix* matrix = getMatrix();
265 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], matrix);
266 renderer.setMatrix(matrix);
Romain Guyb051e892010-09-28 19:09:36 -0700267 }
268 break;
269 case ConcatMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800270 SkMatrix* matrix = getMatrix();
271 DISPLAY_LIST_LOGD("%s%s %p", (char*) indent, OP_NAMES[op], matrix);
272 renderer.concatMatrix(matrix);
Romain Guyb051e892010-09-28 19:09:36 -0700273 }
274 break;
275 case ClipRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800276 float f1 = getFloat();
277 float f2 = getFloat();
278 float f3 = getFloat();
279 float f4 = getFloat();
280 int regionOp = getInt();
281 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %d", (char*) indent, OP_NAMES[op],
282 f1, f2, f3, f4, regionOp);
283 renderer.clipRect(f1, f2, f3, f4, (SkRegion::Op) regionOp);
Romain Guyb051e892010-09-28 19:09:36 -0700284 }
285 break;
Romain Guy0fe478e2010-11-08 12:08:41 -0800286 case DrawDisplayList: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800287 DisplayList* displayList = getDisplayList();
288 DISPLAY_LIST_LOGD("%s%s %p, %d", (char*) indent, OP_NAMES[op],
289 displayList, level + 1);
290 needsInvalidate |= renderer.drawDisplayList(displayList, level + 1);
Romain Guy0fe478e2010-11-08 12:08:41 -0800291 }
292 break;
Romain Guy6c319ca2011-01-11 14:29:25 -0800293 case DrawLayer: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800294 Layer* layer = (Layer*) getInt();
295 float x = getFloat();
296 float y = getFloat();
297 SkPaint* paint = getPaint();
298 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
299 layer, x, y, paint);
300 renderer.drawLayer(layer, x, y, paint);
Romain Guy6c319ca2011-01-11 14:29:25 -0800301 }
302 break;
Romain Guyb051e892010-09-28 19:09:36 -0700303 case DrawBitmap: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800304 SkBitmap* bitmap = getBitmap();
305 float x = getFloat();
306 float y = getFloat();
307 SkPaint* paint = getPaint();
308 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
309 bitmap, x, y, paint);
310 renderer.drawBitmap(bitmap, x, y, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700311 }
312 break;
313 case DrawBitmapMatrix: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800314 SkBitmap* bitmap = getBitmap();
315 SkMatrix* matrix = getMatrix();
316 SkPaint* paint = getPaint();
317 DISPLAY_LIST_LOGD("%s%s %p, %p, %p", (char*) indent, OP_NAMES[op],
318 bitmap, matrix, paint);
319 renderer.drawBitmap(bitmap, matrix, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700320 }
321 break;
322 case DrawBitmapRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800323 SkBitmap* bitmap = getBitmap();
324 float f1 = getFloat();
325 float f2 = getFloat();
326 float f3 = getFloat();
327 float f4 = getFloat();
328 float f5 = getFloat();
329 float f6 = getFloat();
330 float f7 = getFloat();
331 float f8 = getFloat();
332 SkPaint* paint = getPaint();
333 DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p",
334 (char*) indent, OP_NAMES[op], bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
335 renderer.drawBitmap(bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700336 }
337 break;
Romain Guy5a7b4662011-01-20 19:09:30 -0800338 case DrawBitmapMesh: {
339 int verticesCount = 0;
340 uint32_t colorsCount = 0;
341
342 SkBitmap* bitmap = getBitmap();
343 uint32_t meshWidth = getInt();
344 uint32_t meshHeight = getInt();
345 float* vertices = getFloats(verticesCount);
346 bool hasColors = getInt();
347 int* colors = hasColors ? getInts(colorsCount) : NULL;
348
Chet Haasedaf98e92011-01-10 14:10:36 -0800349 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guy5a7b4662011-01-20 19:09:30 -0800350 renderer.drawBitmapMesh(bitmap, meshWidth, meshHeight, vertices, colors, getPaint());
351 }
Romain Guya566b7c2011-01-23 16:36:11 -0800352 break;
Romain Guyb051e892010-09-28 19:09:36 -0700353 case DrawPatch: {
354 int32_t* xDivs = NULL;
355 int32_t* yDivs = NULL;
Romain Guy4bb94202010-10-12 15:59:26 -0700356 uint32_t* colors = NULL;
Romain Guyb051e892010-09-28 19:09:36 -0700357 uint32_t xDivsCount = 0;
358 uint32_t yDivsCount = 0;
Romain Guy4bb94202010-10-12 15:59:26 -0700359 int8_t numColors = 0;
Romain Guyb051e892010-09-28 19:09:36 -0700360
361 SkBitmap* bitmap = getBitmap();
362
363 xDivs = getInts(xDivsCount);
364 yDivs = getInts(yDivsCount);
Romain Guy4bb94202010-10-12 15:59:26 -0700365 colors = getUInts(numColors);
Romain Guyb051e892010-09-28 19:09:36 -0700366
Chet Haasedaf98e92011-01-10 14:10:36 -0800367 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guy4bb94202010-10-12 15:59:26 -0700368 renderer.drawPatch(bitmap, xDivs, yDivs, colors, xDivsCount, yDivsCount,
369 numColors, getFloat(), getFloat(), getFloat(), getFloat(), getPaint());
Romain Guyb051e892010-09-28 19:09:36 -0700370 }
371 break;
372 case DrawColor: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800373 int color = getInt();
374 int xferMode = getInt();
375 DISPLAY_LIST_LOGD("%s%s 0x%x %d", (char*) indent, OP_NAMES[op], color, xferMode);
376 renderer.drawColor(color, (SkXfermode::Mode) xferMode);
Romain Guyb051e892010-09-28 19:09:36 -0700377 }
378 break;
379 case DrawRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800380 float f1 = getFloat();
381 float f2 = getFloat();
382 float f3 = getFloat();
383 float f4 = getFloat();
384 SkPaint* paint = getPaint();
385 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
386 f1, f2, f3, f4, paint);
387 renderer.drawRect(f1, f2, f3, f4, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700388 }
389 break;
Romain Guy01d58e42011-01-19 21:54:02 -0800390 case DrawRoundRect: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800391 float f1 = getFloat();
392 float f2 = getFloat();
393 float f3 = getFloat();
394 float f4 = getFloat();
395 float f5 = getFloat();
396 float f6 = getFloat();
397 SkPaint* paint = getPaint();
398 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %p",
399 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, f5, f6, paint);
400 renderer.drawRoundRect(f1, f2, f3, f4, f5, f6, paint);
Romain Guy01d58e42011-01-19 21:54:02 -0800401 }
402 break;
403 case DrawCircle: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800404 float f1 = getFloat();
405 float f2 = getFloat();
406 float f3 = getFloat();
407 SkPaint* paint = getPaint();
408 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %p",
409 (char*) indent, OP_NAMES[op], f1, f2, f3, paint);
410 renderer.drawCircle(f1, f2, f3, paint);
Romain Guy01d58e42011-01-19 21:54:02 -0800411 }
412 break;
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800413 case DrawOval: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800414 float f1 = getFloat();
415 float f2 = getFloat();
416 float f3 = getFloat();
417 float f4 = getFloat();
418 SkPaint* paint = getPaint();
419 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %p",
420 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, paint);
421 renderer.drawOval(f1, f2, f3, f4, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800422 }
423 break;
Romain Guy8b2f5262011-01-23 16:15:02 -0800424 case DrawArc: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800425 float f1 = getFloat();
426 float f2 = getFloat();
427 float f3 = getFloat();
428 float f4 = getFloat();
429 float f5 = getFloat();
430 float f6 = getFloat();
431 int i1 = getInt();
432 SkPaint* paint = getPaint();
433 DISPLAY_LIST_LOGD("%s%s %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p",
434 (char*) indent, OP_NAMES[op], f1, f2, f3, f4, f5, f6, i1, paint);
435 renderer.drawArc(f1, f2, f3, f4, f5, f6, i1 == 1, paint);
Romain Guy8b2f5262011-01-23 16:15:02 -0800436 }
437 break;
Romain Guyb051e892010-09-28 19:09:36 -0700438 case DrawPath: {
Chet Haasedaf98e92011-01-10 14:10:36 -0800439 SkPath* path = getPath();
440 SkPaint* paint = getPaint();
441 DISPLAY_LIST_LOGD("%s%s %p, %p", (char*) indent, OP_NAMES[op], path, paint);
442 renderer.drawPath(path, paint);
Romain Guyb051e892010-09-28 19:09:36 -0700443 }
444 break;
445 case DrawLines: {
446 int count = 0;
447 float* points = getFloats(count);
Chet Haasedaf98e92011-01-10 14:10:36 -0800448 DISPLAY_LIST_LOGD("%s%s", (char*) indent, OP_NAMES[op]);
Romain Guyb051e892010-09-28 19:09:36 -0700449 renderer.drawLines(points, count, getPaint());
450 }
451 break;
452 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
Chet Haase5a7e8282011-02-04 12:50:55 -0800533 for (size_t i = 0; i < mOriginalPaths.size(); i++) {
534 SkPath* resource = mOriginalPaths.itemAt(i);
535 caches.resourceCache.decrementRefcount(resource);
536 }
537 mOriginalPaths.clear();
538
Romain Guy43ccf462011-01-14 18:51:01 -0800539 for (size_t i = 0; i < mShaders.size(); i++) {
540 caches.resourceCache.decrementRefcount(mShaders.itemAt(i));
541 }
Romain Guy24c00212011-01-14 15:31:00 -0800542 mShaders.clear();
543 mShaderMap.clear();
Romain Guy43ccf462011-01-14 18:51:01 -0800544
545 mPaints.clear();
546 mPaintMap.clear();
Romain Guy2fc941e2011-02-03 15:06:05 -0800547 mPaths.clear();
548 mPathMap.clear();
Chet Haased98aa2d2010-10-25 15:47:32 -0700549 mMatrices.clear();
Romain Guy4aa90572010-09-26 18:40:37 -0700550}
551
552///////////////////////////////////////////////////////////////////////////////
553// Operations
554///////////////////////////////////////////////////////////////////////////////
555
Chet Haase5977baa2011-01-05 18:01:22 -0800556DisplayList* DisplayListRenderer::getDisplayList() {
557 if (mDisplayList == NULL) {
558 mDisplayList = new DisplayList(*this);
559 } else {
Chet Haased63cbd12011-02-03 16:32:46 -0800560 mDisplayList->initFromDisplayListRenderer(*this, true);
Chet Haase5977baa2011-01-05 18:01:22 -0800561 }
562 return mDisplayList;
563}
564
Romain Guyb051e892010-09-28 19:09:36 -0700565void DisplayListRenderer::setViewport(int width, int height) {
566 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
567
568 mWidth = width;
569 mHeight = height;
570}
571
Romain Guy7d7b5492011-01-24 16:33:45 -0800572void DisplayListRenderer::prepareDirty(float left, float top,
573 float right, float bottom, bool opaque) {
Romain Guyb051e892010-09-28 19:09:36 -0700574 mSnapshot = new Snapshot(mFirstSnapshot,
575 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
576 mSaveCount = 1;
577 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guy27454a42011-01-23 12:01:41 -0800578 mRestoreSaveCount = -1;
579}
580
581void DisplayListRenderer::finish() {
582 insertRestoreToCount();
583 OpenGLRenderer::finish();
Romain Guyb051e892010-09-28 19:09:36 -0700584}
585
Chet Haasedaf98e92011-01-10 14:10:36 -0800586void DisplayListRenderer::interrupt() {
Chet Haasedaf98e92011-01-10 14:10:36 -0800587}
Romain Guy2b1847e2011-01-26 13:43:01 -0800588
Chet Haasedaf98e92011-01-10 14:10:36 -0800589void DisplayListRenderer::resume() {
Romain Guy4aa90572010-09-26 18:40:37 -0700590}
591
Chet Haasedaf98e92011-01-10 14:10:36 -0800592bool DisplayListRenderer::callDrawGLFunction(Functor *functor) {
593 addOp(DisplayList::DrawGLFunction);
594 addInt((int) functor);
595 return false; // No invalidate needed at record-time
596}
597
Romain Guy4aa90572010-09-26 18:40:37 -0700598int DisplayListRenderer::save(int flags) {
Romain Guyb051e892010-09-28 19:09:36 -0700599 addOp(DisplayList::Save);
Romain Guy4aa90572010-09-26 18:40:37 -0700600 addInt(flags);
601 return OpenGLRenderer::save(flags);
602}
603
604void DisplayListRenderer::restore() {
Romain Guyb051e892010-09-28 19:09:36 -0700605 addOp(DisplayList::Restore);
Romain Guy4aa90572010-09-26 18:40:37 -0700606 OpenGLRenderer::restore();
607}
608
609void DisplayListRenderer::restoreToCount(int saveCount) {
Romain Guy27454a42011-01-23 12:01:41 -0800610 mRestoreSaveCount = saveCount;
Romain Guy4aa90572010-09-26 18:40:37 -0700611 OpenGLRenderer::restoreToCount(saveCount);
612}
613
614int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700615 SkPaint* p, int flags) {
Romain Guyb051e892010-09-28 19:09:36 -0700616 addOp(DisplayList::SaveLayer);
Romain Guy4aa90572010-09-26 18:40:37 -0700617 addBounds(left, top, right, bottom);
618 addPaint(p);
619 addInt(flags);
Romain Guyb051e892010-09-28 19:09:36 -0700620 return OpenGLRenderer::save(flags);
Romain Guy4aa90572010-09-26 18:40:37 -0700621}
622
Romain Guy5b3b3522010-10-27 18:57:51 -0700623int DisplayListRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
624 int alpha, int flags) {
625 addOp(DisplayList::SaveLayerAlpha);
626 addBounds(left, top, right, bottom);
627 addInt(alpha);
628 addInt(flags);
629 return OpenGLRenderer::save(flags);
630}
631
Romain Guy4aa90572010-09-26 18:40:37 -0700632void DisplayListRenderer::translate(float dx, float dy) {
Romain Guyb051e892010-09-28 19:09:36 -0700633 addOp(DisplayList::Translate);
Romain Guy4aa90572010-09-26 18:40:37 -0700634 addPoint(dx, dy);
635 OpenGLRenderer::translate(dx, dy);
636}
637
638void DisplayListRenderer::rotate(float degrees) {
Romain Guyb051e892010-09-28 19:09:36 -0700639 addOp(DisplayList::Rotate);
Romain Guy4aa90572010-09-26 18:40:37 -0700640 addFloat(degrees);
641 OpenGLRenderer::rotate(degrees);
642}
643
644void DisplayListRenderer::scale(float sx, float sy) {
Romain Guyb051e892010-09-28 19:09:36 -0700645 addOp(DisplayList::Scale);
Romain Guy4aa90572010-09-26 18:40:37 -0700646 addPoint(sx, sy);
647 OpenGLRenderer::scale(sx, sy);
648}
649
Romain Guy807daf72011-01-18 11:19:19 -0800650void DisplayListRenderer::skew(float sx, float sy) {
651 addOp(DisplayList::Skew);
652 addPoint(sx, sy);
653 OpenGLRenderer::skew(sx, sy);
654}
655
Romain Guy4aa90572010-09-26 18:40:37 -0700656void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
Romain Guyb051e892010-09-28 19:09:36 -0700657 addOp(DisplayList::SetMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700658 addMatrix(matrix);
659 OpenGLRenderer::setMatrix(matrix);
660}
661
662void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guyb051e892010-09-28 19:09:36 -0700663 addOp(DisplayList::ConcatMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700664 addMatrix(matrix);
665 OpenGLRenderer::concatMatrix(matrix);
666}
667
668bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
669 SkRegion::Op op) {
Romain Guyb051e892010-09-28 19:09:36 -0700670 addOp(DisplayList::ClipRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700671 addBounds(left, top, right, bottom);
672 addInt(op);
673 return OpenGLRenderer::clipRect(left, top, right, bottom, op);
674}
675
Chet Haasedaf98e92011-01-10 14:10:36 -0800676bool DisplayListRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -0800677 addOp(DisplayList::DrawDisplayList);
678 addDisplayList(displayList);
Chet Haasedaf98e92011-01-10 14:10:36 -0800679 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -0800680}
681
Romain Guyada830f2011-01-13 12:13:20 -0800682void DisplayListRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
Romain Guy6c319ca2011-01-11 14:29:25 -0800683 addOp(DisplayList::DrawLayer);
Romain Guyada830f2011-01-13 12:13:20 -0800684 addInt((int) layer);
685 addPoint(x, y);
Romain Guy6c319ca2011-01-11 14:29:25 -0800686 addPaint(paint);
687}
688
Romain Guy4aa90572010-09-26 18:40:37 -0700689void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top,
Chet Haase5c13d892010-10-08 08:37:55 -0700690 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700691 addOp(DisplayList::DrawBitmap);
Romain Guy4aa90572010-09-26 18:40:37 -0700692 addBitmap(bitmap);
693 addPoint(left, top);
694 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700695}
696
Chet Haase5c13d892010-10-08 08:37:55 -0700697void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix,
698 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700699 addOp(DisplayList::DrawBitmapMatrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700700 addBitmap(bitmap);
701 addMatrix(matrix);
702 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700703}
704
705void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
706 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chet Haase5c13d892010-10-08 08:37:55 -0700707 float dstRight, float dstBottom, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700708 addOp(DisplayList::DrawBitmapRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700709 addBitmap(bitmap);
710 addBounds(srcLeft, srcTop, srcRight, srcBottom);
711 addBounds(dstLeft, dstTop, dstRight, dstBottom);
712 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700713}
714
Romain Guy5a7b4662011-01-20 19:09:30 -0800715void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
716 float* vertices, int* colors, SkPaint* paint) {
717 addOp(DisplayList::DrawBitmapMesh);
718 addBitmap(bitmap);
719 addInt(meshWidth);
720 addInt(meshHeight);
721 addFloats(vertices, (meshWidth + 1) * (meshHeight + 1) * 2);
722 if (colors) {
723 addInt(1);
724 addInts(colors, (meshWidth + 1) * (meshHeight + 1));
725 } else {
726 addInt(0);
727 }
728 addPaint(paint);
729}
730
Romain Guy4aa90572010-09-26 18:40:37 -0700731void DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700732 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700733 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700734 addOp(DisplayList::DrawPatch);
Romain Guy4aa90572010-09-26 18:40:37 -0700735 addBitmap(bitmap);
736 addInts(xDivs, width);
737 addInts(yDivs, height);
Romain Guy4bb94202010-10-12 15:59:26 -0700738 addUInts(colors, numColors);
Romain Guy4aa90572010-09-26 18:40:37 -0700739 addBounds(left, top, right, bottom);
740 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700741}
742
743void DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyb051e892010-09-28 19:09:36 -0700744 addOp(DisplayList::DrawColor);
Romain Guy4aa90572010-09-26 18:40:37 -0700745 addInt(color);
746 addInt(mode);
Romain Guy4aa90572010-09-26 18:40:37 -0700747}
748
749void DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700750 SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700751 addOp(DisplayList::DrawRect);
Romain Guy4aa90572010-09-26 18:40:37 -0700752 addBounds(left, top, right, bottom);
753 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700754}
755
Romain Guy01d58e42011-01-19 21:54:02 -0800756void DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
757 float rx, float ry, SkPaint* paint) {
758 addOp(DisplayList::DrawRoundRect);
759 addBounds(left, top, right, bottom);
760 addPoint(rx, ry);
761 addPaint(paint);
762}
763
764void DisplayListRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
765 addOp(DisplayList::DrawCircle);
766 addPoint(x, y);
767 addFloat(radius);
768 addPaint(paint);
769}
770
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800771void DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
772 SkPaint* paint) {
773 addOp(DisplayList::DrawOval);
774 addBounds(left, top, right, bottom);
775 addPaint(paint);
776}
777
Romain Guy8b2f5262011-01-23 16:15:02 -0800778void DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
779 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
Romain Guy82d41a52011-01-24 21:53:42 -0800780 addOp(DisplayList::DrawArc);
Romain Guy8b2f5262011-01-23 16:15:02 -0800781 addBounds(left, top, right, bottom);
782 addPoint(startAngle, sweepAngle);
783 addInt(useCenter ? 1 : 0);
784 addPaint(paint);
785}
786
Romain Guy4aa90572010-09-26 18:40:37 -0700787void DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700788 addOp(DisplayList::DrawPath);
Romain Guy4aa90572010-09-26 18:40:37 -0700789 addPath(path);
790 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700791}
792
Chet Haase5c13d892010-10-08 08:37:55 -0700793void DisplayListRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700794 addOp(DisplayList::DrawLines);
Romain Guy4aa90572010-09-26 18:40:37 -0700795 addFloats(points, count);
796 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700797}
798
799void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
800 float x, float y, SkPaint* paint) {
Romain Guyb051e892010-09-28 19:09:36 -0700801 addOp(DisplayList::DrawText);
Romain Guy4aa90572010-09-26 18:40:37 -0700802 addText(text, bytesCount);
803 addInt(count);
804 addPoint(x, y);
805 addPaint(paint);
Romain Guy4aa90572010-09-26 18:40:37 -0700806}
807
808void DisplayListRenderer::resetShader() {
Romain Guyb051e892010-09-28 19:09:36 -0700809 addOp(DisplayList::ResetShader);
Romain Guy4aa90572010-09-26 18:40:37 -0700810}
811
812void DisplayListRenderer::setupShader(SkiaShader* shader) {
Chet Haase5c13d892010-10-08 08:37:55 -0700813 addOp(DisplayList::SetupShader);
814 addShader(shader);
Romain Guy4aa90572010-09-26 18:40:37 -0700815}
816
817void DisplayListRenderer::resetColorFilter() {
Romain Guyb051e892010-09-28 19:09:36 -0700818 addOp(DisplayList::ResetColorFilter);
Romain Guy4aa90572010-09-26 18:40:37 -0700819}
820
821void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chet Haasead93c2b2010-10-22 16:17:12 -0700822 addOp(DisplayList::SetupColorFilter);
823 addColorFilter(filter);
Romain Guy4aa90572010-09-26 18:40:37 -0700824}
825
826void DisplayListRenderer::resetShadow() {
Romain Guyb051e892010-09-28 19:09:36 -0700827 addOp(DisplayList::ResetShadow);
Romain Guy4aa90572010-09-26 18:40:37 -0700828}
829
830void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
Romain Guyb051e892010-09-28 19:09:36 -0700831 addOp(DisplayList::SetupShadow);
Romain Guy4aa90572010-09-26 18:40:37 -0700832 addFloat(radius);
833 addPoint(dx, dy);
834 addInt(color);
Romain Guy4aa90572010-09-26 18:40:37 -0700835}
836
Romain Guy4aa90572010-09-26 18:40:37 -0700837}; // namespace uirenderer
838}; // namespace android