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_impl.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_impl.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/geometry/size.h"
39 class GpuMemoryBufferManager
;
46 class GpuMemoryBuffer
;
52 class BlockingTaskRunner
;
55 class SharedBitmapManager
;
56 class TextureUploader
;
58 // This class is not thread-safe and can only be called from the thread it was
59 // created on (in practice, the impl thread).
60 class CC_EXPORT ResourceProvider
{
65 typedef unsigned ResourceId
;
66 typedef std::vector
<ResourceId
> ResourceIdArray
;
67 typedef std::set
<ResourceId
> ResourceIdSet
;
68 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
70 TextureHintDefault
= 0x0,
71 TextureHintImmutable
= 0x1,
72 TextureHintFramebuffer
= 0x2,
73 TextureHintImmutableFramebuffer
=
74 TextureHintImmutable
| TextureHintFramebuffer
82 static scoped_ptr
<ResourceProvider
> Create(
83 OutputSurface
* output_surface
,
84 SharedBitmapManager
* shared_bitmap_manager
,
85 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
86 BlockingTaskRunner
* blocking_main_thread_task_runner
,
87 int highp_threshold_min
,
88 bool use_rgba_4444_texture_format
,
89 size_t id_allocation_chunk_size
);
90 virtual ~ResourceProvider();
92 void InitializeSoftware();
95 void DidLoseOutputSurface() { lost_output_surface_
= true; }
97 int max_texture_size() const { return max_texture_size_
; }
98 ResourceFormat
memory_efficient_texture_format() const {
99 return use_rgba_4444_texture_format_
? RGBA_4444
: best_texture_format_
;
101 ResourceFormat
best_texture_format() const { return best_texture_format_
; }
102 ResourceFormat
yuv_resource_format() const { return yuv_resource_format_
; }
103 bool use_sync_query() const { return use_sync_query_
; }
104 size_t num_resources() const { return resources_
.size(); }
106 // Checks whether a resource is in use by a consumer.
107 bool InUseByConsumer(ResourceId id
);
109 bool IsLost(ResourceId id
);
110 bool AllowOverlay(ResourceId id
);
112 // Producer interface.
114 ResourceType
default_resource_type() const { return default_resource_type_
; }
115 ResourceType
GetResourceType(ResourceId id
);
117 // Creates a resource of the default resource type.
118 ResourceId
CreateResource(const gfx::Size
& size
,
121 ResourceFormat format
);
123 // Creates a resource which is tagged as being managed for GPU memory
124 // accounting purposes.
125 ResourceId
CreateManagedResource(const gfx::Size
& size
,
129 ResourceFormat format
);
131 // You can also explicitly create a specific resource type.
132 ResourceId
CreateGLTexture(const gfx::Size
& size
,
137 ResourceFormat format
);
139 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
140 // Wraps an IOSurface into a GL resource.
141 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
142 unsigned io_surface_id
);
144 // Wraps an external texture mailbox into a GL resource.
145 ResourceId
CreateResourceFromTextureMailbox(
146 const TextureMailbox
& mailbox
,
147 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
149 void DeleteResource(ResourceId id
);
151 // Update pixels from image, copying source_rect (in image) to dest_offset (in
153 void SetPixels(ResourceId id
,
154 const uint8_t* image
,
155 const gfx::Rect
& image_rect
,
156 const gfx::Rect
& source_rect
,
157 const gfx::Vector2d
& dest_offset
);
159 // Check upload status.
160 size_t NumBlockingUploads();
161 void MarkPendingUploadsAsNonBlocking();
162 size_t EstimatedUploadsPerTick();
164 void ReleaseCachedData();
165 base::TimeTicks
EstimatedUploadCompletionTime(size_t uploads_per_tick
);
167 // Only flush the command buffer if supported.
168 // Returns true if the shallow flush occurred, false otherwise.
169 bool ShallowFlushIfSupported();
171 // Creates accounting for a child. Returns a child ID.
172 int CreateChild(const ReturnCallback
& return_callback
);
174 // Destroys accounting for the child, deleting all accounted resources.
175 void DestroyChild(int child
);
177 // Gets the child->parent resource ID map.
178 const ResourceIdMap
& GetChildToParentMap(int child
) const;
180 // Prepares resources to be transfered to the parent, moving them to
181 // mailboxes and serializing meta-data into TransferableResources.
182 // Resources are not removed from the ResourceProvider, but are marked as
184 void PrepareSendToParent(const ResourceIdArray
& resources
,
185 TransferableResourceArray
* transferable_resources
);
187 // Receives resources from a child, moving them from mailboxes. Resource IDs
188 // passed are in the child namespace, and will be translated to the parent
189 // namespace, added to the child->parent map.
190 // This adds the resources to the working set in the ResourceProvider without
191 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
192 // after calling this method to do that. All calls to ReceiveFromChild should
193 // be followed by a DeclareUsedResourcesFromChild.
194 // NOTE: if the sync_point is set on any TransferableResource, this will
196 void ReceiveFromChild(
197 int child
, const TransferableResourceArray
& transferable_resources
);
199 // Once a set of resources have been received, they may or may not be used.
200 // This declares what set of resources are currently in use from the child,
201 // releasing any other resources back to the child.
202 void DeclareUsedResourcesFromChild(
204 const ResourceIdArray
& resources_from_child
);
206 // Receives resources from the parent, moving them from mailboxes. Resource
207 // IDs passed are in the child namespace.
208 // NOTE: if the sync_point is set on any TransferableResource, this will
210 void ReceiveReturnsFromParent(
211 const ReturnedResourceArray
& transferable_resources
);
213 // The following lock classes are part of the ResourceProvider API and are
214 // needed to read and write the resource contents. The user must ensure
215 // that they only use GL locks on GL resources, etc, and this is enforced
217 class CC_EXPORT ScopedReadLockGL
{
219 ScopedReadLockGL(ResourceProvider
* resource_provider
,
220 ResourceProvider::ResourceId resource_id
);
221 virtual ~ScopedReadLockGL();
223 unsigned texture_id() const { return texture_id_
; }
226 ResourceProvider
* resource_provider_
;
227 ResourceProvider::ResourceId resource_id_
;
230 unsigned texture_id_
;
232 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
235 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
237 ScopedSamplerGL(ResourceProvider
* resource_provider
,
238 ResourceProvider::ResourceId resource_id
,
240 ScopedSamplerGL(ResourceProvider
* resource_provider
,
241 ResourceProvider::ResourceId resource_id
,
244 ~ScopedSamplerGL() override
;
246 GLenum
target() const { return target_
; }
252 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
255 class CC_EXPORT ScopedWriteLockGL
{
257 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
258 ResourceProvider::ResourceId resource_id
);
259 ~ScopedWriteLockGL();
261 unsigned texture_id() const { return texture_id_
; }
264 ResourceProvider
* resource_provider_
;
265 ResourceProvider::Resource
* resource_
;
266 unsigned texture_id_
;
268 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
271 class CC_EXPORT ScopedReadLockSoftware
{
273 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
274 ResourceProvider::ResourceId resource_id
);
275 ~ScopedReadLockSoftware();
277 const SkBitmap
* sk_bitmap() const {
281 GLint
wrap_mode() const { return wrap_mode_
; }
283 bool valid() const { return !!sk_bitmap_
.getPixels(); }
286 ResourceProvider
* resource_provider_
;
287 ResourceProvider::ResourceId resource_id_
;
291 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
294 class CC_EXPORT ScopedWriteLockSoftware
{
296 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
297 ResourceProvider::ResourceId resource_id
);
298 ~ScopedWriteLockSoftware();
300 SkBitmap
& sk_bitmap() { return sk_bitmap_
; }
301 bool valid() const { return !!sk_bitmap_
.getPixels(); }
304 ResourceProvider
* resource_provider_
;
305 ResourceProvider::Resource
* resource_
;
307 base::ThreadChecker thread_checker_
;
309 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
312 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer
{
314 ScopedWriteLockGpuMemoryBuffer(ResourceProvider
* resource_provider
,
315 ResourceProvider::ResourceId resource_id
);
316 ~ScopedWriteLockGpuMemoryBuffer();
318 gfx::GpuMemoryBuffer
* GetGpuMemoryBuffer();
321 ResourceProvider
* resource_provider_
;
322 ResourceProvider::Resource
* resource_
;
323 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
324 gfx::GpuMemoryBuffer
* gpu_memory_buffer_
;
326 ResourceFormat format_
;
327 base::ThreadChecker thread_checker_
;
329 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer
);
332 class CC_EXPORT ScopedWriteLockGr
{
334 ScopedWriteLockGr(ResourceProvider
* resource_provider
,
335 ResourceProvider::ResourceId resource_id
);
336 ~ScopedWriteLockGr();
338 SkSurface
* GetSkSurface(bool use_distance_field_text
,
339 bool can_use_lcd_text
);
342 bool SurfaceHasMatchingProperties(bool use_distance_field_text
,
343 bool can_use_lcd_text
) const;
345 ResourceProvider
* resource_provider_
;
346 ResourceProvider::Resource
* resource_
;
347 base::ThreadChecker thread_checker_
;
349 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr
);
352 class Fence
: public base::RefCounted
<Fence
> {
356 virtual void Set() = 0;
357 virtual bool HasPassed() = 0;
358 virtual void Wait() = 0;
361 friend class base::RefCounted
<Fence
>;
365 DISALLOW_COPY_AND_ASSIGN(Fence
);
368 class SynchronousFence
: public ResourceProvider::Fence
{
370 explicit SynchronousFence(gpu::gles2::GLES2Interface
* gl
);
372 // Overridden from Fence:
374 bool HasPassed() override
;
375 void Wait() override
;
377 // Returns true if fence has been set but not yet synchornized.
378 bool has_synchronized() const { return has_synchronized_
; }
381 ~SynchronousFence() override
;
385 gpu::gles2::GLES2Interface
* gl_
;
386 bool has_synchronized_
;
388 DISALLOW_COPY_AND_ASSIGN(SynchronousFence
);
391 // Acquire pixel buffer for resource. The pixel buffer can be used to
392 // set resource pixels without performing unnecessary copying.
393 void AcquirePixelBuffer(ResourceId resource
);
394 void ReleasePixelBuffer(ResourceId resource
);
395 // Map/unmap the acquired pixel buffer.
396 uint8_t* MapPixelBuffer(ResourceId id
, int* stride
);
397 void UnmapPixelBuffer(ResourceId id
);
398 // Asynchronously update pixels from acquired pixel buffer.
399 void BeginSetPixels(ResourceId id
);
400 void ForceSetPixelsToComplete(ResourceId id
);
401 bool DidSetPixelsComplete(ResourceId id
);
403 // For tests only! This prevents detecting uninitialized reads.
404 // Use SetPixels or LockForWrite to allocate implicitly.
405 void AllocateForTesting(ResourceId id
);
408 void CreateForTesting(ResourceId id
);
410 GLenum
TargetForTesting(ResourceId id
);
412 // Sets the current read fence. If a resource is locked for read
413 // and has read fences enabled, the resource will not allow writes
414 // until this fence has passed.
415 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
417 // Indicates if we can currently lock this resource for write.
418 bool CanLockForWrite(ResourceId id
);
420 // Copy pixels from source to destination.
421 void CopyResource(ResourceId source_id
, ResourceId dest_id
);
423 void WaitSyncPointIfNeeded(ResourceId id
);
425 void WaitReadLockIfNeeded(ResourceId id
);
427 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
431 enum Origin
{ Internal
, External
, Delegated
};
435 Resource(unsigned texture_id
,
436 const gfx::Size
& size
,
443 ResourceFormat format
);
444 Resource(uint8_t* pixels
,
445 SharedBitmap
* bitmap
,
446 const gfx::Size
& size
,
450 Resource(const SharedBitmapId
& bitmap_id
,
451 const gfx::Size
& size
,
458 // Pixel buffer used for set pixels without unnecessary copying.
459 unsigned gl_pixel_buffer_id
;
460 // Query used to determine when asynchronous set pixels complete.
461 unsigned gl_upload_query_id
;
462 // Query used to determine when read lock fence has passed.
463 unsigned gl_read_lock_query_id
;
464 TextureMailbox mailbox
;
465 ReleaseCallbackImpl release_callback_impl
;
467 int lock_for_read_count
;
470 bool dirty_image
: 1;
471 bool locked_for_write
: 1;
473 bool marked_for_deletion
: 1;
474 bool pending_set_pixels
: 1;
475 bool set_pixels_completion_forced
: 1;
477 bool read_lock_fences_enabled
: 1;
478 bool has_shared_bitmap_id
: 1;
479 bool allow_overlay
: 1;
480 scoped_refptr
<Fence
> read_lock_fence
;
484 // TODO(skyostil): Use a separate sampler object for filter state.
485 GLenum original_filter
;
488 unsigned bound_image_id
;
493 ResourceFormat format
;
494 SharedBitmapId shared_bitmap_id
;
495 SharedBitmap
* shared_bitmap
;
496 gfx::GpuMemoryBuffer
* gpu_memory_buffer
;
497 skia::RefPtr
<SkSurface
> sk_surface
;
499 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
501 static bool CompareResourceMapIteratorsByChildId(
502 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& a
,
503 const std::pair
<ReturnedResource
, ResourceMap::iterator
>& b
);
509 ResourceIdMap child_to_parent_map
;
510 ResourceIdMap parent_to_child_map
;
511 ReturnCallback return_callback
;
512 ResourceIdSet in_use_resources
;
513 bool marked_for_deletion
;
515 typedef base::hash_map
<int, Child
> ChildMap
;
517 bool ReadLockFenceHasPassed(const Resource
* resource
) {
518 return !resource
->read_lock_fence
.get() ||
519 resource
->read_lock_fence
->HasPassed();
522 ResourceProvider(OutputSurface
* output_surface
,
523 SharedBitmapManager
* shared_bitmap_manager
,
524 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
525 BlockingTaskRunner
* blocking_main_thread_task_runner
,
526 int highp_threshold_min
,
527 bool use_rgba_4444_texture_format
,
528 size_t id_allocation_chunk_size
);
530 void CleanUpGLIfNeeded();
532 Resource
* GetResource(ResourceId id
);
533 const Resource
* LockForRead(ResourceId id
);
534 void UnlockForRead(ResourceId id
);
535 Resource
* LockForWrite(ResourceId id
);
536 void UnlockForWrite(Resource
* resource
);
538 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
539 const Resource
* resource
);
541 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
543 TransferableResource
* resource
);
548 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
549 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
551 const ResourceIdArray
& unused
);
552 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
553 void LazyCreate(Resource
* resource
);
554 void LazyAllocate(Resource
* resource
);
556 void BindImageForSampling(Resource
* resource
);
557 // Binds the given GL resource to a texture target for sampling using the
558 // specified filter for both minification and magnification. Returns the
559 // texture target used. The resource must be locked for reading.
560 GLenum
BindForSampling(ResourceId resource_id
, GLenum unit
, GLenum filter
);
562 // Returns NULL if the output_surface_ does not have a ContextProvider.
563 gpu::gles2::GLES2Interface
* ContextGL() const;
564 class GrContext
* GrContext() const;
566 OutputSurface
* output_surface_
;
567 SharedBitmapManager
* shared_bitmap_manager_
;
568 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
569 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
570 bool lost_output_surface_
;
571 int highp_threshold_min_
;
573 ResourceMap resources_
;
577 ResourceType default_resource_type_
;
578 bool use_texture_storage_ext_
;
579 bool use_texture_format_bgra_
;
580 bool use_texture_usage_hint_
;
581 bool use_compressed_texture_etc1_
;
582 ResourceFormat yuv_resource_format_
;
583 scoped_ptr
<TextureUploader
> texture_uploader_
;
584 int max_texture_size_
;
585 ResourceFormat best_texture_format_
;
587 base::ThreadChecker thread_checker_
;
589 scoped_refptr
<Fence
> current_read_lock_fence_
;
590 bool use_rgba_4444_texture_format_
;
592 const size_t id_allocation_chunk_size_
;
593 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
594 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
596 bool use_sync_query_
;
597 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
598 scoped_refptr
<SynchronousFence
> synchronous_fence_
;
600 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
603 // TODO(epenner): Move these format conversions to resource_format.h
604 // once that builds on mac (npapi.h currently #includes OpenGL.h).
605 inline unsigned BitsPerPixel(ResourceFormat format
) {
624 inline GLenum
GLDataType(ResourceFormat format
) {
625 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
626 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
627 GL_UNSIGNED_BYTE
, // RGBA_8888
628 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
629 GL_UNSIGNED_BYTE
, // BGRA_8888
630 GL_UNSIGNED_BYTE
, // ALPHA_8
631 GL_UNSIGNED_BYTE
, // LUMINANCE_8
632 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
633 GL_UNSIGNED_BYTE
, // ETC1
634 GL_UNSIGNED_BYTE
// RED_8
636 return format_gl_data_type
[format
];
639 inline GLenum
GLDataFormat(ResourceFormat format
) {
640 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
641 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
642 GL_RGBA
, // RGBA_8888
643 GL_RGBA
, // RGBA_4444
644 GL_BGRA_EXT
, // BGRA_8888
646 GL_LUMINANCE
, // LUMINANCE_8
648 GL_ETC1_RGB8_OES
, // ETC1
651 return format_gl_data_format
[format
];
654 inline GLenum
GLInternalFormat(ResourceFormat format
) {
655 return GLDataFormat(format
);
660 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_