blob: 4f9334654c9bc36a37157d794f48b6a7b81ecfee [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 Jawad7612c612022-11-15 10:55:54 -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 Jawad7612c612022-11-15 10:55:54 -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
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040045 bool pinImages(std::vector<SkImage*>& mutableImages) override;
46 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
47 void unpinImages() override;
48
Peiyong Lin1f6aa122018-09-10 16:28:08 -070049 void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
50 bool opaque, const LightInfo& lightInfo) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040051
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050052 // If the given node didn't have a layer surface, or had one of the wrong size, this method
53 // creates a new one and returns true. Otherwise does nothing and returns false.
John Reck1bcacfd2017-11-03 10:12:19 -070054 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070055 ErrorHandler* errorHandler) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040056
John Reckb36bfdd2020-07-23 13:47:49 -070057 void setSurfaceColorProperties(ColorMode colorMode) override;
Derek Sollenberger25833d22019-01-14 13:55:55 -050058 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; }
Peiyong Lin189021b2018-09-27 16:41:40 -070059 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; }
60
Stan Iliev500a0c32016-10-26 10:30:09 -040061 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070062 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050063 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
64 const SkMatrix& preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -040065
Stan Iliev500a0c32016-10-26 10:30:09 -040066 static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
67
Peiyong Lin1f6aa122018-09-10 16:28:08 -070068 void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040069
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040070 // Sets the recording callback to the provided function and the recording mode
71 // to CallbackAPI
John Reck5cca8f22018-12-10 17:06:22 -080072 void setPictureCapturedCallback(
73 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
74 mPictureCapturedCallback = callback;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040075 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None;
John Reck5cca8f22018-12-10 17:06:22 -080076 }
77
Nader Jawad7612c612022-11-15 10:55:54 -080078 virtual void setHardwareBuffer(AHardwareBuffer* buffer) override;
79 bool hasHardwareBuffer() override { return mHardwareBuffer != nullptr; }
80
Stan Iliev500a0c32016-10-26 10:30:09 -040081protected:
Nader Jawad7612c612022-11-15 10:55:54 -080082 sk_sp<SkSurface> getBufferSkSurface(
83 const renderthread::HardwareBufferRenderParams& bufferParams);
Matt Sarett4bda6bf2016-11-07 15:43:41 -050084 void dumpResourceCacheUsage() const;
85
Stan Iliev500a0c32016-10-26 10:30:09 -040086 renderthread::RenderThread& mRenderThread;
Derek Sollenberger1863d942020-02-05 15:41:51 -050087
Nader Jawad7612c612022-11-15 10:55:54 -080088 AHardwareBuffer* mHardwareBuffer = nullptr;
89 sk_sp<SkSurface> mBufferSurface = nullptr;
90 sk_sp<SkColorSpace> mBufferColorSpace = nullptr;
91
John Reckb36bfdd2020-07-23 13:47:49 -070092 ColorMode mColorMode = ColorMode::Default;
Peiyong Lin189021b2018-09-27 16:41:40 -070093 SkColorType mSurfaceColorType;
94 sk_sp<SkColorSpace> mSurfaceColorSpace;
Stan Iliev500a0c32016-10-26 10:30:09 -040095
John Reck76005182021-06-09 22:43:05 -040096 bool isCapturingSkp() const { return mCaptureMode != CaptureMode::None; }
97
Stan Iliev500a0c32016-10-26 10:30:09 -040098private:
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050099 void renderFrameImpl(const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700100 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -0500101 const Rect& contentDrawBounds, SkCanvas* canvas,
102 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500103
104 /**
105 * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating
106 * how many times it has been drawn.
107 */
Nathaniel Nifongdc19a652019-11-11 11:47:50 -0500108 void renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -0700109 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -0500110 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -0500111
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400112 // Called every frame. Normally returns early with screen canvas.
113 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500114 SkCanvas* tryCapture(SkSurface* surface, RenderNode* root, const LayerUpdateQueue& dirtyLayers);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400115 // Called at the end of every frame, closes the recording if necessary.
Stan Ilieve9d00122017-09-19 12:07:10 -0400116 void endCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400117 // Determine if a new file-based capture should be started.
118 // If so, sets mCapturedFile and mCaptureSequence and returns true.
119 // Should be called every frame when capture is enabled.
120 // sets mCaptureMode.
121 bool shouldStartNewFileCapture();
122 // Set up a multi frame capture.
123 bool setupMultiFrameCapture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400124
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400125 std::vector<sk_sp<SkImage>> mPinnedImages;
Stan Iliev23c38a92017-03-23 00:12:50 -0400126
Stan Ilieve9d00122017-09-19 12:07:10 -0400127 // Block of properties used only for debugging to record a SkPicture and save it in a file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400128 // There are three possible ways of recording drawing commands.
129 enum class CaptureMode {
130 // return to this mode when capture stops.
131 None,
132 // A mode where every frame is recorded into an SkPicture and sent to a provided callback,
133 // until that callback is cleared
134 CallbackAPI,
135 // A mode where a finite number of frames are recorded to a file with
136 // SkMultiPictureDocument
137 MultiFrameSKP,
138 // A mode which records a single frame to a normal SKP file.
139 SingleFrameSKP,
140 };
141 CaptureMode mCaptureMode = CaptureMode::None;
142
Stan Ilieve9d00122017-09-19 12:07:10 -0400143 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400144 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or
145 * SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400146 */
147 std::string mCapturedFile;
148 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400149 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable
150 * only to MultiFrameSKP or SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400151 */
152 int mCaptureSequence = 0;
John Reck322b8ab2019-03-14 13:15:28 -0700153
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400154 // Multi frame serialization stream and writer used when serializing more than one frame.
Ady Abrahamb1547922021-05-10 21:20:25 -0700155 std::unique_ptr<SkSharingSerialContext> mSerialContext; // Must be declared before any other
156 // serializing member
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400157 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
158 sk_sp<SkDocument> mMultiPic;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400159
Stan Ilieve9d00122017-09-19 12:07:10 -0400160 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400161 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or
162 * CallbackAPI modes.
Stan Ilieve9d00122017-09-19 12:07:10 -0400163 */
164 std::unique_ptr<SkPictureRecorder> mRecorder;
John Reck5cca8f22018-12-10 17:06:22 -0800165 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400166
167 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing.
168 // Not used in other recording modes.
John Reck5cca8f22018-12-10 17:06:22 -0800169 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Iliev500a0c32016-10-26 10:30:09 -0400170};
171
172} /* namespace skiapipeline */
173} /* namespace uirenderer */
174} /* namespace android */