GrTexture.h revision 09042b80d22837c760bb530124aaa67469b19b8f
1
2/*
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.
7 */
8
9
10
11#ifndef GrTexture_DEFINED
12#define GrTexture_DEFINED
13
14#include "GrResource.h"
15
16class GrRenderTarget;
17
18class GrTexture : public GrResource {
19
20public:
21    /**
22     * Retrieves the width of the texture.
23     *
24     * @return the width in texels
25     */
26    int width() const { return fWidth; }
27
28    /**
29     * Retrieves the height of the texture.
30     *
31     * @return the height in texels
32     */
33    int height() const { return fHeight; }
34
35    /**
36     * Convert from texels to normalized texture coords for POT textures
37     * only.
38     */
39    GrFixed normalizeFixedX(GrFixed x) const { GrAssert(GrIsPow2(fWidth));
40                                               return x >> fShiftFixedX; }
41    GrFixed normalizeFixedY(GrFixed y) const { GrAssert(GrIsPow2(fHeight));
42                                               return y >> fShiftFixedY; }
43
44    /**
45     * Retrieves the pixel config specified when the texture was created.
46     */
47    GrPixelConfig config() const { return fConfig; }
48
49    /**
50     *  Approximate number of bytes used by the texture
51     */
52    virtual size_t sizeInBytes() const {
53        return (size_t) fWidth * fHeight * GrBytesPerPixel(fConfig);
54    }
55
56    /**
57     * Read a rectangle of pixels from the texture.
58     * @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.
64     * @param rowBytes      number of bytes bewtween consecutive rows. Zero
65     *                      means rows are tightly packed.
66     *
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,
71                    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 between 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);
88
89    /**
90     * Retrieves the render target underlying this texture that can be passed to
91     * GrGpu::setRenderTarget().
92     *
93     * @return    handle to render target or NULL if the texture is not a
94     *            render target
95     */
96    GrRenderTarget* asRenderTarget() { return fRenderTarget; }
97    const GrRenderTarget* asRenderTarget() const { return fRenderTarget; }
98
99    /**
100     * Removes the reference on the associated GrRenderTarget held by this
101     * texture. Afterwards asRenderTarget() will return NULL. The
102     * GrRenderTarget survives the release if another ref is held on it.
103     */
104    void releaseRenderTarget();
105
106    /**
107     *  Return the native ID or handle to the texture, depending on the
108     *  platform. e.g. on opengl, return the texture ID.
109     */
110    virtual intptr_t getTextureHandle() const = 0;
111
112#if GR_DEBUG
113    void validate() const {
114        this->INHERITED::validate();
115    }
116#else
117    void validate() const {}
118#endif
119
120protected:
121    GrRenderTarget* fRenderTarget; // texture refs its rt representation
122                                   // base class cons sets to NULL
123                                   // subclass cons can create and set
124
125    GrTexture(GrGpu* gpu,
126              int width,
127              int height,
128              GrPixelConfig config)
129    : INHERITED(gpu)
130    , fRenderTarget(NULL)
131    , fWidth(width)
132    , fHeight(height)
133    , fConfig(config) {
134        // only make sense if alloc size is pow2
135        fShiftFixedX = 31 - Gr_clz(fWidth);
136        fShiftFixedY = 31 - Gr_clz(fHeight);
137    }
138
139    // GrResource overrides
140    virtual void onRelease() {
141        this->releaseRenderTarget();
142    }
143
144    virtual void onAbandon();
145
146private:
147    int fWidth;
148    int fHeight;
149
150    // these two shift a fixed-point value into normalized coordinates
151    // for this texture if the texture is power of two sized.
152    int      fShiftFixedX;
153    int      fShiftFixedY;
154
155    GrPixelConfig fConfig;
156
157    typedef GrResource INHERITED;
158};
159
160#endif
161
162