2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrTexture_DEFINED
|
|
|
|
#define GrTexture_DEFINED
|
|
|
|
|
2012-06-21 21:09:06 +00:00
|
|
|
#include "GrSurface.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-29 15:13:20 +00:00
|
|
|
class GrRenderTarget;
|
2012-06-04 20:05:28 +00:00
|
|
|
class GrResourceKey;
|
2012-07-25 21:27:09 +00:00
|
|
|
class GrTextureParams;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-06-21 21:09:06 +00:00
|
|
|
class GrTexture : public GrSurface {
|
2011-03-30 21:26:44 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
public:
|
2012-06-13 18:54:08 +00:00
|
|
|
SK_DECLARE_INST_COUNT(GrTexture)
|
2012-06-21 21:09:06 +00:00
|
|
|
// from GrResource
|
2012-06-22 12:41:43 +00:00
|
|
|
/**
|
|
|
|
* Informational texture flags
|
|
|
|
*/
|
|
|
|
enum FlagBits {
|
|
|
|
kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This texture should be returned to the texture cache when
|
|
|
|
* it is no longer reffed
|
|
|
|
*/
|
|
|
|
kReturnToCache_FlagBit = kFirstBit,
|
|
|
|
};
|
|
|
|
|
|
|
|
void setFlag(GrTextureFlags flags) {
|
|
|
|
fDesc.fFlags = fDesc.fFlags | flags;
|
|
|
|
}
|
|
|
|
void resetFlag(GrTextureFlags flags) {
|
|
|
|
fDesc.fFlags = fDesc.fFlags & ~flags;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
bool isSetFlag(GrTextureFlags flags) const {
|
|
|
|
return 0 != (fDesc.fFlags & flags);
|
2012-06-22 12:41:43 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
/**
|
2011-02-17 16:43:10 +00:00
|
|
|
* Approximate number of bytes used by the texture
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
2012-06-21 21:09:06 +00:00
|
|
|
virtual size_t sizeInBytes() const SK_OVERRIDE {
|
2012-08-23 18:09:54 +00:00
|
|
|
return (size_t) fDesc.fWidth *
|
|
|
|
fDesc.fHeight *
|
2012-06-04 12:48:45 +00:00
|
|
|
GrBytesPerPixel(fDesc.fConfig);
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-20 19:22:38 +00:00
|
|
|
// GrSurface overrides
|
2012-06-21 21:09:06 +00:00
|
|
|
virtual bool readPixels(int left, int top, int width, int height,
|
2012-08-20 19:22:38 +00:00
|
|
|
GrPixelConfig config,
|
|
|
|
void* buffer,
|
|
|
|
size_t rowBytes = 0,
|
|
|
|
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
|
2011-11-16 20:36:03 +00:00
|
|
|
|
2012-06-21 21:09:06 +00:00
|
|
|
virtual void writePixels(int left, int top, int width, int height,
|
2012-08-20 19:22:38 +00:00
|
|
|
GrPixelConfig config,
|
|
|
|
const void* buffer,
|
|
|
|
size_t rowBytes = 0,
|
|
|
|
uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
|
2012-06-21 21:09:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return this texture
|
|
|
|
*/
|
|
|
|
virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
|
|
|
|
virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
|
2011-04-05 17:08:27 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the render target underlying this texture that can be passed to
|
|
|
|
* GrGpu::setRenderTarget().
|
|
|
|
*
|
2011-04-05 21:16:14 +00:00
|
|
|
* @return handle to render target or NULL if the texture is not a
|
2010-12-22 21:39:39 +00:00
|
|
|
* render target
|
|
|
|
*/
|
2012-08-23 18:09:54 +00:00
|
|
|
virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
|
|
|
|
return fRenderTarget;
|
2012-06-21 21:09:06 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
|
|
|
|
return fRenderTarget;
|
2012-06-21 21:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GrTexture
|
|
|
|
/**
|
|
|
|
* Convert from texels to normalized texture coords for POT textures
|
|
|
|
* only.
|
|
|
|
*/
|
2012-08-23 18:09:54 +00:00
|
|
|
GrFixed normalizeFixedX(GrFixed x) const {
|
2012-06-21 21:09:06 +00:00
|
|
|
GrAssert(GrIsPow2(fDesc.fWidth));
|
2012-08-23 18:09:54 +00:00
|
|
|
return x >> fShiftFixedX;
|
2012-06-21 21:09:06 +00:00
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
GrFixed normalizeFixedY(GrFixed y) const {
|
2012-06-21 21:09:06 +00:00
|
|
|
GrAssert(GrIsPow2(fDesc.fHeight));
|
2012-08-23 18:09:54 +00:00
|
|
|
return y >> fShiftFixedY;
|
2012-06-21 21:09:06 +00:00
|
|
|
}
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2011-03-10 14:51:57 +00:00
|
|
|
* Removes the reference on the associated GrRenderTarget held by this
|
2011-03-30 21:26:44 +00:00
|
|
|
* texture. Afterwards asRenderTarget() will return NULL. The
|
2011-03-10 14:51:57 +00:00
|
|
|
* GrRenderTarget survives the release if another ref is held on it.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
2011-07-29 15:13:20 +00:00
|
|
|
void releaseRenderTarget();
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the native ID or handle to the texture, depending on the
|
2012-10-25 18:56:10 +00:00
|
|
|
* platform. e.g. on OpenGL, return the texture ID.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
2012-10-25 18:56:10 +00:00
|
|
|
virtual GrBackendObject getTextureHandle() const = 0;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-06-12 13:58:36 +00:00
|
|
|
/**
|
|
|
|
* Call this when the state of the native API texture object is
|
2012-08-23 18:09:54 +00:00
|
|
|
* altered directly, without being tracked by skia.
|
2012-06-12 13:58:36 +00:00
|
|
|
*/
|
|
|
|
virtual void invalidateCachedState() = 0;
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#if GR_DEBUG
|
|
|
|
void validate() const {
|
|
|
|
this->INHERITED::validate();
|
2012-06-04 12:48:45 +00:00
|
|
|
|
|
|
|
this->validateDesc();
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
void validate() const {}
|
|
|
|
#endif
|
2012-06-04 20:05:28 +00:00
|
|
|
static GrResourceKey ComputeKey(const GrGpu* gpu,
|
2012-12-20 15:13:01 +00:00
|
|
|
const GrTextureParams* params,
|
2012-06-04 20:05:28 +00:00
|
|
|
const GrTextureDesc& desc,
|
2012-12-20 15:13:01 +00:00
|
|
|
const GrCacheID& cacheID);
|
|
|
|
static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
|
2012-06-04 20:05:28 +00:00
|
|
|
static bool NeedsResizing(const GrResourceKey& key);
|
|
|
|
static bool NeedsFiltering(const GrResourceKey& key);
|
|
|
|
|
2011-04-05 21:16:14 +00:00
|
|
|
protected:
|
|
|
|
GrRenderTarget* fRenderTarget; // texture refs its rt representation
|
|
|
|
// base class cons sets to NULL
|
|
|
|
// subclass cons can create and set
|
|
|
|
|
2013-02-05 19:50:46 +00:00
|
|
|
GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
|
|
|
|
: INHERITED(gpu, isWrapped, desc)
|
2012-06-21 21:09:06 +00:00
|
|
|
, fRenderTarget(NULL) {
|
2012-06-04 12:48:45 +00:00
|
|
|
|
2011-04-05 21:16:14 +00:00
|
|
|
// only make sense if alloc size is pow2
|
2012-11-01 18:02:54 +00:00
|
|
|
fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
|
|
|
|
fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
|
2011-04-05 21:16:14 +00:00
|
|
|
}
|
2011-07-26 12:32:36 +00:00
|
|
|
|
2011-04-05 21:16:14 +00:00
|
|
|
// GrResource overrides
|
2012-06-22 12:41:43 +00:00
|
|
|
virtual void onRelease() SK_OVERRIDE;
|
|
|
|
virtual void onAbandon() SK_OVERRIDE;
|
2011-04-05 21:16:14 +00:00
|
|
|
|
2012-06-04 12:48:45 +00:00
|
|
|
void validateDesc() const;
|
|
|
|
|
2011-02-14 16:51:21 +00:00
|
|
|
private:
|
2010-12-22 21:39:39 +00:00
|
|
|
// these two shift a fixed-point value into normalized coordinates
|
|
|
|
// for this texture if the texture is power of two sized.
|
2012-06-04 12:48:45 +00:00
|
|
|
int fShiftFixedX;
|
|
|
|
int fShiftFixedY;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2012-06-22 12:41:43 +00:00
|
|
|
virtual void internal_dispose() const SK_OVERRIDE;
|
|
|
|
|
2012-06-21 21:09:06 +00:00
|
|
|
typedef GrSurface INHERITED;
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|