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