• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
14 class GrRenderTarget;
15 class GrResourceKey;
16 class GrTextureParams;
17 
18 class GrTexture : public GrSurface {
19 
20 public:
21     SK_DECLARE_INST_COUNT(GrTexture)
22     // from GrResource
23     /**
24      * Informational texture flags
25      */
26     enum FlagBits {
27         kFirstBit = (kLastPublic_GrTextureFlagBit << 1),
28 
29         /**
30          * This texture should be returned to the texture cache when
31          * it is no longer reffed
32          */
33         kReturnToCache_FlagBit        = kFirstBit,
34     };
35 
setFlag(GrTextureFlags flags)36     void setFlag(GrTextureFlags flags) {
37         fDesc.fFlags = fDesc.fFlags | flags;
38     }
resetFlag(GrTextureFlags flags)39     void resetFlag(GrTextureFlags flags) {
40         fDesc.fFlags = fDesc.fFlags & ~flags;
41     }
isSetFlag(GrTextureFlags flags)42     bool isSetFlag(GrTextureFlags flags) const {
43         return 0 != (fDesc.fFlags & flags);
44     }
45 
46     /**
47      *  Approximate number of bytes used by the texture
48      */
sizeInBytes()49     virtual size_t sizeInBytes() const SK_OVERRIDE {
50         return (size_t) fDesc.fWidth *
51                         fDesc.fHeight *
52                         GrBytesPerPixel(fDesc.fConfig);
53     }
54 
55     // GrSurface overrides
56     virtual bool readPixels(int left, int top, int width, int height,
57                             GrPixelConfig config,
58                             void* buffer,
59                             size_t rowBytes = 0,
60                             uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
61 
62     virtual void writePixels(int left, int top, int width, int height,
63                              GrPixelConfig config,
64                              const void* buffer,
65                              size_t rowBytes = 0,
66                              uint32_t pixelOpsFlags = 0) SK_OVERRIDE;
67 
68     /**
69      * @return this texture
70      */
asTexture()71     virtual GrTexture* asTexture() SK_OVERRIDE { return this; }
asTexture()72     virtual const GrTexture* asTexture() const SK_OVERRIDE { return this; }
73 
74     /**
75      * Retrieves the render target underlying this texture that can be passed to
76      * GrGpu::setRenderTarget().
77      *
78      * @return    handle to render target or NULL if the texture is not a
79      *            render target
80      */
asRenderTarget()81     virtual GrRenderTarget* asRenderTarget() SK_OVERRIDE {
82         return fRenderTarget;
83     }
asRenderTarget()84     virtual const GrRenderTarget* asRenderTarget() const SK_OVERRIDE {
85         return fRenderTarget;
86     }
87 
88     // GrTexture
89     /**
90      * Convert from texels to normalized texture coords for POT textures
91      * only.
92      */
normalizeFixedX(GrFixed x)93     GrFixed normalizeFixedX(GrFixed x) const {
94         GrAssert(GrIsPow2(fDesc.fWidth));
95         return x >> fShiftFixedX;
96     }
normalizeFixedY(GrFixed y)97     GrFixed normalizeFixedY(GrFixed y) const {
98         GrAssert(GrIsPow2(fDesc.fHeight));
99         return y >> fShiftFixedY;
100     }
101 
102     /**
103      * Removes the reference on the associated GrRenderTarget held by this
104      * texture. Afterwards asRenderTarget() will return NULL. The
105      * GrRenderTarget survives the release if another ref is held on it.
106      */
107     void releaseRenderTarget();
108 
109     /**
110      *  Return the native ID or handle to the texture, depending on the
111      *  platform. e.g. on OpenGL, return the texture ID.
112      */
113     virtual GrBackendObject getTextureHandle() const = 0;
114 
115     /**
116      *  Call this when the state of the native API texture object is
117      *  altered directly, without being tracked by skia.
118      */
119     virtual void invalidateCachedState() = 0;
120 
121 #if GR_DEBUG
validate()122     void validate() const {
123         this->INHERITED::validate();
124 
125         this->validateDesc();
126     }
127 #else
validate()128     void validate() const {}
129 #endif
130     static GrResourceKey ComputeKey(const GrGpu* gpu,
131                                     const GrTextureParams* params,
132                                     const GrTextureDesc& desc,
133                                     const GrCacheID& cacheID);
134     static GrResourceKey ComputeScratchKey(const GrTextureDesc& desc);
135     static bool NeedsResizing(const GrResourceKey& key);
136     static bool NeedsFiltering(const GrResourceKey& key);
137 
138 protected:
139     GrRenderTarget* fRenderTarget; // texture refs its rt representation
140                                    // base class cons sets to NULL
141                                    // subclass cons can create and set
142 
GrTexture(GrGpu * gpu,bool isWrapped,const GrTextureDesc & desc,GrSurfaceOrigin origin)143     GrTexture(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc, GrSurfaceOrigin origin)
144     : INHERITED(gpu, isWrapped, desc, origin)
145     , fRenderTarget(NULL) {
146 
147         // only make sense if alloc size is pow2
148         fShiftFixedX = 31 - SkCLZ(fDesc.fWidth);
149         fShiftFixedY = 31 - SkCLZ(fDesc.fHeight);
150     }
151 
152     // GrResource overrides
153     virtual void onRelease() SK_OVERRIDE;
154     virtual void onAbandon() SK_OVERRIDE;
155 
156     void validateDesc() const;
157 
158 private:
159     // these two shift a fixed-point value into normalized coordinates
160     // for this texture if the texture is power of two sized.
161     int                 fShiftFixedX;
162     int                 fShiftFixedY;
163 
164     virtual void internal_dispose() const SK_OVERRIDE;
165 
166     typedef GrSurface INHERITED;
167 };
168 
169 #endif
170