/*---------------------------------------------------------------------------* Project: NintendoWare File: dev_Screenshot.cpp Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo and/or its licensed developers and are protected by national and international copyright laws. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. The content herein is highly confidential and should be handled accordingly. $Revision: 31311 $ *---------------------------------------------------------------------------*/ #include #include #include #if defined(NW_DEV_ENABLED) && defined(NW_PLATFORM_CTR) namespace nw { namespace dev { //-------------------------------------------------------------------------- Screenshot* Screenshot::Create( nw::os::IAllocator* allocator, size_t maxLength) { const int HASH_LENGTH = 20; u32* hash = allocator->AllocAndFill(HASH_LENGTH, static_cast(0)); NW_NULL_ASSERT(hash); NW_ASSERT(maxLength != 0); u8* copyScreen = allocator->AllocAndFill(maxLength, static_cast(0)); NW_NULL_ASSERT(copyScreen); void* memory = allocator->Alloc(sizeof(Screenshot)); NW_NULL_ASSERT(memory); return new(memory) Screenshot( allocator, copyScreen, maxLength, hash); } //-------------------------------------------------------------------------- Screenshot::~Screenshot() { if (this->m_Allocator) { if (this->m_CopyScreen) { (this->m_Allocator)->Free(this->m_CopyScreen); } if (this->m_Hash) { (this->m_Allocator)->Free(this->m_Hash); } } } //-------------------------------------------------------------------------- void Screenshot::Set(const DisplayDescription& displayDescription, void* srcScreen, int length, int width, int height) { std::memcpy(m_CopyScreen, srcScreen, length); this->m_DisplayDescription = displayDescription; this->m_Length = length; this->m_Writed = true; this->m_Width = width; this->m_Height = height; nn::crypto::CalculateSha1(this->m_Hash, m_CopyScreen, m_Length); } //-------------------------------------------------------------------------- void Screenshot::DumpHash() const { const char* DISPLAY_0 = "NN_GX_DISPLAY0 "; const char* DISPLAY_0_EXT = "NN_GX_DISPLAY0EXT"; const char* DISPLAY_1 = "NN_GX_DISPLAY1 "; const char* display; switch (this->m_DisplayDescription.display) { case NN_GX_DISPLAY0: display = DISPLAY_0; break; case NN_GX_DISPLAY0_EXT: display = DISPLAY_0_EXT; break; case NN_GX_DISPLAY1: display = DISPLAY_1; break; default: NW_FATAL_ERROR("Unsupported display type"); } NW_DEV_LOG("|%s|0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x|\n", display, m_Hash[0], m_Hash[1], m_Hash[2], m_Hash[3], m_Hash[4]); } //-------------------------------------------------------------------------- ScreenshotManager* ScreenshotManager::Create( nw::os::IAllocator* allocator, int maxScreenshot, int memorySize ) { NW_UNUSED_VARIABLE(maxScreenshot); void* memory = allocator->Alloc(sizeof(Screenshot)); ScreenshotArray screenshots = ScreenshotArray(maxScreenshot, allocator); for (int i = 0; i < maxScreenshot; i++) { screenshots.push_back(Screenshot::Create(allocator, memorySize)); } return new(memory) ScreenshotManager(allocator, screenshots); } //-------------------------------------------------------------------------- void ScreenshotManager::Destroy(nw::os::IAllocator* allocator) { this->~ScreenshotManager(); allocator->Free(this); } //-------------------------------------------------------------------------- ScreenshotManager::ScreenshotManager( nw::os::IAllocator* allocator, ScreenshotArray screenshots ) : m_Allocator(allocator), m_Screenshots(screenshots), m_NextWrited(0) { } //-------------------------------------------------------------------------- ScreenshotManager::~ScreenshotManager() { this->ClearScreenshots(); } //-------------------------------------------------------------------------- int ScreenshotManager::Take( GLenum display, int offsetX, int offsetY) { DisplayDescription displayDescription; displayDescription.display = display; nngxActiveDisplay(display); GLint address; nngxGetDisplaybufferParameteri(NN_GX_DISPLAYBUFFER_ADDRESS, &address); void* srcArea = reinterpret_cast(address); nngxGetDisplaybufferParameteri(NN_GX_DISPLAYBUFFER_FORMAT, &displayDescription.format); nngxGetDisplaybufferParameteri(NN_GX_DISPLAYBUFFER_WIDTH, &displayDescription.width); nngxGetDisplaybufferParameteri(NN_GX_DISPLAYBUFFER_HEIGHT, &displayDescription.height); displayDescription.offsetX = offsetX; displayDescription.offsetY = offsetY; if (displayDescription.format == 0 || displayDescription.width == 0 || displayDescription.height == 0) { return -1; } int length = CalcMemorySize(displayDescription.format, displayDescription.width, displayDescription.height); ScreenshotRange range = this->GetScreenshots(); if (m_NextWrited < m_Screenshots.size()) { Screenshot* screenshot = m_Screenshots[m_NextWrited]; screenshot->Set(displayDescription, srcArea, length, displayDescription.width, displayDescription.height); } else { return -1; } ++m_NextWrited; if (m_NextWrited >= m_Screenshots.size()) { m_NextWrited = 0; } return m_NextWrited; } //-------------------------------------------------------------------------- void ScreenshotManager::ClearScreenshots() { ScreenshotRange range = this->GetScreenshots(); for (ScreenshotArray::iterator iter = range.first; iter != range.second; ++iter) { Screenshot* screenshot = *iter; if (screenshot != NULL) { screenshot->~Screenshot(); m_Allocator->Free(screenshot); } } this->m_Screenshots.clear(); } } // namespace nw::dev } // namespace nw #endif // NW_DEV_ENABLED