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/base/resource_id.h"
22 #include "cc/output/context_provider.h"
23 #include "cc/output/output_surface.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/geometry/size.h"
40 class GpuMemoryBufferManager
;
47 class GpuMemoryBuffer
;
53 class BlockingTaskRunner
;
56 class SharedBitmapManager
;
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 std::vector
<ResourceId
> ResourceIdArray
;
66 typedef base::hash_set
<ResourceId
> ResourceIdSet
;
67 typedef base::hash_map
<ResourceId
, ResourceId
> ResourceIdMap
;
69 TEXTURE_HINT_DEFAULT
= 0x0,
70 TEXTURE_HINT_IMMUTABLE
= 0x1,
71 TEXTURE_HINT_FRAMEBUFFER
= 0x2,
72 TEXTURE_HINT_IMMUTABLE_FRAMEBUFFER
=
73 TEXTURE_HINT_IMMUTABLE
| TEXTURE_HINT_FRAMEBUFFER
76 RESOURCE_TYPE_GL_TEXTURE
,
80 static scoped_ptr
<ResourceProvider
> Create(
81 OutputSurface
* output_surface
,
82 SharedBitmapManager
* shared_bitmap_manager
,
83 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
84 BlockingTaskRunner
* blocking_main_thread_task_runner
,
85 int highp_threshold_min
,
86 bool use_rgba_4444_texture_format
,
87 size_t id_allocation_chunk_size
,
88 bool use_persistent_map_for_gpu_memory_buffers
);
89 virtual ~ResourceProvider();
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 ResourceFormat
best_render_buffer_format() const {
99 return best_render_buffer_format_
;
101 ResourceFormat
yuv_resource_format() const { return yuv_resource_format_
; }
102 bool use_sync_query() const { return use_sync_query_
; }
103 bool use_persistent_map_for_gpu_memory_buffers() const {
104 return use_persistent_map_for_gpu_memory_buffers_
;
106 size_t num_resources() const { return resources_
.size(); }
108 // Checks whether a resource is in use by a consumer.
109 bool InUseByConsumer(ResourceId id
);
111 bool IsLost(ResourceId id
);
113 void EnableReadLockFencesForTesting(ResourceId id
);
115 // Producer interface.
117 ResourceType
default_resource_type() const { return default_resource_type_
; }
118 ResourceType
GetResourceType(ResourceId id
);
120 // Creates a resource of the default resource type.
121 ResourceId
CreateResource(const gfx::Size
& size
,
124 ResourceFormat format
);
126 // Creates a resource which is tagged as being managed for GPU memory
127 // accounting purposes.
128 ResourceId
CreateManagedResource(const gfx::Size
& size
,
132 ResourceFormat format
);
134 // You can also explicitly create a specific resource type.
135 ResourceId
CreateGLTexture(const gfx::Size
& size
,
140 ResourceFormat format
);
142 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
143 // Wraps an IOSurface into a GL resource.
144 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
145 unsigned io_surface_id
);
147 // Wraps an external texture mailbox into a GL resource.
148 ResourceId
CreateResourceFromTextureMailbox(
149 const TextureMailbox
& mailbox
,
150 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
152 ResourceId
CreateResourceFromTextureMailbox(
153 const TextureMailbox
& mailbox
,
154 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
,
155 bool read_lock_fences_enabled
);
157 void DeleteResource(ResourceId id
);
159 // Update pixels from image, copying source_rect (in image) to dest_offset (in
161 void CopyToResource(ResourceId id
,
162 const uint8_t* image
,
163 const gfx::Size
& image_size
);
165 // Only flush the command buffer if supported.
166 // Returns true if the shallow flush occurred, false otherwise.
167 bool ShallowFlushIfSupported();
169 // Creates accounting for a child. Returns a child ID.
170 int CreateChild(const ReturnCallback
& return_callback
);
172 // Destroys accounting for the child, deleting all accounted resources.
173 void DestroyChild(int child
);
175 // Sets whether resources need sync points set on them when returned to this
176 // child. Defaults to true.
177 void SetChildNeedsSyncPoints(int child
, bool needs_sync_points
);
179 // Gets the child->parent resource ID map.
180 const ResourceIdMap
& GetChildToParentMap(int child
) const;
182 // Prepares resources to be transfered to the parent, moving them to
183 // mailboxes and serializing meta-data into TransferableResources.
184 // Resources are not removed from the ResourceProvider, but are marked as
186 void PrepareSendToParent(const ResourceIdArray
& resources
,
187 TransferableResourceArray
* transferable_resources
);
189 // Receives resources from a child, moving them from mailboxes. Resource IDs
190 // passed are in the child namespace, and will be translated to the parent
191 // namespace, added to the child->parent map.
192 // This adds the resources to the working set in the ResourceProvider without
193 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
194 // after calling this method to do that. All calls to ReceiveFromChild should
195 // be followed by a DeclareUsedResourcesFromChild.
196 // NOTE: if the sync_point is set on any TransferableResource, this will
198 void ReceiveFromChild(
199 int child
, const TransferableResourceArray
& transferable_resources
);
201 // Once a set of resources have been received, they may or may not be used.
202 // This declares what set of resources are currently in use from the child,
203 // releasing any other resources back to the child.
204 void DeclareUsedResourcesFromChild(int child
,
205 const ResourceIdSet
& resources_from_child
);
207 // Receives resources from the parent, moving them from mailboxes. Resource
208 // IDs passed are in the child namespace.
209 // NOTE: if the sync_point is set on any TransferableResource, this will
211 void ReceiveReturnsFromParent(
212 const ReturnedResourceArray
& transferable_resources
);
214 // The following lock classes are part of the ResourceProvider API and are
215 // needed to read and write the resource contents. The user must ensure
216 // that they only use GL locks on GL resources, etc, and this is enforced
218 class CC_EXPORT ScopedReadLockGL
{
220 ScopedReadLockGL(ResourceProvider
* resource_provider
,
221 ResourceId resource_id
);
222 virtual ~ScopedReadLockGL();
224 unsigned texture_id() const { return resource_
->gl_id
; }
225 GLenum
target() const { return resource_
->target
; }
228 ResourceProvider
* resource_provider_
;
229 ResourceId resource_id_
;
232 const ResourceProvider::Resource
* resource_
;
234 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
237 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
239 ScopedSamplerGL(ResourceProvider
* resource_provider
,
240 ResourceId resource_id
,
242 ScopedSamplerGL(ResourceProvider
* resource_provider
,
243 ResourceId resource_id
,
246 ~ScopedSamplerGL() override
;
248 GLenum
target() const { return target_
; }
254 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
257 class CC_EXPORT ScopedWriteLockGL
{
259 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
260 ResourceId resource_id
);
261 ~ScopedWriteLockGL();
263 unsigned texture_id() const { return texture_id_
; }
266 ResourceProvider
* resource_provider_
;
267 ResourceProvider::Resource
* resource_
;
268 unsigned texture_id_
;
270 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
273 class CC_EXPORT ScopedReadLockSoftware
{
275 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
276 ResourceId resource_id
);
277 ~ScopedReadLockSoftware();
279 const SkBitmap
* sk_bitmap() const {
283 GLint
wrap_mode() const { return wrap_mode_
; }
285 bool valid() const { return !!sk_bitmap_
.getPixels(); }
288 ResourceProvider
* resource_provider_
;
289 ResourceId resource_id_
;
293 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
296 class CC_EXPORT ScopedWriteLockSoftware
{
298 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
299 ResourceId resource_id
);
300 ~ScopedWriteLockSoftware();
302 SkBitmap
& sk_bitmap() { return sk_bitmap_
; }
303 bool valid() const { return !!sk_bitmap_
.getPixels(); }
306 ResourceProvider
* resource_provider_
;
307 ResourceProvider::Resource
* resource_
;
309 base::ThreadChecker thread_checker_
;
311 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
314 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer
{
316 ScopedWriteLockGpuMemoryBuffer(ResourceProvider
* resource_provider
,
317 ResourceId resource_id
);
318 ~ScopedWriteLockGpuMemoryBuffer();
320 gfx::GpuMemoryBuffer
* GetGpuMemoryBuffer();
323 ResourceProvider
* resource_provider_
;
324 ResourceProvider::Resource
* resource_
;
325 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
326 gfx::GpuMemoryBuffer
* gpu_memory_buffer_
;
328 ResourceFormat format_
;
329 base::ThreadChecker thread_checker_
;
331 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer
);
334 class CC_EXPORT ScopedWriteLockGr
{
336 ScopedWriteLockGr(ResourceProvider
* resource_provider
,
337 ResourceId resource_id
);
338 ~ScopedWriteLockGr();
340 void InitSkSurface(bool use_distance_field_text
,
341 bool can_use_lcd_text
,
342 int msaa_sample_count
);
343 void ReleaseSkSurface();
345 SkSurface
* sk_surface() { return sk_surface_
.get(); }
346 ResourceProvider::Resource
* resource() { return resource_
; }
349 ResourceProvider
* resource_provider_
;
350 ResourceProvider::Resource
* resource_
;
351 base::ThreadChecker thread_checker_
;
352 skia::RefPtr
<SkSurface
> sk_surface_
;
354 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr
);
357 class Fence
: public base::RefCounted
<Fence
> {
361 virtual void Set() = 0;
362 virtual bool HasPassed() = 0;
363 virtual void Wait() = 0;
366 friend class base::RefCounted
<Fence
>;
370 DISALLOW_COPY_AND_ASSIGN(Fence
);
373 class SynchronousFence
: public ResourceProvider::Fence
{
375 explicit SynchronousFence(gpu::gles2::GLES2Interface
* gl
);
377 // Overridden from Fence:
379 bool HasPassed() override
;
380 void Wait() override
;
382 // Returns true if fence has been set but not yet synchornized.
383 bool has_synchronized() const { return has_synchronized_
; }
386 ~SynchronousFence() override
;
390 gpu::gles2::GLES2Interface
* gl_
;
391 bool has_synchronized_
;
393 DISALLOW_COPY_AND_ASSIGN(SynchronousFence
);
396 // Acquire pixel buffer for resource. The pixel buffer can be used to
397 // set resource pixels without performing unnecessary copying.
398 void AcquirePixelBuffer(ResourceId resource
);
399 void ReleasePixelBuffer(ResourceId resource
);
400 // Map/unmap the acquired pixel buffer.
401 uint8_t* MapPixelBuffer(ResourceId id
, int* stride
);
402 void UnmapPixelBuffer(ResourceId id
);
403 // Asynchronously update pixels from acquired pixel buffer.
404 void BeginSetPixels(ResourceId id
);
405 void ForceSetPixelsToComplete(ResourceId id
);
406 bool DidSetPixelsComplete(ResourceId id
);
408 // For tests only! This prevents detecting uninitialized reads.
409 // Use SetPixels or LockForWrite to allocate implicitly.
410 void AllocateForTesting(ResourceId id
);
413 void CreateForTesting(ResourceId id
);
415 GLenum
TargetForTesting(ResourceId id
);
417 // Sets the current read fence. If a resource is locked for read
418 // and has read fences enabled, the resource will not allow writes
419 // until this fence has passed.
420 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
422 // Indicates if we can currently lock this resource for write.
423 bool CanLockForWrite(ResourceId id
);
425 // Copy |rect| pixels from source to destination.
426 void CopyResource(ResourceId source_id
,
428 const gfx::Rect
& rect
);
430 void WaitSyncPointIfNeeded(ResourceId id
);
432 void WaitReadLockIfNeeded(ResourceId id
);
434 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
436 OutputSurface
* output_surface() { return output_surface_
; }
438 void ValidateResource(ResourceId id
) const;
441 ResourceProvider(OutputSurface
* output_surface
,
442 SharedBitmapManager
* shared_bitmap_manager
,
443 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
444 BlockingTaskRunner
* blocking_main_thread_task_runner
,
445 int highp_threshold_min
,
446 bool use_rgba_4444_texture_format
,
447 size_t id_allocation_chunk_size
,
448 bool use_persistent_map_for_gpu_memory_buffers
);
453 enum Origin
{ INTERNAL
, EXTERNAL
, DELEGATED
};
456 Resource(unsigned texture_id
,
457 const gfx::Size
& size
,
464 ResourceFormat format
);
465 Resource(uint8_t* pixels
,
466 SharedBitmap
* bitmap
,
467 const gfx::Size
& size
,
471 Resource(const SharedBitmapId
& bitmap_id
,
472 const gfx::Size
& size
,
479 // Pixel buffer used for set pixels without unnecessary copying.
480 unsigned gl_pixel_buffer_id
;
481 // Query used to determine when asynchronous set pixels complete.
482 unsigned gl_upload_query_id
;
483 // Query used to determine when read lock fence has passed.
484 unsigned gl_read_lock_query_id
;
485 TextureMailbox mailbox
;
486 ReleaseCallbackImpl release_callback_impl
;
488 int lock_for_read_count
;
491 bool dirty_image
: 1;
492 bool locked_for_write
: 1;
494 bool marked_for_deletion
: 1;
495 bool pending_set_pixels
: 1;
496 bool set_pixels_completion_forced
: 1;
498 bool read_lock_fences_enabled
: 1;
499 bool has_shared_bitmap_id
: 1;
500 scoped_refptr
<Fence
> read_lock_fence
;
504 // TODO(skyostil): Use a separate sampler object for filter state.
505 GLenum original_filter
;
508 unsigned bound_image_id
;
513 ResourceFormat format
;
514 SharedBitmapId shared_bitmap_id
;
515 SharedBitmap
* shared_bitmap
;
516 gfx::GpuMemoryBuffer
* gpu_memory_buffer
;
518 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
524 ResourceIdMap child_to_parent_map
;
525 ResourceIdMap parent_to_child_map
;
526 ReturnCallback return_callback
;
527 bool marked_for_deletion
;
528 bool needs_sync_points
;
530 typedef base::hash_map
<int, Child
> ChildMap
;
532 bool ReadLockFenceHasPassed(const Resource
* resource
) {
533 return !resource
->read_lock_fence
.get() ||
534 resource
->read_lock_fence
->HasPassed();
537 Resource
* InsertResource(ResourceId id
, const Resource
& resource
);
538 Resource
* GetResource(ResourceId id
);
539 const Resource
* LockForRead(ResourceId id
);
540 void UnlockForRead(ResourceId id
);
541 Resource
* LockForWrite(ResourceId id
);
542 void UnlockForWrite(Resource
* resource
);
544 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
545 const Resource
* resource
);
547 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
549 TransferableResource
* resource
);
554 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
555 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
557 const ResourceIdArray
& unused
);
558 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
559 void LazyCreate(Resource
* resource
);
560 void LazyAllocate(Resource
* resource
);
562 void BindImageForSampling(Resource
* resource
);
563 // Binds the given GL resource to a texture target for sampling using the
564 // specified filter for both minification and magnification. Returns the
565 // texture target used. The resource must be locked for reading.
566 GLenum
BindForSampling(ResourceId resource_id
, GLenum unit
, GLenum filter
);
568 // Returns NULL if the output_surface_ does not have a ContextProvider.
569 gpu::gles2::GLES2Interface
* ContextGL() const;
570 class GrContext
* GrContext(bool worker_context
) const;
572 OutputSurface
* output_surface_
;
573 SharedBitmapManager
* shared_bitmap_manager_
;
574 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
575 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
576 bool lost_output_surface_
;
577 int highp_threshold_min_
;
579 ResourceMap resources_
;
583 ResourceType default_resource_type_
;
584 bool use_texture_storage_ext_
;
585 bool use_texture_format_bgra_
;
586 bool use_texture_usage_hint_
;
587 bool use_compressed_texture_etc1_
;
588 ResourceFormat yuv_resource_format_
;
589 int max_texture_size_
;
590 ResourceFormat best_texture_format_
;
591 ResourceFormat best_render_buffer_format_
;
593 base::ThreadChecker thread_checker_
;
595 scoped_refptr
<Fence
> current_read_lock_fence_
;
596 bool use_rgba_4444_texture_format_
;
598 const size_t id_allocation_chunk_size_
;
599 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
600 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
602 bool use_sync_query_
;
603 bool use_persistent_map_for_gpu_memory_buffers_
;
604 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
605 scoped_refptr
<SynchronousFence
> synchronous_fence_
;
607 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
610 // TODO(epenner): Move these format conversions to resource_format.h
611 // once that builds on mac (npapi.h currently #includes OpenGL.h).
612 inline int BitsPerPixel(ResourceFormat format
) {
631 inline GLenum
GLDataType(ResourceFormat format
) {
632 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
633 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
634 GL_UNSIGNED_BYTE
, // RGBA_8888
635 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
636 GL_UNSIGNED_BYTE
, // BGRA_8888
637 GL_UNSIGNED_BYTE
, // ALPHA_8
638 GL_UNSIGNED_BYTE
, // LUMINANCE_8
639 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
640 GL_UNSIGNED_BYTE
, // ETC1
641 GL_UNSIGNED_BYTE
// RED_8
643 return format_gl_data_type
[format
];
646 inline GLenum
GLDataFormat(ResourceFormat format
) {
647 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
648 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
649 GL_RGBA
, // RGBA_8888
650 GL_RGBA
, // RGBA_4444
651 GL_BGRA_EXT
, // BGRA_8888
653 GL_LUMINANCE
, // LUMINANCE_8
655 GL_ETC1_RGB8_OES
, // ETC1
658 return format_gl_data_format
[format
];
661 inline GLenum
GLInternalFormat(ResourceFormat format
) {
662 return GLDataFormat(format
);
667 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_