2012-09-11 13:29:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrTextureAccess_DEFINED
|
|
|
|
#define GrTextureAccess_DEFINED
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
#include "GrGpuResourceRef.h"
|
2014-09-16 20:54:53 +00:00
|
|
|
#include "GrTexture.h"
|
2012-09-11 15:45:20 +00:00
|
|
|
#include "SkRefCnt.h"
|
2012-09-18 14:14:49 +00:00
|
|
|
#include "SkShader.h"
|
2012-09-11 13:29:29 +00:00
|
|
|
|
2012-09-18 14:14:49 +00:00
|
|
|
/**
|
|
|
|
* Represents the filtering and tile modes used to access a texture. It is mostly used with
|
|
|
|
* GrTextureAccess (defined below). Also, some of the texture cache methods require knowledge about
|
|
|
|
* filtering and tiling to perform a cache lookup. If it wasn't for this latter usage this would
|
2013-09-06 15:31:06 +00:00
|
|
|
* be folded into GrTextureAccess. The default is clamp tile modes and no filtering.
|
2012-09-18 14:14:49 +00:00
|
|
|
*/
|
|
|
|
class GrTextureParams {
|
|
|
|
public:
|
|
|
|
GrTextureParams() {
|
|
|
|
this->reset();
|
|
|
|
}
|
2013-07-26 07:00:58 +00:00
|
|
|
|
2013-07-25 18:49:07 +00:00
|
|
|
enum FilterMode {
|
|
|
|
kNone_FilterMode,
|
|
|
|
kBilerp_FilterMode,
|
|
|
|
kMipMap_FilterMode
|
|
|
|
};
|
|
|
|
|
|
|
|
GrTextureParams(SkShader::TileMode tileXAndY, FilterMode filterMode) {
|
|
|
|
this->reset(tileXAndY, filterMode);
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 12:53:39 +00:00
|
|
|
GrTextureParams(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
|
2013-07-25 18:49:07 +00:00
|
|
|
this->reset(tileModes, filterMode);
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GrTextureParams(const GrTextureParams& params) {
|
|
|
|
*this = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
GrTextureParams& operator= (const GrTextureParams& params) {
|
|
|
|
fTileModes[0] = params.fTileModes[0];
|
|
|
|
fTileModes[1] = params.fTileModes[1];
|
2013-07-25 18:49:07 +00:00
|
|
|
fFilterMode = params.fFilterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset() {
|
2013-07-25 18:49:07 +00:00
|
|
|
this->reset(SkShader::kClamp_TileMode, kNone_FilterMode);
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
2013-07-25 18:49:07 +00:00
|
|
|
void reset(SkShader::TileMode tileXAndY, FilterMode filterMode) {
|
2012-09-18 14:14:49 +00:00
|
|
|
fTileModes[0] = fTileModes[1] = tileXAndY;
|
2013-07-25 18:49:07 +00:00
|
|
|
fFilterMode = filterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 12:53:39 +00:00
|
|
|
void reset(const SkShader::TileMode tileModes[2], FilterMode filterMode) {
|
2012-09-18 14:14:49 +00:00
|
|
|
fTileModes[0] = tileModes[0];
|
|
|
|
fTileModes[1] = tileModes[1];
|
2013-07-25 18:49:07 +00:00
|
|
|
fFilterMode = filterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setClampNoFilter() {
|
|
|
|
fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
|
2013-07-25 18:49:07 +00:00
|
|
|
fFilterMode = kNone_FilterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setClamp() {
|
|
|
|
fTileModes[0] = fTileModes[1] = SkShader::kClamp_TileMode;
|
|
|
|
}
|
|
|
|
|
2013-07-25 18:49:07 +00:00
|
|
|
void setFilterMode(FilterMode filterMode) { fFilterMode = filterMode; }
|
2012-09-18 14:14:49 +00:00
|
|
|
|
|
|
|
void setTileModeX(const SkShader::TileMode tm) { fTileModes[0] = tm; }
|
|
|
|
void setTileModeY(const SkShader::TileMode tm) { fTileModes[1] = tm; }
|
|
|
|
void setTileModeXAndY(const SkShader::TileMode tm) { fTileModes[0] = fTileModes[1] = tm; }
|
|
|
|
|
|
|
|
SkShader::TileMode getTileModeX() const { return fTileModes[0]; }
|
|
|
|
|
|
|
|
SkShader::TileMode getTileModeY() const { return fTileModes[1]; }
|
|
|
|
|
|
|
|
bool isTiled() const {
|
|
|
|
return SkShader::kClamp_TileMode != fTileModes[0] ||
|
|
|
|
SkShader::kClamp_TileMode != fTileModes[1];
|
|
|
|
}
|
|
|
|
|
2013-07-25 18:49:07 +00:00
|
|
|
FilterMode filterMode() const { return fFilterMode; }
|
2012-09-18 14:14:49 +00:00
|
|
|
|
|
|
|
bool operator== (const GrTextureParams& other) const {
|
|
|
|
return fTileModes[0] == other.fTileModes[0] &&
|
|
|
|
fTileModes[1] == other.fTileModes[1] &&
|
2013-07-25 18:49:07 +00:00
|
|
|
fFilterMode == other.fFilterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!= (const GrTextureParams& other) const { return !(*this == other); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
SkShader::TileMode fTileModes[2];
|
2013-07-25 18:49:07 +00:00
|
|
|
FilterMode fFilterMode;
|
2012-09-18 14:14:49 +00:00
|
|
|
};
|
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/** A class representing the swizzle access pattern for a texture. Note that if the texture is
|
|
|
|
* an alpha-only texture then the alpha channel is substituted for other components. Any mangling
|
|
|
|
* to handle the r,g,b->a conversions for alpha textures is automatically included in the stage
|
2014-09-23 16:50:21 +00:00
|
|
|
* key. However, if a GrProcessor uses different swizzles based on its input then it must
|
2012-09-11 15:45:20 +00:00
|
|
|
* consider that variation in its key-generation.
|
2012-09-11 13:29:29 +00:00
|
|
|
*/
|
2014-09-05 21:27:43 +00:00
|
|
|
class GrTextureAccess : public SkNoncopyable {
|
2012-09-11 13:29:29 +00:00
|
|
|
public:
|
2015-01-13 16:22:43 +00:00
|
|
|
SK_DECLARE_INST_COUNT(GrTextureAccess);
|
2014-09-05 21:27:43 +00:00
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* A default GrTextureAccess must have reset() called on it in a GrProcessor subclass's
|
|
|
|
* constructor if it will be accessible via GrProcessor::textureAccess().
|
2012-09-11 15:45:20 +00:00
|
|
|
*/
|
|
|
|
GrTextureAccess();
|
2012-09-11 13:29:29 +00:00
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/**
|
2012-09-18 14:14:49 +00:00
|
|
|
* Uses the default swizzle, "rgba".
|
2012-09-11 15:45:20 +00:00
|
|
|
*/
|
2012-09-18 14:14:49 +00:00
|
|
|
GrTextureAccess(GrTexture*, const GrTextureParams&);
|
|
|
|
explicit GrTextureAccess(GrTexture*,
|
2013-07-25 18:49:07 +00:00
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
2012-09-18 14:14:49 +00:00
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
|
2012-09-11 13:29:29 +00:00
|
|
|
|
2012-09-11 15:45:20 +00:00
|
|
|
/**
|
2012-09-18 14:14:49 +00:00
|
|
|
* swizzle must be a string between one and four (inclusive) characters containing only 'r',
|
|
|
|
* 'g', 'b', and/or 'a'.
|
2012-09-11 15:45:20 +00:00
|
|
|
*/
|
2012-09-18 14:14:49 +00:00
|
|
|
GrTextureAccess(GrTexture*, const char* swizzle, const GrTextureParams&);
|
|
|
|
GrTextureAccess(GrTexture*,
|
|
|
|
const char* swizzle,
|
2013-07-25 18:49:07 +00:00
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
2012-09-18 14:14:49 +00:00
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
|
2012-09-11 13:29:29 +00:00
|
|
|
|
2012-09-18 14:14:49 +00:00
|
|
|
void reset(GrTexture*, const GrTextureParams&);
|
|
|
|
void reset(GrTexture*,
|
2013-07-25 18:49:07 +00:00
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
2012-09-18 14:14:49 +00:00
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
|
|
|
|
void reset(GrTexture*, const char* swizzle, const GrTextureParams&);
|
|
|
|
void reset(GrTexture*,
|
|
|
|
const char* swizzle,
|
2013-07-25 18:49:07 +00:00
|
|
|
GrTextureParams::FilterMode = GrTextureParams::kNone_FilterMode,
|
2012-09-18 14:14:49 +00:00
|
|
|
SkShader::TileMode tileXAndY = SkShader::kClamp_TileMode);
|
|
|
|
|
|
|
|
bool operator== (const GrTextureAccess& other) const {
|
2013-08-28 14:17:03 +00:00
|
|
|
#ifdef SK_DEBUG
|
2012-09-18 14:14:49 +00:00
|
|
|
// below assumes all chars in fSwizzle are initialized even if string is < 4 chars long.
|
2013-08-17 00:02:59 +00:00
|
|
|
SkASSERT(memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1) ==
|
2012-09-18 14:14:49 +00:00
|
|
|
strcmp(fSwizzle, other.fSwizzle));
|
|
|
|
#endif
|
|
|
|
return fParams == other.fParams &&
|
2014-09-05 21:27:43 +00:00
|
|
|
(this->getTexture() == other.getTexture()) &&
|
2012-09-20 15:37:30 +00:00
|
|
|
(0 == memcmp(fSwizzle, other.fSwizzle, sizeof(fSwizzle)-1));
|
2012-09-18 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!= (const GrTextureAccess& other) const { return !(*this == other); }
|
2012-09-11 15:45:20 +00:00
|
|
|
|
2014-09-16 20:54:53 +00:00
|
|
|
GrTexture* getTexture() const { return fTexture.get(); }
|
2014-09-05 21:27:43 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-23 16:50:21 +00:00
|
|
|
* For internal use by GrProcessor.
|
2014-09-05 21:27:43 +00:00
|
|
|
*/
|
2014-09-17 15:05:40 +00:00
|
|
|
const GrGpuResourceRef* getProgramTexture() const { return &fTexture; }
|
2012-09-11 15:45:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representing the swizzle. The string is is null-terminated.
|
|
|
|
*/
|
|
|
|
const char* getSwizzle() const { return fSwizzle; }
|
|
|
|
|
2013-03-28 13:46:42 +00:00
|
|
|
/** Returns a mask indicating which components are referenced in the swizzle. The return
|
|
|
|
is a bitfield of GrColorComponentFlags. */
|
2012-09-11 15:45:20 +00:00
|
|
|
uint32_t swizzleMask() const { return fSwizzleMask; }
|
2012-09-11 13:29:29 +00:00
|
|
|
|
2012-09-18 14:14:49 +00:00
|
|
|
const GrTextureParams& getParams() const { return fParams; }
|
|
|
|
|
2012-09-11 13:29:29 +00:00
|
|
|
private:
|
2012-09-18 14:14:49 +00:00
|
|
|
void setSwizzle(const char*);
|
|
|
|
|
2014-09-17 15:05:40 +00:00
|
|
|
typedef GrTGpuResourceRef<GrTexture> ProgramTexture;
|
2014-09-16 20:54:53 +00:00
|
|
|
|
|
|
|
ProgramTexture fTexture;
|
2014-09-05 21:27:43 +00:00
|
|
|
GrTextureParams fParams;
|
|
|
|
uint32_t fSwizzleMask;
|
|
|
|
char fSwizzle[5];
|
2012-09-18 14:14:49 +00:00
|
|
|
|
2013-09-18 13:00:55 +00:00
|
|
|
typedef SkNoncopyable INHERITED;
|
2012-09-11 13:29:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|