1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2006 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci/*
28bf215546Sopenharmony_ci * Authors:
29bf215546Sopenharmony_ci *   Keith Whitwell <keithw@vmware.com>
30bf215546Sopenharmony_ci *   Michel Dänzer <daenzer@vmware.com>
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "pipe/p_context.h"
34bf215546Sopenharmony_ci#include "pipe/p_defines.h"
35bf215546Sopenharmony_ci#include "util/u_inlines.h"
36bf215546Sopenharmony_ci#include "util/u_math.h"
37bf215546Sopenharmony_ci#include "util/u_memory.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "i915_context.h"
40bf215546Sopenharmony_ci#include "i915_resource.h"
41bf215546Sopenharmony_ci#include "i915_screen.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_civoid
44bf215546Sopenharmony_cii915_resource_destroy(struct pipe_screen *screen,
45bf215546Sopenharmony_ci                      struct pipe_resource *resource)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   if (resource->target == PIPE_BUFFER) {
48bf215546Sopenharmony_ci      struct i915_buffer *buffer = i915_buffer(resource);
49bf215546Sopenharmony_ci      if (buffer->free_on_destroy)
50bf215546Sopenharmony_ci         align_free(buffer->data);
51bf215546Sopenharmony_ci      FREE(buffer);
52bf215546Sopenharmony_ci   } else {
53bf215546Sopenharmony_ci      struct i915_texture *tex = i915_texture(resource);
54bf215546Sopenharmony_ci      struct i915_winsys *iws = i915_screen(screen)->iws;
55bf215546Sopenharmony_ci      uint32_t i;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci      if (tex->buffer)
58bf215546Sopenharmony_ci         iws->buffer_destroy(iws, tex->buffer);
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci      for (i = 0; i < ARRAY_SIZE(tex->image_offset); i++)
61bf215546Sopenharmony_ci         FREE(tex->image_offset[i]);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci      FREE(tex);
64bf215546Sopenharmony_ci   }
65bf215546Sopenharmony_ci}
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_civoid *
68bf215546Sopenharmony_cii915_buffer_transfer_map(struct pipe_context *pipe,
69bf215546Sopenharmony_ci                         struct pipe_resource *resource, unsigned level,
70bf215546Sopenharmony_ci                         unsigned usage, const struct pipe_box *box,
71bf215546Sopenharmony_ci                         struct pipe_transfer **ptransfer)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci   struct i915_context *i915 = i915_context(pipe);
74bf215546Sopenharmony_ci   struct i915_buffer *buffer = i915_buffer(resource);
75bf215546Sopenharmony_ci   struct pipe_transfer *transfer = slab_alloc_st(&i915->transfer_pool);
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   if (!transfer)
78bf215546Sopenharmony_ci      return NULL;
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   transfer->resource = resource;
81bf215546Sopenharmony_ci   transfer->level = level;
82bf215546Sopenharmony_ci   transfer->usage = usage;
83bf215546Sopenharmony_ci   transfer->box = *box;
84bf215546Sopenharmony_ci   *ptransfer = transfer;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   return buffer->data + transfer->box.x;
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_civoid
90bf215546Sopenharmony_cii915_buffer_transfer_unmap(struct pipe_context *pipe,
91bf215546Sopenharmony_ci                           struct pipe_transfer *transfer)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   struct i915_context *i915 = i915_context(pipe);
94bf215546Sopenharmony_ci   slab_free_st(&i915->transfer_pool, transfer);
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_cii915_buffer_subdata(struct pipe_context *rm_ctx, struct pipe_resource *resource,
99bf215546Sopenharmony_ci                    unsigned usage, unsigned offset, unsigned size,
100bf215546Sopenharmony_ci                    const void *data)
101bf215546Sopenharmony_ci{
102bf215546Sopenharmony_ci   struct i915_buffer *buffer = i915_buffer(resource);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   memcpy(buffer->data + offset, data, size);
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistruct pipe_resource *
108bf215546Sopenharmony_cii915_buffer_create(struct pipe_screen *screen,
109bf215546Sopenharmony_ci                   const struct pipe_resource *template)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   if (!buf)
114bf215546Sopenharmony_ci      return NULL;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   buf->b = *template;
117bf215546Sopenharmony_ci   pipe_reference_init(&buf->b.reference, 1);
118bf215546Sopenharmony_ci   buf->b.screen = screen;
119bf215546Sopenharmony_ci   buf->data = align_malloc(template->width0, 64);
120bf215546Sopenharmony_ci   buf->free_on_destroy = true;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   if (!buf->data)
123bf215546Sopenharmony_ci      goto err;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   return &buf->b;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cierr:
128bf215546Sopenharmony_ci   FREE(buf);
129bf215546Sopenharmony_ci   return NULL;
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_cistruct pipe_resource *
133bf215546Sopenharmony_cii915_user_buffer_create(struct pipe_screen *screen, void *ptr, unsigned bytes,
134bf215546Sopenharmony_ci                        unsigned bind)
135bf215546Sopenharmony_ci{
136bf215546Sopenharmony_ci   struct i915_buffer *buf = CALLOC_STRUCT(i915_buffer);
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   if (!buf)
139bf215546Sopenharmony_ci      return NULL;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   pipe_reference_init(&buf->b.reference, 1);
142bf215546Sopenharmony_ci   buf->b.screen = screen;
143bf215546Sopenharmony_ci   buf->b.format = PIPE_FORMAT_R8_UNORM; /* ?? */
144bf215546Sopenharmony_ci   buf->b.usage = PIPE_USAGE_IMMUTABLE;
145bf215546Sopenharmony_ci   buf->b.bind = bind;
146bf215546Sopenharmony_ci   buf->b.flags = 0;
147bf215546Sopenharmony_ci   buf->b.width0 = bytes;
148bf215546Sopenharmony_ci   buf->b.height0 = 1;
149bf215546Sopenharmony_ci   buf->b.depth0 = 1;
150bf215546Sopenharmony_ci   buf->b.array_size = 1;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   buf->data = ptr;
153bf215546Sopenharmony_ci   buf->free_on_destroy = false;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   return &buf->b;
156bf215546Sopenharmony_ci}
157