1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_DisplayBufferSwapper.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 23208 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_DEMO_DISPLAYBUFFERSWAPPER_H_
17 #define NW_DEMO_DISPLAYBUFFERSWAPPER_H_
18 
19 #include <nw/gfx/gfx_GfxObject.h>
20 #include <nw/gfx/gfx_IRenderTarget.h>
21 
22 #include <nw/ut/ut_MoveArray.h>
23 #include <nw/ut/ut_Preprocessor.h>
24 
25 namespace nw
26 {
27 namespace os
28 {
29 class IAllocator;
30 } // namespace os
31 
32 namespace demo
33 {
34 
35 //---------------------------------------------------------------------------
36 //! @brief        スクリーンの種別を表す定義です。
37 //---------------------------------------------------------------------------
38 enum ScreenKind
39 {
40     UPPER_SCREEN        = 0x1 << 0,   //!< 上画面を表します。
41     LOWER_SCREEN        = 0x1 << 1,   //!< 下画面を表します。
42     EXTENSION_SCREEN    = 0x1 << 2,   //!< 拡張スクリーンを表します。
43     NORMAL_SCREENS      = UPPER_SCREEN | LOWER_SCREEN,
44     BOTH_SCREENS        = UPPER_SCREEN | LOWER_SCREEN | EXTENSION_SCREEN
45 };
46 
47 //---------------------------------------------------------------------------
48 //! @brief        ディスプレイバッファの転送モードを表す定義です。
49 //---------------------------------------------------------------------------
50 enum BufferTransferMode
51 {
52     TRANSFER_MODE_ANTIALIASE_NOT_USED,  //!< アンチエイリアスは行いません。
53     TRANSFER_MODE_ANTIALIASE_2x1,       //!< 2×1アンチエイリアスで転送します。
54     TRANSFER_MODE_ANTIALIASE_2x2,       //!< 2×2アンチエイリアスで転送します。
55     TRANSFER_MODE_ANTIALIASE_COUNT      //!< 転送モードの数を表します。
56 };
57 
58 //---------------------------------------------------------------------------
59 //! @brief        ディスプレイバッファの転送を行うクラスです。
60 //---------------------------------------------------------------------------
61 class DisplayBufferSwapper : public gfx::GfxObject
62 {
63 private:
64     NW_DISALLOW_COPY_AND_ASSIGN(DisplayBufferSwapper);
65 
66 public:
67     NW_UT_RUNTIME_TYPEINFO;
68 
69     //----------------------------------------
70     //! @name 取得/設定
71     //@{
72 
73     //! @brief ディスプレイバッファスワッパの設定内容です。
74     struct Description
75     {
76         ScreenKind screenKind;          //!< スクリーンバッファの種類です。
77         s32 width;                      //!< バッファの横幅です。
78         s32 height;                     //!< バッファの縦幅です。
79         gfx::RenderColorFormat format;  //!< バッファのカラーフォーマットです。
80         BufferTransferMode transferMode;//!< バッファの転送モードです。
81         nw::gfx::GraphicsMemoryArea memoryArea;  //!< 配置するメモリ領域です。
82         bool isTransferFlipX;           //!< 転送時にX方向にフリップするかどうかを表します。
83         s32 transferOffsetX;            //!< 転送を行う始点X座標です。
84         s32 transferOffsetY;            //!< 転送を行う始点Y座標です。
85         s32 displayOffsetX;             //!< 表示を行う始点X座標です。
86         s32 displayOffsetY;             //!< 表示を行う始点Y座標です。
87         size_t bufferCount;             //!< ディスプレイバッファの数です。
88 
89         //! @brief コンストラクタです。
DescriptionDescription90         Description()
91          : screenKind(UPPER_SCREEN),
92            width(400),
93            height(240),
94            format(gfx::RENDER_COLOR_FORMAT_RGB8),
95            transferMode(TRANSFER_MODE_ANTIALIASE_NOT_USED),
96            memoryArea(nw::gfx::MEMORY_AREA_FCRAM),
97            isTransferFlipX(false),
98            transferOffsetX(0),
99            transferOffsetY(0),
100            displayOffsetX(0),
101            displayOffsetY(0),
102            bufferCount(1)
103         {}
104     };
105 
106     //! @brief ディスプレイバッファスワッパを構築するためのクラスです。
107     class Builder
108     {
109     public:
110         //! @brief 構築するディスプレイバッファの内容を設定します。
BufferDescription(const Description & description)111         Builder& BufferDescription(const Description& description)
112         {
113             m_Description = description;
114             return *this;
115         }
116 
117         //! @brief 構築するディスプレイバッファのスクリーンを設定します。
ScreenKind(ScreenKind kind)118         Builder& ScreenKind(ScreenKind kind)
119         {
120             m_Description.screenKind = kind;
121             return *this;
122         }
123 
124         //! @brief 構築するディスプレイバッファのサイズを設定します。
BufferSize(s32 width,s32 height)125         Builder& BufferSize(s32 width, s32 height)
126         {
127             m_Description.width = width;
128             m_Description.height = height;
129             return *this;
130         }
131 
132         //! @brief 構築するディスプレイバッファの横幅を設定します。
Width(s32 width)133         Builder& Width(s32 width) { m_Description.width = width; return *this; }
134 
135         //! @brief 構築するディスプレイバッファの縦幅を設定します。
Height(s32 height)136         Builder& Height(s32 height) { m_Description.height = height; return *this; }
137 
138         //! @brief 構築するディスプレイバッファのカラーフォーマットを設定します。
Format(gfx::RenderColorFormat format)139         Builder& Format(gfx::RenderColorFormat format)
140         {
141             m_Description.format = format;
142             return *this;
143         }
144 
145         //! @brief 構築するディスプレイバッファの転送モードを設定します。
TransferMode(BufferTransferMode mode)146         Builder& TransferMode(BufferTransferMode mode)
147         {
148             m_Description.transferMode = mode;
149             return *this;
150         }
151 
152         //! @brief 構築するディスプレイバッファのメモリ配置を設定します。
MemoryArea(nw::gfx::GraphicsMemoryArea area)153         Builder& MemoryArea(nw::gfx::GraphicsMemoryArea area)
154         {
155             m_Description.memoryArea = area;
156             return *this;
157         }
158 
159         //! @brief 構築するディスプレイバッファをX方向に反転するかどうかを設定します。
IsTransferFlipX(bool isTransferFlipX)160         Builder& IsTransferFlipX(bool isTransferFlipX)
161         {
162             m_Description.isTransferFlipX = isTransferFlipX;
163             return *this;
164         }
165 
166         //! @brief 構築するディスプレイバッファにカラーバッファを転送する始点を設定します。
TransferOffset(s32 x,s32 y)167         Builder& TransferOffset(s32 x, s32 y)
168         {
169             m_Description.transferOffsetX = x;
170             m_Description.transferOffsetY = y;
171             return *this;
172         }
173 
174         //! @brief 構築するディスプレイバッファの表示領域の始点を設定します。
DisplayOffset(s32 x,s32 y)175         Builder& DisplayOffset(s32 x, s32 y)
176         {
177             m_Description.displayOffsetX = x;
178             m_Description.displayOffsetY = y;
179             return *this;
180         }
181 
182         //! @brief 構築するディスプレイバッファの数を設定します。
BufferCount(size_t count)183         Builder& BufferCount(size_t count) { m_Description.bufferCount = count; return *this; }
184 
185         //---------------------------------------------------------------------------
186         //! @brief        ディスプレイバッファスワッパを生成します。
187         //!
188         //! @param[in]    allocator アロケータです。
189         //!
190         //! @return       生成したディスプレイバッファスワッパを返します。
191         //---------------------------------------------------------------------------
192         DisplayBufferSwapper* Create(os::IAllocator* allocator);
193 
194     private:
195         Description m_Description;
196     };
197 
198     //! @brief 設定内容を取得します。
199     //!
200     //! @return 設定内容を返します。
GetDescription()201     const Description& GetDescription() const { return m_Description; }
202 
203     //@}
204 
205 
206     //---------------------------------------------------------------------------
207     //! @brief          ディスプレイバッファにターゲットのバッファを転送するコマンドをコマンドリストに追加します。
208     //!
209     //!                 レンダーバッファーをディスプレイバッファに転送するコマンドを生成し、
210     //!                 バインドされているコマンドリストに追加します。
211     //!
212     //!                 コマンドリストが多重化されている場合は転送コマンドが実行されるタイミングが変わりますので、
213     //!                 転送先のディスプレイバッファを変更します。
214     //!                 isMultiCommandList に true を指定することでこの変更は自動的に行われます。
215     //!
216     //! @param[in]      target ディスプレイバッファに転送するバッファです。
217     //! @param[in]      isMultiCommandList コマンドリストが多重化されているかを指定します。
218     //---------------------------------------------------------------------------
219     void MakeTransferBufferCommand(gfx::IRenderTarget* target, bool isMultiCommandList);
220 
221     //---------------------------------------------------------------------------
222     //! @brief        ディスプレイバッファを有効にします。
223     //---------------------------------------------------------------------------
224     void ActivateBuffer();
225 
226     //----------------------------------------
227     //! @name コンストラクタ/デストラクタ
228     //@{
229 private:
230     //! コンストラクタです。
231     DisplayBufferSwapper(
232         os::IAllocator* allocator,
233         const Description& description);
234 
DisplayBufferSwapper(os::IAllocator * allocator)235     DisplayBufferSwapper(os::IAllocator* allocator) : GfxObject(allocator) {}
236 
237 protected:
238     //! デストラクタです。
~DisplayBufferSwapper()239     virtual ~DisplayBufferSwapper()
240     {
241         if (m_DisplayBuffers)
242         {
243             nngxDeleteDisplaybuffers( m_Description.bufferCount, m_DisplayBuffers );
244             nw::os::SafeFree(m_DisplayBuffers, &this->GetAllocator());
245         }
246     }
247 
248     //@}
249 
250 private:
251     Description m_Description;
252     u32 m_CurrentDisplay;
253     GLuint* m_DisplayBuffers;
254 };
255 
256 } // namespace demo
257 } // namespace nw
258 
259 #endif // NW_DEMO_DISPLAYBUFFERSWAPPER_H_
260