1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2009 Kristian Høgsberg
4 * Copyright © 2009 Chris Wilson
5 * Copyright © 2009 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is Kristian Høgsberg.
34 * Based on the xf86-intel-driver i965 render acceleration code,
36 * Wang Zhenyu <zhenyu.z.wang@intel.com>
37 * Eric Anholt <eric@anholt.net>
38 * Carl Worth <cworth@redhat.com>
39 * Keith Packard <keithp@keithp.com>
44 * FIXME: Use brw_PLN for [DevCTG-B+]
50 #include "cairo-drm-private.h"
51 #include "cairo-drm-ioctl-private.h"
52 #include "cairo-drm-intel-private.h"
53 #include "cairo-drm-intel-command-private.h"
54 #include "cairo-drm-intel-ioctl-private.h"
55 #include "cairo-drm-i965-private.h"
57 #include "cairo-boxes-private.h"
58 #include "cairo-composite-rectangles-private.h"
59 #include "cairo-default-context-private.h"
60 #include "cairo-error-private.h"
61 #include "cairo-region-private.h"
62 #include "cairo-surface-offset-private.h"
64 #include <sys/ioctl.h>
67 #define I965_MAX_SIZE 8192
69 static const cairo_surface_backend_t i965_surface_backend;
72 i965_stream_init (i965_stream_t *stream,
73 uint8_t *data, uint32_t size,
74 struct i965_pending_relocation *pending, int max_pending,
75 struct drm_i915_gem_relocation_entry *relocations, int max_relocations)
78 stream->used = stream->committed = 0;
83 stream->num_pending_relocations = 0;
84 stream->max_pending_relocations = max_pending;
85 stream->pending_relocations = pending;
87 stream->num_relocations = 0;
88 stream->max_relocations = max_relocations;
89 stream->relocations = relocations;
93 i965_add_relocation (i965_device_t *device,
95 uint32_t read_domains,
96 uint32_t write_domain)
98 if (bo->exec == NULL) {
101 device->exec.gtt_size += bo->base.size;
103 i = device->exec.count++;
104 assert (i < ARRAY_LENGTH (device->exec.exec));
106 device->exec.exec[i].handle = bo->base.handle;
107 device->exec.exec[i].relocation_count = 0;
108 device->exec.exec[i].relocs_ptr = 0;
109 device->exec.exec[i].alignment = 0;
110 device->exec.exec[i].offset = 0;
111 device->exec.exec[i].flags = 0;
112 device->exec.exec[i].rsvd1 = 0;
113 device->exec.exec[i].rsvd2 = 0;
115 device->exec.bo[i] = intel_bo_reference (bo);
116 bo->exec = &device->exec.exec[i];
119 if (cairo_list_is_empty (&bo->link))
120 cairo_list_add_tail (&device->flush, &bo->link);
122 assert (write_domain == 0 || bo->batch_write_domain == 0 || bo->batch_write_domain == write_domain);
123 bo->batch_read_domains |= read_domains;
124 bo->batch_write_domain |= write_domain;
128 i965_emit_relocation (i965_device_t *device,
129 i965_stream_t *stream,
131 uint32_t target_offset,
132 uint32_t read_domains,
133 uint32_t write_domain,
138 assert (target_offset < target->base.size);
140 i965_add_relocation (device, target, read_domains, write_domain);
142 n = stream->num_relocations++;
143 assert (n < stream->max_relocations);
145 stream->relocations[n].offset = offset;
146 stream->relocations[n].delta = target_offset;
147 stream->relocations[n].target_handle = target->base.handle;
148 stream->relocations[n].read_domains = read_domains;
149 stream->relocations[n].write_domain = write_domain;
150 stream->relocations[n].presumed_offset = target->offset;
154 i965_stream_reset (i965_stream_t *stream)
156 stream->used = stream->committed = 0;
157 stream->num_relocations = 0;
158 stream->num_pending_relocations = 0;
159 if (++stream->serial == 0)
164 i965_stream_commit (i965_device_t *device,
165 i965_stream_t *stream)
170 assert (stream->used);
172 bo = intel_bo_create (&device->intel,
173 stream->used, stream->used,
174 FALSE, I915_TILING_NONE, 0);
176 /* apply pending relocations */
177 for (n = 0; n < stream->num_pending_relocations; n++) {
178 struct i965_pending_relocation *p = &stream->pending_relocations[n];
180 i965_emit_relocation (device, &device->batch, bo,
186 *(uint32_t *) (device->batch.data + p->offset) = bo->offset + p->delta;
189 intel_bo_write (&device->intel, bo, 0, stream->used, stream->data);
191 if (stream->num_relocations) {
192 assert (bo->exec != NULL);
193 bo->exec->relocs_ptr = (uintptr_t) stream->relocations;
194 bo->exec->relocation_count = stream->num_relocations;
197 intel_bo_destroy (&device->intel, bo);
199 i965_stream_reset (stream);
203 sf_states_pluck (void *entry, void *closure)
205 i965_device_t *device = closure;
207 _cairo_hash_table_remove (device->sf_states, entry);
208 _cairo_freelist_free (&device->sf_freelist, entry);
212 cc_offsets_pluck (void *entry, void *closure)
214 i965_device_t *device = closure;
216 _cairo_hash_table_remove (device->cc_states, entry);
217 _cairo_freelist_free (&device->cc_freelist, entry);
221 wm_kernels_pluck (void *entry, void *closure)
223 i965_device_t *device = closure;
225 _cairo_hash_table_remove (device->wm_kernels, entry);
226 _cairo_freelist_free (&device->wm_kernel_freelist, entry);
230 wm_states_pluck (void *entry, void *closure)
232 i965_device_t *device = closure;
234 _cairo_hash_table_remove (device->wm_states, entry);
235 _cairo_freelist_free (&device->wm_state_freelist, entry);
239 wm_bindings_pluck (void *entry, void *closure)
241 i965_device_t *device = closure;
243 _cairo_hash_table_remove (device->wm_bindings, entry);
244 _cairo_freelist_free (&device->wm_binding_freelist, entry);
248 samplers_pluck (void *entry, void *closure)
250 i965_device_t *device = closure;
252 _cairo_hash_table_remove (device->samplers, entry);
253 _cairo_freelist_free (&device->sampler_freelist, entry);
257 i965_general_state_reset (i965_device_t *device)
259 _cairo_hash_table_foreach (device->sf_states,
263 _cairo_hash_table_foreach (device->cc_states,
267 _cairo_hash_table_foreach (device->wm_kernels,
271 _cairo_hash_table_foreach (device->wm_states,
275 _cairo_hash_table_foreach (device->wm_bindings,
279 _cairo_hash_table_foreach (device->samplers,
283 device->vs_offset = (uint32_t) -1;
284 device->border_color_offset = (uint32_t) -1;
286 if (device->general_state != NULL) {
287 intel_bo_destroy (&device->intel, device->general_state);
288 device->general_state = NULL;
293 i965_device_reset (i965_device_t *device)
295 device->exec.count = 0;
296 device->exec.gtt_size = I965_VERTEX_SIZE +
301 device->sf_state.entry.hash = (uint32_t) -1;
302 device->wm_state.entry.hash = (uint32_t) -1;
303 device->wm_binding.entry.hash = (uint32_t) -1;
304 device->cc_state.entry.hash = (uint32_t) -1;
306 device->target = NULL;
307 device->source = NULL;
311 device->draw_rectangle = (uint32_t) -1;
313 device->vertex_type = (uint32_t) -1;
314 device->vertex_size = 0;
315 device->rectangle_size = 0;
316 device->last_vertex_size = 0;
318 device->constants = NULL;
319 device->constants_size = 0;
321 device->have_urb_fences = FALSE;
324 static cairo_status_t
325 i965_exec (i965_device_t *device, uint32_t offset)
327 struct drm_i915_gem_execbuffer2 execbuf;
328 cairo_status_t status = CAIRO_STATUS_SUCCESS;
331 execbuf.buffers_ptr = (uintptr_t) device->exec.exec;
332 execbuf.buffer_count = device->exec.count;
333 execbuf.batch_start_offset = offset;
334 execbuf.batch_len = device->batch.used;
337 execbuf.num_cliprects = 0;
338 execbuf.cliprects_ptr = 0;
339 execbuf.flags = I915_GEM_3D_PIPELINE;
344 printf ("exec: offset=%d, length=%d, buffers=%d\n",
345 offset, device->batch.used, device->exec.count);
346 intel_dump_batchbuffer ((uint32_t *) device->batch.data,
348 device->intel.base.chip_id);
353 ret = ioctl (device->intel.base.fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
354 } while (ret != 0 && errno == EINTR);
355 if (unlikely (ret)) {
357 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
359 status = _cairo_error (CAIRO_STATUS_DEVICE_ERROR);
361 fprintf (stderr, "Batch submission failed: %d\n", errno);
362 fprintf (stderr, " gtt size: %zd/%zd\n",
363 device->exec.gtt_size, device->intel.gtt_avail_size);
365 fprintf (stderr, " %d buffers:\n",
367 for (i = 0; i < device->exec.count; i++) {
368 fprintf (stderr, " exec[%d] = %d\n",
369 i, device->exec.bo[i]->base.size);
372 intel_dump_batchbuffer ((uint32_t *) device->batch.data,
374 device->intel.base.chip_id);
377 /* XXX any write target within the batch should now be in error */
378 for (i = 0; i < device->exec.count; i++) {
379 intel_bo_t *bo = device->exec.bo[i];
382 bo->offset = device->exec.exec[i].offset;
384 bo->batch_read_domains = 0;
385 bo->batch_write_domain = 0;
392 ret = intel_bo_madvise (&device->intel, bo, I915_MADV_DONTNEED);
393 /* ignore immediate notification of purging */
395 cairo_list_del (&bo->cache_list);
396 cairo_list_init (&bo->link);
397 intel_bo_destroy (&device->intel, bo);
399 cairo_list_init (&device->flush);
401 device->exec.count = 0;
406 static inline uint32_t
407 next_bo_size (uint32_t v)
409 v = (v + 8191) / 8192;
423 _copy_to_bo_and_apply_relocations (i965_device_t *device,
425 i965_stream_t *stream,
430 intel_bo_write (&device->intel, bo,
431 offset, stream->used,
434 for (n = 0; n < stream->num_pending_relocations; n++) {
435 struct i965_pending_relocation *p = &stream->pending_relocations[n];
437 i965_emit_relocation (device, &device->batch, bo,
444 *(uint32_t *) (device->batch.data + p->offset) =
445 bo->offset + p->delta + offset;
451 i965_device_flush (i965_device_t *device)
453 cairo_status_t status;
454 uint32_t aligned, max;
458 if (device->batch.used == 0)
459 return CAIRO_STATUS_SUCCESS;
461 i965_flush_vertices (device);
463 OUT_BATCH (MI_BATCH_BUFFER_END);
464 /* Emit a padding dword if we aren't going to be quad-word aligned. */
465 if (device->batch.used & 4)
469 printf ("device flush: vertex=%d, constant=%d, surface=%d, general=%d, batch=%d\n",
471 device->constant.used,
472 device->surface.used,
473 device->general.used,
477 /* can we pack the surface state into the tail of the general state? */
478 if (device->general.used == device->general.committed) {
479 if (device->general.used) {
480 assert (device->general.num_pending_relocations == 1);
481 assert (device->general_state != NULL);
482 i965_emit_relocation (device, &device->batch,
483 device->general_state,
484 device->general.pending_relocations[0].delta,
485 device->general.pending_relocations[0].read_domains,
486 device->general.pending_relocations[0].write_domain,
487 device->general.pending_relocations[0].offset);
489 if (device->general_state->offset) {
490 *(uint32_t *) (device->batch.data +
491 device->general.pending_relocations[0].offset) =
492 device->general_state->offset +
493 device->general.pending_relocations[0].delta;
497 assert (device->general.num_pending_relocations == 1);
498 if (device->general_state != NULL) {
499 intel_bo_destroy (&device->intel, device->general_state);
500 device->general_state = NULL;
503 bo = intel_bo_create (&device->intel,
504 device->general.used,
505 device->general.used,
506 FALSE, I915_TILING_NONE, 0);
507 if (unlikely (bo == NULL))
508 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
510 aligned = (device->general.used + 31) & -32;
511 if (device->surface.used &&
512 aligned + device->surface.used <= bo->base.size)
514 _copy_to_bo_and_apply_relocations (device, bo, &device->general, 0);
515 _copy_to_bo_and_apply_relocations (device, bo, &device->surface, aligned);
517 if (device->surface.num_relocations) {
518 for (n = 0; n < device->surface.num_relocations; n++)
519 device->surface.relocations[n].offset += aligned;
521 assert (bo->exec != NULL);
522 bo->exec->relocs_ptr = (uintptr_t) device->surface.relocations;
523 bo->exec->relocation_count = device->surface.num_relocations;
526 i965_stream_reset (&device->surface);
530 _copy_to_bo_and_apply_relocations (device, bo, &device->general, 0);
533 /* Note we don't reset the general state, just mark what data we've committed. */
534 device->general.committed = device->general.used;
535 device->general_state = bo;
537 device->general.num_pending_relocations = 0;
539 /* Combine vertex+constant+surface+batch streams? */
540 max = aligned = device->vertex.used;
541 if (device->surface.used) {
542 aligned = (aligned + 63) & -64;
543 aligned += device->surface.used;
544 if (device->surface.used > max)
545 max = device->surface.used;
547 aligned = (aligned + 63) & -64;
548 aligned += device->batch.used;
549 if (device->batch.used > max)
550 max = device->batch.used;
551 if (aligned <= next_bo_size (max)) {
552 int batch_num_relocations;
557 bo = intel_bo_create (&device->intel,
559 FALSE, I915_TILING_NONE, 0);
560 if (unlikely (bo == NULL))
561 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
563 assert (aligned <= bo->base.size);
565 if (device->vertex.used)
566 _copy_to_bo_and_apply_relocations (device, bo, &device->vertex, 0);
568 aligned = device->vertex.used;
570 batch_num_relocations = device->batch.num_relocations;
571 if (device->surface.used) {
572 aligned = (aligned + 63) & -64;
573 _copy_to_bo_and_apply_relocations (device, bo, &device->surface, aligned);
575 batch_num_relocations = device->batch.num_relocations;
576 if (device->surface.num_relocations) {
577 assert (device->batch.num_relocations + device->surface.num_relocations < device->batch.max_relocations);
579 memcpy (device->batch.relocations + device->batch.num_relocations,
580 device->surface.relocations,
581 sizeof (device->surface.relocations[0]) * device->surface.num_relocations);
583 for (n = 0; n < device->surface.num_relocations; n++)
584 device->batch.relocations[device->batch.num_relocations + n].offset += aligned;
586 device->batch.num_relocations += device->surface.num_relocations;
589 aligned += device->surface.used;
592 aligned = (aligned + 63) & -64;
593 intel_bo_write (&device->intel, bo,
594 aligned, device->batch.used,
597 for (n = 0; n < batch_num_relocations; n++)
598 device->batch.relocations[n].offset += aligned;
600 if (device->exec.bo[device->exec.count-1] == bo) {
601 assert (bo->exec == &device->exec.exec[device->exec.count-1]);
603 bo->exec->relocation_count = device->batch.num_relocations;
604 bo->exec->relocs_ptr = (uintptr_t) device->batch.relocations;
605 intel_bo_destroy (&device->intel, bo);
607 assert (bo->exec == NULL);
609 n = device->exec.count++;
610 device->exec.exec[n].handle = bo->base.handle;
611 device->exec.exec[n].relocation_count = device->batch.num_relocations;
612 device->exec.exec[n].relocs_ptr = (uintptr_t) device->batch.relocations;
613 device->exec.exec[n].alignment = 0;
614 device->exec.exec[n].offset = 0;
615 device->exec.exec[n].flags = 0;
616 device->exec.exec[n].rsvd1 = 0;
617 device->exec.exec[n].rsvd2 = 0;
619 /* transfer ownership to the exec */
620 device->exec.bo[n] = bo;
623 i965_stream_commit (device, &device->vertex);
624 if (device->surface.used)
625 i965_stream_commit (device, &device->surface);
627 bo = intel_bo_create (&device->intel,
628 device->batch.used, device->batch.used,
629 FALSE, I915_TILING_NONE, 0);
630 if (unlikely (bo == NULL))
631 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
633 intel_bo_write (&device->intel, bo,
634 0, device->batch.used,
637 n = device->exec.count++;
638 device->exec.exec[n].handle = bo->base.handle;
639 device->exec.exec[n].relocation_count = device->batch.num_relocations;
640 device->exec.exec[n].relocs_ptr = (uintptr_t) device->batch.relocations;
641 device->exec.exec[n].alignment = 0;
642 device->exec.exec[n].offset = 0;
643 device->exec.exec[n].flags = 0;
644 device->exec.exec[n].rsvd1 = 0;
645 device->exec.exec[n].rsvd2 = 0;
647 /* transfer ownership to the exec */
648 device->exec.bo[n] = bo;
652 status = i965_exec (device, aligned);
654 i965_stream_reset (&device->vertex);
655 i965_stream_reset (&device->surface);
656 i965_stream_reset (&device->batch);
658 intel_glyph_cache_unpin (&device->intel);
659 intel_snapshot_cache_thaw (&device->intel);
661 i965_device_reset (device);
666 static cairo_surface_t *
667 i965_surface_create_similar (void *abstract_other,
668 cairo_content_t content,
669 int width, int height)
671 i965_surface_t *other;
672 cairo_format_t format;
674 if (width > 8192 || height > 8192)
677 other = abstract_other;
678 if (content == other->intel.drm.base.content)
679 format = other->intel.drm.format;
681 format = _cairo_format_from_content (content);
683 return i965_surface_create_internal ((cairo_drm_device_t *) other->intel.drm.base.device,
686 I965_TILING_DEFAULT, TRUE);
689 static cairo_status_t
690 i965_surface_finish (void *abstract_surface)
692 i965_surface_t *surface = abstract_surface;
694 return intel_surface_finish (&surface->intel);
697 static cairo_status_t
698 i965_surface_flush (void *abstract_surface)
700 i965_surface_t *surface = abstract_surface;
701 cairo_status_t status = CAIRO_STATUS_SUCCESS;
703 if (surface->intel.drm.fallback != NULL)
704 return intel_surface_flush (abstract_surface);
706 /* Forgo flushing on finish as the user cannot access the surface directly. */
707 if (! surface->intel.drm.base.finished &&
708 to_intel_bo (surface->intel.drm.bo)->exec != NULL)
710 status = cairo_device_acquire (surface->intel.drm.base.device);
711 if (likely (status == CAIRO_STATUS_SUCCESS)) {
712 i965_device_t *device;
714 device = i965_device (surface);
715 status = i965_device_flush (device);
716 cairo_device_release (&device->intel.base.base);
725 static cairo_status_t
726 _composite_boxes_spans (void *closure,
727 cairo_span_renderer_t *renderer,
728 const cairo_rectangle_int_t *extents)
730 cairo_boxes_t *boxes = closure;
731 cairo_rectangular_scan_converter_t converter;
732 struct _cairo_boxes_chunk *chunk;
733 cairo_status_t status;
735 _cairo_rectangular_scan_converter_init (&converter, extents);
736 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
737 cairo_box_t *box = chunk->base;
740 for (i = 0; i < chunk->count; i++) {
741 status = _cairo_rectangular_scan_converter_add_box (&converter, &box[i], 1);
742 if (unlikely (status))
747 status = converter.base.generate (&converter.base, renderer);
750 converter.base.destroy (&converter.base);
755 i965_fixup_unbounded (i965_surface_t *dst,
756 const cairo_composite_rectangles_t *extents,
759 i965_shader_t shader;
760 i965_device_t *device;
761 cairo_status_t status;
763 i965_shader_init (&shader, dst, CAIRO_OPERATOR_CLEAR);
766 cairo_region_t *clip_region = NULL;
768 status = _cairo_clip_get_region (clip, &clip_region);
769 assert (status == CAIRO_STATUS_SUCCESS || CAIRO_INT_STATUS_UNSUPPORTED);
770 assert (clip_region == NULL);
772 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
773 i965_shader_set_clip (&shader, clip);
775 if (extents->bounded.width == extents->unbounded.width &&
776 extents->bounded.height == extents->unbounded.height)
778 return CAIRO_STATUS_SUCCESS;
782 status = i965_shader_acquire_pattern (&shader,
784 &_cairo_pattern_clear.base,
785 &extents->unbounded);
786 if (unlikely (status)) {
787 i965_shader_fini (&shader);
791 device = i965_device (dst);
792 status = cairo_device_acquire (&device->intel.base.base);
793 if (unlikely (status))
796 status = i965_shader_commit (&shader, device);
797 if (unlikely (status)) {
801 if (extents->bounded.width == 0 || extents->bounded.height == 0) {
802 i965_shader_add_rectangle (&shader,
803 extents->unbounded.x,
804 extents->unbounded.y,
805 extents->unbounded.width,
806 extents->unbounded.height);
808 if (extents->bounded.y != extents->unbounded.y) {
809 cairo_rectangle_int_t rect;
811 rect.x = extents->unbounded.x;
812 rect.y = extents->unbounded.y;
813 rect.width = extents->unbounded.width;
814 rect.height = extents->bounded.y - rect.y;
816 i965_shader_add_rectangle (&shader,
818 rect.width, rect.height);
822 if (extents->bounded.x != extents->unbounded.x) {
823 cairo_rectangle_int_t rect;
825 rect.x = extents->unbounded.x;
826 rect.y = extents->bounded.y;
827 rect.width = extents->bounded.x - extents->unbounded.x;
828 rect.height = extents->bounded.height;
830 i965_shader_add_rectangle (&shader,
832 rect.width, rect.height);
836 if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
837 cairo_rectangle_int_t rect;
839 rect.x = extents->bounded.x + extents->bounded.width;
840 rect.y = extents->bounded.y;
841 rect.width = extents->unbounded.x + extents->unbounded.width - rect.x;
842 rect.height = extents->bounded.height;
844 i965_shader_add_rectangle (&shader,
846 rect.width, rect.height);
850 if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
851 cairo_rectangle_int_t rect;
853 rect.x = extents->unbounded.x;
854 rect.y = extents->bounded.y + extents->bounded.height;
855 rect.width = extents->unbounded.width;
856 rect.height = extents->unbounded.y + extents->unbounded.height - rect.y;
858 i965_shader_add_rectangle (&shader,
860 rect.width, rect.height);
864 i965_shader_fini (&shader);
866 cairo_device_release (&device->intel.base.base);
870 static cairo_status_t
871 i965_fixup_unbounded_boxes (i965_surface_t *dst,
872 const cairo_composite_rectangles_t *extents,
874 cairo_boxes_t *boxes)
878 cairo_region_t *clip_region = NULL;
879 cairo_status_t status;
880 struct _cairo_boxes_chunk *chunk;
881 i965_shader_t shader;
884 if (boxes->num_boxes <= 1)
885 return i965_fixup_unbounded (dst, extents, clip);
887 i965_shader_init (&shader, dst, CAIRO_OPERATOR_CLEAR);
889 status = _cairo_clip_get_region (clip, &clip_region);
890 assert (status == CAIRO_STATUS_SUCCESS || CAIRO_INT_STATUS_UNSUPPORTED);
891 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
892 i965_shader_set_clip (&shader, clip);
895 status = i965_shader_acquire_pattern (&shader,
897 &_cairo_pattern_clear.base,
898 &extents->unbounded);
899 if (unlikely (status)) {
900 i965_shader_fini (&shader);
904 _cairo_boxes_init (&clear);
906 box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
907 box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
908 box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
909 box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
911 if (clip_region == NULL) {
914 _cairo_boxes_init (&tmp);
916 status = _cairo_boxes_add (&tmp, &box);
917 assert (status == CAIRO_STATUS_SUCCESS);
919 tmp.chunks.next = &boxes->chunks;
920 tmp.num_boxes += boxes->num_boxes;
922 status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
923 CAIRO_FILL_RULE_WINDING,
926 tmp.chunks.next = NULL;
928 pixman_box32_t *pbox;
930 pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
931 _cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
933 status = _cairo_boxes_add (&clear, &box);
934 assert (status == CAIRO_STATUS_SUCCESS);
936 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
937 for (i = 0; i < chunk->count; i++) {
938 status = _cairo_boxes_add (&clear, &chunk->base[i]);
939 if (unlikely (status)) {
940 _cairo_boxes_fini (&clear);
946 status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
947 CAIRO_FILL_RULE_WINDING,
951 if (likely (status == CAIRO_STATUS_SUCCESS && clear.num_boxes)) {
952 i965_device_t *device;
954 device = i965_device (dst);
955 status = cairo_device_acquire (&device->intel.base.base);
956 if (unlikely (status))
959 status = i965_shader_commit (&shader, device);
960 if (unlikely (status))
963 for (chunk = &clear.chunks; chunk != NULL; chunk = chunk->next) {
964 for (i = 0; i < chunk->count; i++) {
965 int x1 = _cairo_fixed_integer_part (chunk->base[i].p1.x);
966 int y1 = _cairo_fixed_integer_part (chunk->base[i].p1.y);
967 int x2 = _cairo_fixed_integer_part (chunk->base[i].p2.x);
968 int y2 = _cairo_fixed_integer_part (chunk->base[i].p2.y);
970 i965_shader_add_rectangle (&shader, x1, y1, x2 - x1, y2 - y1);
975 cairo_device_release (&device->intel.base.base);
977 i965_shader_fini (&shader);
980 _cairo_boxes_fini (&clear);
985 static cairo_status_t
986 _composite_boxes (i965_surface_t *dst,
988 const cairo_pattern_t *pattern,
989 cairo_boxes_t *boxes,
990 cairo_antialias_t antialias,
992 const cairo_composite_rectangles_t *extents)
994 cairo_bool_t need_clip_surface = FALSE;
995 cairo_region_t *clip_region = NULL;
996 const struct _cairo_boxes_chunk *chunk;
997 cairo_status_t status;
998 i965_shader_t shader;
999 i965_device_t *device;
1002 /* If the boxes are not pixel-aligned, we will need to compute a real mask */
1003 if (antialias != CAIRO_ANTIALIAS_NONE) {
1004 if (! boxes->is_pixel_aligned)
1005 return CAIRO_INT_STATUS_UNSUPPORTED;
1008 i965_shader_init (&shader, dst, op);
1010 status = i965_shader_acquire_pattern (&shader,
1014 if (unlikely (status))
1018 status = _cairo_clip_get_region (clip, &clip_region);
1019 assert (status == CAIRO_STATUS_SUCCESS || CAIRO_INT_STATUS_UNSUPPORTED);
1020 need_clip_surface = status == CAIRO_INT_STATUS_UNSUPPORTED;
1021 if (need_clip_surface)
1022 i965_shader_set_clip (&shader, clip);
1025 device = i965_device (dst);
1026 status = cairo_device_acquire (&device->intel.base.base);
1027 if (unlikely (status))
1030 status = i965_shader_commit (&shader, i965_device (dst));
1031 if (unlikely (status))
1034 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
1035 cairo_box_t *box = chunk->base;
1036 for (i = 0; i < chunk->count; i++) {
1037 int x1 = _cairo_fixed_integer_round (box[i].p1.x);
1038 int y1 = _cairo_fixed_integer_round (box[i].p1.y);
1039 int x2 = _cairo_fixed_integer_round (box[i].p2.x);
1040 int y2 = _cairo_fixed_integer_round (box[i].p2.y);
1042 if (x2 > x1 && y2 > y1)
1043 i965_shader_add_rectangle (&shader, x1, y1, x2 - x1, y2 - y1);
1047 if (! extents->is_bounded)
1048 status = i965_fixup_unbounded_boxes (dst, extents, clip, boxes);
1051 cairo_device_release (&device->intel.base.base);
1053 i965_shader_fini (&shader);
1058 static cairo_status_t
1059 _clip_and_composite_boxes (i965_surface_t *dst,
1060 cairo_operator_t op,
1061 const cairo_pattern_t *src,
1062 cairo_boxes_t *boxes,
1063 cairo_antialias_t antialias,
1064 const cairo_composite_rectangles_t *extents,
1067 cairo_status_t status;
1069 if (boxes->num_boxes == 0) {
1070 if (extents->is_bounded)
1071 return CAIRO_STATUS_SUCCESS;
1073 return i965_fixup_unbounded (dst, extents, clip);
1076 /* Use a fast path if the boxes are pixel aligned */
1077 status = _composite_boxes (dst, op, src, boxes, antialias, clip, extents);
1078 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
1081 /* Otherwise render the boxes via an implicit mask and composite in the usual
1084 return i965_clip_and_composite_spans (dst, op, src, antialias,
1085 _composite_boxes_spans, boxes,
1089 static cairo_int_status_t
1090 i965_surface_paint (void *abstract_dst,
1091 cairo_operator_t op,
1092 const cairo_pattern_t *source,
1095 i965_surface_t *dst = abstract_dst;
1096 cairo_composite_rectangles_t extents;
1097 cairo_boxes_t boxes;
1098 cairo_box_t *clip_boxes = boxes.boxes_embedded;
1099 cairo_clip_t local_clip;
1100 cairo_bool_t have_clip = FALSE;
1101 int num_boxes = ARRAY_LENGTH (boxes.boxes_embedded);
1102 cairo_status_t status;
1104 /* XXX unsupported operators? use pixel shader blending, eventually */
1106 status = _cairo_composite_rectangles_init_for_paint (&extents,
1107 dst->intel.drm.width,
1108 dst->intel.drm.height,
1111 if (unlikely (status))
1114 if (clip != NULL && _cairo_clip_contains_extents (clip, &extents))
1118 clip = _cairo_clip_init_copy (&local_clip, clip);
1122 status = _cairo_clip_to_boxes (&clip, &extents, &clip_boxes, &num_boxes);
1123 if (unlikely (status)) {
1125 _cairo_clip_fini (&local_clip);
1130 _cairo_boxes_init_for_array (&boxes, clip_boxes, num_boxes);
1131 status = _clip_and_composite_boxes (dst, op, source,
1132 &boxes, CAIRO_ANTIALIAS_DEFAULT,
1134 if (clip_boxes != boxes.boxes_embedded)
1138 _cairo_clip_fini (&local_clip);
1143 static cairo_int_status_t
1144 i965_surface_mask (void *abstract_dst,
1145 cairo_operator_t op,
1146 const cairo_pattern_t *source,
1147 const cairo_pattern_t *mask,
1150 i965_surface_t *dst = abstract_dst;
1151 cairo_composite_rectangles_t extents;
1152 i965_shader_t shader;
1153 i965_device_t *device;
1154 cairo_clip_t local_clip;
1155 cairo_region_t *clip_region = NULL;
1156 cairo_bool_t need_clip_surface = FALSE;
1157 cairo_bool_t have_clip = FALSE;
1158 cairo_status_t status;
1160 status = _cairo_composite_rectangles_init_for_mask (&extents,
1161 dst->intel.drm.width,
1162 dst->intel.drm.height,
1163 op, source, mask, clip);
1164 if (unlikely (status))
1167 if (clip != NULL && _cairo_clip_contains_extents (clip, &extents))
1170 if (clip != NULL && extents.is_bounded) {
1171 clip = _cairo_clip_init_copy (&local_clip, clip);
1172 status = _cairo_clip_rectangle (clip, &extents.bounded);
1173 if (unlikely (status)) {
1174 _cairo_clip_fini (&local_clip);
1181 i965_shader_init (&shader, dst, op);
1183 status = i965_shader_acquire_pattern (&shader,
1187 if (unlikely (status))
1190 status = i965_shader_acquire_pattern (&shader,
1194 if (unlikely (status))
1198 status = _cairo_clip_get_region (clip, &clip_region);
1199 assert (status == CAIRO_STATUS_SUCCESS || CAIRO_INT_STATUS_UNSUPPORTED);
1200 need_clip_surface = status == CAIRO_INT_STATUS_UNSUPPORTED;
1201 if (need_clip_surface)
1202 i965_shader_set_clip (&shader, clip);
1205 device = i965_device (dst);
1206 status = cairo_device_acquire (&device->intel.base.base);
1207 if (unlikely (status))
1210 status = i965_shader_commit (&shader, device);
1211 if (unlikely (status))
1214 if (clip_region != NULL) {
1215 unsigned int n, num_rectangles;
1217 num_rectangles = cairo_region_num_rectangles (clip_region);
1218 for (n = 0; n < num_rectangles; n++) {
1219 cairo_rectangle_int_t rect;
1221 cairo_region_get_rectangle (clip_region, n, &rect);
1223 i965_shader_add_rectangle (&shader,
1225 rect.width, rect.height);
1228 i965_shader_add_rectangle (&shader,
1231 extents.bounded.width,
1232 extents.bounded.height);
1235 if (! extents.is_bounded)
1236 status = i965_fixup_unbounded (dst, &extents, clip);
1239 cairo_device_release (&device->intel.base.base);
1241 i965_shader_fini (&shader);
1243 _cairo_clip_fini (&local_clip);
1249 cairo_polygon_t polygon;
1250 cairo_fill_rule_t fill_rule;
1251 cairo_antialias_t antialias;
1252 } composite_polygon_info_t;
1254 static cairo_status_t
1255 _composite_polygon_spans (void *closure,
1256 cairo_span_renderer_t *renderer,
1257 const cairo_rectangle_int_t *extents)
1259 composite_polygon_info_t *info = closure;
1260 cairo_botor_scan_converter_t converter;
1261 cairo_status_t status;
1264 box.p1.x = _cairo_fixed_from_int (extents->x);
1265 box.p1.y = _cairo_fixed_from_int (extents->y);
1266 box.p2.x = _cairo_fixed_from_int (extents->x + extents->width);
1267 box.p2.y = _cairo_fixed_from_int (extents->y + extents->height);
1269 _cairo_botor_scan_converter_init (&converter, &box, info->fill_rule);
1271 status = converter.base.add_polygon (&converter.base, &info->polygon);
1272 if (likely (status == CAIRO_STATUS_SUCCESS))
1273 status = converter.base.generate (&converter.base, renderer);
1275 converter.base.destroy (&converter.base);
1280 static cairo_int_status_t
1281 i965_surface_stroke (void *abstract_dst,
1282 cairo_operator_t op,
1283 const cairo_pattern_t *source,
1284 cairo_path_fixed_t *path,
1285 const cairo_stroke_style_t *stroke_style,
1286 const cairo_matrix_t *ctm,
1287 const cairo_matrix_t *ctm_inverse,
1289 cairo_antialias_t antialias,
1292 i965_surface_t *dst = abstract_dst;
1293 cairo_composite_rectangles_t extents;
1294 composite_polygon_info_t info;
1295 cairo_box_t boxes_stack[32], *clip_boxes = boxes_stack;
1296 int num_boxes = ARRAY_LENGTH (boxes_stack);
1297 cairo_clip_t local_clip;
1298 cairo_bool_t have_clip = FALSE;
1299 cairo_status_t status;
1301 status = _cairo_composite_rectangles_init_for_stroke (&extents,
1302 dst->intel.drm.width,
1303 dst->intel.drm.height,
1305 path, stroke_style, ctm,
1307 if (unlikely (status))
1310 if (clip != NULL && _cairo_clip_contains_extents (clip, &extents))
1314 clip = _cairo_clip_init_copy (&local_clip, clip);
1318 status = _cairo_clip_to_boxes (&clip, &extents, &clip_boxes, &num_boxes);
1319 if (unlikely (status)) {
1321 _cairo_clip_fini (&local_clip);
1326 if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
1327 cairo_boxes_t boxes;
1329 _cairo_boxes_init (&boxes);
1330 _cairo_boxes_limit (&boxes, clip_boxes, num_boxes);
1331 status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
1335 if (likely (status == CAIRO_STATUS_SUCCESS)) {
1336 status = _clip_and_composite_boxes (dst, op, source,
1341 _cairo_boxes_fini (&boxes);
1343 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
1347 _cairo_polygon_init (&info.polygon, clip_boxes, num_boxes);
1349 status = _cairo_path_fixed_stroke_to_polygon (path,
1354 if (unlikely (status))
1355 goto CLEANUP_POLYGON;
1357 if (extents.is_bounded) {
1358 cairo_rectangle_int_t rect;
1360 _cairo_box_round_to_rectangle (&info.polygon.extents, &rect);
1361 if (! _cairo_rectangle_intersect (&extents.bounded, &rect))
1362 goto CLEANUP_POLYGON;
1365 if (info.polygon.num_edges == 0) {
1366 if (! extents.is_bounded)
1367 status = i965_fixup_unbounded (dst, &extents, clip);
1369 info.fill_rule = CAIRO_FILL_RULE_WINDING;
1370 info.antialias = antialias;
1371 status = i965_clip_and_composite_spans (dst, op, source, antialias,
1372 _composite_polygon_spans, &info,
1377 _cairo_polygon_fini (&info.polygon);
1380 if (clip_boxes != boxes_stack)
1384 _cairo_clip_fini (&local_clip);
1389 static cairo_int_status_t
1390 i965_surface_fill (void *abstract_dst,
1391 cairo_operator_t op,
1392 const cairo_pattern_t*source,
1393 cairo_path_fixed_t *path,
1394 cairo_fill_rule_t fill_rule,
1396 cairo_antialias_t antialias,
1399 i965_surface_t *dst = abstract_dst;
1400 cairo_composite_rectangles_t extents;
1401 composite_polygon_info_t info;
1402 cairo_box_t boxes_stack[32], *clip_boxes = boxes_stack;
1403 cairo_clip_t local_clip;
1404 cairo_bool_t have_clip = FALSE;
1405 int num_boxes = ARRAY_LENGTH (boxes_stack);
1406 cairo_status_t status;
1408 status = _cairo_composite_rectangles_init_for_fill (&extents,
1409 dst->intel.drm.width,
1410 dst->intel.drm.height,
1413 if (unlikely (status))
1416 if (clip != NULL && _cairo_clip_contains_extents (clip, &extents))
1420 clip = _cairo_clip_init_copy (&local_clip, clip);
1424 status = _cairo_clip_to_boxes (&clip, &extents, &clip_boxes, &num_boxes);
1425 if (unlikely (status)) {
1427 _cairo_clip_fini (&local_clip);
1432 assert (! _cairo_path_fixed_fill_is_empty (path));
1434 if (_cairo_path_fixed_fill_is_rectilinear (path)) {
1435 cairo_boxes_t boxes;
1437 _cairo_boxes_init (&boxes);
1438 _cairo_boxes_limit (&boxes, clip_boxes, num_boxes);
1439 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
1442 if (likely (status == CAIRO_STATUS_SUCCESS)) {
1443 status = _clip_and_composite_boxes (dst, op, source,
1448 _cairo_boxes_fini (&boxes);
1450 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
1454 _cairo_polygon_init (&info.polygon, clip_boxes, num_boxes);
1456 status = _cairo_path_fixed_fill_to_polygon (path, tolerance, &info.polygon);
1457 if (unlikely (status))
1458 goto CLEANUP_POLYGON;
1460 if (extents.is_bounded) {
1461 cairo_rectangle_int_t rect;
1463 _cairo_box_round_to_rectangle (&info.polygon.extents, &rect);
1464 if (! _cairo_rectangle_intersect (&extents.bounded, &rect))
1465 goto CLEANUP_POLYGON;
1468 if (info.polygon.num_edges == 0) {
1469 if (! extents.is_bounded)
1470 status = i965_fixup_unbounded (dst, &extents, clip);
1472 info.fill_rule = fill_rule;
1473 info.antialias = antialias;
1474 status = i965_clip_and_composite_spans (dst, op, source, antialias,
1475 _composite_polygon_spans, &info,
1480 _cairo_polygon_fini (&info.polygon);
1483 if (clip_boxes != boxes_stack)
1487 _cairo_clip_fini (&local_clip);
1492 static const cairo_surface_backend_t i965_surface_backend = {
1493 CAIRO_SURFACE_TYPE_DRM,
1494 _cairo_default_context_create,
1496 i965_surface_create_similar,
1497 i965_surface_finish,
1498 intel_surface_acquire_source_image,
1499 intel_surface_release_source_image,
1502 NULL, /* composite */
1504 NULL, /* trapezoids */
1506 NULL, /* check-span */
1508 NULL, /* copy_page */
1509 NULL, /* show_page */
1510 _cairo_drm_surface_get_extents,
1511 NULL, /* old-glyphs */
1512 _cairo_drm_surface_get_font_options,
1515 NULL, /* mark_dirty */
1516 intel_scaled_font_fini,
1517 intel_scaled_glyph_fini,
1521 i965_surface_stroke,
1523 i965_surface_glyphs,
1527 i965_surface_init (i965_surface_t *surface,
1528 cairo_drm_device_t *device,
1529 cairo_format_t format,
1530 int width, int height)
1532 intel_surface_init (&surface->intel, &i965_surface_backend, device,
1533 format, width, height);
1534 surface->stream = 0;
1537 static inline int cairo_const
1538 i965_tiling_stride (uint32_t tiling, int stride)
1540 if (tiling == I915_TILING_NONE)
1543 return (stride + 127) & -128;
1546 static inline int cairo_const
1547 i965_tiling_height (uint32_t tiling, int height)
1551 case I915_TILING_NONE: return (height + 1) & -2;
1552 case I915_TILING_X: return (height + 7) & -8;
1553 case I915_TILING_Y: return (height + 31) & -32;
1558 i965_surface_create_internal (cairo_drm_device_t *base_dev,
1559 cairo_format_t format,
1560 int width, int height,
1562 cairo_bool_t gpu_target)
1564 i965_surface_t *surface;
1565 cairo_status_t status_ignored;
1567 surface = malloc (sizeof (i965_surface_t));
1568 if (unlikely (surface == NULL))
1569 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1571 i965_surface_init (surface, base_dev, format, width, height);
1573 if (width && height) {
1574 uint32_t size, stride;
1577 width = (width + 3) & -4;
1578 stride = cairo_format_stride_for_width (surface->intel.drm.format, width);
1579 stride = (stride + 63) & ~63;
1580 stride = i965_tiling_stride (tiling, stride);
1581 surface->intel.drm.stride = stride;
1583 height = i965_tiling_height (tiling, height);
1584 assert (height <= I965_MAX_SIZE);
1586 size = stride * height;
1587 bo = intel_bo_create (to_intel_device (&base_dev->base),
1589 gpu_target, tiling, stride);
1591 status_ignored = _cairo_drm_surface_finish (&surface->intel.drm);
1593 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1596 bo->tiling = tiling;
1597 bo->stride = stride;
1598 surface->intel.drm.bo = &bo->base;
1600 assert (bo->base.size >= (size_t) stride*height);
1603 return &surface->intel.drm.base;
1606 static cairo_surface_t *
1607 i965_surface_create (cairo_drm_device_t *device,
1608 cairo_format_t format, int width, int height)
1611 case CAIRO_FORMAT_ARGB32:
1612 case CAIRO_FORMAT_RGB16_565:
1613 case CAIRO_FORMAT_RGB24:
1614 case CAIRO_FORMAT_A8:
1616 case CAIRO_FORMAT_INVALID:
1618 case CAIRO_FORMAT_A1:
1619 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
1622 return i965_surface_create_internal (device, format, width, height,
1623 I965_TILING_DEFAULT, TRUE);
1626 static cairo_surface_t *
1627 i965_surface_create_for_name (cairo_drm_device_t *base_dev,
1629 cairo_format_t format,
1630 int width, int height, int stride)
1632 i965_device_t *device;
1633 i965_surface_t *surface;
1634 cairo_status_t status_ignored;
1637 min_stride = cairo_format_stride_for_width (format, (width + 3) & -4);
1638 if (stride < min_stride || stride & 63)
1639 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
1641 if (format == CAIRO_FORMAT_A1)
1642 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
1645 case CAIRO_FORMAT_ARGB32:
1646 case CAIRO_FORMAT_RGB16_565:
1647 case CAIRO_FORMAT_RGB24:
1648 case CAIRO_FORMAT_A8:
1650 case CAIRO_FORMAT_INVALID:
1652 case CAIRO_FORMAT_A1:
1653 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
1656 surface = malloc (sizeof (i965_surface_t));
1657 if (unlikely (surface == NULL))
1658 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1660 i965_surface_init (surface, base_dev, format, width, height);
1662 device = (i965_device_t *) base_dev;
1663 surface->intel.drm.bo = &intel_bo_create_for_name (&device->intel, name)->base;
1664 if (unlikely (surface->intel.drm.bo == NULL)) {
1665 status_ignored = _cairo_drm_surface_finish (&surface->intel.drm);
1667 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1670 surface->intel.drm.stride = stride;
1672 return &surface->intel.drm.base;
1675 static cairo_status_t
1676 i965_surface_enable_scan_out (void *abstract_surface)
1678 i965_surface_t *surface = abstract_surface;
1681 if (unlikely (surface->intel.drm.bo == NULL))
1682 return _cairo_error (CAIRO_STATUS_INVALID_SIZE);
1684 bo = to_intel_bo (surface->intel.drm.bo);
1685 if (bo->tiling != I915_TILING_X) {
1686 i965_device_t *device = i965_device (surface);
1687 cairo_surface_pattern_t pattern;
1688 cairo_surface_t *clone;
1689 cairo_status_t status;
1691 clone = i965_surface_create_internal (&device->intel.base,
1692 surface->intel.drm.base.content,
1693 surface->intel.drm.width,
1694 surface->intel.drm.height,
1697 if (unlikely (clone->status))
1698 return clone->status;
1701 _cairo_pattern_init_for_surface (&pattern, &surface->intel.drm.base);
1702 pattern.base.filter = CAIRO_FILTER_NEAREST;
1704 status = _cairo_surface_paint (clone,
1705 CAIRO_OPERATOR_SOURCE,
1709 _cairo_pattern_fini (&pattern.base);
1711 if (unlikely (status)) {
1712 cairo_surface_destroy (clone);
1716 /* swap buffer objects */
1717 surface->intel.drm.bo = ((cairo_drm_surface_t *) clone)->bo;
1718 ((cairo_drm_surface_t *) clone)->bo = &bo->base;
1719 bo = to_intel_bo (surface->intel.drm.bo);
1721 cairo_surface_destroy (clone);
1724 if (unlikely (bo->tiling == I915_TILING_Y))
1725 return _cairo_error (CAIRO_STATUS_INVALID_FORMAT); /* XXX */
1727 return CAIRO_STATUS_SUCCESS;
1730 static cairo_int_status_t
1731 _i965_device_flush (cairo_drm_device_t *device)
1733 cairo_status_t status;
1735 if (unlikely (device->base.finished))
1736 return CAIRO_STATUS_SUCCESS;
1738 status = cairo_device_acquire (&device->base);
1739 if (likely (status == CAIRO_STATUS_SUCCESS))
1740 status = i965_device_flush ((i965_device_t *) device);
1742 cairo_device_release (&device->base);
1747 static cairo_int_status_t
1748 _i965_device_throttle (cairo_drm_device_t *device)
1750 cairo_status_t status;
1752 status = cairo_device_acquire (&device->base);
1753 if (unlikely (status))
1756 status = i965_device_flush ((i965_device_t *) device);
1757 intel_throttle ((intel_device_t *) device);
1759 cairo_device_release (&device->base);
1765 _i965_device_destroy (void *base)
1767 i965_device_t *device = base;
1769 i965_device_reset (device);
1770 i965_general_state_reset (device);
1772 _cairo_hash_table_destroy (device->sf_states);
1773 _cairo_hash_table_destroy (device->samplers);
1774 _cairo_hash_table_destroy (device->cc_states);
1775 _cairo_hash_table_destroy (device->wm_kernels);
1776 _cairo_hash_table_destroy (device->wm_states);
1777 _cairo_hash_table_destroy (device->wm_bindings);
1779 _cairo_freelist_fini (&device->sf_freelist);
1780 _cairo_freelist_fini (&device->cc_freelist);
1781 _cairo_freelist_fini (&device->wm_kernel_freelist);
1782 _cairo_freelist_fini (&device->wm_state_freelist);
1783 _cairo_freelist_fini (&device->wm_binding_freelist);
1784 _cairo_freelist_fini (&device->sampler_freelist);
1786 intel_device_fini (&device->intel);
1791 hash_equal (const void *A, const void *B)
1793 const cairo_hash_entry_t *a = A, *b = B;
1794 return a->hash == b->hash;
1797 cairo_drm_device_t *
1798 _cairo_drm_i965_device_create (int fd, dev_t dev, int vendor_id, int chip_id)
1800 i965_device_t *device;
1802 cairo_status_t status;
1804 if (! intel_info (fd, >t_size))
1807 device = malloc (sizeof (i965_device_t));
1808 if (unlikely (device == NULL))
1809 return (cairo_drm_device_t *) _cairo_device_create_in_error (CAIRO_STATUS_NO_MEMORY);
1811 status = intel_device_init (&device->intel, fd);
1812 if (unlikely (status))
1815 device->is_g4x = IS_G4X (chip_id);
1816 //device->is_g5x = IS_G5X (chip_id);
1818 device->intel.base.surface.create = i965_surface_create;
1819 device->intel.base.surface.create_for_name = i965_surface_create_for_name;
1820 device->intel.base.surface.create_from_cacheable_image = NULL;
1821 device->intel.base.surface.enable_scan_out = i965_surface_enable_scan_out;
1823 device->intel.base.device.flush = _i965_device_flush;
1824 device->intel.base.device.throttle = _i965_device_throttle;
1825 device->intel.base.device.destroy = _i965_device_destroy;
1827 device->sf_states = _cairo_hash_table_create (i965_sf_state_equal);
1828 if (unlikely (device->sf_states == NULL))
1831 _cairo_freelist_init (&device->sf_freelist,
1832 sizeof (struct i965_sf_state));
1835 device->cc_states = _cairo_hash_table_create (i965_cc_state_equal);
1836 if (unlikely (device->cc_states == NULL))
1839 _cairo_freelist_init (&device->cc_freelist,
1840 sizeof (struct i965_cc_state));
1843 device->wm_kernels = _cairo_hash_table_create (hash_equal);
1844 if (unlikely (device->wm_kernels == NULL))
1847 _cairo_freelist_init (&device->wm_kernel_freelist,
1848 sizeof (struct i965_wm_kernel));
1850 device->wm_states = _cairo_hash_table_create (i965_wm_state_equal);
1851 if (unlikely (device->wm_states == NULL))
1852 goto CLEANUP_WM_KERNEL;
1854 _cairo_freelist_init (&device->wm_state_freelist,
1855 sizeof (struct i965_wm_state));
1858 device->wm_bindings = _cairo_hash_table_create (i965_wm_binding_equal);
1859 if (unlikely (device->wm_bindings == NULL))
1860 goto CLEANUP_WM_STATE;
1862 _cairo_freelist_init (&device->wm_binding_freelist,
1863 sizeof (struct i965_wm_binding));
1865 device->samplers = _cairo_hash_table_create (hash_equal);
1866 if (unlikely (device->samplers == NULL))
1867 goto CLEANUP_WM_BINDING;
1869 _cairo_freelist_init (&device->sampler_freelist,
1870 sizeof (struct i965_sampler));
1872 i965_stream_init (&device->batch,
1873 device->batch_base, sizeof (device->batch_base),
1875 device->batch_relocations,
1876 ARRAY_LENGTH (device->batch_relocations));
1878 i965_stream_init (&device->surface,
1879 device->surface_base, sizeof (device->surface_base),
1880 device->surface_pending_relocations,
1881 ARRAY_LENGTH (device->surface_pending_relocations),
1882 device->surface_relocations,
1883 ARRAY_LENGTH (device->surface_relocations));
1885 i965_stream_init (&device->general,
1886 device->general_base, sizeof (device->general_base),
1887 device->general_pending_relocations,
1888 ARRAY_LENGTH (device->general_pending_relocations),
1891 i965_stream_init (&device->vertex,
1892 device->vertex_base, sizeof (device->vertex_base),
1893 device->vertex_pending_relocations,
1894 ARRAY_LENGTH (device->vertex_pending_relocations),
1897 cairo_list_init (&device->flush);
1898 i965_device_reset (device);
1899 device->vs_offset = (uint32_t) -1;
1900 device->border_color_offset = (uint32_t) -1;
1901 device->general_state = NULL;
1903 return _cairo_drm_device_init (&device->intel.base,
1904 fd, dev, vendor_id, chip_id,
1908 _cairo_hash_table_destroy (device->wm_bindings);
1910 _cairo_hash_table_destroy (device->wm_states);
1912 _cairo_hash_table_destroy (device->wm_kernels);
1914 _cairo_hash_table_destroy (device->cc_states);
1916 _cairo_hash_table_destroy (device->sf_states);
1918 intel_device_fini (&device->intel);
1921 return (cairo_drm_device_t *) _cairo_device_create_in_error (status);