PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/hwui/SkiaCanvas.cpp

http://github.com/CyanogenMod/android_frameworks_base
C++ | 854 lines | 595 code | 151 blank | 108 comment | 46 complexity | fe1edad018be27994b1a933b5501af0a MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, CC0-1.0, BitTorrent-1.0, BSD-3-Clause
  1. /*
  2. * Copyright (C) 2014 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. #include "CanvasProperty.h"
  17. #include "Layer.h"
  18. #include "RenderNode.h"
  19. #include "hwui/Canvas.h"
  20. #include <SkCanvas.h>
  21. #include <SkClipStack.h>
  22. #include <SkDrawable.h>
  23. #include <SkDevice.h>
  24. #include <SkDeque.h>
  25. #include <SkDrawFilter.h>
  26. #include <SkGraphics.h>
  27. #include <SkImage.h>
  28. #include <SkShader.h>
  29. #include <SkTArray.h>
  30. #include <SkTLazy.h>
  31. #include <SkTemplates.h>
  32. #include "VectorDrawable.h"
  33. #include <memory>
  34. namespace android {
  35. // Holds an SkCanvas reference plus additional native data.
  36. class SkiaCanvas : public Canvas {
  37. public:
  38. explicit SkiaCanvas(const SkBitmap& bitmap);
  39. /**
  40. * Create a new SkiaCanvas.
  41. *
  42. * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
  43. * not be NULL. This constructor will ref() the SkCanvas, and unref()
  44. * it in its destructor.
  45. */
  46. explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
  47. SkASSERT(canvas);
  48. canvas->ref();
  49. }
  50. virtual SkCanvas* asSkCanvas() override {
  51. return mCanvas.get();
  52. }
  53. virtual void resetRecording(int width, int height) override {
  54. LOG_ALWAYS_FATAL("SkiaCanvas cannot be reset as a recording canvas");
  55. }
  56. virtual uirenderer::DisplayList* finishRecording() override {
  57. LOG_ALWAYS_FATAL("SkiaCanvas does not produce a DisplayList");
  58. return nullptr;
  59. }
  60. virtual void insertReorderBarrier(bool enableReorder) override {
  61. LOG_ALWAYS_FATAL("SkiaCanvas does not support reordering barriers");
  62. }
  63. virtual void setBitmap(const SkBitmap& bitmap) override;
  64. virtual bool isOpaque() override;
  65. virtual int width() override;
  66. virtual int height() override;
  67. virtual void setHighContrastText(bool highContrastText) override {
  68. mHighContrastText = highContrastText;
  69. }
  70. virtual bool isHighContrastText() override { return mHighContrastText; }
  71. virtual int getSaveCount() const override;
  72. virtual int save(SaveFlags::Flags flags) override;
  73. virtual void restore() override;
  74. virtual void restoreToCount(int saveCount) override;
  75. virtual int saveLayer(float left, float top, float right, float bottom,
  76. const SkPaint* paint, SaveFlags::Flags flags) override;
  77. virtual int saveLayerAlpha(float left, float top, float right, float bottom,
  78. int alpha, SaveFlags::Flags flags) override;
  79. virtual void getMatrix(SkMatrix* outMatrix) const override;
  80. virtual void setMatrix(const SkMatrix& matrix) override;
  81. virtual void concat(const SkMatrix& matrix) override;
  82. virtual void rotate(float degrees) override;
  83. virtual void scale(float sx, float sy) override;
  84. virtual void skew(float sx, float sy) override;
  85. virtual void translate(float dx, float dy) override;
  86. virtual bool getClipBounds(SkRect* outRect) const override;
  87. virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
  88. virtual bool quickRejectPath(const SkPath& path) const override;
  89. virtual bool clipRect(float left, float top, float right, float bottom,
  90. SkRegion::Op op) override;
  91. virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
  92. virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
  93. virtual SkDrawFilter* getDrawFilter() override;
  94. virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
  95. virtual void drawColor(int color, SkXfermode::Mode mode) override;
  96. virtual void drawPaint(const SkPaint& paint) override;
  97. virtual void drawPoint(float x, float y, const SkPaint& paint) override;
  98. virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
  99. virtual void drawLine(float startX, float startY, float stopX, float stopY,
  100. const SkPaint& paint) override;
  101. virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
  102. virtual void drawRect(float left, float top, float right, float bottom,
  103. const SkPaint& paint) override;
  104. virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
  105. virtual void drawRoundRect(float left, float top, float right, float bottom,
  106. float rx, float ry, const SkPaint& paint) override;
  107. virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
  108. virtual void drawOval(float left, float top, float right, float bottom,
  109. const SkPaint& paint) override;
  110. virtual void drawArc(float left, float top, float right, float bottom,
  111. float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
  112. virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
  113. virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
  114. const float* verts, const float* tex, const int* colors,
  115. const uint16_t* indices, int indexCount, const SkPaint& paint) override;
  116. virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
  117. const SkPaint* paint) override;
  118. virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
  119. const SkPaint* paint) override;
  120. virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
  121. float srcRight, float srcBottom, float dstLeft, float dstTop,
  122. float dstRight, float dstBottom, const SkPaint* paint) override;
  123. virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
  124. const float* vertices, const int* colors, const SkPaint* paint) override;
  125. virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
  126. float dstLeft, float dstTop, float dstRight, float dstBottom,
  127. const SkPaint* paint) override;
  128. virtual bool drawTextAbsolutePos() const override { return true; }
  129. virtual void drawVectorDrawable(VectorDrawableRoot* vectorDrawable) override;
  130. virtual void drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
  131. uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
  132. uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
  133. uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) override;
  134. virtual void drawCircle(uirenderer::CanvasPropertyPrimitive* x,
  135. uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius,
  136. uirenderer::CanvasPropertyPaint* paint) override;
  137. virtual void drawLayer(uirenderer::DeferredLayerUpdater* layerHandle) override;
  138. virtual void drawRenderNode(uirenderer::RenderNode* renderNode) override;
  139. virtual void callDrawGLFunction(Functor* functor,
  140. uirenderer::GlFunctorLifecycleListener* listener) override;
  141. protected:
  142. virtual void drawGlyphs(const uint16_t* text, const float* positions, int count,
  143. const SkPaint& paint, float x, float y,
  144. float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
  145. float totalAdvance) override;
  146. virtual void drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path,
  147. float hOffset, float vOffset, const SkPaint& paint) override;
  148. private:
  149. struct SaveRec {
  150. int saveCount;
  151. SaveFlags::Flags saveFlags;
  152. };
  153. bool mHighContrastText = false;
  154. void recordPartialSave(SaveFlags::Flags flags);
  155. void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
  156. void applyClips(const SkTArray<SkClipStack::Element>& clips);
  157. void drawPoints(const float* points, int count, const SkPaint& paint,
  158. SkCanvas::PointMode mode);
  159. SkAutoTUnref<SkCanvas> mCanvas;
  160. std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
  161. };
  162. Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
  163. return new SkiaCanvas(bitmap);
  164. }
  165. Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
  166. return new SkiaCanvas(skiaCanvas);
  167. }
  168. SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
  169. mCanvas.reset(new SkCanvas(bitmap));
  170. }
  171. // ----------------------------------------------------------------------------
  172. // Canvas state operations: Replace Bitmap
  173. // ----------------------------------------------------------------------------
  174. class ClipCopier : public SkCanvas::ClipVisitor {
  175. public:
  176. ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
  177. virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
  178. m_dstCanvas->clipRect(rect, op, antialias);
  179. }
  180. virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
  181. m_dstCanvas->clipRRect(rrect, op, antialias);
  182. }
  183. virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
  184. m_dstCanvas->clipPath(path, op, antialias);
  185. }
  186. private:
  187. SkCanvas* m_dstCanvas;
  188. };
  189. void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
  190. SkCanvas* newCanvas = new SkCanvas(bitmap);
  191. if (!bitmap.isNull()) {
  192. // Copy the canvas matrix & clip state.
  193. newCanvas->setMatrix(mCanvas->getTotalMatrix());
  194. ClipCopier copier(newCanvas);
  195. mCanvas->replayClips(&copier);
  196. }
  197. // unrefs the existing canvas
  198. mCanvas.reset(newCanvas);
  199. // clean up the old save stack
  200. mSaveStack.reset(NULL);
  201. }
  202. // ----------------------------------------------------------------------------
  203. // Canvas state operations
  204. // ----------------------------------------------------------------------------
  205. bool SkiaCanvas::isOpaque() {
  206. return mCanvas->imageInfo().isOpaque();
  207. }
  208. int SkiaCanvas::width() {
  209. return mCanvas->imageInfo().width();
  210. }
  211. int SkiaCanvas::height() {
  212. return mCanvas->imageInfo().height();
  213. }
  214. // ----------------------------------------------------------------------------
  215. // Canvas state operations: Save (layer)
  216. // ----------------------------------------------------------------------------
  217. int SkiaCanvas::getSaveCount() const {
  218. return mCanvas->getSaveCount();
  219. }
  220. int SkiaCanvas::save(SaveFlags::Flags flags) {
  221. int count = mCanvas->save();
  222. recordPartialSave(flags);
  223. return count;
  224. }
  225. // The SkiaCanvas::restore operation layers on the capability to preserve
  226. // either (or both) the matrix and/or clip state after a SkCanvas::restore
  227. // operation. It does this by explicitly saving off the clip & matrix state
  228. // when requested and playing it back after the SkCanvas::restore.
  229. void SkiaCanvas::restore() {
  230. const SaveRec* rec = (NULL == mSaveStack.get())
  231. ? NULL
  232. : static_cast<SaveRec*>(mSaveStack->back());
  233. int currentSaveCount = mCanvas->getSaveCount();
  234. SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
  235. if (NULL == rec || rec->saveCount != currentSaveCount) {
  236. // Fast path - no record for this frame.
  237. mCanvas->restore();
  238. return;
  239. }
  240. bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
  241. bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
  242. SkMatrix savedMatrix;
  243. if (preserveMatrix) {
  244. savedMatrix = mCanvas->getTotalMatrix();
  245. }
  246. SkTArray<SkClipStack::Element> savedClips;
  247. int topClipStackFrame = mCanvas->getClipStack()->getSaveCount();
  248. if (preserveClip) {
  249. saveClipsForFrame(savedClips, topClipStackFrame);
  250. }
  251. mCanvas->restore();
  252. if (preserveMatrix) {
  253. mCanvas->setMatrix(savedMatrix);
  254. }
  255. if (preserveClip && !savedClips.empty() &&
  256. topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) {
  257. // Only reapply the saved clips if the top clip stack frame was actually
  258. // popped by restore(). If it wasn't, it means it doesn't belong to the
  259. // restored canvas frame (SkCanvas lazy save/restore kicked in).
  260. applyClips(savedClips);
  261. }
  262. mSaveStack->pop_back();
  263. }
  264. void SkiaCanvas::restoreToCount(int restoreCount) {
  265. while (mCanvas->getSaveCount() > restoreCount) {
  266. this->restore();
  267. }
  268. }
  269. static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
  270. SkCanvas::SaveLayerFlags layerFlags = 0;
  271. // We intentionally ignore the SaveFlags::HasAlphaLayer and
  272. // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
  273. // and our Android client may use it incorrectly.
  274. // In Skia, this flag is purely for performance optimization.
  275. if (!(flags & SaveFlags::ClipToLayer)) {
  276. layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
  277. }
  278. return layerFlags;
  279. }
  280. int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
  281. const SkPaint* paint, SaveFlags::Flags flags) {
  282. const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
  283. const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
  284. int count = mCanvas->saveLayer(rec);
  285. recordPartialSave(flags);
  286. return count;
  287. }
  288. int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
  289. int alpha, SaveFlags::Flags flags) {
  290. SkTLazy<SkPaint> alphaPaint;
  291. if (static_cast<unsigned>(alpha) < 0xFF) {
  292. alphaPaint.init()->setAlpha(alpha);
  293. }
  294. return this->saveLayer(left, top, right, bottom, alphaPaint.getMaybeNull(),
  295. flags);
  296. }
  297. // ----------------------------------------------------------------------------
  298. // functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
  299. // ----------------------------------------------------------------------------
  300. void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
  301. // A partial save is a save operation which doesn't capture the full canvas state.
  302. // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
  303. // Mask-out non canvas state bits.
  304. flags &= SaveFlags::MatrixClip;
  305. if (flags == SaveFlags::MatrixClip) {
  306. // not a partial save.
  307. return;
  308. }
  309. if (NULL == mSaveStack.get()) {
  310. mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
  311. }
  312. SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
  313. rec->saveCount = mCanvas->getSaveCount();
  314. rec->saveFlags = flags;
  315. }
  316. void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips,
  317. int saveCountToBackup) {
  318. // Each SkClipStack::Element stores the index of the canvas save
  319. // with which it is associated. Backup only those Elements that
  320. // are associated with 'saveCountToBackup'
  321. SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
  322. SkClipStack::Iter::kTop_IterStart);
  323. while (const SkClipStack::Element* elem = clipIterator.prev()) {
  324. if (elem->getSaveCount() < saveCountToBackup) {
  325. // done with the target save count.
  326. break;
  327. }
  328. SkASSERT(elem->getSaveCount() == saveCountToBackup);
  329. clips.push_back(*elem);
  330. }
  331. }
  332. void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
  333. ClipCopier clipCopier(mCanvas);
  334. // The clip stack stores clips in device space.
  335. SkMatrix origMatrix = mCanvas->getTotalMatrix();
  336. mCanvas->resetMatrix();
  337. // We pushed the clips in reverse order.
  338. for (int i = clips.count() - 1; i >= 0; --i) {
  339. clips[i].replay(&clipCopier);
  340. }
  341. mCanvas->setMatrix(origMatrix);
  342. }
  343. // ----------------------------------------------------------------------------
  344. // Canvas state operations: Matrix
  345. // ----------------------------------------------------------------------------
  346. void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
  347. *outMatrix = mCanvas->getTotalMatrix();
  348. }
  349. void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
  350. mCanvas->setMatrix(matrix);
  351. }
  352. void SkiaCanvas::concat(const SkMatrix& matrix) {
  353. mCanvas->concat(matrix);
  354. }
  355. void SkiaCanvas::rotate(float degrees) {
  356. mCanvas->rotate(degrees);
  357. }
  358. void SkiaCanvas::scale(float sx, float sy) {
  359. mCanvas->scale(sx, sy);
  360. }
  361. void SkiaCanvas::skew(float sx, float sy) {
  362. mCanvas->skew(sx, sy);
  363. }
  364. void SkiaCanvas::translate(float dx, float dy) {
  365. mCanvas->translate(dx, dy);
  366. }
  367. // ----------------------------------------------------------------------------
  368. // Canvas state operations: Clips
  369. // ----------------------------------------------------------------------------
  370. // This function is a mirror of SkCanvas::getClipBounds except that it does
  371. // not outset the edge of the clip to account for anti-aliasing. There is
  372. // a skia bug to investigate pushing this logic into back into skia.
  373. // (see https://code.google.com/p/skia/issues/detail?id=1303)
  374. bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
  375. SkIRect ibounds;
  376. if (!mCanvas->getClipDeviceBounds(&ibounds)) {
  377. return false;
  378. }
  379. SkMatrix inverse;
  380. // if we can't invert the CTM, we can't return local clip bounds
  381. if (!mCanvas->getTotalMatrix().invert(&inverse)) {
  382. if (outRect) {
  383. outRect->setEmpty();
  384. }
  385. return false;
  386. }
  387. if (NULL != outRect) {
  388. SkRect r = SkRect::Make(ibounds);
  389. inverse.mapRect(outRect, r);
  390. }
  391. return true;
  392. }
  393. bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
  394. SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
  395. return mCanvas->quickReject(bounds);
  396. }
  397. bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
  398. return mCanvas->quickReject(path);
  399. }
  400. bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
  401. SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
  402. mCanvas->clipRect(rect, op);
  403. return !mCanvas->isClipEmpty();
  404. }
  405. bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
  406. mCanvas->clipPath(*path, op);
  407. return !mCanvas->isClipEmpty();
  408. }
  409. bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
  410. SkPath rgnPath;
  411. if (region->getBoundaryPath(&rgnPath)) {
  412. // The region is specified in device space.
  413. SkMatrix savedMatrix = mCanvas->getTotalMatrix();
  414. mCanvas->resetMatrix();
  415. mCanvas->clipPath(rgnPath, op);
  416. mCanvas->setMatrix(savedMatrix);
  417. } else {
  418. mCanvas->clipRect(SkRect::MakeEmpty(), op);
  419. }
  420. return !mCanvas->isClipEmpty();
  421. }
  422. // ----------------------------------------------------------------------------
  423. // Canvas state operations: Filters
  424. // ----------------------------------------------------------------------------
  425. SkDrawFilter* SkiaCanvas::getDrawFilter() {
  426. return mCanvas->getDrawFilter();
  427. }
  428. void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
  429. mCanvas->setDrawFilter(drawFilter);
  430. }
  431. // ----------------------------------------------------------------------------
  432. // Canvas draw operations
  433. // ----------------------------------------------------------------------------
  434. void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
  435. mCanvas->drawColor(color, mode);
  436. }
  437. void SkiaCanvas::drawPaint(const SkPaint& paint) {
  438. mCanvas->drawPaint(paint);
  439. }
  440. // ----------------------------------------------------------------------------
  441. // Canvas draw operations: Geometry
  442. // ----------------------------------------------------------------------------
  443. void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
  444. SkCanvas::PointMode mode) {
  445. // convert the floats into SkPoints
  446. count >>= 1; // now it is the number of points
  447. std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
  448. for (int i = 0; i < count; i++) {
  449. pts[i].set(points[0], points[1]);
  450. points += 2;
  451. }
  452. mCanvas->drawPoints(mode, count, pts.get(), paint);
  453. }
  454. void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
  455. mCanvas->drawPoint(x, y, paint);
  456. }
  457. void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
  458. this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
  459. }
  460. void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
  461. const SkPaint& paint) {
  462. mCanvas->drawLine(startX, startY, stopX, stopY, paint);
  463. }
  464. void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
  465. this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
  466. }
  467. void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
  468. const SkPaint& paint) {
  469. mCanvas->drawRectCoords(left, top, right, bottom, paint);
  470. }
  471. void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
  472. SkRegion::Iterator it(region);
  473. while (!it.done()) {
  474. mCanvas->drawRect(SkRect::Make(it.rect()), paint);
  475. it.next();
  476. }
  477. }
  478. void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
  479. float rx, float ry, const SkPaint& paint) {
  480. SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
  481. mCanvas->drawRoundRect(rect, rx, ry, paint);
  482. }
  483. void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
  484. mCanvas->drawCircle(x, y, radius, paint);
  485. }
  486. void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
  487. SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
  488. mCanvas->drawOval(oval, paint);
  489. }
  490. void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
  491. float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
  492. SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
  493. mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
  494. }
  495. void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
  496. mCanvas->drawPath(path, paint);
  497. }
  498. void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
  499. const float* verts, const float* texs, const int* colors,
  500. const uint16_t* indices, int indexCount, const SkPaint& paint) {
  501. #ifndef SK_SCALAR_IS_FLOAT
  502. SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
  503. #endif
  504. const int ptCount = vertexCount >> 1;
  505. mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
  506. (SkColor*)colors, NULL, indices, indexCount, paint);
  507. }
  508. // ----------------------------------------------------------------------------
  509. // Canvas draw operations: Bitmaps
  510. // ----------------------------------------------------------------------------
  511. void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
  512. mCanvas->drawBitmap(bitmap, left, top, paint);
  513. }
  514. void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
  515. SkAutoCanvasRestore acr(mCanvas, true);
  516. mCanvas->concat(matrix);
  517. mCanvas->drawBitmap(bitmap, 0, 0, paint);
  518. }
  519. void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
  520. float srcRight, float srcBottom, float dstLeft, float dstTop,
  521. float dstRight, float dstBottom, const SkPaint* paint) {
  522. SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
  523. SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
  524. mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
  525. }
  526. void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
  527. const float* vertices, const int* colors, const SkPaint* paint) {
  528. const int ptCount = (meshWidth + 1) * (meshHeight + 1);
  529. const int indexCount = meshWidth * meshHeight * 6;
  530. /* Our temp storage holds 2 or 3 arrays.
  531. texture points [ptCount * sizeof(SkPoint)]
  532. optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
  533. copy to convert from float to fixed
  534. indices [ptCount * sizeof(uint16_t)]
  535. */
  536. ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
  537. storageSize += indexCount * sizeof(uint16_t); // indices[]
  538. #ifndef SK_SCALAR_IS_FLOAT
  539. SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
  540. #endif
  541. std::unique_ptr<char[]> storage(new char[storageSize]);
  542. SkPoint* texs = (SkPoint*)storage.get();
  543. uint16_t* indices = (uint16_t*)(texs + ptCount);
  544. // cons up texture coordinates and indices
  545. {
  546. const SkScalar w = SkIntToScalar(bitmap.width());
  547. const SkScalar h = SkIntToScalar(bitmap.height());
  548. const SkScalar dx = w / meshWidth;
  549. const SkScalar dy = h / meshHeight;
  550. SkPoint* texsPtr = texs;
  551. SkScalar y = 0;
  552. for (int i = 0; i <= meshHeight; i++) {
  553. if (i == meshHeight) {
  554. y = h; // to ensure numerically we hit h exactly
  555. }
  556. SkScalar x = 0;
  557. for (int j = 0; j < meshWidth; j++) {
  558. texsPtr->set(x, y);
  559. texsPtr += 1;
  560. x += dx;
  561. }
  562. texsPtr->set(w, y);
  563. texsPtr += 1;
  564. y += dy;
  565. }
  566. SkASSERT(texsPtr - texs == ptCount);
  567. }
  568. // cons up indices
  569. {
  570. uint16_t* indexPtr = indices;
  571. int index = 0;
  572. for (int i = 0; i < meshHeight; i++) {
  573. for (int j = 0; j < meshWidth; j++) {
  574. // lower-left triangle
  575. *indexPtr++ = index;
  576. *indexPtr++ = index + meshWidth + 1;
  577. *indexPtr++ = index + meshWidth + 2;
  578. // upper-right triangle
  579. *indexPtr++ = index;
  580. *indexPtr++ = index + meshWidth + 2;
  581. *indexPtr++ = index + 1;
  582. // bump to the next cell
  583. index += 1;
  584. }
  585. // bump to the next row
  586. index += 1;
  587. }
  588. SkASSERT(indexPtr - indices == indexCount);
  589. SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
  590. }
  591. // double-check that we have legal indices
  592. #ifdef SK_DEBUG
  593. {
  594. for (int i = 0; i < indexCount; i++) {
  595. SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
  596. }
  597. }
  598. #endif
  599. // cons-up a shader for the bitmap
  600. SkPaint tmpPaint;
  601. if (paint) {
  602. tmpPaint = *paint;
  603. }
  604. SkShader* shader = SkShader::CreateBitmapShader(bitmap,
  605. SkShader::kClamp_TileMode,
  606. SkShader::kClamp_TileMode);
  607. SkSafeUnref(tmpPaint.setShader(shader));
  608. mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
  609. texs, (const SkColor*)colors, NULL, indices,
  610. indexCount, tmpPaint);
  611. }
  612. void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
  613. float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
  614. SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
  615. NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
  616. }
  617. void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
  618. vectorDrawable->drawStaging(this);
  619. }
  620. // ----------------------------------------------------------------------------
  621. // Canvas draw operations: Text
  622. // ----------------------------------------------------------------------------
  623. void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
  624. const SkPaint& paint, float x, float y,
  625. float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
  626. float totalAdvance) {
  627. static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
  628. mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paint);
  629. drawTextDecorations(x, y, totalAdvance, paint);
  630. }
  631. void SkiaCanvas::drawGlyphsOnPath(const uint16_t* glyphs, int count, const SkPath& path,
  632. float hOffset, float vOffset, const SkPaint& paint) {
  633. mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
  634. }
  635. // ----------------------------------------------------------------------------
  636. // Canvas draw operations: Animations
  637. // ----------------------------------------------------------------------------
  638. class AnimatedRoundRect : public SkDrawable {
  639. public:
  640. AnimatedRoundRect(uirenderer::CanvasPropertyPrimitive* left,
  641. uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
  642. uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
  643. uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* p) :
  644. mLeft(left), mTop(top), mRight(right), mBottom(bottom), mRx(rx), mRy(ry), mPaint(p) {}
  645. protected:
  646. virtual SkRect onGetBounds() override {
  647. return SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
  648. }
  649. virtual void onDraw(SkCanvas* canvas) override {
  650. SkRect rect = SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
  651. canvas->drawRoundRect(rect, mRx->value, mRy->value, mPaint->value);
  652. }
  653. private:
  654. sp<uirenderer::CanvasPropertyPrimitive> mLeft;
  655. sp<uirenderer::CanvasPropertyPrimitive> mTop;
  656. sp<uirenderer::CanvasPropertyPrimitive> mRight;
  657. sp<uirenderer::CanvasPropertyPrimitive> mBottom;
  658. sp<uirenderer::CanvasPropertyPrimitive> mRx;
  659. sp<uirenderer::CanvasPropertyPrimitive> mRy;
  660. sp<uirenderer::CanvasPropertyPaint> mPaint;
  661. };
  662. class AnimatedCircle : public SkDrawable {
  663. public:
  664. AnimatedCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
  665. uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) :
  666. mX(x), mY(y), mRadius(radius), mPaint(paint) {}
  667. protected:
  668. virtual SkRect onGetBounds() override {
  669. const float x = mX->value;
  670. const float y = mY->value;
  671. const float radius = mRadius->value;
  672. return SkRect::MakeLTRB(x - radius, y - radius, x + radius, y + radius);
  673. }
  674. virtual void onDraw(SkCanvas* canvas) override {
  675. canvas->drawCircle(mX->value, mY->value, mRadius->value, mPaint->value);
  676. }
  677. private:
  678. sp<uirenderer::CanvasPropertyPrimitive> mX;
  679. sp<uirenderer::CanvasPropertyPrimitive> mY;
  680. sp<uirenderer::CanvasPropertyPrimitive> mRadius;
  681. sp<uirenderer::CanvasPropertyPaint> mPaint;
  682. };
  683. void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
  684. uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
  685. uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
  686. uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
  687. SkAutoTUnref<AnimatedRoundRect> drawable(
  688. new AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
  689. mCanvas->drawDrawable(drawable.get());
  690. }
  691. void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
  692. uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
  693. SkAutoTUnref<AnimatedCircle> drawable(new AnimatedCircle(x, y, radius, paint));
  694. mCanvas->drawDrawable(drawable.get());
  695. }
  696. // ----------------------------------------------------------------------------
  697. // Canvas draw operations: View System
  698. // ----------------------------------------------------------------------------
  699. void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layer) { }
  700. void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { }
  701. void SkiaCanvas::callDrawGLFunction(Functor* functor,
  702. uirenderer::GlFunctorLifecycleListener* listener) { }
  703. } // namespace android