Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "modules/webgl/WebGLGetBufferSubDataAsync.h" | |
| 6 | |
| 7 #include "core/dom/DOMException.h" | |
| 8 #include "gpu/GLES2/gl2extchromium.h" | |
| 9 #include "gpu/command_buffer/client/gles2_interface.h" | |
| 10 #include "modules/webgl/WebGL2RenderingContextBase.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 WebGLGetBufferSubDataAsync::WebGLGetBufferSubDataAsync( | |
| 15 WebGLRenderingContextBase* context) | |
| 16 : WebGLExtension(context) {} | |
| 17 | |
| 18 WebGLExtensionName WebGLGetBufferSubDataAsync::name() const { | |
| 19 return WebGLGetBufferSubDataAsyncName; | |
| 20 } | |
| 21 | |
| 22 WebGLGetBufferSubDataAsync* WebGLGetBufferSubDataAsync::create( | |
| 23 WebGLRenderingContextBase* context) { | |
| 24 return new WebGLGetBufferSubDataAsync(context); | |
| 25 } | |
| 26 | |
| 27 bool WebGLGetBufferSubDataAsync::supported(WebGLRenderingContextBase* context) { | |
| 28 Extensions3DUtil* extensionsUtil = context->extensionsUtil(); | |
| 29 return extensionsUtil->supportsExtension( | |
| 30 "GL_EXT_texture_compression_s3tc_srgb"); | |
|
Ken Russell (switch to Gerrit)
2016/12/13 23:17:29
This looks wrong.
Kai Ninomiya
2016/12/13 23:20:30
uh... oops. error in context switch state restorat
| |
| 31 } | |
| 32 | |
| 33 const char* WebGLGetBufferSubDataAsync::extensionName() { | |
| 34 return "WEBGL_get_buffer_sub_data_async"; | |
| 35 } | |
| 36 | |
| 37 ScriptPromise WebGLGetBufferSubDataAsync::getBufferSubDataAsync( | |
| 38 ScriptState* scriptState, | |
| 39 GLenum target, | |
| 40 GLintptr srcByteOffset, | |
| 41 DOMArrayBufferView* dstData, | |
| 42 GLuint dstOffset, | |
| 43 GLuint length) { | |
| 44 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
| 45 ScriptPromise promise = resolver->promise(); | |
| 46 | |
| 47 WebGLExtensionScopedContext scoped(this); | |
| 48 if (scoped.isLost()) { | |
| 49 DOMException* exception = | |
| 50 DOMException::create(InvalidStateError, "context lost"); | |
| 51 resolver->reject(exception); | |
| 52 return promise; | |
| 53 } | |
| 54 | |
| 55 WebGL2RenderingContextBase* context = nullptr; | |
| 56 { | |
| 57 WebGLRenderingContextBase* contextBase = scoped.context(); | |
| 58 DCHECK_GE(contextBase->version(), 2); | |
| 59 context = static_cast<WebGL2RenderingContextBase*>(contextBase); | |
| 60 } | |
| 61 | |
| 62 WebGLBuffer* sourceBuffer = nullptr; | |
| 63 void* destinationDataPtr = nullptr; | |
| 64 long long destinationByteLength = 0; | |
| 65 const char* message = context->validateGetBufferSubData( | |
| 66 __FUNCTION__, target, srcByteOffset, dstData, dstOffset, length, | |
| 67 &sourceBuffer, &destinationDataPtr, &destinationByteLength); | |
| 68 if (message) { | |
| 69 // If there was a GL error, it was already synthesized in | |
| 70 // validateGetBufferSubData, so it's not done here. | |
| 71 DOMException* exception = DOMException::create(InvalidStateError, message); | |
| 72 resolver->reject(exception); | |
| 73 return promise; | |
| 74 } | |
| 75 | |
| 76 message = context->validateGetBufferSubDataBounds( | |
| 77 __FUNCTION__, sourceBuffer, srcByteOffset, destinationByteLength); | |
| 78 if (message) { | |
| 79 // If there was a GL error, it was already synthesized in | |
| 80 // validateGetBufferSubDataBounds, so it's not done here. | |
| 81 DOMException* exception = DOMException::create(InvalidStateError, message); | |
| 82 resolver->reject(exception); | |
| 83 return promise; | |
| 84 } | |
| 85 | |
| 86 // If the length of the copy is zero, this is a no-op. | |
| 87 if (!destinationByteLength) { | |
| 88 resolver->resolve(dstData); | |
| 89 return promise; | |
| 90 } | |
| 91 | |
| 92 GLuint queryID; | |
| 93 context->contextGL()->GenQueriesEXT(1, &queryID); | |
| 94 context->contextGL()->BeginQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM, queryID); | |
| 95 void* mappedData = context->contextGL()->GetBufferSubDataAsyncCHROMIUM( | |
| 96 target, srcByteOffset, destinationByteLength); | |
| 97 context->contextGL()->EndQueryEXT(GL_COMMANDS_ISSUED_CHROMIUM); | |
| 98 if (!mappedData) { | |
| 99 DOMException* exception = | |
| 100 DOMException::create(InvalidStateError, "Out of memory"); | |
| 101 resolver->reject(exception); | |
| 102 return promise; | |
| 103 } | |
| 104 | |
| 105 auto callbackObject = new WebGLGetBufferSubDataAsyncCallback( | |
| 106 context, resolver, mappedData, queryID, dstData, destinationDataPtr, | |
| 107 destinationByteLength); | |
| 108 context->registerGetBufferSubDataAsyncCallback(callbackObject); | |
| 109 auto callback = WTF::bind(&WebGLGetBufferSubDataAsyncCallback::resolve, | |
| 110 wrapPersistent(callbackObject)); | |
| 111 context->drawingBuffer()->contextProvider()->signalQuery( | |
| 112 queryID, convertToBaseCallback(std::move(callback))); | |
| 113 | |
| 114 return promise; | |
| 115 } | |
| 116 | |
| 117 WebGLGetBufferSubDataAsyncCallback::WebGLGetBufferSubDataAsyncCallback( | |
| 118 WebGL2RenderingContextBase* context, | |
| 119 ScriptPromiseResolver* promiseResolver, | |
| 120 void* shmReadbackResultData, | |
| 121 GLuint commandsIssuedQueryID, | |
| 122 DOMArrayBufferView* destinationArrayBufferView, | |
| 123 void* destinationDataPtr, | |
| 124 long long destinationByteLength) | |
| 125 : m_context(context), | |
| 126 m_promiseResolver(promiseResolver), | |
| 127 m_shmReadbackResultData(shmReadbackResultData), | |
| 128 m_commandsIssuedQueryID(commandsIssuedQueryID), | |
| 129 m_destinationArrayBufferView(destinationArrayBufferView), | |
| 130 m_destinationDataPtr(destinationDataPtr), | |
| 131 m_destinationByteLength(destinationByteLength) { | |
| 132 DCHECK(shmReadbackResultData); | |
| 133 DCHECK(destinationDataPtr); | |
| 134 } | |
| 135 | |
| 136 void WebGLGetBufferSubDataAsyncCallback::destroy() { | |
| 137 DCHECK(m_shmReadbackResultData); | |
| 138 m_context->contextGL()->FreeSharedMemory(m_shmReadbackResultData); | |
| 139 m_shmReadbackResultData = nullptr; | |
| 140 DOMException* exception = | |
| 141 DOMException::create(InvalidStateError, "Context lost or destroyed"); | |
| 142 m_promiseResolver->reject(exception); | |
| 143 } | |
| 144 | |
| 145 void WebGLGetBufferSubDataAsyncCallback::resolve() { | |
| 146 if (!m_context || !m_shmReadbackResultData) { | |
| 147 DOMException* exception = | |
| 148 DOMException::create(InvalidStateError, "Context lost or destroyed"); | |
| 149 m_promiseResolver->reject(exception); | |
| 150 return; | |
| 151 } | |
| 152 if (m_destinationArrayBufferView->buffer()->isNeutered()) { | |
| 153 DOMException* exception = DOMException::create( | |
| 154 InvalidStateError, "ArrayBufferView became invalid asynchronously"); | |
| 155 m_promiseResolver->reject(exception); | |
| 156 return; | |
| 157 } | |
| 158 memcpy(m_destinationDataPtr, m_shmReadbackResultData, | |
| 159 m_destinationByteLength); | |
| 160 // TODO(kainino): What would happen if the DOM was suspended when the | |
| 161 // promise became resolved? Could another JS task happen between the memcpy | |
| 162 // and the promise resolution task, which would see the wrong data? | |
| 163 m_promiseResolver->resolve(m_destinationArrayBufferView); | |
| 164 | |
| 165 m_context->contextGL()->DeleteQueriesEXT(1, &m_commandsIssuedQueryID); | |
| 166 this->destroy(); | |
| 167 m_context->unregisterGetBufferSubDataAsyncCallback(this); | |
| 168 } | |
| 169 | |
| 170 DEFINE_TRACE(WebGLGetBufferSubDataAsyncCallback) { | |
| 171 visitor->trace(m_context); | |
| 172 visitor->trace(m_promiseResolver); | |
| 173 visitor->trace(m_destinationArrayBufferView); | |
| 174 } | |
| 175 | |
| 176 } // namespace blink | |
| OLD | NEW |