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