• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 #ifndef SF_RENDERENGINE_H_
18 #define SF_RENDERENGINE_H_
19 
20 #include <stdint.h>
21 #include <sys/types.h>
22 #include <memory>
23 
24 #include <android-base/unique_fd.h>
25 #include <math/mat4.h>
26 #include <renderengine/DisplaySettings.h>
27 #include <renderengine/Framebuffer.h>
28 #include <renderengine/Image.h>
29 #include <renderengine/LayerSettings.h>
30 #include <ui/GraphicTypes.h>
31 #include <ui/Transform.h>
32 
33 /**
34  * Allows to set RenderEngine backend to GLES (default) or Vulkan (NOT yet supported).
35  */
36 #define PROPERTY_DEBUG_RENDERENGINE_BACKEND "debug.renderengine.backend"
37 
38 struct ANativeWindowBuffer;
39 
40 namespace android {
41 
42 class Rect;
43 class Region;
44 
45 namespace renderengine {
46 
47 class BindNativeBufferAsFramebuffer;
48 class Image;
49 class Mesh;
50 class Texture;
51 
52 namespace impl {
53 class RenderEngine;
54 }
55 
56 enum class Protection {
57     UNPROTECTED = 1,
58     PROTECTED = 2,
59 };
60 
61 class RenderEngine {
62 public:
63     enum FeatureFlag {
64         USE_COLOR_MANAGEMENT = 1 << 0,      // Device manages color
65         USE_HIGH_PRIORITY_CONTEXT = 1 << 1, // Use high priority context
66 
67         // Create a protected context when if possible
68         ENABLE_PROTECTED_CONTEXT = 1 << 2,
69     };
70 
71     static std::unique_ptr<impl::RenderEngine> create(int hwcFormat, uint32_t featureFlags,
72                                                       uint32_t imageCacheSize);
73 
74     virtual ~RenderEngine() = 0;
75 
76     // ----- BEGIN DEPRECATED INTERFACE -----
77     // This interface, while still in use until a suitable replacement is built,
78     // should be considered deprecated, minus some methods which still may be
79     // used to support legacy behavior.
80 
81     virtual std::unique_ptr<Framebuffer> createFramebuffer() = 0;
82     virtual std::unique_ptr<Image> createImage() = 0;
83 
84     virtual void primeCache() const = 0;
85 
86     // dump the extension strings. always call the base class.
87     virtual void dump(std::string& result) = 0;
88 
89     virtual bool useNativeFenceSync() const = 0;
90     virtual bool useWaitSync() const = 0;
91 
92     virtual bool isCurrent() const = 0;
93 
94     // helpers
95     // flush submits RenderEngine command stream for execution and returns a
96     // native fence fd that is signaled when the execution has completed.  It
97     // returns -1 on errors.
98     virtual base::unique_fd flush() = 0;
99     // finish waits until RenderEngine command stream has been executed.  It
100     // returns false on errors.
101     virtual bool finish() = 0;
102     // waitFence inserts a wait on an external fence fd to RenderEngine
103     // command stream.  It returns false on errors.
104     virtual bool waitFence(base::unique_fd fenceFd) = 0;
105 
106     virtual void clearWithColor(float red, float green, float blue, float alpha) = 0;
107     virtual void fillRegionWithColor(const Region& region, float red, float green, float blue,
108                                      float alpha) = 0;
109     virtual void genTextures(size_t count, uint32_t* names) = 0;
110     virtual void deleteTextures(size_t count, uint32_t const* names) = 0;
111     virtual void bindExternalTextureImage(uint32_t texName, const Image& image) = 0;
112     // Legacy public method used by devices that don't support native fence
113     // synchronization in their GPU driver, as this method provides implicit
114     // synchronization for latching buffers.
115     virtual status_t bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
116                                                const sp<Fence>& fence) = 0;
117     // Caches Image resources for this buffer, but does not bind the buffer to
118     // a particular texture.
119     virtual status_t cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) = 0;
120     // Removes internal resources referenced by the bufferId. This method should be
121     // invoked when the caller will no longer hold a reference to a GraphicBuffer
122     // and needs to clean up its resources.
123     virtual void unbindExternalTextureBuffer(uint64_t bufferId) = 0;
124     // When binding a native buffer, it must be done before setViewportAndProjection
125     // Returns NO_ERROR when binds successfully, NO_MEMORY when there's no memory for allocation.
126     virtual status_t bindFrameBuffer(Framebuffer* framebuffer) = 0;
127     virtual void unbindFrameBuffer(Framebuffer* framebuffer) = 0;
128 
129     // set-up
130     virtual void checkErrors() const = 0;
131     virtual void setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
132                                           ui::Transform::orientation_flags rotation) = 0;
133     virtual void setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
134                                     const half4& color, float cornerRadius) = 0;
135     virtual void setupLayerTexturing(const Texture& texture) = 0;
136     virtual void setupLayerBlackedOut() = 0;
137     virtual void setupFillWithColor(float r, float g, float b, float a) = 0;
138     // Sets up the crop size for corner radius clipping.
139     //
140     // Having corner radius will force GPU composition on the layer and its children, drawing it
141     // with a special shader. The shader will receive the radius and the crop rectangle as input,
142     // modifying the opacity of the destination texture, multiplying it by a number between 0 and 1.
143     // We query Layer#getRoundedCornerState() to retrieve the radius as well as the rounded crop
144     // rectangle to figure out how to apply the radius for this layer. The crop rectangle will be
145     // in local layer coordinate space, so we have to take the layer transform into account when
146     // walking up the tree.
147     virtual void setupCornerRadiusCropSize(float width, float height) = 0;
148 
149     // Set a color transform matrix that is applied in linear space right before OETF.
150     virtual void setColorTransform(const mat4& /* colorTransform */) = 0;
151     virtual void disableTexturing() = 0;
152     virtual void disableBlending() = 0;
153 
154     // HDR and color management support
155     virtual void setSourceY410BT2020(bool enable) = 0;
156     virtual void setSourceDataSpace(ui::Dataspace source) = 0;
157     virtual void setOutputDataSpace(ui::Dataspace dataspace) = 0;
158     virtual void setDisplayMaxLuminance(const float maxLuminance) = 0;
159 
160     // drawing
161     virtual void drawMesh(const Mesh& mesh) = 0;
162 
163     // queries
164     virtual size_t getMaxTextureSize() const = 0;
165     virtual size_t getMaxViewportDims() const = 0;
166 
167     // ----- END DEPRECATED INTERFACE -----
168 
169     // ----- BEGIN NEW INTERFACE -----
170 
171     virtual bool isProtected() const = 0;
172     virtual bool supportsProtectedContent() const = 0;
173     virtual bool useProtectedContext(bool useProtectedContext) = 0;
174 
175     // Renders layers for a particular display via GPU composition. This method
176     // should be called for every display that needs to be rendered via the GPU.
177     // @param display The display-wide settings that should be applied prior to
178     // drawing any layers.
179     // @param layers The layers to draw onto the display, in Z-order.
180     // @param buffer The buffer which will be drawn to. This buffer will be
181     // ready once drawFence fires.
182     // @param useFramebufferCache True if the framebuffer cache should be used.
183     // If an implementation does not cache output framebuffers, then this
184     // parameter does nothing.
185     // @param bufferFence Fence signalling that the buffer is ready to be drawn
186     // to.
187     // @param drawFence A pointer to a fence, which will fire when the buffer
188     // has been drawn to and is ready to be examined. The fence will be
189     // initialized by this method. The caller will be responsible for owning the
190     // fence.
191     // @return An error code indicating whether drawing was successful. For
192     // now, this always returns NO_ERROR.
193     virtual status_t drawLayers(const DisplaySettings& display,
194                                 const std::vector<LayerSettings>& layers,
195                                 ANativeWindowBuffer* buffer, const bool useFramebufferCache,
196                                 base::unique_fd&& bufferFence, base::unique_fd* drawFence) = 0;
197 
198 protected:
199     // Gets a framebuffer to render to. This framebuffer may or may not be
200     // cached depending on the implementation.
201     //
202     // Note that this method does not transfer ownership, so the caller most not
203     // live longer than RenderEngine.
204     virtual Framebuffer* getFramebufferForDrawing() = 0;
205     friend class BindNativeBufferAsFramebuffer;
206 };
207 
208 class BindNativeBufferAsFramebuffer {
209 public:
BindNativeBufferAsFramebuffer(RenderEngine & engine,ANativeWindowBuffer * buffer,const bool useFramebufferCache)210     BindNativeBufferAsFramebuffer(RenderEngine& engine, ANativeWindowBuffer* buffer,
211                                   const bool useFramebufferCache)
212           : mEngine(engine), mFramebuffer(mEngine.getFramebufferForDrawing()), mStatus(NO_ERROR) {
213         mStatus = mFramebuffer->setNativeWindowBuffer(buffer, mEngine.isProtected(),
214                                                       useFramebufferCache)
215                 ? mEngine.bindFrameBuffer(mFramebuffer)
216                 : NO_MEMORY;
217     }
~BindNativeBufferAsFramebuffer()218     ~BindNativeBufferAsFramebuffer() {
219         mFramebuffer->setNativeWindowBuffer(nullptr, false, /*arbitrary*/ true);
220         mEngine.unbindFrameBuffer(mFramebuffer);
221     }
getStatus()222     status_t getStatus() const { return mStatus; }
223 
224 private:
225     RenderEngine& mEngine;
226     Framebuffer* mFramebuffer;
227     status_t mStatus;
228 };
229 
230 namespace impl {
231 
232 // impl::RenderEngine contains common implementation that is graphics back-end agnostic.
233 class RenderEngine : public renderengine::RenderEngine {
234 public:
235     virtual ~RenderEngine() = 0;
236 
237     bool useNativeFenceSync() const override;
238     bool useWaitSync() const override;
239 
240 protected:
241     RenderEngine(uint32_t featureFlags);
242     const uint32_t mFeatureFlags;
243 };
244 
245 } // namespace impl
246 } // namespace renderengine
247 } // namespace android
248 
249 #endif /* SF_RENDERENGINE_H_ */
250