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