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 19 #include <SkColorSpace.h> 20 #include <SkDocument.h> 21 #include <SkSurface.h> 22 23 #include "Lighting.h" 24 #include "hwui/AnimatedImageDrawable.h" 25 #include "renderthread/CanvasContext.h" 26 #include "renderthread/HardwareBufferRenderParams.h" 27 #include "renderthread/IRenderPipeline.h" 28 29 class SkFILEWStream; 30 class SkPictureRecorder; 31 struct SkSharingSerialContext; 32 33 namespace android { 34 namespace uirenderer { 35 namespace skiapipeline { 36 37 class SkiaPipeline : public renderthread::IRenderPipeline { 38 public: 39 explicit SkiaPipeline(renderthread::RenderThread& thread); 40 virtual ~SkiaPipeline(); 41 42 void onDestroyHardwareResources() override; 43 44 void renderLayers(const LightGeometry& lightGeometry, LayerUpdateQueue* layerUpdateQueue, 45 bool opaque, const LightInfo& lightInfo) override; 46 47 void setSurfaceColorProperties(ColorMode colorMode) override; getSurfaceColorType()48 SkColorType getSurfaceColorType() const override { return mSurfaceColorType; } getSurfaceColorSpace()49 sk_sp<SkColorSpace> getSurfaceColorSpace() override { return mSurfaceColorSpace; } 50 51 void renderFrame(const LayerUpdateQueue& layers, const SkRect& clip, 52 const std::vector<sp<RenderNode>>& nodes, bool opaque, 53 const Rect& contentDrawBounds, sk_sp<SkSurface> surface, 54 const SkMatrix& preTransform); 55 56 bool renderLayerImpl(RenderNode* layerNode, const Rect& layerDamage); 57 virtual void renderLayersImpl(const LayerUpdateQueue& layers, bool opaque) = 0; 58 59 // Sets the recording callback to the provided function and the recording mode 60 // to CallbackAPI setPictureCapturedCallback(const std::function<void (sk_sp<SkPicture> &&)> & callback)61 void setPictureCapturedCallback( 62 const std::function<void(sk_sp<SkPicture>&&)>& callback) override { 63 mPictureCapturedCallback = callback; 64 mCaptureMode = callback ? CaptureMode::CallbackAPI : CaptureMode::None; 65 } 66 67 void setTargetSdrHdrRatio(float ratio) override; 68 69 protected: 70 renderthread::RenderThread& mRenderThread; 71 72 sk_sp<SkSurface> mBufferSurface = nullptr; 73 sk_sp<SkColorSpace> mBufferColorSpace = nullptr; 74 75 ColorMode mColorMode = ColorMode::Default; 76 SkColorType mSurfaceColorType; 77 sk_sp<SkColorSpace> mSurfaceColorSpace; 78 float mTargetSdrHdrRatio = 1.f; 79 isCapturingSkp()80 bool isCapturingSkp() const { return mCaptureMode != CaptureMode::None; } 81 82 private: 83 void renderFrameImpl(const SkRect& clip, 84 const std::vector<sp<RenderNode>>& nodes, bool opaque, 85 const Rect& contentDrawBounds, SkCanvas* canvas, 86 const SkMatrix& preTransform); 87 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 SkRect& clip, 93 const std::vector<sp<RenderNode>>& nodes, const Rect& contentDrawBounds, 94 sk_sp<SkSurface> surface, const SkMatrix& preTransform); 95 96 // Called every frame. Normally returns early with screen canvas. 97 // But when capture is enabled, returns an nwaycanvas where commands are also recorded. 98 SkCanvas* tryCapture(SkSurface* surface, RenderNode* root, const LayerUpdateQueue& dirtyLayers); 99 // Called at the end of every frame, closes the recording if necessary. 100 void endCapture(SkSurface* surface); 101 // Determine if a new file-based capture should be started. 102 // If so, sets mCapturedFile and mCaptureSequence and returns true. 103 // Should be called every frame when capture is enabled. 104 // sets mCaptureMode. 105 bool shouldStartNewFileCapture(); 106 // Set up a multi frame capture. 107 bool setupMultiFrameCapture(); 108 109 // Block of properties used only for debugging to record a SkPicture and save it in a file. 110 // There are three possible ways of recording drawing commands. 111 enum class CaptureMode { 112 // return to this mode when capture stops. 113 None, 114 // A mode where every frame is recorded into an SkPicture and sent to a provided callback, 115 // until that callback is cleared 116 CallbackAPI, 117 // A mode where a finite number of frames are recorded to a file with 118 // SkMultiPictureDocument 119 MultiFrameSKP, 120 // A mode which records a single frame to a normal SKP file. 121 SingleFrameSKP, 122 }; 123 CaptureMode mCaptureMode = CaptureMode::None; 124 125 /** 126 * mCapturedFile - the filename to write a recorded SKP to in either MultiFrameSKP or 127 * SingleFrameSKP mode. 128 */ 129 std::string mCapturedFile; 130 /** 131 * mCaptureSequence counts down how many frames are left to take in the sequence. Applicable 132 * only to MultiFrameSKP or SingleFrameSKP mode. 133 */ 134 int mCaptureSequence = 0; 135 136 // Multi frame serialization stream and writer used when serializing more than one frame. 137 std::unique_ptr<SkSharingSerialContext> mSerialContext; // Must be declared before any other 138 // serializing member 139 std::unique_ptr<SkFILEWStream> mOpenMultiPicStream; 140 sk_sp<SkDocument> mMultiPic; 141 142 /** 143 * mRecorder holds the current picture recorder when serializing in either SingleFrameSKP or 144 * CallbackAPI modes. 145 */ 146 std::unique_ptr<SkPictureRecorder> mRecorder; 147 std::unique_ptr<SkNWayCanvas> mNwayCanvas; 148 149 // Set by setPictureCapturedCallback and when set, CallbackAPI mode recording is ongoing. 150 // Not used in other recording modes. 151 std::function<void(sk_sp<SkPicture>&&)> mPictureCapturedCallback; 152 }; 153 154 } /* namespace skiapipeline */ 155 } /* namespace uirenderer */ 156 } /* namespace android */ 157