| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 1 | /* | 
|  | 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 Lubick | 07d6aae | 2022-04-01 14:03:11 -0400 | [diff] [blame] | 19 | #include <SkColorSpace.h> | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 20 | #include <SkDocument.h> | 
|  | 21 | #include <SkMultiPictureDocument.h> | 
| Kevin Lubick | 07d6aae | 2022-04-01 14:03:11 -0400 | [diff] [blame] | 22 | #include <SkSurface.h> | 
| John Reck | d9d7f12 | 2018-05-03 14:40:56 -0700 | [diff] [blame] | 23 | #include "Lighting.h" | 
| Derek Sollenberger | 2d14213 | 2018-01-22 10:25:26 -0500 | [diff] [blame] | 24 | #include "hwui/AnimatedImageDrawable.h" | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | #include "renderthread/CanvasContext.h" | 
|  | 26 | #include "renderthread/IRenderPipeline.h" | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 27 |  | 
| Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 28 | class SkFILEWStream; | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 29 | class SkPictureRecorder; | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 30 | struct SkSharingSerialContext; | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 31 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 32 | namespace android { | 
|  | 33 | namespace uirenderer { | 
|  | 34 | namespace skiapipeline { | 
|  | 35 |  | 
|  | 36 | class SkiaPipeline : public renderthread::IRenderPipeline { | 
|  | 37 | public: | 
| Chih-Hung Hsieh | f933641 | 2018-12-20 13:48:57 -0800 | [diff] [blame] | 38 | explicit SkiaPipeline(renderthread::RenderThread& thread); | 
| Stan Iliev | 232f362 | 2017-08-23 17:15:09 -0400 | [diff] [blame] | 39 | virtual ~SkiaPipeline(); | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 40 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 41 | void onDestroyHardwareResources() override; | 
|  | 42 |  | 
| Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 43 | bool pinImages(std::vector<SkImage*>& mutableImages) override; | 
|  | 44 | bool pinImages(LsaVector<sk_sp<Bitmap>>& images) override { return false; } | 
|  | 45 | void unpinImages() override; | 
|  | 46 |  | 
| Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 47 | void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, | 
|  | 48 | bool opaque, const LightInfo& lightInfo) override; | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 49 |  | 
| Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 50 | // If the given node didn't have a layer surface, or had one of the wrong size, this method | 
|  | 51 | // creates a new one and returns true. Otherwise does nothing and returns false. | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 52 | bool createOrUpdateLayer(RenderNode* node, const DamageAccumulator& damageAccumulator, | 
| Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 53 | ErrorHandler* errorHandler) override; | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 54 |  | 
| John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 55 | void setSurfaceColorProperties(ColorMode colorMode) override; | 
| Derek Sollenberger | 25833d2 | 2019-01-14 13:55:55 -0500 | [diff] [blame] | 56 | SkColorType getSurfaceColorType() const override { return mSurfaceColorType; } | 
| Peiyong Lin | 189021b | 2018-09-27 16:41:40 -0700 | [diff] [blame] | 57 | sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; } | 
|  | 58 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 59 | void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, | 
| Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 60 | const std::vector<sp<RenderNode>>& nodes, bool opaque, | 
| Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 61 | const Rect& contentDrawBounds, sk_sp<SkSurface> surface, | 
|  | 62 | const SkMatrix& preTransform); | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 63 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 64 | static void prepareToDraw(const renderthread::RenderThread& thread, Bitmap* bitmap); | 
|  | 65 |  | 
| Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 66 | void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque); | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 67 |  | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 68 | // Sets the recording callback to the provided function and the recording mode | 
|  | 69 | // to CallbackAPI | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 70 | void setPictureCapturedCallback( | 
|  | 71 | const std::function<void(sk_sp<SkPicture>&&)>& callback) override { | 
|  | 72 | mPictureCapturedCallback = callback; | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 73 | mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None; | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 76 | protected: | 
| Matt Sarett | 4bda6bf | 2016-11-07 15:43:41 -0500 | [diff] [blame] | 77 | void dumpResourceCacheUsage() const; | 
|  | 78 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 79 | renderthread::RenderThread& mRenderThread; | 
| Derek Sollenberger | 1863d94 | 2020-02-05 15:41:51 -0500 | [diff] [blame] | 80 |  | 
| John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 81 | ColorMode mColorMode = ColorMode::Default; | 
| Peiyong Lin | 189021b | 2018-09-27 16:41:40 -0700 | [diff] [blame] | 82 | SkColorType mSurfaceColorType; | 
|  | 83 | sk_sp<SkColorSpace> mSurfaceColorSpace; | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 84 |  | 
| John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 85 | bool isCapturingSkp() const { return mCaptureMode != CaptureMode::None; } | 
|  | 86 |  | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 87 | private: | 
| Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 88 | void renderFrameImpl(const SkRect& clip, | 
| Peiyong Lin | 1f6aa12 | 2018-09-10 16:28:08 -0700 | [diff] [blame] | 89 | const std::vector<sp<RenderNode>>& nodes, bool opaque, | 
| Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 90 | const Rect& contentDrawBounds, SkCanvas* canvas, | 
|  | 91 | const SkMatrix& preTransform); | 
| Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 92 |  | 
|  | 93 | /** | 
|  | 94 | *  Debugging feature.  Draws a semi-transparent overlay on each pixel, indicating | 
|  | 95 | *  how many times it has been drawn. | 
|  | 96 | */ | 
| Nathaniel Nifong | dc19a65 | 2019-11-11 11:47:50 -0500 | [diff] [blame] | 97 | void renderOverdraw(const SkRect& clip, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 98 | const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds, | 
| Greg Daniel | c407678 | 2019-01-08 16:01:18 -0500 | [diff] [blame] | 99 | sk_sp<SkSurface> surface, const SkMatrix& preTransform); | 
| Matt Sarett | f58cc92 | 2016-11-14 18:33:38 -0500 | [diff] [blame] | 100 |  | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 101 | // Called every frame. Normally returns early with screen canvas. | 
|  | 102 | // But when capture is enabled, returns an nwaycanvas where commands are also recorded. | 
| Nathaniel Nifong | 2945bff | 2019-11-25 09:34:21 -0500 | [diff] [blame] | 103 | SkCanvas* tryCapture(SkSurface* surface, RenderNode* root, const LayerUpdateQueue& dirtyLayers); | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 104 | // Called at the end of every frame, closes the recording if necessary. | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 105 | void endCapture(SkSurface* surface); | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 106 | // 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 Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 113 |  | 
| Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 114 | std::vector<sk_sp<SkImage>> mPinnedImages; | 
| Stan Iliev | 23c38a9 | 2017-03-23 00:12:50 -0400 | [diff] [blame] | 115 |  | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 116 | // Block of properties used only for debugging to record a SkPicture and save it in a file. | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 117 | // There are three possible ways of recording drawing commands. | 
|  | 118 | enum class CaptureMode { | 
|  | 119 | // return to this mode when capture stops. | 
|  | 120 | None, | 
|  | 121 | // A mode where every frame is recorded into an SkPicture and sent to a provided callback, | 
|  | 122 | // until that callback is cleared | 
|  | 123 | CallbackAPI, | 
|  | 124 | // A mode where a finite number of frames are recorded to a file with | 
|  | 125 | // SkMultiPictureDocument | 
|  | 126 | MultiFrameSKP, | 
|  | 127 | // A mode which records a single frame to a normal SKP file. | 
|  | 128 | SingleFrameSKP, | 
|  | 129 | }; | 
|  | 130 | CaptureMode mCaptureMode = CaptureMode::None; | 
|  | 131 |  | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 132 | /** | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 133 | * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or | 
|  | 134 | * SingleFrameSKP mode. | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 135 | */ | 
|  | 136 | std::string mCapturedFile; | 
|  | 137 | /** | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 138 | * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable | 
|  | 139 | * only to MultiFrameSKP or SingleFrameSKP mode. | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 140 | */ | 
|  | 141 | int mCaptureSequence = 0; | 
| John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 142 |  | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 143 | // Multi frame serialization stream and writer used when serializing more than one frame. | 
| Ady Abraham | b154792 | 2021-05-10 21:20:25 -0700 | [diff] [blame] | 144 | std::unique_ptr<SkSharingSerialContext> mSerialContext;  // Must be declared before any other | 
|  | 145 | // serializing member | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 146 | std::unique_ptr<SkFILEWStream> mOpenMultiPicStream; | 
|  | 147 | sk_sp<SkDocument> mMultiPic; | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 148 |  | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 149 | /** | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 150 | * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or | 
|  | 151 | * CallbackAPI modes. | 
| Stan Iliev | e9d0012 | 2017-09-19 12:07:10 -0400 | [diff] [blame] | 152 | */ | 
|  | 153 | std::unique_ptr<SkPictureRecorder> mRecorder; | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 154 | std::unique_ptr<SkNWayCanvas> mNwayCanvas; | 
| Nathaniel Nifong | d2e49a2 | 2019-06-24 15:07:34 -0400 | [diff] [blame] | 155 |  | 
|  | 156 | // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing. | 
|  | 157 | // Not used in other recording modes. | 
| John Reck | 5cca8f2 | 2018-12-10 17:06:22 -0800 | [diff] [blame] | 158 | std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback; | 
| Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 159 | }; | 
|  | 160 |  | 
|  | 161 | } /* namespace skiapipeline */ | 
|  | 162 | } /* namespace uirenderer */ | 
|  | 163 | } /* namespace android */ |