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