aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/hle/service/y2r_u.cpp
blob: 73a0899ddfb06dbf6254b12dbf8d754146e48706 (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
// Copyright 2015 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <cstring>

#include "common/logging/log.h"

#include "core/hle/hle.h"
#include "core/hle/kernel/event.h"
#include "core/hle/service/y2r_u.h"
#include "core/mem_map.h"
#include "core/memory.h"

#include "video_core/utils.h"
#include "video_core/video_core.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
// Namespace Y2R_U

namespace Y2R_U {

enum class InputFormat : u8 {
    /// 8-bit input, with YUV components in separate planes and using 4:2:2 subsampling.
    YUV422_Indiv8 = 0,
    /// 8-bit input, with YUV components in separate planes and using 4:2:0 subsampling.
    YUV420_Indiv8 = 1,

    YUV422_INDIV_16 = 2,
    YUV420_INDIV_16 = 3,
    YUV422_BATCH = 4,
};

enum class OutputFormat : u8 {
    Rgb32 = 0,
    Rgb24 = 1,
    Rgb16_555 = 2,
    Rgb16_565 = 3,
};

enum class Rotation : u8 {
    None = 0,
    Clockwise_90 = 1,
    Clockwise_180 = 2,
    Clockwise_270 = 3,
};

enum class BlockAlignment : u8 {
    /// Image is output in linear format suitable for use as a framebuffer.
    Linear = 0,
    /// Image is output in tiled PICA format, suitable for use as a texture.
    Block8x8 = 1,
};

enum class StandardCoefficient : u8 {
    ITU_Rec601 = 0,
    ITU_Rec709 = 1,
    ITU_Rec601_Scaling = 2,
    ITU_Rec709_Scaling = 3,
};

static Kernel::SharedPtr<Kernel::Event> completion_event;

struct ConversionParameters {
    InputFormat input_format;
    OutputFormat output_format;
    Rotation rotation;
    BlockAlignment block_alignment;
    u16 input_line_width;
    u16 input_lines;
    StandardCoefficient standard_coefficient;
    u8 reserved;
    u16 alpha;
};
static_assert(sizeof(ConversionParameters) == 12, "ConversionParameters struct has incorrect size");

struct ConversionBuffer {
    VAddr address;
    u32 image_size;
    u16 transfer_unit;
    u16 stride;
};

struct ConversionData {
    ConversionParameters params;
    /// Input parameters for the Y (luma) plane
    ConversionBuffer src_Y;
    /// Output parameters for the conversion results
    ConversionBuffer dst;
};

static ConversionData conversion;

static void SetInputFormat(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.input_format = static_cast<InputFormat>(cmd_buff[1]);
    LOG_DEBUG(Service_Y2R, "called input_format=%u", conversion.params.input_format);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetOutputFormat(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.output_format = static_cast<OutputFormat>(cmd_buff[1]);
    LOG_DEBUG(Service_Y2R, "called output_format=%u", conversion.params.output_format);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetRotation(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.rotation = static_cast<Rotation>(cmd_buff[1]);
    LOG_DEBUG(Service_Y2R, "called rotation=%u", conversion.params.rotation);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetBlockAlignment(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.block_alignment = static_cast<BlockAlignment>(cmd_buff[1]);
    LOG_DEBUG(Service_Y2R, "called alignment=%u", conversion.params.block_alignment);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

/**
 * Y2R_U::GetTransferEndEvent service function
 *  Outputs:
 *      1 : Result of function, 0 on success, otherwise error code
 *      3 : The handle of the completion event
 */
static void GetTransferEndEvent(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    cmd_buff[1] = RESULT_SUCCESS.raw;
    cmd_buff[3] = Kernel::g_handle_table.Create(completion_event).MoveFrom();
    LOG_DEBUG(Service_Y2R, "called");
}

static void SetSendingY(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.src_Y.address = cmd_buff[1];
    conversion.src_Y.image_size = cmd_buff[2];
    conversion.src_Y.transfer_unit = cmd_buff[3];
    conversion.src_Y.stride = cmd_buff[4];
    u32 src_process_handle = cmd_buff[6];
    LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
        "src_process_handle=0x%08X", conversion.src_Y.image_size,
        conversion.src_Y.transfer_unit, conversion.src_Y.stride, src_process_handle);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetReceiving(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.dst.address = cmd_buff[1];
    conversion.dst.image_size = cmd_buff[2];
    conversion.dst.transfer_unit = cmd_buff[3];
    conversion.dst.stride = cmd_buff[4];
    u32 dst_process_handle = cmd_buff[6];
    LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
        "dst_process_handle=0x%08X", conversion.dst.image_size,
        conversion.dst.transfer_unit, conversion.dst.stride,
        dst_process_handle);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetInputLineWidth(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.input_line_width = cmd_buff[1];
    LOG_DEBUG(Service_Y2R, "input_line_width=%u", conversion.params.input_line_width);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void SetInputLines(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    conversion.params.input_lines = cmd_buff[1];
    LOG_DEBUG(Service_Y2R, "input_line_number=%u", conversion.params.input_lines);

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

static void StartConversion(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    const ConversionParameters& params = conversion.params;

    const u8* srcY_buffer = Memory::GetPointer(conversion.src_Y.address);
    u8* dst_buffer = Memory::GetPointer(conversion.dst.address);

    // TODO: support color and other kinds of conversions
    ASSERT(params.input_format == InputFormat::YUV422_Indiv8
        || params.input_format == InputFormat::YUV420_Indiv8);
    ASSERT(params.output_format == OutputFormat::Rgb24);
    ASSERT(params.rotation == Rotation::None);
    const int bpp = 3;

    switch (params.block_alignment) {
    case BlockAlignment::Linear:
    {
        const size_t input_lines = params.input_lines;
        const size_t input_line_width = params.input_line_width;
        const size_t srcY_stride = conversion.src_Y.stride;
        const size_t dst_stride = conversion.dst.stride;

        size_t srcY_offset = 0;
        size_t dst_offset = 0;

        for (size_t line = 0; line < input_lines; ++line) {
            for (size_t i = 0; i < input_line_width; ++i) {
                u8 Y = srcY_buffer[srcY_offset];
                dst_buffer[dst_offset + 0] = Y;
                dst_buffer[dst_offset + 1] = Y;
                dst_buffer[dst_offset + 2] = Y;

                srcY_offset += 1;
                dst_offset += bpp;
            }
            srcY_offset += srcY_stride;
            dst_offset += dst_stride;
        }
        break;
    }
    case BlockAlignment::Block8x8:
    {
        const size_t input_lines = params.input_lines;
        const size_t input_line_width = params.input_line_width;
        const size_t srcY_stride = conversion.src_Y.stride;
        const size_t dst_transfer_unit = conversion.dst.transfer_unit;
        const size_t dst_stride = conversion.dst.stride;

        size_t srcY_offset = 0;
        size_t dst_tile_line_offs = 0;

        const size_t tile_size = 8 * 8 * bpp;

        for (size_t line = 0; line < input_lines;) {
            size_t max_line = line + 8;

            for (; line < max_line; ++line) {
                for (size_t x = 0; x < input_line_width; ++x) {
                    size_t tile_x = x / 8;

                    size_t dst_tile_offs = dst_tile_line_offs + tile_x * tile_size;
                    size_t tile_i = VideoCore::MortonInterleave((u32)x, (u32)line);

                    size_t dst_offset = dst_tile_offs + tile_i * bpp;

                    u8 Y = srcY_buffer[srcY_offset];
                    dst_buffer[dst_offset + 0] = Y;
                    dst_buffer[dst_offset + 1] = Y;
                    dst_buffer[dst_offset + 2] = Y;

                    srcY_offset += 1;
                }

                srcY_offset += srcY_stride;
            }

            dst_tile_line_offs += dst_transfer_unit + dst_stride;
        }
        break;
    }
    }

    // dst_image_size would seem to be perfect for this, but it doesn't include the stride :(
    u32 total_output_size = params.input_lines *
        (conversion.dst.transfer_unit + conversion.dst.stride);
    VideoCore::g_renderer->hw_rasterizer->NotifyFlush(
        Memory::VirtualToPhysicalAddress(conversion.dst.address), total_output_size);

    LOG_DEBUG(Service_Y2R, "called");
    completion_event->Signal();

    cmd_buff[1] = RESULT_SUCCESS.raw;
}

/**
 * Y2R_U::IsBusyConversion service function
 *  Outputs:
 *      1 : Result of function, 0 on success, otherwise error code
 *      2 : 1 if there's a conversion running, otherwise 0.
 */
static void IsBusyConversion(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    cmd_buff[1] = RESULT_SUCCESS.raw;
    cmd_buff[2] = 0; // StartConversion always finishes immediately
    LOG_DEBUG(Service_Y2R, "called");
}

/**
 * Y2R_U::SetConversionParams service function
 */
static void SetConversionParams(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    auto params = reinterpret_cast<const ConversionParameters*>(&cmd_buff[1]);
    conversion.params = *params;

    cmd_buff[0] = 0x00290000; // TODO verify
    cmd_buff[1] = RESULT_SUCCESS.raw;
    LOG_DEBUG(Service_Y2R,
        "called input_format=%hhu output_format=%hhu rotation=%hhu block_alignment=%hhu "
        "input_line_width=%hX input_lines=%hu standard_coefficient=%hhu reserved=%hhu alpha=%hX",
        params->input_format, params->output_format, params->rotation, params->block_alignment,
        params->input_line_width, params->input_lines, params->standard_coefficient);
}

static void PingProcess(Service::Interface* self) {
    u32* cmd_buff = Kernel::GetCommandBuffer();

    cmd_buff[1] = RESULT_SUCCESS.raw;
    cmd_buff[2] = 0;
    LOG_WARNING(Service_Y2R, "(STUBBED) called");
}

const Interface::FunctionInfo FunctionTable[] = {
    {0x00010040, SetInputFormat,          "SetInputFormat"},
    {0x00030040, SetOutputFormat,         "SetOutputFormat"},
    {0x00050040, SetRotation,             "SetRotation"},
    {0x00070040, SetBlockAlignment,       "SetBlockAlignment"},
    {0x000D0040, nullptr,                 "SetTransferEndInterrupt"},
    {0x000F0000, GetTransferEndEvent,     "GetTransferEndEvent"},
    {0x00100102, SetSendingY,             "SetSendingY"},
    {0x00110102, nullptr,                 "SetSendingU"},
    {0x00120102, nullptr,                 "SetSendingV"},
    {0x00180102, SetReceiving,            "SetReceiving"},
    {0x001A0040, SetInputLineWidth,       "SetInputLineWidth"},
    {0x001C0040, SetInputLines,           "SetInputLines"},
    {0x00200040, nullptr,                 "SetStandardCoefficient"},
    {0x00220040, nullptr,                 "SetAlpha"},
    {0x00260000, StartConversion,         "StartConversion"},
    {0x00270000, nullptr,                 "StopConversion"},
    {0x00280000, IsBusyConversion,        "IsBusyConversion"},
    {0x002901C0, SetConversionParams,     "SetConversionParams"},
    {0x002A0000, PingProcess,             "PingProcess"},
    {0x002B0000, nullptr,                 "DriverInitialize"},
    {0x002C0000, nullptr,                 "DriverFinalize"},
};

////////////////////////////////////////////////////////////////////////////////////////////////////
// Interface class

Interface::Interface() {
    completion_event = Kernel::Event::Create(RESETTYPE_ONESHOT, "Y2R:Completed");
    std::memset(&conversion, 0, sizeof(conversion));

    Register(FunctionTable);
}

} // namespace