Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -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 | #pragma once |
| 18 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 19 | #include <SkSurface.h> |
John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 20 | #include "Lighting.h" |
Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 21 | #include "hwui/AnimatedImageDrawable.h" |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include "renderthread/CanvasContext.h" |
| 23 | #include "renderthread/IRenderPipeline.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 24 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 25 | class SkPictureRecorder; |
| 26 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | namespace skiapipeline { |
| 30 | |
| 31 | class SkiaPipeline : public renderthread::IRenderPipeline { |
| 32 | public: |
Chih-Hung Hsieh | f933641 | 2018-12-20 13:48:57 -0800 | [diff] [blame] | 33 | explicit SkiaPipeline(renderthread::RenderThread& thread); |
Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 34 | virtual ~SkiaPipeline(); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 35 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 36 | void onDestroyHardwareResources() override; |
| 37 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 38 | bool pinImages(std::vector<SkImage*>& mutableImages) override; |
| 39 | bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; } |
| 40 | void unpinImages() override; |
Stan Iliev | 47fed6ba | 2017-10-18 17:56:43 -0400 | [diff] [blame] | 41 | void onPrepareTree() override; |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 42 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 43 | void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, |
| 44 | bool opaque, const LightInfo& lightInfo) override; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 45 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 47 | ErrorHandler* errorHandler) override; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 48 | |
Derek Sollenberger | 25833d2 | 2019-01-14 13:55:55 -0500 | [diff] [blame] | 49 | SkColorType getSurfaceColorType() const override { return mSurfaceColorType; } |
Peiyong Lin | 189021b | 2018-09-27 16:41:40 -0700 | [diff] [blame] | 50 | sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; } |
| 51 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 52 | void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 53 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 54 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, |
| 55 | const SkMatrix& preTransform); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 56 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 57 | std::vector<VectorDrawableRoot*>* getVectorDrawables() { return &mVectorDrawables; } |
| 58 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 59 | static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap); |
| 60 | |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 61 | void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque); |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 62 | |
| 63 | static float getLightRadius() { |
| 64 | if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) { |
| 65 | return Properties::overrideLightRadius; |
| 66 | } |
| 67 | return mLightRadius; |
| 68 | } |
| 69 | |
| 70 | static uint8_t getAmbientShadowAlpha() { |
| 71 | if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) { |
| 72 | return Properties::overrideAmbientShadowStrength; |
| 73 | } |
| 74 | return mAmbientShadowAlpha; |
| 75 | } |
| 76 | |
| 77 | static uint8_t getSpotShadowAlpha() { |
| 78 | if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) { |
| 79 | return Properties::overrideSpotShadowStrength; |
| 80 | } |
| 81 | return mSpotShadowAlpha; |
| 82 | } |
| 83 | |
| 84 | static Vector3 getLightCenter() { |
| 85 | if (CC_UNLIKELY(Properties::overrideLightPosY > 0 || Properties::overrideLightPosZ > 0)) { |
| 86 | Vector3 adjustedLightCenter = mLightCenter; |
| 87 | if (CC_UNLIKELY(Properties::overrideLightPosY > 0)) { |
| 88 | // negated since this shifts up |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | adjustedLightCenter.y = -Properties::overrideLightPosY; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 90 | } |
| 91 | if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) { |
| 92 | adjustedLightCenter.z = Properties::overrideLightPosZ; |
| 93 | } |
| 94 | return adjustedLightCenter; |
| 95 | } |
| 96 | return mLightCenter; |
| 97 | } |
| 98 | |
John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 99 | static void updateLighting(const LightGeometry& lightGeometry, const LightInfo& lightInfo) { |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 100 | mLightRadius = lightGeometry.radius; |
| 101 | mAmbientShadowAlpha = lightInfo.ambientShadowAlpha; |
| 102 | mSpotShadowAlpha = lightInfo.spotShadowAlpha; |
| 103 | mLightCenter = lightGeometry.center; |
| 104 | } |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 105 | |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 106 | void setPictureCapturedCallback( |
| 107 | const std::function<void(sk_sp<SkPicture>&&)>& callback) override { |
| 108 | mPictureCapturedCallback = callback; |
| 109 | } |
| 110 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 111 | protected: |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 112 | void dumpResourceCacheUsage() const; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 113 | void setSurfaceColorProperties(renderthread::ColorMode colorMode); |
Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 114 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 115 | renderthread::RenderThread& mRenderThread; |
Peiyong Lin | 189021b | 2018-09-27 16:41:40 -0700 | [diff] [blame] | 116 | SkColorType mSurfaceColorType; |
| 117 | sk_sp<SkColorSpace> mSurfaceColorSpace; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 118 | |
| 119 | private: |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 120 | void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip, |
Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 121 | const std::vector<sp<RenderNode>>& nodes, bool opaque, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 122 | const Rect& contentDrawBounds, SkCanvas* canvas, |
| 123 | const SkMatrix& preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating |
| 127 | * how many times it has been drawn. |
| 128 | */ |
| 129 | void renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 130 | const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds, |
Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 131 | sk_sp<SkSurface> surface, const SkMatrix& preTransform); |
Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 132 | |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 133 | /** |
| 134 | * Render mVectorDrawables into offscreen buffers. |
| 135 | */ |
| 136 | void renderVectorDrawableCache(); |
| 137 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 138 | SkCanvas* tryCapture(SkSurface* surface); |
| 139 | void endCapture(SkSurface* surface); |
| 140 | |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 141 | std::vector<sk_sp<SkImage>> mPinnedImages; |
Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * populated by prepareTree with dirty VDs |
| 145 | */ |
| 146 | std::vector<VectorDrawableRoot*> mVectorDrawables; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 147 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 148 | // Block of properties used only for debugging to record a SkPicture and save it in a file. |
| 149 | /** |
| 150 | * mCapturedFile is used to enforce we don't capture more than once for a given name (cause |
| 151 | * permissions don't allow to reset a property from render thread). |
| 152 | */ |
| 153 | std::string mCapturedFile; |
| 154 | /** |
| 155 | * mCaptureSequence counts how many frames are left to take in the sequence. |
| 156 | */ |
| 157 | int mCaptureSequence = 0; |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame^] | 158 | |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 159 | /** |
| 160 | * mRecorder holds the current picture recorder. We could store it on the stack to support |
| 161 | * parallel tryCapture calls (not really needed). |
| 162 | */ |
| 163 | std::unique_ptr<SkPictureRecorder> mRecorder; |
John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 164 | std::unique_ptr<SkNWayCanvas> mNwayCanvas; |
| 165 | std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback; |
Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 166 | |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 167 | static float mLightRadius; |
| 168 | static uint8_t mAmbientShadowAlpha; |
| 169 | static uint8_t mSpotShadowAlpha; |
| 170 | static Vector3 mLightCenter; |
| 171 | }; |
| 172 | |
| 173 | } /* namespace skiapipeline */ |
| 174 | } /* namespace uirenderer */ |
| 175 | } /* namespace android */ |