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 
19 #include <SkSurface.h>
20 #include "FrameBuilder.h"
21 #include "hwui/AnimatedImageDrawable.h"
22 #include "renderthread/CanvasContext.h"
23 #include "renderthread/IRenderPipeline.h"
24 
25 class SkPictureRecorder;
26 
27 namespace android {
28 namespace uirenderer {
29 namespace skiapipeline {
30 
31 class SkiaPipeline : public renderthread::IRenderPipeline {
32 public:
33     SkiaPipeline(renderthread::RenderThread& thread);
34     virtual ~SkiaPipeline();
35 
36     TaskManager* getTaskManager() override;
37 
38     void onDestroyHardwareResources() override;
39 
40     bool pinImages(std::vector<SkImage*>& mutableImages) override;
pinImages(LsaVector<sk_sp<Bitmap>> & images)41     bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
42     void unpinImages() override;
43     void onPrepareTree() override;
44 
45     void renderLayers(const FrameBuilder::LightGeometry& lightGeometry,
46                       LayerUpdateQueue* layerUpdateQueue, bool opaque, bool wideColorGamut,
47                       const BakedOpRenderer::LightInfo& lightInfo) override;
48 
49     bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
50                              bool wideColorGamut, ErrorHandler* errorHandler) override;
51 
52     void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
53                      const std::vector<sp<RenderNode>>& nodes, bool opaque, bool wideColorGamut,
54                      const Rect& contentDrawBounds, sk_sp<SkSurface> surface);
55 
getVectorDrawables()56     std::vector<VectorDrawableRoot*>* getVectorDrawables() { return &mVectorDrawables; }
57 
58     static void destroyLayer(RenderNode* node);
59 
60     static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
61 
62     void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque, bool wideColorGamut);
63 
getLightRadius()64     static float getLightRadius() {
65         if (CC_UNLIKELY(Properties::overrideLightRadius > 0)) {
66             return Properties::overrideLightRadius;
67         }
68         return mLightRadius;
69     }
70 
getAmbientShadowAlpha()71     static uint8_t getAmbientShadowAlpha() {
72         if (CC_UNLIKELY(Properties::overrideAmbientShadowStrength >= 0)) {
73             return Properties::overrideAmbientShadowStrength;
74         }
75         return mAmbientShadowAlpha;
76     }
77 
getSpotShadowAlpha()78     static uint8_t getSpotShadowAlpha() {
79         if (CC_UNLIKELY(Properties::overrideSpotShadowStrength >= 0)) {
80             return Properties::overrideSpotShadowStrength;
81         }
82         return mSpotShadowAlpha;
83     }
84 
getLightCenter()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
90                 adjustedLightCenter.y = -Properties::overrideLightPosY;
91             }
92             if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
93                 adjustedLightCenter.z = Properties::overrideLightPosZ;
94             }
95             return adjustedLightCenter;
96         }
97         return mLightCenter;
98     }
99 
updateLighting(const FrameBuilder::LightGeometry & lightGeometry,const BakedOpRenderer::LightInfo & lightInfo)100     static void updateLighting(const FrameBuilder::LightGeometry& lightGeometry,
101                                const BakedOpRenderer::LightInfo& lightInfo) {
102         mLightRadius = lightGeometry.radius;
103         mAmbientShadowAlpha = lightInfo.ambientShadowAlpha;
104         mSpotShadowAlpha = lightInfo.spotShadowAlpha;
105         mLightCenter = lightGeometry.center;
106     }
107 
108 protected:
109     void dumpResourceCacheUsage() const;
110 
111     renderthread::RenderThread& mRenderThread;
112 
113 private:
114     void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
115                          const std::vector<sp<RenderNode>>& nodes, bool opaque, bool wideColorGamut,
116                          const Rect& contentDrawBounds, SkCanvas* canvas);
117 
118     /**
119      *  Debugging feature.  Draws a semi-transparent overlay on each pixel, indicating
120      *  how many times it has been drawn.
121      */
122     void renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
123                         const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
124                         sk_sp<SkSurface>);
125 
126     /**
127      *  Render mVectorDrawables into offscreen buffers.
128      */
129     void renderVectorDrawableCache();
130 
131     SkCanvas* tryCapture(SkSurface* surface);
132     void endCapture(SkSurface* surface);
133 
134     std::vector<sk_sp<SkImage>> mPinnedImages;
135 
136     /**
137      *  populated by prepareTree with dirty VDs
138      */
139     std::vector<VectorDrawableRoot*> mVectorDrawables;
140 
141     // Block of properties used only for debugging to record a SkPicture and save it in a file.
142     /**
143      * mCapturedFile is used to enforce we don't capture more than once for a given name (cause
144      * permissions don't allow to reset a property from render thread).
145      */
146     std::string mCapturedFile;
147     /**
148      *  mCaptureSequence counts how many frames are left to take in the sequence.
149      */
150     int mCaptureSequence = 0;
151     /**
152      *  mSavePictureProcessor is used to run the file saving code in a separate thread.
153      */
154     class SavePictureProcessor;
155     sp<SavePictureProcessor> mSavePictureProcessor;
156     /**
157      *  mRecorder holds the current picture recorder. We could store it on the stack to support
158      *  parallel tryCapture calls (not really needed).
159      */
160     std::unique_ptr<SkPictureRecorder> mRecorder;
161 
162     static float mLightRadius;
163     static uint8_t mAmbientShadowAlpha;
164     static uint8_t mSpotShadowAlpha;
165     static Vector3 mLightCenter;
166 };
167 
168 } /* namespace skiapipeline */
169 } /* namespace uirenderer */
170 } /* namespace android */
171