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