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/release_callback.h"
24 #include "cc/resources/resource_format.h"
25 #include "cc/resources/return_callback.h"
26 #include "cc/resources/shared_bitmap.h"
27 #include "cc/resources/single_release_callback.h"
28 #include "cc/resources/texture_mailbox.h"
29 #include "cc/resources/transferable_resource.h"
30 #include "third_party/khronos/GLES2/gl2.h"
31 #include "third_party/khronos/GLES2/gl2ext.h"
32 #include "third_party/skia/include/core/SkBitmap.h"
33 #include "third_party/skia/include/core/SkCanvas.h"
34 #include "ui/gfx/size.h"
52 class SharedBitmapManager
;
53 class TextureUploader
;
55 // This class is not thread-safe and can only be called from the thread it was
56 // created on (in practice, the impl thread).
57 class CC_EXPORT ResourceProvider
{
59 typedef unsigned ResourceId
;
60 typedef std::vector
<ResourceId
> ResourceIdArray
;
61 typedef std::set
<ResourceId
> ResourceIdSet
;
62 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
63 enum TextureUsageHint
{
65 TextureUsageFramebuffer
,
73 static scoped_ptr
<ResourceProvider
> Create(
74 OutputSurface
* output_surface
,
75 SharedBitmapManager
* shared_bitmap_manager
,
76 int highp_threshold_min
,
77 bool use_rgba_4444_texture_format
,
78 size_t id_allocation_chunk_size
);
79 virtual ~ResourceProvider();
81 void InitializeSoftware();
84 void DidLoseOutputSurface() { lost_output_surface_
= true; }
86 int max_texture_size() const { return max_texture_size_
; }
87 ResourceFormat
memory_efficient_texture_format() const {
88 return use_rgba_4444_texture_format_
? RGBA_4444
: best_texture_format_
;
90 ResourceFormat
best_texture_format() const { return best_texture_format_
; }
91 size_t num_resources() const { return resources_
.size(); }
93 // Checks whether a resource is in use by a consumer.
94 bool InUseByConsumer(ResourceId id
);
96 bool IsLost(ResourceId id
);
97 bool AllowOverlay(ResourceId id
);
99 // Producer interface.
101 ResourceType
default_resource_type() const { return default_resource_type_
; }
102 ResourceType
GetResourceType(ResourceId id
);
104 // Creates a resource of the default resource type.
105 ResourceId
CreateResource(const gfx::Size
& size
,
107 TextureUsageHint hint
,
108 ResourceFormat format
);
110 // Creates a resource which is tagged as being managed for GPU memory
111 // accounting purposes.
112 ResourceId
CreateManagedResource(const gfx::Size
& size
,
115 TextureUsageHint hint
,
116 ResourceFormat format
);
118 // You can also explicitly create a specific resource type.
119 ResourceId
CreateGLTexture(const gfx::Size
& size
,
123 TextureUsageHint hint
,
124 ResourceFormat format
);
126 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
127 // Wraps an external texture into a GL resource.
128 ResourceId
CreateResourceFromExternalTexture(
129 unsigned texture_target
,
130 unsigned texture_id
);
132 // Wraps an external texture mailbox into a GL resource.
133 ResourceId
CreateResourceFromTextureMailbox(
134 const TextureMailbox
& mailbox
,
135 scoped_ptr
<SingleReleaseCallback
> release_callback
);
137 void DeleteResource(ResourceId id
);
139 // Update pixels from image, copying source_rect (in image) to dest_offset (in
141 void SetPixels(ResourceId id
,
142 const uint8_t* image
,
143 const gfx::Rect
& image_rect
,
144 const gfx::Rect
& source_rect
,
145 const gfx::Vector2d
& dest_offset
);
147 // Check upload status.
148 size_t NumBlockingUploads();
149 void MarkPendingUploadsAsNonBlocking();
150 size_t EstimatedUploadsPerTick();
152 void ReleaseCachedData();
153 base::TimeTicks
EstimatedUploadCompletionTime(size_t uploads_per_tick
);
155 // Flush all context operations, kicking uploads and ensuring ordering with
156 // respect to other contexts.
159 // Finish all context operations, causing any pending callbacks to be
163 // Only flush the command buffer if supported.
164 // Returns true if the shallow flush occurred, false otherwise.
165 bool ShallowFlushIfSupported();
167 // Creates accounting for a child. Returns a child ID.
168 int CreateChild(const ReturnCallback
& return_callback
);
170 // Destroys accounting for the child, deleting all accounted resources.
171 void DestroyChild(int child
);
173 // Gets the child->parent resource ID map.
174 const ResourceIdMap
& GetChildToParentMap(int child
) const;
176 // Prepares resources to be transfered to the parent, moving them to
177 // mailboxes and serializing meta-data into TransferableResources.
178 // Resources are not removed from the ResourceProvider, but are marked as
180 void PrepareSendToParent(const ResourceIdArray
& resources
,
181 TransferableResourceArray
* transferable_resources
);
183 // Receives resources from a child, moving them from mailboxes. Resource IDs
184 // passed are in the child namespace, and will be translated to the parent
185 // namespace, added to the child->parent map.
186 // This adds the resources to the working set in the ResourceProvider without
187 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
188 // after calling this method to do that. All calls to ReceiveFromChild should
189 // be followed by a DeclareUsedResourcesFromChild.
190 // NOTE: if the sync_point is set on any TransferableResource, this will
192 void ReceiveFromChild(
193 int child
, const TransferableResourceArray
& transferable_resources
);
195 // Once a set of resources have been received, they may or may not be used.
196 // This declares what set of resources are currently in use from the child,
197 // releasing any other resources back to the child.
198 void DeclareUsedResourcesFromChild(
200 const ResourceIdArray
& resources_from_child
);
202 // Receives resources from the parent, moving them from mailboxes. Resource
203 // IDs passed are in the child namespace.
204 // NOTE: if the sync_point is set on any TransferableResource, this will
206 void ReceiveReturnsFromParent(
207 const ReturnedResourceArray
& transferable_resources
);
209 // The following lock classes are part of the ResourceProvider API and are
210 // needed to read and write the resource contents. The user must ensure
211 // that they only use GL locks on GL resources, etc, and this is enforced
213 class CC_EXPORT ScopedReadLockGL
{
215 ScopedReadLockGL(ResourceProvider
* resource_provider
,
216 ResourceProvider::ResourceId resource_id
);
217 virtual ~ScopedReadLockGL();
219 unsigned texture_id() const { return texture_id_
; }
222 ResourceProvider
* resource_provider_
;
223 ResourceProvider::ResourceId resource_id_
;
226 unsigned texture_id_
;
228 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
231 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
233 ScopedSamplerGL(ResourceProvider
* resource_provider
,
234 ResourceProvider::ResourceId resource_id
,
236 ScopedSamplerGL(ResourceProvider
* resource_provider
,
237 ResourceProvider::ResourceId resource_id
,
240 virtual ~ScopedSamplerGL();
242 GLenum
target() const { return target_
; }
248 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
251 class CC_EXPORT ScopedWriteLockGL
{
253 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
254 ResourceProvider::ResourceId resource_id
);
255 ~ScopedWriteLockGL();
257 unsigned texture_id() const { return texture_id_
; }
260 ResourceProvider
* resource_provider_
;
261 ResourceProvider::ResourceId resource_id_
;
262 unsigned texture_id_
;
264 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
267 class CC_EXPORT ScopedReadLockSoftware
{
269 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
270 ResourceProvider::ResourceId resource_id
);
271 ~ScopedReadLockSoftware();
273 const SkBitmap
* sk_bitmap() const {
277 GLint
wrap_mode() const { return wrap_mode_
; }
279 bool valid() const { return !!sk_bitmap_
.getPixels(); }
282 ResourceProvider
* resource_provider_
;
283 ResourceProvider::ResourceId resource_id_
;
287 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
290 class CC_EXPORT ScopedWriteLockSoftware
{
292 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
293 ResourceProvider::ResourceId resource_id
);
294 ~ScopedWriteLockSoftware();
296 SkCanvas
* sk_canvas() { return sk_canvas_
.get(); }
297 bool valid() const { return !!sk_bitmap_
.getPixels(); }
300 ResourceProvider
* resource_provider_
;
301 ResourceProvider::ResourceId resource_id_
;
303 scoped_ptr
<SkCanvas
> sk_canvas_
;
305 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
308 class Fence
: public base::RefCounted
<Fence
> {
311 virtual bool HasPassed() = 0;
314 friend class base::RefCounted
<Fence
>;
318 DISALLOW_COPY_AND_ASSIGN(Fence
);
321 // Returns a canvas for direct rasterization.
322 // Call Unmap before the resource can be read or used for compositing.
323 // It is used for direct gpu rasterization.
324 SkCanvas
* MapDirectRasterBuffer(ResourceId id
);
325 void UnmapDirectRasterBuffer(ResourceId id
);
327 // Returns a canvas backed by an image buffer. UnmapImageRasterBuffer
328 // returns true if canvas was written to while mapped.
329 // Rasterizing to the canvas writes the content into the image buffer,
330 // which is internally bound to the underlying resource when read.
331 // Call Unmap before the resource can be read or used for compositing.
332 // It is used by ImageRasterWorkerPool.
333 SkCanvas
* MapImageRasterBuffer(ResourceId id
);
334 bool UnmapImageRasterBuffer(ResourceId id
);
336 // Returns a canvas backed by pixel buffer. UnmapPixelRasterBuffer
337 // returns true if canvas was written to while mapped.
338 // The pixel buffer needs to be uploaded to the underlying resource
339 // using BeginSetPixels before the resouce can be used for compositing.
340 // It is used by PixelRasterWorkerPool.
341 void AcquirePixelRasterBuffer(ResourceId id
);
342 void ReleasePixelRasterBuffer(ResourceId id
);
343 SkCanvas
* MapPixelRasterBuffer(ResourceId id
);
344 bool UnmapPixelRasterBuffer(ResourceId id
);
346 // Asynchronously update pixels from acquired pixel buffer.
347 void BeginSetPixels(ResourceId id
);
348 void ForceSetPixelsToComplete(ResourceId id
);
349 bool DidSetPixelsComplete(ResourceId id
);
351 // For tests only! This prevents detecting uninitialized reads.
352 // Use SetPixels or LockForWrite to allocate implicitly.
353 void AllocateForTesting(ResourceId id
);
356 void CreateForTesting(ResourceId id
);
358 GLenum
TargetForTesting(ResourceId id
);
360 // Sets the current read fence. If a resource is locked for read
361 // and has read fences enabled, the resource will not allow writes
362 // until this fence has passed.
363 void SetReadLockFence(scoped_refptr
<Fence
> fence
) {
364 current_read_lock_fence_
= fence
;
366 Fence
* GetReadLockFence() { return current_read_lock_fence_
.get(); }
368 // Enable read lock fences for a specific resource.
369 void EnableReadLockFences(ResourceProvider::ResourceId id
, bool enable
);
371 // Indicates if we can currently lock this resource for write.
372 bool CanLockForWrite(ResourceId id
);
374 // Copy pixels from source to destination.
375 void CopyResource(ResourceId source_id
, ResourceId dest_id
);
377 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
380 class DirectRasterBuffer
;
381 class ImageRasterBuffer
;
382 class PixelRasterBuffer
;
385 enum Origin
{ Internal
, External
, Delegated
};
389 Resource(unsigned texture_id
,
390 const gfx::Size
& size
,
396 TextureUsageHint hint
,
397 ResourceFormat format
);
398 Resource(uint8_t* pixels
,
399 SharedBitmap
* bitmap
,
400 const gfx::Size
& size
,
404 Resource(const SharedBitmapId
& bitmap_id
,
405 const gfx::Size
& size
,
412 // Pixel buffer used for set pixels without unnecessary copying.
413 unsigned gl_pixel_buffer_id
;
414 // Query used to determine when asynchronous set pixels complete.
415 unsigned gl_upload_query_id
;
416 // Query used to determine when read lock fence has passed.
417 unsigned gl_read_lock_query_id
;
418 TextureMailbox mailbox
;
419 ReleaseCallback release_callback
;
421 int lock_for_read_count
;
424 bool dirty_image
: 1;
425 bool locked_for_write
: 1;
427 bool marked_for_deletion
: 1;
428 bool pending_set_pixels
: 1;
429 bool set_pixels_completion_forced
: 1;
431 bool enable_read_lock_fences
: 1;
432 bool has_shared_bitmap_id
: 1;
433 bool allow_overlay
: 1;
434 scoped_refptr
<Fence
> read_lock_fence
;
438 // TODO(skyostil): Use a separate sampler object for filter state.
439 GLenum original_filter
;
442 unsigned bound_image_id
;
445 TextureUsageHint hint
;
447 ResourceFormat format
;
448 SharedBitmapId shared_bitmap_id
;
449 SharedBitmap
* shared_bitmap
;
450 linked_ptr
<DirectRasterBuffer
> direct_raster_buffer
;
451 linked_ptr
<ImageRasterBuffer
> image_raster_buffer
;
452 linked_ptr
<PixelRasterBuffer
> pixel_raster_buffer
;
454 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
458 virtual ~RasterBuffer();
460 SkCanvas
* LockForWrite();
461 // Returns true if canvas was written to while locked.
462 bool UnlockForWrite();
465 RasterBuffer(const Resource
* resource
, ResourceProvider
* resource_provider
);
466 const Resource
* resource() const { return resource_
; }
467 ResourceProvider
* resource_provider() const { return resource_provider_
; }
469 virtual SkCanvas
* DoLockForWrite() = 0;
470 virtual bool DoUnlockForWrite() = 0;
473 const Resource
* resource_
;
474 ResourceProvider
* resource_provider_
;
475 SkCanvas
* locked_canvas_
;
476 int canvas_save_count_
;
479 class DirectRasterBuffer
: public RasterBuffer
{
481 DirectRasterBuffer(const Resource
* resource
,
482 ResourceProvider
* resource_provider
);
483 virtual ~DirectRasterBuffer();
486 virtual SkCanvas
* DoLockForWrite() OVERRIDE
;
487 virtual bool DoUnlockForWrite() OVERRIDE
;
488 skia::RefPtr
<SkSurface
> CreateSurface();
491 skia::RefPtr
<SkSurface
> surface_
;
492 uint32_t surface_generation_id_
;
494 DISALLOW_COPY_AND_ASSIGN(DirectRasterBuffer
);
497 class BitmapRasterBuffer
: public RasterBuffer
{
499 virtual ~BitmapRasterBuffer();
502 BitmapRasterBuffer(const Resource
* resource
,
503 ResourceProvider
* resource_provider
);
505 virtual SkCanvas
* DoLockForWrite() OVERRIDE
;
506 virtual bool DoUnlockForWrite() OVERRIDE
;
508 virtual uint8_t* MapBuffer(int* stride
) = 0;
509 virtual void UnmapBuffer() = 0;
512 uint8_t* mapped_buffer_
;
513 SkBitmap raster_bitmap_
;
514 uint32_t raster_bitmap_generation_id_
;
515 skia::RefPtr
<SkCanvas
> raster_canvas_
;
518 class ImageRasterBuffer
: public BitmapRasterBuffer
{
520 ImageRasterBuffer(const Resource
* resource
,
521 ResourceProvider
* resource_provider
);
522 virtual ~ImageRasterBuffer();
525 virtual uint8_t* MapBuffer(int* stride
) OVERRIDE
;
526 virtual void UnmapBuffer() OVERRIDE
;
529 DISALLOW_COPY_AND_ASSIGN(ImageRasterBuffer
);
532 class PixelRasterBuffer
: public BitmapRasterBuffer
{
534 PixelRasterBuffer(const Resource
* resource
,
535 ResourceProvider
* resource_provider
);
536 virtual ~PixelRasterBuffer();
539 virtual uint8_t* MapBuffer(int* stride
) OVERRIDE
;
540 virtual void UnmapBuffer() OVERRIDE
;
543 DISALLOW_COPY_AND_ASSIGN(PixelRasterBuffer
);
546 static bool CompareResourceMapIteratorsByChildId(
547 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& a
,
548 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& b
);
554 ResourceIdMap child_to_parent_map
;
555 ResourceIdMap parent_to_child_map
;
556 ReturnCallback return_callback
;
557 ResourceIdSet in_use_resources
;
558 bool marked_for_deletion
;
560 typedef base::hash_map
<int, Child
> ChildMap
;
562 bool ReadLockFenceHasPassed(const Resource
* resource
) {
563 return !resource
->read_lock_fence
.get() ||
564 resource
->read_lock_fence
->HasPassed();
567 ResourceProvider(OutputSurface
* output_surface
,
568 SharedBitmapManager
* shared_bitmap_manager
,
569 int highp_threshold_min
,
570 bool use_rgba_4444_texture_format
,
571 size_t id_allocation_chunk_size
);
573 void CleanUpGLIfNeeded();
575 Resource
* GetResource(ResourceId id
);
576 const Resource
* LockForRead(ResourceId id
);
577 void UnlockForRead(ResourceId id
);
578 const Resource
* LockForWrite(ResourceId id
);
579 void UnlockForWrite(ResourceId id
);
580 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
581 const Resource
* resource
);
583 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
585 TransferableResource
* resource
);
590 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
591 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
593 const ResourceIdArray
& unused
);
594 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
595 void LazyCreate(Resource
* resource
);
596 void LazyAllocate(Resource
* resource
);
598 // TODO(alokp): Move the implementation to PixelRasterBuffer.
599 // Acquire pixel buffer for resource. The pixel buffer can be used to
600 // set resource pixels without performing unnecessary copying.
601 void AcquirePixelBuffer(Resource
* resource
);
602 void ReleasePixelBuffer(Resource
* resource
);
603 // Map/unmap the acquired pixel buffer.
604 uint8_t* MapPixelBuffer(const Resource
* resource
, int* stride
);
605 void UnmapPixelBuffer(const Resource
* resource
);
607 // TODO(alokp): Move the implementation to ImageRasterBuffer.
608 // Acquire and release an image. The image allows direct
609 // manipulation of texture memory.
610 void AcquireImage(Resource
* resource
);
611 void ReleaseImage(Resource
* resource
);
612 // Maps the acquired image so that its pixels could be modified.
613 // Unmap is called when all pixels are set.
614 uint8_t* MapImage(const Resource
* resource
, int* stride
);
615 void UnmapImage(const Resource
* resource
);
617 void BindImageForSampling(Resource
* resource
);
618 // Binds the given GL resource to a texture target for sampling using the
619 // specified filter for both minification and magnification. Returns the
620 // texture target used. The resource must be locked for reading.
621 GLenum
BindForSampling(ResourceProvider::ResourceId resource_id
,
625 // Returns NULL if the output_surface_ does not have a ContextProvider.
626 gpu::gles2::GLES2Interface
* ContextGL() const;
627 class GrContext
* GrContext() const;
629 OutputSurface
* output_surface_
;
630 SharedBitmapManager
* shared_bitmap_manager_
;
631 bool lost_output_surface_
;
632 int highp_threshold_min_
;
634 ResourceMap resources_
;
638 ResourceType default_resource_type_
;
639 bool use_texture_storage_ext_
;
640 bool use_texture_usage_hint_
;
641 bool use_compressed_texture_etc1_
;
642 scoped_ptr
<TextureUploader
> texture_uploader_
;
643 int max_texture_size_
;
644 ResourceFormat best_texture_format_
;
646 base::ThreadChecker thread_checker_
;
648 scoped_refptr
<Fence
> current_read_lock_fence_
;
649 bool use_rgba_4444_texture_format_
;
651 const size_t id_allocation_chunk_size_
;
652 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
653 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
655 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
659 // TODO(epenner): Move these format conversions to resource_format.h
660 // once that builds on mac (npapi.h currently #includes OpenGL.h).
661 inline unsigned BitsPerPixel(ResourceFormat format
) {
662 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
663 static const unsigned format_bits_per_pixel
[RESOURCE_FORMAT_MAX
+ 1] = {
671 return format_bits_per_pixel
[format
];
674 inline GLenum
GLDataType(ResourceFormat format
) {
675 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
676 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
677 GL_UNSIGNED_BYTE
, // RGBA_8888
678 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
679 GL_UNSIGNED_BYTE
, // BGRA_8888
680 GL_UNSIGNED_BYTE
, // LUMINANCE_8
681 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
682 GL_UNSIGNED_BYTE
// ETC1
684 return format_gl_data_type
[format
];
687 inline GLenum
GLDataFormat(ResourceFormat format
) {
688 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
689 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
690 GL_RGBA
, // RGBA_8888
691 GL_RGBA
, // RGBA_4444
692 GL_BGRA_EXT
, // BGRA_8888
693 GL_LUMINANCE
, // LUMINANCE_8
695 GL_ETC1_RGB8_OES
// ETC1
697 return format_gl_data_format
[format
];
700 inline GLenum
GLInternalFormat(ResourceFormat format
) {
701 return GLDataFormat(format
);
706 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_