2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "SkiaCanvasProxy.h"
19 #include <cutils/log.h>
20 #include <SkPatchUtils.h>
23 #include <SkPixelRef.h>
30 namespace uirenderer {
32 SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
33 : INHERITED(canvas->width(), canvas->height())
35 , mFilterHwuiCalls(filterHwuiCalls) {}
37 void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
38 mCanvas->drawPaint(paint);
41 void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
42 const SkPaint& paint) {
43 if (!pts || count == 0) {
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;
53 case kPoints_PointMode: {
54 mCanvas->drawPoints(floatArray, floatCount, paint);
57 case kLines_PointMode: {
58 mCanvas->drawLines(floatArray, floatCount, paint);
61 case kPolygon_PointMode: {
62 SkPaint strokedPaint(paint);
63 strokedPaint.setStyle(SkPaint::kStroke_Style);
66 for (size_t i = 0; i < count - 1; i++) {
68 path.lineTo(pts[i+1]);
69 this->drawPath(path, strokedPaint);
75 LOG_ALWAYS_FATAL("Unknown point type");
79 void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
80 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
83 void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
84 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
87 void 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);
95 path.addRRect(roundRect);
96 mCanvas->drawPath(path, paint);
100 void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
101 mCanvas->drawPath(path, paint);
104 void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
105 const SkPaint* paint) {
106 SkPixelRef* pxRef = bitmap.pixelRef();
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()) {
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(),
119 left + bitmap.dimensions().width(),
120 top + bitmap.dimensions().height(),
123 mCanvas->drawBitmap(bitmap, left, top, paint);
127 void 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);
135 void 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");
141 void 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) {
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);
156 SkSurface* SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
157 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
161 void SkiaCanvasProxy::willSave() {
162 mCanvas->save(android::SaveFlags::MatrixClip);
165 static inline SaveFlags::Flags saveFlags(SkCanvas::SaveLayerFlags layerFlags) {
166 SaveFlags::Flags saveFlags = 0;
168 if (!(layerFlags & SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag)) {
169 saveFlags |= SaveFlags::ClipToLayer;
172 if (!(layerFlags & SkCanvas::kIsOpaque_SaveLayerFlag)) {
173 saveFlags |= SaveFlags::HasAlphaLayer;
179 SkCanvas::SaveLayerStrategy SkiaCanvasProxy::getSaveLayerStrategy(const SaveLayerRec& saveLayerRec) {
181 if (saveLayerRec.fBounds) {
182 rect = *saveLayerRec.fBounds;
183 } else if (!mCanvas->getClipBounds(&rect)) {
184 rect = SkRect::MakeEmpty();
186 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, saveLayerRec.fPaint,
187 saveFlags(saveLayerRec.fSaveLayerFlags));
188 return SkCanvas::kNoLayer_SaveLayerStrategy;
191 void SkiaCanvasProxy::willRestore() {
195 void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
196 mCanvas->concat(matrix);
199 void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
200 mCanvas->setMatrix(matrix);
203 void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
204 const SkPaint& paint) {
206 path.addRRect(outer);
207 path.addRRect(inner);
208 path.setFillType(SkPath::kEvenOdd_FillType);
209 this->drawPath(path, paint);
213 * Utility class that converts the incoming text & paint from the given encoding
216 class GlyphIDConverter {
218 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
220 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
221 glyphIDs = (uint16_t*)text;
222 count = byteLength >> 1;
224 // ensure space for one glyph per ID given UTF8 encoding.
225 storage.reset(new uint16_t[byteLength]);
226 glyphIDs = storage.get();
227 count = paint.textToGlyphs(text, byteLength, storage.get());
228 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
236 std::unique_ptr<uint16_t[]> storage;
239 void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
240 const SkPaint& origPaint) {
241 // convert to glyphIDs if necessary
242 GlyphIDConverter glyphs(text, byteLength, origPaint);
244 // compute the glyph positions
245 std::unique_ptr<SkPoint[]> pointStorage(new SkPoint[glyphs.count]);
246 std::unique_ptr<SkScalar[]> glyphWidths(new SkScalar[glyphs.count]);
247 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
249 // compute conservative bounds
250 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
251 // but even more conservative bounds if this is too slow.
253 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
255 // adjust for non-left alignment
256 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
258 for (int i = 0; i < glyphs.count; i++) {
259 stop += glyphWidths[i];
261 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
262 stop = SkScalarHalf(stop);
264 if (glyphs.paint.isVerticalText()) {
271 // setup the first glyph position and adjust bounds if needed
274 if (mCanvas->drawTextAbsolutePos()) {
279 pointStorage[0].set(xBaseline, yBaseline);
281 // setup the remaining glyph positions
282 if (glyphs.paint.isVerticalText()) {
283 for (int i = 1; i < glyphs.count; i++) {
284 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
287 for (int i = 1; i < glyphs.count; i++) {
288 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
292 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
293 mCanvas->drawGlyphs(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
294 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
297 void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
298 const SkPaint& origPaint) {
299 // convert to glyphIDs if necessary
300 GlyphIDConverter glyphs(text, byteLength, origPaint);
302 // convert to relative positions if necessary
304 const SkPoint* posArray;
305 std::unique_ptr<SkPoint[]> pointStorage;
306 if (mCanvas->drawTextAbsolutePos()) {
313 pointStorage.reset(new SkPoint[glyphs.count]);
314 for (int i = 0; i < glyphs.count; i++) {
315 pointStorage[i].fX = pos[i].fX - x;
316 pointStorage[i].fY = pos[i].fY - y;
318 posArray = pointStorage.get();
321 // Compute conservative bounds. If the content has already been processed
322 // by Minikin then it had already computed these bounds. Unfortunately,
323 // there is no way to capture those bounds as part of the Skia drawPosText
324 // API so we need to do that computation again here.
326 for (int i = 0; i < glyphs.count; i++) {
328 glyphs.paint.measureText(&glyphs.glyphIDs[i], sizeof(uint16_t), &glyphBounds);
329 glyphBounds.offset(pos[i].fX, pos[i].fY);
330 bounds.join(glyphBounds);
333 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
334 mCanvas->drawGlyphs(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
335 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
338 void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
339 SkScalar constY, const SkPaint& paint) {
340 const size_t pointCount = byteLength >> 1;
341 std::unique_ptr<SkPoint[]> pts(new SkPoint[pointCount]);
342 for (size_t i = 0; i < pointCount; i++) {
343 pts[i].set(xpos[i], constY);
345 this->onDrawPosText(text, byteLength, pts.get(), paint);
348 void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
349 const SkMatrix* matrix, const SkPaint& origPaint) {
350 // convert to glyphIDs if necessary
351 GlyphIDConverter glyphs(text, byteLength, origPaint);
352 mCanvas->drawGlyphsOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
355 void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
356 const SkPaint& paint) {
357 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
360 void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
361 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
362 if (mFilterHwuiCalls) {
365 SkPatchUtils::VertexData data;
368 mCanvas->getMatrix(&matrix);
369 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
371 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
372 // If it fails to generate the vertices, then we do not draw.
373 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
374 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
375 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
380 void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
381 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
384 void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
386 path.addRRect(roundRect);
387 mCanvas->clipPath(&path, op);
390 void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
391 mCanvas->clipPath(&path, op);
394 void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
395 mCanvas->clipRegion(®ion, op);
398 }; // namespace uirenderer
399 }; // namespace android