1/*
2 * Copyright (C) 2009-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "WebGLSharedObject.h"
29#include <wtf/Forward.h>
30
31namespace JSC {
32class ArrayBuffer;
33class ArrayBufferView;
34}
35
36namespace WebCore {
37
38class WebGLBuffer final : public WebGLSharedObject {
39public:
40 static Ref<WebGLBuffer> create(WebGLRenderingContextBase&);
41 virtual ~WebGLBuffer();
42
43 bool associateBufferData(GC3Dsizeiptr size);
44 bool associateBufferData(JSC::ArrayBuffer*);
45 bool associateBufferData(JSC::ArrayBufferView*);
46 bool associateBufferSubData(GC3Dintptr offset, JSC::ArrayBuffer*);
47 bool associateBufferSubData(GC3Dintptr offset, JSC::ArrayBufferView*);
48 bool associateCopyBufferSubData(const WebGLBuffer& readBuffer, GC3Dintptr readOffset, GC3Dintptr writeOffset, GC3Dsizeiptr);
49
50 void disassociateBufferData();
51
52 GC3Dsizeiptr byteLength() const;
53 const RefPtr<JSC::ArrayBuffer> elementArrayBuffer() const { return m_elementArrayBuffer; }
54
55 // Gets the cached max index for the given type if one has been set.
56 Optional<unsigned> getCachedMaxIndex(GC3Denum type);
57 // Sets the cached max index for the given type.
58 void setCachedMaxIndex(GC3Denum type, unsigned value);
59
60 GC3Denum getTarget() const { return m_target; }
61 void setTarget(GC3Denum, bool forWebGL2);
62
63 bool hasEverBeenBound() const { return object() && m_target; }
64
65protected:
66 WebGLBuffer(WebGLRenderingContextBase&);
67
68 void deleteObjectImpl(GraphicsContext3D*, Platform3DObject) override;
69
70private:
71 GC3Denum m_target { 0 };
72
73 RefPtr<JSC::ArrayBuffer> m_elementArrayBuffer;
74 GC3Dsizeiptr m_byteLength { 0 };
75
76 // Optimization for index validation. For each type of index
77 // (i.e., UNSIGNED_SHORT), cache the maximum index in the
78 // entire buffer.
79 //
80 // This is sufficient to eliminate a lot of work upon each
81 // draw call as long as all bound array buffers are at least
82 // that size.
83 struct MaxIndexCacheEntry {
84 GC3Denum type;
85 unsigned maxIndex;
86 };
87 // OpenGL ES 2.0 only has two valid index types (UNSIGNED_BYTE
88 // and UNSIGNED_SHORT) plus one extension (UNSIGNED_INT).
89 MaxIndexCacheEntry m_maxIndexCache[4];
90 unsigned m_nextAvailableCacheEntry { 0 };
91
92 // Clears all of the cached max indices.
93 void clearCachedMaxIndices();
94
95 // Helper function called by the three associateBufferData().
96 bool associateBufferDataImpl(const void* data, GC3Dsizeiptr byteLength);
97 // Helper function called by the two associateBufferSubData().
98 bool associateBufferSubDataImpl(GC3Dintptr offset, const void* data, GC3Dsizeiptr byteLength);
99};
100
101} // namespace WebCore
102