blob: 370c7b379de28f5084e6b61c18e99644062aacfd [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>
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040020#include <SkDocument.h>
21#include <SkMultiPictureDocument.h>
John Reckd9d7f122018-05-03 14:40:56 -070022#include "Lighting.h"
Derek Sollenberger2d142132018-01-22 10:25:26 -050023#include "hwui/AnimatedImageDrawable.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "renderthread/CanvasContext.h"
25#include "renderthread/IRenderPipeline.h"
Stan Iliev500a0c32016-10-26 10:30:09 -040026
Kevin Lubick1175dc02022-02-28 12:41:27 -050027class SkFILEWStream;
Stan Ilieve9d00122017-09-19 12:07:10 -040028class SkPictureRecorder;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040029struct SkSharingSerialContext;
Stan Ilieve9d00122017-09-19 12:07:10 -040030
Stan Iliev500a0c32016-10-26 10:30:09 -040031namespace android {
32namespace uirenderer {
33namespace skiapipeline {
34
35class SkiaPipeline : public renderthread::IRenderPipeline {
36public:
Chih-Hung Hsiehf9336412018-12-20 13:48:57 -080037 explicit SkiaPipeline(renderthread::RenderThread& thread);
Stan Iliev232f3622017-08-23 17:15:09 -040038 virtual ~SkiaPipeline();
Stan Iliev500a0c32016-10-26 10:30:09 -040039
Stan Iliev500a0c32016-10-26 10:30:09 -040040 void onDestroyHardwareResources() override;
41
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040042 bool pinImages(std::vector<SkImage*>& mutableImages) override;
43 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
44 void unpinImages() override;
45
Peiyong Lin1f6aa122018-09-10 16:28:08 -070046 void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue,
47 bool opaque, const LightInfo& lightInfo) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040048
Nathaniel Nifong2945bff2019-11-25 09:34:21 -050049 // If the given node didn't have a layer surface, or had one of the wrong size, this method
50 // creates a new one and returns true. Otherwise does nothing and returns false.
John Reck1bcacfd2017-11-03 10:12:19 -070051 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070052 ErrorHandler* errorHandler) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040053
John Reckb36bfdd2020-07-23 13:47:49 -070054 void setSurfaceColorProperties(ColorMode colorMode) override;
Derek Sollenberger25833d22019-01-14 13:55:55 -050055 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; }
Peiyong Lin189021b2018-09-27 16:41:40 -070056 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; }
57
Stan Iliev500a0c32016-10-26 10:30:09 -040058 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070059 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050060 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
61 const SkMatrix& preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -040062
Stan Iliev500a0c32016-10-26 10:30:09 -040063 static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
64
Peiyong Lin1f6aa122018-09-10 16:28:08 -070065 void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040066
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040067 // Sets the recording callback to the provided function and the recording mode
68 // to CallbackAPI
John Reck5cca8f22018-12-10 17:06:22 -080069 void setPictureCapturedCallback(
70 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
71 mPictureCapturedCallback = callback;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040072 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None;
John Reck5cca8f22018-12-10 17:06:22 -080073 }
74
Stan Iliev500a0c32016-10-26 10:30:09 -040075protected:
Matt Sarett4bda6bf2016-11-07 15:43:41 -050076 void dumpResourceCacheUsage() const;
77
Stan Iliev500a0c32016-10-26 10:30:09 -040078 renderthread::RenderThread& mRenderThread;
Derek Sollenberger1863d942020-02-05 15:41:51 -050079
John Reckb36bfdd2020-07-23 13:47:49 -070080 ColorMode mColorMode = ColorMode::Default;
Peiyong Lin189021b2018-09-27 16:41:40 -070081 SkColorType mSurfaceColorType;
82 sk_sp<SkColorSpace> mSurfaceColorSpace;
Stan Iliev500a0c32016-10-26 10:30:09 -040083
John Reck76005182021-06-09 22:43:05 -040084 bool isCapturingSkp() const { return mCaptureMode != CaptureMode::None; }
85
Stan Iliev500a0c32016-10-26 10:30:09 -040086private:
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050087 void renderFrameImpl(const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070088 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050089 const Rect& contentDrawBounds, SkCanvas* canvas,
90 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050091
92 /**
93 * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating
94 * how many times it has been drawn.
95 */
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050096 void renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -070097 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -050098 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050099
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400100 // Called every frame. Normally returns early with screen canvas.
101 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
Nathaniel Nifong2945bff2019-11-25 09:34:21 -0500102 SkCanvas* tryCapture(SkSurface* surface, RenderNode* root, const LayerUpdateQueue& dirtyLayers);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400103 // Called at the end of every frame, closes the recording if necessary.
Stan Ilieve9d00122017-09-19 12:07:10 -0400104 void endCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400105 // Determine if a new file-based capture should be started.
106 // If so, sets mCapturedFile and mCaptureSequence and returns true.
107 // Should be called every frame when capture is enabled.
108 // sets mCaptureMode.
109 bool shouldStartNewFileCapture();
110 // Set up a multi frame capture.
111 bool setupMultiFrameCapture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400112
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400113 std::vector<sk_sp<SkImage>> mPinnedImages;
Stan Iliev23c38a92017-03-23 00:12:50 -0400114
Stan Ilieve9d00122017-09-19 12:07:10 -0400115 // Block of properties used only for debugging to record a SkPicture and save it in a file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400116 // There are three possible ways of recording drawing commands.
117 enum class CaptureMode {
118 // return to this mode when capture stops.
119 None,
120 // A mode where every frame is recorded into an SkPicture and sent to a provided callback,
121 // until that callback is cleared
122 CallbackAPI,
123 // A mode where a finite number of frames are recorded to a file with
124 // SkMultiPictureDocument
125 MultiFrameSKP,
126 // A mode which records a single frame to a normal SKP file.
127 SingleFrameSKP,
128 };
129 CaptureMode mCaptureMode = CaptureMode::None;
130
Stan Ilieve9d00122017-09-19 12:07:10 -0400131 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400132 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or
133 * SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400134 */
135 std::string mCapturedFile;
136 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400137 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable
138 * only to MultiFrameSKP or SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400139 */
140 int mCaptureSequence = 0;
John Reck322b8ab2019-03-14 13:15:28 -0700141
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400142 // Multi frame serialization stream and writer used when serializing more than one frame.
Ady Abrahamb1547922021-05-10 21:20:25 -0700143 std::unique_ptr<SkSharingSerialContext> mSerialContext; // Must be declared before any other
144 // serializing member
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400145 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
146 sk_sp<SkDocument> mMultiPic;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400147
Stan Ilieve9d00122017-09-19 12:07:10 -0400148 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400149 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or
150 * CallbackAPI modes.
Stan Ilieve9d00122017-09-19 12:07:10 -0400151 */
152 std::unique_ptr<SkPictureRecorder> mRecorder;
John Reck5cca8f22018-12-10 17:06:22 -0800153 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400154
155 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing.
156 // Not used in other recording modes.
John Reck5cca8f22018-12-10 17:06:22 -0800157 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Iliev500a0c32016-10-26 10:30:09 -0400158};
159
160} /* namespace skiapipeline */
161} /* namespace uirenderer */
162} /* namespace android */