18fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com */
827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
1027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#ifndef GrGeometryBuffer_DEFINED
1127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#define GrGeometryBuffer_DEFINED
1227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
136d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon#include "GrGpuResource.h"
148fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
158fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.comclass GrGpu;
1627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
1727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com/**
1827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com * Parent class for vertex and index buffers
1927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com */
206d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomonclass GrGeometryBuffer : public GrGpuResource {
2127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.compublic:
227fa18762e0ab64c3473df3aab0c2bfd6fabd8831robertphillips@google.com    SK_DECLARE_INST_COUNT(GrGeometryBuffer);
238fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
2427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
2527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *Retrieves whether the buffer was created with the dynamic flag
2627847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *
2727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @return true if the buffer was created with the dynamic flag
2827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
2927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    bool dynamic() const { return fDynamic; }
308fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
3127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
32ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com     * Returns true if the buffer is a wrapper around a CPU array. If true it
338341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * indicates that map will always succeed and will be free.
34ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com     */
35ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com    bool isCPUBacked() const { return fCPUBacked; }
36ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com
37ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com    /**
388341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * Maps the buffer to be written by the CPU.
398fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
4027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * The previous content of the buffer is invalidated. It is an error
418341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * to draw from the buffer while it is mapped. It is an error to call map
428341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * on an already mapped buffer. It may fail if the backend doesn't support
438341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * mapping the buffer. If the buffer is CPU backed then it will always
448341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * succeed and is a free operation. Must be matched by an unmap() call.
458341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * Currently only one map at a time is supported (no nesting of
468341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * map/unmap).
478fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
48e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * Note that buffer mapping does not go through GrContext and therefore is
49e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * not serialized with other operations.
50e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     *
518341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     * @return a pointer to the data or NULL if the map fails.
5227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
53e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     void* map() { return (fMapPtr = this->onMap()); }
548fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
5527847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
56e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * Unmaps the buffer.
5727847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     *
58e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * The pointer returned by the previous map call will no longer be valid.
5927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
60e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     void unmap() {
6149f085dddff10473b6ebf832a974288300224e60bsalomon         SkASSERT(fMapPtr);
62e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org         this->onUnmap();
63e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org         fMapPtr = NULL;
64e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     }
658fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
668fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
67e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * Returns the same ptr that map() returned at time of map or NULL if the
68e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * is not mapped.
698fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
70e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * @return ptr to mapped buffer data or NULL if buffer is not mapped.
7127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
72e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     void* mapPtr() const { return fMapPtr; }
738fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
748fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com    /**
758341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     Queries whether the buffer has been mapped.
768fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
778341eb76fbc54593e873f5589961e02793e7f15fcommit-bot@chromium.org     @return true if the buffer is mapped, false otherwise.
7827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
7949f085dddff10473b6ebf832a974288300224e60bsalomon     bool isMapped() const { return SkToBool(fMapPtr); }
808fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
8127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    /**
828fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * Updates the buffer data.
838fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
848fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     * The size of the buffer will be preserved. The src data will be
85ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com     * placed at the beginning of the buffer and any remaining contents will
86e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * be undefined. srcSizeInBytes must be <= to the buffer size.
87e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     *
88e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * The buffer must not be mapped.
89e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     *
90e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * Note that buffer updates do not go through GrContext and therefore are
91e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org     * not serialized with other operations.
928fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com     *
9327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     * @return returns true if the update succeeds, false otherwise.
9427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com     */
95e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    bool updateData(const void* src, size_t srcSizeInBytes) {
96e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org        SkASSERT(!this->isMapped());
97e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org        SkASSERT(srcSizeInBytes <= fGpuMemorySize);
98e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org        return this->onUpdateData(src, srcSizeInBytes);
99e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    }
1008fe72477f204b1a45393e6a64caa84fd287b805bbsalomon@google.com
10127847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comprotected:
1025236cf480daf82b2f36e42795abdbbc915533a59bsalomon    GrGeometryBuffer(GrGpu* gpu, size_t gpuMemorySize, bool dynamic, bool cpuBacked)
1035236cf480daf82b2f36e42795abdbbc915533a59bsalomon        : INHERITED(gpu, kCached_LifeCycle)
104e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org        , fMapPtr(NULL)
105089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org        , fGpuMemorySize(gpuMemorySize)
106ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com        , fDynamic(dynamic)
107ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com        , fCPUBacked(cpuBacked) {}
10827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
10927847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.comprivate:
11069ed47f42d4877c178fdc0031cb01af2966ae235bsalomon    virtual size_t onGpuMemorySize() const { return fGpuMemorySize; }
11169ed47f42d4877c178fdc0031cb01af2966ae235bsalomon
112e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    virtual void* onMap() = 0;
113e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    virtual void onUnmap() = 0;
114e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
115e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org
116e529a61dd416e454bd858ef948aa49f5b4e02b80commit-bot@chromium.org    void*    fMapPtr;
117089a780c3355129eefc942246534bc1f126b8ccbcommit-bot@chromium.org    size_t   fGpuMemorySize;
11827847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com    bool     fDynamic;
119ee3bc3b26771a58a78075f11cde8801e0e79f723bsalomon@google.com    bool     fCPUBacked;
12027847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
1216d3fe022d68fd6dd32c0fab30e24fa5a4f048946bsalomon    typedef GrGpuResource INHERITED;
12227847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com};
12327847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com
12427847dedd9b5c1f48998c40842f3494c0746257fbsalomon@google.com#endif
125