blob: f9d37b924321369dfed9dfa3a7e64c3665b452db [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
Kevin Lubick07d6aae2022-04-01 14:03:11 -040019#include <SkColorSpace.h>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040020#include <SkDocument.h>
21#include <SkMultiPictureDocument.h>
Kevin Lubick07d6aae2022-04-01 14:03:11 -040022#include <SkSurface.h>
Nader Jawada3521852023-01-30 20:23:46 -080023
John Reckd9d7f122018-05-03 14:40:56 -070024#include "Lighting.h"
Derek Sollenberger2d142132018-01-22 10:25:26 -050025#include "hwui/AnimatedImageDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070026#include "renderthread/CanvasContext.h"
Nader Jawada3521852023-01-30 20:23:46 -080027#include "renderthread/HardwareBufferRenderParams.h"
John Reck1bcacfd2017-11-03 10:12:19 -070028#include "renderthread/IRenderPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040029
Kevin Lubick1175dc02022-02-28 12:41:27 -050030class SkFILEWStream;
Stan Ilieve9d00122017-09-19 12:07:10 -040031class SkPictureRecorder;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040032struct SkSharingSerialContext;
Stan Ilieve9d00122017-09-19 12:07:10 -040033
Stan Iliev500a0c32016-10-26 10:30:09 -040034namespace android {
35namespace uirenderer {
36namespace skiapipeline {
37
38class SkiaPipeline : public renderthread::IRenderPipeline {
39public:
Chih-Hung Hsiehf9336412018-12-20 13:48:57 -080040 explicit SkiaPipeline(renderthread::RenderThread& thread);
Stan Iliev232f3622017-08-23 17:15:09 -040041 virtual ~SkiaPipeline();
Stan Iliev500a0c32016-10-26 10:30:09 -040042
Stan Iliev500a0c32016-10-26 10:30:09 -040043 void onDestroyHardwareResources() override;
44
Peiyong Lin1f6aa122018-09-10 16:28:08 -070045 void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
46 bool opaque, const LightInfo& lightInfo) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040047
John Reckb36bfdd2020-07-23 13:47:49 -070048 void setSurfaceColorProperties(ColorMode colorMode) override;
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
Jerome Gaillard09a38e42024-03-14 15:20:56 +000057 void renderLayerImpl(RenderNode* layerNode, const Rect& layerDamage);
58 virtual void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) = 0;
Stan Iliev500a0c32016-10-26 10:30:09 -040059
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040060 // Sets the recording callback to the provided function and the recording mode
61 // to CallbackAPI
John Reck5cca8f22018-12-10 17:06:22 -080062 void setPictureCapturedCallback(
63 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
64 mPictureCapturedCallback = callback;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040065 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None;
John Reck5cca8f22018-12-10 17:06:22 -080066 }
67
John Reck55887762023-01-25 16:51:18 -050068 void setTargetSdrHdrRatio(float ratio) override;
69
Stan Iliev500a0c32016-10-26 10:30:09 -040070protected:
71 renderthread::RenderThread& mRenderThread;
Derek Sollenberger1863d942020-02-05 15:41:51 -050072
Nader Jawada3521852023-01-30 20:23:46 -080073 sk_sp<SkSurface> mBufferSurface = nullptr;
74 sk_sp<SkColorSpace> mBufferColorSpace = nullptr;
75
John Reckb36bfdd2020-07-23 13:47:49 -070076 ColorMode mColorMode = ColorMode::Default;
Peiyong Lin189021b2018-09-27 16:41:40 -070077 SkColorType mSurfaceColorType;
78 sk_sp<SkColorSpace> mSurfaceColorSpace;
John Reck55887762023-01-25 16:51:18 -050079 float mTargetSdrHdrRatio = 1.f;
Stan Iliev500a0c32016-10-26 10:30:09 -040080
John Reck76005182021-06-09 22:43:05 -040081 bool isCapturingSkp() const { return mCaptureMode != CaptureMode::None; }
82
Stan Iliev500a0c32016-10-26 10:30:09 -040083private:
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050084 void renderFrameImpl(const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070085 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050086 const Rect& contentDrawBounds, SkCanvas* canvas,
87 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050088
89 /**
90 * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating
91 * how many times it has been drawn.
92 */
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050093 void renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -070094 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -050095 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050096
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040097 // Called every frame. Normally returns early with screen canvas.
98 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050099 SkCanvas* tryCapture(SkSurface* surface, RenderNode* root, const LayerUpdateQueue& dirtyLayers);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400100 // Called at the end of every frame, closes the recording if necessary.
Stan Ilieve9d00122017-09-19 12:07:10 -0400101 void endCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400102 // Determine if a new file-based capture should be started.
103 // If so, sets mCapturedFile and mCaptureSequence and returns true.
104 // Should be called every frame when capture is enabled.
105 // sets mCaptureMode.
106 bool shouldStartNewFileCapture();
107 // Set up a multi frame capture.
108 bool setupMultiFrameCapture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400109
Stan Ilieve9d00122017-09-19 12:07:10 -0400110 // Block of properties used only for debugging to record a SkPicture and save it in a file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400111 // There are three possible ways of recording drawing commands.
112 enum class CaptureMode {
113 // return to this mode when capture stops.
114 None,
115 // A mode where every frame is recorded into an SkPicture and sent to a provided callback,
116 // until that callback is cleared
117 CallbackAPI,
118 // A mode where a finite number of frames are recorded to a file with
119 // SkMultiPictureDocument
120 MultiFrameSKP,
121 // A mode which records a single frame to a normal SKP file.
122 SingleFrameSKP,
123 };
124 CaptureMode mCaptureMode = CaptureMode::None;
125
Stan Ilieve9d00122017-09-19 12:07:10 -0400126 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400127 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or
128 * SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400129 */
130 std::string mCapturedFile;
131 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400132 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable
133 * only to MultiFrameSKP or SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400134 */
135 int mCaptureSequence = 0;
John Reck322b8ab2019-03-14 13:15:28 -0700136
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400137 // Multi frame serialization stream and writer used when serializing more than one frame.
Ady Abrahamb1547922021-05-10 21:20:25 -0700138 std::unique_ptr<SkSharingSerialContext> mSerialContext; // Must be declared before any other
139 // serializing member
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400140 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
141 sk_sp<SkDocument> mMultiPic;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400142
Stan Ilieve9d00122017-09-19 12:07:10 -0400143 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400144 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or
145 * CallbackAPI modes.
Stan Ilieve9d00122017-09-19 12:07:10 -0400146 */
147 std::unique_ptr<SkPictureRecorder> mRecorder;
John Reck5cca8f22018-12-10 17:06:22 -0800148 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400149
150 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing.
151 // Not used in other recording modes.
John Reck5cca8f22018-12-10 17:06:22 -0800152 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Iliev500a0c32016-10-26 10:30:09 -0400153};
154
155} /* namespace skiapipeline */
156} /* namespace uirenderer */
157} /* namespace android */