SkiaCanvasProxy.cpp revision 770e0b500793bce45442b5f403913d14017df4e8
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 19#include "hwui/Bitmap.h" 20 21#include <cutils/log.h> 22#include <SkLatticeIter.h> 23#include <SkPatchUtils.h> 24#include <SkPaint.h> 25#include <SkPath.h> 26#include <SkPixelRef.h> 27#include <SkRect.h> 28#include <SkRRect.h> 29#include <SkRSXform.h> 30#include <SkSurface.h> 31#include <SkTextBlobRunIterator.h> 32 33#include <memory> 34 35namespace android { 36namespace uirenderer { 37 38SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls) 39 : INHERITED(canvas->width(), canvas->height()) 40 , mCanvas(canvas) 41 , mFilterHwuiCalls(filterHwuiCalls) {} 42 43void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) { 44 mCanvas->drawPaint(paint); 45} 46 47void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[], 48 const SkPaint& paint) { 49 if (!pts || count == 0) { 50 return; 51 } 52 53 // convert the SkPoints into floats 54 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 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 85void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) { 86 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); 87} 88 89void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) { 90 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint); 91} 92 93void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) { 94 if (!roundRect.isComplex()) { 95 const SkRect& rect = roundRect.rect(); 96 SkVector radii = roundRect.getSimpleRadii(); 97 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, 98 radii.fX, radii.fY, paint); 99 } else { 100 SkPath path; 101 path.addRRect(roundRect); 102 mCanvas->drawPath(path, paint); 103 } 104} 105 106void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) { 107 mCanvas->drawPath(path, paint); 108} 109 110void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top, 111 const SkPaint* paint) { 112 sk_sp<Bitmap> hwuiBitmap = Bitmap::createFrom(bitmap.info(), *bitmap.pixelRef()); 113 // HWUI doesn't support extractSubset(), so convert any subsetted bitmap into 114 // a drawBitmapRect(); pass through an un-subsetted bitmap. 115 if (hwuiBitmap && bitmap.dimensions() != hwuiBitmap->info().dimensions()) { 116 SkIPoint origin = bitmap.pixelRefOrigin(); 117 mCanvas->drawBitmap(*hwuiBitmap, origin.fX, origin.fY, 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 { 125 mCanvas->drawBitmap(*hwuiBitmap, left, top, paint); 126 } 127} 128 129void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& skBitmap, const SkRect* srcPtr, 130 const SkRect& dst, const SkPaint* paint, SrcRectConstraint) { 131 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(skBitmap.width(), skBitmap.height()); 132 // TODO: if bitmap is a subset, do we need to add pixelRefOrigin to src? 133 Bitmap* bitmap = reinterpret_cast<Bitmap*>(skBitmap.pixelRef()); 134 mCanvas->drawBitmap(*bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, 135 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint); 136} 137 138void 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 144void 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 152void 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 163void SkiaCanvasProxy::onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, 164 const SkPaint*) { 165 SkDEBUGFAIL("SkiaCanvasProxy::onDrawImageNine is not yet supported"); 166} 167 168void 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 177void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[], 178 const SkPoint texs[], const SkColor colors[], SkBlendMode, const uint16_t indices[], 179 int indexCount, const SkPaint& paint) { 180 // TODO: should we pass through blendmode 181 if (mFilterHwuiCalls) { 182 return; 183 } 184 // convert the SkPoints into floats 185 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 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 193sk_sp<SkSurface> SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) { 194 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported"); 195 return NULL; 196} 197 198void SkiaCanvasProxy::willSave() { 199 mCanvas->save(android::SaveFlags::MatrixClip); 200} 201 202static 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; 214} 215 216SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) { 217 SkRect rect; 218 if (saveLayerRec.fBounds) { 219 rect = *saveLayerRec.fBounds; 220 } else if (!mCanvas->getClipBounds(&rect)) { 221 rect = SkRect::MakeEmpty(); 222 } 223 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint, 224 saveFlags(saveLayerRec.fSaveLayerFlags)); 225 return SkCanvas::kNoLayer_SaveLayerStrategy; 226} 227 228void SkiaCanvasProxy::willRestore() { 229 mCanvas->restore(); 230} 231 232void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) { 233 mCanvas->concat(matrix); 234} 235 236void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) { 237 mCanvas->setMatrix(matrix); 238} 239 240void 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 */ 253class GlyphIDConverter { 254public: 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 { 261 // ensure space for one glyph per ID given UTF8 encoding. 262 storage.reset(new uint16_t[byteLength]); 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; 272private: 273 std::unique_ptr<uint16_t[]> storage; 274}; 275 276void 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 282 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]); 283 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]); 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 309 int xBaseline = 0; 310 int yBaseline = 0; 311 if (mCanvas->drawTextAbsolutePos()) { 312 bounds.offset(x,y); 313 xBaseline = x; 314 yBaseline = y; 315 } 316 pointStorage[0].set(xBaseline, yBaseline); 317 318 // setup the remaining glyph positions 319 if (glyphs.paint.isVerticalText()) { 320 for (int i = 1; i < glyphs.count; i++) { 321 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY); 322 } 323 } else { 324 for (int i = 1; i < glyphs.count; i++) { 325 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline); 326 } 327 } 328 329 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 330 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint, 331 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); 332} 333 334void 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; 342 std::unique_ptr<SkPoint[]> pointStorage; 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; 350 pointStorage.reset(new SkPoint[glyphs.count]); 351 for (int i = 0; i < glyphs.count; i++) { 352 pointStorage[i].fX = pos[i].fX - x; 353 pointStorage[i].fY = pos[i].fY - y; 354 } 355 posArray = pointStorage.get(); 356 } 357 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. 362 SkRect bounds = SkRect::MakeEmpty(); 363 for (int i = 0; i < glyphs.count; i++) { 364 SkRect glyphBounds = SkRect::MakeEmpty(); 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 } 369 370 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats"); 371 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y, 372 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0); 373} 374 375void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], 376 SkScalar constY, const SkPaint& paint) { 377 const size_t pointCount = byteLength >> 1; 378 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]); 379 for (size_t i = 0; i < pointCount; i++) { 380 pts[i].set(xpos[i], constY); 381 } 382 this->onDrawPosText(text, byteLength, pts.get(), paint); 383} 384 385void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 386 const SkMatrix* matrix, const SkPaint& origPaint) { 387 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextOnPath is not supported"); 388} 389 390void 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 407void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 408 const SkPaint& paint) { 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 } 445} 446 447void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 448 const SkPoint texCoords[4], SkBlendMode bmode, const SkPaint& paint) { 449 if (mFilterHwuiCalls) { 450 return; 451 } 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, 462 data.fTexCoords, data.fColors, bmode, data.fIndices, data.fIndexCount, 463 paint); 464 } 465} 466 467void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle) { 468 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op); 469} 470 471void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkClipOp op, ClipEdgeStyle) { 472 SkPath path; 473 path.addRRect(roundRect); 474 mCanvas->clipPath(&path, op); 475} 476 477void SkiaCanvasProxy::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle) { 478 mCanvas->clipPath(&path, op); 479} 480 481void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkClipOp op) { 482 mCanvas->clipRegion(®ion, op); 483} 484 485}; // namespace uirenderer 486}; // namespace android 487