1 /*
2  * Copyright 2019 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 <cstdint>
20 #include <vector>
21 
22 #include <ui/Fence.h>
23 #include <ui/GraphicTypes.h>
24 #include <ui/Size.h>
25 #include <utils/Errors.h>
26 #include <utils/StrongPointer.h>
27 
28 namespace android {
29 
30 class GraphicBuffer;
31 
32 namespace compositionengine {
33 
34 /**
35  * Encapsulates everything for composing to a render surface with RenderEngine
36  */
37 class RenderSurface {
38 public:
39     virtual ~RenderSurface();
40 
41     // Returns true if the render surface is valid. This is meant to be checked
42     // post-construction and prior to use, as not everything is set up by the
43     // constructor.
44     virtual bool isValid() const = 0;
45 
46     // Performs one-time initialization of the render surface. This is meant
47     // to be called after the validation check.
48     virtual void initialize() = 0;
49 
50     // Returns the bounds of the surface
51     virtual const ui::Size& getSize() const = 0;
52 
53     // Returns whether the surface is protected.
54     virtual bool isProtected() const = 0;
55 
56     // Gets the latest fence to pass to the HWC to signal that the surface
57     // buffer is done rendering
58     virtual const sp<Fence>& getClientTargetAcquireFence() const = 0;
59 
60     // Sets the size of the surface
61     virtual void setDisplaySize(const ui::Size&) = 0;
62 
63     // Sets the dataspace used for rendering the surface
64     virtual void setBufferDataspace(ui::Dataspace) = 0;
65 
66     // Sets the pixel format used for rendering the surface.
67     // Changing the pixel format of the buffer will result in buffer
68     // reallocation as well as some reconfiguration of the graphics context,
69     // which are both expensive operations.
70     virtual void setBufferPixelFormat(ui::PixelFormat) = 0;
71 
72     // Configures the protected rendering on the surface
73     virtual void setProtected(bool useProtected) = 0;
74 
75     // Called to signal that rendering has started. 'mustRecompose' should be
76     // true if the entire frame must be recomposed.
77     virtual status_t beginFrame(bool mustRecompose) = 0;
78 
79     // Prepares the frame for rendering
80     virtual void prepareFrame(bool usesClientComposition, bool usesDeviceComposition) = 0;
81 
82     // Allocates a buffer as scratch space for GPU composition
83     virtual sp<GraphicBuffer> dequeueBuffer(base::unique_fd* bufferFence) = 0;
84 
85     // Queues the drawn buffer for consumption by HWC. readyFence is the fence
86     // which will fire when the buffer is ready for consumption.
87     virtual void queueBuffer(base::unique_fd readyFence) = 0;
88 
89     // Called after the HWC calls are made to present the display
90     virtual void onPresentDisplayCompleted() = 0;
91 
92     // Called after the surface has been rendering to signal the surface should
93     // be made ready for displaying
94     virtual void flip() = 0;
95 
96     // Debugging - Dumps the state of the RenderSurface to a string
97     virtual void dump(std::string& result) const = 0;
98 
99     // Debugging - gets the page flip count for the RenderSurface
100     virtual std::uint32_t getPageFlipCount() const = 0;
101 };
102 
103 } // namespace compositionengine
104 } // namespace android
105