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