blob: 8274140adc2055a083c50585e5a041374afcff17 [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 /**
36 * Convert from texels to normalized texture coords for POT textures
37 * only.
38 */
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000039 GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
reed@google.comac10a2d2010-12-22 21:39:39 +000040 return x >> fShiftFixedX; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000041 GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
reed@google.comac10a2d2010-12-22 21:39:39 +000042 return y >> fShiftFixedY; }
43
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000045 * Retrieves the pixel config specified when the texture was created.
46 */
bsalomon@google.com669fdc42011-04-05 17:08:27 +000047 GrPixelConfig config() const { return fConfig; }
reed@google.comac10a2d2010-12-22 21:39:39 +000048
49 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000050 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000051 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000052 virtual size_t sizeInBytes() const {
bsalomon@google.com99621082011-11-15 16:47:16 +000053 return (size_t) fWidth * fHeight * GrBytesPerPixel(fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000054 }
55
56 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000057 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000058 * @param left left edge of the rectangle to read (inclusive)
59 * @param top top edge of the rectangle to read (inclusive)
60 * @param width width of rectangle to read in pixels.
61 * @param height height of rectangle to read in pixels.
62 * @param config the pixel config of the destination buffer
63 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000064 * @param rowBytes number of bytes bewtween consecutive rows. Zero
65 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000066 *
67 * @return true if the read succeeded, false if not. The read can fail
68 * because of a unsupported pixel config.
69 */
70 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000071 GrPixelConfig config, void* buffer,
72 size_t rowBytes);
73
74 /**
75 * Writes a rectangle of pixels to the texture.
76 * @param left left edge of the rectangle to write (inclusive)
77 * @param top top edge of the rectangle to write (inclusive)
78 * @param width width of rectangle to write in pixels.
79 * @param height height of rectangle to write in pixels.
80 * @param config the pixel config of the source buffer
81 * @param buffer memory to read pixels from
82 * @param rowBytes number of bytes bewtween consecutive rows. Zero
83 * means rows are tightly packed.
84 */
85 void writePixels(int left, int top, int width, int height,
86 GrPixelConfig config, const void* buffer,
87 size_t rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000088
89 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000090 * Retrieves the render target underlying this texture that can be passed to
91 * GrGpu::setRenderTarget().
92 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000093 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +000094 * render target
95 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +000096 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
98 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +000099 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000101 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000103 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000104
105 /**
106 * Return the native ID or handle to the texture, depending on the
107 * platform. e.g. on opengl, return the texture ID.
108 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000109 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
111#if GR_DEBUG
112 void validate() const {
113 this->INHERITED::validate();
114 }
115#else
116 void validate() const {}
117#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000118
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000119protected:
120 GrRenderTarget* fRenderTarget; // texture refs its rt representation
121 // base class cons sets to NULL
122 // subclass cons can create and set
123
124 GrTexture(GrGpu* gpu,
125 int width,
126 int height,
127 GrPixelConfig config)
128 : INHERITED(gpu)
129 , fRenderTarget(NULL)
130 , fWidth(width)
131 , fHeight(height)
132 , fConfig(config) {
133 // only make sense if alloc size is pow2
134 fShiftFixedX = 31 - Gr_clz(fWidth);
135 fShiftFixedY = 31 - Gr_clz(fHeight);
136 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000137
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000138 // GrResource overrides
139 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000140 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000141 }
142
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000143 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000144
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000145private:
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000146 int fWidth;
147 int fHeight;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 // these two shift a fixed-point value into normalized coordinates
150 // for this texture if the texture is power of two sized.
151 int fShiftFixedX;
152 int fShiftFixedY;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000153
154 GrPixelConfig fConfig;
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157};
158
159#endif
160