target/haiku-softpipe: Fix viewport issues
[mesa.git] / src / gallium / targets / haiku-softpipe / GalliumFramebuffer.cpp
1 /*
2 * Copyright 2012-2013, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Artur Wyszynski, harakash@gmail.com
7 * Alexander von Gluck IV, kallisti5@unixzen.com
8 */
9
10
11 #include "GalliumFramebuffer.h"
12
13 extern "C" {
14 #include "main/context.h"
15 #include "main/framebuffer.h"
16 #include "main/renderbuffer.h"
17 #include "pipe/p_format.h"
18 #include "state_tracker/st_manager.h"
19 }
20
21 #include "GalliumContext.h"
22
23
24 #ifdef DEBUG
25 # define TRACE(x...) printf("GalliumFramebuffer: " x)
26 # define CALLED() TRACE("CALLED: %s\n", __PRETTY_FUNCTION__)
27 #else
28 # define TRACE(x...)
29 # define CALLED()
30 #endif
31 #define ERROR(x...) printf("GalliumFramebuffer: " x)
32
33
34 static boolean
35 hgl_framebuffer_flush_front(struct st_context_iface *stctx,
36 struct st_framebuffer_iface* stfb, enum st_attachment_type statt)
37 {
38 CALLED();
39
40 hgl_context* context = (hgl_context*)stfb->st_manager_private;
41
42 if (!context) {
43 ERROR("%s: Couldn't obtain valid hgl_context!\n", __func__);
44 return FALSE;
45 }
46
47 #if 0
48 struct stw_st_framebuffer *stwfb = stw_st_framebuffer(stfb);
49 pipe_mutex_lock(stwfb->fb->mutex);
50
51 struct pipe_resource* resource = textures[statt];
52 if (resource)
53 stw_framebuffer_present_locked(...);
54 #endif
55
56 return TRUE;
57 }
58
59
60 static boolean
61 hgl_framebuffer_validate(struct st_context_iface* stctx,
62 struct st_framebuffer_iface* stfb,
63 const enum st_attachment_type* statts, unsigned count,
64 struct pipe_resource** out)
65 {
66 CALLED();
67
68 if (!stfb) {
69 ERROR("%s: Invalid st framebuffer interface!\n", __func__);
70 return FALSE;
71 }
72
73 hgl_context* context = (hgl_context*)stfb->st_manager_private;
74
75 if (!context) {
76 ERROR("%s: Couldn't obtain valid hgl_context!\n", __func__);
77 return FALSE;
78 }
79
80 int32 width = 0;
81 int32 height = 0;
82 get_bitmap_size(context->bitmap, &width, &height);
83
84 struct pipe_resource templat;
85 memset(&templat, 0, sizeof(templat));
86 templat.target = PIPE_TEXTURE_RECT;
87 templat.width0 = width;
88 templat.height0 = height;
89 templat.depth0 = 1;
90 templat.array_size = 1;
91 templat.usage = PIPE_USAGE_DEFAULT;
92
93 if (context->stVisual && context->manager && context->manager->screen) {
94 TRACE("%s: Updating resources\n", __func__);
95 int i;
96 for (i = 0; i < count; i++) {
97 enum pipe_format format = PIPE_FORMAT_NONE;
98 unsigned bind = 0;
99
100 switch(statts[i]) {
101 case ST_ATTACHMENT_FRONT_LEFT:
102 format = context->stVisual->color_format;
103 bind = PIPE_BIND_RENDER_TARGET;
104 break;
105 case ST_ATTACHMENT_DEPTH_STENCIL:
106 format = context->stVisual->depth_stencil_format;
107 bind = PIPE_BIND_DEPTH_STENCIL;
108 break;
109 case ST_ATTACHMENT_ACCUM:
110 format = context->stVisual->accum_format;
111 bind = PIPE_BIND_RENDER_TARGET;
112 break;
113 default:
114 ERROR("%s: Unexpected attachment type!\n", __func__);
115 }
116 templat.format = format;
117 templat.bind = bind;
118
119 struct pipe_screen* screen = context->manager->screen;
120 context->textures[i] = screen->resource_create(screen, &templat);
121 out[i] = context->textures[i];
122 }
123 }
124
125 return TRUE;
126 }
127
128
129 GalliumFramebuffer::GalliumFramebuffer(struct st_visual* visual,
130 void* privateContext)
131 :
132 fBuffer(NULL)
133 {
134 CALLED();
135 fBuffer = CALLOC_STRUCT(st_framebuffer_iface);
136 if (!fBuffer) {
137 ERROR("%s: Couldn't calloc framebuffer!\n", __func__);
138 return;
139 }
140 fBuffer->visual = visual;
141 fBuffer->flush_front = hgl_framebuffer_flush_front;
142 fBuffer->validate = hgl_framebuffer_validate;
143 fBuffer->st_manager_private = privateContext;
144
145 pipe_mutex_init(fMutex);
146 }
147
148
149 GalliumFramebuffer::~GalliumFramebuffer()
150 {
151 CALLED();
152 // We lock and unlock to try and make sure we wait for anything
153 // using the framebuffer to finish
154 Lock();
155 if (!fBuffer) {
156 ERROR("%s: Strange, no Gallium Framebuffer to free?\n", __func__);
157 return;
158 }
159 FREE(fBuffer);
160 Unlock();
161
162 pipe_mutex_destroy(fMutex);
163 }
164
165
166 status_t
167 GalliumFramebuffer::Lock()
168 {
169 CALLED();
170 pipe_mutex_lock(fMutex);
171 return B_OK;
172 }
173
174
175 status_t
176 GalliumFramebuffer::Unlock()
177 {
178 CALLED();
179 pipe_mutex_unlock(fMutex);
180 return B_OK;
181 }