Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 1 | /* |
| 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 Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <memory> |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 20 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 21 | #include <log/log.h> |
| 22 | |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 23 | #include <SkLatticeIter.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 24 | #include <SkPaint.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 25 | #include <SkPatchUtils.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 26 | #include <SkPath.h> |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 27 | #include <SkPixelRef.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 28 | #include <SkRRect.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 29 | #include <SkRSXform.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 30 | #include <SkRect.h> |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 31 | #include <SkSurface.h> |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 32 | #include <SkTextBlobRunIterator.h> |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 33 | #include <SkVertices.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 34 | #include "hwui/Bitmap.h" |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | namespace uirenderer { |
| 38 | |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 39 | SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 40 | : INHERITED(canvas->width(), canvas->height()) |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 41 | , mCanvas(canvas) |
| 42 | , mFilterHwuiCalls(filterHwuiCalls) {} |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 43 | |
| 44 | void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { |
| 45 | mCanvas->drawPaint(paint); |
| 46 | } |
| 47 | |
| 48 | void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 49 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 50 | if (!pts || count == 0) { |
| 51 | return; |
| 52 | } |
| 53 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 54 | // convert the SkPoints into floats |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 55 | static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 56 | 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]); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 75 | path.lineTo(pts[i + 1]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 76 | this->drawPath(path, strokedPaint); |
| 77 | path.rewind(); |
| 78 | } |
| 79 | break; |
| 80 | } |
| 81 | default: |
| 82 | LOG_ALWAYS_FATAL("Unknown point type"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { |
| 87 | mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 88 | } |
| 89 | |
| 90 | void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| 91 | mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 92 | } |
| 93 | |
| 94 | void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) { |
| 95 | if (!roundRect.isComplex()) { |
| 96 | const SkRect& rect = roundRect.rect(); |
| 97 | SkVector radii = roundRect.getSimpleRadii(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 98 | mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, radii.fX, radii.fY, |
| 99 | paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 100 | } else { |
| 101 | SkPath path; |
| 102 | path.addRRect(roundRect); |
| 103 | mCanvas->drawPath(path, paint); |
| 104 | } |
| 105 | } |
| 106 | |
Yuqian Li | 9969111 | 2017-02-03 15:01:41 -0500 | [diff] [blame] | 107 | void SkiaCanvasProxy::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, |
| 108 | bool useCenter, const SkPaint& paint) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 109 | mCanvas->drawArc(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, startAngle, sweepAngle, |
| 110 | useCenter, paint); |
Yuqian Li | 9969111 | 2017-02-03 15:01:41 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 113 | void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { |
| 114 | mCanvas->drawPath(path, paint); |
| 115 | } |
| 116 | |
| 117 | void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 118 | const SkPaint* paint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 119 | sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 120 | // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into |
| 121 | // a drawBitmapRect(); pass through an un-subsetted bitmap. |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 122 | if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 123 | SkIPoint origin = bitmap.pixelRefOrigin(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 124 | mCanvas->drawBitmap( |
| 125 | *hwuiBitmap, origin.fX, origin.fY, origin.fX + bitmap.dimensions().width(), |
| 126 | origin.fY + bitmap.dimensions().height(), left, top, |
| 127 | left + bitmap.dimensions().width(), top + bitmap.dimensions().height(), paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 128 | } else { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 129 | mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 130 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 131 | } |
| 132 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 133 | void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 134 | const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame] | 135 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 136 | // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src? |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 137 | Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); |
| 138 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, dst.fTop, |
| 139 | dst.fRight, dst.fBottom, paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 143 | const SkRect& dst, const SkPaint*) { |
| 144 | // TODO make nine-patch drawing a method on Canvas.h |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 145 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported"); |
| 146 | } |
| 147 | |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 148 | void SkiaCanvasProxy::onDrawImage(const SkImage* image, SkScalar left, SkScalar top, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 149 | const SkPaint* paint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 150 | SkBitmap skiaBitmap; |
Stan Iliev | 0a3ff95 | 2017-07-10 17:04:03 -0400 | [diff] [blame] | 151 | SkPixmap pixmap; |
| 152 | if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 153 | onDrawBitmap(skiaBitmap, left, top, paint); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void SkiaCanvasProxy::onDrawImageRect(const SkImage* image, const SkRect* srcPtr, const SkRect& dst, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 158 | const SkPaint* paint, SrcRectConstraint constraint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 159 | SkBitmap skiaBitmap; |
Stan Iliev | 0a3ff95 | 2017-07-10 17:04:03 -0400 | [diff] [blame] | 160 | SkPixmap pixmap; |
| 161 | if (image->peekPixels(&pixmap) && skiaBitmap.installPixels(pixmap)) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 162 | sk_sp<Bitmap> bitmap = Bitmap::createFrom(skiaBitmap.info(), *skiaBitmap.pixelRef()); |
| 163 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(image->width(), image->height()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 164 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, |
| 165 | dst.fTop, dst.fRight, dst.fBottom, paint); |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 170 | const SkPaint*) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 171 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported"); |
| 172 | } |
| 173 | |
| 174 | void SkiaCanvasProxy::onDrawImageLattice(const SkImage* image, const Lattice& lattice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 175 | const SkRect& dst, const SkPaint* paint) { |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 176 | SkLatticeIter iter(lattice, dst); |
| 177 | SkRect srcR, dstR; |
| 178 | while (iter.next(&srcR, &dstR)) { |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 179 | onDrawImageRect(image, &srcR, dstR, paint, SkCanvas::kFast_SrcRectConstraint); |
Stan Iliev | 770e0b5 | 2017-01-05 16:53:14 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Mike Reed | 871cd2d | 2017-03-17 10:15:52 -0400 | [diff] [blame] | 183 | void SkiaCanvasProxy::onDrawVerticesObject(const SkVertices* vertices, SkBlendMode bmode, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 184 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 185 | if (mFilterHwuiCalls) { |
| 186 | return; |
| 187 | } |
Mike Reed | 826deef | 2017-04-04 15:32:04 -0400 | [diff] [blame] | 188 | mCanvas->drawVertices(vertices, bmode, paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 191 | sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 192 | SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | void SkiaCanvasProxy::willSave() { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 197 | mCanvas->save(android::SaveFlags::MatrixClip); |
| 198 | } |
| 199 | |
| 200 | static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) { |
| 201 | SaveFlags::Flags saveFlags = 0; |
| 202 | |
| 203 | if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) { |
| 204 | saveFlags |= SaveFlags::ClipToLayer; |
| 205 | } |
| 206 | |
| 207 | if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) { |
| 208 | saveFlags |= SaveFlags::HasAlphaLayer; |
| 209 | } |
| 210 | |
| 211 | return saveFlags; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 212 | } |
| 213 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 214 | SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy( |
| 215 | const SaveLayerRec& saveLayerRec) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 216 | SkRect rect; |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 217 | if (saveLayerRec.fBounds) { |
| 218 | rect = *saveLayerRec.fBounds; |
| 219 | } else if (!mCanvas->getClipBounds(&rect)) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 220 | rect = SkRect::MakeEmpty(); |
| 221 | } |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 222 | mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 223 | saveFlags(saveLayerRec.fSaveLayerFlags)); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 224 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 225 | } |
| 226 | |
| 227 | void SkiaCanvasProxy::willRestore() { |
| 228 | mCanvas->restore(); |
| 229 | } |
| 230 | |
| 231 | void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { |
| 232 | mCanvas->concat(matrix); |
| 233 | } |
| 234 | |
| 235 | void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 236 | mCanvas->setMatrix(matrix); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 240 | const SkPaint& paint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 241 | SkPath path; |
| 242 | path.addRRect(outer); |
| 243 | path.addRRect(inner); |
| 244 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 245 | this->drawPath(path, paint); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Utility class that converts the incoming text & paint from the given encoding |
| 250 | * into glyphIDs. |
| 251 | */ |
| 252 | class GlyphIDConverter { |
| 253 | public: |
| 254 | GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) { |
| 255 | paint = origPaint; |
| 256 | if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) { |
| 257 | glyphIDs = (uint16_t*)text; |
| 258 | count = byteLength >> 1; |
| 259 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 260 | // ensure space for one glyph per ID given UTF8 encoding. |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 261 | storage.reset(new uint16_t[byteLength]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 262 | glyphIDs = storage.get(); |
| 263 | count = paint.textToGlyphs(text, byteLength, storage.get()); |
| 264 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | SkPaint paint; |
| 269 | uint16_t* glyphIDs; |
| 270 | int count; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 271 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 272 | private: |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 273 | std::unique_ptr<uint16_t[]> storage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 277 | const SkPaint& origPaint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 278 | // convert to glyphIDs if necessary |
| 279 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 280 | |
| 281 | // compute the glyph positions |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 282 | std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 283 | glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get()); |
| 284 | |
| 285 | // compute conservative bounds |
| 286 | // NOTE: We could call the faster paint.getFontBounds for a less accurate, |
| 287 | // but even more conservative bounds if this is too slow. |
| 288 | SkRect bounds; |
| 289 | glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds); |
| 290 | |
| 291 | // adjust for non-left alignment |
| 292 | if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 293 | SkScalar stop = 0; |
| 294 | for (int i = 0; i < glyphs.count; i++) { |
| 295 | stop += glyphWidths[i]; |
| 296 | } |
| 297 | if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 298 | stop = SkScalarHalf(stop); |
| 299 | } |
| 300 | if (glyphs.paint.isVerticalText()) { |
| 301 | y -= stop; |
| 302 | } else { |
| 303 | x -= stop; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // setup the first glyph position and adjust bounds if needed |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 308 | int xBaseline = 0; |
| 309 | int yBaseline = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 310 | if (mCanvas->drawTextAbsolutePos()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 311 | bounds.offset(x, y); |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 312 | xBaseline = x; |
| 313 | yBaseline = y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 314 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 315 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 316 | static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats"); |
| 317 | auto glyphFunc = [&](uint16_t* text, float* positions) { |
| 318 | memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t)); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 319 | size_t posIndex = 0; |
| 320 | // setup the first glyph position |
| 321 | positions[posIndex++] = xBaseline; |
| 322 | positions[posIndex++] = yBaseline; |
| 323 | // setup the remaining glyph positions |
| 324 | if (glyphs.paint.isVerticalText()) { |
| 325 | float yPosition = yBaseline; |
| 326 | for (int i = 1; i < glyphs.count; i++) { |
| 327 | positions[posIndex++] = xBaseline; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 328 | yPosition += glyphWidths[i - 1]; |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 329 | positions[posIndex++] = yPosition; |
| 330 | } |
| 331 | } else { |
| 332 | float xPosition = xBaseline; |
| 333 | for (int i = 1; i < glyphs.count; i++) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 334 | xPosition += glyphWidths[i - 1]; |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 335 | positions[posIndex++] = xPosition; |
| 336 | positions[posIndex++] = yBaseline; |
| 337 | } |
| 338 | } |
| 339 | }; |
| 340 | mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 341 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 345 | const SkPaint& origPaint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 346 | // convert to glyphIDs if necessary |
| 347 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 348 | |
| 349 | // convert to relative positions if necessary |
| 350 | int x, y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 351 | if (mCanvas->drawTextAbsolutePos()) { |
| 352 | x = 0; |
| 353 | y = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 354 | } else { |
| 355 | x = pos[0].fX; |
| 356 | y = pos[0].fY; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 357 | } |
| 358 | |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 359 | // Compute conservative bounds. If the content has already been processed |
| 360 | // by Minikin then it had already computed these bounds. Unfortunately, |
| 361 | // there is no way to capture those bounds as part of the Skia drawPosText |
| 362 | // API so we need to do that computation again here. |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 363 | SkRect bounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 364 | for (int i = 0; i < glyphs.count; i++) { |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 365 | SkRect glyphBounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 366 | glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds); |
| 367 | glyphBounds.offset(pos[i].fX, pos[i].fY); |
| 368 | bounds.join(glyphBounds); |
| 369 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 370 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 371 | static_assert(sizeof(SkPoint) == sizeof(float) * 2, "SkPoint is no longer two floats"); |
| 372 | auto glyphFunc = [&](uint16_t* text, float* positions) { |
| 373 | memcpy(text, glyphs.glyphIDs, glyphs.count * sizeof(uint16_t)); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 374 | if (mCanvas->drawTextAbsolutePos()) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 375 | memcpy(positions, pos, 2 * glyphs.count * sizeof(float)); |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 376 | } else { |
| 377 | for (int i = 0, posIndex = 0; i < glyphs.count; i++) { |
| 378 | positions[posIndex++] = pos[i].fX - x; |
| 379 | positions[posIndex++] = pos[i].fY - y; |
| 380 | } |
| 381 | } |
| 382 | }; |
| 383 | mCanvas->drawGlyphs(glyphFunc, glyphs.count, glyphs.paint, x, y, bounds.fLeft, bounds.fTop, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 384 | bounds.fRight, bounds.fBottom, 0); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 388 | SkScalar constY, const SkPaint& paint) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 389 | const size_t pointCount = byteLength >> 1; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 390 | std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 391 | for (size_t i = 0; i < pointCount; i++) { |
| 392 | pts[i].set(xpos[i], constY); |
| 393 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 394 | this->onDrawPosText(text, byteLength, pts.get(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 398 | const SkMatrix* matrix, const SkPaint& origPaint) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 399 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 400 | } |
| 401 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 402 | void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 403 | const SkRSXform xform[], const SkRect* cullRect, |
| 404 | const SkPaint& paint) { |
| 405 | GlyphIDConverter glyphs(text, byteLength, paint); // Just get count |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 406 | SkMatrix localM, currM, origM; |
| 407 | mCanvas->getMatrix(&currM); |
| 408 | origM = currM; |
| 409 | for (int i = 0; i < glyphs.count; i++) { |
| 410 | localM.setRSXform(*xform++); |
| 411 | currM.setConcat(origM, localM); |
| 412 | mCanvas->setMatrix(currM); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 413 | this->onDrawText((char*)text + (byteLength / glyphs.count * i), byteLength / glyphs.count, |
| 414 | 0, 0, paint); |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 415 | } |
| 416 | mCanvas->setMatrix(origM); |
| 417 | } |
| 418 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 419 | void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 420 | const SkPaint& paint) { |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 421 | SkPaint runPaint = paint; |
| 422 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 423 | SkTextBlobRunIterator it(blob); |
| 424 | for (; !it.done(); it.next()) { |
| 425 | size_t textLen = it.glyphCount() * sizeof(uint16_t); |
| 426 | const SkPoint& offset = it.offset(); |
| 427 | // applyFontToPaint() always overwrites the exact same attributes, |
| 428 | // so it is safe to not re-seed the paint for this reason. |
| 429 | it.applyFontToPaint(&runPaint); |
Derek Sollenberger | 09bd6c2 | 2016-11-03 12:54:06 -0400 | [diff] [blame] | 430 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 431 | switch (it.positioning()) { |
| 432 | case SkTextBlob::kDefault_Positioning: |
| 433 | this->drawText(it.glyphs(), textLen, x + offset.x(), y + offset.y(), runPaint); |
| 434 | break; |
| 435 | case SkTextBlob::kHorizontal_Positioning: { |
| 436 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 437 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 438 | pts[i].set(x + offset.x() + it.pos()[i], y + offset.y()); |
| 439 | } |
| 440 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 441 | break; |
| 442 | } |
| 443 | case SkTextBlob::kFull_Positioning: { |
| 444 | std::unique_ptr<SkPoint[]> pts(new SkPoint[it.glyphCount()]); |
| 445 | for (size_t i = 0; i < it.glyphCount(); i++) { |
| 446 | const size_t xIndex = i * 2; |
| 447 | const size_t yIndex = xIndex + 1; |
| 448 | pts[i].set(x + offset.x() + it.pos()[xIndex], |
| 449 | y + offset.y() + it.pos()[yIndex]); |
| 450 | } |
| 451 | this->drawPosText(it.glyphs(), textLen, pts.get(), runPaint); |
| 452 | break; |
| 453 | } |
| 454 | default: |
| 455 | SK_ABORT("unhandled positioning mode"); |
| 456 | } |
| 457 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 461 | const SkPoint texCoords[4], SkBlendMode bmode, |
| 462 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 463 | if (mFilterHwuiCalls) { |
| 464 | return; |
| 465 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 466 | SkMatrix matrix; |
| 467 | mCanvas->getMatrix(&matrix); |
| 468 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix); |
| 469 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 470 | mCanvas->drawVertices( |
| 471 | SkPatchUtils::MakeVertices(cubics, colors, texCoords, lod.width(), lod.height()).get(), |
| 472 | bmode, paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 475 | void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 476 | mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); |
| 477 | } |
| 478 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 479 | void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 480 | SkPath path; |
| 481 | path.addRRect(roundRect); |
| 482 | mCanvas->clipPath(&path, op); |
| 483 | } |
| 484 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 485 | void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 486 | mCanvas->clipPath(&path, op); |
| 487 | } |
| 488 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame^] | 489 | }; // namespace uirenderer |
| 490 | }; // namespace android |