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 "SkPoint.h" 14 #include "GrRenderTarget.h" 15 16 class GrResourceKey; 17 class GrTextureParams; 18 19 class GrTexture : public GrSurface { 20 21 public: 22 SK_DECLARE_INST_COUNT(GrTexture) 23 // from GrResource 24 /** 25 * Informational texture flags 26 */ 27 enum FlagBits { 28 kFirstBit = (kLastPublic_GrTextureFlagBit << 1), 29 30 /** 31 * This texture should be returned to the texture cache when 32 * it is no longer reffed 33 */ 34 kReturnToCache_FlagBit = kFirstBit, 35 }; 36 37 void setFlag(GrTextureFlags flags) { 38 fDesc.fFlags = fDesc.fFlags | flags; 39 } 40 void resetFlag(GrTextureFlags flags) { 41 fDesc.fFlags = fDesc.fFlags & ~flags; 42 } 43 bool isSetFlag(GrTextureFlags flags) const { 44 return 0 != (fDesc.fFlags & flags); 45 } 46 47 void dirtyMipMaps(bool mipMapsDirty) { 48 fMipMapsDirty = mipMapsDirty; 49 } 50 51 bool mipMapsAreDirty() const { 52 return fMipMapsDirty; 53 } 54 55 /** 56 * Approximate number of bytes used by the texture 57 */ 58 virtual size_t sizeInBytes() const SK_OVERRIDE { 59 return (size_t) fDesc.fWidth * 60 fDesc.fHeight * 61 GrBytesPerPixel(fDesc.fConfig); 62 } 63 64 // GrSurface overrides 65 virtual bool readPixels(int left, int top, int width, int height, 66 GrPixelConfig config, 67 void* buffer, 68 size_t rowBytes = 0, 69 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; 70 71 virtual void writePixels(int left, int top, int width, int height, 72 GrPixelConfig config, 73 const void* buffer, 74 size_t rowBytes = 0, 75 uint32_t pixelOpsFlags = 0) SK_OVERRIDE; 76 77 /** 78 * @return this texture 79 */ 80 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } 81 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } 82 83 /** 84 * Retrieves the render target underlying this texture that can be passed to 85 * GrGpu::setRenderTarget(). 86 * 87 * @return handle to render target or NULL if the texture is not a 88 * render target 89 */ 90 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { 91 return fRenderTarget.get(); 92 } 93 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { 94 return fRenderTarget.get(); 95 } 96 97 // GrTexture 98 /** 99 * Convert from texels to normalized texture coords for POT textures 100 * only. 101 */ 102 GrFixed normalizeFixedX(GrFixed x) const { 103 GrAssert(GrIsPow2(fDesc.fWidth)); 104 return x >> fShiftFixedX; 105 } 106 GrFixed normalizeFixedY(GrFixed y) const { 107 GrAssert(GrIsPow2(fDesc.fHeight)); 108 return y >> fShiftFixedY; 109 } 110 111 /** 112 * Return the native ID or handle to the texture, depending on the 113 * platform. e.g. on OpenGL, return the texture ID. 114 */ 115 virtual GrBackendObject getTextureHandle() const = 0; 116 117 /** 118 * Call this when the state of the native API texture object is 119 * altered directly, without being tracked by skia. 120 */ 121 virtual void invalidateCachedState() = 0; 122 123 #if GR_DEBUG 124 void validate() const { 125 this->INHERITED::validate(); 126 127 this->validateDesc(); 128 } 129 #else 130 void validate() const {} 131 #endif 132 static GrResourceKey ComputeKey(const GrGpu* gpu, 133 const GrTextureParams* params, 134 const GrTextureDesc& desc, 135 const GrCacheID& cacheID); 136 static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc); 137 static bool NeedsResizing(const GrResourceKey& key); 138 static bool NeedsBilerp(const GrResourceKey& key); 139 140 protected: 141 // A texture refs its rt representation but not vice-versa. It is up to 142 // the subclass constructor to initialize this pointer. 143 SkAutoTUnref<GrRenderTarget> fRenderTarget; 144 145 GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc) 146 : INHERITED(gpu, isWrapped, desc) 147 , fRenderTarget(NULL) 148 , fMipMapsDirty(true) { 149 150 // only make sense if alloc size is pow2 151 fShiftFixedX = 31 - SkCLZ(fDesc.fWidth); 152 fShiftFixedY = 31 - SkCLZ(fDesc.fHeight); 153 } 154 virtual ~GrTexture(); 155 156 // GrResource overrides 157 virtual void onRelease() SK_OVERRIDE; 158 virtual void onAbandon() SK_OVERRIDE; 159 160 void validateDesc() const; 161 162 private: 163 // these two shift a fixed-point value into normalized coordinates 164 // for this texture if the texture is power of two sized. 165 int fShiftFixedX; 166 int fShiftFixedY; 167 168 bool fMipMapsDirty; 169 170 virtual void internal_dispose() const SK_OVERRIDE; 171 172 typedef GrSurface INHERITED; 173 }; 174 175 /** 176 * Represents a texture that is intended to be accessed in device coords with an offset. 177 */ 178 class GrDeviceCoordTexture { 179 public: 180 GrDeviceCoordTexture() { fOffset.set(0, 0); } 181 182 GrDeviceCoordTexture(const GrDeviceCoordTexture& other) { 183 *this = other; 184 } 185 186 GrDeviceCoordTexture(GrTexture* texture, const SkIPoint& offset) 187 : fTexture(SkSafeRef(texture)) 188 , fOffset(offset) { 189 } 190 191 GrDeviceCoordTexture& operator=(const GrDeviceCoordTexture& other) { 192 fTexture.reset(SkSafeRef(other.fTexture.get())); 193 fOffset = other.fOffset; 194 return *this; 195 } 196 197 const SkIPoint& offset() const { return fOffset; } 198 199 void setOffset(const SkIPoint& offset) { fOffset = offset; } 200 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } 201 202 GrTexture* texture() const { return fTexture.get(); } 203 204 GrTexture* setTexture(GrTexture* texture) { 205 fTexture.reset(SkSafeRef(texture)); 206 return texture; 207 } 208 private: 209 SkAutoTUnref<GrTexture> fTexture; 210 SkIPoint fOffset; 211 }; 212 213 #endif 214