WebKit Roll 139512:139548
[chromium-blink-merge.git] / cc / nine_patch_layer.cc
blob54f50f3887dfcae88955b06e50aac7d6fe4c2994
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 #include "cc/nine_patch_layer.h"
7 #include "cc/layer_tree_host.h"
8 #include "cc/nine_patch_layer_impl.h"
9 #include "cc/prioritized_resource.h"
10 #include "cc/resource_update.h"
11 #include "cc/resource_update_queue.h"
13 namespace cc {
15 scoped_refptr<NinePatchLayer> NinePatchLayer::create()
17 return make_scoped_refptr(new NinePatchLayer());
20 NinePatchLayer::NinePatchLayer()
21 : m_bitmapDirty(false)
25 NinePatchLayer::~NinePatchLayer()
29 scoped_ptr<LayerImpl> NinePatchLayer::createLayerImpl(LayerTreeImpl* treeImpl)
31 return NinePatchLayerImpl::create(treeImpl, id()).PassAs<LayerImpl>();
34 void NinePatchLayer::setTexturePriorities(const PriorityCalculator& priorityCalc)
36 if (m_resource && !m_resource->texture()->resourceManager()) {
37 // Release the resource here, as it is no longer tied to a resource manager.
38 m_resource.reset();
39 if (!m_bitmap.isNull())
40 createResource();
41 } else if (m_needsDisplay && m_bitmapDirty && drawsContent()) {
42 createResource();
45 if (m_resource) {
46 m_resource->texture()->setRequestPriority(PriorityCalculator::uiPriority(true));
47 // FIXME: Need to support swizzle in the shader for !PlatformColor::sameComponentOrder(textureFormat)
48 GLenum textureFormat = layerTreeHost()->rendererCapabilities().bestTextureFormat;
49 m_resource->texture()->setDimensions(gfx::Size(m_bitmap.width(), m_bitmap.height()), textureFormat);
53 void NinePatchLayer::setBitmap(const SkBitmap& bitmap, const gfx::Rect& aperture) {
54 m_bitmap = bitmap;
55 m_imageAperture = aperture;
56 m_bitmapDirty = true;
57 setNeedsDisplay();
60 void NinePatchLayer::update(ResourceUpdateQueue& queue, const OcclusionTracker* occlusion, RenderingStats& stats)
62 createUpdaterIfNeeded();
64 if (m_resource && (m_bitmapDirty || m_resource->texture()->resourceId() == 0)) {
65 gfx::Rect contentRect(gfx::Point(), gfx::Size(m_bitmap.width(), m_bitmap.height()));
66 ResourceUpdate upload = ResourceUpdate::Create(m_resource->texture(), &m_bitmap, contentRect, contentRect, gfx::Vector2d());
67 queue.appendFullUpload(upload);
68 m_bitmapDirty = false;
72 void NinePatchLayer::createUpdaterIfNeeded()
74 if (m_updater)
75 return;
77 m_updater = ImageLayerUpdater::create();
80 void NinePatchLayer::createResource()
82 DCHECK(!m_bitmap.isNull());
83 createUpdaterIfNeeded();
84 m_updater->setBitmap(m_bitmap);
85 m_needsDisplay = false;
87 if (!m_resource)
88 m_resource = m_updater->createResource(layerTreeHost()->contentsTextureManager());
91 bool NinePatchLayer::drawsContent() const
93 bool draws = !m_bitmap.isNull() && Layer::drawsContent() && m_bitmap.width() && m_bitmap.height();
94 return draws;
97 void NinePatchLayer::pushPropertiesTo(LayerImpl* layer)
99 Layer::pushPropertiesTo(layer);
100 NinePatchLayerImpl* layerImpl = static_cast<NinePatchLayerImpl*>(layer);
102 if (m_resource) {
103 DCHECK(!m_bitmap.isNull());
104 layerImpl->setResourceId(m_resource->texture()->resourceId());
105 layerImpl->setLayout(gfx::Size(m_bitmap.width(), m_bitmap.height()), m_imageAperture);