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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 10 | #ifndef GrGLTexture_DEFINED |
| 11 | #define GrGLTexture_DEFINED |
| 12 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame^] | 13 | #include "GrGLRenderTarget.h" |
bsalomon@google.com | 8895a7a | 2011-02-18 16:09:55 +0000 | [diff] [blame] | 14 | #include "GrScalar.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame^] | 15 | #include "GrTexture.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 17 | /** |
| 18 | * A ref counted tex id that deletes the texture in its destructor. |
| 19 | */ |
| 20 | class GrGLTexID : public GrRefCnt { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 22 | public: |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 23 | GrGLTexID(GrGLuint texID, bool ownsID) : fTexID(texID), fOwnsID(ownsID) {} |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 24 | |
| 25 | virtual ~GrGLTexID() { |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 26 | if (0 != fTexID && fOwnsID) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 27 | GR_GL(DeleteTextures(1, &fTexID)); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void abandon() { fTexID = 0; } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 32 | GrGLuint id() const { return fTexID; } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 33 | |
| 34 | private: |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 35 | GrGLuint fTexID; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 36 | bool fOwnsID; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 39 | //////////////////////////////////////////////////////////////////////////////// |
| 40 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 41 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | class GrGLTexture : public GrTexture { |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 43 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | public: |
| 45 | enum Orientation { |
| 46 | kBottomUp_Orientation, |
| 47 | kTopDown_Orientation, |
| 48 | }; |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 50 | struct TexParams { |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 51 | GrGLenum fFilter; |
| 52 | GrGLenum fWrapS; |
| 53 | GrGLenum fWrapT; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 54 | void invalidate() { memset(this, 0xff, sizeof(TexParams)); } |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 55 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 57 | struct GLTextureDesc { |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 58 | int fContentWidth; |
| 59 | int fContentHeight; |
| 60 | int fAllocWidth; |
| 61 | int fAllocHeight; |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 62 | GrPixelConfig fFormat; |
| 63 | GrGLuint fTextureID; |
| 64 | bool fOwnsID; |
| 65 | GrGLenum fUploadFormat; |
| 66 | GrGLenum fUploadByteCount; |
| 67 | GrGLenum fUploadType; |
| 68 | GrGLuint fStencilBits; |
| 69 | Orientation fOrientation; |
| 70 | }; |
| 71 | |
| 72 | typedef GrGLRenderTarget::GLRenderTargetIDs GLRenderTargetIDs; |
| 73 | |
| 74 | GrGLTexture(GrGpuGL* gpu, |
| 75 | const GLTextureDesc& textureDesc, |
| 76 | const GLRenderTargetIDs& rtIDs, |
| 77 | const TexParams& initialTexParams); |
| 78 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 79 | virtual ~GrGLTexture() { this->release(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 81 | // overrides of GrTexture |
bsalomon@google.com | 79d2dbe | 2011-06-13 19:28:02 +0000 | [diff] [blame] | 82 | virtual void uploadTextureData(int x, |
| 83 | int y, |
| 84 | int width, |
| 85 | int height, |
junov@google.com | 4ee7ae5 | 2011-06-30 17:30:49 +0000 | [diff] [blame] | 86 | const void* srcData, |
| 87 | size_t rowBytes); |
bsalomon@google.com | cee661a | 2011-07-26 12:32:36 +0000 | [diff] [blame] | 88 | virtual intptr_t getTextureHandle() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 90 | const TexParams& getTexParams() const { return fTexParams; } |
| 91 | void setTexParams(const TexParams& texParams) { fTexParams = texParams; } |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 92 | GrGLuint textureID() const { return fTexIDObj->id(); } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 94 | GrGLenum uploadFormat() const { return fUploadFormat; } |
| 95 | GrGLenum uploadByteCount() const { return fUploadByteCount; } |
| 96 | GrGLenum uploadType() const { return fUploadType; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Retrieves the texture width actually allocated in texels. |
| 100 | * |
| 101 | * @return the width in texels |
| 102 | */ |
| 103 | int allocWidth() const { return fAllocWidth; } |
| 104 | |
| 105 | /** |
| 106 | * Retrieves the texture height actually allocated in texels. |
| 107 | * |
| 108 | * @return the height in texels |
| 109 | */ |
| 110 | int allocHeight() const { return fAllocHeight; } |
| 111 | |
| 112 | /** |
| 113 | * @return width() / allocWidth() |
| 114 | */ |
| 115 | GrScalar contentScaleX() const { return fScaleX; } |
| 116 | |
| 117 | /** |
| 118 | * @return height() / allocHeight() |
| 119 | */ |
| 120 | GrScalar contentScaleY() const { return fScaleY; } |
| 121 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | // Ganesh assumes texture coordinates have their origin |
| 123 | // in the top-left corner of the image. OpenGL, however, |
| 124 | // has the origin in the lower-left corner. For content that |
| 125 | // is loaded by Ganesh we just push the content "upside down" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 126 | // (by GL's understanding of the world ) in glTex*Image and the |
| 127 | // addressing just works out. However, content generated by GL |
| 128 | // (FBO or externally imported texture) will be updside down |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 129 | // and it is up to the GrGpuGL derivative to handle y-mirroing. |
| 130 | Orientation orientation() const { return fOrientation; } |
| 131 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 132 | static const GrGLenum* WrapMode2GLWrap(); |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 133 | |
bsalomon@google.com | 5877ffd | 2011-04-11 17:58:48 +0000 | [diff] [blame] | 134 | protected: |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | 6dcf499 | 2011-04-05 21:16:14 +0000 | [diff] [blame] | 136 | // overrides of GrTexture |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 137 | virtual void onAbandon(); |
| 138 | virtual void onRelease(); |
| 139 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | private: |
bsalomon@google.com | da96ea0 | 2010-12-23 16:53:57 +0000 | [diff] [blame] | 141 | TexParams fTexParams; |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 142 | GrGLTexID* fTexIDObj; |
twiz@google.com | 0f31ca7 | 2011-03-18 17:38:11 +0000 | [diff] [blame] | 143 | GrGLenum fUploadFormat; |
| 144 | GrGLenum fUploadByteCount; |
| 145 | GrGLenum fUploadType; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 146 | int fAllocWidth; |
| 147 | int fAllocHeight; |
| 148 | // precomputed content / alloc ratios |
| 149 | GrScalar fScaleX; |
| 150 | GrScalar fScaleY; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | Orientation fOrientation; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 152 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | typedef GrTexture INHERITED; |
| 154 | }; |
| 155 | |
| 156 | #endif |