Update SkSurface MakeFromBackend* factories to take an SkColorType.

Bug: skia:
Change-Id: Ib1b03b1181ec937843eac2e8d8cb03ebe53e32c1
Reviewed-on: https://skia-review.googlesource.com/86760
Commit-Queue: Greg Daniel <egdaniel@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Greg Daniel 2017-12-19 13:15:02 -05:00 committed by Skia Commit-Bot
parent 040238bded
commit faa095e984
32 changed files with 413 additions and 108 deletions

View File

@ -56,8 +56,14 @@ void SkGLWidget::createRenderTarget() {
glClear(GL_STENCIL_BUFFER_BIT);
fCurContext->resetContext();
GrBackendRenderTarget backendRenderTarget = this->getBackendRenderTarget();
SkColorType colorType;
if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) {
colorType = kRGBA_8888_SkColorType;
} else {
colorType = kBGRA_8888_SkColorType;
}
fGpuSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext.get(), backendRenderTarget,
kBottomLeft_GrSurfaceOrigin,
kBottomLeft_GrSurfaceOrigin, colorType,
nullptr, nullptr);
fCanvas = fGpuSurface->getCanvas();
}
@ -84,11 +90,12 @@ GrBackendRenderTarget SkGLWidget::getBackendRenderTarget() {
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_FRAMEBUFFER_BINDING, &info.fFBOID);
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_SAMPLES, &sampleCnt);
GR_GL_GetIntegerv(fCurIntf.get(), GR_GL_STENCIL_BITS, &stencilBits);
// We are on desktop so we assume the internal config is RGBA
info.fFormat = GR_GL_RGBA8;
return GrBackendRenderTarget(SkScalarRoundToInt(this->width()),
SkScalarRoundToInt(this->height()),
sampleCnt,
stencilBits,
kSkia8888_GrPixelConfig,
info);
}

View File

@ -180,6 +180,11 @@ int main(int argc, char** argv) {
return success;
}
uint32_t windowFormat = SDL_GetWindowPixelFormat(window);
int contextType;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &contextType);
int dw, dh;
SDL_GL_GetDrawableSize(window, &dw, &dh);
@ -201,8 +206,23 @@ int main(int argc, char** argv) {
GR_GL_GetIntegerv(interface.get(), GR_GL_FRAMEBUFFER_BINDING, &buffer);
GrGLFramebufferInfo info;
info.fFBOID = (GrGLuint) buffer;
GrBackendRenderTarget target(dw, dh, kMsaaSampleCount, kStencilBits,
kSkia8888_GrPixelConfig, info);
SkColorType colorType;
if (SDL_PIXELFORMAT_RGBA8888 == windowFormat) {
info.fFormat = GR_GL_RGBA8;
colorType = kRGBA_8888_SkColorType;
} else {
SkASSERT(SDL_PIXELFORMAT_BGRA8888);
colorType = kBGRA_8888_SkColorType;
if (SDL_GL_CONTEXT_PROFILE_ES == contextType) {
info.fFormat = GR_GL_BGRA8;
} else {
// We assume the internal format is RGBA8 on desktop GL
info.fFormat = GR_GL_RGBA8;
}
}
GrBackendRenderTarget target(dw, dh, kMsaaSampleCount, kStencilBits, info);
// setup SkSurface
// To use distance field text, use commented out SkSurfaceProps instead
@ -212,7 +232,7 @@ int main(int argc, char** argv) {
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendRenderTarget(grContext.get(), target,
kBottomLeft_GrSurfaceOrigin,
nullptr, &props));
colorType, nullptr, &props));
SkCanvas* canvas = surface->getCanvas();
canvas->scale((float)dw/dm.w, (float)dh/dm.h);

View File

@ -34,14 +34,22 @@ static void init_skia(int w, int h) {
GrGLFramebufferInfo framebufferInfo;
framebufferInfo.fFBOID = 0; // assume default framebuffer
// We are always using OpenGL and we use RGBA8 internal format for both RGBA and BGRA configs in
// OpenGL.
framebufferInfo.fFormat = GR_GL_RGBA8;
SkColorType colorType;
if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) {
colorType = kRGBA_8888_SkColorType;
} else {
colorType = kBGRA_8888_SkColorType;
}
GrBackendRenderTarget backendRenderTarget(w, h,
0, // sample count
0, // stencil bits
kSkia8888_GrPixelConfig,
framebufferInfo);
sSurface = SkSurface::MakeFromBackendRenderTarget(sContext, backendRenderTarget,
kBottomLeft_GrSurfaceOrigin,
kBottomLeft_GrSurfaceOrigin, colortype,
nullptr, nullptr).release();
}

View File

@ -131,7 +131,7 @@ public:
* Create a new image from the specified descriptor. Note - the caller is responsible for
* managing the lifetime of the underlying platform texture.
*
* The GrBackendTexture mush have a valid backed format supplied (GrGLTextureInfo::fFormat,
* The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendTexture. If the format in the
* GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we
@ -152,7 +152,7 @@ public:
* valid and unaltered until the specified release-proc is invoked, indicating that Skia
* no longer is holding a reference to it.
*
* The GrBackendTexture mush have a valid backed format supplied (GrGLTextureInfo::fFormat,
* The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendTexture. If the format in the
* GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we
@ -218,7 +218,7 @@ public:
* Create a new image from the specified descriptor. Note - Skia will delete or recycle the
* texture when the image is released.
*
* The GrBackendTexture mush have a valid backed format supplied (GrGLTextureInfo::fFormat,
* The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendTexture. If the format in the
* GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we

View File

@ -99,12 +99,45 @@ public:
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
/**
* Used to wrap a pre-existing backend 3D API texture as a SkSurface. Skia will not assume
* ownership of the texture and the client must ensure the texture is valid for the lifetime
* of the SkSurface. If sampleCnt > 0, then we will create an intermediate mssa surface which
* we will use for rendering. We then resolve into the passed in texture.
*
* The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendTexture. If the format in the
* GrBackendTexture is not compitable with the sampleCnt, SkColorType, and SkColorSpace we
* will return nullptr.
*/
static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
const GrBackendTexture& backendTexture,
GrSurfaceOrigin origin, int sampleCnt,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
const GrBackendRenderTarget& backendRenderTarget,
GrSurfaceOrigin origin,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
/**
* The GrBackendRenderTarget must have a valid backend format set (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendRenderTarget. If the format in the
* GrBackendRenderTarget is not compitable with the sampleCnt, SkColorType, and SkColorSpace
* we will return nullptr.
*/
static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
const GrBackendRenderTarget& backendRenderTarget,
GrSurfaceOrigin origin,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
/**
* Used to wrap a pre-existing 3D API texture as a SkSurface. Skia will treat the texture as
* a rendering target only, but unlike NewFromBackendRenderTarget, Skia will manage and own
@ -119,6 +152,27 @@ public:
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
/**
* Used to wrap a pre-existing 3D API texture as a SkSurface. Skia will treat the texture as
* a rendering target only, but unlike NewFromBackendRenderTarget, Skia will manage and own
* the associated render target objects (but not the provided texture). Skia will not assume
* ownership of the texture and the client must ensure the texture is valid for the lifetime
* of the SkSurface.
*
* The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat,
* GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should
* interpret the backend format supplied by the GrBackendTexture. If the format in the
* GrBackendTexture is not compitable with the sampleCnt, SkColorType, and SkColorSpace we
* will return nullptr.
*/
static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
const GrBackendTexture& backendTexture,
GrSurfaceOrigin origin,
int sampleCnt,
SkColorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* surfaceProps);
/**
* Return a new surface whose contents will be drawn to an offscreen
* render target, allocated by the surface. The optional shouldCreateWithMips flag is a hint

View File

@ -83,12 +83,10 @@ public:
private:
// Friending for access to the GrPixelConfig
friend class SkImage;
friend class SkSurface;
friend class GrCaps;
friend class GrGpu;
friend class GrGLCaps;
friend class GrGLGpu;
friend class GrVkCaps;
friend class GrVkGpu;
GrPixelConfig config() const { return fConfig; }
@ -112,6 +110,8 @@ public:
// Creates an invalid backend texture.
GrBackendRenderTarget() : fConfig(kUnknown_GrPixelConfig) {}
// GrGLTextureInfo::fFormat is ignored
// Deprecated: Should use version that does not take a GrPixelConfig instead
GrBackendRenderTarget(int width,
int height,
int sampleCnt,
@ -119,6 +119,13 @@ public:
GrPixelConfig config,
const GrGLFramebufferInfo& glInfo);
// The GrGLTextureInfo must have a valid fFormat.
GrBackendRenderTarget(int width,
int height,
int sampleCnt,
int stencilBits,
const GrGLFramebufferInfo& glInfo);
#ifdef SK_VULKAN
GrBackendRenderTarget(int width,
int height,
@ -149,11 +156,10 @@ public:
private:
// Friending for access to the GrPixelConfig
friend class SkSurface;
friend class GrCaps;
friend class SkSurface_Gpu;
friend class SkImage_Gpu;
friend class GrGpu;
friend class GrGLCaps;
friend class GrGLGpu;
friend class GrVkCaps;
friend class GrVkGpu;
GrPixelConfig config() const { return fConfig; }

View File

@ -15,6 +15,7 @@
#include "SkRefCnt.h"
#include "SkString.h"
class GrBackendRenderTarget;
class GrBackendTexture;
struct GrContextOptions;
class GrRenderTargetProxy;
@ -174,10 +175,13 @@ public:
/**
* Returns true if the GrBackendTexutre can we used with the supplied SkColorType. If it is
* compatible, the GrPixelConfig on the GrBackendTexture will be set to a config that matches
* the backend format and requested SkColorType.
* compatible, the passed in GrPixelConfig will be set to a config that matches the backend
* format and requested SkColorType.
*/
bool validateBackendTexture(GrBackendTexture* tex, SkColorType ct) const;
virtual bool validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
GrPixelConfig*) const = 0;
virtual bool validateBackendRenderTarget(const GrBackendRenderTarget&, SkColorType,
GrPixelConfig*) const = 0;
protected:
/** Subclasses must call this at the end of their constructors in order to apply caps
@ -238,7 +242,6 @@ protected:
int fMaxClipAnalyticFPs;
private:
virtual bool onValidateBackendTexture(GrBackendTexture* tex, SkColorType ct) const = 0;
virtual void onApplyOptionsOverrides(const GrContextOptions&) {}
virtual void onDumpJSON(SkJSONWriter*) const {}

View File

@ -119,6 +119,7 @@ GR_STATIC_ASSERT(sizeof(GrBackendObject) >= sizeof(const GrGLTextureInfo*));
struct GrGLFramebufferInfo {
GrGLuint fFBOID;
GrGLenum fFormat = 0;
};
GR_STATIC_ASSERT(sizeof(GrBackendObject) >= sizeof(const GrGLFramebufferInfo*));

View File

@ -127,6 +127,19 @@ GrBackendRenderTarget::GrBackendRenderTarget(int width,
, fBackend(kOpenGL_GrBackend)
, fGLInfo(glInfo) {}
GrBackendRenderTarget::GrBackendRenderTarget(int width,
int height,
int sampleCnt,
int stencilBits,
const GrGLFramebufferInfo& glInfo)
: fWidth(width)
, fHeight(height)
, fSampleCnt(sampleCnt)
, fStencilBits(stencilBits)
, fConfig(GrGLSizedFormatToPixelConfig(glInfo.fFormat))
, fBackend(kOpenGL_GrBackend)
, fGLInfo(glInfo) {}
#ifdef SK_VULKAN
const GrVkImageInfo* GrBackendRenderTarget::getVkImageInfo() const {
if (kVulkan_GrBackend == fBackend) {

View File

@ -218,11 +218,3 @@ void GrCaps::dumpJSON(SkJSONWriter* writer) const {
writer->endObject();
}
bool GrCaps::validateBackendTexture(GrBackendTexture* tex, SkColorType ct) const {
if (!this->onValidateBackendTexture(tex, ct)) {
return false;
}
return this->isConfigTexturable(tex->fConfig);
}

View File

@ -2361,62 +2361,82 @@ int GrGLCaps::getSampleCount(int requestedCount, GrPixelConfig config) const {
return fConfigTable[config].fColorSampleCounts[count-1];
}
bool GrGLCaps::onValidateBackendTexture(GrBackendTexture* tex, SkColorType ct) const {
const GrGLTextureInfo* texInfo = tex->getGLTextureInfo();
if (!texInfo) {
return false;
}
GrGLenum format = texInfo->fFormat;
tex->fConfig = kUnknown_GrPixelConfig;
bool validate_sized_format(GrGLenum format, SkColorType ct, GrPixelConfig* config,
GrGLStandard standard) {
*config = kUnknown_GrPixelConfig;
switch (ct) {
case kUnknown_SkColorType:
return false;
case kAlpha_8_SkColorType:
if (GR_GL_ALPHA8 == format) {
tex->fConfig = kAlpha_8_as_Alpha_GrPixelConfig;
*config = kAlpha_8_as_Alpha_GrPixelConfig;
} else if (GR_GL_R8 == format) {
tex->fConfig = kAlpha_8_as_Red_GrPixelConfig;
*config = kAlpha_8_as_Red_GrPixelConfig;
}
break;
case kRGB_565_SkColorType:
if (GR_GL_RGB565 == format) {
tex->fConfig = kRGB_565_GrPixelConfig;
*config = kRGB_565_GrPixelConfig;
}
break;
case kARGB_4444_SkColorType:
if (GR_GL_RGBA4 == format) {
tex->fConfig = kRGBA_4444_GrPixelConfig;
*config = kRGBA_4444_GrPixelConfig;
}
break;
case kRGBA_8888_SkColorType:
if (GR_GL_RGBA8 == format) {
tex->fConfig = kRGBA_8888_GrPixelConfig;
*config = kRGBA_8888_GrPixelConfig;
} else if (GR_GL_SRGB8_ALPHA8 == format) {
tex->fConfig = kSRGBA_8888_GrPixelConfig;
*config = kSRGBA_8888_GrPixelConfig;
}
break;
case kBGRA_8888_SkColorType:
if (GR_GL_BGRA8 == format) {
tex->fConfig = kBGRA_8888_GrPixelConfig;
if (GR_GL_RGBA8 == format) {
if (kGL_GrGLStandard == standard) {
*config = kBGRA_8888_GrPixelConfig;
}
} else if (GR_GL_BGRA8 == format) {
if (kGLES_GrGLStandard == standard) {
*config = kBGRA_8888_GrPixelConfig;
}
} else if (GR_GL_SRGB8_ALPHA8 == format) {
tex->fConfig = kSBGRA_8888_GrPixelConfig;
*config = kSBGRA_8888_GrPixelConfig;
}
break;
case kGray_8_SkColorType:
if (GR_GL_LUMINANCE8 == format) {
tex->fConfig = kGray_8_as_Lum_GrPixelConfig;
*config = kGray_8_as_Lum_GrPixelConfig;
} else if (GR_GL_R8 == format) {
tex->fConfig = kGray_8_as_Red_GrPixelConfig;
*config = kGray_8_as_Red_GrPixelConfig;
}
break;
case kRGBA_F16_SkColorType:
if (GR_GL_RGBA16F == format) {
tex->fConfig = kRGBA_half_GrPixelConfig;
*config = kRGBA_half_GrPixelConfig;
}
break;
}
return kUnknown_GrPixelConfig != tex->fConfig;
return kUnknown_GrPixelConfig != *config;
}
bool GrGLCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
GrPixelConfig* config) const {
const GrGLTextureInfo* texInfo = tex.getGLTextureInfo();
if (!texInfo) {
return false;
}
return validate_sized_format(texInfo->fFormat, ct, config, fStandard);
}
bool GrGLCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
GrPixelConfig* config) const {
const GrGLFramebufferInfo* fbInfo = rt.getGLFramebufferInfo();
if (!fbInfo) {
return false;
}
return validate_sized_format(fbInfo->fFormat, ct, config, fStandard);
}

View File

@ -415,9 +415,12 @@ public:
return fProgramBinarySupport;
}
private:
bool onValidateBackendTexture(GrBackendTexture*, SkColorType) const override;
bool validateBackendTexture(const GrBackendTexture&, SkColorType,
GrPixelConfig*) const override;
bool validateBackendRenderTarget(const GrBackendRenderTarget&, SkColorType,
GrPixelConfig*) const override;
private:
enum ExternalFormatUsage {
kTexImage_ExternalFormatUsage,
kOther_ExternalFormatUsage,

View File

@ -82,6 +82,15 @@ sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb));
}
GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
GrGLFramebufferInfo fbi;
fbi.fFBOID = fRTFBOID;
fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
this->numStencilSamples(), fbi);
}
size_t GrGLRenderTarget::onGpuMemorySize() const {
return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
fNumSamplesOwnedPerPixel, GrMipMapped::kNo);

View File

@ -63,13 +63,7 @@ public:
GrBackendObject getRenderTargetHandle() const override { return fRTFBOID; }
GrBackendRenderTarget getBackendRenderTarget() const override {
GrGLFramebufferInfo fbi;
fbi.fFBOID = fRTFBOID;
return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
this->numStencilSamples(), this->config(), fbi);
}
GrBackendRenderTarget getBackendRenderTarget() const override;
bool canAttemptStencilAttachment() const override;

View File

@ -107,8 +107,7 @@ GrBackendObject GrGLTexture::getTextureHandle() const {
}
GrBackendTexture GrGLTexture::getBackendTexture() const {
return GrBackendTexture(this->width(), this->height(), this->config(),
this->texturePriv().mipMapped(), fInfo);
return GrBackendTexture(this->width(), this->height(), this->texturePriv().mipMapped(), fInfo);
}
void GrGLTexture::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,

View File

@ -51,11 +51,17 @@ public:
return false;
}
private:
bool onValidateBackendTexture(GrBackendTexture* tex, SkColorType ct) const override {
return SkToBool(tex->getMockTextureInfo());
bool validateBackendTexture(const GrBackendTexture& tex, SkColorType,
GrPixelConfig*) const override {
return SkToBool(tex.getMockTextureInfo());
}
bool validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType,
GrPixelConfig*) const override {
return false;
}
private:
GrMockOptions fOptions;
typedef GrCaps INHERITED;
};

View File

@ -56,11 +56,16 @@ public:
return false;
}
private:
bool onValidateBackendTexture(GrBackendTexture* tex, SkColorType ct) const override {
bool validateBackendTexture(const GrBackendTexture&, SkColorType,
GrPixelConfig*) const override {
return false;
}
bool validateBackendRenderTarget(const GrBackendRenderTarget&, SkColorType,
GrPixelConfig*) const override {
return false;
}
private:
void initFeatureSet(MTLFeatureSet featureSet);
void initGrCaps(const id<MTLDevice> device);

View File

@ -415,59 +415,67 @@ int GrVkCaps::getSampleCount(int requestedCount, GrPixelConfig config) const {
return fConfigTable[config].fColorSampleCounts[count-1];
}
bool GrVkCaps::onValidateBackendTexture(GrBackendTexture* tex, SkColorType ct) const {
const GrVkImageInfo* imageInfo = tex->getVkImageInfo();
bool validate_image_info(const GrVkImageInfo* imageInfo, SkColorType ct, GrPixelConfig* config) {
if (!imageInfo) {
return false;
}
VkFormat format = imageInfo->fFormat;
tex->fConfig = kUnknown_GrPixelConfig;
*config = kUnknown_GrPixelConfig;
switch (ct) {
case kUnknown_SkColorType:
return false;
case kAlpha_8_SkColorType:
if (VK_FORMAT_R8_UNORM == format) {
tex->fConfig = kAlpha_8_as_Red_GrPixelConfig;
*config = kAlpha_8_as_Red_GrPixelConfig;
}
break;
case kRGB_565_SkColorType:
if (VK_FORMAT_R5G6B5_UNORM_PACK16 == format) {
tex->fConfig = kRGB_565_GrPixelConfig;
*config = kRGB_565_GrPixelConfig;
}
break;
case kARGB_4444_SkColorType:
if (VK_FORMAT_B4G4R4A4_UNORM_PACK16 == format) {
tex->fConfig = kRGBA_4444_GrPixelConfig;
*config = kRGBA_4444_GrPixelConfig;
}
break;
case kRGBA_8888_SkColorType:
if (VK_FORMAT_R8G8B8A8_UNORM == format) {
tex->fConfig = kRGBA_8888_GrPixelConfig;
*config = kRGBA_8888_GrPixelConfig;
} else if (VK_FORMAT_R8G8B8A8_SRGB == format) {
tex->fConfig = kSRGBA_8888_GrPixelConfig;
*config = kSRGBA_8888_GrPixelConfig;
}
break;
case kBGRA_8888_SkColorType:
if (VK_FORMAT_B8G8R8A8_UNORM == format) {
tex->fConfig = kBGRA_8888_GrPixelConfig;
*config = kBGRA_8888_GrPixelConfig;
} else if (VK_FORMAT_B8G8R8A8_SRGB == format) {
tex->fConfig = kSBGRA_8888_GrPixelConfig;
*config = kSBGRA_8888_GrPixelConfig;
}
break;
case kGray_8_SkColorType:
if (VK_FORMAT_R8_UNORM == format) {
tex->fConfig = kGray_8_as_Red_GrPixelConfig;
*config = kGray_8_as_Red_GrPixelConfig;
}
break;
case kRGBA_F16_SkColorType:
if (VK_FORMAT_R16G16B16A16_SFLOAT == format) {
tex->fConfig = kRGBA_half_GrPixelConfig;
*config = kRGBA_half_GrPixelConfig;
}
break;
}
return kUnknown_GrPixelConfig != tex->fConfig;
return kUnknown_GrPixelConfig != *config;
}
bool GrVkCaps::validateBackendTexture(const GrBackendTexture& tex, SkColorType ct,
GrPixelConfig* config) const {
return validate_image_info(tex.getVkImageInfo(), ct, config);
}
bool GrVkCaps::validateBackendRenderTarget(const GrBackendRenderTarget& rt, SkColorType ct,
GrPixelConfig* config) const {
return validate_image_info(rt.getVkImageInfo(), ct, config);
}

View File

@ -111,9 +111,12 @@ public:
bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
bool* rectsMustMatch, bool* disallowSubrect) const override;
private:
bool onValidateBackendTexture(GrBackendTexture*, SkColorType) const override;
bool validateBackendTexture(const GrBackendTexture&, SkColorType,
GrPixelConfig*) const override;
bool validateBackendRenderTarget(const GrBackendRenderTarget&, SkColorType,
GrPixelConfig*) const override;
private:
enum VkVendor {
kAMD_VkVendor = 4098,
kImagination_VkVendor = 4112,

View File

@ -297,11 +297,14 @@ sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
const GrBackendTexture& tex, GrSurfaceOrigin origin,
SkAlphaType at, sk_sp<SkColorSpace> cs,
TextureReleaseProc releaseP, ReleaseContext releaseC) {
if (!ctx) {
return nullptr;
}
return new_wrapped_texture_common(ctx, tex, origin, at, std::move(cs), kBorrow_GrWrapOwnership,
releaseP, releaseC);
}
bool validate_backend_texture(GrContext* ctx, GrBackendTexture* tex,
bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config,
SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs) {
// TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
// create a fake image info here.
@ -310,15 +313,18 @@ bool validate_backend_texture(GrContext* ctx, GrBackendTexture* tex,
return false;
}
return ctx->caps()->validateBackendTexture(tex, ct);
return ctx->caps()->validateBackendTexture(tex, ct, config);
}
sk_sp<SkImage> SkImage::MakeFromTexture(GrContext* ctx,
const GrBackendTexture& tex, GrSurfaceOrigin origin,
SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs,
TextureReleaseProc releaseP, ReleaseContext releaseC) {
if (!ctx) {
return nullptr;
}
GrBackendTexture texCopy = tex;
if (!validate_backend_texture(ctx, &texCopy, ct, at, cs)) {
if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
return nullptr;
}
return MakeFromTexture(ctx, texCopy, origin, at, cs, releaseP, releaseC);
@ -336,7 +342,7 @@ sk_sp<SkImage> SkImage::MakeFromAdoptedTexture(GrContext* ctx,
SkColorType ct, SkAlphaType at,
sk_sp<SkColorSpace> cs) {
GrBackendTexture texCopy = tex;
if (!validate_backend_texture(ctx, &texCopy, ct, at, cs)) {
if (!validate_backend_texture(ctx, texCopy, &texCopy.fConfig, ct, at, cs)) {
return nullptr;
}
return MakeFromAdoptedTexture(ctx, texCopy, origin, at, cs);

View File

@ -258,6 +258,13 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext*, const GrBackendTe
return nullptr;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext*, const GrBackendTexture&,
GrSurfaceOrigin origin, int sampleCnt,
SkColorType, sk_sp<SkColorSpace>,
const SkSurfaceProps*) {
return nullptr;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext*,
const GrBackendRenderTarget&,
GrSurfaceOrigin origin,
@ -266,6 +273,15 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext*,
return nullptr;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext*,
const GrBackendRenderTarget&,
GrSurfaceOrigin origin,
SkColorType,
sk_sp<SkColorSpace>,
const SkSurfaceProps*) {
return nullptr;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext*,
const GrBackendTexture&,
GrSurfaceOrigin origin,
@ -275,4 +291,14 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext*,
return nullptr;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext*,
const GrBackendTexture&,
GrSurfaceOrigin origin,
int sampleCnt,
SkColorType,
sk_sp<SkColorSpace>,
const SkSurfaceProps*) {
return nullptr;
}
#endif

View File

@ -294,6 +294,52 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context, const GrB
return sk_make_sp<SkSurface_Gpu>(std::move(device));
}
bool validate_backend_texture(GrContext* ctx, const GrBackendTexture& tex, GrPixelConfig* config,
int sampleCnt, SkColorType ct, sk_sp<SkColorSpace> cs,
bool texturable) {
// TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
// create a fake image info here.
SkImageInfo info = SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType, cs);
if (!SkSurface_Gpu::Valid(info)) {
return false;
}
if (!ctx->caps()->validateBackendTexture(tex, ct, config)) {
return false;
}
if (!ctx->caps()->isConfigRenderable(*config, sampleCnt > 0)) {
return false;
}
if (ctx->caps()->getSampleCount(sampleCnt, *config) != sampleCnt) {
return false;
}
if (texturable && !ctx->caps()->isConfigTexturable(*config)) {
return false;
}
return true;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrContext* context, const GrBackendTexture& tex,
GrSurfaceOrigin origin, int sampleCnt,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* props) {
if (!context) {
return nullptr;
}
GrBackendTexture texCopy = tex;
if (!validate_backend_texture(context, texCopy, &texCopy.fConfig,
sampleCnt, colorType, colorSpace, true)) {
return nullptr;
}
return MakeFromBackendTexture(context, texCopy, origin, sampleCnt, colorSpace, props);
}
sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext* context,
const GrBackendRenderTarget& backendRT,
GrSurfaceOrigin origin,
@ -325,6 +371,44 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext* context,
return sk_make_sp<SkSurface_Gpu>(std::move(device));
}
bool validate_backend_render_target(GrContext* ctx, const GrBackendRenderTarget& rt,
GrPixelConfig* config, SkColorType ct, sk_sp<SkColorSpace> cs) {
// TODO: Create a SkImageColorInfo struct for color, alpha, and color space so we don't need to
// create a fake image info here.
SkImageInfo info = SkImageInfo::Make(1, 1, ct, kPremul_SkAlphaType, cs);
if (!SkSurface_Gpu::Valid(info)) {
return false;
}
if (!ctx->caps()->validateBackendRenderTarget(rt, ct, config)) {
return false;
}
if (!ctx->caps()->isConfigRenderable(*config, false)) {
return false;
}
return true;
}
sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrContext* context,
const GrBackendRenderTarget& rt,
GrSurfaceOrigin origin,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* props) {
if (!context) {
return nullptr;
}
GrBackendRenderTarget rtCopy = rt;
if (!validate_backend_render_target(context, rtCopy, &rtCopy.fConfig, colorType, colorSpace)) {
return nullptr;
}
return MakeFromBackendRenderTarget(context, rtCopy, origin, colorSpace, props);
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext* context,
const GrBackendTexture& tex,
GrSurfaceOrigin origin,
@ -357,4 +441,24 @@ sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext* cont
return sk_make_sp<SkSurface_Gpu>(std::move(device));
}
sk_sp<SkSurface> SkSurface::MakeFromBackendTextureAsRenderTarget(GrContext* context,
const GrBackendTexture& tex,
GrSurfaceOrigin origin,
int sampleCnt,
SkColorType colorType,
sk_sp<SkColorSpace> colorSpace,
const SkSurfaceProps* props) {
if (!context) {
return nullptr;
}
GrBackendTexture texCopy = tex;
if (!validate_backend_texture(context, texCopy, &texCopy.fConfig,
sampleCnt, colorType, colorSpace, false)) {
return nullptr;
}
return MakeFromBackendTextureAsRenderTarget(context, texCopy, origin, sampleCnt, colorSpace,
props);
}
#endif

View File

@ -84,8 +84,8 @@ DEF_TEST(Blend_byte_multiply, r) {
#if SK_SUPPORT_GPU
namespace {
static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
GrContext* context, int sampleCnt, int width, int height, GrPixelConfig config,
GrSurfaceOrigin origin,
GrContext* context, int sampleCnt, int width, int height, SkColorType colorType,
GrPixelConfig config, GrSurfaceOrigin origin,
sk_sp<GrTexture>* backingSurface) {
GrSurfaceDesc backingDesc;
backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
@ -104,7 +104,7 @@ static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
sk_sp<SkSurface> surface =
SkSurface::MakeFromBackendTextureAsRenderTarget(context, backendTex, origin,
sampleCnt, nullptr, nullptr);
sampleCnt, colorType, nullptr, nullptr);
return surface;
}
@ -158,7 +158,7 @@ DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ES2BlendWithNoTexture, reporter, ctxInfo)
sk_sp<GrTexture> backingSurface;
// BGRA forces a framebuffer blit on ES2.
sk_sp<SkSurface> surface = create_gpu_surface_backend_texture_as_render_target(
context, sampleCnt, kWidth, kHeight, kConfig, origin, &backingSurface);
context, sampleCnt, kWidth, kHeight, kColorType, kConfig, origin, &backingSurface);
if (!surface && sampleCnt > 0) {
// Some platforms don't support MSAA.

View File

@ -53,6 +53,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrWrappedMipMappedTest, reporter, ctxInfo) {
backendTex,
kTopLeft_GrSurfaceOrigin,
0,
kRGBA_8888_SkColorType,
nullptr,
nullptr);
@ -221,6 +222,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrImageSnapshotMipMappedTest, reporter, ctxIn
backendTex,
kTopLeft_GrSurfaceOrigin,
0,
kRGBA_8888_SkColorType,
nullptr,
nullptr);
} else {

View File

@ -598,6 +598,7 @@ static sk_sp<SkSurface> create_gpu_surface_backend_texture(
sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(context, *outTexture,
kTopLeft_GrSurfaceOrigin, sampleCnt,
kRGBA_8888_SkColorType,
nullptr, nullptr);
if (!surface) {
context->getGpu()->deleteTestingOnlyBackendTexture(outTexture);
@ -621,7 +622,8 @@ static sk_sp<SkSurface> create_gpu_surface_backend_texture_as_render_target(
}
sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTextureAsRenderTarget(
context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, nullptr, nullptr);
context, *outTexture, kTopLeft_GrSurfaceOrigin, sampleCnt, kRGBA_8888_SkColorType,
nullptr, nullptr);
if (!surface) {
context->getGpu()->deleteTestingOnlyBackendTexture(outTexture);
@ -894,6 +896,7 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SurfaceCreationWithColorSpace_Gpu, reporter,
return SkSurface::MakeFromBackendTexture(context, backendTex,
kTopLeft_GrSurfaceOrigin, 0,
info.colorType(),
sk_ref_sp(info.colorSpace()), nullptr);
};

View File

@ -430,8 +430,14 @@ DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo)
for (int sampleCnt : {0, 4}) {
GrBackendTexture backendTex = context->getGpu()->createTestingOnlyBackendTexture(
nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true, GrMipMapped::kNo);
SkColorType colorType;
if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig) {
colorType = kRGBA_8888_SkColorType;
} else {
colorType = kBGRA_8888_SkColorType;
}
sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(
context, backendTex, origin, sampleCnt, nullptr, nullptr));
context, backendTex, origin, sampleCnt, colorType, nullptr, nullptr));
if (!surface) {
context->getGpu()->deleteTestingOnlyBackendTexture(&backendTex);
continue;

View File

@ -33,6 +33,7 @@ void draw(SkCanvas* canvas) {
sk_sp<SkImage> tmp = SkImage::MakeFromTexture(context,
backEndTexture,
kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
kOpaque_SkAlphaType,
nullptr);
@ -40,13 +41,14 @@ void draw(SkCanvas* canvas) {
sk_sp<SkSurface> tmp2 = SkSurface::MakeFromBackendTexture(context,
backEndTextureRenderTarget,
kTopLeft_GrSurfaceOrigin,
0, nullptr, nullptr);
0, kRGBA_8888_SkColorType,
nullptr, nullptr);
// Note: this surface should only be renderable (i.e., not textureable)
sk_sp<SkSurface> tmp3 = SkSurface::MakeFromBackendRenderTarget(context,
backEndRenderTarget,
kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
nullptr, nullptr);
}
}

View File

@ -129,6 +129,7 @@ static bool setup_backend_objects(GrContext* context,
backingDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
backingDesc.fWidth = bm.width();
backingDesc.fHeight = bm.height();
// This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
backingDesc.fSampleCnt = 0;

View File

@ -39,15 +39,6 @@ void GLWindowContext::initializeContext() {
this->initializeContext();
return;
}
if (fContext) {
// We may not have real sRGB support (ANGLE, in particular), so check for
// that, and fall back to L32:
fPixelConfig = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
? kSRGBA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
} else {
fPixelConfig = kUnknown_GrPixelConfig;
}
}
void GLWindowContext::destroyContext() {
@ -72,16 +63,18 @@ sk_sp<SkSurface> GLWindowContext::getBackbufferSurface() {
GR_GL_CALL(fBackendContext.get(), GetIntegerv(GR_GL_FRAMEBUFFER_BINDING,
&buffer));
fbInfo.fFBOID = buffer;
fbInfo.fFormat = fContext->caps()->srgbSupport() && fDisplayParams.fColorSpace
? GR_GL_SRGB8_ALPHA8 : GR_GL_RGBA8;
GrBackendRenderTarget backendRT(fWidth,
fHeight,
fSampleCount,
fStencilBits,
fPixelConfig,
fbInfo);
fSurface = SkSurface::MakeFromBackendRenderTarget(fContext.get(), backendRT,
kBottomLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
fDisplayParams.fColorSpace,
&fSurfaceProps);
}

View File

@ -201,6 +201,20 @@ bool VulkanWindowContext::createSwapchain(int width, int height,
return false;
}
SkColorType colorType;
switch (surfaceFormat) {
case VK_FORMAT_R8G8B8A8_UNORM: // fall through
case VK_FORMAT_R8G8B8A8_SRGB:
colorType = kRGBA_8888_SkColorType;
break;
case VK_FORMAT_B8G8R8A8_UNORM: // fall through
case VK_FORMAT_B8G8R8A8_SRGB:
colorType = kBGRA_8888_SkColorType;
break;
default:
return false;
}
// If mailbox mode is available, use it, as it is the lowest-latency non-
// tearing mode. If not, fall back to FIFO which is always available.
VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
@ -254,15 +268,12 @@ bool VulkanWindowContext::createSwapchain(int width, int height,
fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
}
this->createBuffers(swapchainCreateInfo.imageFormat);
this->createBuffers(swapchainCreateInfo.imageFormat, colorType);
return true;
}
void VulkanWindowContext::createBuffers(VkFormat format) {
fPixelConfig = GrVkFormatToPixelConfig(format);
SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) {
fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
SkASSERT(fImageCount);
fImages = new VkImage[fImageCount];
@ -287,6 +298,7 @@ void VulkanWindowContext::createBuffers(VkFormat format) {
fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext.get(), backendTex,
kTopLeft_GrSurfaceOrigin,
fSampleCount,
colorType,
fDisplayParams.fColorSpace,
&fSurfaceProps);
}

View File

@ -64,7 +64,7 @@ private:
BackbufferInfo* getAvailableBackbuffer();
bool createSwapchain(int width, int height, const DisplayParams& params);
void createBuffers(VkFormat format);
void createBuffers(VkFormat format, SkColorType colorType);
void destroyBuffers();
sk_sp<const GrVkBackendContext> fBackendContext;

View File

@ -61,7 +61,6 @@ protected:
int fWidth;
int fHeight;
DisplayParams fDisplayParams;
GrPixelConfig fPixelConfig;
SkSurfaceProps fSurfaceProps;
// parameters obtained from the native window