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