blob: 77f88fa110736cfdf3fb8904f20274632a5ccd69 [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.com0168afc2011-08-08 13:21:05 +000067 return fAllocatedWidth * fAllocatedHeight * GrBytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000068 }
69
70 /**
71 * Updates a subrectangle of texels in the texture.
72 *
junov@google.com4ee7ae52011-06-30 17:30:49 +000073 * @param x left edge of rectangle to update
74 * @param y top edge of rectangle to update
75 * @param width width of rectangle to update
76 * @param height height of rectangle to update
77 * @param srcData width*height texels of data in same format that was
78 * used at texture creation.
79 * @param rowBytes number of bytes per row in srcData, 0 means rows are
80 * packed
reed@google.comac10a2d2010-12-22 21:39:39 +000081 */
bsalomon@google.com79d2dbe2011-06-13 19:28:02 +000082 virtual void uploadTextureData(int x,
83 int y,
84 int width,
85 int height,
junov@google.com4ee7ae52011-06-30 17:30:49 +000086 const void* srcData,
87 size_t rowBytes) = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +000088
89 /**
bsalomon@google.com669fdc42011-04-05 17:08:27 +000090 * Reads a rectangle of pixels from the texture.
91 * @param left left edge of the rectangle to read (inclusive)
92 * @param top top edge of the rectangle to read (inclusive)
93 * @param width width of rectangle to read in pixels.
94 * @param height height of rectangle to read in pixels.
95 * @param config the pixel config of the destination buffer
96 * @param buffer memory to read the rectangle into.
97 *
98 * @return true if the read succeeded, false if not. The read can fail
99 * because of a unsupported pixel config.
100 */
101 bool readPixels(int left, int top, int width, int height,
102 GrPixelConfig config, void* buffer);
103
104 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 * Retrieves the render target underlying this texture that can be passed to
106 * GrGpu::setRenderTarget().
107 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000108 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 * render target
110 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000111 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000112
113 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000114 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000116 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000117 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000118 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000119
120 /**
121 * Return the native ID or handle to the texture, depending on the
122 * platform. e.g. on opengl, return the texture ID.
123 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000124 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126#if GR_DEBUG
127 void validate() const {
128 this->INHERITED::validate();
129 }
130#else
131 void validate() const {}
132#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000134protected:
135 GrRenderTarget* fRenderTarget; // texture refs its rt representation
136 // base class cons sets to NULL
137 // subclass cons can create and set
138
139 GrTexture(GrGpu* gpu,
140 int width,
141 int height,
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000142 int allocatedWidth,
143 int allocatedHeight,
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000144 GrPixelConfig config)
145 : INHERITED(gpu)
146 , fRenderTarget(NULL)
147 , fWidth(width)
148 , fHeight(height)
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000149 , fAllocatedWidth(allocatedWidth)
150 , fAllocatedHeight(allocatedHeight)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000151 , fConfig(config) {
152 // only make sense if alloc size is pow2
153 fShiftFixedX = 31 - Gr_clz(fWidth);
154 fShiftFixedY = 31 - Gr_clz(fHeight);
155 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000156
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000157 // GrResource overrides
158 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000159 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000160 }
161
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000162 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000163
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000164private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000165 int fWidth;
166 int fHeight;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000167 int fAllocatedWidth;
168 int fAllocatedHeight;
169
reed@google.comac10a2d2010-12-22 21:39:39 +0000170 // these two shift a fixed-point value into normalized coordinates
171 // for this texture if the texture is power of two sized.
172 int fShiftFixedX;
173 int fShiftFixedY;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000174
175 GrPixelConfig fConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000176
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178};
179
180#endif
181