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