Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "RecordingCanvas.h" |
| 18 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 19 | #include "DeferredLayerUpdater.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 20 | #include "RecordedOp.h" |
| 21 | #include "RenderNode.h" |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 22 | #include "VectorDrawable.h" |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 23 | #include "hwui/MinikinUtils.h" |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | RecordingCanvas::RecordingCanvas(size_t width, size_t height) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | : mState(*this), mResourceCache(ResourceCache::getInstance()) { |
Derek Sollenberger | 6f48556 | 2015-07-30 10:00:39 -0400 | [diff] [blame] | 30 | resetRecording(width, height); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | RecordingCanvas::~RecordingCanvas() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | LOG_ALWAYS_FATAL_IF(mDisplayList, "Destroyed a RecordingCanvas during a record!"); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Stan Iliev | c0e7a90 | 2016-10-13 17:07:09 -0400 | [diff] [blame] | 37 | void RecordingCanvas::resetRecording(int width, int height, RenderNode* node) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | LOG_ALWAYS_FATAL_IF(mDisplayList, "prepareDirty called a second time during a recording!"); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 39 | mDisplayList = new DisplayList(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 40 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 41 | mState.initializeRecordingSaveStack(width, height); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 42 | |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 43 | mDeferredBarrierType = DeferredBarrierType::InOrder; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 46 | DisplayList* RecordingCanvas::finishRecording() { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 47 | restoreToCount(1); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 48 | mPaintMap.clear(); |
| 49 | mRegionMap.clear(); |
| 50 | mPathMap.clear(); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 51 | DisplayList* displayList = mDisplayList; |
| 52 | mDisplayList = nullptr; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 53 | mSkiaCanvasProxy.reset(nullptr); |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 54 | return displayList; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 57 | void RecordingCanvas::insertReorderBarrier(bool enableReorder) { |
| 58 | if (enableReorder) { |
| 59 | mDeferredBarrierType = DeferredBarrierType::OutOfOrder; |
| 60 | mDeferredBarrierClip = getRecordedClip(); |
| 61 | } else { |
| 62 | mDeferredBarrierType = DeferredBarrierType::InOrder; |
| 63 | mDeferredBarrierClip = nullptr; |
| 64 | } |
| 65 | } |
| 66 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 67 | SkCanvas* RecordingCanvas::asSkCanvas() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 68 | LOG_ALWAYS_FATAL_IF(!mDisplayList, "attempting to get an SkCanvas when we are not recording!"); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 69 | if (!mSkiaCanvasProxy) { |
| 70 | mSkiaCanvasProxy.reset(new SkiaCanvasProxy(this)); |
| 71 | } |
| 72 | |
| 73 | // SkCanvas instances default to identity transform, but should inherit |
| 74 | // the state of this Canvas; if this code was in the SkiaCanvasProxy |
| 75 | // constructor, we couldn't cache mSkiaCanvasProxy. |
| 76 | SkMatrix parentTransform; |
| 77 | getMatrix(&parentTransform); |
| 78 | mSkiaCanvasProxy.get()->setMatrix(parentTransform); |
| 79 | |
| 80 | return mSkiaCanvasProxy.get(); |
| 81 | } |
| 82 | |
| 83 | // ---------------------------------------------------------------------------- |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 84 | // CanvasStateClient implementation |
| 85 | // ---------------------------------------------------------------------------- |
| 86 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | void RecordingCanvas::onViewportInitialized() {} |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 88 | |
| 89 | void RecordingCanvas::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) { |
| 90 | if (removed.flags & Snapshot::kFlagIsFboLayer) { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 91 | addOp(alloc().create_trivial<EndLayerOp>()); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 92 | } else if (removed.flags & Snapshot::kFlagIsLayer) { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 93 | addOp(alloc().create_trivial<EndUnclippedLayerOp>()); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | // ---------------------------------------------------------------------------- |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 98 | // android/graphics/Canvas state operations |
| 99 | // ---------------------------------------------------------------------------- |
| 100 | // Save (layer) |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 101 | int RecordingCanvas::save(SaveFlags::Flags flags) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | return mState.save((int)flags); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void RecordingCanvas::RecordingCanvas::restore() { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 106 | mState.restore(); |
| 107 | } |
| 108 | |
| 109 | void RecordingCanvas::restoreToCount(int saveCount) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 110 | mState.restoreToCount(saveCount); |
| 111 | } |
| 112 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 113 | int RecordingCanvas::saveLayer(float left, float top, float right, float bottom, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 114 | const SkPaint* paint, SaveFlags::Flags flags) { |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 115 | // force matrix/clip isolation for layer |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 116 | flags |= SaveFlags::MatrixClip; |
| 117 | bool clippedLayer = flags & SaveFlags::ClipToLayer; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 118 | |
| 119 | const Snapshot& previous = *mState.currentSnapshot(); |
| 120 | |
| 121 | // initialize the snapshot as though it almost represents an FBO layer so deferred draw |
| 122 | // operations will be able to store and restore the current clip and transform info, and |
| 123 | // quick rejection will be correct (for display lists) |
| 124 | |
Chris Craik | e4f6d96 | 2016-06-27 16:30:27 -0700 | [diff] [blame] | 125 | Rect unmappedBounds(left, top, right, bottom); |
| 126 | unmappedBounds.roundOut(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 127 | |
| 128 | // determine clipped bounds relative to previous viewport. |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 129 | Rect visibleBounds = unmappedBounds; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 130 | previous.transform->mapRect(visibleBounds); |
| 131 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | if (CC_UNLIKELY(!clippedLayer && previous.transform->rectToRect() && |
| 133 | visibleBounds.contains(previous.getRenderTargetClip()))) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 134 | // unlikely case where an unclipped savelayer is recorded with a clip it can use, |
| 135 | // as none of its unaffected/unclipped area is visible |
| 136 | clippedLayer = true; |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 137 | flags |= SaveFlags::ClipToLayer; |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 138 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 139 | |
| 140 | visibleBounds.doIntersect(previous.getRenderTargetClip()); |
| 141 | visibleBounds.snapToPixelBoundaries(); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 142 | visibleBounds.doIntersect(Rect(previous.getViewportWidth(), previous.getViewportHeight())); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 143 | |
| 144 | // Map visible bounds back to layer space, and intersect with parameter bounds |
| 145 | Rect layerBounds = visibleBounds; |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 146 | if (CC_LIKELY(!layerBounds.isEmpty())) { |
| 147 | // if non-empty, can safely map by the inverse transform |
| 148 | Matrix4 inverse; |
| 149 | inverse.loadInverse(*previous.transform); |
| 150 | inverse.mapRect(layerBounds); |
| 151 | layerBounds.doIntersect(unmappedBounds); |
| 152 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 153 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 154 | int saveValue = mState.save((int)flags); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 155 | Snapshot& snapshot = *mState.writableSnapshot(); |
| 156 | |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 157 | // layerBounds is in original bounds space, but clipped by current recording clip |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 158 | if (!layerBounds.isEmpty() && !unmappedBounds.isEmpty()) { |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 159 | if (CC_LIKELY(clippedLayer)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | auto previousClip = getRecordedClip(); // capture before new snapshot clip has changed |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 161 | if (addOp(alloc().create_trivial<BeginLayerOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 162 | unmappedBounds, |
| 163 | *previous.transform, // transform to *draw* with |
| 164 | previousClip, // clip to *draw* with |
| 165 | refPaint(paint))) >= 0) { |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 166 | snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer; |
| 167 | snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight()); |
| 168 | snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f); |
| 169 | |
| 170 | Rect clip = layerBounds; |
| 171 | clip.translate(-unmappedBounds.left, -unmappedBounds.top); |
| 172 | snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 173 | snapshot.roundRectClipState = nullptr; |
| 174 | return saveValue; |
| 175 | } |
| 176 | } else { |
| 177 | if (addOp(alloc().create_trivial<BeginUnclippedLayerOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 178 | unmappedBounds, *mState.currentSnapshot()->transform, getRecordedClip(), |
| 179 | refPaint(paint))) >= 0) { |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 180 | snapshot.flags |= Snapshot::kFlagIsLayer; |
| 181 | return saveValue; |
| 182 | } |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 183 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 186 | // Layer not needed, so skip recording it... |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 187 | if (CC_LIKELY(clippedLayer)) { |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 188 | // ... and set empty clip to reject inner content, if possible |
| 189 | snapshot.resetClip(0, 0, 0, 0); |
Chris Craik | b87eadd | 2016-01-06 09:16:05 -0800 | [diff] [blame] | 190 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 191 | return saveValue; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // Matrix |
| 195 | void RecordingCanvas::rotate(float degrees) { |
| 196 | if (degrees == 0) return; |
| 197 | |
| 198 | mState.rotate(degrees); |
| 199 | } |
| 200 | |
| 201 | void RecordingCanvas::scale(float sx, float sy) { |
| 202 | if (sx == 1 && sy == 1) return; |
| 203 | |
| 204 | mState.scale(sx, sy); |
| 205 | } |
| 206 | |
| 207 | void RecordingCanvas::skew(float sx, float sy) { |
| 208 | mState.skew(sx, sy); |
| 209 | } |
| 210 | |
| 211 | void RecordingCanvas::translate(float dx, float dy) { |
| 212 | if (dx == 0 && dy == 0) return; |
| 213 | |
| 214 | mState.translate(dx, dy, 0); |
| 215 | } |
| 216 | |
| 217 | // Clip |
| 218 | bool RecordingCanvas::getClipBounds(SkRect* outRect) const { |
Chris Craik | e29ce6f | 2015-12-10 16:25:13 -0800 | [diff] [blame] | 219 | *outRect = mState.getLocalClipBounds().toSkRect(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 220 | return !(outRect->isEmpty()); |
| 221 | } |
| 222 | bool RecordingCanvas::quickRejectRect(float left, float top, float right, float bottom) const { |
| 223 | return mState.quickRejectConservative(left, top, right, bottom); |
| 224 | } |
| 225 | bool RecordingCanvas::quickRejectPath(const SkPath& path) const { |
| 226 | SkRect bounds = path.getBounds(); |
| 227 | return mState.quickRejectConservative(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom); |
| 228 | } |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 229 | bool RecordingCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 230 | return mState.clipRect(left, top, right, bottom, op); |
| 231 | } |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 232 | bool RecordingCanvas::clipPath(const SkPath* path, SkClipOp op) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 233 | return mState.clipPath(path, op); |
| 234 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 235 | |
| 236 | // ---------------------------------------------------------------------------- |
| 237 | // android/graphics/Canvas draw operations |
| 238 | // ---------------------------------------------------------------------------- |
Mike Reed | 260ab72 | 2016-10-07 15:59:20 -0400 | [diff] [blame] | 239 | void RecordingCanvas::drawColor(int color, SkBlendMode mode) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 240 | addOp(alloc().create_trivial<ColorOp>(getRecordedClip(), color, mode)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void RecordingCanvas::drawPaint(const SkPaint& paint) { |
Chris Craik | 4c3980b | 2016-03-15 14:20:18 -0700 | [diff] [blame] | 244 | SkRect bounds; |
| 245 | if (getClipBounds(&bounds)) { |
| 246 | drawRect(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, paint); |
| 247 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 250 | static Rect calcBoundsOfPoints(const float* points, int floatCount) { |
| 251 | Rect unmappedBounds(points[0], points[1], points[0], points[1]); |
| 252 | for (int i = 2; i < floatCount; i += 2) { |
| 253 | unmappedBounds.expandToCover(points[i], points[i + 1]); |
| 254 | } |
| 255 | return unmappedBounds; |
| 256 | } |
| 257 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 258 | // Geometry |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 259 | void RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 260 | if (CC_UNLIKELY(floatCount < 2 || paint.nothingToDraw())) return; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 261 | floatCount &= ~0x1; // round down to nearest two |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 262 | |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 263 | addOp(alloc().create_trivial<PointsOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 264 | calcBoundsOfPoints(points, floatCount), *mState.currentSnapshot()->transform, |
| 265 | getRecordedClip(), refPaint(&paint), refBuffer<float>(points, floatCount), floatCount)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 266 | } |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 267 | |
| 268 | void RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 269 | if (CC_UNLIKELY(floatCount < 4 || paint.nothingToDraw())) return; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 270 | floatCount &= ~0x3; // round down to nearest four |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 271 | |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 272 | addOp(alloc().create_trivial<LinesOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | calcBoundsOfPoints(points, floatCount), *mState.currentSnapshot()->transform, |
| 274 | getRecordedClip(), refPaint(&paint), refBuffer<float>(points, floatCount), floatCount)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 275 | } |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 276 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 277 | void RecordingCanvas::drawRect(float left, float top, float right, float bottom, |
| 278 | const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 279 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 280 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 281 | addOp(alloc().create_trivial<RectOp>(Rect(left, top, right, bottom), |
| 282 | *(mState.currentSnapshot()->transform), getRecordedClip(), |
| 283 | refPaint(&paint))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void RecordingCanvas::drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint) { |
| 287 | if (rects == nullptr) return; |
| 288 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 289 | Vertex* rectData = (Vertex*)mDisplayList->allocator.create_trivial_array<Vertex>(vertexCount); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 290 | Vertex* vertex = rectData; |
| 291 | |
| 292 | float left = FLT_MAX; |
| 293 | float top = FLT_MAX; |
| 294 | float right = FLT_MIN; |
| 295 | float bottom = FLT_MIN; |
| 296 | for (int index = 0; index < vertexCount; index += 4) { |
| 297 | float l = rects[index + 0]; |
| 298 | float t = rects[index + 1]; |
| 299 | float r = rects[index + 2]; |
| 300 | float b = rects[index + 3]; |
| 301 | |
| 302 | Vertex::set(vertex++, l, t); |
| 303 | Vertex::set(vertex++, r, t); |
| 304 | Vertex::set(vertex++, l, b); |
| 305 | Vertex::set(vertex++, r, b); |
| 306 | |
| 307 | left = std::min(left, l); |
| 308 | top = std::min(top, t); |
| 309 | right = std::max(right, r); |
| 310 | bottom = std::max(bottom, b); |
| 311 | } |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 312 | addOp(alloc().create_trivial<SimpleRectsOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 313 | Rect(left, top, right, bottom), *(mState.currentSnapshot()->transform), |
| 314 | getRecordedClip(), refPaint(paint), rectData, vertexCount)); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 318 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 319 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 320 | if (paint.getStyle() == SkPaint::kFill_Style && |
| 321 | (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 322 | int count = 0; |
| 323 | Vector<float> rects; |
| 324 | SkRegion::Iterator it(region); |
| 325 | while (!it.done()) { |
| 326 | const SkIRect& r = it.rect(); |
| 327 | rects.push(r.fLeft); |
| 328 | rects.push(r.fTop); |
| 329 | rects.push(r.fRight); |
| 330 | rects.push(r.fBottom); |
| 331 | count += 4; |
| 332 | it.next(); |
| 333 | } |
| 334 | drawSimpleRects(rects.array(), count, &paint); |
| 335 | } else { |
| 336 | SkRegion::Iterator it(region); |
| 337 | while (!it.done()) { |
| 338 | const SkIRect& r = it.rect(); |
| 339 | drawRect(r.fLeft, r.fTop, r.fRight, r.fBottom, paint); |
| 340 | it.next(); |
| 341 | } |
| 342 | } |
| 343 | } |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 344 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 345 | void RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, |
| 346 | float ry, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 347 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 348 | |
Chris Craik | 2dbb4c4 | 2016-03-11 18:58:37 -0800 | [diff] [blame] | 349 | if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 350 | addOp(alloc().create_trivial<RoundRectOp>(Rect(left, top, right, bottom), |
| 351 | *(mState.currentSnapshot()->transform), |
| 352 | getRecordedClip(), refPaint(&paint), rx, ry)); |
Chris Craik | 2dbb4c4 | 2016-03-11 18:58:37 -0800 | [diff] [blame] | 353 | } else { |
| 354 | drawRect(left, top, right, bottom, paint); |
| 355 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 356 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 357 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 358 | void RecordingCanvas::drawRoundRect(CanvasPropertyPrimitive* left, CanvasPropertyPrimitive* top, |
| 359 | CanvasPropertyPrimitive* right, CanvasPropertyPrimitive* bottom, |
| 360 | CanvasPropertyPrimitive* rx, CanvasPropertyPrimitive* ry, |
| 361 | CanvasPropertyPaint* paint) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 362 | mDisplayList->ref(left); |
| 363 | mDisplayList->ref(top); |
| 364 | mDisplayList->ref(right); |
| 365 | mDisplayList->ref(bottom); |
| 366 | mDisplayList->ref(rx); |
| 367 | mDisplayList->ref(ry); |
| 368 | mDisplayList->ref(paint); |
| 369 | refBitmapsInShader(paint->value.getShader()); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 370 | addOp(alloc().create_trivial<RoundRectPropsOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 371 | *(mState.currentSnapshot()->transform), getRecordedClip(), &paint->value, &left->value, |
| 372 | &top->value, &right->value, &bottom->value, &rx->value, &ry->value)); |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 375 | void RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 376 | // TODO: move to Canvas.h |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 377 | if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 378 | |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 379 | drawOval(x - radius, y - radius, x + radius, y + radius, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 380 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 381 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 382 | void RecordingCanvas::drawCircle(CanvasPropertyPrimitive* x, CanvasPropertyPrimitive* y, |
| 383 | CanvasPropertyPrimitive* radius, CanvasPropertyPaint* paint) { |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 384 | mDisplayList->ref(x); |
| 385 | mDisplayList->ref(y); |
| 386 | mDisplayList->ref(radius); |
| 387 | mDisplayList->ref(paint); |
| 388 | refBitmapsInShader(paint->value.getShader()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 389 | addOp(alloc().create_trivial<CirclePropsOp>(*(mState.currentSnapshot()->transform), |
| 390 | getRecordedClip(), &paint->value, &x->value, |
| 391 | &y->value, &radius->value)); |
Chris Craik | 268a9c0 | 2015-12-09 18:05:12 -0800 | [diff] [blame] | 392 | } |
| 393 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 394 | void RecordingCanvas::drawOval(float left, float top, float right, float bottom, |
| 395 | const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 396 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 397 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 398 | addOp(alloc().create_trivial<OvalOp>(Rect(left, top, right, bottom), |
| 399 | *(mState.currentSnapshot()->transform), getRecordedClip(), |
| 400 | refPaint(&paint))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 401 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 402 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 403 | void RecordingCanvas::drawArc(float left, float top, float right, float bottom, float startAngle, |
| 404 | float sweepAngle, bool useCenter, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 405 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 406 | |
Chris Craik | caa2418 | 2016-02-19 15:20:35 -0800 | [diff] [blame] | 407 | if (fabs(sweepAngle) >= 360.0f) { |
| 408 | drawOval(left, top, right, bottom, paint); |
| 409 | } else { |
| 410 | addOp(alloc().create_trivial<ArcOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 411 | Rect(left, top, right, bottom), *(mState.currentSnapshot()->transform), |
| 412 | getRecordedClip(), refPaint(&paint), startAngle, sweepAngle, useCenter)); |
Chris Craik | caa2418 | 2016-02-19 15:20:35 -0800 | [diff] [blame] | 413 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 414 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 415 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 416 | void RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 417 | if (CC_UNLIKELY(paint.nothingToDraw())) return; |
Chris Craik | 814ee6a9 | 2016-07-26 16:22:50 -0700 | [diff] [blame] | 418 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 419 | addOp(alloc().create_trivial<PathOp>(Rect(path.getBounds()), |
| 420 | *(mState.currentSnapshot()->transform), getRecordedClip(), |
| 421 | refPaint(&paint), refPath(&path))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 424 | void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) { |
| 425 | mDisplayList->ref(tree); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 426 | mDisplayList->vectorDrawables.push_back(tree); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 427 | addOp(alloc().create_trivial<VectorDrawableOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 428 | tree, Rect(tree->stagingProperties()->getBounds()), |
| 429 | *(mState.currentSnapshot()->transform), getRecordedClip())); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 432 | // Bitmap-based |
sergeyv | aed7f58 | 2016-10-14 16:30:21 -0700 | [diff] [blame] | 433 | void RecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 434 | save(SaveFlags::Matrix); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 435 | translate(left, top); |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 436 | drawBitmap(bitmap, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 437 | restore(); |
| 438 | } |
| 439 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 440 | void RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 441 | if (matrix.isIdentity()) { |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 442 | drawBitmap(bitmap, paint); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 443 | } else if (!(matrix.getType() & ~(SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) && |
| 444 | MathUtils::isPositive(matrix.getScaleX()) && |
| 445 | MathUtils::isPositive(matrix.getScaleY())) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 446 | // SkMatrix::isScaleTranslate() not available in L |
| 447 | SkRect src; |
| 448 | SkRect dst; |
| 449 | bitmap.getBounds(&src); |
| 450 | matrix.mapRect(&dst, src); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 451 | drawBitmap(bitmap, src.fLeft, src.fTop, src.fRight, src.fBottom, dst.fLeft, dst.fTop, |
| 452 | dst.fRight, dst.fBottom, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 453 | } else { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 454 | save(SaveFlags::Matrix); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 455 | concat(matrix); |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 456 | drawBitmap(bitmap, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 457 | restore(); |
| 458 | } |
| 459 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 460 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 461 | void RecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, |
| 462 | float srcBottom, float dstLeft, float dstTop, float dstRight, |
| 463 | float dstBottom, const SkPaint* paint) { |
| 464 | if (srcLeft == 0 && srcTop == 0 && srcRight == bitmap.width() && srcBottom == bitmap.height() && |
| 465 | (srcBottom - srcTop == dstBottom - dstTop) && (srcRight - srcLeft == dstRight - dstLeft)) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 466 | // transform simple rect to rect drawing case into position bitmap ops, since they merge |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 467 | save(SaveFlags::Matrix); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 468 | translate(dstLeft, dstTop); |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 469 | drawBitmap(bitmap, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 470 | restore(); |
| 471 | } else { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 472 | addOp(alloc().create_trivial<BitmapRectOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 473 | Rect(dstLeft, dstTop, dstRight, dstBottom), *(mState.currentSnapshot()->transform), |
| 474 | getRecordedClip(), refPaint(paint), refBitmap(bitmap), |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 475 | Rect(srcLeft, srcTop, srcRight, srcBottom))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 478 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 479 | void RecordingCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 480 | const float* vertices, const int* colors, |
| 481 | const SkPaint* paint) { |
Chris Craik | f09ff5a | 2015-12-08 17:21:58 -0800 | [diff] [blame] | 482 | int vertexCount = (meshWidth + 1) * (meshHeight + 1); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 483 | addOp(alloc().create_trivial<BitmapMeshOp>( |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 484 | calcBoundsOfPoints(vertices, vertexCount * 2), *(mState.currentSnapshot()->transform), |
| 485 | getRecordedClip(), refPaint(paint), refBitmap(bitmap), meshWidth, meshHeight, |
| 486 | refBuffer<float>(vertices, vertexCount * 2), // 2 floats per vertex |
| 487 | refBuffer<int>(colors, vertexCount))); // 1 color per vertex |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 488 | } |
Chris Craik | 386aa03 | 2015-12-07 17:08:25 -0800 | [diff] [blame] | 489 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 490 | void RecordingCanvas::drawNinePatch(Bitmap& bitmap, const android::Res_png_9patch& patch, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 491 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
| 492 | const SkPaint* paint) { |
| 493 | addOp(alloc().create_trivial<PatchOp>(Rect(dstLeft, dstTop, dstRight, dstBottom), |
| 494 | *(mState.currentSnapshot()->transform), getRecordedClip(), |
| 495 | refPaint(paint), refBitmap(bitmap), refPatch(&patch))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 498 | double RecordingCanvas::drawAnimatedImage(AnimatedImageDrawable*) { |
Leon Scroggins III | 671cce2 | 2018-01-14 16:52:17 -0500 | [diff] [blame] | 499 | // Unimplemented |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 500 | return 0; |
Leon Scroggins III | 671cce2 | 2018-01-14 16:52:17 -0500 | [diff] [blame] | 501 | } |
| 502 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 503 | // Text |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 504 | void RecordingCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int glyphCount, const SkPaint& paint, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 505 | float x, float y, float boundsLeft, float boundsTop, |
| 506 | float boundsRight, float boundsBottom, float totalAdvance) { |
Stan Iliev | 0b58d99 | 2017-03-30 18:22:27 -0400 | [diff] [blame] | 507 | if (glyphCount <= 0 || paint.nothingToDraw()) return; |
| 508 | uint16_t* glyphs = (glyph_t*)alloc().alloc<glyph_t>(glyphCount * sizeof(glyph_t)); |
| 509 | float* positions = (float*)alloc().alloc<float>(2 * glyphCount * sizeof(float)); |
| 510 | glyphFunc(glyphs, positions); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 511 | |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 512 | // TODO: either must account for text shadow in bounds, or record separate ops for text shadows |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 513 | addOp(alloc().create_trivial<TextOp>(Rect(boundsLeft, boundsTop, boundsRight, boundsBottom), |
| 514 | *(mState.currentSnapshot()->transform), getRecordedClip(), |
| 515 | refPaint(&paint), glyphs, positions, glyphCount, x, y)); |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 516 | drawTextDecorations(x, y, totalAdvance, paint); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 517 | } |
Chris Craik | a171727 | 2015-11-19 13:02:43 -0800 | [diff] [blame] | 518 | |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 519 | void RecordingCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 520 | const SkPaint& paint, const SkPath& path, size_t start, |
| 521 | size_t end) { |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 522 | uint16_t glyphs[1]; |
| 523 | for (size_t i = start; i < end; i++) { |
| 524 | glyphs[0] = layout.getGlyphId(i); |
| 525 | float x = hOffset + layout.getX(i); |
| 526 | float y = vOffset + layout.getY(i); |
Derek Sollenberger | 792fb32 | 2017-03-03 14:02:09 -0500 | [diff] [blame] | 527 | if (paint.nothingToDraw()) return; |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 528 | const uint16_t* tempGlyphs = refBuffer<glyph_t>(glyphs, 1); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 529 | addOp(alloc().create_trivial<TextOnPathOp>(*(mState.currentSnapshot()->transform), |
| 530 | getRecordedClip(), refPaint(&paint), tempGlyphs, |
| 531 | 1, refPath(&path), x, y)); |
Yuqian Li | afc22149 | 2016-07-18 13:07:42 -0400 | [diff] [blame] | 532 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 533 | } |
| 534 | |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 535 | void RecordingCanvas::drawBitmap(Bitmap& bitmap, const SkPaint* paint) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 536 | addOp(alloc().create_trivial<BitmapOp>(Rect(bitmap.width(), bitmap.height()), |
| 537 | *(mState.currentSnapshot()->transform), |
| 538 | getRecordedClip(), refPaint(paint), refBitmap(bitmap))); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 539 | } |
Chris Craik | e29ce6f | 2015-12-10 16:25:13 -0800 | [diff] [blame] | 540 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 541 | void RecordingCanvas::drawRenderNode(RenderNode* renderNode) { |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 542 | auto&& stagingProps = renderNode->stagingProperties(); |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 543 | RenderNodeOp* op = alloc().create_trivial<RenderNodeOp>( |
Chris Craik | 54fa17f | 2015-11-25 14:14:53 -0800 | [diff] [blame] | 544 | Rect(stagingProps.getWidth(), stagingProps.getHeight()), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 545 | *(mState.currentSnapshot()->transform), getRecordedClip(), renderNode); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 546 | int opIndex = addOp(op); |
Chris Craik | 1367d25 | 2016-03-10 15:43:13 -0800 | [diff] [blame] | 547 | if (CC_LIKELY(opIndex >= 0)) { |
| 548 | int childIndex = mDisplayList->addChild(op); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 549 | |
Chris Craik | 1367d25 | 2016-03-10 15:43:13 -0800 | [diff] [blame] | 550 | // update the chunk's child indices |
| 551 | DisplayList::Chunk& chunk = mDisplayList->chunks.back(); |
| 552 | chunk.endChildIndex = childIndex + 1; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 553 | |
Chris Craik | 1367d25 | 2016-03-10 15:43:13 -0800 | [diff] [blame] | 554 | if (renderNode->stagingProperties().isProjectionReceiver()) { |
| 555 | // use staging property, since recording on UI thread |
| 556 | mDisplayList->projectionReceiveIndex = opIndex; |
| 557 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 561 | void RecordingCanvas::drawLayer(DeferredLayerUpdater* layerHandle) { |
| 562 | // We ref the DeferredLayerUpdater due to its thread-safe ref-counting semantics. |
| 563 | mDisplayList->ref(layerHandle); |
| 564 | |
sergeyv | 3e9999b | 2017-01-19 15:37:02 -0800 | [diff] [blame] | 565 | LOG_ALWAYS_FATAL_IF(layerHandle->getBackingLayerApi() != Layer::Api::OpenGL); |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 566 | // Note that the backing layer has *not* yet been updated, so don't trust |
| 567 | // its width, height, transform, etc...! |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 568 | addOp(alloc().create_trivial<TextureLayerOp>( |
John Reck | 417ed6d | 2016-03-22 16:01:08 -0700 | [diff] [blame] | 569 | Rect(layerHandle->getWidth(), layerHandle->getHeight()), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 570 | *(mState.currentSnapshot()->transform), getRecordedClip(), layerHandle)); |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 571 | } |
| 572 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 573 | void RecordingCanvas::callDrawGLFunction(Functor* functor, GlFunctorLifecycleListener* listener) { |
John Reck | cd1c3eb | 2016-04-14 10:38:54 -0700 | [diff] [blame] | 574 | mDisplayList->functors.push_back({functor, listener}); |
| 575 | mDisplayList->ref(listener); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 576 | addOp(alloc().create_trivial<FunctorOp>(*(mState.currentSnapshot()->transform), |
| 577 | getRecordedClip(), functor)); |
Chris Craik | e29ce6f | 2015-12-10 16:25:13 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Chris Craik | 3c53ec5 | 2016-08-08 15:15:57 -0700 | [diff] [blame] | 580 | int RecordingCanvas::addOp(RecordedOp* op) { |
Chris Craik | 261725f | 2016-02-29 12:52:33 -0800 | [diff] [blame] | 581 | // skip op with empty clip |
| 582 | if (op->localClip && op->localClip->rect.isEmpty()) { |
| 583 | // NOTE: this rejection happens after op construction/content ref-ing, so content ref'd |
| 584 | // and held by renderthread isn't affected by clip rejection. |
| 585 | // Could rewind alloc here if desired, but callers would have to not touch op afterwards. |
| 586 | return -1; |
| 587 | } |
| 588 | |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 589 | int insertIndex = mDisplayList->ops.size(); |
| 590 | mDisplayList->ops.push_back(op); |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 591 | if (mDeferredBarrierType != DeferredBarrierType::None) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 592 | // op is first in new chunk |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 593 | mDisplayList->chunks.emplace_back(); |
| 594 | DisplayList::Chunk& newChunk = mDisplayList->chunks.back(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 595 | newChunk.beginOpIndex = insertIndex; |
| 596 | newChunk.endOpIndex = insertIndex + 1; |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 597 | newChunk.reorderChildren = (mDeferredBarrierType == DeferredBarrierType::OutOfOrder); |
Chris Craik | d645640 | 2016-04-11 12:24:23 -0700 | [diff] [blame] | 598 | newChunk.reorderClip = mDeferredBarrierClip; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 599 | |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 600 | int nextChildIndex = mDisplayList->children.size(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 601 | newChunk.beginChildIndex = newChunk.endChildIndex = nextChildIndex; |
Chris Craik | 161f54b | 2015-11-05 11:08:52 -0800 | [diff] [blame] | 602 | mDeferredBarrierType = DeferredBarrierType::None; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 603 | } else { |
| 604 | // standard case - append to existing chunk |
Chris Craik | 003cc3d | 2015-10-16 10:24:55 -0700 | [diff] [blame] | 605 | mDisplayList->chunks.back().endOpIndex = insertIndex + 1; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 606 | } |
| 607 | return insertIndex; |
| 608 | } |
| 609 | |
| 610 | void RecordingCanvas::refBitmapsInShader(const SkShader* shader) { |
| 611 | if (!shader) return; |
| 612 | |
| 613 | // If this paint has an SkShader that has an SkBitmap add |
| 614 | // it to the bitmap pile |
| 615 | SkBitmap bitmap; |
| 616 | SkShader::TileMode xy[2]; |
Leon Scroggins III | f35b989 | 2015-07-31 10:38:40 -0400 | [diff] [blame] | 617 | if (shader->isABitmap(&bitmap, nullptr, xy)) { |
sergeyv | ec4a4b1 | 2016-10-20 18:39:04 -0700 | [diff] [blame] | 618 | Bitmap* hwuiBitmap = static_cast<Bitmap*>(bitmap.pixelRef()); |
| 619 | refBitmap(*hwuiBitmap); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | SkShader::ComposeRec rec; |
| 623 | if (shader->asACompose(&rec)) { |
| 624 | refBitmapsInShader(rec.fShaderA); |
| 625 | refBitmapsInShader(rec.fShaderB); |
| 626 | return; |
| 627 | } |
| 628 | } |
| 629 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 630 | }; // namespace uirenderer |
| 631 | }; // namespace android |