Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "SkiaRecordingCanvas.h" |
| 18 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 19 | #include <SkImagePriv.h> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 20 | #include "Layer.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 21 | #include "LayerDrawable.h" |
| 22 | #include "NinePatchUtils.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 23 | #include "RenderNode.h" |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 24 | #include "pipeline/skia/AnimatedDrawables.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | namespace skiapipeline { |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | // Recording Canvas Setup |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | void SkiaRecordingCanvas::initDisplayList(uirenderer::RenderNode* renderNode, int width, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 35 | int height) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 36 | mCurrentBarrier = nullptr; |
| 37 | SkASSERT(mDisplayList.get() == nullptr); |
| 38 | |
| 39 | if (renderNode) { |
| 40 | mDisplayList = renderNode->detachAvailableList(); |
| 41 | } |
Derek Sollenberger | ea1fe9b | 2017-03-01 13:02:43 -0500 | [diff] [blame] | 42 | if (!mDisplayList) { |
| 43 | mDisplayList.reset(new SkiaDisplayList()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 44 | } |
| 45 | |
Derek Sollenberger | ea1fe9b | 2017-03-01 13:02:43 -0500 | [diff] [blame] | 46 | mDisplayList->attachRecorder(&mRecorder, SkIRect::MakeWH(width, height)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 47 | SkiaCanvas::reset(&mRecorder); |
| 48 | } |
| 49 | |
| 50 | uirenderer::DisplayList* SkiaRecordingCanvas::finishRecording() { |
| 51 | // close any existing chunks if necessary |
| 52 | insertReorderBarrier(false); |
| 53 | mRecorder.restoreToCount(1); |
| 54 | return mDisplayList.release(); |
| 55 | } |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | // Recording Canvas draw operations: View System |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
| 61 | void SkiaRecordingCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 62 | uirenderer::CanvasPropertyPrimitive* top, |
| 63 | uirenderer::CanvasPropertyPrimitive* right, |
| 64 | uirenderer::CanvasPropertyPrimitive* bottom, |
| 65 | uirenderer::CanvasPropertyPrimitive* rx, |
| 66 | uirenderer::CanvasPropertyPrimitive* ry, |
| 67 | uirenderer::CanvasPropertyPaint* paint) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 68 | // Destructor of drawables created with allocateDrawable, will be invoked by ~LinearAllocator. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 69 | drawDrawable(mDisplayList->allocateDrawable<AnimatedRoundRect>(left, top, right, bottom, rx, ry, |
| 70 | paint)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | uirenderer::CanvasPropertyPrimitive* y, |
| 75 | uirenderer::CanvasPropertyPrimitive* radius, |
| 76 | uirenderer::CanvasPropertyPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 77 | drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint)); |
| 78 | } |
| 79 | |
| 80 | void SkiaRecordingCanvas::insertReorderBarrier(bool enableReorder) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 81 | if (nullptr != mCurrentBarrier) { |
| 82 | // finish off the existing chunk |
| 83 | SkDrawable* drawable = |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 84 | mDisplayList->allocateDrawable<EndReorderBarrierDrawable>(mCurrentBarrier); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 85 | mCurrentBarrier = nullptr; |
| 86 | drawDrawable(drawable); |
| 87 | } |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 88 | if (enableReorder) { |
| 89 | mCurrentBarrier = (StartReorderBarrierDrawable*) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 90 | mDisplayList->allocateDrawable<StartReorderBarrierDrawable>( |
| 91 | mDisplayList.get()); |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 92 | drawDrawable(mCurrentBarrier); |
| 93 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void SkiaRecordingCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) { |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 97 | if (layerUpdater != nullptr) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 98 | // Create a ref-counted drawable, which is kept alive by sk_sp in SkLiteDL. |
Derek Sollenberger | f5a370e | 2017-06-15 13:50:08 -0400 | [diff] [blame] | 99 | sk_sp<SkDrawable> drawable(new LayerDrawable(layerUpdater)); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 100 | drawDrawable(drawable.get()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void SkiaRecordingCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 105 | // Record the child node. Drawable dtor will be invoked when mChildNodes deque is cleared. |
Stan Iliev | 2f06e8a | 2016-11-02 15:29:03 -0400 | [diff] [blame] | 106 | mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 107 | auto& renderNodeDrawable = mDisplayList->mChildNodes.back(); |
| 108 | drawDrawable(&renderNodeDrawable); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 109 | |
| 110 | // use staging property, since recording on UI thread |
| 111 | if (renderNode->stagingProperties().isProjectionReceiver()) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 112 | mDisplayList->mProjectionReceiver = &renderNodeDrawable; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 113 | // set projectionReceiveIndex so that RenderNode.hasProjectionReceiver returns true |
| 114 | mDisplayList->projectionReceiveIndex = mDisplayList->mChildNodes.size() - 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void SkiaRecordingCanvas::callDrawGLFunction(Functor* functor, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 119 | uirenderer::GlFunctorLifecycleListener* listener) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 120 | // Drawable dtor will be invoked when mChildFunctors deque is cleared. |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 121 | mDisplayList->mChildFunctors.emplace_back(functor, listener, asSkCanvas()); |
| 122 | drawDrawable(&mDisplayList->mChildFunctors.back()); |
| 123 | } |
| 124 | |
| 125 | class VectorDrawable : public SkDrawable { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 126 | public: |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 127 | VectorDrawable(VectorDrawableRoot* tree) : mRoot(tree) {} |
| 128 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 129 | protected: |
| 130 | virtual SkRect onGetBounds() override { return SkRect::MakeLargest(); } |
| 131 | virtual void onDraw(SkCanvas* canvas) override { mRoot->draw(canvas); } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 132 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 133 | private: |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 134 | sp<VectorDrawableRoot> mRoot; |
| 135 | }; |
| 136 | |
| 137 | void SkiaRecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) { |
| 138 | drawDrawable(mDisplayList->allocateDrawable<VectorDrawable>(tree)); |
| 139 | mDisplayList->mVectorDrawables.push_back(tree); |
| 140 | } |
| 141 | |
| 142 | // ---------------------------------------------------------------------------- |
| 143 | // Recording Canvas draw operations: Bitmaps |
| 144 | // ---------------------------------------------------------------------------- |
| 145 | |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 146 | inline static const SkPaint* bitmapPaint(const SkPaint* origPaint, SkPaint* tmpPaint, |
Derek Sollenberger | 4eb2144 | 2017-11-09 12:31:36 -0500 | [diff] [blame] | 147 | sk_sp<SkColorFilter> colorSpaceFilter) { |
| 148 | if ((origPaint && origPaint->isAntiAlias()) || colorSpaceFilter) { |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 149 | if (origPaint) { |
| 150 | *tmpPaint = *origPaint; |
| 151 | } |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 152 | |
Derek Sollenberger | 4eb2144 | 2017-11-09 12:31:36 -0500 | [diff] [blame] | 153 | if (colorSpaceFilter) { |
| 154 | if (tmpPaint->getColorFilter()) { |
| 155 | tmpPaint->setColorFilter( |
| 156 | SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter)); |
| 157 | } else { |
| 158 | tmpPaint->setColorFilter(colorSpaceFilter); |
| 159 | } |
| 160 | LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter()); |
Derek Sollenberger | fa3e340 | 2017-08-04 08:35:10 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Derek Sollenberger | 4eb2144 | 2017-11-09 12:31:36 -0500 | [diff] [blame] | 163 | |
| 164 | // disabling AA on bitmap draws matches legacy HWUI behavior |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 165 | tmpPaint->setAntiAlias(false); |
| 166 | return tmpPaint; |
| 167 | } else { |
| 168 | return origPaint; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 173 | SkPaint tmpPaint; |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 174 | sk_sp<SkColorFilter> colorFilter; |
| 175 | sk_sp<SkImage> image = bitmap.makeImage(&colorFilter); |
| 176 | mRecorder.drawImage(image, left, top, bitmapPaint(paint, &tmpPaint, colorFilter)); |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 177 | // if image->unique() is true, then mRecorder.drawImage failed for some reason. It also means |
| 178 | // it is not safe to store a raw SkImage pointer, because the image object will be destroyed |
| 179 | // when this function ends. |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 180 | if (!bitmap.isImmutable() && image.get() && !image->unique()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 181 | mDisplayList->mMutableImages.push_back(image.get()); |
| 182 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 183 | } |
| 184 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 185 | void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 186 | SkAutoCanvasRestore acr(&mRecorder, true); |
| 187 | concat(matrix); |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 188 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 189 | SkPaint tmpPaint; |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 190 | sk_sp<SkColorFilter> colorFilter; |
| 191 | sk_sp<SkImage> image = bitmap.makeImage(&colorFilter); |
| 192 | mRecorder.drawImage(image, 0, 0, bitmapPaint(paint, &tmpPaint, colorFilter)); |
| 193 | if (!bitmap.isImmutable() && image.get() && !image->unique()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 194 | mDisplayList->mMutableImages.push_back(image.get()); |
| 195 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 196 | } |
| 197 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 198 | void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight, |
| 199 | float srcBottom, float dstLeft, float dstTop, float dstRight, |
| 200 | float dstBottom, const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 201 | SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom); |
| 202 | SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 203 | |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 204 | SkPaint tmpPaint; |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 205 | sk_sp<SkColorFilter> colorFilter; |
| 206 | sk_sp<SkImage> image = bitmap.makeImage(&colorFilter); |
Derek Sollenberger | 6c2a9e2 | 2017-08-15 16:23:01 -0400 | [diff] [blame] | 207 | mRecorder.drawImageRect(image, srcRect, dstRect, bitmapPaint(paint, &tmpPaint, colorFilter), |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 208 | SkCanvas::kFast_SrcRectConstraint); |
| 209 | if (!bitmap.isImmutable() && image.get() && !image->unique() && !srcRect.isEmpty() && |
| 210 | !dstRect.isEmpty()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 211 | mDisplayList->mMutableImages.push_back(image.get()); |
| 212 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 213 | } |
| 214 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 215 | void SkiaRecordingCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft, |
| 216 | float dstTop, float dstRight, float dstBottom, |
| 217 | const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 218 | SkCanvas::Lattice lattice; |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 219 | NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 220 | |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame] | 221 | lattice.fRectTypes = nullptr; |
| 222 | lattice.fColors = nullptr; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 223 | int numFlags = 0; |
| 224 | if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) { |
| 225 | // We can expect the framework to give us a color for every distinct rect. |
| 226 | // Skia requires placeholder flags for degenerate rects. |
| 227 | numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1); |
| 228 | } |
| 229 | |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame] | 230 | SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags); |
| 231 | SkAutoSTMalloc<25, SkColor> colors(numFlags); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 232 | if (numFlags > 0) { |
Stan Iliev | e12d731 | 2017-12-04 14:48:27 -0500 | [diff] [blame] | 233 | NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | lattice.fBounds = nullptr; |
| 237 | SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 238 | |
| 239 | SkPaint tmpPaint; |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 240 | sk_sp<SkColorFilter> colorFilter; |
| 241 | sk_sp<SkImage> image = bitmap.makeImage(&colorFilter); |
| 242 | mRecorder.drawImageLattice(image.get(), lattice, dst, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 243 | bitmapPaint(paint, &tmpPaint, colorFilter)); |
Derek Sollenberger | fb0c8fc | 2017-07-26 13:53:27 -0400 | [diff] [blame] | 244 | if (!bitmap.isImmutable() && image.get() && !image->unique() && !dst.isEmpty()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 245 | mDisplayList->mMutableImages.push_back(image.get()); |
| 246 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 249 | double SkiaRecordingCanvas::drawAnimatedImage(AnimatedImageDrawable* animatedImage) { |
| 250 | drawDrawable(animatedImage); |
| 251 | mDisplayList->mAnimatedImages.push_back(animatedImage); |
| 252 | return 0; |
| 253 | } |
| 254 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 255 | }; // namespace skiapipeline |
| 256 | }; // namespace uirenderer |
| 257 | }; // namespace android |