/* Copyright 2010 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #ifndef GrDrawTarget_DEFINED #define GrDrawTarget_DEFINED #include "GrMatrix.h" #include "GrColor.h" #include "GrRefCnt.h" #include "GrSamplerState.h" #include "GrClip.h" class GrTexture; class GrRenderTarget; class GrClipIterator; class GrVertexBuffer; class GrIndexBuffer; class GrDrawTarget : public GrRefCnt { public: /** * Number of texture stages. Each stage takes as input a color and * 2D texture coordinates. The color input to the first enabled stage is the * per-vertex color or the constant color (setColor/setAlpha) if there are * no per-vertex colors. For subsequent stages the input color is the output * color from the previous enabled stage. The output color of each stage is * the input color modulated with the result of a texture lookup. Texture * lookups are specified by a texture (setTexture), a texture matrix * (setTextureMatrix), and a sampler (setSamplerState). Texture coordinates * for each stage come from the vertices based on a GrVertexLayout bitfield. * The output fragment color is the output color of the last enabled stage. * The presence or absence of texture coordinates for each stage in the * vertex layout indicates whether a stage is enabled or not. */ enum { kNumStages = 2, kMaxTexCoords = kNumStages }; /** * Geometric primitives used for drawing. */ enum PrimitiveType { kTriangles_PrimitiveType, kTriangleStrip_PrimitiveType, kTriangleFan_PrimitiveType, kPoints_PrimitiveType, kLines_PrimitiveType, kLineStrip_PrimitiveType }; /** * Flags that affect rendering. Controlled using enable/disableState(). All * default to disabled. */ enum StateBits { kDither_StateBit = 0x1,// 0. * @param indices will point to reserved index space if indexCount is * non-zero. Illegal to pass NULL if indexCount > 0. * * @return true if succeeded in allocating space for the vertices and false * if not. */ bool reserveAndLockGeometry(GrVertexLayout vertexLayout, uint32_t vertexCount, uint32_t indexCount, void** vertices, void** indices); /** * Provides hints to caller about the number of vertices and indices * that can be allocated cheaply. This can be useful if caller is reserving * space but doesn't know exactly how much geometry is needed. * * Also may hint whether the draw target should be flushed first. This is * useful for deferred targets. * * @param vertexLayout layout of vertices caller would like to reserve * @param vertexCount in: hint about how many vertices the caller would * like to allocate. * out: a hint about the number of vertices that can be * allocated cheaply. Negative means no hint. * Ignored if NULL. * @param indexCount in: hint about how many indices the caller would * like to allocate. * out: a hint about the number of indices that can be * allocated cheaply. Negative means no hint. * Ignored if NULL. * * @return true if target should be flushed based on the input values. */ virtual bool geometryHints(GrVertexLayout vertexLayout, int* vertexCount, int* indexCount) const; /** * Releases reserved vertex/index data from reserveAndLockGeometry(). */ void releaseReservedGeometry(); /** * Sets source of vertex data for the next draw. Array must contain * the vertex data when this is called. * * @param array cpu array containing vertex data. * @param size size of the vertex data. * @param vertexCount the number of vertices in the array. */ void setVertexSourceToArray(GrVertexLayout vertexLayout, const void* vertexArray, int vertexCount); /** * Sets source of index data for the next indexed draw. Array must contain * the indices when this is called. * * @param array cpu array containing index data. * @param indexCount the number of indices in the array. */ void setIndexSourceToArray(const void* indexArray, int indexCount); /** * Sets source of vertex data for the next draw. Data does not have to be * in the buffer until drawIndexed or drawNonIndexed. * * @param buffer vertex buffer containing vertex data. Must be * unlocked before draw call. * @param vertexLayout layout of the vertex data in the buffer. */ void setVertexSourceToBuffer(GrVertexLayout vertexLayout, const GrVertexBuffer* buffer); /** * Sets source of index data for the next indexed draw. Data does not have * to be in the buffer until drawIndexed or drawNonIndexed. * * @param buffer index buffer containing indices. Must be unlocked * before indexed draw call. */ void setIndexSourceToBuffer(const GrIndexBuffer* buffer); /** * Draws indexed geometry using the current state and current vertex / index * sources. * * @param type The type of primitives to draw. * @param startVertex the vertex in the vertex array/buffer corresponding * to index 0 * @param startIndex first index to read from index src. * @param vertexCount one greater than the max index. * @param indexCount the number of index elements to read. The index count * is effectively trimmed to the last completely * specified primitive. */ virtual void drawIndexed(PrimitiveType type, int startVertex, int startIndex, int vertexCount, int indexCount) = 0; /** * Draws non-indexed geometry using the current state and current vertex * sources. * * @param type The type of primitives to draw. * @param startVertex the vertex in the vertex array/buffer corresponding * to index 0 * @param vertexCount one greater than the max index. */ virtual void drawNonIndexed(PrimitiveType type, int startVertex, int vertexCount) = 0; /** * Helper function for drawing rects. This does not use the current index * and vertex sources. After returning, the vertex and index sources may * have changed. They should be reestablished before the next drawIndexed * or drawNonIndexed. This cannot be called between reserving and releasing * geometry. The GrDrawTarget subclass may be able to perform additional * optimizations if drawRect is used rather than drawIndexed or * drawNonIndexed. * @param rect the rect to draw * @param matrix optional matrix applied to rect (before viewMatrix) * @param stageEnableMask bitmask indicating which stages are enabled. * Bit i indicates whether stage i is enabled. * @param srcRects specifies rects for stages enabled by stageEnableMask. * if stageEnableMask bit i is 1, srcRects is not NULL, * and srcRects[i] is not NULL, then srcRects[i] will be * used as coordinates for stage i. Otherwise, if stage i * is enabled then rect is used as the coordinates. * @param srcMatrices optional matrices applied to srcRects. If * srcRect[i] is non-NULL and srcMatrices[i] is * non-NULL then srcRect[i] will be transformed by * srcMatrix[i]. srcMatrices can be NULL when no * srcMatrices are desired. */ virtual void drawRect(const GrRect& rect, const GrMatrix* matrix, int stageEnableMask, const GrRect* srcRects[], const GrMatrix* srcMatrices[]); /** * Helper for drawRect when the caller doesn't need separate src rects or * matrices. */ void drawSimpleRect(const GrRect& rect, const GrMatrix* matrix, int stageEnableMask) { drawRect(rect, matrix, stageEnableMask, NULL, NULL); } /////////////////////////////////////////////////////////////////////////// class AutoStateRestore : ::GrNoncopyable { public: AutoStateRestore(GrDrawTarget* target); ~AutoStateRestore(); private: GrDrawTarget* fDrawTarget; SavedDrawState fDrawState; }; /////////////////////////////////////////////////////////////////////////// class AutoViewMatrixRestore : ::GrNoncopyable { public: AutoViewMatrixRestore() { fDrawTarget = NULL; } AutoViewMatrixRestore(GrDrawTarget* target) : fDrawTarget(target), fMatrix(fDrawTarget->getViewMatrix()) { GrAssert(NULL != target); } void set(GrDrawTarget* target) { GrAssert(NULL != target); if (NULL != fDrawTarget) { fDrawTarget->setViewMatrix(fMatrix); } fDrawTarget = target; fMatrix = target->getViewMatrix(); } ~AutoViewMatrixRestore() { if (NULL != fDrawTarget) { fDrawTarget->setViewMatrix(fMatrix); } } private: GrDrawTarget* fDrawTarget; GrMatrix fMatrix; }; /////////////////////////////////////////////////////////////////////////// class AutoReleaseGeometry : ::GrNoncopyable { public: AutoReleaseGeometry(GrDrawTarget* target, GrVertexLayout vertexLayout, uint32_t vertexCount, uint32_t indexCount) { fTarget = target; fSuccess = fTarget->reserveAndLockGeometry(vertexLayout, vertexCount, indexCount, &fVertices, &fIndices); } AutoReleaseGeometry() { fSuccess = false; } ~AutoReleaseGeometry() { if (fSuccess) { fTarget->releaseReservedGeometry(); } } bool set(GrDrawTarget* target, GrVertexLayout vertexLayout, uint32_t vertexCount, uint32_t indexCount) { if (fSuccess) { fTarget->releaseReservedGeometry(); } fTarget = target; fSuccess = fTarget->reserveAndLockGeometry(vertexLayout, vertexCount, indexCount, &fVertices, &fIndices); return fSuccess; } bool succeeded() const { return fSuccess; } void* vertices() const { return fVertices; } void* indices() const { return fIndices; } GrPoint* positions() const { return static_cast(fVertices); } private: GrDrawTarget* fTarget; bool fSuccess; void* fVertices; void* fIndices; }; /////////////////////////////////////////////////////////////////////////// class AutoClipRestore : ::GrNoncopyable { public: AutoClipRestore(GrDrawTarget* target) { fTarget = target; fClip = fTarget->getClip(); } ~AutoClipRestore() { fTarget->setClip(fClip); } private: GrDrawTarget* fTarget; GrClip fClip; }; //////////////////////////////////////////////////////////////////////////// // Helpers for picking apart vertex layouts /** * Helper function to compute the size of a vertex from a vertex layout * @return size of a single vertex. */ static size_t VertexSize(GrVertexLayout vertexLayout); /** * Helper function for determining the index of texture coordinates that * is input for a texture stage. Note that a stage may instead use positions * as texture coordinates, in which case the result of the function is * indistinguishable from the case when the stage is disabled. * * @param stage the stage to query * @param vertexLayout layout to query * * @return the texture coordinate index or -1 if the stage doesn't use * separate (non-position) texture coordinates. */ static int VertexTexCoordsForStage(int stage, GrVertexLayout vertexLayout); /** * Helper function to compute the offset of texture coordinates in a vertex * @return offset of texture coordinates in vertex layout or -1 if the * layout has no texture coordinates. Will be 0 if positions are * used as texture coordinates for the stage. */ static int VertexStageCoordOffset(int stage, GrVertexLayout vertexLayout); /** * Helper function to compute the offset of the color in a vertex * @return offset of color in vertex layout or -1 if the * layout has no color. */ static int VertexColorOffset(GrVertexLayout vertexLayout); /** * Helper function to determine if vertex layout contains explicit texture * coordinates of some index. * * @param coordIndex the tex coord index to query * @param vertexLayout layout to query * * @return true if vertex specifies texture coordinates for the index, * false otherwise. */ static bool VertexUsesTexCoordIdx(int coordIndex, GrVertexLayout vertexLayout); /** * Helper function to determine if vertex layout contains either explicit or * implicit texture coordinates for a stage. * * @param stage the stage to query * @param vertexLayout layout to query * * @return true if vertex specifies texture coordinates for the stage, * false otherwise. */ static bool VertexUsesStage(int stage, GrVertexLayout vertexLayout); /** * Helper function to compute the size of each vertex and the offsets of * texture coordinates and color. Determines tex coord offsets by tex coord * index rather than by stage. (Each stage can be mapped to any t.c. index * by StageTexCoordVertexLayoutBit.) * * @param vertexLayout the layout to query * @param texCoordOffsetsByIdx after return it is the offset of each * tex coord index in the vertex or -1 if * index isn't used. * @return size of a single vertex */ static int VertexSizeAndOffsetsByIdx(GrVertexLayout vertexLayout, int texCoordOffsetsByIdx[kMaxTexCoords], int *colorOffset); /** * Helper function to compute the size of each vertex and the offsets of * texture coordinates and color. Determines tex coord offsets by stage * rather than by index. (Each stage can be mapped to any t.c. index * by StageTexCoordVertexLayoutBit.) If a stage uses positions for * tex coords then that stage's offset will be 0 (positions are always at 0). * * @param vertexLayout the layout to query * @param texCoordOffsetsByStage after return it is the offset of each * tex coord index in the vertex or -1 if * index isn't used. * @return size of a single vertex */ static int VertexSizeAndOffsetsByStage(GrVertexLayout vertexLayout, int texCoordOffsetsByStage[kNumStages], int *colorOffset); /** * Accessing positions, texture coords, or colors, of a vertex within an * array is a hassle involving casts and simple math. These helpers exist * to keep GrDrawTarget clients' code a bit nicer looking. */ /** * Gets a pointer to a GrPoint of a vertex's position or texture * coordinate. * @param vertices the vetex array * @param vertexIndex the index of the vertex in the array * @param vertexSize the size of each vertex in the array * @param offset the offset in bytes of the vertex component. * Defaults to zero (corresponding to vertex position) * @return pointer to the vertex component as a GrPoint */ static GrPoint* GetVertexPoint(void* vertices, int vertexIndex, int vertexSize, int offset = 0) { intptr_t start = GrTCast(vertices); return GrTCast(start + offset + vertexIndex * vertexSize); } static const GrPoint* GetVertexPoint(const void* vertices, int vertexIndex, int vertexSize, int offset = 0) { intptr_t start = GrTCast(vertices); return GrTCast(start + offset + vertexIndex * vertexSize); } /** * Gets a pointer to a GrColor inside a vertex within a vertex array. * @param vertices the vetex array * @param vertexIndex the index of the vertex in the array * @param vertexSize the size of each vertex in the array * @param offset the offset in bytes of the vertex color * @return pointer to the vertex component as a GrColor */ static GrColor* GetVertexColor(void* vertices, int vertexIndex, int vertexSize, int offset) { intptr_t start = GrTCast(vertices); return GrTCast(start + offset + vertexIndex * vertexSize); } static const GrColor* GetVertexColor(const void* vertices, int vertexIndex, int vertexSize, int offset) { const intptr_t start = GrTCast(vertices); return GrTCast(start + offset + vertexIndex * vertexSize); } protected: // Helpers for GrDrawTarget subclasses that won't have private access to // SavedDrawState but need to peek at the state values. static DrState& accessSavedDrawState(SavedDrawState& sds) { return sds.fState; } static const DrState& accessSavedDrawState(const SavedDrawState& sds) { return sds.fState; } // implemented by subclass virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, void** vertices, void** indices) = 0; virtual void releaseGeometryHelper() = 0; // subclass overrides to be notified when clip is set. virtual void clipWillBeSet(const GrClip& clip) = 0; virtual void setVertexSourceToArrayHelper(const void* vertexArray, int vertexCount) = 0; virtual void setIndexSourceToArrayHelper(const void* indexArray, int indexCount) = 0; // Helpers for drawRect, protected so subclasses that override drawRect // can use them. static GrVertexLayout GetRectVertexLayout(int stageEnableMask, const GrRect* srcRects[]); static void SetRectVertices(const GrRect& rect, const GrMatrix* matrix, const GrRect* srcRects[], const GrMatrix* srcMatrices[], GrVertexLayout layout, void* vertices); enum GeometrySrcType { kReserved_GeometrySrcType, // src was set using reserveAndLockGeometry kArray_GeometrySrcType, // src was set using set*SourceToArray kBuffer_GeometrySrcType // src was set using set*SourceToBuffer }; struct { bool fLocked; uint32_t fVertexCount; uint32_t fIndexCount; } fReservedGeometry; struct GeometrySrc { GeometrySrcType fVertexSrc; const GrVertexBuffer* fVertexBuffer; // valid if src type is buffer GeometrySrcType fIndexSrc; const GrIndexBuffer* fIndexBuffer; // valid if src type is buffer GrVertexLayout fVertexLayout; } fGeometrySrc; GrClip fClip; DrState fCurrDrawState; // Not meant for external use. Only setVertexSourceToBuffer and // setIndexSourceToBuffer will work since GrDrawTarget subclasses don't // support nested reserveAndLockGeometry (and cpu arrays internally use the // same path). class AutoGeometrySrcRestore { public: AutoGeometrySrcRestore(GrDrawTarget* target) { fTarget = target; fGeometrySrc = fTarget->fGeometrySrc; } ~AutoGeometrySrcRestore() { fTarget->fGeometrySrc = fGeometrySrc; } private: GrDrawTarget *fTarget; GeometrySrc fGeometrySrc; AutoGeometrySrcRestore(); AutoGeometrySrcRestore(const AutoGeometrySrcRestore&); AutoGeometrySrcRestore& operator =(AutoGeometrySrcRestore&); }; private: void VertexLayoutUnitTest(); }; #endif