blob: 37b559f92f05fda27a120a80d482917e8b02062e [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
Stan Ilieve9d00122017-09-19 12:07:10 -040027class SkPictureRecorder;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040028struct SkSharingSerialContext;
Stan Ilieve9d00122017-09-19 12:07:10 -040029
Stan Iliev500a0c32016-10-26 10:30:09 -040030namespace android {
31namespace uirenderer {
32namespace skiapipeline {
33
34class SkiaPipeline : public renderthread::IRenderPipeline {
35public:
Chih-Hung Hsiehf9336412018-12-20 13:48:57 -080036 explicit SkiaPipeline(renderthread::RenderThread& thread);
Stan Iliev232f3622017-08-23 17:15:09 -040037 virtual ~SkiaPipeline();
Stan Iliev500a0c32016-10-26 10:30:09 -040038
Stan Iliev500a0c32016-10-26 10:30:09 -040039 void onDestroyHardwareResources() override;
40
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040041 bool pinImages(std::vector<SkImage*>& mutableImages) override;
42 bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; }
43 void unpinImages() override;
Stan Iliev47fed6ba2017-10-18 17:56:43 -040044 void onPrepareTree() override;
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -040045
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
John Reck1bcacfd2017-11-03 10:12:19 -070049 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070050 ErrorHandler* errorHandler) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040051
Derek Sollenberger25833d22019-01-14 13:55:55 -050052 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; }
Peiyong Lin189021b2018-09-27 16:41:40 -070053 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; }
54
Stan Iliev500a0c32016-10-26 10:30:09 -040055 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070056 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050057 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
58 const SkMatrix& preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -040059
Stan Iliev23c38a92017-03-23 00:12:50 -040060 std::vector<VectorDrawableRoot*>* getVectorDrawables() { return &mVectorDrawables; }
61
Stan Iliev500a0c32016-10-26 10:30:09 -040062 static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap);
63
Peiyong Lin1f6aa122018-09-10 16:28:08 -070064 void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque);
Stan Iliev500a0c32016-10-26 10:30:09 -040065
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040066 // Sets the recording callback to the provided function and the recording mode
67 // to CallbackAPI
John Reck5cca8f22018-12-10 17:06:22 -080068 void setPictureCapturedCallback(
69 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
70 mPictureCapturedCallback = callback;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040071 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None;
John Reck5cca8f22018-12-10 17:06:22 -080072 }
73
Stan Iliev500a0c32016-10-26 10:30:09 -040074protected:
Matt Sarett4bda6bf2016-11-07 15:43:41 -050075 void dumpResourceCacheUsage() const;
Peiyong Lin3bff1352018-12-11 07:56:07 -080076 void setSurfaceColorProperties(renderthread::ColorMode colorMode);
Matt Sarett4bda6bf2016-11-07 15:43:41 -050077
Stan Iliev500a0c32016-10-26 10:30:09 -040078 renderthread::RenderThread& mRenderThread;
Peiyong Lin189021b2018-09-27 16:41:40 -070079 SkColorType mSurfaceColorType;
80 sk_sp<SkColorSpace> mSurfaceColorSpace;
Stan Iliev500a0c32016-10-26 10:30:09 -040081
82private:
Matt Sarettf58cc922016-11-14 18:33:38 -050083 void renderFrameImpl(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070084 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050085 const Rect& contentDrawBounds, SkCanvas* canvas,
86 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050087
88 /**
89 * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating
90 * how many times it has been drawn.
91 */
92 void renderOverdraw(const LayerUpdateQueue& layers, const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -070093 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -050094 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050095
Stan Iliev23c38a92017-03-23 00:12:50 -040096 /**
97 * Render mVectorDrawables into offscreen buffers.
98 */
99 void renderVectorDrawableCache();
100
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400101 // Called every frame. Normally returns early with screen canvas.
102 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
Stan Ilieve9d00122017-09-19 12:07:10 -0400103 SkCanvas* tryCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400104 // Called at the end of every frame, closes the recording if necessary.
Stan Ilieve9d00122017-09-19 12:07:10 -0400105 void endCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400106 // Determine if a new file-based capture should be started.
107 // If so, sets mCapturedFile and mCaptureSequence and returns true.
108 // Should be called every frame when capture is enabled.
109 // sets mCaptureMode.
110 bool shouldStartNewFileCapture();
111 // Set up a multi frame capture.
112 bool setupMultiFrameCapture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400113
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400114 std::vector<sk_sp<SkImage>> mPinnedImages;
Stan Iliev23c38a92017-03-23 00:12:50 -0400115
116 /**
117 * populated by prepareTree with dirty VDs
118 */
119 std::vector<VectorDrawableRoot*> mVectorDrawables;
Stan Ilieve9d00122017-09-19 12:07:10 -0400120
Stan Ilieve9d00122017-09-19 12:07:10 -0400121 // Block of properties used only for debugging to record a SkPicture and save it in a file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400122 // There are three possible ways of recording drawing commands.
123 enum class CaptureMode {
124 // return to this mode when capture stops.
125 None,
126 // A mode where every frame is recorded into an SkPicture and sent to a provided callback,
127 // until that callback is cleared
128 CallbackAPI,
129 // A mode where a finite number of frames are recorded to a file with
130 // SkMultiPictureDocument
131 MultiFrameSKP,
132 // A mode which records a single frame to a normal SKP file.
133 SingleFrameSKP,
134 };
135 CaptureMode mCaptureMode = CaptureMode::None;
136
Stan Ilieve9d00122017-09-19 12:07:10 -0400137 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400138 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or
139 * SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400140 */
141 std::string mCapturedFile;
142 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400143 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable
144 * only to MultiFrameSKP or SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400145 */
146 int mCaptureSequence = 0;
John Reck322b8ab2019-03-14 13:15:28 -0700147
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400148 // Multi frame serialization stream and writer used when serializing more than one frame.
149 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
150 sk_sp<SkDocument> mMultiPic;
151 std::unique_ptr<SkSharingSerialContext> mSerialContext;
152
Stan Ilieve9d00122017-09-19 12:07:10 -0400153 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400154 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or
155 * CallbackAPI modes.
Stan Ilieve9d00122017-09-19 12:07:10 -0400156 */
157 std::unique_ptr<SkPictureRecorder> mRecorder;
John Reck5cca8f22018-12-10 17:06:22 -0800158 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400159
160 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing.
161 // Not used in other recording modes.
John Reck5cca8f22018-12-10 17:06:22 -0800162 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Iliev500a0c32016-10-26 10:30:09 -0400163};
164
165} /* namespace skiapipeline */
166} /* namespace uirenderer */
167} /* namespace android */