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    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    int height() const { return fDesc.fHeight; }
347d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
3524ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    /**
3624ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     * Helper that gets the width and height of the surface as a bounding rectangle.
3724ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org     */
3824ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org    void getBoundsRect(SkRect* rect) const { rect->setWH(SkIntToScalar(this->width()),
3924ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org                                                         SkIntToScalar(this->height())); }
4024ab3b0ce50b3428f063849b6160e468f047487ccommit-bot@chromium.org
41ef5dbe1cd90fe586f165e54cb6f7608942610793senorblanco@chromium.org    GrSurfaceOrigin origin() const {
42f6de475e5cbd143f348ff7738919e397b7fe7f57tfarina@chromium.org        SkASSERT(kTopLeft_GrSurfaceOrigin == fDesc.fOrigin || kBottomLeft_GrSurfaceOrigin == fDesc.fOrigin);
433cb406bb88f5aa09cf9f5a9554b4b1314cf1a2eesenorblanco@chromium.org        return fDesc.fOrigin;
442d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    }
452d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
462d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com    /**
477d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Retrieves the pixel config specified when the surface was created.
487d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * For render targets this can be kUnknown_GrPixelConfig
497d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * if client asked us to render to a target that has a pixel
507d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * config that isn't equivalent with one of our configs.
517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
527d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    GrPixelConfig config() const { return fDesc.fConfig; }
537d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
547d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
557d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Return the descriptor describing the surface
567d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
57f2703d83da3ab2ae18b45231fd4f11e16cce3184bsalomon    const GrSurfaceDesc& desc() const { return fDesc; }
587d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
597d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
607d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the texture associated with the surface, may be NULL.
617d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
6237dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon    virtual GrTexture* asTexture() { return NULL; }
6337dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon    virtual const GrTexture* asTexture() const { return NULL; }
647d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
657d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
667d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @return the render target underlying this surface, may be NULL.
677d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
6837dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon    virtual GrRenderTarget* asRenderTarget() { return NULL; }
6937dd331b20a92ce79cc26556e065dec98a66cb0bbsalomon    virtual const GrRenderTarget* asRenderTarget() const { return NULL; }
707d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
717d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
727d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * Reads a rectangle of pixels from the surface.
737d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to read (inclusive)
747d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to read (inclusive)
757d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to read in pixels.
767d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to read in pixels.
777d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the destination buffer
787d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle into.
792d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
800342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
810342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
827d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     *
830342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
840342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *              pixel config.
857d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
8681beccc4fb1396fe94af15bfce26e68b82b93809bsalomon    bool readPixels(int left, int top, int width, int height,
8781beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    GrPixelConfig config,
8881beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    void* buffer,
8981beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    size_t rowBytes = 0,
9081beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                    uint32_t pixelOpsFlags = 0);
917d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
927d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    /**
930342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * Copy the src pixels [buffer, rowbytes, pixelconfig] into the surface at the specified
940342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * rectangle.
957d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param left          left edge of the rectangle to write (inclusive)
967d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param top           top edge of the rectangle to write (inclusive)
977d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param width         width of rectangle to write in pixels.
987d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param height        height of rectangle to write in pixels.
997d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param config        the pixel config of the source buffer
1007d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     * @param buffer        memory to read the rectangle from.
1012d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com     * @param rowBytes      number of bytes between consecutive rows. Zero means rows are tightly
1020342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     *                      packed.
1030342a85091fd430c90a142d155dc9642aa729d9ebsalomon@google.com     * @param pixelOpsFlags See the GrContext::PixelOpsFlags enum.
104e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     *
105e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     * @return true if the read succeeded, false if not. The read can fail because of an unsupported
106e30597375c19dfb5197fd065a3d1768401eb00fabsalomon     *              pixel config.
1077d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com     */
10881beccc4fb1396fe94af15bfce26e68b82b93809bsalomon    bool writePixels(int left, int top, int width, int height,
10981beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     GrPixelConfig config,
11081beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     const void* buffer,
11181beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     size_t rowBytes = 0,
11281beccc4fb1396fe94af15bfce26e68b82b93809bsalomon                     uint32_t pixelOpsFlags = 0);
1137d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
114f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon    /**
115f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon     * After this returns any pending writes to the surface will be issued to the backend 3D API.
116f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon     */
117f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon    void flushWrites();
118f80bfedc42fde88ae3f8dbd7b21950a22258fd22bsalomon
11987a94eb1632d06eeeb89490a91565e786440d6d0bsalomon
12087a94eb1632d06eeeb89490a91565e786440d6d0bsalomon    /**
12187a94eb1632d06eeeb89490a91565e786440d6d0bsalomon     * After this returns any pending writes to the surface will be issued to the backend 3D API and
12287a94eb1632d06eeeb89490a91565e786440d6d0bsalomon     * if the surface has MSAA it will be resolved.
12387a94eb1632d06eeeb89490a91565e786440d6d0bsalomon     */
12487a94eb1632d06eeeb89490a91565e786440d6d0bsalomon    void prepareForExternalRead();
12587a94eb1632d06eeeb89490a91565e786440d6d0bsalomon
126afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    /** Access methods that are only to be used within Skia code. */
127afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    inline GrSurfacePriv surfacePriv();
128afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    inline const GrSurfacePriv surfacePriv() const;
129b06e5a2f558133b865cb0c3cc0b36e9bd2e594e2robertphillips
130afbf2d6273cd22c683f20a7e5773843876af3085bsalomonprotected:
131afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    // Methods made available via GrSurfacePriv
132afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    SkImageInfo info() const;
133afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    bool savePixels(const char* filename);
1348d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingRead() const;
1358d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingWrite() const;
1368d034a154fec81167ecb696c07da389b98cc02a7bsalomon    bool hasPendingIO() const;
137afbf2d6273cd22c683f20a7e5773843876af3085bsalomon
138afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    // Provides access to methods that should be public within Skia code.
139afbf2d6273cd22c683f20a7e5773843876af3085bsalomon    friend class GrSurfacePriv;
1408d034a154fec81167ecb696c07da389b98cc02a7bsalomon
1415236cf480daf82b2f36e42795abdbbc915533a59bsalomon    GrSurface(GrGpu* gpu, LifeCycle lifeCycle, const GrSurfaceDesc& desc)
1425236cf480daf82b2f36e42795abdbbc915533a59bsalomon        : INHERITED(gpu, lifeCycle)
1435236cf480daf82b2f36e42795abdbbc915533a59bsalomon        , fDesc(desc) {
1447d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com    }
1457d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
146f2703d83da3ab2ae18b45231fd4f11e16cce3184bsalomon    GrSurfaceDesc fDesc;
1472d0baded0f45dfde9dc8c25313ff14ea18c0c915bsalomon@google.com
1487d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.comprivate:
1496d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    typedef GrGpuResource INHERITED;
1507d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com};
1517d501ab502e861f2b2367b10a701b8449241558erobertphillips@google.com
152afbf2d6273cd22c683f20a7e5773843876af3085bsalomon#endif
153