GrSurface.h revision f6de475e5cbd143f348ff7738919e397b7fe7f57
17d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com/*
27d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com * Copyright 2012 Google Inc.
37d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com *
47d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
57d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com * found in the LICENSE file.
67d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com */
77d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
87d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
97d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#ifndef GrSurface_DEFINED
107d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#define GrSurface_DEFINED
117d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
127d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#include "GrTypes.h"
137d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#include "GrResource.h"
1424ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org#include "SkRect.h"
157d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
167d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comclass GrTexture;
177d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comclass GrRenderTarget;
187d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
197d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comclass GrSurface : public GrResource {
207d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.compublic:
217d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    SK_DECLARE_INST_COUNT(GrSurface);
227d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
237d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
247d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the width of the surface.
257d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
267d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the width in texels
277d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
287d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    int width() const { return fDesc.fWidth; }
297d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
307d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
317d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the height of the surface.
327d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
337d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the height in texels
347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
357d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    int height() const { return fDesc.fHeight; }
367d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
3724ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    /**
3824ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     * Helper that gets the width and height of the surface as a bounding rectangle.
3924ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     */
4024ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
4124ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org                                                         SkIntToScalar(this->height())); }
4224ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org
43ef5dbe1cd90fe586f165e54cb6f7608942610793senorblanco@chromium.org    GrSurfaceOrigin origin() const {
44f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
453cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org        return fDesc.fOrigin;
462d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    }
472d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
482d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    /**
497d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the pixel config specified when the surface was created.
507d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * For render targets this can be kUnknown_GrPixelConfig
517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * if client asked us to render to a target that has a pixel
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * config that isn't equivalent with one of our configs.
537d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
547d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    GrPixelConfig config() const { return fDesc.fConfig; }
557d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
567d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
577d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Return the descriptor describing the surface
587d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
597d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    const GrTextureDesc& desc() const { return fDesc; }
607d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
617d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
627d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the texture associated with the surface, may be NULL.
637d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
647d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrTexture* asTexture() = 0;
657d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrTexture* asTexture() const = 0;
667d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
677d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
687d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the render target underlying this surface, may be NULL.
697d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
707d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrRenderTarget* asRenderTarget() = 0;
717d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrRenderTarget* asRenderTarget() const = 0;
727d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
737d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
74686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com     * Checks whether this GrSurface refers to the same GPU object as other. This
75686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com     * catches the case where a GrTexture and GrRenderTarget refer to the same
76686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com     * GPU memory.
77686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com     */
78686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    bool isSameAs(const GrSurface* other) const {
79686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        const GrRenderTarget* thisRT = this->asRenderTarget();
80686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        if (NULL != thisRT) {
81686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com            return thisRT == other->asRenderTarget();
82686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        } else {
83686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com            const GrTexture* thisTex = this->asTexture();
84f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org            SkASSERT(NULL != thisTex); // We must be one or the other
85686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com            return thisTex == other->asTexture();
86686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com        }
87686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    }
88686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com
89686bcb871b8425603b9accbf72e27a9309f786d8bsalomon@google.com    /**
907d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Reads a rectangle of pixels from the surface.
917d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to read (inclusive)
927d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to read (inclusive)
937d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to read in pixels.
947d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to read in pixels.
957d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the destination buffer
967d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle into.
972d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
980342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
990342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
1007d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
1010342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
1020342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *              pixel config.
1037d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
1047d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual bool readPixels(int left, int top, int width, int height,
1050342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            GrPixelConfig config,
1060342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            void* buffer,
1070342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            size_t rowBytes = 0,
1080342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                            uint32_t pixelOpsFlags = 0) = 0;
1097d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
1107d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
1110342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
1120342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * rectangle.
1137d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to write (inclusive)
1147d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to write (inclusive)
1157d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to write in pixels.
1167d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to write in pixels.
1177d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the source buffer
1187d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle from.
1192d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
1200342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
1210342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
1227d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
1237d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual void writePixels(int left, int top, int width, int height,
1240342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             GrPixelConfig config,
1250342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             const void* buffer,
1260342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             size_t rowBytes = 0,
1270342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com                             uint32_t pixelOpsFlags = 0) = 0;
1287d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
1297d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comprotected:
1303cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
131728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    : INHERITED(gpu, isWrapped)
1323cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    , fDesc(desc) {
1337d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    }
1347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
1352d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    GrTextureDesc fDesc;
1362d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
1377d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comprivate:
1387d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    typedef GrResource INHERITED;
1397d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com};
1407d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
1417d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com#endif // GrSurface_DEFINED
142