GrTexture.h revision b8670998a59d305cd22a3c0cbdc6e075b0a37a6e
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 14class GrRenderTarget; 15class GrResourceKey; 16class GrTextureParams; 17 18/* 19 * All uncached textures should have this value as their fClientCacheID 20 */ 21static const uint64_t kUncached_CacheID = 0xAAAAAAAA; 22 23/* 24 * Scratch textures should all have this value as their fClientCacheID 25 */ 26static const uint64_t kScratch_CacheID = 0xBBBBBBBB; 27 28 29class GrTexture : public GrSurface { 30 31public: 32 SK_DECLARE_INST_COUNT(GrTexture) 33 34 // from GrResource 35 /** 36 * Informational texture flags 37 */ 38 enum FlagBits { 39 kFirstBit = (kLastPublic_GrTextureFlagBit << 1), 40 41 /** 42 * This texture should be returned to the texture cache when 43 * it is no longer reffed 44 */ 45 kReturnToCache_FlagBit = kFirstBit, 46 }; 47 48 void setFlag(GrTextureFlags flags) { 49 fDesc.fFlags = fDesc.fFlags | flags; 50 } 51 void resetFlag(GrTextureFlags flags) { 52 fDesc.fFlags = fDesc.fFlags & ~flags; 53 } 54 bool isSetFlag(GrTextureFlags flags) const { 55 return 0 != (fDesc.fFlags & flags); 56 } 57 58 /** 59 * Approximate number of bytes used by the texture 60 */ 61 virtual size_t sizeInBytes() const SK_OVERRIDE { 62 return (size_t) fDesc.fWidth * 63 fDesc.fHeight * 64 GrBytesPerPixel(fDesc.fConfig); 65 } 66 67 // from GrSurface 68 /** 69 * Read a rectangle of pixels from the texture. 70 * @param left left edge of the rectangle to read (inclusive) 71 * @param top top edge of the rectangle to read (inclusive) 72 * @param width width of rectangle to read in pixels. 73 * @param height height of rectangle to read in pixels. 74 * @param config the pixel config of the destination buffer 75 * @param buffer memory to read the rectangle into. 76 * @param rowBytes number of bytes bewtween consecutive rows. Zero 77 * means rows are tightly packed. 78 * 79 * @return true if the read succeeded, false if not. The read can fail 80 * because of a unsupported pixel config. 81 */ 82 virtual bool readPixels(int left, int top, int width, int height, 83 GrPixelConfig config, void* buffer, 84 size_t rowBytes) SK_OVERRIDE; 85 86 /** 87 * Writes a rectangle of pixels to the texture. 88 * @param left left edge of the rectangle to write (inclusive) 89 * @param top top edge of the rectangle to write (inclusive) 90 * @param width width of rectangle to write in pixels. 91 * @param height height of rectangle to write in pixels. 92 * @param config the pixel config of the source buffer 93 * @param buffer memory to read pixels from 94 * @param rowBytes number of bytes between consecutive rows. Zero 95 * means rows are tightly packed. 96 */ 97 virtual void writePixels(int left, int top, int width, int height, 98 GrPixelConfig config, const void* buffer, 99 size_t rowBytes) SK_OVERRIDE; 100 101 /** 102 * @return this texture 103 */ 104 virtual GrTexture* asTexture() SK_OVERRIDE { return this; } 105 virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; } 106 107 /** 108 * Retrieves the render target underlying this texture that can be passed to 109 * GrGpu::setRenderTarget(). 110 * 111 * @return handle to render target or NULL if the texture is not a 112 * render target 113 */ 114 virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE { 115 return fRenderTarget; 116 } 117 virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE { 118 return fRenderTarget; 119 } 120 121 // GrTexture 122 /** 123 * Convert from texels to normalized texture coords for POT textures 124 * only. 125 */ 126 GrFixed normalizeFixedX(GrFixed x) const { 127 GrAssert(GrIsPow2(fDesc.fWidth)); 128 return x >> fShiftFixedX; 129 } 130 GrFixed normalizeFixedY(GrFixed y) const { 131 GrAssert(GrIsPow2(fDesc.fHeight)); 132 return y >> fShiftFixedY; 133 } 134 135 /** 136 * Removes the reference on the associated GrRenderTarget held by this 137 * texture. Afterwards asRenderTarget() will return NULL. The 138 * GrRenderTarget survives the release if another ref is held on it. 139 */ 140 void releaseRenderTarget(); 141 142 /** 143 * Return the native ID or handle to the texture, depending on the 144 * platform. e.g. on opengl, return the texture ID. 145 */ 146 virtual intptr_t getTextureHandle() const = 0; 147 148 /** 149 * Call this when the state of the native API texture object is 150 * altered directly, without being tracked by skia. 151 */ 152 virtual void invalidateCachedState() = 0; 153 154#if GR_DEBUG 155 void validate() const { 156 this->INHERITED::validate(); 157 158 this->validateDesc(); 159 } 160#else 161 void validate() const {} 162#endif 163 164 static GrResourceKey ComputeKey(const GrGpu* gpu, 165 const GrTextureParams* sampler, 166 const GrTextureDesc& desc, 167 bool scratch); 168 169 static bool NeedsResizing(const GrResourceKey& key); 170 static bool IsScratchTexture(const GrResourceKey& key); 171 static bool NeedsFiltering(const GrResourceKey& key); 172 173protected: 174 GrRenderTarget* fRenderTarget; // texture refs its rt representation 175 // base class cons sets to NULL 176 // subclass cons can create and set 177 178 GrTexture(GrGpu* gpu, const GrTextureDesc& desc) 179 : INHERITED(gpu, desc) 180 , fRenderTarget(NULL) { 181 182 // only make sense if alloc size is pow2 183 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth); 184 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight); 185 } 186 187 // GrResource overrides 188 virtual void onRelease() SK_OVERRIDE; 189 virtual void onAbandon() SK_OVERRIDE; 190 191 void validateDesc() const; 192 193private: 194 // these two shift a fixed-point value into normalized coordinates 195 // for this texture if the texture is power of two sized. 196 int fShiftFixedX; 197 int fShiftFixedY; 198 199 virtual void internal_dispose() const SK_OVERRIDE; 200 201 typedef GrSurface INHERITED; 202}; 203 204#endif 205 206