1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2019 Google LLC 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "src/gpu/GrClientMappedBufferManager.h" 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include <algorithm> 11cb93a386Sopenharmony_ci 12cb93a386Sopenharmony_ciGrClientMappedBufferManager::GrClientMappedBufferManager( 13cb93a386Sopenharmony_ci GrDirectContext::DirectContextID owningDirectContext) 14cb93a386Sopenharmony_ci : fFinishedBufferInbox(owningDirectContext) { 15cb93a386Sopenharmony_ci} 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ciGrClientMappedBufferManager::~GrClientMappedBufferManager() { 18cb93a386Sopenharmony_ci this->process(); 19cb93a386Sopenharmony_ci if (!fAbandoned) { 20cb93a386Sopenharmony_ci // If we're going down before we got the messages we go ahead and unmap all the buffers. 21cb93a386Sopenharmony_ci // It's up to the client to ensure that they aren't being accessed on another thread while 22cb93a386Sopenharmony_ci // this is happening (or afterwards on any thread). 23cb93a386Sopenharmony_ci for (auto& b : fClientHeldBuffers) { 24cb93a386Sopenharmony_ci b->unmap(); 25cb93a386Sopenharmony_ci } 26cb93a386Sopenharmony_ci } 27cb93a386Sopenharmony_ci} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_civoid GrClientMappedBufferManager::insert(sk_sp<GrGpuBuffer> b) { 30cb93a386Sopenharmony_ci SkDEBUGCODE(auto end = fClientHeldBuffers.end()); 31cb93a386Sopenharmony_ci SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) == end); 32cb93a386Sopenharmony_ci fClientHeldBuffers.emplace_front(std::move(b)); 33cb93a386Sopenharmony_ci} 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_civoid GrClientMappedBufferManager::process() { 36cb93a386Sopenharmony_ci SkSTArray<4, BufferFinishedMessage> messages; 37cb93a386Sopenharmony_ci fFinishedBufferInbox.poll(&messages); 38cb93a386Sopenharmony_ci if (!fAbandoned) { 39cb93a386Sopenharmony_ci for (auto& m : messages) { 40cb93a386Sopenharmony_ci this->remove(m.fBuffer); 41cb93a386Sopenharmony_ci m.fBuffer->unmap(); 42cb93a386Sopenharmony_ci } 43cb93a386Sopenharmony_ci } 44cb93a386Sopenharmony_ci} 45cb93a386Sopenharmony_ci 46cb93a386Sopenharmony_civoid GrClientMappedBufferManager::abandon() { 47cb93a386Sopenharmony_ci fAbandoned = true; 48cb93a386Sopenharmony_ci fClientHeldBuffers.clear(); 49cb93a386Sopenharmony_ci} 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_civoid GrClientMappedBufferManager::remove(const sk_sp<GrGpuBuffer>& b) { 52cb93a386Sopenharmony_ci // There is no convenient remove only the first element that equals a value functionality in 53cb93a386Sopenharmony_ci // std::forward_list. 54cb93a386Sopenharmony_ci auto prev = fClientHeldBuffers.before_begin(); 55cb93a386Sopenharmony_ci auto end = fClientHeldBuffers.end(); 56cb93a386Sopenharmony_ci SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) != end); 57cb93a386Sopenharmony_ci for (auto cur = fClientHeldBuffers.begin(); cur != end; prev = cur++) { 58cb93a386Sopenharmony_ci if (*cur == b) { 59cb93a386Sopenharmony_ci fClientHeldBuffers.erase_after(prev); 60cb93a386Sopenharmony_ci break; 61cb93a386Sopenharmony_ci } 62cb93a386Sopenharmony_ci } 63cb93a386Sopenharmony_ci SkASSERT(std::find(fClientHeldBuffers.begin(), end, b) == end); 64cb93a386Sopenharmony_ci} 65cb93a386Sopenharmony_ci 66cb93a386Sopenharmony_ci////////////////////////////////////////////////////////////////////////////// 67cb93a386Sopenharmony_ci 68cb93a386Sopenharmony_ciDECLARE_SKMESSAGEBUS_MESSAGE(GrClientMappedBufferManager::BufferFinishedMessage, 69cb93a386Sopenharmony_ci GrDirectContext::DirectContextID, 70cb93a386Sopenharmony_ci false) 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_cibool SkShouldPostMessageToBus(const GrClientMappedBufferManager::BufferFinishedMessage& m, 73cb93a386Sopenharmony_ci GrDirectContext::DirectContextID potentialRecipient) { 74cb93a386Sopenharmony_ci return m.fIntendedRecipient == potentialRecipient; 75cb93a386Sopenharmony_ci} 76