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