blob: ea30dc64e4def8c2b9d82ac85eb23fc515abbb6d [file] [log] [blame]
John Reck67b1e2b2020-08-26 13:17:24 -07001/*
2 * Copyright 2020 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_SKIAGLRENDERENGINE_H_
18#define SF_SKIAGLRENDERENGINE_H_
19
Alec Mourib5777452020-09-28 11:32:42 -070020#include <EGL/egl.h>
21#include <EGL/eglext.h>
22#include <GLES2/gl2.h>
23#include <GrDirectContext.h>
24#include <SkSurface.h>
25#include <android-base/thread_annotations.h>
26#include <renderengine/RenderEngine.h>
John Reck67b1e2b2020-08-26 13:17:24 -070027#include <sys/types.h>
Alec Mourib5777452020-09-28 11:32:42 -070028
John Reck67b1e2b2020-08-26 13:17:24 -070029#include <mutex>
30#include <unordered_map>
31
Alec Mourib5777452020-09-28 11:32:42 -070032#include "EGL/egl.h"
John Reck67b1e2b2020-08-26 13:17:24 -070033#include "SkiaRenderEngine.h"
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070034#include "filters/BlurFilter.h"
John Reck67b1e2b2020-08-26 13:17:24 -070035
36namespace android {
37namespace renderengine {
38namespace skia {
39
40class SkiaGLRenderEngine : public skia::SkiaRenderEngine {
41public:
42 static std::unique_ptr<SkiaGLRenderEngine> create(const RenderEngineCreationArgs& args);
Lucas Dupind508e472020-11-04 04:32:06 +000043 SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, EGLContext ctxt,
44 EGLSurface placeholder, EGLContext protectedContext,
John Reck67b1e2b2020-08-26 13:17:24 -070045 EGLSurface protectedPlaceholder);
46 ~SkiaGLRenderEngine() override{};
47
48 void unbindExternalTextureBuffer(uint64_t bufferId) override;
49 status_t drawLayers(const DisplaySettings& display,
50 const std::vector<const LayerSettings*>& layers,
51 const sp<GraphicBuffer>& buffer, const bool useFramebufferCache,
52 base::unique_fd&& bufferFence, base::unique_fd* drawFence) override;
53 void cleanFramebufferCache() override;
Lucas Dupind508e472020-11-04 04:32:06 +000054 bool isProtected() const override { return mInProtectedContext; }
55 bool supportsProtectedContent() const override;
56 bool useProtectedContext(bool useProtectedContext) override;
John Reck67b1e2b2020-08-26 13:17:24 -070057
58protected:
59 void dump(std::string& /*result*/) override{};
60 size_t getMaxTextureSize() const override;
61 size_t getMaxViewportDims() const override;
62
63private:
64 static EGLConfig chooseEglConfig(EGLDisplay display, int format, bool logConfig);
65 static EGLContext createEglContext(EGLDisplay display, EGLConfig config,
66 EGLContext shareContext, bool useContextPriority,
67 Protection protection);
68 static EGLSurface createPlaceholderEglPbufferSurface(EGLDisplay display, EGLConfig config,
69 int hwcFormat, Protection protection);
Lucas Dupin3f11e922020-09-22 17:31:04 -070070 inline SkRect getSkRect(const FloatRect& layer);
71 inline SkRect getSkRect(const Rect& layer);
Lucas Dupin21f348e2020-09-16 17:31:26 -070072 inline SkRRect getRoundedRect(const LayerSettings* layer);
Lucas Dupin3f11e922020-09-22 17:31:04 -070073 inline SkColor getSkColor(const vec4& color);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -070074 inline SkM44 getSkM44(const mat4& matrix);
Lucas Dupin3f11e922020-09-22 17:31:04 -070075 inline SkPoint3 getSkPoint3(const vec3& vector);
John Reck67b1e2b2020-08-26 13:17:24 -070076
77 base::unique_fd flush();
78 bool waitFence(base::unique_fd fenceFd);
Lucas Dupin3f11e922020-09-22 17:31:04 -070079 void drawShadow(SkCanvas* canvas, const SkRect& casterRect, float casterCornerRadius,
80 const ShadowSettings& shadowSettings);
Lucas Dupinc3800b82020-10-02 16:24:48 -070081 void drawBlurRegion(SkCanvas* canvas, const BlurRegion& blurRegion, const SkRect& layerBounds,
82 sk_sp<SkSurface> blurrendSurface);
John Reck67b1e2b2020-08-26 13:17:24 -070083
84 EGLDisplay mEGLDisplay;
John Reck67b1e2b2020-08-26 13:17:24 -070085 EGLContext mEGLContext;
86 EGLSurface mPlaceholderSurface;
87 EGLContext mProtectedEGLContext;
88 EGLSurface mProtectedPlaceholderSurface;
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070089 BlurFilter* mBlurFilter = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -070090
Alec Mourib5777452020-09-28 11:32:42 -070091 const bool mUseColorManagement;
92
John Reck67b1e2b2020-08-26 13:17:24 -070093 // Cache of GL images that we'll store per GraphicBuffer ID
94 std::unordered_map<uint64_t, sk_sp<SkImage>> mImageCache GUARDED_BY(mRenderingMutex);
95 // Mutex guarding rendering operations, so that:
96 // 1. GL operations aren't interleaved, and
97 // 2. Internal state related to rendering that is potentially modified by
98 // multiple threads is guaranteed thread-safe.
99 std::mutex mRenderingMutex;
100
101 sp<Fence> mLastDrawFence;
102
Lucas Dupind508e472020-11-04 04:32:06 +0000103 // Graphics context used for creating surfaces and submitting commands
John Reck67b1e2b2020-08-26 13:17:24 -0700104 sk_sp<GrDirectContext> mGrContext;
Lucas Dupind508e472020-11-04 04:32:06 +0000105 // Same as above, but for protected content (eg. DRM)
106 sk_sp<GrDirectContext> mProtectedGrContext;
John Reck67b1e2b2020-08-26 13:17:24 -0700107
108 std::unordered_map<uint64_t, sk_sp<SkSurface>> mSurfaceCache;
Lucas Dupind508e472020-11-04 04:32:06 +0000109 std::unordered_map<uint64_t, sk_sp<SkSurface>> mProtectedSurfaceCache;
110 bool mInProtectedContext = false;
John Reck67b1e2b2020-08-26 13:17:24 -0700111};
112
113} // namespace skia
114} // namespace renderengine
115} // namespace android
116
117#endif /* SF_GLESRENDERENGINE_H_ */