blob: c4556c4f5cfb4e2a4da5b9840a07a8c7fb0e471c [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>
Yuqian Liafc221492016-07-18 13:07:42 -040026#include <SkRSXform.h>
Matt Sarett79fc3b12016-03-24 11:02:44 -040027#include <SkSurface.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050028
Ben Wagner6bbf68d2015-08-19 11:26:06 -040029#include <memory>
30
Derek Sollenberger1db141f2014-12-16 08:37:20 -050031namespace android {
32namespace uirenderer {
33
Tom Hudsonb1476ae2015-03-05 10:30:18 -050034SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050035 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050036 , mCanvas(canvas)
37 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050038
39void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
40 mCanvas->drawPaint(paint);
41}
42
43void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
44 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050045 if (!pts || count == 0) {
46 return;
47 }
48
Derek Sollenberger1db141f2014-12-16 08:37:20 -050049 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040050 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050051 const size_t floatCount = count << 1;
52 const float* floatArray = &pts[0].fX;
53
54 switch (pointMode) {
55 case kPoints_PointMode: {
56 mCanvas->drawPoints(floatArray, floatCount, paint);
57 break;
58 }
59 case kLines_PointMode: {
60 mCanvas->drawLines(floatArray, floatCount, paint);
61 break;
62 }
63 case kPolygon_PointMode: {
64 SkPaint strokedPaint(paint);
65 strokedPaint.setStyle(SkPaint::kStroke_Style);
66
67 SkPath path;
68 for (size_t i = 0; i < count - 1; i++) {
69 path.moveTo(pts[i]);
70 path.lineTo(pts[i+1]);
71 this->drawPath(path, strokedPaint);
72 path.rewind();
73 }
74 break;
75 }
76 default:
77 LOG_ALWAYS_FATAL("Unknown point type");
78 }
79}
80
81void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
82 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
83}
84
85void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
86 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
87}
88
89void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
90 if (!roundRect.isComplex()) {
91 const SkRect& rect = roundRect.rect();
92 SkVector radii = roundRect.getSimpleRadii();
93 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
94 radii.fX, radii.fY, paint);
95 } else {
96 SkPath path;
97 path.addRRect(roundRect);
98 mCanvas->drawPath(path, paint);
99 }
100}
101
102void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
103 mCanvas->drawPath(path, paint);
104}
105
106void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
107 const SkPaint* paint) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400108 SkPixelRef* pxRef = bitmap.pixelRef();
109
110 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
111 // a drawBitmapRect(); pass through an un-subsetted bitmap.
112 if (pxRef && bitmap.dimensions() != pxRef->info().dimensions()) {
113 SkBitmap fullBitmap;
114 fullBitmap.setInfo(pxRef->info());
115 fullBitmap.setPixelRef(pxRef, 0, 0);
116 SkIPoint origin = bitmap.pixelRefOrigin();
117 mCanvas->drawBitmap(fullBitmap, origin.fX, origin.fY,
118 origin.fX + bitmap.dimensions().width(),
119 origin.fY + bitmap.dimensions().height(),
120 left, top,
121 left + bitmap.dimensions().width(),
122 top + bitmap.dimensions().height(),
123 paint);
124 } else {
125 mCanvas->drawBitmap(bitmap, left, top, paint);
126 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500127}
128
129void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400130 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500131 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400132 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500133 mCanvas->drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
134 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
135}
136
137void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
138 const SkRect& dst, const SkPaint*) {
139 //TODO make nine-patch drawing a method on Canvas.h
140 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
141}
142
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500143void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
144 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
145 int indexCount, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500146 if (mFilterHwuiCalls) {
147 return;
148 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500149 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400150 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500151 const int floatCount = vertexCount << 1;
152 const float* vArray = &vertices[0].fX;
153 const float* tArray = (texs) ? &texs[0].fX : NULL;
154 const int* cArray = (colors) ? (int*)colors : NULL;
155 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
156}
157
Matt Sarett79fc3b12016-03-24 11:02:44 -0400158sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500159 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
160 return NULL;
161}
162
163void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500164 mCanvas->save(android::SaveFlags::MatrixClip);
165}
166
167static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
168 SaveFlags::Flags saveFlags = 0;
169
170 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
171 saveFlags |= SaveFlags::ClipToLayer;
172 }
173
174 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
175 saveFlags |= SaveFlags::HasAlphaLayer;
176 }
177
178 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500179}
180
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500181SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500182 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500183 if (saveLayerRec.fBounds) {
184 rect = *saveLayerRec.fBounds;
185 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500186 rect = SkRect::MakeEmpty();
187 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500188 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500189 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500190 return SkCanvas::kNoLayer_SaveLayerStrategy;
191}
192
193void SkiaCanvasProxy::willRestore() {
194 mCanvas->restore();
195}
196
197void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
198 mCanvas->concat(matrix);
199}
200
201void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700202 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500203}
204
205void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
206 const SkPaint& paint) {
207 SkPath path;
208 path.addRRect(outer);
209 path.addRRect(inner);
210 path.setFillType(SkPath::kEvenOdd_FillType);
211 this->drawPath(path, paint);
212}
213
214/**
215 * Utility class that converts the incoming text & paint from the given encoding
216 * into glyphIDs.
217 */
218class GlyphIDConverter {
219public:
220 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
221 paint = origPaint;
222 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
223 glyphIDs = (uint16_t*)text;
224 count = byteLength >> 1;
225 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400226 // ensure space for one glyph per ID given UTF8 encoding.
227 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500228 glyphIDs = storage.get();
229 count = paint.textToGlyphs(text, byteLength, storage.get());
230 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
231 }
232 }
233
234 SkPaint paint;
235 uint16_t* glyphIDs;
236 int count;
237private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400238 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500239};
240
241void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
242 const SkPaint& origPaint) {
243 // convert to glyphIDs if necessary
244 GlyphIDConverter glyphs(text, byteLength, origPaint);
245
246 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400247 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
248 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500249 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
250
251 // compute conservative bounds
252 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
253 // but even more conservative bounds if this is too slow.
254 SkRect bounds;
255 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
256
257 // adjust for non-left alignment
258 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
259 SkScalar stop = 0;
260 for (int i = 0; i < glyphs.count; i++) {
261 stop += glyphWidths[i];
262 }
263 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
264 stop = SkScalarHalf(stop);
265 }
266 if (glyphs.paint.isVerticalText()) {
267 y -= stop;
268 } else {
269 x -= stop;
270 }
271 }
272
273 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500274 int xBaseline = 0;
275 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500276 if (mCanvas->drawTextAbsolutePos()) {
277 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500278 xBaseline = x;
279 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500280 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500281 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500282
283 // setup the remaining glyph positions
284 if (glyphs.paint.isVerticalText()) {
285 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500286 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500287 }
288 } else {
289 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500290 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500291 }
292 }
293
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400294 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700295 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500296 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
297}
298
299void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
300 const SkPaint& origPaint) {
301 // convert to glyphIDs if necessary
302 GlyphIDConverter glyphs(text, byteLength, origPaint);
303
304 // convert to relative positions if necessary
305 int x, y;
306 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400307 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500308 if (mCanvas->drawTextAbsolutePos()) {
309 x = 0;
310 y = 0;
311 posArray = pos;
312 } else {
313 x = pos[0].fX;
314 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400315 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500316 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400317 pointStorage[i].fX = pos[i].fX - x;
318 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500319 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400320 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500321 }
322
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400323 // Compute conservative bounds. If the content has already been processed
324 // by Minikin then it had already computed these bounds. Unfortunately,
325 // there is no way to capture those bounds as part of the Skia drawPosText
326 // API so we need to do that computation again here.
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500327 SkRect bounds;
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400328 for (int i = 0; i < glyphs.count; i++) {
329 SkRect glyphBounds;
330 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
331 glyphBounds.offset(pos[i].fX, pos[i].fY);
332 bounds.join(glyphBounds);
333 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500334
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400335 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700336 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500337 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
338}
339
340void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
341 SkScalar constY, const SkPaint& paint) {
342 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400343 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500344 for (size_t i = 0; i < pointCount; i++) {
345 pts[i].set(xpos[i], constY);
346 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400347 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500348}
349
350void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
351 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400352 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500353}
354
Yuqian Liafc221492016-07-18 13:07:42 -0400355void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
356 const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) {
357 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
358 SkMatrix localM, currM, origM;
359 mCanvas->getMatrix(&currM);
360 origM = currM;
361 for (int i = 0; i < glyphs.count; i++) {
362 localM.setRSXform(*xform++);
363 currM.setConcat(origM, localM);
364 mCanvas->setMatrix(currM);
365 this->onDrawText((char*)text + (byteLength / glyphs.count * i),
366 byteLength / glyphs.count, 0, 0, paint);
367 }
368 mCanvas->setMatrix(origM);
369}
370
371
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500372void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
373 const SkPaint& paint) {
374 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
375}
376
377void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
378 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500379 if (mFilterHwuiCalls) {
380 return;
381 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500382 SkPatchUtils::VertexData data;
383
384 SkMatrix matrix;
385 mCanvas->getMatrix(&matrix);
386 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
387
388 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
389 // If it fails to generate the vertices, then we do not draw.
390 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
391 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
392 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
393 paint);
394 }
395}
396
397void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
398 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
399}
400
401void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
402 SkPath path;
403 path.addRRect(roundRect);
404 mCanvas->clipPath(&path, op);
405}
406
407void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
408 mCanvas->clipPath(&path, op);
409}
410
411void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
412 mCanvas->clipRegion(&region, op);
413}
414
415}; // namespace uirenderer
416}; // namespace android