1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_RESOURCE_PROVIDER_H_
6 #define CC_RESOURCES_RESOURCE_PROVIDER_H_
14 #include "base/basictypes.h"
15 #include "base/callback.h"
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/threading/thread_checker.h"
20 #include "cc/base/cc_export.h"
21 #include "cc/output/context_provider.h"
22 #include "cc/output/output_surface.h"
23 #include "cc/resources/raster_buffer.h"
24 #include "cc/resources/release_callback_impl.h"
25 #include "cc/resources/resource_format.h"
26 #include "cc/resources/return_callback.h"
27 #include "cc/resources/shared_bitmap.h"
28 #include "cc/resources/single_release_callback_impl.h"
29 #include "cc/resources/texture_mailbox.h"
30 #include "cc/resources/transferable_resource.h"
31 #include "third_party/khronos/GLES2/gl2.h"
32 #include "third_party/khronos/GLES2/gl2ext.h"
33 #include "third_party/skia/include/core/SkBitmap.h"
34 #include "third_party/skia/include/core/SkCanvas.h"
35 #include "ui/gfx/size.h"
51 class BlockingTaskRunner
;
54 class SharedBitmapManager
;
55 class TextureUploader
;
57 // This class is not thread-safe and can only be called from the thread it was
58 // created on (in practice, the impl thread).
59 class CC_EXPORT ResourceProvider
{
61 typedef unsigned ResourceId
;
62 typedef std::vector
<ResourceId
> ResourceIdArray
;
63 typedef std::set
<ResourceId
> ResourceIdSet
;
64 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
66 TextureHintDefault
= 0x0,
67 TextureHintImmutable
= 0x1,
68 TextureHintFramebuffer
= 0x2,
69 TextureHintImmutableFramebuffer
=
70 TextureHintImmutable
| TextureHintFramebuffer
78 static scoped_ptr
<ResourceProvider
> Create(
79 OutputSurface
* output_surface
,
80 SharedBitmapManager
* shared_bitmap_manager
,
81 BlockingTaskRunner
* blocking_main_thread_task_runner
,
82 int highp_threshold_min
,
83 bool use_rgba_4444_texture_format
,
84 size_t id_allocation_chunk_size
,
85 bool use_distance_field_text
);
86 virtual ~ResourceProvider();
88 void InitializeSoftware();
91 void DidLoseOutputSurface() { lost_output_surface_
= true; }
93 int max_texture_size() const { return max_texture_size_
; }
94 ResourceFormat
memory_efficient_texture_format() const {
95 return use_rgba_4444_texture_format_
? RGBA_4444
: best_texture_format_
;
97 ResourceFormat
best_texture_format() const { return best_texture_format_
; }
98 bool use_sync_query() const { return use_sync_query_
; }
99 size_t num_resources() const { return resources_
.size(); }
101 // Checks whether a resource is in use by a consumer.
102 bool InUseByConsumer(ResourceId id
);
104 bool IsLost(ResourceId id
);
105 bool AllowOverlay(ResourceId id
);
107 // Producer interface.
109 ResourceType
default_resource_type() const { return default_resource_type_
; }
110 ResourceType
GetResourceType(ResourceId id
);
112 // Creates a resource of the default resource type.
113 ResourceId
CreateResource(const gfx::Size
& size
,
116 ResourceFormat format
);
118 // Creates a resource which is tagged as being managed for GPU memory
119 // accounting purposes.
120 ResourceId
CreateManagedResource(const gfx::Size
& size
,
124 ResourceFormat format
);
126 // You can also explicitly create a specific resource type.
127 ResourceId
CreateGLTexture(const gfx::Size
& size
,
132 ResourceFormat format
);
134 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
135 // Wraps an IOSurface into a GL resource.
136 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
137 unsigned io_surface_id
);
139 // Wraps an external texture mailbox into a GL resource.
140 ResourceId
CreateResourceFromTextureMailbox(
141 const TextureMailbox
& mailbox
,
142 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
144 void DeleteResource(ResourceId id
);
146 // Update pixels from image, copying source_rect (in image) to dest_offset (in
148 void SetPixels(ResourceId id
,
149 const uint8_t* image
,
150 const gfx::Rect
& image_rect
,
151 const gfx::Rect
& source_rect
,
152 const gfx::Vector2d
& dest_offset
);
154 // Check upload status.
155 size_t NumBlockingUploads();
156 void MarkPendingUploadsAsNonBlocking();
157 size_t EstimatedUploadsPerTick();
159 void ReleaseCachedData();
160 base::TimeTicks
EstimatedUploadCompletionTime(size_t uploads_per_tick
);
162 // Only flush the command buffer if supported.
163 // Returns true if the shallow flush occurred, false otherwise.
164 bool ShallowFlushIfSupported();
166 // Creates accounting for a child. Returns a child ID.
167 int CreateChild(const ReturnCallback
& return_callback
);
169 // Destroys accounting for the child, deleting all accounted resources.
170 void DestroyChild(int child
);
172 // Gets the child->parent resource ID map.
173 const ResourceIdMap
& GetChildToParentMap(int child
) const;
175 // Prepares resources to be transfered to the parent, moving them to
176 // mailboxes and serializing meta-data into TransferableResources.
177 // Resources are not removed from the ResourceProvider, but are marked as
179 void PrepareSendToParent(const ResourceIdArray
& resources
,
180 TransferableResourceArray
* transferable_resources
);
182 // Receives resources from a child, moving them from mailboxes. Resource IDs
183 // passed are in the child namespace, and will be translated to the parent
184 // namespace, added to the child->parent map.
185 // This adds the resources to the working set in the ResourceProvider without
186 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
187 // after calling this method to do that. All calls to ReceiveFromChild should
188 // be followed by a DeclareUsedResourcesFromChild.
189 // NOTE: if the sync_point is set on any TransferableResource, this will
191 void ReceiveFromChild(
192 int child
, const TransferableResourceArray
& transferable_resources
);
194 // Once a set of resources have been received, they may or may not be used.
195 // This declares what set of resources are currently in use from the child,
196 // releasing any other resources back to the child.
197 void DeclareUsedResourcesFromChild(
199 const ResourceIdArray
& resources_from_child
);
201 // Receives resources from the parent, moving them from mailboxes. Resource
202 // IDs passed are in the child namespace.
203 // NOTE: if the sync_point is set on any TransferableResource, this will
205 void ReceiveReturnsFromParent(
206 const ReturnedResourceArray
& transferable_resources
);
208 // The following lock classes are part of the ResourceProvider API and are
209 // needed to read and write the resource contents. The user must ensure
210 // that they only use GL locks on GL resources, etc, and this is enforced
212 class CC_EXPORT ScopedReadLockGL
{
214 ScopedReadLockGL(ResourceProvider
* resource_provider
,
215 ResourceProvider::ResourceId resource_id
);
216 virtual ~ScopedReadLockGL();
218 unsigned texture_id() const { return texture_id_
; }
221 ResourceProvider
* resource_provider_
;
222 ResourceProvider::ResourceId resource_id_
;
225 unsigned texture_id_
;
227 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
230 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
232 ScopedSamplerGL(ResourceProvider
* resource_provider
,
233 ResourceProvider::ResourceId resource_id
,
235 ScopedSamplerGL(ResourceProvider
* resource_provider
,
236 ResourceProvider::ResourceId resource_id
,
239 virtual ~ScopedSamplerGL();
241 GLenum
target() const { return target_
; }
247 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
250 class CC_EXPORT ScopedWriteLockGL
{
252 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
253 ResourceProvider::ResourceId resource_id
);
254 ~ScopedWriteLockGL();
256 unsigned texture_id() const { return texture_id_
; }
259 ResourceProvider
* resource_provider_
;
260 ResourceProvider::ResourceId resource_id_
;
261 unsigned texture_id_
;
263 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
266 class CC_EXPORT ScopedReadLockSoftware
{
268 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
269 ResourceProvider::ResourceId resource_id
);
270 ~ScopedReadLockSoftware();
272 const SkBitmap
* sk_bitmap() const {
276 GLint
wrap_mode() const { return wrap_mode_
; }
278 bool valid() const { return !!sk_bitmap_
.getPixels(); }
281 ResourceProvider
* resource_provider_
;
282 ResourceProvider::ResourceId resource_id_
;
286 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
289 class CC_EXPORT ScopedWriteLockSoftware
{
291 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
292 ResourceProvider::ResourceId resource_id
);
293 ~ScopedWriteLockSoftware();
295 SkCanvas
* sk_canvas() { return sk_canvas_
.get(); }
296 bool valid() const { return !!sk_bitmap_
.getPixels(); }
299 ResourceProvider
* resource_provider_
;
300 ResourceProvider::ResourceId resource_id_
;
302 scoped_ptr
<SkCanvas
> sk_canvas_
;
304 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
307 class Fence
: public base::RefCounted
<Fence
> {
311 virtual void Set() = 0;
312 virtual bool HasPassed() = 0;
315 friend class base::RefCounted
<Fence
>;
319 DISALLOW_COPY_AND_ASSIGN(Fence
);
322 // Returns a RasterBuffer for gpu rasterization.
323 // Call Release before the resource can be read or used for compositing.
324 // It is used for direct gpu rasterization.
325 RasterBuffer
* AcquireGpuRasterBuffer(ResourceId id
);
326 void ReleaseGpuRasterBuffer(ResourceId id
);
328 // Returns a RasterBuffer backed by an image buffer. ReleaseImageRasterBuffer
329 // returns true if RasterBuffer was written to while acquired.
330 // Rasterizing to the RasterBuffer writes the content into the image buffer,
331 // which is internally bound to the underlying resource when read.
332 // Call Release before the resource can be read or used for compositing.
333 // It is used by ImageRasterWorkerPool.
334 RasterBuffer
* AcquireImageRasterBuffer(ResourceId id
);
335 bool ReleaseImageRasterBuffer(ResourceId id
);
337 // Returns a RasterBuffer backed by pixel buffer. ReleasePixelRasterBuffer
338 // returns true if RasterBuffer was written to while acquired.
339 // The pixel buffer needs to be uploaded to the underlying resource
340 // using BeginSetPixels before the resouce can be used for compositing.
341 // It is used by PixelRasterWorkerPool.
342 RasterBuffer
* AcquirePixelRasterBuffer(ResourceId id
);
343 bool ReleasePixelRasterBuffer(ResourceId id
);
345 // Asynchronously update pixels from acquired pixel buffer.
346 void BeginSetPixels(ResourceId id
);
347 void ForceSetPixelsToComplete(ResourceId id
);
348 bool DidSetPixelsComplete(ResourceId id
);
350 // For tests only! This prevents detecting uninitialized reads.
351 // Use SetPixels or LockForWrite to allocate implicitly.
352 void AllocateForTesting(ResourceId id
);
355 void CreateForTesting(ResourceId id
);
357 GLenum
TargetForTesting(ResourceId id
);
359 // Sets the current read fence. If a resource is locked for read
360 // and has read fences enabled, the resource will not allow writes
361 // until this fence has passed.
362 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
364 // Enable read lock fences for a specific resource.
365 void EnableReadLockFences(ResourceProvider::ResourceId id
);
367 // Indicates if we can currently lock this resource for write.
368 bool CanLockForWrite(ResourceId id
);
370 // Copy pixels from source to destination.
371 void CopyResource(ResourceId source_id
, ResourceId dest_id
);
373 void WaitSyncPointIfNeeded(ResourceId id
);
375 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
378 class GpuRasterBuffer
;
379 class ImageRasterBuffer
;
380 class PixelRasterBuffer
;
383 enum Origin
{ Internal
, External
, Delegated
};
387 Resource(unsigned texture_id
,
388 const gfx::Size
& size
,
395 ResourceFormat format
);
396 Resource(uint8_t* pixels
,
397 SharedBitmap
* bitmap
,
398 const gfx::Size
& size
,
402 Resource(const SharedBitmapId
& bitmap_id
,
403 const gfx::Size
& size
,
410 // Pixel buffer used for set pixels without unnecessary copying.
411 unsigned gl_pixel_buffer_id
;
412 // Query used to determine when asynchronous set pixels complete.
413 unsigned gl_upload_query_id
;
414 // Query used to determine when read lock fence has passed.
415 unsigned gl_read_lock_query_id
;
416 TextureMailbox mailbox
;
417 ReleaseCallbackImpl release_callback_impl
;
419 int lock_for_read_count
;
422 bool dirty_image
: 1;
423 bool locked_for_write
: 1;
425 bool marked_for_deletion
: 1;
426 bool pending_set_pixels
: 1;
427 bool set_pixels_completion_forced
: 1;
429 bool read_lock_fences_enabled
: 1;
430 bool has_shared_bitmap_id
: 1;
431 bool allow_overlay
: 1;
432 scoped_refptr
<Fence
> read_lock_fence
;
436 // TODO(skyostil): Use a separate sampler object for filter state.
437 GLenum original_filter
;
440 unsigned bound_image_id
;
445 ResourceFormat format
;
446 SharedBitmapId shared_bitmap_id
;
447 SharedBitmap
* shared_bitmap
;
448 linked_ptr
<GpuRasterBuffer
> gpu_raster_buffer
;
449 linked_ptr
<ImageRasterBuffer
> image_raster_buffer
;
450 linked_ptr
<PixelRasterBuffer
> pixel_raster_buffer
;
452 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
454 class GpuRasterBuffer
: public RasterBuffer
{
456 GpuRasterBuffer(const Resource
* resource
,
457 ResourceProvider
* resource_provider
,
458 bool use_distance_field_text
);
459 virtual ~GpuRasterBuffer();
461 virtual skia::RefPtr
<SkCanvas
> AcquireSkCanvas() OVERRIDE
;
462 virtual void ReleaseSkCanvas(const skia::RefPtr
<SkCanvas
>& canvas
) OVERRIDE
;
465 const Resource
* resource_
;
466 ResourceProvider
* resource_provider_
;
467 skia::RefPtr
<SkSurface
> surface_
;
469 DISALLOW_COPY_AND_ASSIGN(GpuRasterBuffer
);
472 class ImageRasterBuffer
: public RasterBuffer
{
474 ImageRasterBuffer(const Resource
* resource
,
475 ResourceProvider
* resource_provider
);
476 virtual ~ImageRasterBuffer();
481 virtual skia::RefPtr
<SkCanvas
> AcquireSkCanvas() OVERRIDE
;
482 virtual void ReleaseSkCanvas(const skia::RefPtr
<SkCanvas
>& canvas
) OVERRIDE
;
485 const Resource
* resource_
;
486 ResourceProvider
* resource_provider_
;
487 uint8_t* mapped_buffer_
;
488 SkBitmap raster_bitmap_
;
489 bool raster_bitmap_changed_
;
492 DISALLOW_COPY_AND_ASSIGN(ImageRasterBuffer
);
495 class PixelRasterBuffer
: public RasterBuffer
{
497 PixelRasterBuffer(const Resource
* resource
,
498 ResourceProvider
* resource_provider
);
499 virtual ~PixelRasterBuffer();
504 virtual skia::RefPtr
<SkCanvas
> AcquireSkCanvas() OVERRIDE
;
505 virtual void ReleaseSkCanvas(const skia::RefPtr
<SkCanvas
>& canvas
) OVERRIDE
;
508 const Resource
* resource_
;
509 ResourceProvider
* resource_provider_
;
510 uint8_t* mapped_buffer_
;
511 SkBitmap raster_bitmap_
;
512 bool raster_bitmap_changed_
;
515 DISALLOW_COPY_AND_ASSIGN(PixelRasterBuffer
);
518 static bool CompareResourceMapIteratorsByChildId(
519 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& a
,
520 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& b
);
526 ResourceIdMap child_to_parent_map
;
527 ResourceIdMap parent_to_child_map
;
528 ReturnCallback return_callback
;
529 ResourceIdSet in_use_resources
;
530 bool marked_for_deletion
;
532 typedef base::hash_map
<int, Child
> ChildMap
;
534 bool ReadLockFenceHasPassed(const Resource
* resource
) {
535 return !resource
->read_lock_fence
.get() ||
536 resource
->read_lock_fence
->HasPassed();
539 ResourceProvider(OutputSurface
* output_surface
,
540 SharedBitmapManager
* shared_bitmap_manager
,
541 BlockingTaskRunner
* blocking_main_thread_task_runner
,
542 int highp_threshold_min
,
543 bool use_rgba_4444_texture_format
,
544 size_t id_allocation_chunk_size
,
545 bool use_distance_field_text
);
547 void CleanUpGLIfNeeded();
549 Resource
* GetResource(ResourceId id
);
550 const Resource
* LockForRead(ResourceId id
);
551 void UnlockForRead(ResourceId id
);
552 const Resource
* LockForWrite(ResourceId id
);
553 void UnlockForWrite(ResourceId id
);
554 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
555 const Resource
* resource
);
557 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
559 TransferableResource
* resource
);
564 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
565 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
567 const ResourceIdArray
& unused
);
568 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
569 void LazyCreate(Resource
* resource
);
570 void LazyAllocate(Resource
* resource
);
572 // TODO(alokp): Move the implementation to PixelRasterBuffer.
573 // Acquire pixel buffer for resource. The pixel buffer can be used to
574 // set resource pixels without performing unnecessary copying.
575 void AcquirePixelBuffer(Resource
* resource
);
576 void ReleasePixelBuffer(Resource
* resource
);
577 // Map/unmap the acquired pixel buffer.
578 uint8_t* MapPixelBuffer(const Resource
* resource
, int* stride
);
579 void UnmapPixelBuffer(const Resource
* resource
);
581 // TODO(alokp): Move the implementation to ImageRasterBuffer.
582 // Acquire and release an image. The image allows direct
583 // manipulation of texture memory.
584 void AcquireImage(Resource
* resource
);
585 void ReleaseImage(Resource
* resource
);
586 // Maps the acquired image so that its pixels could be modified.
587 // Unmap is called when all pixels are set.
588 uint8_t* MapImage(const Resource
* resource
, int* stride
);
589 void UnmapImage(const Resource
* resource
);
591 void BindImageForSampling(Resource
* resource
);
592 // Binds the given GL resource to a texture target for sampling using the
593 // specified filter for both minification and magnification. Returns the
594 // texture target used. The resource must be locked for reading.
595 GLenum
BindForSampling(ResourceProvider::ResourceId resource_id
,
599 // Returns NULL if the output_surface_ does not have a ContextProvider.
600 gpu::gles2::GLES2Interface
* ContextGL() const;
601 class GrContext
* GrContext() const;
603 OutputSurface
* output_surface_
;
604 SharedBitmapManager
* shared_bitmap_manager_
;
605 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
606 bool lost_output_surface_
;
607 int highp_threshold_min_
;
609 ResourceMap resources_
;
613 ResourceType default_resource_type_
;
614 bool use_texture_storage_ext_
;
615 bool use_texture_format_bgra_
;
616 bool use_texture_usage_hint_
;
617 bool use_compressed_texture_etc1_
;
618 scoped_ptr
<TextureUploader
> texture_uploader_
;
619 int max_texture_size_
;
620 ResourceFormat best_texture_format_
;
622 base::ThreadChecker thread_checker_
;
624 scoped_refptr
<Fence
> current_read_lock_fence_
;
625 bool use_rgba_4444_texture_format_
;
627 const size_t id_allocation_chunk_size_
;
628 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
629 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
631 bool use_sync_query_
;
633 bool use_distance_field_text_
;
635 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
639 // TODO(epenner): Move these format conversions to resource_format.h
640 // once that builds on mac (npapi.h currently #includes OpenGL.h).
641 inline unsigned BitsPerPixel(ResourceFormat format
) {
642 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
643 static const unsigned format_bits_per_pixel
[RESOURCE_FORMAT_MAX
+ 1] = {
652 return format_bits_per_pixel
[format
];
655 inline GLenum
GLDataType(ResourceFormat format
) {
656 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
657 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
658 GL_UNSIGNED_BYTE
, // RGBA_8888
659 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
660 GL_UNSIGNED_BYTE
, // BGRA_8888
661 GL_UNSIGNED_BYTE
, // ALPHA_8
662 GL_UNSIGNED_BYTE
, // LUMINANCE_8
663 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
664 GL_UNSIGNED_BYTE
// ETC1
666 return format_gl_data_type
[format
];
669 inline GLenum
GLDataFormat(ResourceFormat format
) {
670 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
671 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
672 GL_RGBA
, // RGBA_8888
673 GL_RGBA
, // RGBA_4444
674 GL_BGRA_EXT
, // BGRA_8888
676 GL_LUMINANCE
, // LUMINANCE_8
678 GL_ETC1_RGB8_OES
// ETC1
680 return format_gl_data_format
[format
];
683 inline GLenum
GLInternalFormat(ResourceFormat format
) {
684 return GLDataFormat(format
);
689 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_