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