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 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrTexture_DEFINED |
| 12 | #define GrTexture_DEFINED |
| 13 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 14 | #include "GrResource.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 16 | class GrRenderTarget; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 18 | class GrTexture : public GrResource { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 19 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | public: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 22 | * Retrieves the width of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 23 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | * @return the width in texels |
| 25 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 26 | int width() const { return fWidth; } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 27 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 29 | * Retrieves the height of the texture. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 30 | * |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 31 | * @return the height in texels |
| 32 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 33 | int height() const { return fHeight; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 36 | * Retrieves the allocated width. It may differ from width for |
| 37 | * NPOT or min-RT size reasons. |
| 38 | * @return allocated width in texels |
| 39 | */ |
| 40 | int allocatedWidth() const { return fAllocatedWidth; } |
| 41 | |
| 42 | /** |
| 43 | * Retrieves the allocated height. It may differ from height for |
| 44 | * NPOT or min-RT size reasons. |
| 45 | * @return allocated height in texels |
| 46 | */ |
| 47 | int allocatedHeight() const { return fAllocatedHeight; } |
| 48 | |
| 49 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | * Convert from texels to normalized texture coords for POT textures |
| 51 | * only. |
| 52 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 53 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | return x >> fShiftFixedX; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 55 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | return y >> fShiftFixedY; } |
| 57 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 58 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | * Retrieves the pixel config specified when the texture was created. |
| 60 | */ |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 61 | GrPixelConfig config() const { return fConfig; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 64 | * Approximate number of bytes used by the texture |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 65 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 66 | virtual size_t sizeInBytes() const { |
bsalomon@google.com | 558a75b | 2011-08-08 17:01:14 +0000 | [diff] [blame] | 67 | return (size_t) fAllocatedWidth * |
| 68 | fAllocatedHeight * |
| 69 | GrBytesPerPixel(fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Updates a subrectangle of texels in the texture. |
| 74 | * |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 75 | * @param x left edge of rectangle to update |
| 76 | * @param y top edge of rectangle to update |
| 77 | * @param width width of rectangle to update |
| 78 | * @param height height of rectangle to update |
| 79 | * @param srcData width*height texels of data in same format that was |
| 80 | * used at texture creation. |
| 81 | * @param rowBytes number of bytes per row in srcData, 0 means rows are |
| 82 | * packed |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | */ |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 84 | virtual void uploadTextureData(int x, |
| 85 | int y, |
| 86 | int width, |
| 87 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 88 | const void* srcData, |
| 89 | size_t rowBytes) = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 92 | * Reads a rectangle of pixels from the texture. |
| 93 | * @param left left edge of the rectangle to read (inclusive) |
| 94 | * @param top top edge of the rectangle to read (inclusive) |
| 95 | * @param width width of rectangle to read in pixels. |
| 96 | * @param height height of rectangle to read in pixels. |
| 97 | * @param config the pixel config of the destination buffer |
| 98 | * @param buffer memory to read the rectangle into. |
| 99 | * |
| 100 | * @return true if the read succeeded, false if not. The read can fail |
| 101 | * because of a unsupported pixel config. |
| 102 | */ |
| 103 | bool readPixels(int left, int top, int width, int height, |
| 104 | GrPixelConfig config, void* buffer); |
| 105 | |
| 106 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | * Retrieves the render target underlying this texture that can be passed to |
| 108 | * GrGpu::setRenderTarget(). |
| 109 | * |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 110 | * @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] | 111 | * render target |
| 112 | */ |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 113 | GrRenderTarget* asRenderTarget() { return fRenderTarget; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 116 | * Removes the reference on the associated GrRenderTarget held by this |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 117 | * texture. Afterwards asRenderTarget() will return NULL. The |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 118 | * GrRenderTarget survives the release if another ref is held on it. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | */ |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 120 | void releaseRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Return the native ID or handle to the texture, depending on the |
| 124 | * platform. e.g. on opengl, return the texture ID. |
| 125 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 126 | virtual intptr_t getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | |
| 128 | #if GR_DEBUG |
| 129 | void validate() const { |
| 130 | this->INHERITED::validate(); |
| 131 | } |
| 132 | #else |
| 133 | void validate() const {} |
| 134 | #endif |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 136 | protected: |
| 137 | GrRenderTarget* fRenderTarget; // texture refs its rt representation |
| 138 | // base class cons sets to NULL |
| 139 | // subclass cons can create and set |
| 140 | |
| 141 | GrTexture(GrGpu* gpu, |
| 142 | int width, |
| 143 | int height, |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 144 | int allocatedWidth, |
| 145 | int allocatedHeight, |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 146 | GrPixelConfig config) |
| 147 | : INHERITED(gpu) |
| 148 | , fRenderTarget(NULL) |
| 149 | , fWidth(width) |
| 150 | , fHeight(height) |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 151 | , fAllocatedWidth(allocatedWidth) |
| 152 | , fAllocatedHeight(allocatedHeight) |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 153 | , fConfig(config) { |
| 154 | // only make sense if alloc size is pow2 |
| 155 | fShiftFixedX = 31 - Gr_clz(fWidth); |
| 156 | fShiftFixedY = 31 - Gr_clz(fHeight); |
| 157 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 158 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 159 | // GrResource overrides |
| 160 | virtual void onRelease() { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 161 | this->releaseRenderTarget(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 164 | virtual void onAbandon(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 165 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 166 | private: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 167 | int fWidth; |
| 168 | int fHeight; |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 169 | int fAllocatedWidth; |
| 170 | int fAllocatedHeight; |
| 171 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | // these two shift a fixed-point value into normalized coordinates |
| 173 | // for this texture if the texture is power of two sized. |
| 174 | int fShiftFixedX; |
| 175 | int fShiftFixedY; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 176 | |
| 177 | GrPixelConfig fConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 179 | typedef GrResource INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | #endif |
| 183 | |