1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2012 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/u_debug.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "svga_resource.h"
29bf215546Sopenharmony_ci#include "svga_resource_buffer.h"
30bf215546Sopenharmony_ci#include "svga_resource_texture.h"
31bf215546Sopenharmony_ci#include "svga_context.h"
32bf215546Sopenharmony_ci#include "svga_screen.h"
33bf215546Sopenharmony_ci#include "svga_format.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci/**
37bf215546Sopenharmony_ci * This is the primary driver entrypoint for allocating graphics memory
38bf215546Sopenharmony_ci * (vertex/index/constant buffers, textures, etc)
39bf215546Sopenharmony_ci */
40bf215546Sopenharmony_cistatic struct pipe_resource *
41bf215546Sopenharmony_cisvga_resource_create(struct pipe_screen *screen,
42bf215546Sopenharmony_ci                     const struct pipe_resource *template)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   struct pipe_resource *r;
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci   if (template->target == PIPE_BUFFER)
47bf215546Sopenharmony_ci      r = svga_buffer_create(screen, template);
48bf215546Sopenharmony_ci   else
49bf215546Sopenharmony_ci      r = svga_texture_create(screen, template);
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   if (!r) {
52bf215546Sopenharmony_ci      struct svga_screen *svgascreen = svga_screen(screen);
53bf215546Sopenharmony_ci      svgascreen->hud.num_failed_allocations++;
54bf215546Sopenharmony_ci   }
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   return r;
57bf215546Sopenharmony_ci}
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistatic struct pipe_resource *
61bf215546Sopenharmony_cisvga_resource_from_handle(struct pipe_screen * screen,
62bf215546Sopenharmony_ci                          const struct pipe_resource *template,
63bf215546Sopenharmony_ci                          struct winsys_handle *whandle,
64bf215546Sopenharmony_ci                          unsigned usage)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   if (template->target == PIPE_BUFFER)
67bf215546Sopenharmony_ci      return NULL;
68bf215546Sopenharmony_ci   else
69bf215546Sopenharmony_ci      return svga_texture_from_handle(screen, template, whandle);
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci/**
74bf215546Sopenharmony_ci * Check if a resource (texture, buffer) of the given size
75bf215546Sopenharmony_ci * and format can be created.
76bf215546Sopenharmony_ci * \Return TRUE if OK, FALSE if too large.
77bf215546Sopenharmony_ci */
78bf215546Sopenharmony_cistatic bool
79bf215546Sopenharmony_cisvga_can_create_resource(struct pipe_screen *screen,
80bf215546Sopenharmony_ci                         const struct pipe_resource *res)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   struct svga_screen *svgascreen = svga_screen(screen);
83bf215546Sopenharmony_ci   struct svga_winsys_screen *sws = svgascreen->sws;
84bf215546Sopenharmony_ci   SVGA3dSurfaceFormat format;
85bf215546Sopenharmony_ci   SVGA3dSize base_level_size;
86bf215546Sopenharmony_ci   uint32 numMipLevels;
87bf215546Sopenharmony_ci   uint32 arraySize;
88bf215546Sopenharmony_ci   uint32 numSamples;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   if (res->target == PIPE_BUFFER) {
91bf215546Sopenharmony_ci      format = SVGA3D_BUFFER;
92bf215546Sopenharmony_ci      base_level_size.width = res->width0;
93bf215546Sopenharmony_ci      base_level_size.height = 1;
94bf215546Sopenharmony_ci      base_level_size.depth = 1;
95bf215546Sopenharmony_ci      numMipLevels = 1;
96bf215546Sopenharmony_ci      arraySize = 1;
97bf215546Sopenharmony_ci      numSamples = 0;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   } else {
100bf215546Sopenharmony_ci      if (res->target == PIPE_TEXTURE_CUBE)
101bf215546Sopenharmony_ci         assert(res->array_size == 6);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci      format = svga_translate_format(svgascreen, res->format, res->bind);
104bf215546Sopenharmony_ci      if (format == SVGA3D_FORMAT_INVALID)
105bf215546Sopenharmony_ci         return false;
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci      base_level_size.width = res->width0;
108bf215546Sopenharmony_ci      base_level_size.height = res->height0;
109bf215546Sopenharmony_ci      base_level_size.depth = res->depth0;
110bf215546Sopenharmony_ci      numMipLevels = res->last_level + 1;
111bf215546Sopenharmony_ci      arraySize = res->array_size;
112bf215546Sopenharmony_ci      numSamples = res->nr_samples;
113bf215546Sopenharmony_ci   }
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci   return sws->surface_can_create(sws, format, base_level_size,
116bf215546Sopenharmony_ci                                  arraySize, numMipLevels, numSamples);
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_civoid
121bf215546Sopenharmony_cisvga_init_resource_functions(struct svga_context *svga)
122bf215546Sopenharmony_ci{
123bf215546Sopenharmony_ci   svga->pipe.buffer_map = svga_buffer_transfer_map;
124bf215546Sopenharmony_ci   svga->pipe.texture_map = svga_texture_transfer_map;
125bf215546Sopenharmony_ci   svga->pipe.transfer_flush_region = svga_buffer_transfer_flush_region;
126bf215546Sopenharmony_ci   svga->pipe.buffer_unmap = svga_buffer_transfer_unmap;
127bf215546Sopenharmony_ci   svga->pipe.texture_unmap = svga_texture_transfer_unmap;
128bf215546Sopenharmony_ci   svga->pipe.buffer_subdata = u_default_buffer_subdata;
129bf215546Sopenharmony_ci   svga->pipe.texture_subdata = u_default_texture_subdata;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   if (svga_have_vgpu10(svga)) {
132bf215546Sopenharmony_ci      svga->pipe.generate_mipmap = svga_texture_generate_mipmap;
133bf215546Sopenharmony_ci   } else {
134bf215546Sopenharmony_ci      svga->pipe.generate_mipmap = NULL;
135bf215546Sopenharmony_ci   }
136bf215546Sopenharmony_ci}
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_civoid
139bf215546Sopenharmony_cisvga_init_screen_resource_functions(struct svga_screen *is)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci   is->screen.resource_create = svga_resource_create;
142bf215546Sopenharmony_ci   is->screen.resource_from_handle = svga_resource_from_handle;
143bf215546Sopenharmony_ci   is->screen.resource_get_handle = svga_resource_get_handle;
144bf215546Sopenharmony_ci   is->screen.resource_destroy = svga_resource_destroy;
145bf215546Sopenharmony_ci   is->screen.can_create_resource = svga_can_create_resource;
146bf215546Sopenharmony_ci}
147