blob: d96ca2afed0018b0d1e76d31d01300f10c99442d [file] [log] [blame]
Derek Sollenberger1db141f2014-12-16 08:37:20 -05001/*
2 * Copyright (C) 2015 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#include "SkiaCanvasProxy.h"
18
19#include <cutils/log.h>
20#include <SkPatchUtils.h>
21
22namespace android {
23namespace uirenderer {
24
Tom Hudsonb1476ae2015-03-05 10:30:18 -050025SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050026 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050027 , mCanvas(canvas)
28 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050029
30void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
31 mCanvas->drawPaint(paint);
32}
33
34void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
35 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050036 if (!pts || count == 0) {
37 return;
38 }
39
Derek Sollenberger1db141f2014-12-16 08:37:20 -050040 // convert the SkPoints into floats
41 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
42 const size_t floatCount = count << 1;
43 const float* floatArray = &pts[0].fX;
44
45 switch (pointMode) {
46 case kPoints_PointMode: {
47 mCanvas->drawPoints(floatArray, floatCount, paint);
48 break;
49 }
50 case kLines_PointMode: {
51 mCanvas->drawLines(floatArray, floatCount, paint);
52 break;
53 }
54 case kPolygon_PointMode: {
55 SkPaint strokedPaint(paint);
56 strokedPaint.setStyle(SkPaint::kStroke_Style);
57
58 SkPath path;
59 for (size_t i = 0; i < count - 1; i++) {
60 path.moveTo(pts[i]);
61 path.lineTo(pts[i+1]);
62 this->drawPath(path, strokedPaint);
63 path.rewind();
64 }
65 break;
66 }
67 default:
68 LOG_ALWAYS_FATAL("Unknown point type");
69 }
70}
71
72void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
73 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
74}
75
76void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
77 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
78}
79
80void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
81 if (!roundRect.isComplex()) {
82 const SkRect& rect = roundRect.rect();
83 SkVector radii = roundRect.getSimpleRadii();
84 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
85 radii.fX, radii.fY, paint);
86 } else {
87 SkPath path;
88 path.addRRect(roundRect);
89 mCanvas->drawPath(path, paint);
90 }
91}
92
93void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
94 mCanvas->drawPath(path, paint);
95}
96
97void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
98 const SkPaint* paint) {
99 mCanvas->drawBitmap(bitmap, left, top, paint);
100}
101
102void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
103 const SkRect& dst, const SkPaint* paint, DrawBitmapRectFlags) {
104 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
105 mCanvas->drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
106 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
107}
108
109void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
110 const SkRect& dst, const SkPaint*) {
111 //TODO make nine-patch drawing a method on Canvas.h
112 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
113}
114
115void SkiaCanvasProxy::onDrawSprite(const SkBitmap& bitmap, int left, int top,
116 const SkPaint* paint) {
117 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
Tom Hudsonac7b6d32015-06-30 11:26:13 -0400118 mCanvas->setLocalMatrix(SkMatrix::I());
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500119 mCanvas->drawBitmap(bitmap, left, top, paint);
120 mCanvas->restore();
121}
122
123void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
124 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
125 int indexCount, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500126 if (mFilterHwuiCalls) {
127 return;
128 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500129 // convert the SkPoints into floats
130 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
131 const int floatCount = vertexCount << 1;
132 const float* vArray = &vertices[0].fX;
133 const float* tArray = (texs) ? &texs[0].fX : NULL;
134 const int* cArray = (colors) ? (int*)colors : NULL;
135 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
136}
137
138SkSurface* SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
139 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
140 return NULL;
141}
142
143void SkiaCanvasProxy::willSave() {
144 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
145}
146
147SkCanvas::SaveLayerStrategy SkiaCanvasProxy::willSaveLayer(const SkRect* rectPtr,
148 const SkPaint* paint, SaveFlags flags) {
149 SkRect rect;
150 if (rectPtr) {
151 rect = *rectPtr;
152 } else if(!mCanvas->getClipBounds(&rect)) {
153 rect = SkRect::MakeEmpty();
154 }
155 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint, flags);
156 return SkCanvas::kNoLayer_SaveLayerStrategy;
157}
158
159void SkiaCanvasProxy::willRestore() {
160 mCanvas->restore();
161}
162
163void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
164 mCanvas->concat(matrix);
165}
166
167void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Tom Hudsonac7b6d32015-06-30 11:26:13 -0400168 // SkCanvas setMatrix() is relative to the Canvas origin, but OpenGLRenderer's
169 // setMatrix() is relative to device origin; call setLocalMatrix() instead.
170 mCanvas->setLocalMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500171}
172
173void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
174 const SkPaint& paint) {
175 SkPath path;
176 path.addRRect(outer);
177 path.addRRect(inner);
178 path.setFillType(SkPath::kEvenOdd_FillType);
179 this->drawPath(path, paint);
180}
181
182/**
183 * Utility class that converts the incoming text & paint from the given encoding
184 * into glyphIDs.
185 */
186class GlyphIDConverter {
187public:
188 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
189 paint = origPaint;
190 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
191 glyphIDs = (uint16_t*)text;
192 count = byteLength >> 1;
193 } else {
194 storage.reset(byteLength); // ensures space for one glyph per ID given UTF8 encoding.
195 glyphIDs = storage.get();
196 count = paint.textToGlyphs(text, byteLength, storage.get());
197 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
198 }
199 }
200
201 SkPaint paint;
202 uint16_t* glyphIDs;
203 int count;
204private:
205 SkAutoSTMalloc<32, uint16_t> storage;
206};
207
208void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
209 const SkPaint& origPaint) {
210 // convert to glyphIDs if necessary
211 GlyphIDConverter glyphs(text, byteLength, origPaint);
212
213 // compute the glyph positions
214 SkAutoSTMalloc<32, SkPoint> pointStorage(glyphs.count);
215 SkAutoSTMalloc<32, SkScalar> glyphWidths(glyphs.count);
216 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
217
218 // compute conservative bounds
219 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
220 // but even more conservative bounds if this is too slow.
221 SkRect bounds;
222 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
223
224 // adjust for non-left alignment
225 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
226 SkScalar stop = 0;
227 for (int i = 0; i < glyphs.count; i++) {
228 stop += glyphWidths[i];
229 }
230 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
231 stop = SkScalarHalf(stop);
232 }
233 if (glyphs.paint.isVerticalText()) {
234 y -= stop;
235 } else {
236 x -= stop;
237 }
238 }
239
240 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500241 int xBaseline = 0;
242 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500243 if (mCanvas->drawTextAbsolutePos()) {
244 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500245 xBaseline = x;
246 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500247 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500248 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500249
250 // setup the remaining glyph positions
251 if (glyphs.paint.isVerticalText()) {
252 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500253 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500254 }
255 } else {
256 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500257 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500258 }
259 }
260
261 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
262 mCanvas->drawText(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
263 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
264}
265
266void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
267 const SkPaint& origPaint) {
268 // convert to glyphIDs if necessary
269 GlyphIDConverter glyphs(text, byteLength, origPaint);
270
271 // convert to relative positions if necessary
272 int x, y;
273 const SkPoint* posArray;
274 SkAutoSTMalloc<32, SkPoint> pointStorage;
275 if (mCanvas->drawTextAbsolutePos()) {
276 x = 0;
277 y = 0;
278 posArray = pos;
279 } else {
280 x = pos[0].fX;
281 y = pos[0].fY;
282 posArray = pointStorage.reset(glyphs.count);
283 for (int i = 0; i < glyphs.count; i++) {
284 pointStorage[i].fX = pos[i].fX- x;
285 pointStorage[i].fY = pos[i].fY- y;
286 }
287 }
288
289 // compute conservative bounds
290 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
291 // but even more conservative bounds if this is too slow.
292 SkRect bounds;
293 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
Tom Hudson20c2b3e2015-04-15 13:54:32 -0400294 bounds.offset(x, y);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500295
296 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
297 mCanvas->drawText(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
298 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
299}
300
301void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
302 SkScalar constY, const SkPaint& paint) {
303 const size_t pointCount = byteLength >> 1;
304 SkAutoSTMalloc<32, SkPoint> storage(pointCount);
305 SkPoint* pts = storage.get();
306 for (size_t i = 0; i < pointCount; i++) {
307 pts[i].set(xpos[i], constY);
308 }
309 this->onDrawPosText(text, byteLength, pts, paint);
310}
311
312void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
313 const SkMatrix* matrix, const SkPaint& origPaint) {
314 // convert to glyphIDs if necessary
315 GlyphIDConverter glyphs(text, byteLength, origPaint);
316 mCanvas->drawTextOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
317}
318
319void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
320 const SkPaint& paint) {
321 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
322}
323
324void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
325 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500326 if (mFilterHwuiCalls) {
327 return;
328 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500329 SkPatchUtils::VertexData data;
330
331 SkMatrix matrix;
332 mCanvas->getMatrix(&matrix);
333 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
334
335 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
336 // If it fails to generate the vertices, then we do not draw.
337 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
338 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
339 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
340 paint);
341 }
342}
343
344void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
345 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
346}
347
348void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
349 SkPath path;
350 path.addRRect(roundRect);
351 mCanvas->clipPath(&path, op);
352}
353
354void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
355 mCanvas->clipPath(&path, op);
356}
357
358void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
359 mCanvas->clipRegion(&region, op);
360}
361
362}; // namespace uirenderer
363}; // namespace android