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 namespace uirenderer {
25 SkiaCanvasProxy::SkiaCanvasProxy(Canvas* canvas, bool filterHwuiCalls)
26 : INHERITED(canvas->width(), canvas->height())
28 , mFilterHwuiCalls(filterHwuiCalls) {}
30 void SkiaCanvasProxy::onDrawPaint(const SkPaint& paint) {
31 mCanvas->drawPaint(paint);
34 void SkiaCanvasProxy::onDrawPoints(PointMode pointMode, size_t count, const SkPoint pts[],
35 const SkPaint& paint) {
36 if (!pts || count == 0) {
40 // convert the SkPoints into floats
41 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
42 const size_t floatCount = count << 1;
43 const float* floatArray = &pts[0].fX;
46 case kPoints_PointMode: {
47 mCanvas->drawPoints(floatArray, floatCount, paint);
50 case kLines_PointMode: {
51 mCanvas->drawLines(floatArray, floatCount, paint);
54 case kPolygon_PointMode: {
55 SkPaint strokedPaint(paint);
56 strokedPaint.setStyle(SkPaint::kStroke_Style);
59 for (size_t i = 0; i < count - 1; i++) {
61 path.lineTo(pts[i+1]);
62 this->drawPath(path, strokedPaint);
68 LOG_ALWAYS_FATAL("Unknown point type");
72 void SkiaCanvasProxy::onDrawOval(const SkRect& rect, const SkPaint& paint) {
73 mCanvas->drawOval(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
76 void SkiaCanvasProxy::onDrawRect(const SkRect& rect, const SkPaint& paint) {
77 mCanvas->drawRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint);
80 void SkiaCanvasProxy::onDrawRRect(const SkRRect& roundRect, const SkPaint& paint) {
81 if (!roundRect.isComplex()) {
82 const SkRect& rect = roundRect.rect();
83 SkVector radii = roundRect.getSimpleRadii();
84 mCanvas->drawRoundRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom,
85 radii.fX, radii.fY, paint);
88 path.addRRect(roundRect);
89 mCanvas->drawPath(path, paint);
93 void SkiaCanvasProxy::onDrawPath(const SkPath& path, const SkPaint& paint) {
94 mCanvas->drawPath(path, paint);
97 void SkiaCanvasProxy::onDrawBitmap(const SkBitmap& bitmap, SkScalar left, SkScalar top,
98 const SkPaint* paint) {
99 mCanvas->drawBitmap(bitmap, left, top, paint);
102 void SkiaCanvasProxy::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* srcPtr,
103 const SkRect& dst, const SkPaint* paint, DrawBitmapRectFlags) {
104 SkRect src = (srcPtr) ? *srcPtr : SkRect::MakeWH(bitmap.width(), bitmap.height());
105 mCanvas->drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom,
106 dst.fLeft, dst.fTop, dst.fRight, dst.fBottom, paint);
109 void SkiaCanvasProxy::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
110 const SkRect& dst, const SkPaint*) {
111 //TODO make nine-patch drawing a method on Canvas.h
112 SkDEBUGFAIL("SkiaCanvasProxy::onDrawBitmapNine is not yet supported");
115 void SkiaCanvasProxy::onDrawSprite(const SkBitmap& bitmap, int left, int top,
116 const SkPaint* paint) {
117 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
118 mCanvas->setMatrix(SkMatrix::I());
119 mCanvas->drawBitmap(bitmap, left, top, paint);
123 void SkiaCanvasProxy::onDrawVertices(VertexMode mode, int vertexCount, const SkPoint vertices[],
124 const SkPoint texs[], const SkColor colors[], SkXfermode*, const uint16_t indices[],
125 int indexCount, const SkPaint& paint) {
126 if (mFilterHwuiCalls) {
129 // convert the SkPoints into floats
130 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
131 const int floatCount = vertexCount << 1;
132 const float* vArray = &vertices[0].fX;
133 const float* tArray = (texs) ? &texs[0].fX : NULL;
134 const int* cArray = (colors) ? (int*)colors : NULL;
135 mCanvas->drawVertices(mode, floatCount, vArray, tArray, cArray, indices, indexCount, paint);
138 SkSurface* SkiaCanvasProxy::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
139 SkDEBUGFAIL("SkiaCanvasProxy::onNewSurface is not supported");
143 void SkiaCanvasProxy::willSave() {
144 mCanvas->save(SkCanvas::kMatrixClip_SaveFlag);
147 SkCanvas::SaveLayerStrategy SkiaCanvasProxy::willSaveLayer(const SkRect* rectPtr,
148 const SkPaint* paint, SaveFlags flags) {
152 } else if(!mCanvas->getClipBounds(&rect)) {
153 rect = SkRect::MakeEmpty();
155 mCanvas->saveLayer(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, paint, flags);
156 return SkCanvas::kNoLayer_SaveLayerStrategy;
159 void SkiaCanvasProxy::willRestore() {
163 void SkiaCanvasProxy::didConcat(const SkMatrix& matrix) {
164 mCanvas->concat(matrix);
167 void SkiaCanvasProxy::didSetMatrix(const SkMatrix& matrix) {
168 mCanvas->setMatrix(matrix);
171 void SkiaCanvasProxy::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
172 const SkPaint& paint) {
174 path.addRRect(outer);
175 path.addRRect(inner);
176 path.setFillType(SkPath::kEvenOdd_FillType);
177 this->drawPath(path, paint);
181 * Utility class that converts the incoming text & paint from the given encoding
184 class GlyphIDConverter {
186 GlyphIDConverter(const void* text, size_t byteLength, const SkPaint& origPaint) {
188 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
189 glyphIDs = (uint16_t*)text;
190 count = byteLength >> 1;
192 storage.reset(byteLength); // ensures space for one glyph per ID given UTF8 encoding.
193 glyphIDs = storage.get();
194 count = paint.textToGlyphs(text, byteLength, storage.get());
195 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
203 SkAutoSTMalloc<32, uint16_t> storage;
206 void SkiaCanvasProxy::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
207 const SkPaint& origPaint) {
208 // convert to glyphIDs if necessary
209 GlyphIDConverter glyphs(text, byteLength, origPaint);
211 // compute the glyph positions
212 SkAutoSTMalloc<32, SkPoint> pointStorage(glyphs.count);
213 SkAutoSTMalloc<32, SkScalar> glyphWidths(glyphs.count);
214 glyphs.paint.getTextWidths(glyphs.glyphIDs, glyphs.count << 1, glyphWidths.get());
216 // compute conservative bounds
217 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
218 // but even more conservative bounds if this is too slow.
220 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
222 // adjust for non-left alignment
223 if (glyphs.paint.getTextAlign() != SkPaint::kLeft_Align) {
225 for (int i = 0; i < glyphs.count; i++) {
226 stop += glyphWidths[i];
228 if (glyphs.paint.getTextAlign() == SkPaint::kCenter_Align) {
229 stop = SkScalarHalf(stop);
231 if (glyphs.paint.isVerticalText()) {
238 // setup the first glyph position and adjust bounds if needed
241 if (mCanvas->drawTextAbsolutePos()) {
246 pointStorage[0].set(xBaseline, yBaseline);
248 // setup the remaining glyph positions
249 if (glyphs.paint.isVerticalText()) {
250 for (int i = 1; i < glyphs.count; i++) {
251 pointStorage[i].set(xBaseline, glyphWidths[i-1] + pointStorage[i-1].fY);
254 for (int i = 1; i < glyphs.count; i++) {
255 pointStorage[i].set(glyphWidths[i-1] + pointStorage[i-1].fX, yBaseline);
259 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
260 mCanvas->drawText(glyphs.glyphIDs, &pointStorage[0].fX, glyphs.count, glyphs.paint,
261 x, y, bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
264 void SkiaCanvasProxy::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
265 const SkPaint& origPaint) {
266 // convert to glyphIDs if necessary
267 GlyphIDConverter glyphs(text, byteLength, origPaint);
269 // convert to relative positions if necessary
271 const SkPoint* posArray;
272 SkAutoSTMalloc<32, SkPoint> pointStorage;
273 if (mCanvas->drawTextAbsolutePos()) {
280 posArray = pointStorage.reset(glyphs.count);
281 for (int i = 0; i < glyphs.count; i++) {
282 pointStorage[i].fX = pos[i].fX- x;
283 pointStorage[i].fY = pos[i].fY- y;
287 // compute conservative bounds
288 // NOTE: We could call the faster paint.getFontBounds for a less accurate,
289 // but even more conservative bounds if this is too slow.
291 glyphs.paint.measureText(glyphs.glyphIDs, glyphs.count << 1, &bounds);
293 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
294 mCanvas->drawText(glyphs.glyphIDs, &posArray[0].fX, glyphs.count, glyphs.paint, x, y,
295 bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, 0);
298 void SkiaCanvasProxy::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
299 SkScalar constY, const SkPaint& paint) {
300 const size_t pointCount = byteLength >> 1;
301 SkAutoSTMalloc<32, SkPoint> storage(pointCount);
302 SkPoint* pts = storage.get();
303 for (size_t i = 0; i < pointCount; i++) {
304 pts[i].set(xpos[i], constY);
306 this->onDrawPosText(text, byteLength, pts, paint);
309 void SkiaCanvasProxy::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
310 const SkMatrix* matrix, const SkPaint& origPaint) {
311 // convert to glyphIDs if necessary
312 GlyphIDConverter glyphs(text, byteLength, origPaint);
313 mCanvas->drawTextOnPath(glyphs.glyphIDs, glyphs.count, path, 0, 0, glyphs.paint);
316 void SkiaCanvasProxy::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
317 const SkPaint& paint) {
318 SkDEBUGFAIL("SkiaCanvasProxy::onDrawTextBlob is not supported");
321 void SkiaCanvasProxy::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
322 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) {
323 if (mFilterHwuiCalls) {
326 SkPatchUtils::VertexData data;
329 mCanvas->getMatrix(&matrix);
330 SkISize lod = SkPatchUtils::GetLevelOfDetail(cubics, &matrix);
332 // It automatically adjusts lodX and lodY in case it exceeds the number of indices.
333 // If it fails to generate the vertices, then we do not draw.
334 if (SkPatchUtils::getVertexData(&data, cubics, colors, texCoords, lod.width(), lod.height())) {
335 this->drawVertices(SkCanvas::kTriangles_VertexMode, data.fVertexCount, data.fPoints,
336 data.fTexCoords, data.fColors, xmode, data.fIndices, data.fIndexCount,
341 void SkiaCanvasProxy::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle) {
342 mCanvas->clipRect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, op);
345 void SkiaCanvasProxy::onClipRRect(const SkRRect& roundRect, SkRegion::Op op, ClipEdgeStyle) {
347 path.addRRect(roundRect);
348 mCanvas->clipPath(&path, op);
351 void SkiaCanvasProxy::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle) {
352 mCanvas->clipPath(&path, op);
355 void SkiaCanvasProxy::onClipRegion(const SkRegion& region, SkRegion::Op op) {
356 mCanvas->clipRegion(®ion, op);
359 }; // namespace uirenderer
360 }; // namespace android