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 | |
| 19 | #include "Layer.h" |
| 20 | #include "RenderNode.h" |
| 21 | #include "LayerDrawable.h" |
| 22 | #include "NinePatchUtils.h" |
| 23 | #include "pipeline/skia/AnimatedDrawables.h" |
Leon Scroggins III | ee708fa | 2016-12-12 15:31:39 -0500 | [diff] [blame] | 24 | #include <SkImagePriv.h> |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 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, |
| 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, |
| 62 | uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right, |
| 63 | uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx, |
| 64 | uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 65 | // Destructor of drawables created with allocateDrawable, will be invoked by ~LinearAllocator. |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 66 | drawDrawable(mDisplayList->allocateDrawable<AnimatedRoundRect>(left, top, right, bottom, |
| 67 | rx, ry, paint)); |
| 68 | } |
| 69 | |
| 70 | void SkiaRecordingCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, |
| 71 | uirenderer::CanvasPropertyPrimitive* y, uirenderer::CanvasPropertyPrimitive* radius, |
| 72 | uirenderer::CanvasPropertyPaint* paint) { |
| 73 | drawDrawable(mDisplayList->allocateDrawable<AnimatedCircle>(x, y, radius, paint)); |
| 74 | } |
| 75 | |
| 76 | void SkiaRecordingCanvas::insertReorderBarrier(bool enableReorder) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 77 | if (nullptr != mCurrentBarrier) { |
| 78 | // finish off the existing chunk |
| 79 | SkDrawable* drawable = |
| 80 | mDisplayList->allocateDrawable<EndReorderBarrierDrawable>( |
| 81 | mCurrentBarrier); |
| 82 | mCurrentBarrier = nullptr; |
| 83 | drawDrawable(drawable); |
| 84 | } |
Stan Iliev | 88e0891 | 2016-11-22 18:19:29 -0500 | [diff] [blame] | 85 | if (enableReorder) { |
| 86 | mCurrentBarrier = (StartReorderBarrierDrawable*) |
| 87 | mDisplayList->allocateDrawable<StartReorderBarrierDrawable>( |
| 88 | mDisplayList.get()); |
| 89 | drawDrawable(mCurrentBarrier); |
| 90 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void SkiaRecordingCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) { |
| 94 | if (layerUpdater != nullptr && layerUpdater->backingLayer() != nullptr) { |
| 95 | uirenderer::Layer* layer = layerUpdater->backingLayer(); |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 96 | // Create a ref-counted drawable, which is kept alive by sk_sp in SkLiteDL. |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 97 | sk_sp<SkDrawable> drawable(new LayerDrawable(layer)); |
| 98 | drawDrawable(drawable.get()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void SkiaRecordingCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 103 | // 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] | 104 | mDisplayList->mChildNodes.emplace_back(renderNode, asSkCanvas(), true, mCurrentBarrier); |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 105 | auto& renderNodeDrawable = mDisplayList->mChildNodes.back(); |
| 106 | drawDrawable(&renderNodeDrawable); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 107 | |
| 108 | // use staging property, since recording on UI thread |
| 109 | if (renderNode->stagingProperties().isProjectionReceiver()) { |
Stan Iliev | db45a4b | 2016-11-08 14:18:31 -0500 | [diff] [blame] | 110 | mDisplayList->mProjectionReceiver = &renderNodeDrawable; |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 111 | // set projectionReceiveIndex so that RenderNode.hasProjectionReceiver returns true |
| 112 | mDisplayList->projectionReceiveIndex = mDisplayList->mChildNodes.size() - 1; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void SkiaRecordingCanvas::callDrawGLFunction(Functor* functor, |
| 117 | uirenderer::GlFunctorLifecycleListener* listener) { |
Stan Iliev | f7aa6cd | 2017-03-29 18:25:12 -0400 | [diff] [blame] | 118 | // Drawable dtor will be invoked when mChildFunctors deque is cleared. |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 119 | mDisplayList->mChildFunctors.emplace_back(functor, listener, asSkCanvas()); |
| 120 | drawDrawable(&mDisplayList->mChildFunctors.back()); |
| 121 | } |
| 122 | |
| 123 | class VectorDrawable : public SkDrawable { |
| 124 | public: |
| 125 | VectorDrawable(VectorDrawableRoot* tree) : mRoot(tree) {} |
| 126 | |
| 127 | protected: |
| 128 | virtual SkRect onGetBounds() override { |
| 129 | return SkRect::MakeLargest(); |
| 130 | } |
| 131 | virtual void onDraw(SkCanvas* canvas) override { |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 132 | mRoot->draw(canvas); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
| 136 | sp<VectorDrawableRoot> mRoot; |
| 137 | }; |
| 138 | |
| 139 | void SkiaRecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) { |
| 140 | drawDrawable(mDisplayList->allocateDrawable<VectorDrawable>(tree)); |
| 141 | mDisplayList->mVectorDrawables.push_back(tree); |
| 142 | } |
| 143 | |
| 144 | // ---------------------------------------------------------------------------- |
| 145 | // Recording Canvas draw operations: Bitmaps |
| 146 | // ---------------------------------------------------------------------------- |
| 147 | |
| 148 | inline static const SkPaint* nonAAPaint(const SkPaint* origPaint, SkPaint* tmpPaint) { |
| 149 | if (origPaint && origPaint->isAntiAlias()) { |
| 150 | *tmpPaint = *origPaint; |
| 151 | tmpPaint->setAntiAlias(false); |
| 152 | return tmpPaint; |
| 153 | } else { |
| 154 | return origPaint; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void SkiaRecordingCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) { |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 159 | sk_sp<SkImage> image = bitmap.makeImage(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 160 | SkPaint tmpPaint; |
| 161 | mRecorder.drawImage(image, left, top, nonAAPaint(paint, &tmpPaint)); |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 162 | // if image->unique() is true, then mRecorder.drawImage failed for some reason. It also means |
| 163 | // it is not safe to store a raw SkImage pointer, because the image object will be destroyed |
| 164 | // when this function ends. |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 165 | if (!bitmap.isImmutable() && image.get() && !image->unique()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 166 | mDisplayList->mMutableImages.push_back(image.get()); |
| 167 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void SkiaRecordingCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, |
| 171 | const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 172 | SkAutoCanvasRestore acr(&mRecorder, true); |
| 173 | concat(matrix); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 174 | sk_sp<SkImage> image = hwuiBitmap.makeImage(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 175 | SkPaint tmpPaint; |
| 176 | mRecorder.drawImage(image, 0, 0, nonAAPaint(paint, &tmpPaint)); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 177 | if (!hwuiBitmap.isImmutable() && image.get() && !image->unique()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 178 | mDisplayList->mMutableImages.push_back(image.get()); |
| 179 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void SkiaRecordingCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop, |
| 183 | float srcRight, float srcBottom, float dstLeft, float dstTop, float dstRight, |
| 184 | float dstBottom, const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 185 | SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom); |
| 186 | SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 187 | sk_sp<SkImage> image = hwuiBitmap.makeImage(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 188 | SkPaint tmpPaint; |
| 189 | mRecorder.drawImageRect(image, srcRect, dstRect, nonAAPaint(paint, &tmpPaint)); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 190 | if (!hwuiBitmap.isImmutable() && image.get() && !image->unique() && !srcRect.isEmpty() |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 191 | && !dstRect.isEmpty()) { |
| 192 | mDisplayList->mMutableImages.push_back(image.get()); |
| 193 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void SkiaRecordingCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk, |
| 197 | float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) { |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 198 | SkCanvas::Lattice lattice; |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 199 | NinePatchUtils::SetLatticeDivs(&lattice, chunk, hwuiBitmap.width(), hwuiBitmap.height()); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 200 | |
| 201 | lattice.fFlags = nullptr; |
| 202 | int numFlags = 0; |
| 203 | if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) { |
| 204 | // We can expect the framework to give us a color for every distinct rect. |
| 205 | // Skia requires placeholder flags for degenerate rects. |
| 206 | numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1); |
| 207 | } |
| 208 | |
| 209 | SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags); |
| 210 | if (numFlags > 0) { |
| 211 | NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk); |
| 212 | } |
| 213 | |
| 214 | lattice.fBounds = nullptr; |
| 215 | SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 216 | sk_sp<SkImage> image = hwuiBitmap.makeImage(); |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 217 | |
| 218 | SkPaint tmpPaint; |
| 219 | mRecorder.drawImageLattice(image.get(), lattice, dst, nonAAPaint(paint, &tmpPaint)); |
Stan Iliev | 7bc3bc6 | 2017-05-24 13:28:36 -0400 | [diff] [blame] | 220 | if (!hwuiBitmap.isImmutable() && image.get() && !image->unique() && !dst.isEmpty()) { |
Stan Iliev | 0cc4e36 | 2017-05-18 14:21:23 -0400 | [diff] [blame] | 221 | mDisplayList->mMutableImages.push_back(image.get()); |
| 222 | } |
Stan Iliev | 021693b | 2016-10-17 16:26:15 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | }; // namespace skiapipeline |
| 226 | }; // namespace uirenderer |
| 227 | }; // namespace android |