1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 #ifndef GrTexture_DEFINED 10 #define GrTexture_DEFINED 11 12 #include "GrSurface.h" 13 #include "GrRenderTarget.h" 14 #include "SkPoint.h" 15 #include "SkRefCnt.h" 16 17 class GrResourceKey; 18 class GrTextureParams; 19 class GrTextureImpl; 20 21 class GrTexture : public GrSurface { 22 public: 23 /** 24 * Approximate number of bytes used by the texture 25 */ 26 virtual size_t gpuMemorySize() const SK_OVERRIDE; 27 28 // GrSurface overrides 29 virtual bool readPixels(int left, int top, int width, int height, 30 GrPixelConfig config, 31 void* buffer, 32 size_t rowBytes = 0, 33 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; 34 35 virtual void writePixels(int left, int top, int width, int height, 36 GrPixelConfig config, 37 const void* buffer, 38 size_t rowBytes = 0, 39 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; 40 41 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } 42 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } 43 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { return fRenderTarget.get(); } 44 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { return fRenderTarget.get(); } 45 46 /** 47 * Convert from texels to normalized texture coords for POT textures only. Please don't add 48 * new callsites for these functions. They are slated for removal. 49 */ 50 SkFixed normalizeFixedX(SkFixed x) const { 51 SkASSERT(SkIsPow2(fDesc.fWidth)); 52 return x >> fShiftFixedX; 53 } 54 SkFixed normalizeFixedY(SkFixed y) const { 55 SkASSERT(SkIsPow2(fDesc.fHeight)); 56 return y >> fShiftFixedY; 57 } 58 59 /** 60 * Return the native ID or handle to the texture, depending on the 61 * platform. e.g. on OpenGL, return the texture ID. 62 */ 63 virtual GrBackendObject getTextureHandle() const = 0; 64 65 /** 66 * This function indicates that the texture parameters (wrap mode, filtering, ...) have been 67 * changed externally to Skia. 68 */ 69 virtual void textureParamsModified() = 0; 70 SK_ATTR_DEPRECATED("Renamed to textureParamsModified.") 71 void invalidateCachedState() { this->textureParamsModified(); } 72 73 /** 74 * Informational texture flags. This will be moved to the private GrTextureImpl class soon. 75 */ 76 enum FlagBits { 77 kFirstBit = (kLastPublic_GrTextureFlagBit << 1), 78 79 /** 80 * This texture should be returned to the texture cache when 81 * it is no longer reffed 82 */ 83 kReturnToCache_FlagBit = kFirstBit, 84 }; 85 86 void resetFlag(GrTextureFlags flags) { 87 fDesc.fFlags = fDesc.fFlags & ~flags; 88 } 89 90 #ifdef SK_DEBUG 91 void validate() const { 92 this->INHERITED::validate(); 93 this->validateDesc(); 94 } 95 #endif 96 97 GrTextureImpl* impl() { return reinterpret_cast<GrTextureImpl*>(this); } 98 const GrTextureImpl* impl() const { return reinterpret_cast<const GrTextureImpl*>(this); } 99 100 protected: 101 // A texture refs its rt representation but not vice-versa. It is up to 102 // the subclass constructor to initialize this pointer. 103 SkAutoTUnref<GrRenderTarget> fRenderTarget; 104 105 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) 106 : INHERITED(gpu, isWrapped, desc) 107 , fRenderTarget(NULL) { 108 // only make sense if alloc size is pow2 109 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); 110 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); 111 } 112 113 virtual ~GrTexture(); 114 115 // GrResource overrides 116 virtual void onRelease() SK_OVERRIDE; 117 virtual void onAbandon() SK_OVERRIDE; 118 119 void validateDesc() const; 120 121 private: 122 void abandonReleaseCommon(); 123 virtual void internal_dispose() const SK_OVERRIDE; 124 125 // these two shift a fixed-point value into normalized coordinates 126 // for this texture if the texture is power of two sized. 127 int fShiftFixedX; 128 int fShiftFixedY; 129 130 typedef GrSurface INHERITED; 131 }; 132 133 class GrTextureImpl : public GrTexture { 134 public: 135 SK_DECLARE_INST_COUNT(GrTextureImpl) 136 137 void setFlag(GrTextureFlags flags) { 138 fDesc.fFlags = fDesc.fFlags | flags; 139 } 140 void resetFlag(GrTextureFlags flags) { 141 fDesc.fFlags = fDesc.fFlags & ~flags; 142 } 143 bool isSetFlag(GrTextureFlags flags) const { 144 return 0 != (fDesc.fFlags & flags); 145 } 146 147 void dirtyMipMaps(bool mipMapsDirty); 148 149 bool mipMapsAreDirty() const { 150 return kValid_MipMapsStatus != fMipMapsStatus; 151 } 152 153 bool hasMipMaps() const { 154 return kNotAllocated_MipMapsStatus != fMipMapsStatus; 155 } 156 157 static GrResourceKey ComputeKey(const GrGpu* gpu, 158 const GrTextureParams* params, 159 const GrTextureDesc& desc, 160 const GrCacheID& cacheID); 161 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); 162 static bool NeedsResizing(const GrResourceKey& key); 163 static bool NeedsBilerp(const GrResourceKey& key); 164 165 protected: 166 GrTextureImpl(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc); 167 168 private: 169 enum MipMapsStatus { 170 kNotAllocated_MipMapsStatus, 171 kAllocated_MipMapsStatus, 172 kValid_MipMapsStatus 173 }; 174 175 MipMapsStatus fMipMapsStatus; 176 177 typedef GrTexture INHERITED; 178 }; 179 180 /** 181 * Represents a texture that is intended to be accessed in device coords with an offset. 182 */ 183 class GrDeviceCoordTexture { 184 public: 185 GrDeviceCoordTexture() { fOffset.set(0, 0); } 186 187 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { 188 *this = other; 189 } 190 191 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset) 192 : fTexture(SkSafeRef(texture)) 193 , fOffset(offset) { 194 } 195 196 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) { 197 fTexture.reset(SkSafeRef(other.fTexture.get())); 198 fOffset = other.fOffset; 199 return *this; 200 } 201 202 const SkIPoint& offset() const { return fOffset; } 203 204 void setOffset(const SkIPoint& offset) { fOffset = offset; } 205 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } 206 207 GrTexture* texture() const { return fTexture.get(); } 208 209 GrTexture* setTexture(GrTexture* texture) { 210 fTexture.reset(SkSafeRef(texture)); 211 return texture; 212 } 213 214 private: 215 SkAutoTUnref<GrTexture> fTexture; 216 SkIPoint fOffset; 217 }; 218 219 #endif 220