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