blob: 6620458cefb643fc3ffe34e3cafb787f9eccd863 [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
sergeyvaed7f582016-10-14 16:30:21 -070019#include "hwui/Bitmap.h"
20
Derek Sollenberger1db141f2014-12-16 08:37:20 -050021#include <cutils/log.h>
22#include <SkPatchUtils.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040023#include <SkPaint.h>
24#include <SkPath.h>
Tom Hudson17c5adf2015-06-09 15:46:04 -040025#include <SkPixelRef.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040026#include <SkRect.h>
27#include <SkRRect.h>
Yuqian Liafc221492016-07-18 13:07:42 -040028#include <SkRSXform.h>
Matt Sarett79fc3b12016-03-24 11:02:44 -040029#include <SkSurface.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050030
Ben Wagner6bbf68d2015-08-19 11:26:06 -040031#include <memory>
32
Derek Sollenberger1db141f2014-12-16 08:37:20 -050033namespace android {
34namespace uirenderer {
35
Tom Hudsonb1476ae2015-03-05 10:30:18 -050036SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050037 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050038 , mCanvas(canvas)
39 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050040
41void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
42 mCanvas->drawPaint(paint);
43}
44
45void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
46 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050047 if (!pts || count == 0) {
48 return;
49 }
50
Derek Sollenberger1db141f2014-12-16 08:37:20 -050051 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040052 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050053 const size_t floatCount = count << 1;
54 const float* floatArray = &pts[0].fX;
55
56 switch (pointMode) {
57 case kPoints_PointMode: {
58 mCanvas->drawPoints(floatArray, floatCount, paint);
59 break;
60 }
61 case kLines_PointMode: {
62 mCanvas->drawLines(floatArray, floatCount, paint);
63 break;
64 }
65 case kPolygon_PointMode: {
66 SkPaint strokedPaint(paint);
67 strokedPaint.setStyle(SkPaint::kStroke_Style);
68
69 SkPath path;
70 for (size_t i = 0; i < count - 1; i++) {
71 path.moveTo(pts[i]);
72 path.lineTo(pts[i+1]);
73 this->drawPath(path, strokedPaint);
74 path.rewind();
75 }
76 break;
77 }
78 default:
79 LOG_ALWAYS_FATAL("Unknown point type");
80 }
81}
82
83void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
84 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
85}
86
87void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
88 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
89}
90
91void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
92 if (!roundRect.isComplex()) {
93 const SkRect& rect = roundRect.rect();
94 SkVector radii = roundRect.getSimpleRadii();
95 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
96 radii.fX, radii.fY, paint);
97 } else {
98 SkPath path;
99 path.addRRect(roundRect);
100 mCanvas->drawPath(path, paint);
101 }
102}
103
104void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
105 mCanvas->drawPath(path, paint);
106}
107
108void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
109 const SkPaint* paint) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400110 SkPixelRef* pxRef = bitmap.pixelRef();
111
112 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
113 // a drawBitmapRect(); pass through an un-subsetted bitmap.
114 if (pxRef && bitmap.dimensions() != pxRef->info().dimensions()) {
115 SkBitmap fullBitmap;
116 fullBitmap.setInfo(pxRef->info());
117 fullBitmap.setPixelRef(pxRef, 0, 0);
118 SkIPoint origin = bitmap.pixelRefOrigin();
119 mCanvas->drawBitmap(fullBitmap, origin.fX, origin.fY,
120 origin.fX + bitmap.dimensions().width(),
121 origin.fY + bitmap.dimensions().height(),
122 left, top,
123 left + bitmap.dimensions().width(),
124 top + bitmap.dimensions().height(),
125 paint);
126 } else {
sergeyvaed7f582016-10-14 16:30:21 -0700127 auto hwuiBitmap= Bitmap::createFrom(bitmap.info(), *pxRef);
128 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint);
Tom Hudson17c5adf2015-06-09 15:46:04 -0400129 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500130}
131
132void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400133 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500134 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400135 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500136 mCanvas->drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
137 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
138}
139
140void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
141 const SkRect& dst, const SkPaint*) {
142 //TODO make nine-patch drawing a method on Canvas.h
143 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
144}
145
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500146void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
147 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
148 int indexCount, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500149 if (mFilterHwuiCalls) {
150 return;
151 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500152 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400153 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500154 const int floatCount = vertexCount << 1;
155 const float* vArray = &vertices[0].fX;
156 const float* tArray = (texs) ? &texs[0].fX : NULL;
157 const int* cArray = (colors) ? (int*)colors : NULL;
158 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
159}
160
Matt Sarett79fc3b12016-03-24 11:02:44 -0400161sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500162 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
163 return NULL;
164}
165
166void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500167 mCanvas->save(android::SaveFlags::MatrixClip);
168}
169
170static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
171 SaveFlags::Flags saveFlags = 0;
172
173 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
174 saveFlags |= SaveFlags::ClipToLayer;
175 }
176
177 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
178 saveFlags |= SaveFlags::HasAlphaLayer;
179 }
180
181 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500182}
183
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500184SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500185 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500186 if (saveLayerRec.fBounds) {
187 rect = *saveLayerRec.fBounds;
188 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500189 rect = SkRect::MakeEmpty();
190 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500191 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500192 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500193 return SkCanvas::kNoLayer_SaveLayerStrategy;
194}
195
196void SkiaCanvasProxy::willRestore() {
197 mCanvas->restore();
198}
199
200void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
201 mCanvas->concat(matrix);
202}
203
204void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700205 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500206}
207
208void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
209 const SkPaint& paint) {
210 SkPath path;
211 path.addRRect(outer);
212 path.addRRect(inner);
213 path.setFillType(SkPath::kEvenOdd_FillType);
214 this->drawPath(path, paint);
215}
216
217/**
218 * Utility class that converts the incoming text & paint from the given encoding
219 * into glyphIDs.
220 */
221class GlyphIDConverter {
222public:
223 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
224 paint = origPaint;
225 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
226 glyphIDs = (uint16_t*)text;
227 count = byteLength >> 1;
228 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400229 // ensure space for one glyph per ID given UTF8 encoding.
230 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500231 glyphIDs = storage.get();
232 count = paint.textToGlyphs(text, byteLength, storage.get());
233 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
234 }
235 }
236
237 SkPaint paint;
238 uint16_t* glyphIDs;
239 int count;
240private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400241 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500242};
243
244void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
245 const SkPaint& origPaint) {
246 // convert to glyphIDs if necessary
247 GlyphIDConverter glyphs(text, byteLength, origPaint);
248
249 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400250 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
251 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500252 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
253
254 // compute conservative bounds
255 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
256 // but even more conservative bounds if this is too slow.
257 SkRect bounds;
258 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
259
260 // adjust for non-left alignment
261 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
262 SkScalar stop = 0;
263 for (int i = 0; i < glyphs.count; i++) {
264 stop += glyphWidths[i];
265 }
266 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
267 stop = SkScalarHalf(stop);
268 }
269 if (glyphs.paint.isVerticalText()) {
270 y -= stop;
271 } else {
272 x -= stop;
273 }
274 }
275
276 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500277 int xBaseline = 0;
278 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500279 if (mCanvas->drawTextAbsolutePos()) {
280 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500281 xBaseline = x;
282 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500283 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500284 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500285
286 // setup the remaining glyph positions
287 if (glyphs.paint.isVerticalText()) {
288 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500289 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500290 }
291 } else {
292 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500293 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500294 }
295 }
296
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400297 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700298 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500299 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
300}
301
302void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
303 const SkPaint& origPaint) {
304 // convert to glyphIDs if necessary
305 GlyphIDConverter glyphs(text, byteLength, origPaint);
306
307 // convert to relative positions if necessary
308 int x, y;
309 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400310 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500311 if (mCanvas->drawTextAbsolutePos()) {
312 x = 0;
313 y = 0;
314 posArray = pos;
315 } else {
316 x = pos[0].fX;
317 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400318 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500319 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400320 pointStorage[i].fX = pos[i].fX - x;
321 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500322 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400323 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500324 }
325
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400326 // Compute conservative bounds. If the content has already been processed
327 // by Minikin then it had already computed these bounds. Unfortunately,
328 // there is no way to capture those bounds as part of the Skia drawPosText
329 // API so we need to do that computation again here.
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400330 SkRect bounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400331 for (int i = 0; i < glyphs.count; i++) {
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400332 SkRect glyphBounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400333 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
334 glyphBounds.offset(pos[i].fX, pos[i].fY);
335 bounds.join(glyphBounds);
336 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500337
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400338 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700339 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500340 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
341}
342
343void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
344 SkScalar constY, const SkPaint& paint) {
345 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400346 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500347 for (size_t i = 0; i < pointCount; i++) {
348 pts[i].set(xpos[i], constY);
349 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400350 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500351}
352
353void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
354 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400355 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500356}
357
Yuqian Liafc221492016-07-18 13:07:42 -0400358void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
359 const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) {
360 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
361 SkMatrix localM, currM, origM;
362 mCanvas->getMatrix(&currM);
363 origM = currM;
364 for (int i = 0; i < glyphs.count; i++) {
365 localM.setRSXform(*xform++);
366 currM.setConcat(origM, localM);
367 mCanvas->setMatrix(currM);
368 this->onDrawText((char*)text + (byteLength / glyphs.count * i),
369 byteLength / glyphs.count, 0, 0, paint);
370 }
371 mCanvas->setMatrix(origM);
372}
373
374
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500375void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
376 const SkPaint& paint) {
377 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
378}
379
380void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
381 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500382 if (mFilterHwuiCalls) {
383 return;
384 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500385 SkPatchUtils::VertexData data;
386
387 SkMatrix matrix;
388 mCanvas->getMatrix(&matrix);
389 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
390
391 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
392 // If it fails to generate the vertices, then we do not draw.
393 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
394 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
395 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
396 paint);
397 }
398}
399
400void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
401 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
402}
403
404void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
405 SkPath path;
406 path.addRRect(roundRect);
407 mCanvas->clipPath(&path, op);
408}
409
410void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
411 mCanvas->clipPath(&path, op);
412}
413
414void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
415 mCanvas->clipRegion(&region, op);
416}
417
418}; // namespace uirenderer
419}; // namespace android