GrSurface.h revision e30597375c19dfb5197fd065a3d1768401eb00fa
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"
136d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#include "GrGpuResource.h"
14afbf2d6273cd22c683f20a7e5773843876af3085bsalomon#include "SkImageInfo.h"
1524ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org#include "SkRect.h"
167d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
17b06e5a2f558133b865cb0c3cc0b36e9bd2e594e2robertphillipsclass GrRenderTarget;
18afbf2d6273cd22c683f20a7e5773843876af3085bsalomonclass GrSurfacePriv;
19afbf2d6273cd22c683f20a7e5773843876af3085bsalomonclass GrTexture;
207d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
216d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomonclass GrSurface : public GrGpuResource {
227d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.compublic:
237d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    SK_DECLARE_INST_COUNT(GrSurface);
247d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
257d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
267d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the width of the surface.
277d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
287d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the width in texels
297d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
307d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    int width() const { return fDesc.fWidth; }
317d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
327d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
337d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the height of the surface.
347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
357d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the height in texels
367d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
377d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    int height() const { return fDesc.fHeight; }
387d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
3924ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    /**
4024ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     * Helper that gets the width and height of the surface as a bounding rectangle.
4124ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     */
4224ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
4324ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org                                                         SkIntToScalar(this->height())); }
4424ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org
45ef5dbe1cd90fe586f165e54cb6f7608942610793senorblanco@chromium.org    GrSurfaceOrigin origin() const {
46f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
473cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org        return fDesc.fOrigin;
482d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    }
492d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
502d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    /**
517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the pixel config specified when the surface was created.
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * For render targets this can be kUnknown_GrPixelConfig
537d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * if client asked us to render to a target that has a pixel
547d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * config that isn't equivalent with one of our configs.
557d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
567d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    GrPixelConfig config() const { return fDesc.fConfig; }
577d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
587d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
597d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Return the descriptor describing the surface
607d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
617d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    const GrTextureDesc& desc() const { return fDesc; }
627d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
637d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
647d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the texture associated with the surface, may be NULL.
657d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
667d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrTexture* asTexture() = 0;
677d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrTexture* asTexture() const = 0;
687d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
697d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
707d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the render target underlying this surface, may be NULL.
717d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
727d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual GrRenderTarget* asRenderTarget() = 0;
737d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    virtual const GrRenderTarget* asRenderTarget() const = 0;
747d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
757d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
767d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Reads a rectangle of pixels from the surface.
777d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to read (inclusive)
787d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to read (inclusive)
797d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to read in pixels.
807d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to read in pixels.
817d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the destination buffer
827d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle into.
832d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
840342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
850342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
867d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
870342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
880342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *              pixel config.
897d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
9081beccc4fb1396fe94af15bfce26e68b82b93809bsalomon    bool readPixels(int left, int top, int width, int height,
9181beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    GrPixelConfig config,
9281beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    void* buffer,
9381beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    size_t rowBytes = 0,
9481beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    uint32_t pixelOpsFlags = 0);
957d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
967d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
970342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
980342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * rectangle.
997d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to write (inclusive)
1007d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to write (inclusive)
1017d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to write in pixels.
1027d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to write in pixels.
1037d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the source buffer
1047d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle from.
1052d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
1060342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
1070342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
108e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     *
109e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
110e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     *              pixel config.
1117d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
11281beccc4fb1396fe94af15bfce26e68b82b93809bsalomon    bool writePixels(int left, int top, int width, int height,
11381beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     GrPixelConfig config,
11481beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     const void* buffer,
11581beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     size_t rowBytes = 0,
11681beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     uint32_t pixelOpsFlags = 0);
1177d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
118f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon    /**
119f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon     * After this returns any pending writes to the surface will be issued to the backend 3D API.
120f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon     */
121f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon    void flushWrites();
122f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon
123afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    /** Access methods that are only to be used within Skia code. */
124afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    inline GrSurfacePriv surfacePriv();
125afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    inline const GrSurfacePriv surfacePriv() const;
126b06e5a2f558133b865cb0c3cc0b36e9bd2e594e2robertphillips
127afbf2d6273cd22c683f20a7e5773843876af3085bsalomonprotected:
128afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    // Methods made available via GrSurfacePriv
129afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    SkImageInfo info() const;
130afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    bool savePixels(const char* filename);
1318d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingRead() const;
1328d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingWrite() const;
1338d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingIO() const;
134afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    bool isSameAs(const GrSurface* other) const;
135afbf2d6273cd22c683f20a7e5773843876af3085bsalomon
136afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    // Provides access to methods that should be public within Skia code.
137afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    friend class GrSurfacePriv;
1388d034a154fec81167ecb696c07da389b98cc02a7bsalomon
1393cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    GrSurface(GrGpu* gpu, bool isWrapped, const GrTextureDesc& desc)
140728302281920727b96e6cec0bfc7575900f34a8bbsalomon@google.com    : INHERITED(gpu, isWrapped)
1413cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org    , fDesc(desc) {
1427d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    }
1437d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
1442d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    GrTextureDesc fDesc;
1452d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
1467d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comprivate:
1476d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    typedef GrGpuResource INHERITED;
1487d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com};
1497d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
150afbf2d6273cd22c683f20a7e5773843876af3085bsalomon#endif
151