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> |
| 22 | #include <SkPatchUtils.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 23 | #include <SkPaint.h> |
| 24 | #include <SkPath.h> |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 25 | #include <SkPixelRef.h> |
Ben Wagner | a11ee3c | 2015-08-04 11:14:15 -0400 | [diff] [blame] | 26 | #include <SkRect.h> |
| 27 | #include <SkRRect.h> |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 28 | #include <SkRSXform.h> |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 29 | #include <SkSurface.h> |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 30 | |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 36 | SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 37 | : INHERITED(canvas->width(), canvas->height()) |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 38 | , mCanvas(canvas) |
| 39 | , mFilterHwuiCalls(filterHwuiCalls) {} |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 40 | |
| 41 | void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { |
| 42 | mCanvas->drawPaint(paint); |
| 43 | } |
| 44 | |
| 45 | void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], |
| 46 | const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 47 | if (!pts || count == 0) { |
| 48 | return; |
| 49 | } |
| 50 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 51 | // convert the SkPoints into floats |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 52 | 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] | 53 | const size_t floatCount = count << 1; |
| 54 | const float* floatArray = &pts[0].fX; |
| 55 | |
| 56 | switch (pointMode) { |
| 57 | case kPoints_PointMode: { |
| 58 | mCanvas->drawPoints(floatArray, floatCount, paint); |
| 59 | break; |
| 60 | } |
| 61 | case kLines_PointMode: { |
| 62 | mCanvas->drawLines(floatArray, floatCount, paint); |
| 63 | break; |
| 64 | } |
| 65 | case kPolygon_PointMode: { |
| 66 | SkPaint strokedPaint(paint); |
| 67 | strokedPaint.setStyle(SkPaint::kStroke_Style); |
| 68 | |
| 69 | SkPath path; |
| 70 | for (size_t i = 0; i < count - 1; i++) { |
| 71 | path.moveTo(pts[i]); |
| 72 | path.lineTo(pts[i+1]); |
| 73 | this->drawPath(path, strokedPaint); |
| 74 | path.rewind(); |
| 75 | } |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | LOG_ALWAYS_FATAL("Unknown point type"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { |
| 84 | mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 85 | } |
| 86 | |
| 87 | void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { |
| 88 | mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); |
| 89 | } |
| 90 | |
| 91 | void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) { |
| 92 | if (!roundRect.isComplex()) { |
| 93 | const SkRect& rect = roundRect.rect(); |
| 94 | SkVector radii = roundRect.getSimpleRadii(); |
| 95 | mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, |
| 96 | radii.fX, radii.fY, paint); |
| 97 | } else { |
| 98 | SkPath path; |
| 99 | path.addRRect(roundRect); |
| 100 | mCanvas->drawPath(path, paint); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { |
| 105 | mCanvas->drawPath(path, paint); |
| 106 | } |
| 107 | |
| 108 | void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, |
| 109 | const SkPaint* paint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 110 | sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 111 | // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into |
| 112 | // a drawBitmapRect(); pass through an un-subsetted bitmap. |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 113 | if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 114 | SkIPoint origin = bitmap.pixelRefOrigin(); |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 115 | mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY, |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 116 | origin.fX + bitmap.dimensions().width(), |
| 117 | origin.fY + bitmap.dimensions().height(), |
| 118 | left, top, |
| 119 | left + bitmap.dimensions().width(), |
| 120 | top + bitmap.dimensions().height(), |
| 121 | paint); |
| 122 | } else { |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 123 | mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 124 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 125 | } |
| 126 | |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 127 | void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 128 | const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { |
sergeyv | fc999950 | 2016-10-17 13:07:38 -0700 | [diff] [blame^] | 129 | SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); |
Tom Hudson | 17c5adf | 2015-06-09 15:46:04 -0400 | [diff] [blame] | 130 | // 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^] | 131 | Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); |
| 132 | mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 133 | dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); |
| 134 | } |
| 135 | |
| 136 | void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 137 | const SkRect& dst, const SkPaint*) { |
| 138 | //TODO make nine-patch drawing a method on Canvas.h |
| 139 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported"); |
| 140 | } |
| 141 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 142 | void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[], |
| 143 | const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[], |
| 144 | int indexCount, const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 145 | if (mFilterHwuiCalls) { |
| 146 | return; |
| 147 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 148 | // convert the SkPoints into floats |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 149 | 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] | 150 | const int floatCount = vertexCount << 1; |
| 151 | const float* vArray = &vertices[0].fX; |
| 152 | const float* tArray = (texs) ? &texs[0].fX : NULL; |
| 153 | const int* cArray = (colors) ? (int*)colors : NULL; |
| 154 | mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint); |
| 155 | } |
| 156 | |
Matt Sarett | 79fc3b1 | 2016-03-24 11:02:44 -0400 | [diff] [blame] | 157 | sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 158 | SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | void SkiaCanvasProxy::willSave() { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 163 | mCanvas->save(android::SaveFlags::MatrixClip); |
| 164 | } |
| 165 | |
| 166 | static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) { |
| 167 | SaveFlags::Flags saveFlags = 0; |
| 168 | |
| 169 | if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) { |
| 170 | saveFlags |= SaveFlags::ClipToLayer; |
| 171 | } |
| 172 | |
| 173 | if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) { |
| 174 | saveFlags |= SaveFlags::HasAlphaLayer; |
| 175 | } |
| 176 | |
| 177 | return saveFlags; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 180 | SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 181 | SkRect rect; |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 182 | if (saveLayerRec.fBounds) { |
| 183 | rect = *saveLayerRec.fBounds; |
| 184 | } else if (!mCanvas->getClipBounds(&rect)) { |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 185 | rect = SkRect::MakeEmpty(); |
| 186 | } |
Leon Scroggins III | 5518e7c | 2016-01-04 09:37:42 -0500 | [diff] [blame] | 187 | mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 188 | saveFlags(saveLayerRec.fSaveLayerFlags)); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 189 | return SkCanvas::kNoLayer_SaveLayerStrategy; |
| 190 | } |
| 191 | |
| 192 | void SkiaCanvasProxy::willRestore() { |
| 193 | mCanvas->restore(); |
| 194 | } |
| 195 | |
| 196 | void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { |
| 197 | mCanvas->concat(matrix); |
| 198 | } |
| 199 | |
| 200 | void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 201 | mCanvas->setMatrix(matrix); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 205 | const SkPaint& paint) { |
| 206 | SkPath path; |
| 207 | path.addRRect(outer); |
| 208 | path.addRRect(inner); |
| 209 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 210 | this->drawPath(path, paint); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Utility class that converts the incoming text & paint from the given encoding |
| 215 | * into glyphIDs. |
| 216 | */ |
| 217 | class GlyphIDConverter { |
| 218 | public: |
| 219 | GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) { |
| 220 | paint = origPaint; |
| 221 | if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) { |
| 222 | glyphIDs = (uint16_t*)text; |
| 223 | count = byteLength >> 1; |
| 224 | } else { |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 225 | // ensure space for one glyph per ID given UTF8 encoding. |
| 226 | storage.reset(new uint16_t[byteLength]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 227 | glyphIDs = storage.get(); |
| 228 | count = paint.textToGlyphs(text, byteLength, storage.get()); |
| 229 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | SkPaint paint; |
| 234 | uint16_t* glyphIDs; |
| 235 | int count; |
| 236 | private: |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 237 | std::unique_ptr<uint16_t[]> storage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 241 | const SkPaint& origPaint) { |
| 242 | // convert to glyphIDs if necessary |
| 243 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 244 | |
| 245 | // compute the glyph positions |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 246 | std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]); |
| 247 | std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 248 | glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get()); |
| 249 | |
| 250 | // compute conservative bounds |
| 251 | // NOTE: We could call the faster paint.getFontBounds for a less accurate, |
| 252 | // but even more conservative bounds if this is too slow. |
| 253 | SkRect bounds; |
| 254 | glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds); |
| 255 | |
| 256 | // adjust for non-left alignment |
| 257 | if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 258 | SkScalar stop = 0; |
| 259 | for (int i = 0; i < glyphs.count; i++) { |
| 260 | stop += glyphWidths[i]; |
| 261 | } |
| 262 | if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 263 | stop = SkScalarHalf(stop); |
| 264 | } |
| 265 | if (glyphs.paint.isVerticalText()) { |
| 266 | y -= stop; |
| 267 | } else { |
| 268 | x -= stop; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // setup the first glyph position and adjust bounds if needed |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 273 | int xBaseline = 0; |
| 274 | int yBaseline = 0; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 275 | if (mCanvas->drawTextAbsolutePos()) { |
| 276 | bounds.offset(x,y); |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 277 | xBaseline = x; |
| 278 | yBaseline = y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 279 | } |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 280 | pointStorage[0].set(xBaseline, yBaseline); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 281 | |
| 282 | // setup the remaining glyph positions |
| 283 | if (glyphs.paint.isVerticalText()) { |
| 284 | for (int i = 1; i < glyphs.count; i++) { |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 285 | pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 286 | } |
| 287 | } else { |
| 288 | for (int i = 1; i < glyphs.count; i++) { |
Tom Hudson | 806a6f0 | 2015-02-19 17:11:32 -0500 | [diff] [blame] | 289 | pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 293 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 294 | mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint, |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 295 | x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); |
| 296 | } |
| 297 | |
| 298 | void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 299 | const SkPaint& origPaint) { |
| 300 | // convert to glyphIDs if necessary |
| 301 | GlyphIDConverter glyphs(text, byteLength, origPaint); |
| 302 | |
| 303 | // convert to relative positions if necessary |
| 304 | int x, y; |
| 305 | const SkPoint* posArray; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 306 | std::unique_ptr<SkPoint[]> pointStorage; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 307 | if (mCanvas->drawTextAbsolutePos()) { |
| 308 | x = 0; |
| 309 | y = 0; |
| 310 | posArray = pos; |
| 311 | } else { |
| 312 | x = pos[0].fX; |
| 313 | y = pos[0].fY; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 314 | pointStorage.reset(new SkPoint[glyphs.count]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 315 | for (int i = 0; i < glyphs.count; i++) { |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 316 | pointStorage[i].fX = pos[i].fX - x; |
| 317 | pointStorage[i].fY = pos[i].fY - y; |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 318 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 319 | posArray = pointStorage.get(); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 322 | // Compute conservative bounds. If the content has already been processed |
| 323 | // by Minikin then it had already computed these bounds. Unfortunately, |
| 324 | // there is no way to capture those bounds as part of the Skia drawPosText |
| 325 | // API so we need to do that computation again here. |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 326 | SkRect bounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 327 | for (int i = 0; i < glyphs.count; i++) { |
Stan Iliev | d84d2ee | 2016-07-25 13:32:25 -0400 | [diff] [blame] | 328 | SkRect glyphBounds = SkRect::MakeEmpty(); |
Derek Sollenberger | 35934cc | 2016-03-23 14:59:10 -0400 | [diff] [blame] | 329 | glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds); |
| 330 | glyphBounds.offset(pos[i].fX, pos[i].fY); |
| 331 | bounds.join(glyphBounds); |
| 332 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 333 | |
Ben Wagner | e3a40ea | 2015-08-19 15:49:37 -0400 | [diff] [blame] | 334 | static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 335 | 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] | 336 | bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); |
| 337 | } |
| 338 | |
| 339 | void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 340 | SkScalar constY, const SkPaint& paint) { |
| 341 | const size_t pointCount = byteLength >> 1; |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 342 | std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 343 | for (size_t i = 0; i < pointCount; i++) { |
| 344 | pts[i].set(xpos[i], constY); |
| 345 | } |
Ben Wagner | 6bbf68d | 2015-08-19 11:26:06 -0400 | [diff] [blame] | 346 | this->onDrawPosText(text, byteLength, pts.get(), paint); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 350 | const SkMatrix* matrix, const SkPaint& origPaint) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 351 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 354 | void SkiaCanvasProxy::onDrawTextRSXform(const void* text, size_t byteLength, |
| 355 | const SkRSXform xform[], const SkRect* cullRect, const SkPaint& paint) { |
| 356 | GlyphIDConverter glyphs(text, byteLength, paint); // Just get count |
| 357 | SkMatrix localM, currM, origM; |
| 358 | mCanvas->getMatrix(&currM); |
| 359 | origM = currM; |
| 360 | for (int i = 0; i < glyphs.count; i++) { |
| 361 | localM.setRSXform(*xform++); |
| 362 | currM.setConcat(origM, localM); |
| 363 | mCanvas->setMatrix(currM); |
| 364 | this->onDrawText((char*)text + (byteLength / glyphs.count * i), |
| 365 | byteLength / glyphs.count, 0, 0, paint); |
| 366 | } |
| 367 | mCanvas->setMatrix(origM); |
| 368 | } |
| 369 | |
| 370 | |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 371 | void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 372 | const SkPaint& paint) { |
| 373 | SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported"); |
| 374 | } |
| 375 | |
| 376 | void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 377 | const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { |
Tom Hudson | b1476ae | 2015-03-05 10:30:18 -0500 | [diff] [blame] | 378 | if (mFilterHwuiCalls) { |
| 379 | return; |
| 380 | } |
Derek Sollenberger | 1db141f | 2014-12-16 08:37:20 -0500 | [diff] [blame] | 381 | SkPatchUtils::VertexData data; |
| 382 | |
| 383 | SkMatrix matrix; |
| 384 | mCanvas->getMatrix(&matrix); |
| 385 | SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix); |
| 386 | |
| 387 | // It automatically adjusts lodX and lodY in case it exceeds the number of indices. |
| 388 | // If it fails to generate the vertices, then we do not draw. |
| 389 | if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) { |
| 390 | this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints, |
| 391 | data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount, |
| 392 | paint); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) { |
| 397 | mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); |
| 398 | } |
| 399 | |
| 400 | void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) { |
| 401 | SkPath path; |
| 402 | path.addRRect(roundRect); |
| 403 | mCanvas->clipPath(&path, op); |
| 404 | } |
| 405 | |
| 406 | void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) { |
| 407 | mCanvas->clipPath(&path, op); |
| 408 | } |
| 409 | |
| 410 | void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) { |
| 411 | mCanvas->clipRegion(®ion, op); |
| 412 | } |
| 413 | |
| 414 | }; // namespace uirenderer |
| 415 | }; // namespace android |