blob: 5a86b0881bab0a0df0af2d3eaf0d5a3a3be6dc2f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTexture_DEFINED
12#define GrTexture_DEFINED
13
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014#include "GrResource.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000016class GrRenderTarget;
reed@google.comac10a2d2010-12-22 21:39:39 +000017
bsalomon@google.com8fe72472011-03-30 21:26:44 +000018class GrTexture : public GrResource {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000019
reed@google.comac10a2d2010-12-22 21:39:39 +000020public:
reed@google.comac10a2d2010-12-22 21:39:39 +000021 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000022 * Retrieves the width of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000023 *
reed@google.comac10a2d2010-12-22 21:39:39 +000024 * @return the width in texels
25 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000026 int width() const { return fWidth; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000027
reed@google.comac10a2d2010-12-22 21:39:39 +000028 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000029 * Retrieves the height of the texture.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000030 *
reed@google.comac10a2d2010-12-22 21:39:39 +000031 * @return the height in texels
32 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000033 int height() const { return fHeight; }
reed@google.comac10a2d2010-12-22 21:39:39 +000034
35 /**
bsalomon@google.com0168afc2011-08-08 13:21:05 +000036 * 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.comac10a2d2010-12-22 21:39:39 +000050 * Convert from texels to normalized texture coords for POT textures
51 * only.
52 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000053 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +000054 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000055 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +000056 return y >> fShiftFixedY; }
57
bsalomon@google.com1c13c962011-02-14 16:51:21 +000058 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000059 * Retrieves the pixel config specified when the texture was created.
60 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +000061 GrPixelConfig config() const { return fConfig; }
reed@google.comac10a2d2010-12-22 21:39:39 +000062
63 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000064 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000065 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000066 virtual size_t sizeInBytes() const {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000067 return (size_t) fAllocatedWidth *
68 fAllocatedHeight *
69 GrBytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000070 }
71
72 /**
73 * Updates a subrectangle of texels in the texture.
74 *
junov@google.com4ee7ae52011-06-30 17:30:49 +000075 * @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.comac10a2d2010-12-22 21:39:39 +000083 */
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000084 virtual void uploadTextureData(int x,
85 int y,
86 int width,
87 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000088 const void* srcData,
89 size_t rowBytes) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000090
91 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +000092 * 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.comac10a2d2010-12-22 21:39:39 +0000107 * Retrieves the render target underlying this texture that can be passed to
108 * GrGpu::setRenderTarget().
109 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000110 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 * render target
112 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000113 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
115 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000116 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000117 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000118 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000120 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
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.comcee661a2011-07-26 12:32:36 +0000126 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
128#if GR_DEBUG
129 void validate() const {
130 this->INHERITED::validate();
131 }
132#else
133 void validate() const {}
134#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000135
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000136protected:
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.com0168afc2011-08-08 13:21:05 +0000144 int allocatedWidth,
145 int allocatedHeight,
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000146 GrPixelConfig config)
147 : INHERITED(gpu)
148 , fRenderTarget(NULL)
149 , fWidth(width)
150 , fHeight(height)
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000151 , fAllocatedWidth(allocatedWidth)
152 , fAllocatedHeight(allocatedHeight)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000153 , 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.comcee661a2011-07-26 12:32:36 +0000158
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000159 // GrResource overrides
160 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000161 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000162 }
163
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000164 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000165
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000166private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000167 int fWidth;
168 int fHeight;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000169 int fAllocatedWidth;
170 int fAllocatedHeight;
171
reed@google.comac10a2d2010-12-22 21:39:39 +0000172 // 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.com669fdc42011-04-05 17:08:27 +0000176
177 GrPixelConfig fConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000180};
181
182#endif
183