summaryrefslogtreecommitdiff
path: root/indra/newview/llavatarrenderinfoaccountant.cpp
blob: fe94cd27b6d5dfb0e9d938f37965d3bf7658cb9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
 * @file   llavatarrenderinfoaccountant.cpp
 * @author Dave Simmons
 * @date   2013-02-28
 * @brief  
 * 
 * $LicenseInfo:firstyear=2013&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2013, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

// Precompiled header
#include "llviewerprecompiledheaders.h"
// associated header
#include "llavatarrenderinfoaccountant.h"
#include "llavatarrendernotifier.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "llcharacter.h"
#include "lltimer.h"
#include "llviewercontrol.h"
#include "llviewermenu.h"
#include "llviewerobjectlist.h"
#include "llviewerregion.h"
#include "llvoavatar.h"
#include "llworld.h"
#include "llhttpsdhandler.h"
#include "httpheaders.h"
#include "httpoptions.h"
#include "llcorehttputil.h"

static	const std::string KEY_AGENTS = "agents";			// map
static 	const std::string KEY_WEIGHT = "weight";			// integer
static 	const std::string KEY_TOO_COMPLEX  = "tooComplex";  // bool
static  const std::string KEY_OVER_COMPLEXITY_LIMIT = "overlimit";  // integer
static  const std::string KEY_REPORTING_COMPLEXITY_LIMIT = "reportinglimit";  // integer

static	const std::string KEY_IDENTIFIER = "identifier";
static	const std::string KEY_MESSAGE = "message";
static	const std::string KEY_ERROR = "error";

static const F32 SECS_BETWEEN_REGION_SCANS   =  5.f;		// Scan the region list every 5 seconds
static const F32 SECS_BETWEEN_REGION_REQUEST = 15.0;		// Look for new avs every 15 seconds
static const F32 SECS_BETWEEN_REGION_REPORTS = 60.0;		// Update each region every 60 seconds
	

//=========================================================================
LLAvatarRenderInfoAccountant::LLAvatarRenderInfoAccountant()
{
}

LLAvatarRenderInfoAccountant::~LLAvatarRenderInfoAccountant()
{
}


void LLAvatarRenderInfoAccountant::avatarRenderInfoGetCoro(std::string url, U64 regionHandle)
{
    LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID);
    LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t 
        httpAdapter(new LLCoreHttpUtil::HttpCoroutineAdapter("AvatarRenderInfoAccountant", httpPolicy));
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);

    LLSD result = httpAdapter->getAndSuspend(httpRequest, url);

    LLWorld *world_inst = LLWorld::getInstance();
    if (!world_inst)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Avatar render weight info received but world no longer exists "
            << regionHandle << LL_ENDL;
        return;
    }

    LLViewerRegion * regionp = world_inst->getRegionFromHandle(regionHandle);
    if (!regionp)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Avatar render weight info received but region not found for " 
                << regionHandle << LL_ENDL;
        return;
    }

    regionp->getRenderInfoRequestTimer().resetWithExpiry(SECS_BETWEEN_REGION_REQUEST);

    LLSD httpResults = result["http_result"];
    LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);

    if (!status)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "HTTP status, " << status.toTerseString() << LL_ENDL;
        return;
    }

    if (result.has(KEY_AGENTS))
    {
        const LLSD & agents = result[KEY_AGENTS];
        if (agents.isMap())
        {
            for (LLSD::map_const_iterator agent_iter = agents.beginMap();
                 agent_iter != agents.endMap();
                 agent_iter++
                 )
            {
                LLUUID target_agent_id = LLUUID(agent_iter->first);
                LLViewerObject* vobjp = gObjectList.findObject(target_agent_id);
                LLVOAvatar *avatarp = NULL;
                if (vobjp)
                {
                    avatarp = vobjp->asAvatar();
                }
                if (avatarp && !avatarp->isControlAvatar())
                {
                    const LLSD & agent_info_map = agent_iter->second;
                    if (agent_info_map.isMap())
                    {
                        LL_DEBUGS("AvatarRenderInfo") << " Agent " << target_agent_id 
                                                      << ": " << agent_info_map << LL_ENDL;

                        if (agent_info_map.has(KEY_WEIGHT))
                        {
                            avatarp->setReportedVisualComplexity(agent_info_map[KEY_WEIGHT].asInteger());
                        }
                    }
                    else
                    {
                        LL_WARNS("AvatarRenderInfo") << "agent entry invalid"
                                                     << " agent " << target_agent_id
                                                     << " map " << agent_info_map
                                                     << LL_ENDL;
                    }
                }
                else
                {
                    LL_DEBUGS("AvatarRenderInfo") << "Unknown agent " << target_agent_id << LL_ENDL;
                }
            } // for agent_iter
        }
        else
        {
            LL_WARNS("AvatarRenderInfo") << "malformed get response '" << KEY_AGENTS << "' is not map" << LL_ENDL;
        }
    }	// has "agents"
    else
    {
        LL_INFOS("AvatarRenderInfo") << "no '"<< KEY_AGENTS << "' key in get response" << LL_ENDL;
    }

    if (   result.has(KEY_REPORTING_COMPLEXITY_LIMIT)
        && result.has(KEY_OVER_COMPLEXITY_LIMIT))
    {
        U32 reporting = result[KEY_REPORTING_COMPLEXITY_LIMIT].asInteger();
        U32 overlimit = result[KEY_OVER_COMPLEXITY_LIMIT].asInteger();

        LL_DEBUGS("AvatarRenderInfo") << "complexity limit: "<<reporting<<" reporting, "<<overlimit<<" over limit"<<LL_ENDL;

        LLAvatarRenderNotifier::getInstance()->updateNotificationRegion(reporting, overlimit);
    }
    else
    {
        LL_WARNS("AvatarRenderInfo")
            << "response is missing either '" << KEY_REPORTING_COMPLEXITY_LIMIT
            << "' or '" << KEY_OVER_COMPLEXITY_LIMIT << "'"
            << LL_ENDL;
    }

}

//-------------------------------------------------------------------------
void LLAvatarRenderInfoAccountant::avatarRenderInfoReportCoro(std::string url, U64 regionHandle)
{
    LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID);
    LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t
        httpAdapter(new LLCoreHttpUtil::HttpCoroutineAdapter("AvatarRenderInfoAccountant", httpPolicy));
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);

    LLWorld *world_inst = LLWorld::getInstance();
    if (!world_inst)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Avatar render weight calculation but world no longer exists "
            << regionHandle << LL_ENDL;
        return;
    }

    LLViewerRegion * regionp = world_inst->getRegionFromHandle(regionHandle);
    if (!regionp)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Avatar render weight calculation but region not found for "
            << regionHandle << LL_ENDL;
        return;
    }

    LL_DEBUGS("AvatarRenderInfoAccountant")
        << "Sending avatar render info for region " << regionp->getName()
        << " to " << url << LL_ENDL;

    U32 num_avs = 0;
    // Build the render info to POST to the region
    LLSD agents = LLSD::emptyMap();
				
    std::vector<LLCharacter*>::iterator iter = LLCharacter::sInstances.begin();
    while( iter != LLCharacter::sInstances.end() )
    {
        LLVOAvatar* avatar = dynamic_cast<LLVOAvatar*>(*iter);
        if (avatar &&
            avatar->getRezzedStatus() >= 2 &&					// Mostly rezzed (maybe without baked textures downloaded)
            !avatar->isDead() &&								// Not dead yet
            !avatar->isControlAvatar() &&						// Not part of an animated object
            avatar->getObjectHost() == regionp->getHost())		// Ensure it's on the same region
        {
            avatar->calculateUpdateRenderComplexity();			// Make sure the numbers are up-to-date

            LLSD info = LLSD::emptyMap();
            U32 avatar_complexity = avatar->getVisualComplexity();
            if (avatar_complexity > 0)
            {
                // the weight/complexity is unsigned, but LLSD only stores signed integers,
                // so if it's over that (which would be ridiculously high), just store the maximum signed int value
                info[KEY_WEIGHT] = (S32)(avatar_complexity < S32_MAX ? avatar_complexity : S32_MAX);
                info[KEY_TOO_COMPLEX]  = LLSD::Boolean(avatar->isTooComplex());
                agents[avatar->getID().asString()] = info;

                LL_DEBUGS("AvatarRenderInfo") << "Sending avatar render info for " << avatar->getID()
                                              << ": " << info << LL_ENDL;
                num_avs++;
            }
        }
        iter++;
    }

    // Reset this regions timer, moving to longer intervals if there are lots of avatars around
    regionp->getRenderInfoReportTimer().resetWithExpiry(SECS_BETWEEN_REGION_REPORTS + (2.f * num_avs));

    if (num_avs == 0)
        return; // nothing to report
    
    LLSD report = LLSD::emptyMap();
    report[KEY_AGENTS] = agents;

    regionp = NULL;
    world_inst = NULL;
    LLSD result = httpAdapter->postAndSuspend(httpRequest, url, report);

    world_inst = LLWorld::getInstance();
    if (!world_inst)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Avatar render weight POST result but world no longer exists "
            << regionHandle << LL_ENDL;
        return;
    }

    regionp = world_inst->getRegionFromHandle(regionHandle);
    if (!regionp)
    {
        LL_INFOS("AvatarRenderInfoAccountant") << "Avatar render weight POST result received but region not found for "
                << regionHandle << LL_ENDL;
        return;
    }

    LLSD httpResults = result["http_result"];
    LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);
    if (!status)
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "HTTP status, " << status.toTerseString() << LL_ENDL;
        return;
    }

    if (result.isMap())
    {
        if (result.has(KEY_ERROR))
        {
            const LLSD & error = result[KEY_ERROR];
            LL_WARNS("AvatarRenderInfoAccountant") << "POST error: "
                << error[KEY_IDENTIFIER]
                << ": " << error[KEY_MESSAGE] 
                << " from region " << regionp->getName()
                << LL_ENDL;
        }
        else
        {
            LL_DEBUGS("AvatarRenderInfoAccountant")
                << "POST result for region " << regionp->getName()
                << ": " << result
                << LL_ENDL;
        }
    }
    else
    {
        LL_WARNS("AvatarRenderInfoAccountant") << "Malformed POST response from region '" << regionp->getName()
                                               << LL_ENDL;
    }
}

// Send request for avatar weights in one region
// called when the mRenderInfoScanTimer expires (forced when entering a new region)
void LLAvatarRenderInfoAccountant::sendRenderInfoToRegion(LLViewerRegion * regionp)
{
	std::string url = regionp->getCapability("AvatarRenderInfo");
	if (   !url.empty() // we have the capability
        && regionp->getRenderInfoReportTimer().hasExpired() // Time to make request)
        )
	{
        // make sure we won't re-report, coro will update timer with correct time later
        regionp->getRenderInfoReportTimer().resetWithExpiry(SECS_BETWEEN_REGION_REPORTS);

        std::string coroname =
            LLCoros::instance().launch("LLAvatarRenderInfoAccountant::avatarRenderInfoReportCoro",
            boost::bind(&LLAvatarRenderInfoAccountant::avatarRenderInfoReportCoro, url, regionp->getHandle()));
	}
}

// Send request for avatar weights in one region
// called when the mRenderInfoScanTimer expires (forced when entering a new region)
void LLAvatarRenderInfoAccountant::getRenderInfoFromRegion(LLViewerRegion * regionp)
{
	std::string url = regionp->getCapability("AvatarRenderInfo");
	if (   !url.empty()
        && regionp->getRenderInfoRequestTimer().hasExpired()
        )
	{
        LL_DEBUGS("AvatarRenderInfo")
            << "Requesting avatar render info for region " << regionp->getName() 
            << " from " << url
            << LL_ENDL;

        // make sure we won't re-request, coro will update timer with correct time later
        regionp->getRenderInfoRequestTimer().resetWithExpiry(SECS_BETWEEN_REGION_REQUEST);

		// First send a request to get the latest data
        std::string coroname =
            LLCoros::instance().launch("LLAvatarRenderInfoAccountant::avatarRenderInfoGetCoro",
            boost::bind(&LLAvatarRenderInfoAccountant::avatarRenderInfoGetCoro, url, regionp->getHandle()));
	}
}

// static
// Called every frame - send render weight requests to every region
void LLAvatarRenderInfoAccountant::idle()
{
	if (mRenderInfoScanTimer.hasExpired())
	{
		LL_DEBUGS("AvatarRenderInfo") << "Scanning regions for render info updates"
									  << LL_ENDL;

		// Check all regions
		for (LLWorld::region_list_t::const_iterator iter = LLWorld::getInstance()->getRegionList().begin();
			 iter != LLWorld::getInstance()->getRegionList().end();
			 ++iter)
		{
			LLViewerRegion* regionp = *iter;
			if (   regionp
				&& regionp->isAlive()
				&& regionp->capabilitiesReceived())
			{
				// each of these is further governed by and resets its own timer
                // Note: We can have multiple regions, each launches up to two coroutines,
                // it likely is expensive
				sendRenderInfoToRegion(regionp);
				getRenderInfoFromRegion(regionp);
			}
		}

		// We scanned all the regions, reset the request timer.
		mRenderInfoScanTimer.resetWithExpiry(SECS_BETWEEN_REGION_SCANS);
	}
}

void LLAvatarRenderInfoAccountant::resetRenderInfoScanTimer()
{
	// this will force the next frame to rescan
	mRenderInfoScanTimer.reset();
}

// static
// Called via LLViewerRegion::setCapabilitiesReceived() boost signals when the capabilities
// are returned for a new LLViewerRegion, and is the earliest time to get render info
void LLAvatarRenderInfoAccountant::scanNewRegion(const LLUUID& region_id)
{
	LL_DEBUGS("AvatarRenderInfo") << region_id << LL_ENDL;

	// Reset the global timer so it will scan regions on the next call to ::idle
	LLAvatarRenderInfoAccountant::getInstance()->resetRenderInfoScanTimer();
	
	LLViewerRegion* regionp = LLWorld::instance().getRegionFromID(region_id);
	if (regionp)
	{	// Reset the region's timers so we will:
		//  * request render info from it immediately
		//  * report on the following scan
		regionp->getRenderInfoRequestTimer().reset();
		regionp->getRenderInfoReportTimer().resetWithExpiry(SECS_BETWEEN_REGION_SCANS);
	}
	else
	{
		LL_WARNS("AvatarRenderInfo") << "unable to resolve region "<<region_id<<LL_ENDL;
	}
}