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 LoseResourceForTesting(ResourceId id
);
114 void EnableReadLockFencesForTesting(ResourceId id
);
116 // Producer interface.
118 ResourceType
default_resource_type() const { return default_resource_type_
; }
119 ResourceType
GetResourceType(ResourceId id
);
121 // Creates a resource of the default resource type.
122 ResourceId
CreateResource(const gfx::Size
& size
,
125 ResourceFormat format
);
127 // Creates a resource which is tagged as being managed for GPU memory
128 // accounting purposes.
129 ResourceId
CreateManagedResource(const gfx::Size
& size
,
133 ResourceFormat format
);
135 // You can also explicitly create a specific resource type.
136 ResourceId
CreateGLTexture(const gfx::Size
& size
,
141 ResourceFormat format
);
143 ResourceId
CreateBitmap(const gfx::Size
& size
, GLint wrap_mode
);
144 // Wraps an IOSurface into a GL resource.
145 ResourceId
CreateResourceFromIOSurface(const gfx::Size
& size
,
146 unsigned io_surface_id
);
148 // Wraps an external texture mailbox into a GL resource.
149 ResourceId
CreateResourceFromTextureMailbox(
150 const TextureMailbox
& mailbox
,
151 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
);
153 ResourceId
CreateResourceFromTextureMailbox(
154 const TextureMailbox
& mailbox
,
155 scoped_ptr
<SingleReleaseCallbackImpl
> release_callback_impl
,
156 bool read_lock_fences_enabled
);
158 void DeleteResource(ResourceId id
);
160 // Update pixels from image, copying source_rect (in image) to dest_offset (in
162 void CopyToResource(ResourceId id
,
163 const uint8_t* image
,
164 const gfx::Size
& image_size
);
166 // Only flush the command buffer if supported.
167 // Returns true if the shallow flush occurred, false otherwise.
168 bool ShallowFlushIfSupported();
170 // Creates accounting for a child. Returns a child ID.
171 int CreateChild(const ReturnCallback
& return_callback
);
173 // Destroys accounting for the child, deleting all accounted resources.
174 void DestroyChild(int child
);
176 // Sets whether resources need sync points set on them when returned to this
177 // child. Defaults to true.
178 void SetChildNeedsSyncPoints(int child
, bool needs_sync_points
);
180 // Gets the child->parent resource ID map.
181 const ResourceIdMap
& GetChildToParentMap(int child
) const;
183 // Prepares resources to be transfered to the parent, moving them to
184 // mailboxes and serializing meta-data into TransferableResources.
185 // Resources are not removed from the ResourceProvider, but are marked as
187 void PrepareSendToParent(const ResourceIdArray
& resources
,
188 TransferableResourceArray
* transferable_resources
);
190 // Receives resources from a child, moving them from mailboxes. Resource IDs
191 // passed are in the child namespace, and will be translated to the parent
192 // namespace, added to the child->parent map.
193 // This adds the resources to the working set in the ResourceProvider without
194 // declaring which resources are in use. Use DeclareUsedResourcesFromChild
195 // after calling this method to do that. All calls to ReceiveFromChild should
196 // be followed by a DeclareUsedResourcesFromChild.
197 // NOTE: if the sync_point is set on any TransferableResource, this will
199 void ReceiveFromChild(
200 int child
, const TransferableResourceArray
& transferable_resources
);
202 // Once a set of resources have been received, they may or may not be used.
203 // This declares what set of resources are currently in use from the child,
204 // releasing any other resources back to the child.
205 void DeclareUsedResourcesFromChild(int child
,
206 const ResourceIdSet
& resources_from_child
);
208 // Receives resources from the parent, moving them from mailboxes. Resource
209 // IDs passed are in the child namespace.
210 // NOTE: if the sync_point is set on any TransferableResource, this will
212 void ReceiveReturnsFromParent(
213 const ReturnedResourceArray
& transferable_resources
);
215 // The following lock classes are part of the ResourceProvider API and are
216 // needed to read and write the resource contents. The user must ensure
217 // that they only use GL locks on GL resources, etc, and this is enforced
219 class CC_EXPORT ScopedReadLockGL
{
221 ScopedReadLockGL(ResourceProvider
* resource_provider
,
222 ResourceId resource_id
);
223 virtual ~ScopedReadLockGL();
225 unsigned texture_id() const { return resource_
->gl_id
; }
226 GLenum
target() const { return resource_
->target
; }
229 ResourceProvider
* resource_provider_
;
230 ResourceId resource_id_
;
233 const ResourceProvider::Resource
* resource_
;
235 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockGL
);
238 class CC_EXPORT ScopedSamplerGL
: public ScopedReadLockGL
{
240 ScopedSamplerGL(ResourceProvider
* resource_provider
,
241 ResourceId resource_id
,
243 ScopedSamplerGL(ResourceProvider
* resource_provider
,
244 ResourceId resource_id
,
247 ~ScopedSamplerGL() override
;
249 GLenum
target() const { return target_
; }
255 DISALLOW_COPY_AND_ASSIGN(ScopedSamplerGL
);
258 class CC_EXPORT ScopedWriteLockGL
{
260 ScopedWriteLockGL(ResourceProvider
* resource_provider
,
261 ResourceId resource_id
);
262 ~ScopedWriteLockGL();
264 unsigned texture_id() const { return texture_id_
; }
267 ResourceProvider
* resource_provider_
;
268 ResourceProvider::Resource
* resource_
;
269 unsigned texture_id_
;
271 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGL
);
274 class CC_EXPORT ScopedReadLockSoftware
{
276 ScopedReadLockSoftware(ResourceProvider
* resource_provider
,
277 ResourceId resource_id
);
278 ~ScopedReadLockSoftware();
280 const SkBitmap
* sk_bitmap() const {
284 GLint
wrap_mode() const { return wrap_mode_
; }
286 bool valid() const { return !!sk_bitmap_
.getPixels(); }
289 ResourceProvider
* resource_provider_
;
290 ResourceId resource_id_
;
294 DISALLOW_COPY_AND_ASSIGN(ScopedReadLockSoftware
);
297 class CC_EXPORT ScopedWriteLockSoftware
{
299 ScopedWriteLockSoftware(ResourceProvider
* resource_provider
,
300 ResourceId resource_id
);
301 ~ScopedWriteLockSoftware();
303 SkBitmap
& sk_bitmap() { return sk_bitmap_
; }
304 bool valid() const { return !!sk_bitmap_
.getPixels(); }
307 ResourceProvider
* resource_provider_
;
308 ResourceProvider::Resource
* resource_
;
310 base::ThreadChecker thread_checker_
;
312 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockSoftware
);
315 class CC_EXPORT ScopedWriteLockGpuMemoryBuffer
{
317 ScopedWriteLockGpuMemoryBuffer(ResourceProvider
* resource_provider
,
318 ResourceId resource_id
);
319 ~ScopedWriteLockGpuMemoryBuffer();
321 gfx::GpuMemoryBuffer
* GetGpuMemoryBuffer();
324 ResourceProvider
* resource_provider_
;
325 ResourceProvider::Resource
* resource_
;
326 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
327 gfx::GpuMemoryBuffer
* gpu_memory_buffer_
;
329 ResourceFormat format_
;
330 base::ThreadChecker thread_checker_
;
332 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGpuMemoryBuffer
);
335 class CC_EXPORT ScopedWriteLockGr
{
337 ScopedWriteLockGr(ResourceProvider
* resource_provider
,
338 ResourceId resource_id
);
339 ~ScopedWriteLockGr();
341 void InitSkSurface(bool use_distance_field_text
,
342 bool can_use_lcd_text
,
343 int msaa_sample_count
);
344 void ReleaseSkSurface();
346 SkSurface
* sk_surface() { return sk_surface_
.get(); }
347 ResourceProvider::Resource
* resource() { return resource_
; }
350 ResourceProvider
* resource_provider_
;
351 ResourceProvider::Resource
* resource_
;
352 base::ThreadChecker thread_checker_
;
353 skia::RefPtr
<SkSurface
> sk_surface_
;
355 DISALLOW_COPY_AND_ASSIGN(ScopedWriteLockGr
);
358 class Fence
: public base::RefCounted
<Fence
> {
362 virtual void Set() = 0;
363 virtual bool HasPassed() = 0;
364 virtual void Wait() = 0;
367 friend class base::RefCounted
<Fence
>;
371 DISALLOW_COPY_AND_ASSIGN(Fence
);
374 class SynchronousFence
: public ResourceProvider::Fence
{
376 explicit SynchronousFence(gpu::gles2::GLES2Interface
* gl
);
378 // Overridden from Fence:
380 bool HasPassed() override
;
381 void Wait() override
;
383 // Returns true if fence has been set but not yet synchornized.
384 bool has_synchronized() const { return has_synchronized_
; }
387 ~SynchronousFence() override
;
391 gpu::gles2::GLES2Interface
* gl_
;
392 bool has_synchronized_
;
394 DISALLOW_COPY_AND_ASSIGN(SynchronousFence
);
397 // Acquire pixel buffer for resource. The pixel buffer can be used to
398 // set resource pixels without performing unnecessary copying.
399 void AcquirePixelBuffer(ResourceId resource
);
400 void ReleasePixelBuffer(ResourceId resource
);
401 // Map/unmap the acquired pixel buffer.
402 uint8_t* MapPixelBuffer(ResourceId id
, int* stride
);
403 void UnmapPixelBuffer(ResourceId id
);
404 // Asynchronously update pixels from acquired pixel buffer.
405 void BeginSetPixels(ResourceId id
);
406 void ForceSetPixelsToComplete(ResourceId id
);
407 bool DidSetPixelsComplete(ResourceId id
);
409 // For tests only! This prevents detecting uninitialized reads.
410 // Use SetPixels or LockForWrite to allocate implicitly.
411 void AllocateForTesting(ResourceId id
);
414 void CreateForTesting(ResourceId id
);
416 GLenum
TargetForTesting(ResourceId id
);
418 // Sets the current read fence. If a resource is locked for read
419 // and has read fences enabled, the resource will not allow writes
420 // until this fence has passed.
421 void SetReadLockFence(Fence
* fence
) { current_read_lock_fence_
= fence
; }
423 // Indicates if we can currently lock this resource for write.
424 bool CanLockForWrite(ResourceId id
);
426 // Copy |rect| pixels from source to destination.
427 void CopyResource(ResourceId source_id
,
429 const gfx::Rect
& rect
);
431 void WaitSyncPointIfNeeded(ResourceId id
);
433 void WaitReadLockIfNeeded(ResourceId id
);
435 static GLint
GetActiveTextureUnit(gpu::gles2::GLES2Interface
* gl
);
437 OutputSurface
* output_surface() { return output_surface_
; }
439 void ValidateResource(ResourceId id
) const;
442 ResourceProvider(OutputSurface
* output_surface
,
443 SharedBitmapManager
* shared_bitmap_manager
,
444 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
445 BlockingTaskRunner
* blocking_main_thread_task_runner
,
446 int highp_threshold_min
,
447 bool use_rgba_4444_texture_format
,
448 size_t id_allocation_chunk_size
,
449 bool use_persistent_map_for_gpu_memory_buffers
);
454 enum Origin
{ INTERNAL
, EXTERNAL
, DELEGATED
};
457 Resource(unsigned texture_id
,
458 const gfx::Size
& size
,
465 ResourceFormat format
);
466 Resource(uint8_t* pixels
,
467 SharedBitmap
* bitmap
,
468 const gfx::Size
& size
,
472 Resource(const SharedBitmapId
& bitmap_id
,
473 const gfx::Size
& size
,
480 // Pixel buffer used for set pixels without unnecessary copying.
481 unsigned gl_pixel_buffer_id
;
482 // Query used to determine when asynchronous set pixels complete.
483 unsigned gl_upload_query_id
;
484 // Query used to determine when read lock fence has passed.
485 unsigned gl_read_lock_query_id
;
486 TextureMailbox mailbox
;
487 ReleaseCallbackImpl release_callback_impl
;
489 int lock_for_read_count
;
492 bool dirty_image
: 1;
493 bool locked_for_write
: 1;
495 bool marked_for_deletion
: 1;
496 bool pending_set_pixels
: 1;
497 bool set_pixels_completion_forced
: 1;
499 bool read_lock_fences_enabled
: 1;
500 bool has_shared_bitmap_id
: 1;
501 scoped_refptr
<Fence
> read_lock_fence
;
505 // TODO(skyostil): Use a separate sampler object for filter state.
506 GLenum original_filter
;
509 unsigned bound_image_id
;
514 ResourceFormat format
;
515 SharedBitmapId shared_bitmap_id
;
516 SharedBitmap
* shared_bitmap
;
517 gfx::GpuMemoryBuffer
* gpu_memory_buffer
;
519 typedef base::hash_map
<ResourceId
, Resource
> ResourceMap
;
525 ResourceIdMap child_to_parent_map
;
526 ResourceIdMap parent_to_child_map
;
527 ReturnCallback return_callback
;
528 bool marked_for_deletion
;
529 bool needs_sync_points
;
531 typedef base::hash_map
<int, Child
> ChildMap
;
533 bool ReadLockFenceHasPassed(const Resource
* resource
) {
534 return !resource
->read_lock_fence
.get() ||
535 resource
->read_lock_fence
->HasPassed();
538 Resource
* InsertResource(ResourceId id
, const Resource
& resource
);
539 Resource
* GetResource(ResourceId id
);
540 const Resource
* LockForRead(ResourceId id
);
541 void UnlockForRead(ResourceId id
);
542 Resource
* LockForWrite(ResourceId id
);
543 void UnlockForWrite(Resource
* resource
);
545 static void PopulateSkBitmapWithResource(SkBitmap
* sk_bitmap
,
546 const Resource
* resource
);
548 void TransferResource(gpu::gles2::GLES2Interface
* gl
,
550 TransferableResource
* resource
);
555 void DeleteResourceInternal(ResourceMap::iterator it
, DeleteStyle style
);
556 void DeleteAndReturnUnusedResourcesToChild(ChildMap::iterator child_it
,
558 const ResourceIdArray
& unused
);
559 void DestroyChildInternal(ChildMap::iterator it
, DeleteStyle style
);
560 void LazyCreate(Resource
* resource
);
561 void LazyAllocate(Resource
* resource
);
563 void BindImageForSampling(Resource
* resource
);
564 // Binds the given GL resource to a texture target for sampling using the
565 // specified filter for both minification and magnification. Returns the
566 // texture target used. The resource must be locked for reading.
567 GLenum
BindForSampling(ResourceId resource_id
, GLenum unit
, GLenum filter
);
569 // Returns NULL if the output_surface_ does not have a ContextProvider.
570 gpu::gles2::GLES2Interface
* ContextGL() const;
571 class GrContext
* GrContext(bool worker_context
) const;
573 OutputSurface
* output_surface_
;
574 SharedBitmapManager
* shared_bitmap_manager_
;
575 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
576 BlockingTaskRunner
* blocking_main_thread_task_runner_
;
577 bool lost_output_surface_
;
578 int highp_threshold_min_
;
580 ResourceMap resources_
;
584 ResourceType default_resource_type_
;
585 bool use_texture_storage_ext_
;
586 bool use_texture_format_bgra_
;
587 bool use_texture_usage_hint_
;
588 bool use_compressed_texture_etc1_
;
589 ResourceFormat yuv_resource_format_
;
590 int max_texture_size_
;
591 ResourceFormat best_texture_format_
;
592 ResourceFormat best_render_buffer_format_
;
594 base::ThreadChecker thread_checker_
;
596 scoped_refptr
<Fence
> current_read_lock_fence_
;
597 bool use_rgba_4444_texture_format_
;
599 const size_t id_allocation_chunk_size_
;
600 scoped_ptr
<IdAllocator
> texture_id_allocator_
;
601 scoped_ptr
<IdAllocator
> buffer_id_allocator_
;
603 bool use_sync_query_
;
604 bool use_persistent_map_for_gpu_memory_buffers_
;
605 // Fence used for CopyResource if CHROMIUM_sync_query is not supported.
606 scoped_refptr
<SynchronousFence
> synchronous_fence_
;
608 DISALLOW_COPY_AND_ASSIGN(ResourceProvider
);
611 // TODO(epenner): Move these format conversions to resource_format.h
612 // once that builds on mac (npapi.h currently #includes OpenGL.h).
613 inline int BitsPerPixel(ResourceFormat format
) {
632 inline GLenum
GLDataType(ResourceFormat format
) {
633 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
634 static const unsigned format_gl_data_type
[RESOURCE_FORMAT_MAX
+ 1] = {
635 GL_UNSIGNED_BYTE
, // RGBA_8888
636 GL_UNSIGNED_SHORT_4_4_4_4
, // RGBA_4444
637 GL_UNSIGNED_BYTE
, // BGRA_8888
638 GL_UNSIGNED_BYTE
, // ALPHA_8
639 GL_UNSIGNED_BYTE
, // LUMINANCE_8
640 GL_UNSIGNED_SHORT_5_6_5
, // RGB_565,
641 GL_UNSIGNED_BYTE
, // ETC1
642 GL_UNSIGNED_BYTE
// RED_8
644 return format_gl_data_type
[format
];
647 inline GLenum
GLDataFormat(ResourceFormat format
) {
648 DCHECK_LE(format
, RESOURCE_FORMAT_MAX
);
649 static const unsigned format_gl_data_format
[RESOURCE_FORMAT_MAX
+ 1] = {
650 GL_RGBA
, // RGBA_8888
651 GL_RGBA
, // RGBA_4444
652 GL_BGRA_EXT
, // BGRA_8888
654 GL_LUMINANCE
, // LUMINANCE_8
656 GL_ETC1_RGB8_OES
, // ETC1
659 return format_gl_data_format
[format
];
662 inline GLenum
GLInternalFormat(ResourceFormat format
) {
663 return GLDataFormat(format
);
668 #endif // CC_RESOURCES_RESOURCE_PROVIDER_H_