blob: fa12624ff72ef882a88d8dfabf45d11a785e941f [file] [log] [blame]
Nolan Scobieca050282024-03-15 13:27:06 -04001/*
2 * Copyright 2024 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 <include/android/GrAHardwareBufferUtils.h>
20#include <include/core/SkColorSpace.h>
Nolan Scobiebc3f3602024-08-30 13:51:37 -040021#include <include/gpu/ganesh/GrDirectContext.h>
Nolan Scobieca050282024-03-15 13:27:06 -040022
23#include <android/hardware_buffer.h>
24#include <ui/GraphicTypes.h>
25
26namespace android::renderengine::skia {
27
28/**
29 * Abstraction over a Skia backend-specific texture type.
30 *
31 * This class does not do any lifecycle management, and should typically be wrapped in an
32 * AutoBackendTexture::LocalRef. Typically created via SkiaGpuContext::makeBackendTexture(...).
33 */
34class SkiaBackendTexture {
35public:
36 SkiaBackendTexture(AHardwareBuffer* buffer, bool isOutputBuffer)
37 : mIsOutputBuffer(isOutputBuffer) {
38 AHardwareBuffer_Desc desc;
39 AHardwareBuffer_describe(buffer, &desc);
40
41 mColorType = GrAHardwareBufferUtils::GetSkColorTypeFromBufferFormat(desc.format);
42 }
43 virtual ~SkiaBackendTexture() = default;
44
45 // These two definitions mirror Skia's own types used for texture release callbacks, which are
46 // re-declared multiple times between context-specific implementation headers for Ganesh vs.
47 // Graphite, and within the context of SkImages vs. SkSurfaces. Our own re-declaration allows us
48 // to not pull in any implementation-specific headers here.
49 using ReleaseContext = void*;
50 using TextureReleaseProc = void (*)(ReleaseContext);
51
52 // Guaranteed to be non-null (crashes otherwise). An opaque alphaType may coerce the internal
53 // color type to RBGX.
54 virtual sk_sp<SkImage> makeImage(SkAlphaType alphaType, ui::Dataspace dataspace,
55 TextureReleaseProc releaseImageProc,
56 ReleaseContext releaseContext) = 0;
57
58 // Guaranteed to be non-null (crashes otherwise).
59 virtual sk_sp<SkSurface> makeSurface(ui::Dataspace dataspace,
60 TextureReleaseProc releaseSurfaceProc,
61 ReleaseContext releaseContext) = 0;
62
63 bool isOutputBuffer() const { return mIsOutputBuffer; }
64
65 SkColorType internalColorType() const { return mColorType; }
66
67protected:
68 // Strip alpha channel from rawColorType if alphaType is opaque (note: only works for RGBA_8888)
69 SkColorType colorTypeForImage(SkAlphaType alphaType) const {
70 if (alphaType == kOpaque_SkAlphaType) {
71 // TODO: b/40043126 - Support RGBX SkColorType for F16 and support it and 101010x as a
72 // source
73 if (internalColorType() == kRGBA_8888_SkColorType) {
74 return kRGB_888x_SkColorType;
75 }
76 }
77 return internalColorType();
78 }
79
80private:
81 const bool mIsOutputBuffer;
82 SkColorType mColorType = kUnknown_SkColorType;
83};
84
85} // namespace android::renderengine::skia