blob: 2e5a45ecbae9586e84aee41b94208aeedd6f9c1b [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>
Mike Reed871cd2d2017-03-17 10:15:52 -040034#include <SkVertices.h>
Derek Sollenberger1db141f2014-12-16 08:37:20 -050035
36namespace android {
37namespace uirenderer {
38
Tom Hudsonb1476ae2015-03-05 10:30:18 -050039SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
Derek Sollenberger1db141f2014-12-16 08:37:20 -050040 : INHERITED(canvas->width(), canvas->height())
Tom Hudsonb1476ae2015-03-05 10:30:18 -050041 , mCanvas(canvas)
42 , mFilterHwuiCalls(filterHwuiCalls) {}
Derek Sollenberger1db141f2014-12-16 08:37:20 -050043
44void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
45 mCanvas->drawPaint(paint);
46}
47
48void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
49 const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -050050 if (!pts || count == 0) {
51 return;
52 }
53
Derek Sollenberger1db141f2014-12-16 08:37:20 -050054 // convert the SkPoints into floats
Ben Wagnere3a40ea2015-08-19 15:49:37 -040055 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger1db141f2014-12-16 08:37:20 -050056 const size_t floatCount = count << 1;
57 const float* floatArray = &pts[0].fX;
58
59 switch (pointMode) {
60 case kPoints_PointMode: {
61 mCanvas->drawPoints(floatArray, floatCount, paint);
62 break;
63 }
64 case kLines_PointMode: {
65 mCanvas->drawLines(floatArray, floatCount, paint);
66 break;
67 }
68 case kPolygon_PointMode: {
69 SkPaint strokedPaint(paint);
70 strokedPaint.setStyle(SkPaint::kStroke_Style);
71
72 SkPath path;
73 for (size_t i = 0; i < count - 1; i++) {
74 path.moveTo(pts[i]);
75 path.lineTo(pts[i+1]);
76 this->drawPath(path, strokedPaint);
77 path.rewind();
78 }
79 break;
80 }
81 default:
82 LOG_ALWAYS_FATAL("Unknown point type");
83 }
84}
85
86void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
87 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
88}
89
90void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
91 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
92}
93
94void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
95 if (!roundRect.isComplex()) {
96 const SkRect& rect = roundRect.rect();
97 SkVector radii = roundRect.getSimpleRadii();
98 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
99 radii.fX, radii.fY, paint);
100 } else {
101 SkPath path;
102 path.addRRect(roundRect);
103 mCanvas->drawPath(path, paint);
104 }
105}
106
Yuqian Li99691112017-02-03 15:01:41 -0500107void SkiaCanvasProxy::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
108 bool useCenter, const SkPaint& paint) {
109 mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
110 startAngle, sweepAngle, useCenter, paint);
111}
112
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500113void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
114 mCanvas->drawPath(path, paint);
115}
116
117void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
118 const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700119 sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400120 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into
121 // a drawBitmapRect(); pass through an un-subsetted bitmap.
sergeyvfc9999502016-10-17 13:07:38 -0700122 if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) {
Tom Hudson17c5adf2015-06-09 15:46:04 -0400123 SkIPoint origin = bitmap.pixelRefOrigin();
sergeyvfc9999502016-10-17 13:07:38 -0700124 mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY,
Tom Hudson17c5adf2015-06-09 15:46:04 -0400125 origin.fX + bitmap.dimensions().width(),
126 origin.fY + bitmap.dimensions().height(),
127 left, top,
128 left + bitmap.dimensions().width(),
129 top + bitmap.dimensions().height(),
130 paint);
131 } else {
sergeyvaed7f582016-10-14 16:30:21 -0700132 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint);
Tom Hudson17c5adf2015-06-09 15:46:04 -0400133 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500134}
135
sergeyvfc9999502016-10-17 13:07:38 -0700136void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr,
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400137 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) {
sergeyvfc9999502016-10-17 13:07:38 -0700138 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height());
Tom Hudson17c5adf2015-06-09 15:46:04 -0400139 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src?
sergeyvfc9999502016-10-17 13:07:38 -0700140 Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef());
141 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500142 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
143}
144
145void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
146 const SkRect& dst, const SkPaint*) {
147 //TODO make nine-patch drawing a method on Canvas.h
148 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
149}
150
Stan Iliev770e0b52017-01-05 16:53:14 -0500151void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top,
152 const SkPaint* paint) {
153 SkBitmap skiaBitmap;
154 if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) {
155 onDrawBitmap(skiaBitmap, left, top, paint);
156 }
157}
158
159void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst,
160 const SkPaint* paint, SrcRectConstraint constraint) {
161 SkBitmap skiaBitmap;
162 if (image->asLegacyBitmap(&skiaBitmap, SkImage::kRO_LegacyBitmapMode)) {
163 sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef());
164 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height());
165 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
166 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
167 }
168}
169
170void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
171 const SkPaint*) {
172 SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported");
173}
174
175void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice,
176 const SkRect& dst, const SkPaint* paint) {
177 SkLatticeIter iter(lattice, dst);
178 SkRect srcR, dstR;
179 while (iter.next(&srcR, &dstR)) {
180 onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kStrict_SrcRectConstraint);
181 }
182}
183
Mike Reed871cd2d2017-03-17 10:15:52 -0400184void SkiaCanvasProxy::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode,
185 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");
Mike Reed871cd2d2017-03-17 10:15:52 -0400192 const int floatCount = vertices->vertexCount() << 1;
193 const float* vArray = (const float*)vertices->positions();
194 const float* tArray = (const float*)vertices->texCoords();
195 const int* cArray = (const int*)vertices->colors();
Mike Reed68136352017-04-03 13:13:51 -0400196 // Can remove this cast after changing to SkVertices::VertexMode
197 SkCanvas::VertexMode vmode = static_cast<SkCanvas::VertexMode>(vertices->mode());
198 mCanvas->drawVertices(vmode, floatCount, vArray, tArray, cArray,
Mike Reed871cd2d2017-03-17 10:15:52 -0400199 vertices->indices(), vertices->indexCount(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500200}
201
Matt Sarett79fc3b12016-03-24 11:02:44 -0400202sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500203 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
204 return NULL;
205}
206
207void SkiaCanvasProxy::willSave() {
Florin Malitaeecff562015-12-21 10:43:01 -0500208 mCanvas->save(android::SaveFlags::MatrixClip);
209}
210
211static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
212 SaveFlags::Flags saveFlags = 0;
213
214 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
215 saveFlags |= SaveFlags::ClipToLayer;
216 }
217
218 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
219 saveFlags |= SaveFlags::HasAlphaLayer;
220 }
221
222 return saveFlags;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500223}
224
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500225SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500226 SkRect rect;
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500227 if (saveLayerRec.fBounds) {
228 rect = *saveLayerRec.fBounds;
229 } else if (!mCanvas->getClipBounds(&rect)) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500230 rect = SkRect::MakeEmpty();
231 }
Leon Scroggins III5518e7c2016-01-04 09:37:42 -0500232 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
Florin Malitaeecff562015-12-21 10:43:01 -0500233 saveFlags(saveLayerRec.fSaveLayerFlags));
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500234 return SkCanvas::kNoLayer_SaveLayerStrategy;
235}
236
237void SkiaCanvasProxy::willRestore() {
238 mCanvas->restore();
239}
240
241void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
242 mCanvas->concat(matrix);
243}
244
245void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
Chris Craik6daa13c2015-08-19 13:32:12 -0700246 mCanvas->setMatrix(matrix);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500247}
248
249void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
250 const SkPaint& paint) {
251 SkPath path;
252 path.addRRect(outer);
253 path.addRRect(inner);
254 path.setFillType(SkPath::kEvenOdd_FillType);
255 this->drawPath(path, paint);
256}
257
258/**
259 * Utility class that converts the incoming text & paint from the given encoding
260 * into glyphIDs.
261 */
262class GlyphIDConverter {
263public:
264 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
265 paint = origPaint;
266 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
267 glyphIDs = (uint16_t*)text;
268 count = byteLength >> 1;
269 } else {
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400270 // ensure space for one glyph per ID given UTF8 encoding.
271 storage.reset(new uint16_t[byteLength]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500272 glyphIDs = storage.get();
273 count = paint.textToGlyphs(text, byteLength, storage.get());
274 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
275 }
276 }
277
278 SkPaint paint;
279 uint16_t* glyphIDs;
280 int count;
281private:
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400282 std::unique_ptr<uint16_t[]> storage;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500283};
284
285void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
286 const SkPaint& origPaint) {
287 // convert to glyphIDs if necessary
288 GlyphIDConverter glyphs(text, byteLength, origPaint);
289
290 // compute the glyph positions
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400291 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500292 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
293
294 // compute conservative bounds
295 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
296 // but even more conservative bounds if this is too slow.
297 SkRect bounds;
298 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
299
300 // adjust for non-left alignment
301 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
302 SkScalar stop = 0;
303 for (int i = 0; i < glyphs.count; i++) {
304 stop += glyphWidths[i];
305 }
306 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
307 stop = SkScalarHalf(stop);
308 }
309 if (glyphs.paint.isVerticalText()) {
310 y -= stop;
311 } else {
312 x -= stop;
313 }
314 }
315
316 // setup the first glyph position and adjust bounds if needed
Tom Hudson806a6f02015-02-19 17:11:32 -0500317 int xBaseline = 0;
318 int yBaseline = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500319 if (mCanvas->drawTextAbsolutePos()) {
320 bounds.offset(x,y);
Tom Hudson806a6f02015-02-19 17:11:32 -0500321 xBaseline = x;
322 yBaseline = y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500323 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500324
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400325 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Stan Iliev0b58d992017-03-30 18:22:27 -0400326 auto glyphFunc = [&] (uint16_t* text, float* positions) {
327 memcpy(text, glyphs.glyphIDs, glyphs.count*sizeof(uint16_t));
328 size_t posIndex = 0;
329 // setup the first glyph position
330 positions[posIndex++] = xBaseline;
331 positions[posIndex++] = yBaseline;
332 // setup the remaining glyph positions
333 if (glyphs.paint.isVerticalText()) {
334 float yPosition = yBaseline;
335 for (int i = 1; i < glyphs.count; i++) {
336 positions[posIndex++] = xBaseline;
337 yPosition += glyphWidths[i-1];
338 positions[posIndex++] = yPosition;
339 }
340 } else {
341 float xPosition = xBaseline;
342 for (int i = 1; i < glyphs.count; i++) {
343 xPosition += glyphWidths[i-1];
344 positions[posIndex++] = xPosition;
345 positions[posIndex++] = yBaseline;
346 }
347 }
348 };
349 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
350 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500351}
352
353void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
354 const SkPaint& origPaint) {
355 // convert to glyphIDs if necessary
356 GlyphIDConverter glyphs(text, byteLength, origPaint);
357
358 // convert to relative positions if necessary
359 int x, y;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500360 if (mCanvas->drawTextAbsolutePos()) {
361 x = 0;
362 y = 0;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500363 } else {
364 x = pos[0].fX;
365 y = pos[0].fY;
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500366 }
367
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400368 // Compute conservative bounds. If the content has already been processed
369 // by Minikin then it had already computed these bounds. Unfortunately,
370 // there is no way to capture those bounds as part of the Skia drawPosText
371 // API so we need to do that computation again here.
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400372 SkRect bounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400373 for (int i = 0; i < glyphs.count; i++) {
Stan Ilievd84d2ee2016-07-25 13:32:25 -0400374 SkRect glyphBounds = SkRect::MakeEmpty();
Derek Sollenberger35934cc2016-03-23 14:59:10 -0400375 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
376 glyphBounds.offset(pos[i].fX, pos[i].fY);
377 bounds.join(glyphBounds);
378 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500379
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400380 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Stan Iliev0b58d992017-03-30 18:22:27 -0400381 auto glyphFunc = [&] (uint16_t* text, float* positions) {
382 memcpy(text, glyphs.glyphIDs, glyphs.count*sizeof(uint16_t));
383 if (mCanvas->drawTextAbsolutePos()) {
384 memcpy(positions, pos, 2*glyphs.count*sizeof(float));
385 } else {
386 for (int i = 0, posIndex = 0; i < glyphs.count; i++) {
387 positions[posIndex++] = pos[i].fX - x;
388 positions[posIndex++] = pos[i].fY - y;
389 }
390 }
391 };
392 mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop,
393 bounds.fRight, bounds.fBottom, 0);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500394}
395
396void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
397 SkScalar constY, const SkPaint& paint) {
398 const size_t pointCount = byteLength >> 1;
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400399 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500400 for (size_t i = 0; i < pointCount; i++) {
401 pts[i].set(xpos[i], constY);
402 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400403 this->onDrawPosText(text, byteLength, pts.get(), paint);
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500404}
405
406void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
407 const SkMatrix* matrix, const SkPaint& origPaint) {
Yuqian Liafc221492016-07-18 13:07:42 -0400408 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported");
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500409}
410
Yuqian Liafc221492016-07-18 13:07:42 -0400411void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength,
412 const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) {
413 GlyphIDConverter glyphs(text, byteLength, paint); // Just get count
414 SkMatrix localM, currM, origM;
415 mCanvas->getMatrix(&currM);
416 origM = currM;
417 for (int i = 0; i < glyphs.count; i++) {
418 localM.setRSXform(*xform++);
419 currM.setConcat(origM, localM);
420 mCanvas->setMatrix(currM);
421 this->onDrawText((char*)text + (byteLength / glyphs.count * i),
422 byteLength / glyphs.count, 0, 0, paint);
423 }
424 mCanvas->setMatrix(origM);
425}
426
427
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500428void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
429 const SkPaint& paint) {
Derek Sollenberger09bd6c22016-11-03 12:54:06 -0400430 SkPaint runPaint = paint;
431
432 SkTextBlobRunIterator it(blob);
433 for (;!it.done(); it.next()) {
434 size_t textLen = it.glyphCount() * sizeof(uint16_t);
435 const SkPoint& offset = it.offset();
436 // applyFontToPaint() always overwrites the exact same attributes,
437 // so it is safe to not re-seed the paint for this reason.
438 it.applyFontToPaint(&runPaint);
439
440 switch (it.positioning()) {
441 case SkTextBlob::kDefault_Positioning:
442 this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint);
443 break;
444 case SkTextBlob::kHorizontal_Positioning: {
445 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
446 for (size_t i = 0; i < it.glyphCount(); i++) {
447 pts[i].set(x + offset.x() + it.pos()[i], y + offset.y());
448 }
449 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
450 break;
451 }
452 case SkTextBlob::kFull_Positioning: {
453 std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]);
454 for (size_t i = 0; i < it.glyphCount(); i++) {
455 const size_t xIndex = i*2;
456 const size_t yIndex = xIndex + 1;
457 pts[i].set(x + offset.x() + it.pos()[xIndex], y + offset.y() + it.pos()[yIndex]);
458 }
459 this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint);
460 break;
461 }
462 default:
463 SkFAIL("unhandled positioning mode");
464 }
465 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500466}
467
468void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
Mike Reedc2f31df2016-10-28 17:21:45 -0400469 const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) {
Tom Hudsonb1476ae2015-03-05 10:30:18 -0500470 if (mFilterHwuiCalls) {
471 return;
472 }
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500473 SkPatchUtils::VertexData data;
474
475 SkMatrix matrix;
476 mCanvas->getMatrix(&matrix);
477 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
478
479 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
480 // If it fails to generate the vertices, then we do not draw.
481 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
482 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
Mike Reedc2f31df2016-10-28 17:21:45 -0400483 data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount,
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500484 paint);
485 }
486}
487
Mike Reed6e49c9f2016-12-02 15:36:59 -0500488void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500489 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
490}
491
Mike Reed6e49c9f2016-12-02 15:36:59 -0500492void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500493 SkPath path;
494 path.addRRect(roundRect);
495 mCanvas->clipPath(&path, op);
496}
497
Mike Reed6e49c9f2016-12-02 15:36:59 -0500498void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) {
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500499 mCanvas->clipPath(&path, op);
500}
501
Derek Sollenberger1db141f2014-12-16 08:37:20 -0500502}; // namespace uirenderer
503}; // namespace android