1 /*
2 * Copyright 2011, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "ImageTexture.h"
28
29 #include "ImagesManager.h"
30 #include "SkDevice.h"
31 #include "TilesManager.h"
32
33 #include <cutils/log.h>
34 #include <wtf/CurrentTime.h>
35 #include <wtf/text/CString.h>
36
37 #undef XLOGC
38 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "ImageTexture", __VA_ARGS__)
39
40 #ifdef DEBUG
41
42 #undef XLOG
43 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "ImageTexture", __VA_ARGS__)
44
45 #else
46
47 #undef XLOG
48 #define XLOG(...)
49
50 #endif // DEBUG
51
52 namespace WebCore {
53
ImageTexture(SkBitmapRef * img)54 ImageTexture::ImageTexture(SkBitmapRef* img)
55 : m_imageRef(img)
56 , m_image(0)
57 , m_textureId(0)
58 , m_refCount(0)
59 {
60 #ifdef DEBUG_COUNT
61 ClassTracker::instance()->increment("ImageTexture");
62 #endif
63 if (!m_imageRef)
64 return;
65
66 SkBitmap* bitmap = &m_imageRef->bitmap();
67 m_image = new SkBitmap();
68 int w = bitmap->width();
69 int h = bitmap->height();
70 m_image->setConfig(SkBitmap::kARGB_8888_Config, w, h);
71 m_image->allocPixels();
72 SkDevice* device = new SkDevice(NULL, *m_image, false);
73 SkCanvas canvas;
74 canvas.setDevice(device);
75 device->unref();
76 SkRect dest;
77 dest.set(0, 0, w, h);
78 m_image->setIsOpaque(false);
79 m_image->eraseARGB(0, 0, 0, 0);
80 canvas.drawBitmapRect(*bitmap, 0, dest);
81 }
82
~ImageTexture()83 ImageTexture::~ImageTexture()
84 {
85 #ifdef DEBUG_COUNT
86 ClassTracker::instance()->decrement("ImageTexture");
87 #endif
88 delete m_image;
89 }
90
prepareGL()91 void ImageTexture::prepareGL()
92 {
93 if (m_textureId)
94 return;
95
96 ImagesManager::instance()->scheduleTextureUpload(this);
97 }
98
uploadGLTexture()99 void ImageTexture::uploadGLTexture()
100 {
101 if (m_textureId)
102 return;
103
104 glGenTextures(1, &m_textureId);
105 GLUtils::createTextureWithBitmap(m_textureId, *m_image);
106 }
107
drawGL(LayerAndroid * layer)108 void ImageTexture::drawGL(LayerAndroid* layer)
109 {
110 if (!layer)
111 return;
112 if (!m_textureId)
113 return;
114 if (!m_image)
115 return;
116
117 SkRect rect;
118 rect.fLeft = 0;
119 rect.fTop = 0;
120 rect.fRight = layer->getSize().width();
121 rect.fBottom = layer->getSize().height();
122 TilesManager::instance()->shader()->drawLayerQuad(*layer->drawTransform(),
123 rect, m_textureId,
124 layer->drawOpacity(), true);
125 }
126
drawCanvas(SkCanvas * canvas,SkRect & rect)127 void ImageTexture::drawCanvas(SkCanvas* canvas, SkRect& rect)
128 {
129 canvas->drawBitmapRect(*m_image, 0, rect);
130 }
131
release()132 void ImageTexture::release()
133 {
134 if (m_refCount >= 1)
135 m_refCount--;
136 if (!m_refCount)
137 deleteTexture();
138 }
139
deleteTexture()140 void ImageTexture::deleteTexture()
141 {
142 if (m_textureId)
143 glDeleteTextures(1, &m_textureId);
144 }
145
146 } // namespace WebCore
147