Add SkColorSpace to SkImageInfo

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=2000713003

Review-Url: https://codereview.chromium.org/2000713003
This commit is contained in:
msarett 2016-05-27 07:39:02 -07:00 committed by Commit bot
parent 85f758c334
commit 23c5110ba2
7 changed files with 58 additions and 26 deletions

View File

@ -9,7 +9,8 @@
#define SkEncodedInfo_DEFINED
#include "SkImageInfo.h"
#include "../private/SkImageInfoPriv.h"
class SkColorSpace;
struct SkEncodedInfo {
public:
@ -116,22 +117,21 @@ public:
* Returns an SkImageInfo with Skia color and alpha types that are the
* closest possible match to the encoded info.
*/
SkImageInfo makeImageInfo(int width, int height) const {
SkColorProfileType profileType = SkDefaultColorProfile();
SkImageInfo makeImageInfo(int width, int height, const sk_sp<SkColorSpace>& colorSpace) const {
switch (fColor) {
case kGray_Color:
SkASSERT(kOpaque_Alpha == fAlpha);
return SkImageInfo::Make(width, height, kGray_8_SkColorType,
kOpaque_SkAlphaType, profileType);
kOpaque_SkAlphaType, colorSpace);
case kGrayAlpha_Color:
SkASSERT(kOpaque_Alpha != fAlpha);
return SkImageInfo::Make(width, height, kN32_SkColorType,
kUnpremul_SkAlphaType, profileType);
kUnpremul_SkAlphaType, colorSpace);
case kPalette_Color: {
SkAlphaType alphaType = (kOpaque_Alpha == fAlpha) ? kOpaque_SkAlphaType :
kUnpremul_SkAlphaType;
return SkImageInfo::Make(width, height, kIndex_8_SkColorType,
alphaType, profileType);
alphaType, colorSpace);
}
case kRGB_Color:
case kBGR_Color:
@ -141,13 +141,13 @@ public:
case kYCCK_Color:
SkASSERT(kOpaque_Alpha == fAlpha);
return SkImageInfo::Make(width, height, kN32_SkColorType,
kOpaque_SkAlphaType, profileType);
kOpaque_SkAlphaType, colorSpace);
case kRGBA_Color:
case kBGRA_Color:
case kYUVA_Color:
SkASSERT(kOpaque_Alpha != fAlpha);
return SkImageInfo::Make(width, height, kN32_SkColorType,
kUnpremul_SkAlphaType, profileType);
kUnpremul_SkAlphaType, colorSpace);
default:
SkASSERT(false);
return SkImageInfo::MakeUnknown();

View File

@ -8,6 +8,7 @@
#ifndef SkImageInfo_DEFINED
#define SkImageInfo_DEFINED
#include "SkColorSpace.h"
#include "SkMath.h"
#include "SkRect.h"
#include "SkSize.h"
@ -182,7 +183,8 @@ enum SkColorProfileType {
struct SK_API SkImageInfo {
public:
SkImageInfo()
: fWidth(0)
: fColorSpace(nullptr)
, fWidth(0)
, fHeight(0)
, fColorType(kUnknown_SkColorType)
, fAlphaType(kUnknown_SkAlphaType)
@ -191,15 +193,18 @@ public:
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
SkColorProfileType pt = kLinear_SkColorProfileType) {
return SkImageInfo(width, height, ct, at, pt);
return SkImageInfo(width, height, ct, at, pt, nullptr);
}
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
sk_sp<SkColorSpace> cs);
/**
* Sets colortype to the native ARGB32 type.
*/
static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
SkColorProfileType pt = kLinear_SkColorProfileType) {
return SkImageInfo(width, height, kN32_SkColorType, at, pt);
return SkImageInfo(width, height, kN32_SkColorType, at, pt, nullptr);
}
/**
@ -207,7 +212,7 @@ public:
*/
static SkImageInfo MakeN32Premul(int width, int height,
SkColorProfileType pt = kLinear_SkColorProfileType) {
return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType, pt);
return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType, pt, nullptr);
}
/**
@ -220,12 +225,12 @@ public:
static SkImageInfo MakeA8(int width, int height) {
return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType,
kLinear_SkColorProfileType);
kLinear_SkColorProfileType, nullptr);
}
static SkImageInfo MakeUnknown(int width, int height) {
return SkImageInfo(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType,
kLinear_SkColorProfileType);
kLinear_SkColorProfileType, nullptr);
}
static SkImageInfo MakeUnknown() {
@ -236,6 +241,9 @@ public:
int height() const { return fHeight; }
SkColorType colorType() const { return fColorType; }
SkAlphaType alphaType() const { return fAlphaType; }
SkColorSpace* colorSpace() const { return fColorSpace.get(); }
// Deprecated
SkColorProfileType profileType() const { return fProfileType; }
bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
@ -244,6 +252,7 @@ public:
return SkAlphaTypeIsOpaque(fAlphaType);
}
// Deprecated
bool isLinear() const { return kLinear_SkColorProfileType == fProfileType; }
bool isSRGB() const { return kSRGB_SkColorProfileType == fProfileType; }
@ -255,15 +264,15 @@ public:
* but with the specified width and height.
*/
SkImageInfo makeWH(int newWidth, int newHeight) const {
return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType, fProfileType);
return SkImageInfo(newWidth, newHeight, fColorType, fAlphaType, fProfileType, fColorSpace);
}
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
return SkImageInfo::Make(fWidth, fHeight, fColorType, newAlphaType, fProfileType);
return SkImageInfo(fWidth, fHeight, fColorType, newAlphaType, fProfileType, fColorSpace);
}
SkImageInfo makeColorType(SkColorType newColorType) const {
return SkImageInfo::Make(fWidth, fHeight, newColorType, fAlphaType, fProfileType);
return SkImageInfo(fWidth, fHeight, newColorType, fAlphaType, fProfileType, fColorSpace);
}
int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
@ -285,10 +294,14 @@ public:
}
bool operator==(const SkImageInfo& other) const {
return 0 == memcmp(this, &other, sizeof(other));
return fWidth == other.fWidth && fHeight == other.fHeight &&
fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
fProfileType == other.fProfileType && fColorSpace == other.fColorSpace;
}
bool operator!=(const SkImageInfo& other) const {
return 0 != memcmp(this, &other, sizeof(other));
return fWidth != other.fWidth || fHeight != other.fHeight ||
fColorType != other.fColorType || fAlphaType != other.fAlphaType ||
fProfileType != other.fProfileType || fColorSpace != other.fColorSpace;
}
void unflatten(SkReadBuffer&);
@ -314,17 +327,29 @@ public:
return rowBytes >= rb;
}
void reset() {
fWidth = 0;
fHeight = 0;
fColorType = kUnknown_SkColorType;
fAlphaType = kUnknown_SkAlphaType;
fProfileType = kLinear_SkColorProfileType;
fColorSpace = nullptr;
}
SkDEBUGCODE(void validate() const;)
private:
sk_sp<SkColorSpace> fColorSpace;
int fWidth;
int fHeight;
SkColorType fColorType;
SkAlphaType fAlphaType;
SkColorProfileType fProfileType;
SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, SkColorProfileType pt)
: fWidth(width)
SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, SkColorProfileType pt,
sk_sp<SkColorSpace> cs)
: fColorSpace(std::move(cs))
, fWidth(width)
, fHeight(height)
, fColorType(ct)
, fAlphaType(at)

View File

@ -117,10 +117,10 @@ SkCodec* SkCodec::NewFromData(SkData* data, SkPngChunkReader* reader) {
SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
sk_sp<SkColorSpace> colorSpace, Origin origin)
: fEncodedInfo(info)
, fSrcInfo(info.makeImageInfo(width, height))
, fSrcInfo(info.makeImageInfo(width, height, colorSpace))
, fStream(stream)
, fNeedsRewind(false)
, fColorSpace(colorSpace)
, fColorSpace(std::move(colorSpace))
, fOrigin(origin)
, fDstInfo()
, fOptions()

View File

@ -53,6 +53,7 @@ SkBitmap& SkBitmap::operator=(const SkBitmap& src) {
// inc src reference counts
SkSafeRef(src.fPixelRef);
SkSafeRef(src.fInfo.colorSpace());
// we reset our locks if we get blown away
fPixelLockCount = 0;
@ -97,6 +98,7 @@ void SkBitmap::swap(SkBitmap& other) {
void SkBitmap::reset() {
this->freePixels();
this->fInfo.reset();
sk_bzero(this, sizeof(*this));
}

View File

@ -30,6 +30,11 @@ static bool color_type_is_valid(SkColorType colorType) {
return (colorType >= 0) && (colorType <= kLastEnum_SkColorType);
}
SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at,
sk_sp<SkColorSpace> cs) {
return SkImageInfo(width, height, ct, at, SkDefaultColorProfile(), std::move(cs));
}
void SkImageInfo::unflatten(SkReadBuffer& buffer) {
fWidth = buffer.read32();
fHeight = buffer.read32();

View File

@ -451,7 +451,7 @@ SkMipMap* SkMipMap::Build(const SkPixmap& src, SkDiscardableFactoryProc fact) {
height = SkTMax(1, height >> 1);
rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
levels[i].fPixmap = SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
new (&levels[i].fPixmap) SkPixmap(SkImageInfo::Make(width, height, ct, at), addr, rowBytes);
levels[i].fScale = SkSize::Make(SkIntToScalar(width) / src.width(),
SkIntToScalar(height) / src.height());

View File

@ -24,10 +24,10 @@ static void test_flatten(skiatest::Reporter* reporter, const SkImageInfo& info)
SkASSERT(wb.bytesWritten() < sizeof(storage));
SkReadBuffer rb(storage, wb.bytesWritten());
SkImageInfo info2;
// pick a noisy byte pattern, so we ensure that unflatten sets all of our fields
memset(&info2, 0xB8, sizeof(info2));
SkImageInfo info2 = SkImageInfo::Make(0xB8, 0xB8, (SkColorType) 0xB8, (SkAlphaType) 0xB8,
(SkColorProfileType) 0xB8);
info2.unflatten(rb);
REPORTER_ASSERT(reporter, rb.offset() == wb.bytesWritten());