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 | /** |
| 36 | * Convert from texels to normalized texture coords for POT textures |
| 37 | * only. |
| 38 | */ |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 39 | GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | return x >> fShiftFixedX; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 41 | GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | return y >> fShiftFixedY; } |
| 43 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 44 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | * Retrieves the pixel config specified when the texture was created. |
| 46 | */ |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 47 | GrPixelConfig config() const { return fConfig; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 52 | virtual size_t sizeInBytes() const { |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 53 | return (size_t) fWidth * fHeight * GrBytesPerPixel(fConfig); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 57 | * Read a rectangle of pixels from the texture. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 58 | * @param left left edge of the rectangle to read (inclusive) |
| 59 | * @param top top edge of the rectangle to read (inclusive) |
| 60 | * @param width width of rectangle to read in pixels. |
| 61 | * @param height height of rectangle to read in pixels. |
| 62 | * @param config the pixel config of the destination buffer |
| 63 | * @param buffer memory to read the rectangle into. |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 64 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 65 | * means rows are tightly packed. |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 66 | * |
| 67 | * @return true if the read succeeded, false if not. The read can fail |
| 68 | * because of a unsupported pixel config. |
| 69 | */ |
| 70 | bool readPixels(int left, int top, int width, int height, |
bsalomon@google.com | 6f37951 | 2011-11-16 20:36:03 +0000 | [diff] [blame^] | 71 | GrPixelConfig config, void* buffer, |
| 72 | size_t rowBytes); |
| 73 | |
| 74 | /** |
| 75 | * Writes a rectangle of pixels to the texture. |
| 76 | * @param left left edge of the rectangle to write (inclusive) |
| 77 | * @param top top edge of the rectangle to write (inclusive) |
| 78 | * @param width width of rectangle to write in pixels. |
| 79 | * @param height height of rectangle to write in pixels. |
| 80 | * @param config the pixel config of the source buffer |
| 81 | * @param buffer memory to read pixels from |
| 82 | * @param rowBytes number of bytes bewtween consecutive rows. Zero |
| 83 | * means rows are tightly packed. |
| 84 | */ |
| 85 | void writePixels(int left, int top, int width, int height, |
| 86 | GrPixelConfig config, const void* buffer, |
| 87 | size_t rowBytes); |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | * Retrieves the render target underlying this texture that can be passed to |
| 91 | * GrGpu::setRenderTarget(). |
| 92 | * |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 93 | * @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] | 94 | * render target |
| 95 | */ |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 96 | GrRenderTarget* asRenderTarget() { return fRenderTarget; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 99 | * Removes the reference on the associated GrRenderTarget held by this |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 100 | * texture. Afterwards asRenderTarget() will return NULL. The |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 101 | * GrRenderTarget survives the release if another ref is held on it. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | */ |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 103 | void releaseRenderTarget(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Return the native ID or handle to the texture, depending on the |
| 107 | * platform. e.g. on opengl, return the texture ID. |
| 108 | */ |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 109 | virtual intptr_t getTextureHandle() const = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | |
| 111 | #if GR_DEBUG |
| 112 | void validate() const { |
| 113 | this->INHERITED::validate(); |
| 114 | } |
| 115 | #else |
| 116 | void validate() const {} |
| 117 | #endif |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 118 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 119 | protected: |
| 120 | GrRenderTarget* fRenderTarget; // texture refs its rt representation |
| 121 | // base class cons sets to NULL |
| 122 | // subclass cons can create and set |
| 123 | |
| 124 | GrTexture(GrGpu* gpu, |
| 125 | int width, |
| 126 | int height, |
| 127 | GrPixelConfig config) |
| 128 | : INHERITED(gpu) |
| 129 | , fRenderTarget(NULL) |
| 130 | , fWidth(width) |
| 131 | , fHeight(height) |
| 132 | , fConfig(config) { |
| 133 | // only make sense if alloc size is pow2 |
| 134 | fShiftFixedX = 31 - Gr_clz(fWidth); |
| 135 | fShiftFixedY = 31 - Gr_clz(fHeight); |
| 136 | } |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 137 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 138 | // GrResource overrides |
| 139 | virtual void onRelease() { |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 140 | this->releaseRenderTarget(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 143 | virtual void onAbandon(); |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 144 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 145 | private: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 146 | int fWidth; |
| 147 | int fHeight; |
bsalomon@google.com | 0168afc | 2011-08-08 13:21:05 +0000 | [diff] [blame] | 148 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | // these two shift a fixed-point value into normalized coordinates |
| 150 | // for this texture if the texture is power of two sized. |
| 151 | int fShiftFixedX; |
| 152 | int fShiftFixedY; |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 153 | |
| 154 | GrPixelConfig fConfig; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 156 | typedef GrResource INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | #endif |
| 160 | |