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