blob: e5c67d674b4bb1eb9805f5361fb8a89d2cb935e7 [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 */
robertphillips@google.com32716282012-06-04 12:48:45 +000026 int width() const { return fDesc.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 */
robertphillips@google.com32716282012-06-04 12:48:45 +000033 int height() const { return fDesc.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 */
robertphillips@google.com32716282012-06-04 12:48:45 +000039 GrFixed normalizeFixedX(GrFixed x) const {
40 GrAssert(GrIsPow2(fDesc.fWidth));
41 return x >> fShiftFixedX;
42 }
43 GrFixed normalizeFixedY(GrFixed y) const {
44 GrAssert(GrIsPow2(fDesc.fHeight));
45 return y >> fShiftFixedY;
46 }
reed@google.comac10a2d2010-12-22 21:39:39 +000047
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000049 * Retrieves the pixel config specified when the texture was created.
50 */
robertphillips@google.com32716282012-06-04 12:48:45 +000051 GrPixelConfig config() const { return fDesc.fConfig; }
52
53 /**
54 * Return the descriptor describing the texture
55 */
56 const GrTextureDesc& desc() const { return fDesc; }
reed@google.comac10a2d2010-12-22 21:39:39 +000057
58 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000059 * Approximate number of bytes used by the texture
reed@google.comac10a2d2010-12-22 21:39:39 +000060 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +000061 virtual size_t sizeInBytes() const {
robertphillips@google.com32716282012-06-04 12:48:45 +000062 return (size_t) fDesc.fWidth *
63 fDesc.fHeight *
64 GrBytesPerPixel(fDesc.fConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 }
66
67 /**
bsalomon@google.com6f379512011-11-16 20:36:03 +000068 * Read a rectangle of pixels from the texture.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000069 * @param left left edge of the rectangle to read (inclusive)
70 * @param top top edge of the rectangle to read (inclusive)
71 * @param width width of rectangle to read in pixels.
72 * @param height height of rectangle to read in pixels.
73 * @param config the pixel config of the destination buffer
74 * @param buffer memory to read the rectangle into.
bsalomon@google.com6f379512011-11-16 20:36:03 +000075 * @param rowBytes number of bytes bewtween consecutive rows. Zero
76 * means rows are tightly packed.
bsalomon@google.com669fdc42011-04-05 17:08:27 +000077 *
78 * @return true if the read succeeded, false if not. The read can fail
79 * because of a unsupported pixel config.
80 */
81 bool readPixels(int left, int top, int width, int height,
bsalomon@google.com6f379512011-11-16 20:36:03 +000082 GrPixelConfig config, void* buffer,
83 size_t rowBytes);
84
85 /**
86 * Writes a rectangle of pixels to the texture.
87 * @param left left edge of the rectangle to write (inclusive)
88 * @param top top edge of the rectangle to write (inclusive)
89 * @param width width of rectangle to write in pixels.
90 * @param height height of rectangle to write in pixels.
91 * @param config the pixel config of the source buffer
92 * @param buffer memory to read pixels from
robertphillips@google.com09042b82012-04-06 20:01:46 +000093 * @param rowBytes number of bytes between consecutive rows. Zero
bsalomon@google.com6f379512011-11-16 20:36:03 +000094 * means rows are tightly packed.
95 */
96 void writePixels(int left, int top, int width, int height,
97 GrPixelConfig config, const void* buffer,
98 size_t rowBytes);
bsalomon@google.com669fdc42011-04-05 17:08:27 +000099
100 /**
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 * Retrieves the render target underlying this texture that can be passed to
102 * GrGpu::setRenderTarget().
103 *
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000104 * @return handle to render target or NULL if the texture is not a
reed@google.comac10a2d2010-12-22 21:39:39 +0000105 * render target
106 */
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000107 GrRenderTarget* asRenderTarget() { return fRenderTarget; }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000108 const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
110 /**
bsalomon@google.com1da07462011-03-10 14:51:57 +0000111 * Removes the reference on the associated GrRenderTarget held by this
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112 * texture. Afterwards asRenderTarget() will return NULL. The
bsalomon@google.com1da07462011-03-10 14:51:57 +0000113 * GrRenderTarget survives the release if another ref is held on it.
reed@google.comac10a2d2010-12-22 21:39:39 +0000114 */
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000115 void releaseRenderTarget();
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
117 /**
118 * Return the native ID or handle to the texture, depending on the
119 * platform. e.g. on opengl, return the texture ID.
120 */
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000121 virtual intptr_t getTextureHandle() const = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123#if GR_DEBUG
124 void validate() const {
125 this->INHERITED::validate();
robertphillips@google.com32716282012-06-04 12:48:45 +0000126
127 this->validateDesc();
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 }
129#else
130 void validate() const {}
131#endif
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000132
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000133protected:
134 GrRenderTarget* fRenderTarget; // texture refs its rt representation
135 // base class cons sets to NULL
136 // subclass cons can create and set
137
robertphillips@google.com32716282012-06-04 12:48:45 +0000138 GrTexture(GrGpu* gpu, const GrTextureDesc& desc)
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000139 : INHERITED(gpu)
140 , fRenderTarget(NULL)
robertphillips@google.com32716282012-06-04 12:48:45 +0000141 , fDesc(desc) {
142
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000143 // only make sense if alloc size is pow2
robertphillips@google.com32716282012-06-04 12:48:45 +0000144 fShiftFixedX = 31 - Gr_clz(fDesc.fWidth);
145 fShiftFixedY = 31 - Gr_clz(fDesc.fHeight);
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000146 }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000147
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000148 // GrResource overrides
149 virtual void onRelease() {
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000150 this->releaseRenderTarget();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000151 }
152
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000153 virtual void onAbandon();
bsalomon@google.com6dcf4992011-04-05 21:16:14 +0000154
robertphillips@google.com32716282012-06-04 12:48:45 +0000155 void validateDesc() const;
156
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000157private:
robertphillips@google.com32716282012-06-04 12:48:45 +0000158 GrTextureDesc fDesc;
bsalomon@google.com0168afc2011-08-08 13:21:05 +0000159
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 // these two shift a fixed-point value into normalized coordinates
161 // for this texture if the texture is power of two sized.
robertphillips@google.com32716282012-06-04 12:48:45 +0000162 int fShiftFixedX;
163 int fShiftFixedY;
reed@google.comac10a2d2010-12-22 21:39:39 +0000164
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000165 typedef GrResource INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000166};
167
168#endif
169