blob: 7d575ad01936cad4b61e61b8df04652842a297a6 [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;
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 Reck1bcacfd2017-11-03 10:12:19 -070048 bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070049 ErrorHandler* errorHandler) override;
Stan Iliev500a0c32016-10-26 10:30:09 -040050
Derek Sollenberger25833d22019-01-14 13:55:55 -050051 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; }
Peiyong Lin189021b2018-09-27 16:41:40 -070052 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; }
53
Stan Iliev500a0c32016-10-26 10:30:09 -040054 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070055 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050056 const Rect& contentDrawBounds, sk_sp<SkSurface> surface,
57 const SkMatrix& preTransform);
Stan Iliev500a0c32016-10-26 10:30:09 -040058
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
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040063 // Sets the recording callback to the provided function and the recording mode
64 // to CallbackAPI
John Reck5cca8f22018-12-10 17:06:22 -080065 void setPictureCapturedCallback(
66 const std::function<void(sk_sp<SkPicture>&&)>& callback) override {
67 mPictureCapturedCallback = callback;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040068 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None;
John Reck5cca8f22018-12-10 17:06:22 -080069 }
70
Stan Iliev500a0c32016-10-26 10:30:09 -040071protected:
Matt Sarett4bda6bf2016-11-07 15:43:41 -050072 void dumpResourceCacheUsage() const;
Peiyong Lin3bff1352018-12-11 07:56:07 -080073 void setSurfaceColorProperties(renderthread::ColorMode colorMode);
Matt Sarett4bda6bf2016-11-07 15:43:41 -050074
Stan Iliev500a0c32016-10-26 10:30:09 -040075 renderthread::RenderThread& mRenderThread;
Peiyong Lin189021b2018-09-27 16:41:40 -070076 SkColorType mSurfaceColorType;
77 sk_sp<SkColorSpace> mSurfaceColorSpace;
Stan Iliev500a0c32016-10-26 10:30:09 -040078
79private:
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050080 void renderFrameImpl(const SkRect& clip,
Peiyong Lin1f6aa122018-09-10 16:28:08 -070081 const std::vector<sp<RenderNode>>& nodes, bool opaque,
Greg Danielc4076782019-01-08 16:01:18 -050082 const Rect& contentDrawBounds, SkCanvas* canvas,
83 const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050084
85 /**
86 * Debugging feature. Draws a semi-transparent overlay on each pixel, indicating
87 * how many times it has been drawn.
88 */
Nathaniel Nifongdc19a652019-11-11 11:47:50 -050089 void renderOverdraw(const SkRect& clip,
John Reck1bcacfd2017-11-03 10:12:19 -070090 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds,
Greg Danielc4076782019-01-08 16:01:18 -050091 sk_sp<SkSurface> surface, const SkMatrix& preTransform);
Matt Sarettf58cc922016-11-14 18:33:38 -050092
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040093 // Called every frame. Normally returns early with screen canvas.
94 // But when capture is enabled, returns an nwaycanvas where commands are also recorded.
Stan Ilieve9d00122017-09-19 12:07:10 -040095 SkCanvas* tryCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040096 // Called at the end of every frame, closes the recording if necessary.
Stan Ilieve9d00122017-09-19 12:07:10 -040097 void endCapture(SkSurface* surface);
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -040098 // Determine if a new file-based capture should be started.
99 // If so, sets mCapturedFile and mCaptureSequence and returns true.
100 // Should be called every frame when capture is enabled.
101 // sets mCaptureMode.
102 bool shouldStartNewFileCapture();
103 // Set up a multi frame capture.
104 bool setupMultiFrameCapture();
Stan Ilieve9d00122017-09-19 12:07:10 -0400105
Derek Sollenbergerb7d34b62016-11-04 10:46:18 -0400106 std::vector<sk_sp<SkImage>> mPinnedImages;
Stan Iliev23c38a92017-03-23 00:12:50 -0400107
Stan Ilieve9d00122017-09-19 12:07:10 -0400108 // Block of properties used only for debugging to record a SkPicture and save it in a file.
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400109 // There are three possible ways of recording drawing commands.
110 enum class CaptureMode {
111 // return to this mode when capture stops.
112 None,
113 // A mode where every frame is recorded into an SkPicture and sent to a provided callback,
114 // until that callback is cleared
115 CallbackAPI,
116 // A mode where a finite number of frames are recorded to a file with
117 // SkMultiPictureDocument
118 MultiFrameSKP,
119 // A mode which records a single frame to a normal SKP file.
120 SingleFrameSKP,
121 };
122 CaptureMode mCaptureMode = CaptureMode::None;
123
Stan Ilieve9d00122017-09-19 12:07:10 -0400124 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400125 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or
126 * SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400127 */
128 std::string mCapturedFile;
129 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400130 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable
131 * only to MultiFrameSKP or SingleFrameSKP mode.
Stan Ilieve9d00122017-09-19 12:07:10 -0400132 */
133 int mCaptureSequence = 0;
John Reck322b8ab2019-03-14 13:15:28 -0700134
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400135 // Multi frame serialization stream and writer used when serializing more than one frame.
136 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream;
137 sk_sp<SkDocument> mMultiPic;
138 std::unique_ptr<SkSharingSerialContext> mSerialContext;
139
Stan Ilieve9d00122017-09-19 12:07:10 -0400140 /**
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400141 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or
142 * CallbackAPI modes.
Stan Ilieve9d00122017-09-19 12:07:10 -0400143 */
144 std::unique_ptr<SkPictureRecorder> mRecorder;
John Reck5cca8f22018-12-10 17:06:22 -0800145 std::unique_ptr<SkNWayCanvas> mNwayCanvas;
Nathaniel Nifongd2e49a22019-06-24 15:07:34 -0400146
147 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing.
148 // Not used in other recording modes.
John Reck5cca8f22018-12-10 17:06:22 -0800149 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback;
Stan Iliev500a0c32016-10-26 10:30:09 -0400150};
151
152} /* namespace skiapipeline */
153} /* namespace uirenderer */
154} /* namespace android */