blob: 916e9166e76c59894bafddcf6060efd228ca0aa0 [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
Romain Guyd5a85fb2012-03-13 11:18:20 -070019#include <SkCamera.h>
Romain Guyc46d07a2013-03-15 19:06:39 -070020#include <SkCanvas.h>
Chet Haase9c1e23b2011-03-24 10:51:31 -070021
Romain Guy65549432012-03-26 16:45:05 -070022#include <private/hwui/DrawGlInfo.h>
23
John Recka35778c72014-11-06 09:45:10 -080024#include "ResourceCache.h"
Chris Craikc3566d02013-02-04 16:16:33 -080025#include "DeferredDisplayList.h"
John Reck12f5e342014-11-07 07:53:43 -080026#include "DeferredLayerUpdater.h"
Chet Haase9c1e23b2011-03-24 10:51:31 -070027#include "DisplayListLogBuffer.h"
Chris Craik2af46352012-11-26 18:30:17 -080028#include "DisplayListOp.h"
Romain Guy4aa90572010-09-26 18:40:37 -070029#include "DisplayListRenderer.h"
John Reck113e0822014-03-18 09:22:59 -070030#include "RenderNode.h"
Romain Guy13631f32012-01-30 17:41:55 -080031
Romain Guy4aa90572010-09-26 18:40:37 -070032namespace android {
33namespace uirenderer {
34
Chris Craikcce47eb2014-07-16 15:12:15 -070035DisplayListRenderer::DisplayListRenderer()
Tom Hudson984162f2014-10-10 13:38:16 -040036 : mState(*this)
John Reck088c5142014-11-06 13:04:49 -080037 , mResourceCache(ResourceCache::getInstance())
Chris Craik8afd0f22014-08-21 17:41:57 -070038 , mDisplayListData(NULL)
Chris Craikcce47eb2014-07-16 15:12:15 -070039 , mTranslateX(0.0f)
40 , mTranslateY(0.0f)
Rob Tsuka5f2e072014-11-17 12:57:32 -080041 , mHasDeferredTranslate(false)
Chris Craik8afd0f22014-08-21 17:41:57 -070042 , mDeferredBarrierType(kBarrier_None)
Chris Craikcce47eb2014-07-16 15:12:15 -070043 , mHighContrastText(false)
Rob Tsukc495e612014-11-11 15:44:13 -080044 , mOverrideXfermode(-1)
Chris Craikcce47eb2014-07-16 15:12:15 -070045 , mRestoreSaveCount(-1) {
Romain Guy4aa90572010-09-26 18:40:37 -070046}
47
48DisplayListRenderer::~DisplayListRenderer() {
John Reck44fd8d22014-02-26 11:00:11 -080049 LOG_ALWAYS_FATAL_IF(mDisplayListData,
50 "Destroyed a DisplayListRenderer during a record!");
Romain Guy4aa90572010-09-26 18:40:37 -070051}
52
53///////////////////////////////////////////////////////////////////////////////
54// Operations
55///////////////////////////////////////////////////////////////////////////////
56
John Reck44fd8d22014-02-26 11:00:11 -080057DisplayListData* DisplayListRenderer::finishRecording() {
John Reck44fd8d22014-02-26 11:00:11 -080058 mPaintMap.clear();
59 mRegionMap.clear();
60 mPathMap.clear();
61 DisplayListData* data = mDisplayListData;
62 mDisplayListData = 0;
63 return data;
Chet Haase5977baa2011-01-05 18:01:22 -080064}
65
Tom Hudson107843d2014-09-08 11:26:26 -040066void DisplayListRenderer::prepareDirty(float left, float top,
Romain Guy7d7b5492011-01-24 16:33:45 -080067 float right, float bottom, bool opaque) {
John Reck44fd8d22014-02-26 11:00:11 -080068
69 LOG_ALWAYS_FATAL_IF(mDisplayListData,
70 "prepareDirty called a second time during a recording!");
71 mDisplayListData = new DisplayListData();
72
Tom Hudson984162f2014-10-10 13:38:16 -040073 mState.initializeSaveStack(0, 0, mState.getWidth(), mState.getHeight(), Vector3());
Romain Guy45e4c3d2012-09-11 17:17:07 -070074
Chris Craik8afd0f22014-08-21 17:41:57 -070075 mDeferredBarrierType = kBarrier_InOrder;
Tom Hudson984162f2014-10-10 13:38:16 -040076 mState.setDirtyClip(opaque);
Romain Guy27454a42011-01-23 12:01:41 -080077 mRestoreSaveCount = -1;
78}
79
Tom Hudson107843d2014-09-08 11:26:26 -040080bool DisplayListRenderer::finish() {
Chris Craik8afd0f22014-08-21 17:41:57 -070081 flushRestoreToCount();
82 flushTranslate();
Tom Hudson107843d2014-09-08 11:26:26 -040083 return false;
Romain Guyb051e892010-09-28 19:09:36 -070084}
85
Chet Haasedaf98e92011-01-10 14:10:36 -080086void DisplayListRenderer::interrupt() {
Chet Haasedaf98e92011-01-10 14:10:36 -080087}
Romain Guy2b1847e2011-01-26 13:43:01 -080088
Chet Haasedaf98e92011-01-10 14:10:36 -080089void DisplayListRenderer::resume() {
Romain Guy4aa90572010-09-26 18:40:37 -070090}
91
Tom Hudson107843d2014-09-08 11:26:26 -040092void DisplayListRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Romain Guycabfcc12011-03-07 18:06:46 -080093 // Ignore dirty during recording, it matters only when we replay
Chris Craik2af46352012-11-26 18:30:17 -080094 addDrawOp(new (alloc()) DrawFunctorOp(functor));
John Reck09d5cdd2014-07-24 10:36:08 -070095 mDisplayListData->functors.add(functor);
Chet Haasedaf98e92011-01-10 14:10:36 -080096}
97
Romain Guy4aa90572010-09-26 18:40:37 -070098int DisplayListRenderer::save(int flags) {
Chris Craik2af46352012-11-26 18:30:17 -080099 addStateOp(new (alloc()) SaveOp(flags));
Tom Hudson984162f2014-10-10 13:38:16 -0400100 return mState.save(flags);
Romain Guy4aa90572010-09-26 18:40:37 -0700101}
102
103void DisplayListRenderer::restore() {
Romain Guy04c9d8c2011-08-25 14:01:48 -0700104 if (mRestoreSaveCount < 0) {
Romain Guy33f6beb2012-02-16 19:24:51 -0800105 restoreToCount(getSaveCount() - 1);
106 return;
Romain Guy04c9d8c2011-08-25 14:01:48 -0700107 }
Romain Guy33f6beb2012-02-16 19:24:51 -0800108
109 mRestoreSaveCount--;
Chris Craik8afd0f22014-08-21 17:41:57 -0700110 flushTranslate();
Tom Hudson984162f2014-10-10 13:38:16 -0400111 mState.restore();
Romain Guy4aa90572010-09-26 18:40:37 -0700112}
113
114void DisplayListRenderer::restoreToCount(int saveCount) {
Romain Guy27454a42011-01-23 12:01:41 -0800115 mRestoreSaveCount = saveCount;
Chris Craik8afd0f22014-08-21 17:41:57 -0700116 flushTranslate();
Tom Hudson984162f2014-10-10 13:38:16 -0400117 mState.restoreToCount(saveCount);
Romain Guy4aa90572010-09-26 18:40:37 -0700118}
119
120int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500121 const SkPaint* paint, int flags) {
Chris Craik4ace7302014-09-14 15:49:54 -0700122 // force matrix/clip isolation for layer
123 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
124
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500125 paint = refPaint(paint);
126 addStateOp(new (alloc()) SaveLayerOp(left, top, right, bottom, paint, flags));
Tom Hudson984162f2014-10-10 13:38:16 -0400127 return mState.save(flags);
Romain Guy5b3b3522010-10-27 18:57:51 -0700128}
129
Chris Craikb4589422013-12-26 15:13:13 -0800130void DisplayListRenderer::translate(float dx, float dy, float dz) {
131 // ignore dz, not used at defer time
Chris Craik8afd0f22014-08-21 17:41:57 -0700132 mHasDeferredTranslate = true;
Romain Guy33f6beb2012-02-16 19:24:51 -0800133 mTranslateX += dx;
134 mTranslateY += dy;
Chris Craik8afd0f22014-08-21 17:41:57 -0700135 flushRestoreToCount();
Tom Hudson984162f2014-10-10 13:38:16 -0400136 mState.translate(dx, dy, dz);
Romain Guy4aa90572010-09-26 18:40:37 -0700137}
138
139void DisplayListRenderer::rotate(float degrees) {
Chris Craik2af46352012-11-26 18:30:17 -0800140 addStateOp(new (alloc()) RotateOp(degrees));
Tom Hudson984162f2014-10-10 13:38:16 -0400141 mState.rotate(degrees);
Romain Guy4aa90572010-09-26 18:40:37 -0700142}
143
144void DisplayListRenderer::scale(float sx, float sy) {
Chris Craik2af46352012-11-26 18:30:17 -0800145 addStateOp(new (alloc()) ScaleOp(sx, sy));
Tom Hudson984162f2014-10-10 13:38:16 -0400146 mState.scale(sx, sy);
Romain Guy4aa90572010-09-26 18:40:37 -0700147}
148
Romain Guy807daf72011-01-18 11:19:19 -0800149void DisplayListRenderer::skew(float sx, float sy) {
Chris Craik2af46352012-11-26 18:30:17 -0800150 addStateOp(new (alloc()) SkewOp(sx, sy));
Tom Hudson984162f2014-10-10 13:38:16 -0400151 mState.skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -0800152}
153
Derek Sollenberger13908822013-12-10 12:28:58 -0500154void DisplayListRenderer::setMatrix(const SkMatrix& matrix) {
Chris Craik2af46352012-11-26 18:30:17 -0800155 addStateOp(new (alloc()) SetMatrixOp(matrix));
Tom Hudson984162f2014-10-10 13:38:16 -0400156 mState.setMatrix(matrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700157}
158
Derek Sollenberger13908822013-12-10 12:28:58 -0500159void DisplayListRenderer::concatMatrix(const SkMatrix& matrix) {
Chris Craik2af46352012-11-26 18:30:17 -0800160 addStateOp(new (alloc()) ConcatMatrixOp(matrix));
Tom Hudson984162f2014-10-10 13:38:16 -0400161 mState.concatMatrix(matrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700162}
163
164bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
165 SkRegion::Op op) {
Chris Craik2af46352012-11-26 18:30:17 -0800166 addStateOp(new (alloc()) ClipRectOp(left, top, right, bottom, op));
Tom Hudson984162f2014-10-10 13:38:16 -0400167 return mState.clipRect(left, top, right, bottom, op);
Romain Guy4aa90572010-09-26 18:40:37 -0700168}
169
Chris Craikd218a922014-01-02 17:13:34 -0800170bool DisplayListRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
Chris Craik2af46352012-11-26 18:30:17 -0800171 path = refPath(path);
172 addStateOp(new (alloc()) ClipPathOp(path, op));
Tom Hudson984162f2014-10-10 13:38:16 -0400173 return mState.clipPath(path, op);
Romain Guy735738c2012-12-03 12:34:51 -0800174}
175
Chris Craikd218a922014-01-02 17:13:34 -0800176bool DisplayListRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
Chris Craik2af46352012-11-26 18:30:17 -0800177 region = refRegion(region);
178 addStateOp(new (alloc()) ClipRegionOp(region, op));
Tom Hudson984162f2014-10-10 13:38:16 -0400179 return mState.clipRegion(region, op);
Romain Guy735738c2012-12-03 12:34:51 -0800180}
181
Tom Hudson107843d2014-09-08 11:26:26 -0400182void DisplayListRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t flags) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700183 LOG_ALWAYS_FATAL_IF(!renderNode, "missing rendernode");
184
Romain Guycabfcc12011-03-07 18:06:46 -0800185 // dirty is an out parameter and should not be recorded,
186 // it matters only when replaying the display list
Tom Hudson984162f2014-10-10 13:38:16 -0400187 DrawRenderNodeOp* op = new (alloc()) DrawRenderNodeOp(renderNode, flags, *mState.currentTransform());
Chris Craik8afd0f22014-08-21 17:41:57 -0700188 addRenderNodeOp(op);
Romain Guy0fe478e2010-11-08 12:08:41 -0800189}
190
John Reck65f2def2014-11-07 11:29:08 -0800191void DisplayListRenderer::drawLayer(DeferredLayerUpdater* layerHandle, float x, float y) {
John Reck12f5e342014-11-07 07:53:43 -0800192 // We ref the DeferredLayerUpdater due to its thread-safe ref-counting
193 // semantics.
194 mDisplayListData->ref(layerHandle);
195 addDrawOp(new (alloc()) DrawLayerOp(layerHandle->backingLayer(), x, y));
Romain Guy6c319ca2011-01-11 14:29:25 -0800196}
197
Tom Hudson107843d2014-09-08 11:26:26 -0400198void DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800199 bitmap = refBitmap(bitmap);
200 paint = refPaint(paint);
201
Chris Craik79647502014-08-06 13:42:24 -0700202 addDrawOp(new (alloc()) DrawBitmapOp(bitmap, paint));
Romain Guy4aa90572010-09-26 18:40:37 -0700203}
204
Tom Hudson107843d2014-09-08 11:26:26 -0400205void DisplayListRenderer::drawBitmap(const SkBitmap* bitmap, float srcLeft, float srcTop,
Romain Guy4aa90572010-09-26 18:40:37 -0700206 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chris Craikd218a922014-01-02 17:13:34 -0800207 float dstRight, float dstBottom, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -0700208 if (srcLeft == 0 && srcTop == 0
209 && srcRight == bitmap->width() && srcBottom == bitmap->height()
210 && (srcBottom - srcTop == dstBottom - dstTop)
211 && (srcRight - srcLeft == dstRight - dstLeft)) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800212 // transform simple rect to rect drawing case into position bitmap ops, since they merge
Chris Craik79647502014-08-06 13:42:24 -0700213 save(SkCanvas::kMatrix_SaveFlag);
214 translate(dstLeft, dstTop);
215 drawBitmap(bitmap, paint);
216 restore();
217 } else {
218 bitmap = refBitmap(bitmap);
219 paint = refPaint(paint);
Chris Craik527a3aa2013-03-04 10:19:31 -0800220
Chris Craik79647502014-08-06 13:42:24 -0700221 addDrawOp(new (alloc()) DrawBitmapRectOp(bitmap,
222 srcLeft, srcTop, srcRight, srcBottom,
223 dstLeft, dstTop, dstRight, dstBottom, paint));
224 }
Romain Guy4aa90572010-09-26 18:40:37 -0700225}
226
Tom Hudson107843d2014-09-08 11:26:26 -0400227void DisplayListRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800228 bitmap = refBitmapData(bitmap);
229 paint = refPaint(paint);
230
Chris Craik79647502014-08-06 13:42:24 -0700231 addDrawOp(new (alloc()) DrawBitmapDataOp(bitmap, paint));
Romain Guye651cc62012-05-14 19:44:40 -0700232}
233
Tom Hudson107843d2014-09-08 11:26:26 -0400234void DisplayListRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -0800235 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craik0664fef2014-04-11 13:40:05 -0700236 int vertexCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik2af46352012-11-26 18:30:17 -0800237 bitmap = refBitmap(bitmap);
Chris Craik0664fef2014-04-11 13:40:05 -0700238 vertices = refBuffer<float>(vertices, vertexCount * 2); // 2 floats per vertex
Chris Craik2af46352012-11-26 18:30:17 -0800239 paint = refPaint(paint);
Chris Craik0664fef2014-04-11 13:40:05 -0700240 colors = refBuffer<int>(colors, vertexCount); // 1 color per vertex
Chris Craik2af46352012-11-26 18:30:17 -0800241
242 addDrawOp(new (alloc()) DrawBitmapMeshOp(bitmap, meshWidth, meshHeight,
243 vertices, colors, paint));
Romain Guy5a7b4662011-01-20 19:09:30 -0800244}
245
Tom Hudson107843d2014-09-08 11:26:26 -0400246void DisplayListRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -0800247 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800248 bitmap = refBitmap(bitmap);
Romain Guye3b0a012013-06-26 15:45:41 -0700249 patch = refPatch(patch);
Romain Guy16ea8d32013-06-21 11:35:52 -0700250 paint = refPaint(paint);
Chris Craik2af46352012-11-26 18:30:17 -0800251
Romain Guy03c00b52013-06-20 18:30:28 -0700252 addDrawOp(new (alloc()) DrawPatchOp(bitmap, patch, left, top, right, bottom, paint));
Romain Guy4aa90572010-09-26 18:40:37 -0700253}
254
Tom Hudson107843d2014-09-08 11:26:26 -0400255void DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
Chris Craik2af46352012-11-26 18:30:17 -0800256 addDrawOp(new (alloc()) DrawColorOp(color, mode));
Romain Guy4aa90572010-09-26 18:40:37 -0700257}
258
Tom Hudson107843d2014-09-08 11:26:26 -0400259void DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800260 const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800261 paint = refPaint(paint);
262 addDrawOp(new (alloc()) DrawRectOp(left, top, right, bottom, paint));
Romain Guy4aa90572010-09-26 18:40:37 -0700263}
264
Tom Hudson107843d2014-09-08 11:26:26 -0400265void DisplayListRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800266 float rx, float ry, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800267 paint = refPaint(paint);
268 addDrawOp(new (alloc()) DrawRoundRectOp(left, top, right, bottom, rx, ry, paint));
Romain Guy01d58e42011-01-19 21:54:02 -0800269}
270
Tom Hudson107843d2014-09-08 11:26:26 -0400271void DisplayListRenderer::drawRoundRect(
Jorim Jaggi072707d2014-09-15 17:20:08 +0200272 CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top,
273 CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom,
274 CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry,
275 CanvasPropertyPaint* paint) {
John Reck0e89e2b2014-10-31 14:49:06 -0700276 mDisplayListData->ref(left);
277 mDisplayListData->ref(top);
278 mDisplayListData->ref(right);
279 mDisplayListData->ref(bottom);
280 mDisplayListData->ref(rx);
281 mDisplayListData->ref(ry);
282 mDisplayListData->ref(paint);
Jorim Jaggi072707d2014-09-15 17:20:08 +0200283 addDrawOp(new (alloc()) DrawRoundRectPropsOp(&left->value, &top->value,
284 &right->value, &bottom->value, &rx->value, &ry->value, &paint->value));
Jorim Jaggi072707d2014-09-15 17:20:08 +0200285}
286
Tom Hudson107843d2014-09-08 11:26:26 -0400287void DisplayListRenderer::drawCircle(float x, float y, float radius, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800288 paint = refPaint(paint);
289 addDrawOp(new (alloc()) DrawCircleOp(x, y, radius, paint));
Romain Guy01d58e42011-01-19 21:54:02 -0800290}
291
Tom Hudson107843d2014-09-08 11:26:26 -0400292void DisplayListRenderer::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y,
John Reck52244ff2014-05-01 21:27:37 -0700293 CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) {
John Reck0e89e2b2014-10-31 14:49:06 -0700294 mDisplayListData->ref(x);
295 mDisplayListData->ref(y);
296 mDisplayListData->ref(radius);
297 mDisplayListData->ref(paint);
John Reck52244ff2014-05-01 21:27:37 -0700298 addDrawOp(new (alloc()) DrawCirclePropsOp(&x->value, &y->value,
299 &radius->value, &paint->value));
John Reck52244ff2014-05-01 21:27:37 -0700300}
301
Tom Hudson107843d2014-09-08 11:26:26 -0400302void DisplayListRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800303 const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800304 paint = refPaint(paint);
305 addDrawOp(new (alloc()) DrawOvalOp(left, top, right, bottom, paint));
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800306}
307
Tom Hudson107843d2014-09-08 11:26:26 -0400308void DisplayListRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -0800309 float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint) {
Chris Craik544e5242014-07-11 10:58:10 -0700310 if (fabs(sweepAngle) >= 360.0f) {
Tom Hudson107843d2014-09-08 11:26:26 -0400311 drawOval(left, top, right, bottom, paint);
312 } else {
313 paint = refPaint(paint);
314 addDrawOp(new (alloc()) DrawArcOp(left, top, right, bottom,
315 startAngle, sweepAngle, useCenter, paint));
Chris Craik6ac174b2014-06-17 13:47:05 -0700316 }
Romain Guy8b2f5262011-01-23 16:15:02 -0800317}
318
Tom Hudson107843d2014-09-08 11:26:26 -0400319void DisplayListRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800320 path = refPath(path);
321 paint = refPaint(paint);
Romain Guy33f6beb2012-02-16 19:24:51 -0800322
Chris Craik2af46352012-11-26 18:30:17 -0800323 addDrawOp(new (alloc()) DrawPathOp(path, paint));
Romain Guy4aa90572010-09-26 18:40:37 -0700324}
325
Tom Hudson107843d2014-09-08 11:26:26 -0400326void DisplayListRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800327 points = refBuffer<float>(points, count);
328 paint = refPaint(paint);
329
330 addDrawOp(new (alloc()) DrawLinesOp(points, count, paint));
Romain Guy4aa90572010-09-26 18:40:37 -0700331}
332
Tom Hudson107843d2014-09-08 11:26:26 -0400333void DisplayListRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Chris Craik2af46352012-11-26 18:30:17 -0800334 points = refBuffer<float>(points, count);
335 paint = refPaint(paint);
336
337 addDrawOp(new (alloc()) DrawPointsOp(points, count, paint));
Romain Guyed6fcb02011-03-21 13:11:28 -0700338}
339
Tom Hudson107843d2014-09-08 11:26:26 -0400340void DisplayListRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800341 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -0400342 if (!text || count <= 0) return;
Chris Craik2af46352012-11-26 18:30:17 -0800343
Chris Craik2af46352012-11-26 18:30:17 -0800344 text = refText(text, bytesCount);
345 path = refPath(path);
346 paint = refPaint(paint);
347
348 DrawOp* op = new (alloc()) DrawTextOnPathOp(text, bytesCount, count, path,
349 hOffset, vOffset, paint);
Romain Guy0f667532013-03-01 14:31:04 -0800350 addDrawOp(op);
Romain Guy325740f2012-02-24 16:48:34 -0800351}
352
Tom Hudson107843d2014-09-08 11:26:26 -0400353void DisplayListRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800354 const float* positions, const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -0400355 if (!text || count <= 0) return;
Chris Craik2af46352012-11-26 18:30:17 -0800356
Chris Craik2af46352012-11-26 18:30:17 -0800357 text = refText(text, bytesCount);
358 positions = refBuffer<float>(positions, count * 2);
359 paint = refPaint(paint);
360
361 DrawOp* op = new (alloc()) DrawPosTextOp(text, bytesCount, count, positions, paint);
Romain Guy0f667532013-03-01 14:31:04 -0800362 addDrawOp(op);
Romain Guyeb9a5362012-01-17 17:39:26 -0800363}
364
Chris Craikcce47eb2014-07-16 15:12:15 -0700365static void simplifyPaint(int color, SkPaint* paint) {
366 paint->setColor(color);
367 paint->setShader(NULL);
368 paint->setColorFilter(NULL);
369 paint->setLooper(NULL);
370 paint->setStrokeWidth(4 + 0.04 * paint->getTextSize());
371 paint->setStrokeJoin(SkPaint::kRound_Join);
372 paint->setLooper(NULL);
373}
374
Tom Hudson107843d2014-09-08 11:26:26 -0400375void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -0800376 float x, float y, const float* positions, const SkPaint* paint,
Chris Craik41541822013-05-03 16:35:54 -0700377 float totalAdvance, const Rect& bounds, DrawOpMode drawOpMode) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800378
Tom Hudson107843d2014-09-08 11:26:26 -0400379 if (!text || count <= 0 || paintWillNotDrawText(*paint)) return;
Raph Levien996e57c2012-07-23 15:22:52 -0700380
Chris Craik2af46352012-11-26 18:30:17 -0800381 text = refText(text, bytesCount);
382 positions = refBuffer<float>(positions, count * 2);
Raph Levien996e57c2012-07-23 15:22:52 -0700383
Chris Craikcce47eb2014-07-16 15:12:15 -0700384 if (CC_UNLIKELY(mHighContrastText)) {
385 // high contrast draw path
386 int color = paint->getColor();
387 int channelSum = SkColorGetR(color) + SkColorGetG(color) + SkColorGetB(color);
388 bool darken = channelSum < (128 * 3);
389
390 // outline
391 SkPaint* outlinePaint = copyPaint(paint);
392 simplifyPaint(darken ? SK_ColorWHITE : SK_ColorBLACK, outlinePaint);
393 outlinePaint->setStyle(SkPaint::kStrokeAndFill_Style);
394 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count,
395 x, y, positions, outlinePaint, totalAdvance, bounds)); // bounds?
396
397 // inner
398 SkPaint* innerPaint = copyPaint(paint);
399 simplifyPaint(darken ? SK_ColorBLACK : SK_ColorWHITE, innerPaint);
400 innerPaint->setStyle(SkPaint::kFill_Style);
401 addDrawOp(new (alloc()) DrawTextOp(text, bytesCount, count,
402 x, y, positions, innerPaint, totalAdvance, bounds));
403 } else {
404 // standard draw path
405 paint = refPaint(paint);
406
407 DrawOp* op = new (alloc()) DrawTextOp(text, bytesCount, count,
408 x, y, positions, paint, totalAdvance, bounds);
409 addDrawOp(op);
410 }
Raph Levien996e57c2012-07-23 15:22:52 -0700411}
412
Tom Hudson107843d2014-09-08 11:26:26 -0400413void DisplayListRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
414 if (count <= 0) return;
Romain Guy672433d2013-01-04 19:05:13 -0800415
Chris Craik2af46352012-11-26 18:30:17 -0800416 rects = refBuffer<float>(rects, count);
417 paint = refPaint(paint);
418 addDrawOp(new (alloc()) DrawRectsOp(rects, count, paint));
Romain Guy672433d2013-01-04 19:05:13 -0800419}
420
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -0400421void DisplayListRenderer::setDrawFilter(SkDrawFilter* filter) {
422 mDrawFilter.reset(filter);
Chris Craik2af46352012-11-26 18:30:17 -0800423}
424
Chris Craik8afd0f22014-08-21 17:41:57 -0700425void DisplayListRenderer::insertReorderBarrier(bool enableReorder) {
426 flushRestoreToCount();
427 flushTranslate();
428 mDeferredBarrierType = enableReorder ? kBarrier_OutOfOrder : kBarrier_InOrder;
429}
430
431void DisplayListRenderer::flushRestoreToCount() {
Chris Craik2af46352012-11-26 18:30:17 -0800432 if (mRestoreSaveCount >= 0) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700433 addOpAndUpdateChunk(new (alloc()) RestoreToCountOp(mRestoreSaveCount));
Chris Craik2af46352012-11-26 18:30:17 -0800434 mRestoreSaveCount = -1;
435 }
436}
437
Chris Craik8afd0f22014-08-21 17:41:57 -0700438void DisplayListRenderer::flushTranslate() {
439 if (mHasDeferredTranslate) {
Chris Craik2af46352012-11-26 18:30:17 -0800440 if (mTranslateX != 0.0f || mTranslateY != 0.0f) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700441 addOpAndUpdateChunk(new (alloc()) TranslateOp(mTranslateX, mTranslateY));
Chris Craik2af46352012-11-26 18:30:17 -0800442 mTranslateX = mTranslateY = 0.0f;
443 }
Chris Craik8afd0f22014-08-21 17:41:57 -0700444 mHasDeferredTranslate = false;
Chris Craik2af46352012-11-26 18:30:17 -0800445 }
446}
447
Chris Craik8afd0f22014-08-21 17:41:57 -0700448size_t DisplayListRenderer::addOpAndUpdateChunk(DisplayListOp* op) {
449 int insertIndex = mDisplayListData->displayListOps.add(op);
450 if (mDeferredBarrierType != kBarrier_None) {
451 // op is first in new chunk
452 mDisplayListData->chunks.push();
453 DisplayListData::Chunk& newChunk = mDisplayListData->chunks.editTop();
454 newChunk.beginOpIndex = insertIndex;
455 newChunk.endOpIndex = insertIndex + 1;
456 newChunk.reorderChildren = (mDeferredBarrierType == kBarrier_OutOfOrder);
457
458 int nextChildIndex = mDisplayListData->children().size();
459 newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex;
460 mDeferredBarrierType = kBarrier_None;
461 } else {
462 // standard case - append to existing chunk
463 mDisplayListData->chunks.editTop().endOpIndex = insertIndex + 1;
464 }
465 return insertIndex;
Chris Craik2af46352012-11-26 18:30:17 -0800466}
467
Chris Craik8afd0f22014-08-21 17:41:57 -0700468size_t DisplayListRenderer::flushAndAddOp(DisplayListOp* op) {
469 flushRestoreToCount();
470 flushTranslate();
471 return addOpAndUpdateChunk(op);
472}
473
474size_t DisplayListRenderer::addStateOp(StateOp* op) {
475 return flushAndAddOp(op);
476}
477
478size_t DisplayListRenderer::addDrawOp(DrawOp* op) {
Chris Craik2af46352012-11-26 18:30:17 -0800479 Rect localBounds;
John Reck3b202512014-06-23 13:13:08 -0700480 if (op->getLocalBounds(localBounds)) {
Chris Craikf0a59072013-11-19 18:00:46 -0800481 bool rejected = quickRejectConservative(localBounds.left, localBounds.top,
Chris Craik2af46352012-11-26 18:30:17 -0800482 localBounds.right, localBounds.bottom);
483 op->setQuickRejected(rejected);
484 }
Chris Craikc1c5f082013-09-11 16:23:37 -0700485
John Reck44fd8d22014-02-26 11:00:11 -0800486 mDisplayListData->hasDrawOps = true;
Chris Craik8afd0f22014-08-21 17:41:57 -0700487 return flushAndAddOp(op);
488}
489
490size_t DisplayListRenderer::addRenderNodeOp(DrawRenderNodeOp* op) {
491 int opIndex = addDrawOp(op);
492 int childIndex = mDisplayListData->addChild(op);
493
494 // update the chunk's child indices
495 DisplayListData::Chunk& chunk = mDisplayListData->chunks.editTop();
496 chunk.endChildIndex = childIndex + 1;
497
498 if (op->renderNode()->stagingProperties().isProjectionReceiver()) {
499 // use staging property, since recording on UI thread
500 mDisplayListData->projectionReceiveIndex = opIndex;
501 }
502 return opIndex;
Romain Guy5ff9df62012-01-23 17:09:05 -0800503}
504
Romain Guy4aa90572010-09-26 18:40:37 -0700505}; // namespace uirenderer
506}; // namespace android