| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
#include "include/gpu/ganesh/gl/GrGLBackendSurface.h" |
| 8 |
|
| 9 |
#include "include/core/SkRefCnt.h" |
| 10 |
#include "include/core/SkTextureCompressionType.h" |
| 11 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 12 |
#include "include/gpu/ganesh/GrTypes.h" |
| 13 |
#include "include/gpu/ganesh/gl/GrGLTypes.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 17 |
#include "src/gpu/ganesh/GrBackendSurfacePriv.h" |
| 18 |
#include "src/gpu/ganesh/gl/GrGLBackendSurfacePriv.h" |
| 19 |
#include "src/gpu/ganesh/gl/GrGLDefines.h" |
| 20 |
#include "src/gpu/ganesh/gl/GrGLTypesPriv.h" |
| 21 |
#include "src/gpu/ganesh/gl/GrGLUtil.h" |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <cstddef> |
| 25 |
#include <cstdint> |
| 26 |
#include <string> |
| 27 |
|
| 28 |
class GrGLBackendFormatData final : public GrBackendFormatData { |
| 29 |
public: |
| 30 |
GrGLBackendFormatData(GrGLenum format) : fGLFormat(format) {} |
| 31 |
|
| 32 |
GrGLenum asEnum() const { return fGLFormat; } |
| 33 |
|
| 34 |
private: |
| 35 |
SkTextureCompressionType compressionType() const override { |
| 36 |
switch (GrGLFormatFromGLEnum(fGLFormat)) { |
| 37 |
case GrGLFormat::kCOMPRESSED_ETC1_RGB8: |
| 38 |
case GrGLFormat::kCOMPRESSED_RGB8_ETC2: |
| 39 |
return SkTextureCompressionType::kETC2_RGB8_UNORM; |
| 40 |
case GrGLFormat::kCOMPRESSED_RGB8_BC1: |
| 41 |
return SkTextureCompressionType::kBC1_RGB8_UNORM; |
| 42 |
case GrGLFormat::kCOMPRESSED_RGBA8_BC1: |
| 43 |
return SkTextureCompressionType::kBC1_RGBA8_UNORM; |
| 44 |
default: |
| 45 |
return SkTextureCompressionType::kNone; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
size_t bytesPerBlock() const override { |
| 50 |
return GrGLFormatBytesPerBlock(GrGLFormatFromGLEnum(fGLFormat)); |
| 51 |
} |
| 52 |
|
| 53 |
int stencilBits() const override { |
| 54 |
return GrGLFormatStencilBits(GrGLFormatFromGLEnum(fGLFormat)); |
| 55 |
} |
| 56 |
|
| 57 |
uint32_t channelMask() const override { |
| 58 |
return GrGLFormatChannels(GrGLFormatFromGLEnum(fGLFormat)); |
| 59 |
} |
| 60 |
|
| 61 |
GrColorFormatDesc desc() const override { |
| 62 |
return GrGLFormatDesc(GrGLFormatFromGLEnum(fGLFormat)); |
| 63 |
} |
| 64 |
|
| 65 |
bool equal(const GrBackendFormatData* that) const override { |
| 66 |
SkASSERT(!that || that->type() == GrBackendApi::kOpenGL); |
| 67 |
if (auto otherGL = static_cast<const GrGLBackendFormatData*>(that)) { |
| 68 |
return fGLFormat == otherGL->fGLFormat; |
| 69 |
} |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
std::string toString() const override { |
| 74 |
#if defined(SK_DEBUG) || defined(GPU_TEST_UTILS) |
| 75 |
return GrGLFormatToStr(fGLFormat); |
| 76 |
#else |
| 77 |
return ""; |
| 78 |
#endif |
| 79 |
} |
| 80 |
|
| 81 |
void copyTo(AnyFormatData& formatData) const override { |
| 82 |
formatData.emplace<GrGLBackendFormatData>(fGLFormat); |
| 83 |
} |
| 84 |
|
| 85 |
#if defined(SK_DEBUG) |
| 86 |
GrBackendApi type() const override { return GrBackendApi::kOpenGL; } |
| 87 |
#endif |
| 88 |
|
| 89 |
GrGLenum fGLFormat; // the sized, internal format of the GL resource |
| 90 |
}; |
| 91 |
|
| 92 |
static GrTextureType gl_target_to_gr_target(GrGLenum target) { |
| 93 |
switch (target) { |
| 94 |
case GR_GL_TEXTURE_NONE: |
| 95 |
return GrTextureType::kNone; |
| 96 |
case GR_GL_TEXTURE_2D: |
| 97 |
return GrTextureType::k2D; |
| 98 |
case GR_GL_TEXTURE_RECTANGLE: |
| 99 |
return GrTextureType::kRectangle; |
| 100 |
case GR_GL_TEXTURE_EXTERNAL: |
| 101 |
return GrTextureType::kExternal; |
| 102 |
default: |
| 103 |
SkUNREACHABLE; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static const GrGLBackendFormatData* get_and_cast_data(const GrBackendFormat& format) { |
| 108 |
auto data = GrBackendSurfacePriv::GetBackendData(format); |
| 109 |
SkASSERT(!data || data->type() == GrBackendApi::kOpenGL); |
| 110 |
return static_cast<const GrGLBackendFormatData*>(data); |
| 111 |
} |
| 112 |
|
| 113 |
namespace GrBackendFormats { |
| 114 |
GrBackendFormat MakeGL(GrGLenum format, GrGLenum target) { |
| 115 |
return GrBackendSurfacePriv::MakeGrBackendFormat( |
| 116 |
gl_target_to_gr_target(target), GrBackendApi::kOpenGL, GrGLBackendFormatData(format)); |
| 117 |
} |
| 118 |
|
| 119 |
GrBackendFormat MakeGL(GrGLenum format) { |
| 120 |
return GrBackendSurfacePriv::MakeGrBackendFormat( |
| 121 |
GrTextureType::k2D, GrBackendApi::kOpenGL, GrGLBackendFormatData(format)); |
| 122 |
} |
| 123 |
|
| 124 |
GrBackendFormat MakeGLExternal() { |
| 125 |
// For external backend formats, GL_RGBA8 likely does not match the actual bit depth of the |
| 126 |
// underlying data. However, we just need a value that is defined in the GrGLFormat enum to |
| 127 |
// avoid thinking that it's unsupported. |
| 128 |
return GrBackendSurfacePriv::MakeGrBackendFormat( |
| 129 |
GrTextureType::kExternal, GrBackendApi::kOpenGL, GrGLBackendFormatData(GR_GL_RGBA8)); |
| 130 |
} |
| 131 |
|
| 132 |
GrGLFormat AsGLFormat(const GrBackendFormat& format) { |
| 133 |
if (format.isValid() && format.backend() == GrBackendApi::kOpenGL) { |
| 134 |
const GrGLBackendFormatData* data = get_and_cast_data(format); |
| 135 |
SkASSERT(data); |
| 136 |
return GrGLFormatFromGLEnum(data->asEnum()); |
| 137 |
} |
| 138 |
return GrGLFormat::kUnknown; |
| 139 |
} |
| 140 |
|
| 141 |
GrGLenum AsGLFormatEnum(const GrBackendFormat& format) { |
| 142 |
if (format.isValid() && format.backend() == GrBackendApi::kOpenGL) { |
| 143 |
const GrGLBackendFormatData* data = get_and_cast_data(format); |
| 144 |
SkASSERT(data); |
| 145 |
return data->asEnum(); |
| 146 |
} |
| 147 |
return 0; |
| 148 |
} |
| 149 |
} // namespace GrBackendFormats |
| 150 |
|
| 151 |
GrGLBackendTextureData::GrGLBackendTextureData(const GrGLTextureInfo& info, |
| 152 |
sk_sp<GrGLTextureParameters> params) |
| 153 |
: fGLInfo(info, params) {} |
| 154 |
|
| 155 |
void GrGLBackendTextureData::copyTo(AnyTextureData& textureData) const { |
| 156 |
textureData.emplace<GrGLBackendTextureData>(fGLInfo.info(), fGLInfo.refParameters()); |
| 157 |
} |
| 158 |
|
| 159 |
bool GrGLBackendTextureData::isProtected() const { return fGLInfo.isProtected(); } |
| 160 |
|
| 161 |
bool GrGLBackendTextureData::equal(const GrBackendTextureData* that) const { |
| 162 |
SkASSERT(!that || that->type() == GrBackendApi::kOpenGL); |
| 163 |
if (auto otherGL = static_cast<const GrGLBackendTextureData*>(that)) { |
| 164 |
return fGLInfo.info() == otherGL->fGLInfo.info(); |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
bool GrGLBackendTextureData::isSameTexture(const GrBackendTextureData* that) const { |
| 170 |
SkASSERT(!that || that->type() == GrBackendApi::kOpenGL); |
| 171 |
if (auto otherGL = static_cast<const GrGLBackendTextureData*>(that)) { |
| 172 |
return fGLInfo.info().fID == otherGL->fGLInfo.info().fID; |
| 173 |
} |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
GrBackendFormat GrGLBackendTextureData::getBackendFormat() const { |
| 178 |
return GrBackendFormats::MakeGL(fGLInfo.info().fFormat, fGLInfo.info().fTarget); |
| 179 |
} |
| 180 |
|
| 181 |
static const GrGLBackendTextureData* get_and_cast_data(const GrBackendTexture& texture) { |
| 182 |
auto data = GrBackendSurfacePriv::GetBackendData(texture); |
| 183 |
SkASSERT(!data || data->type() == GrBackendApi::kOpenGL); |
| 184 |
return static_cast<const GrGLBackendTextureData*>(data); |
| 185 |
} |
| 186 |
|
| 187 |
static GrGLBackendTextureData* get_and_cast_data(GrBackendTexture* texture) { |
| 188 |
auto data = GrBackendSurfacePriv::GetBackendData(texture); |
| 189 |
SkASSERT(!data || data->type() == GrBackendApi::kOpenGL); |
| 190 |
return static_cast<GrGLBackendTextureData*>(data); |
| 191 |
} |
| 192 |
|
| 193 |
namespace GrBackendTextures { |
| 194 |
GrBackendTexture MakeGL(int width, |
| 195 |
int height, |
| 196 |
skgpu::Mipmapped mipped, |
| 197 |
const GrGLTextureInfo& glInfo, |
| 198 |
std::string_view label) { |
| 199 |
auto tex = GrBackendSurfacePriv::MakeGrBackendTexture( |
| 200 |
width, |
| 201 |
height, |
| 202 |
label, |
| 203 |
mipped, |
| 204 |
GrBackendApi::kOpenGL, |
| 205 |
gl_target_to_gr_target(glInfo.fTarget), |
| 206 |
GrGLBackendTextureData(glInfo, sk_make_sp<GrGLTextureParameters>())); |
| 207 |
// Make no assumptions about client's texture's parameters. |
| 208 |
GLTextureParametersModified(&tex); |
| 209 |
return tex; |
| 210 |
} |
| 211 |
|
| 212 |
GrBackendTexture MakeGL(int width, |
| 213 |
int height, |
| 214 |
skgpu::Mipmapped mipped, |
| 215 |
const GrGLTextureInfo& glInfo, |
| 216 |
sk_sp<GrGLTextureParameters> params, |
| 217 |
std::string_view label) { |
| 218 |
return GrBackendSurfacePriv::MakeGrBackendTexture(width, |
| 219 |
height, |
| 220 |
label, |
| 221 |
mipped, |
| 222 |
GrBackendApi::kOpenGL, |
| 223 |
gl_target_to_gr_target(glInfo.fTarget), |
| 224 |
GrGLBackendTextureData(glInfo, params)); |
| 225 |
} |
| 226 |
|
| 227 |
bool GetGLTextureInfo(const GrBackendTexture& tex, GrGLTextureInfo* outInfo) { |
| 228 |
if (!tex.isValid() || tex.backend() != GrBackendApi::kOpenGL) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
const GrGLBackendTextureData* data = get_and_cast_data(tex); |
| 232 |
SkASSERT(data); |
| 233 |
*outInfo = data->info().info(); |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
void GLTextureParametersModified(GrBackendTexture* tex) { |
| 238 |
if (tex && tex->isValid() && tex->backend() == GrBackendApi::kOpenGL) { |
| 239 |
GrGLBackendTextureData* data = get_and_cast_data(tex); |
| 240 |
SkASSERT(data); |
| 241 |
data->info().parameters()->invalidate(); |
| 242 |
} |
| 243 |
} |
| 244 |
} // namespace GrBackendTextures |
| 245 |
|
| 246 |
class GrGLBackendRenderTargetData final : public GrBackendRenderTargetData { |
| 247 |
public: |
| 248 |
GrGLBackendRenderTargetData(GrGLFramebufferInfo info) : fGLInfo(info) {} |
| 249 |
|
| 250 |
GrGLFramebufferInfo info() const { return fGLInfo; } |
| 251 |
|
| 252 |
private: |
| 253 |
GrBackendFormat getBackendFormat() const override { |
| 254 |
return GrBackendFormats::MakeGL(fGLInfo.fFormat, GR_GL_TEXTURE_NONE); |
| 255 |
} |
| 256 |
|
| 257 |
bool isProtected() const override { return fGLInfo.isProtected(); } |
| 258 |
|
| 259 |
bool equal(const GrBackendRenderTargetData* that) const override { |
| 260 |
SkASSERT(!that || that->type() == GrBackendApi::kOpenGL); |
| 261 |
if (auto otherGL = static_cast<const GrGLBackendRenderTargetData*>(that)) { |
| 262 |
return fGLInfo == otherGL->fGLInfo; |
| 263 |
} |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
void copyTo(AnyRenderTargetData& rtData) const override { |
| 268 |
rtData.emplace<GrGLBackendRenderTargetData>(fGLInfo); |
| 269 |
} |
| 270 |
|
| 271 |
#if defined(SK_DEBUG) |
| 272 |
GrBackendApi type() const override { return GrBackendApi::kOpenGL; } |
| 273 |
#endif |
| 274 |
|
| 275 |
GrGLFramebufferInfo fGLInfo; |
| 276 |
}; |
| 277 |
|
| 278 |
static const GrGLBackendRenderTargetData* get_and_cast_data(const GrBackendRenderTarget& rt) { |
| 279 |
auto data = GrBackendSurfacePriv::GetBackendData(rt); |
| 280 |
SkASSERT(!data || data->type() == GrBackendApi::kOpenGL); |
| 281 |
return static_cast<const GrGLBackendRenderTargetData*>(data); |
| 282 |
} |
| 283 |
|
| 284 |
namespace GrBackendRenderTargets { |
| 285 |
// The GrGLTextureInfo must have a valid fFormat. If wrapping in an SkSurface we require the |
| 286 |
// stencil bits to be either 0, 8 or 16. |
| 287 |
GrBackendRenderTarget MakeGL( |
| 288 |
int width, int height, int sampleCnt, int stencilBits, const GrGLFramebufferInfo& glInfo) { |
| 289 |
return GrBackendSurfacePriv::MakeGrBackendRenderTarget(width, |
| 290 |
height, |
| 291 |
std::max(1, sampleCnt), |
| 292 |
stencilBits, |
| 293 |
GrBackendApi::kOpenGL, |
| 294 |
/*framebufferOnly=*/false, |
| 295 |
GrGLBackendRenderTargetData(glInfo)); |
| 296 |
} |
| 297 |
|
| 298 |
bool GetGLFramebufferInfo(const GrBackendRenderTarget& rt, GrGLFramebufferInfo* outInfo) { |
| 299 |
if (!rt.isValid() || rt.backend() != GrBackendApi::kOpenGL) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
const GrGLBackendRenderTargetData* data = get_and_cast_data(rt); |
| 303 |
SkASSERT(data); |
| 304 |
*outInfo = data->info(); |
| 305 |
return true; |
| 306 |
} |
| 307 |
|
| 308 |
} // namespace GrBackendRenderTargets |