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