blob: 9df32b28bf3b95a06bc1a2a7c193d68b4444c356 [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>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040021#include <SkPaint.h>
22#include <SkPath.h>
Tom Hudson17c5adf2015-06-09 15:46:04 -040023#include <SkPixelRef.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040024#include <SkRect.h>
25#include <SkRRect.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050026
Ben Wagner6bbf68d2015-08-19 11:26:06 -040027#include <memory>
28
Derek Sollenberger1db141f2014-12-16 08:37:20 -050029namespace android {
30namespace uirenderer {
31
Tom Hudsonb1476ae2015-03-05 10:30:18 -050032SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050033 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050034 , mCanvas(canvas)
35 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050036
37void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
38 mCanvas->drawPaint(paint);
39}
40
41void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
42 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050043 if (!pts || count == 0) {
44 return;
45 }
46
Derek Sollenberger1db141f2014-12-16 08:37:20 -050047 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040048 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050049 const size_t floatCount = count << 1;
50 const float* floatArray = &pts[0].fX;
51
52 switch (pointMode) {
53 case kPoints_PointMode: {
54 mCanvas->drawPoints(floatArray, floatCount, paint);
55 break;
56 }
57 case kLines_PointMode: {
58 mCanvas->drawLines(floatArray, floatCount, paint);
59 break;
60 }
61 case kPolygon_PointMode: {
62 SkPaint strokedPaint(paint);
63 strokedPaint.setStyle(SkPaint::kStroke_Style);
64
65 SkPath path;
66 for (size_t i = 0; i < count - 1; i++) {
67 path.moveTo(pts[i]);
68 path.lineTo(pts[i+1]);
69 this->drawPath(path, strokedPaint);
70 path.rewind();
71 }
72 break;
73 }
74 default:
75 LOG_ALWAYS_FATAL("Unknown point type");
76 }
77}
78
79void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
80 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
81}
82
83void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
84 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
85}
86
87void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
88 if (!roundRect.isComplex()) {
89 const SkRect& rect = roundRect.rect();
90 SkVector radii = roundRect.getSimpleRadii();
91 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
92 radii.fX, radii.fY, paint);
93 } else {
94 SkPath path;
95 path.addRRect(roundRect);
96 mCanvas->drawPath(path, paint);
97 }
98}
99
100void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
101 mCanvas->drawPath(path, paint);
102}
103
104void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
105 const SkPaint* paint) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400106 SkPixelRef* pxRef = bitmap.pixelRef();
107
108 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
109 // a drawBitmapRect(); pass through an un-subsetted bitmap.
110 if (pxRef && bitmap.dimensions() != pxRef->info().dimensions()) {
111 SkBitmap fullBitmap;
112 fullBitmap.setInfo(pxRef->info());
113 fullBitmap.setPixelRef(pxRef, 0, 0);
114 SkIPoint origin = bitmap.pixelRefOrigin();
115 mCanvas->drawBitmap(fullBitmap, origin.fX, origin.fY,
116 origin.fX + bitmap.dimensions().width(),
117 origin.fY + bitmap.dimensions().height(),
118 left, top,
119 left + bitmap.dimensions().width(),
120 top + bitmap.dimensions().height(),
121 paint);
122 } else {
123 mCanvas->drawBitmap(bitmap, left, top, paint);
124 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500125}
126
127void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400128 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500129 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400130 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500131 mCanvas->drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
132 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
133}
134
135void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
136 const SkRect& dst, const SkPaint*) {
137 //TODO make nine-patch drawing a method on Canvas.h
138 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
139}
140
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500141void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
142 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
143 int indexCount, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500144 if (mFilterHwuiCalls) {
145 return;
146 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500147 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400148 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500149 const int floatCount = vertexCount << 1;
150 const float* vArray = &vertices[0].fX;
151 const float* tArray = (texs) ? &texs[0].fX : NULL;
152 const int* cArray = (colors) ? (int*)colors : NULL;
153 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
154}
155
156SkSurface* SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
157 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
158 return NULL;
159}
160
161void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500162 mCanvas->save(android::SaveFlags::MatrixClip);
163}
164
165static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
166 SaveFlags::Flags saveFlags = 0;
167
168 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
169 saveFlags |= SaveFlags::ClipToLayer;
170 }
171
172 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
173 saveFlags |= SaveFlags::HasAlphaLayer;
174 }
175
176 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500177}
178
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500179SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500180 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500181 if (saveLayerRec.fBounds) {
182 rect = *saveLayerRec.fBounds;
183 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500184 rect = SkRect::MakeEmpty();
185 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500186 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500187 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500188 return SkCanvas::kNoLayer_SaveLayerStrategy;
189}
190
191void SkiaCanvasProxy::willRestore() {
192 mCanvas->restore();
193}
194
195void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
196 mCanvas->concat(matrix);
197}
198
199void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700200 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500201}
202
203void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
204 const SkPaint& paint) {
205 SkPath path;
206 path.addRRect(outer);
207 path.addRRect(inner);
208 path.setFillType(SkPath::kEvenOdd_FillType);
209 this->drawPath(path, paint);
210}
211
212/**
213 * Utility class that converts the incoming text & paint from the given encoding
214 * into glyphIDs.
215 */
216class GlyphIDConverter {
217public:
218 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
219 paint = origPaint;
220 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
221 glyphIDs = (uint16_t*)text;
222 count = byteLength >> 1;
223 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400224 // ensure space for one glyph per ID given UTF8 encoding.
225 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500226 glyphIDs = storage.get();
227 count = paint.textToGlyphs(text, byteLength, storage.get());
228 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
229 }
230 }
231
232 SkPaint paint;
233 uint16_t* glyphIDs;
234 int count;
235private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400236 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500237};
238
239void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
240 const SkPaint& origPaint) {
241 // convert to glyphIDs if necessary
242 GlyphIDConverter glyphs(text, byteLength, origPaint);
243
244 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400245 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
246 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500247 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
248
249 // compute conservative bounds
250 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
251 // but even more conservative bounds if this is too slow.
252 SkRect bounds;
253 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
254
255 // adjust for non-left alignment
256 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
257 SkScalar stop = 0;
258 for (int i = 0; i < glyphs.count; i++) {
259 stop += glyphWidths[i];
260 }
261 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
262 stop = SkScalarHalf(stop);
263 }
264 if (glyphs.paint.isVerticalText()) {
265 y -= stop;
266 } else {
267 x -= stop;
268 }
269 }
270
271 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500272 int xBaseline = 0;
273 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500274 if (mCanvas->drawTextAbsolutePos()) {
275 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500276 xBaseline = x;
277 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500278 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500279 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500280
281 // setup the remaining glyph positions
282 if (glyphs.paint.isVerticalText()) {
283 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500284 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500285 }
286 } else {
287 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500288 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500289 }
290 }
291
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400292 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700293 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500294 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
295}
296
297void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
298 const SkPaint& origPaint) {
299 // convert to glyphIDs if necessary
300 GlyphIDConverter glyphs(text, byteLength, origPaint);
301
302 // convert to relative positions if necessary
303 int x, y;
304 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400305 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500306 if (mCanvas->drawTextAbsolutePos()) {
307 x = 0;
308 y = 0;
309 posArray = pos;
310 } else {
311 x = pos[0].fX;
312 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400313 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500314 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400315 pointStorage[i].fX = pos[i].fX - x;
316 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500317 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400318 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500319 }
320
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400321 // Compute conservative bounds. If the content has already been processed
322 // by Minikin then it had already computed these bounds. Unfortunately,
323 // there is no way to capture those bounds as part of the Skia drawPosText
324 // API so we need to do that computation again here.
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500325 SkRect bounds;
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400326 for (int i = 0; i < glyphs.count; i++) {
327 SkRect glyphBounds;
328 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
329 glyphBounds.offset(pos[i].fX, pos[i].fY);
330 bounds.join(glyphBounds);
331 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500332
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400333 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700334 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500335 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
336}
337
338void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
339 SkScalar constY, const SkPaint& paint) {
340 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400341 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500342 for (size_t i = 0; i < pointCount; i++) {
343 pts[i].set(xpos[i], constY);
344 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400345 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500346}
347
348void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
349 const SkMatrix* matrix, const SkPaint& origPaint) {
350 // convert to glyphIDs if necessary
351 GlyphIDConverter glyphs(text, byteLength, origPaint);
sergeyvdccca442016-03-21 15:38:21 -0700352 mCanvas->drawGlyphsOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500353}
354
355void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
356 const SkPaint& paint) {
357 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
358}
359
360void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
361 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500362 if (mFilterHwuiCalls) {
363 return;
364 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500365 SkPatchUtils::VertexData data;
366
367 SkMatrix matrix;
368 mCanvas->getMatrix(&matrix);
369 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
370
371 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
372 // If it fails to generate the vertices, then we do not draw.
373 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
374 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
375 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
376 paint);
377 }
378}
379
380void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
381 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
382}
383
384void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
385 SkPath path;
386 path.addRRect(roundRect);
387 mCanvas->clipPath(&path, op);
388}
389
390void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
391 mCanvas->clipPath(&path, op);
392}
393
394void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
395 mCanvas->clipRegion(&region, op);
396}
397
398}; // namespace uirenderer
399}; // namespace android