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