blob: 41d864653b67e54a1d443455e7926266d1680eb1 [file] [log] [blame]
Stan Iliev500a0c32016-10-26 10:30:09 -04001/*
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 Iliev500a0c32016-10-26 10:30:09 -040019#include <SkSurface.h>
John Reckd9d7f122018-05-03 14:40:56 -070020#include "Lighting.h"
Derek Sollenberger2d142132018-01-22 10:25:26 -050021#include "hwui/AnimatedImageDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070022#include "renderthread/CanvasContext.h"
23#include "renderthread/IRenderPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040024
Stan Ilieve9d00122017-09-19 12:07:10 -040025class SkPictureRecorder;
26
Stan Iliev500a0c32016-10-26 10:30:09 -040027namespace android {
28namespace uirenderer {
29namespace skiapipeline {
30
31class SkiaPipeline : public renderthread::IRenderPipeline {
32public:
Chih-Hung Hsiehf9336412018-12-20 13:48:57 -080033 explicit SkiaPipeline(renderthread::RenderThread& thread);
Stan Iliev232f3622017-08-23 17:15:09 -040034 virtual ~SkiaPipeline();
Stan Iliev500a0c32016-10-26 10:30:09 -040035
Stan Iliev500a0c32016-10-26 10:30:09 -040036 void onDestroyHardwareResources() override;
37
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040038 bool pinImages(std::vector<SkImage*>& mutableImages) override;
39 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
40 void unpinImages() override;
Stan Iliev47fed6ba2017-10-18 17:56:43 -040041 void onPrepareTree() override;
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040042
Peiyong Lin1f6aa122018-09-10 16:28:08 -070043 void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
44 bool opaque, const LightInfo& lightInfo) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040045
John Reck1bcacfd2017-11-03 10:12:19 -070046 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070047 ErrorHandler* errorHandler) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040048
Derek Sollenberger25833d22019-01-14 13:55:55 -050049 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; }
Peiyong Lin189021b2018-09-27 16:41:40 -070050 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; }
51
Stan Iliev500a0c32016-10-26 10:30:09 -040052 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070053 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050054 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
55 const SkMatrix& preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -040056
Stan Iliev23c38a92017-03-23 00:12:50 -040057 std::vector<VectorDrawableRoot*>* getVectorDrawables() { return &mVectorDrawables; }
58
Stan Iliev500a0c32016-10-26 10:30:09 -040059 static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
60
Peiyong Lin1f6aa122018-09-10 16:28:08 -070061 void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040062
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 Reck1bcacfd2017-11-03 10:12:19 -070089 adjustedLightCenter.y = -Properties::overrideLightPosY;
Stan Iliev500a0c32016-10-26 10:30:09 -040090 }
91 if (CC_UNLIKELY(Properties::overrideLightPosZ > 0)) {
92 adjustedLightCenter.z = Properties::overrideLightPosZ;
93 }
94 return adjustedLightCenter;
95 }
96 return mLightCenter;
97 }
98
John Reck283bb462018-12-13 16:40:14 -080099 static void updateLighting(const LightGeometry& lightGeometry, const LightInfo& lightInfo) {
Stan Iliev500a0c32016-10-26 10:30:09 -0400100 mLightRadius = lightGeometry.radius;
101 mAmbientShadowAlpha = lightInfo.ambientShadowAlpha;
102 mSpotShadowAlpha = lightInfo.spotShadowAlpha;
103 mLightCenter = lightGeometry.center;
104 }
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500105
John Reck5cca8f22018-12-10 17:06:22 -0800106 void setPictureCapturedCallback(
107 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
108 mPictureCapturedCallback = callback;
109 }
110
Stan Iliev500a0c32016-10-26 10:30:09 -0400111protected:
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500112 void dumpResourceCacheUsage() const;
Peiyong Lin3bff1352018-12-11 07:56:07 -0800113 void setSurfaceColorProperties(renderthread::ColorMode colorMode);
Matt Sarett4bda6bf2016-11-07 15:43:41 -0500114
Stan Iliev500a0c32016-10-26 10:30:09 -0400115 renderthread::RenderThread& mRenderThread;
Peiyong Lin189021b2018-09-27 16:41:40 -0700116 SkColorType mSurfaceColorType;
117 sk_sp<SkColorSpace> mSurfaceColorSpace;
Stan Iliev500a0c32016-10-26 10:30:09 -0400118
119private:
Matt Sarettf58cc922016-11-14 18:33:38 -0500120 void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700121 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500122 const Rect& contentDrawBounds, SkCanvas* canvas,
123 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500124
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 Reck1bcacfd2017-11-03 10:12:19 -0700130 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -0500131 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500132
Stan Iliev23c38a92017-03-23 00:12:50 -0400133 /**
134 * Render mVectorDrawables into offscreen buffers.
135 */
136 void renderVectorDrawableCache();
137
Stan Ilieve9d00122017-09-19 12:07:10 -0400138 SkCanvas* tryCapture(SkSurface* surface);
139 void endCapture(SkSurface* surface);
140
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400141 std::vector<sk_sp<SkImage>> mPinnedImages;
Stan Iliev23c38a92017-03-23 00:12:50 -0400142
143 /**
144 * populated by prepareTree with dirty VDs
145 */
146 std::vector<VectorDrawableRoot*> mVectorDrawables;
Stan Ilieve9d00122017-09-19 12:07:10 -0400147
Stan Ilieve9d00122017-09-19 12:07:10 -0400148 // 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 Reck322b8ab2019-03-14 13:15:28 -0700158
Stan Ilieve9d00122017-09-19 12:07:10 -0400159 /**
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 Reck5cca8f22018-12-10 17:06:22 -0800164 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
165 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Ilieve9d00122017-09-19 12:07:10 -0400166
Stan Iliev500a0c32016-10-26 10:30:09 -0400167 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 */