blob: 20ca80b954651177477c93894be007fbfd8cb2fc [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
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070019#include <memory>
sergeyvaed7f582016-10-14 16:30:21 -070020
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070021#include <log/log.h>
22
Mark Salyzyndb155372017-01-11 08:30:03 -080023#include "hwui/Bitmap.h"
Stan Iliev770e0b52017-01-05 16:53:14 -050024#include <SkLatticeIter.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050025#include <SkPatchUtils.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040026#include <SkPaint.h>
27#include <SkPath.h>
Tom Hudson17c5adf2015-06-09 15:46:04 -040028#include <SkPixelRef.h>
Ben Wagnera11ee3c2015-08-04 11:14:15 -040029#include <SkRect.h>
30#include <SkRRect.h>
Yuqian Liafc221492016-07-18 13:07:42 -040031#include <SkRSXform.h>
Matt Sarett79fc3b12016-03-24 11:02:44 -040032#include <SkSurface.h>
Derek Sollenberger09bd6c22016-11-03 12:54:06 -040033#include <SkTextBlobRunIterator.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050034
35namespace android {
36namespace uirenderer {
37
Tom Hudsonb1476ae2015-03-05 10:30:18 -050038SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050039 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050040 , mCanvas(canvas)
41 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050042
43void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
44 mCanvas->drawPaint(paint);
45}
46
47void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
48 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050049 if (!pts || count == 0) {
50 return;
51 }
52
Derek Sollenberger1db141f2014-12-16 08:37:20 -050053 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040054 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050055 const size_t floatCount = count << 1;
56 const float* floatArray = &pts[0].fX;
57
58 switch (pointMode) {
59 case kPoints_PointMode: {
60 mCanvas->drawPoints(floatArray, floatCount, paint);
61 break;
62 }
63 case kLines_PointMode: {
64 mCanvas->drawLines(floatArray, floatCount, paint);
65 break;
66 }
67 case kPolygon_PointMode: {
68 SkPaint strokedPaint(paint);
69 strokedPaint.setStyle(SkPaint::kStroke_Style);
70
71 SkPath path;
72 for (size_t i = 0; i < count - 1; i++) {
73 path.moveTo(pts[i]);
74 path.lineTo(pts[i+1]);
75 this->drawPath(path, strokedPaint);
76 path.rewind();
77 }
78 break;
79 }
80 default:
81 LOG_ALWAYS_FATAL("Unknown point type");
82 }
83}
84
85void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
86 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
87}
88
89void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
90 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
91}
92
93void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
94 if (!roundRect.isComplex()) {
95 const SkRect& rect = roundRect.rect();
96 SkVector radii = roundRect.getSimpleRadii();
97 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
98 radii.fX, radii.fY, paint);
99 } else {
100 SkPath path;
101 path.addRRect(roundRect);
102 mCanvas->drawPath(path, paint);
103 }
104}
105
Yuqian Li99691112017-02-03 15:01:41 -0500106void SkiaCanvasProxy::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
107 bool useCenter, const SkPaint& paint) {
108 mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
109 startAngle, sweepAngle, useCenter, paint);
110}
111
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500112void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
113 mCanvas->drawPath(path, paint);
114}
115
116void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
117 const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700118 sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400119 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
120 // a drawBitmapRect(); pass through an un-subsetted bitmap.
sergeyvfc9999502016-10-17 13:07:38 -0700121 if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400122 SkIPoint origin = bitmap.pixelRefOrigin();
sergeyvfc9999502016-10-17 13:07:38 -0700123 mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY,
Tom Hudson17c5adf2015-06-09 15:46:04 -0400124 origin.fX + bitmap.dimensions().width(),
125 origin.fY + bitmap.dimensions().height(),
126 left, top,
127 left + bitmap.dimensions().width(),
128 top + bitmap.dimensions().height(),
129 paint);
130 } else {
sergeyvaed7f582016-10-14 16:30:21 -0700131 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint);
Tom Hudson17c5adf2015-06-09 15:46:04 -0400132 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500133}
134
sergeyvfc9999502016-10-17 13:07:38 -0700135void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400136 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
sergeyvfc9999502016-10-17 13:07:38 -0700137 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400138 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
sergeyvfc9999502016-10-17 13:07:38 -0700139 Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef());
140 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500141 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
142}
143
144void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
145 const SkRect& dst, const SkPaint*) {
146 //TODO make nine-patch drawing a method on Canvas.h
147 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
148}
149
Stan Iliev770e0b52017-01-05 16:53:14 -0500150void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
151 const SkPaint* paint) {
152 SkBitmap skiaBitmap;
153 if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) {
154 onDrawBitmap(skiaBitmap, left, top, paint);
155 }
156}
157
158void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst,
159 const SkPaint* paint, SrcRectConstraint constraint) {
160 SkBitmap skiaBitmap;
161 if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) {
162 sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef());
163 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height());
164 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
165 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
166 }
167}
168
169void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
170 const SkPaint*) {
171 SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported");
172}
173
174void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
175 const SkRect& dst, const SkPaint* paint) {
176 SkLatticeIter iter(lattice, dst);
177 SkRect srcR, dstR;
178 while (iter.next(&srcR, &dstR)) {
179 onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
180 }
181}
182
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500183void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
Mike Reedc2f31df2016-10-28 17:21:45 -0400184 const SkPoint texs[], const SkColor colors[], SkBlendMode, const uint16_t indices[],
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500185 int indexCount, const SkPaint& paint) {
Mike Reedc2f31df2016-10-28 17:21:45 -0400186 // TODO: should we pass through blendmode
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500187 if (mFilterHwuiCalls) {
188 return;
189 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500190 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400191 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500192 const int floatCount = vertexCount << 1;
193 const float* vArray = &vertices[0].fX;
194 const float* tArray = (texs) ? &texs[0].fX : NULL;
195 const int* cArray = (colors) ? (int*)colors : NULL;
196 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
197}
198
Matt Sarett79fc3b12016-03-24 11:02:44 -0400199sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500200 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
201 return NULL;
202}
203
204void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500205 mCanvas->save(android::SaveFlags::MatrixClip);
206}
207
208static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
209 SaveFlags::Flags saveFlags = 0;
210
211 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
212 saveFlags |= SaveFlags::ClipToLayer;
213 }
214
215 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
216 saveFlags |= SaveFlags::HasAlphaLayer;
217 }
218
219 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500220}
221
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500222SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500223 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500224 if (saveLayerRec.fBounds) {
225 rect = *saveLayerRec.fBounds;
226 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500227 rect = SkRect::MakeEmpty();
228 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500229 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500230 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500231 return SkCanvas::kNoLayer_SaveLayerStrategy;
232}
233
234void SkiaCanvasProxy::willRestore() {
235 mCanvas->restore();
236}
237
238void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
239 mCanvas->concat(matrix);
240}
241
242void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700243 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500244}
245
246void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
247 const SkPaint& paint) {
248 SkPath path;
249 path.addRRect(outer);
250 path.addRRect(inner);
251 path.setFillType(SkPath::kEvenOdd_FillType);
252 this->drawPath(path, paint);
253}
254
255/**
256 * Utility class that converts the incoming text & paint from the given encoding
257 * into glyphIDs.
258 */
259class GlyphIDConverter {
260public:
261 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
262 paint = origPaint;
263 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
264 glyphIDs = (uint16_t*)text;
265 count = byteLength >> 1;
266 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400267 // ensure space for one glyph per ID given UTF8 encoding.
268 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500269 glyphIDs = storage.get();
270 count = paint.textToGlyphs(text, byteLength, storage.get());
271 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
272 }
273 }
274
275 SkPaint paint;
276 uint16_t* glyphIDs;
277 int count;
278private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400279 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500280};
281
282void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
283 const SkPaint& origPaint) {
284 // convert to glyphIDs if necessary
285 GlyphIDConverter glyphs(text, byteLength, origPaint);
286
287 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400288 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
289 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500290 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
291
292 // compute conservative bounds
293 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
294 // but even more conservative bounds if this is too slow.
295 SkRect bounds;
296 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
297
298 // adjust for non-left alignment
299 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
300 SkScalar stop = 0;
301 for (int i = 0; i < glyphs.count; i++) {
302 stop += glyphWidths[i];
303 }
304 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
305 stop = SkScalarHalf(stop);
306 }
307 if (glyphs.paint.isVerticalText()) {
308 y -= stop;
309 } else {
310 x -= stop;
311 }
312 }
313
314 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500315 int xBaseline = 0;
316 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500317 if (mCanvas->drawTextAbsolutePos()) {
318 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500319 xBaseline = x;
320 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500321 }
Tom Hudson806a6f02015-02-19 17:11:32 -0500322 pointStorage[0].set(xBaseline, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500323
324 // setup the remaining glyph positions
325 if (glyphs.paint.isVerticalText()) {
326 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500327 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500328 }
329 } else {
330 for (int i = 1; i < glyphs.count; i++) {
Tom Hudson806a6f02015-02-19 17:11:32 -0500331 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500332 }
333 }
334
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, &pointStorage[0].fX, glyphs.count, glyphs.paint,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500337 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
338}
339
340void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
341 const SkPaint& origPaint) {
342 // convert to glyphIDs if necessary
343 GlyphIDConverter glyphs(text, byteLength, origPaint);
344
345 // convert to relative positions if necessary
346 int x, y;
347 const SkPoint* posArray;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400348 std::unique_ptr<SkPoint[]> pointStorage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500349 if (mCanvas->drawTextAbsolutePos()) {
350 x = 0;
351 y = 0;
352 posArray = pos;
353 } else {
354 x = pos[0].fX;
355 y = pos[0].fY;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400356 pointStorage.reset(new SkPoint[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500357 for (int i = 0; i < glyphs.count; i++) {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400358 pointStorage[i].fX = pos[i].fX - x;
359 pointStorage[i].fY = pos[i].fY - y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500360 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400361 posArray = pointStorage.get();
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500362 }
363
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400364 // Compute conservative bounds. If the content has already been processed
365 // by Minikin then it had already computed these bounds. Unfortunately,
366 // there is no way to capture those bounds as part of the Skia drawPosText
367 // API so we need to do that computation again here.
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400368 SkRect bounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400369 for (int i = 0; i < glyphs.count; i++) {
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400370 SkRect glyphBounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400371 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
372 glyphBounds.offset(pos[i].fX, pos[i].fY);
373 bounds.join(glyphBounds);
374 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500375
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400376 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
sergeyvdccca442016-03-21 15:38:21 -0700377 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500378 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
379}
380
381void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
382 SkScalar constY, const SkPaint& paint) {
383 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400384 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500385 for (size_t i = 0; i < pointCount; i++) {
386 pts[i].set(xpos[i], constY);
387 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400388 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500389}
390
391void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
392 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400393 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500394}
395
Yuqian Liafc221492016-07-18 13:07:42 -0400396void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
397 const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) {
398 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
399 SkMatrix localM, currM, origM;
400 mCanvas->getMatrix(&currM);
401 origM = currM;
402 for (int i = 0; i < glyphs.count; i++) {
403 localM.setRSXform(*xform++);
404 currM.setConcat(origM, localM);
405 mCanvas->setMatrix(currM);
406 this->onDrawText((char*)text + (byteLength / glyphs.count * i),
407 byteLength / glyphs.count, 0, 0, paint);
408 }
409 mCanvas->setMatrix(origM);
410}
411
412
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500413void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
414 const SkPaint& paint) {
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400415 SkPaint runPaint = paint;
416
417 SkTextBlobRunIterator it(blob);
418 for (;!it.done(); it.next()) {
419 size_t textLen = it.glyphCount() * sizeof(uint16_t);
420 const SkPoint& offset = it.offset();
421 // applyFontToPaint() always overwrites the exact same attributes,
422 // so it is safe to not re-seed the paint for this reason.
423 it.applyFontToPaint(&runPaint);
424
425 switch (it.positioning()) {
426 case SkTextBlob::kDefault_Positioning:
427 this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
428 break;
429 case SkTextBlob::kHorizontal_Positioning: {
430 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
431 for (size_t i = 0; i < it.glyphCount(); i++) {
432 pts[i].set(x + offset.x() + it.pos()[i], y + offset.y());
433 }
434 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
435 break;
436 }
437 case SkTextBlob::kFull_Positioning: {
438 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
439 for (size_t i = 0; i < it.glyphCount(); i++) {
440 const size_t xIndex = i*2;
441 const size_t yIndex = xIndex + 1;
442 pts[i].set(x + offset.x() + it.pos()[xIndex], y + offset.y() + it.pos()[yIndex]);
443 }
444 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
445 break;
446 }
447 default:
448 SkFAIL("unhandled positioning mode");
449 }
450 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500451}
452
453void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedc2f31df2016-10-28 17:21:45 -0400454 const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500455 if (mFilterHwuiCalls) {
456 return;
457 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500458 SkPatchUtils::VertexData data;
459
460 SkMatrix matrix;
461 mCanvas->getMatrix(&matrix);
462 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
463
464 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
465 // If it fails to generate the vertices, then we do not draw.
466 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
467 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
Mike Reedc2f31df2016-10-28 17:21:45 -0400468 data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500469 paint);
470 }
471}
472
Mike Reed6e49c9f2016-12-02 15:36:59 -0500473void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500474 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
475}
476
Mike Reed6e49c9f2016-12-02 15:36:59 -0500477void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500478 SkPath path;
479 path.addRRect(roundRect);
480 mCanvas->clipPath(&path, op);
481}
482
Mike Reed6e49c9f2016-12-02 15:36:59 -0500483void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500484 mCanvas->clipPath(&path, op);
485}
486
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500487}; // namespace uirenderer
488}; // namespace android