diff options
27 files changed, 3970 insertions, 3969 deletions
@@ -7,56 +7,57 @@ *.xcodeproj /Makefile /breakpad/src/ -/build/util/support/ +/build/util/support /chrome/Debug /chrome/Hammer /chrome/Release -/chrome/app/theme/google_chrome/ -/chrome/installer/linux/ +/chrome/app/theme/google_chrome +/chrome/installer/linux /chrome/personalization/ -/chrome/test/data/firefox2_profile/searchplugins/ -/chrome/test/data/firefox2_searchplugins/ -/chrome/test/data/firefox3_profile/searchplugins/ -/chrome/test/data/firefox3_searchplugins/ -/chrome/test/data/layout_tests/ -/chrome/test/data/osdd/ -/chrome/test/data/plugin/ -/chrome/test/data/workers/LayoutTests/ -/chrome/tools/test/reference_build/ -/data/ -/googleurl/ -/native_client/ -/out/ -/rlz/binaries/ +/chrome/test/data/firefox2_profile/searchplugins +/chrome/test/data/firefox2_searchplugins +/chrome/test/data/firefox3_profile/searchplugins +/chrome/test/data/firefox3_searchplugins +/chrome/test/data/layout_tests +/chrome/test/data/osdd +/chrome/test/data/plugin +/chrome/test/data/workers/LayoutTests +/chrome/tools/test/reference_build +/data +/googleurl +/native_client +/out +/rlz/binaries /sconsbuild -/sdch/open-vcdiff/ -/testing/gtest/ +/sdch/open-vcdiff +/testing/gtest /testserver.log -/third_party/GTM/ -/third_party/WebKit/ -/third_party/ffmpeg/binaries/ -/third_party/googlemac/ -/third_party/hunspell/ -/third_party/icu/ -/third_party/personalization/ -/third_party/pdfsqueeze/ -/third_party/protobuf2/ -/third_party/skia/ -/third_party/tcmalloc/tcmalloc/ -/third_party/xdg-utils/ -/tools/distcc/ -/tools/grit/grit/test/data/ -/tools/gyp/ -/tools/page_cycler/acid3/ -/tools/tryserver/ -/valgrind.tmp/ -/v8/ -/webkit/Debug/ -/webkit/Release/ -/webkit/data/bmp_decoder/ -/webkit/data/ico_decoder/ -/webkit/data/layout_tests/LayoutTests/ -/webkit/data/test_shell/plugins/ -/webkit/data/xbm_decoder/ -/xcodebuild/ +/third_party/GTM +/third_party/WebKit +/third_party/ffmpeg/binaries +/third_party/forge +/third_party/googlemac +/third_party/hunspell +/third_party/icu +/third_party/personalization +/third_party/pdfsqueeze +/third_party/protobuf2 +/third_party/skia +/third_party/tcmalloc/tcmalloc +/third_party/xdg-utils +/tools/distcc +/tools/grit/grit/test/data +/tools/gyp +/tools/page_cycler/acid3 +/tools/tryserver +/valgrind.tmp +/v8 +/webkit/Debug +/webkit/Release +/webkit/data/bmp_decoder +/webkit/data/ico_decoder +/webkit/data/layout_tests/LayoutTests +/webkit/data/test_shell/plugins +/webkit/data/xbm_decoder +/xcodebuild SConstruct diff --git a/net/tools/fetch/fetch_server.cc b/net/tools/fetch/fetch_server.cc index 5e23fbe..eab8851 100644 --- a/net/tools/fetch/fetch_server.cc +++ b/net/tools/fetch/fetch_server.cc @@ -1,56 +1,56 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "base/at_exit.h"
-#include "base/command_line.h"
-#include "base/message_loop.h"
-#include "base/singleton.h"
-#include "base/stats_counters.h"
-#include "net/base/completion_callback.h"
-#include "net/base/io_buffer.h"
-#include "net/base/net_errors.h"
-#include "net/base/winsock_init.h"
-#include "net/http/http_network_layer.h"
-#include "net/http/http_request_info.h"
-#include "net/http/http_transaction.h"
-#include "net/proxy/proxy_service.h"
-#include "net/tools/fetch/http_server.h"
-
-void usage(const char* program_name) {
- printf("usage: %s\n", program_name);
- exit(-1);
-}
-
-int main(int argc, char**argv) {
- base::AtExitManager exit;
- StatsTable table("fetchserver", 50, 1000);
- table.set_current(&table);
-
-#if defined(OS_WIN)
- net::EnsureWinsockInit();
-#endif // defined(OS_WIN)
-
- CommandLine::Init(0, NULL);
- const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
-
- // Do work here.
- MessageLoop loop;
- HttpServer server("", 80); // TODO(mbelshe): make port configurable
- MessageLoop::current()->Run();
-
- if (parsed_command_line.HasSwitch(L"stats")) {
- // Dump the stats table.
- printf("<stats>\n");
- int counter_max = table.GetMaxCounters();
- for (int index=0; index < counter_max; index++) {
- std::string name(table.GetRowName(index));
- if (name.length() > 0) {
- int value = table.GetRowValue(index);
- printf("%s:\t%d\n", name.c_str(), value);
- }
- }
- printf("</stats>\n");
- }
-
-}
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/at_exit.h" +#include "base/command_line.h" +#include "base/message_loop.h" +#include "base/singleton.h" +#include "base/stats_counters.h" +#include "net/base/completion_callback.h" +#include "net/base/io_buffer.h" +#include "net/base/net_errors.h" +#include "net/base/winsock_init.h" +#include "net/http/http_network_layer.h" +#include "net/http/http_request_info.h" +#include "net/http/http_transaction.h" +#include "net/proxy/proxy_service.h" +#include "net/tools/fetch/http_server.h" + +void usage(const char* program_name) { + printf("usage: %s\n", program_name); + exit(-1); +} + +int main(int argc, char**argv) { + base::AtExitManager exit; + StatsTable table("fetchserver", 50, 1000); + table.set_current(&table); + +#if defined(OS_WIN) + net::EnsureWinsockInit(); +#endif // defined(OS_WIN) + + CommandLine::Init(0, NULL); + const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); + + // Do work here. + MessageLoop loop; + HttpServer server("", 80); // TODO(mbelshe): make port configurable + MessageLoop::current()->Run(); + + if (parsed_command_line.HasSwitch(L"stats")) { + // Dump the stats table. + printf("<stats>\n"); + int counter_max = table.GetMaxCounters(); + for (int index=0; index < counter_max; index++) { + std::string name(table.GetRowName(index)); + if (name.length() > 0) { + int value = table.GetRowValue(index); + printf("%s:\t%d\n", name.c_str(), value); + } + } + printf("</stats>\n"); + } + +} diff --git a/net/tools/fetch/http_listen_socket.cc b/net/tools/fetch/http_listen_socket.cc index c04720e..033afc4 100644 --- a/net/tools/fetch/http_listen_socket.cc +++ b/net/tools/fetch/http_listen_socket.cc @@ -1,242 +1,242 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "base/compiler_specific.h"
-#include "base/logging.h"
-#include "base/message_loop.h"
-#include "base/string_util.h"
-#include "net/tools/fetch/http_listen_socket.h"
-#include "net/tools/fetch/http_server_request_info.h"
-#include "net/tools/fetch/http_server_response_info.h"
-
-// must run in the IO thread
-HttpListenSocket::HttpListenSocket(SOCKET s,
- HttpListenSocket::Delegate* delegate)
- : ALLOW_THIS_IN_INITIALIZER_LIST(ListenSocket(s, this)),
- delegate_(delegate) {
-}
-
-// must run in the IO thread
-HttpListenSocket::~HttpListenSocket() {
-}
-
-void HttpListenSocket::Accept() {
- SOCKET conn = ListenSocket::Accept(socket_);
- DCHECK_NE(conn, ListenSocket::kInvalidSocket);
- if (conn == ListenSocket::kInvalidSocket) {
- // TODO
- } else {
- scoped_refptr<HttpListenSocket> sock =
- new HttpListenSocket(conn, delegate_);
- // it's up to the delegate to AddRef if it wants to keep it around
- DidAccept(this, sock);
- }
-}
-
-HttpListenSocket* HttpListenSocket::Listen(const std::string& ip, int port,
- HttpListenSocket::Delegate* delegate) {
- SOCKET s = ListenSocket::Listen(ip, port);
- if (s == ListenSocket::kInvalidSocket) {
- // TODO (ibrar): error handling
- } else {
- HttpListenSocket *serv = new HttpListenSocket(s, delegate);
- serv->Listen();
- return serv;
- }
- return NULL;
-}
-
-//
-// HTTP Request Parser
-// This HTTP request parser uses a simple state machine to quickly parse
-// through the headers. The parser is not 100% complete, as it is designed
-// for use in this simple test driver.
-//
-// Known issues:
-// - does not handle whitespace on first HTTP line correctly. Expects
-// a single space between the method/url and url/protocol.
-
-// Input character types.
-enum header_parse_inputs {
- INPUT_SPACE,
- INPUT_CR,
- INPUT_LF,
- INPUT_COLON,
- INPUT_DEFAULT,
- MAX_INPUTS
-};
-
-// Parser states.
-enum header_parse_states {
- ST_METHOD, // Receiving the method
- ST_URL, // Receiving the URL
- ST_PROTO, // Receiving the protocol
- ST_HEADER, // Starting a Request Header
- ST_NAME, // Receiving a request header name
- ST_SEPARATOR, // Receiving the separator between header name and value
- ST_VALUE, // Receiving a request header value
- ST_DONE, // Parsing is complete and successful
- ST_ERR, // Parsing encountered invalid syntax.
- MAX_STATES
-};
-
-// State transition table
-int parser_state[MAX_STATES][MAX_INPUTS] = {
-/* METHOD */ { ST_URL, ST_ERR, ST_ERR, ST_ERR, ST_METHOD },
-/* URL */ { ST_PROTO, ST_ERR, ST_ERR, ST_URL, ST_URL },
-/* PROTOCOL */ { ST_ERR, ST_HEADER, ST_NAME, ST_ERR, ST_PROTO },
-/* HEADER */ { ST_ERR, ST_ERR, ST_NAME, ST_ERR, ST_ERR },
-/* NAME */ { ST_SEPARATOR, ST_DONE, ST_ERR, ST_SEPARATOR, ST_NAME },
-/* SEPARATOR */ { ST_SEPARATOR, ST_ERR, ST_ERR, ST_SEPARATOR, ST_VALUE },
-/* VALUE */ { ST_VALUE, ST_HEADER, ST_NAME, ST_VALUE, ST_VALUE },
-/* DONE */ { ST_DONE, ST_DONE, ST_DONE, ST_DONE, ST_DONE },
-/* ERR */ { ST_ERR, ST_ERR, ST_ERR, ST_ERR, ST_ERR }
-};
-
-// Convert an input character to the parser's input token.
-int charToInput(char ch) {
- switch(ch) {
- case ' ':
- return INPUT_SPACE;
- case '\r':
- return INPUT_CR;
- case '\n':
- return INPUT_LF;
- case ':':
- return INPUT_COLON;
- }
- return INPUT_DEFAULT;
-}
-
-HttpServerRequestInfo* HttpListenSocket::ParseHeaders() {
- int pos = 0;
- int data_len = recv_data_.length();
- int state = ST_METHOD;
- HttpServerRequestInfo* info = new HttpServerRequestInfo();
- std::string buffer;
- std::string header_name;
- std::string header_value;
- while (pos < data_len) {
- char ch = recv_data_[pos++];
- int input = charToInput(ch);
- int next_state = parser_state[state][input];
-
- bool transition = (next_state != state);
- if (transition) {
- // Do any actions based on state transitions.
- switch (state) {
- case ST_METHOD:
- info->method = buffer;
- buffer.clear();
- break;
- case ST_URL:
- info->url = GURL(buffer);
- buffer.clear();
- break;
- case ST_PROTO:
- // TODO(mbelshe): Deal better with parsing protocol.
- DCHECK(buffer == "HTTP/1.1");
- buffer.clear();
- break;
- case ST_NAME:
- header_name = buffer;
- buffer.clear();
- break;
- case ST_VALUE:
- header_value = buffer;
- // TODO(mbelshe): Deal better with duplicate headers
- DCHECK(info->headers.find(header_name) == info->headers.end());
- info->headers[header_name] = header_value;
- buffer.clear();
- break;
- }
- state = next_state;
- } else {
- // Do any actions based on current state
- switch (state) {
- case ST_METHOD:
- case ST_URL:
- case ST_PROTO:
- case ST_VALUE:
- case ST_NAME:
- buffer.append(&ch, 1);
- break;
- case ST_DONE:
- recv_data_ = recv_data_.substr(pos);
- return info;
- case ST_ERR:
- delete info;
- return NULL;
- }
- }
- }
- // No more characters, but we haven't finished parsing yet.
- delete info;
- return NULL;
-}
-
-void HttpListenSocket::DidAccept(ListenSocket* server,
- ListenSocket* connection) {
- connection->AddRef();
-}
-
-void HttpListenSocket::DidRead(ListenSocket* connection,
- const std::string& data) {
- recv_data_ += data;
- while (recv_data_.length()) {
- HttpServerRequestInfo* request = ParseHeaders();
- if (!request)
- break;
- delegate_->OnRequest(this, request);
- delete request;
- }
-}
-
-void HttpListenSocket::DidClose(ListenSocket* sock) {
- sock->Release();
-}
-
-// Convert the numeric status code to a string.
-// e.g. 200 -> "200 OK"
-std::string ServerStatus(int code) {
- switch(code) {
- case 200:
- return std::string("200 OK");
- // TODO(mbelshe): handle other codes.
- }
- NOTREACHED();
- return std::string();
-}
-
-void HttpListenSocket::Respond(HttpServerResponseInfo* info,
- std::string& data) {
- std::string response;
-
- // status line
- response = info->protocol + " ";
- response += ServerStatus(info->status);
- response += "\r\n";
-
- // standard headers
- if (info->content_type.length())
- response += "Content-type: " + info->content_type + "\r\n";
-
- if (info->content_length > 0)
- response += "Content-length: " + IntToString(info->content_length) +
- "\r\n";
-
- if (info->connection_close)
- response += "Connection: close\r\n";
-
- // TODO(mbelshe): support additional headers
-
- // End of headers
- response += "\r\n";
-
- // Add data
- response += data;
-
- // Write it all out.
- this->Send(response, false);
-}
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/compiler_specific.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/string_util.h" +#include "net/tools/fetch/http_listen_socket.h" +#include "net/tools/fetch/http_server_request_info.h" +#include "net/tools/fetch/http_server_response_info.h" + +// must run in the IO thread +HttpListenSocket::HttpListenSocket(SOCKET s, + HttpListenSocket::Delegate* delegate) + : ALLOW_THIS_IN_INITIALIZER_LIST(ListenSocket(s, this)), + delegate_(delegate) { +} + +// must run in the IO thread +HttpListenSocket::~HttpListenSocket() { +} + +void HttpListenSocket::Accept() { + SOCKET conn = ListenSocket::Accept(socket_); + DCHECK_NE(conn, ListenSocket::kInvalidSocket); + if (conn == ListenSocket::kInvalidSocket) { + // TODO + } else { + scoped_refptr<HttpListenSocket> sock = + new HttpListenSocket(conn, delegate_); + // it's up to the delegate to AddRef if it wants to keep it around + DidAccept(this, sock); + } +} + +HttpListenSocket* HttpListenSocket::Listen(const std::string& ip, int port, + HttpListenSocket::Delegate* delegate) { + SOCKET s = ListenSocket::Listen(ip, port); + if (s == ListenSocket::kInvalidSocket) { + // TODO (ibrar): error handling + } else { + HttpListenSocket *serv = new HttpListenSocket(s, delegate); + serv->Listen(); + return serv; + } + return NULL; +} + +// +// HTTP Request Parser +// This HTTP request parser uses a simple state machine to quickly parse +// through the headers. The parser is not 100% complete, as it is designed +// for use in this simple test driver. +// +// Known issues: +// - does not handle whitespace on first HTTP line correctly. Expects +// a single space between the method/url and url/protocol. + +// Input character types. +enum header_parse_inputs { + INPUT_SPACE, + INPUT_CR, + INPUT_LF, + INPUT_COLON, + INPUT_DEFAULT, + MAX_INPUTS +}; + +// Parser states. +enum header_parse_states { + ST_METHOD, // Receiving the method + ST_URL, // Receiving the URL + ST_PROTO, // Receiving the protocol + ST_HEADER, // Starting a Request Header + ST_NAME, // Receiving a request header name + ST_SEPARATOR, // Receiving the separator between header name and value + ST_VALUE, // Receiving a request header value + ST_DONE, // Parsing is complete and successful + ST_ERR, // Parsing encountered invalid syntax. + MAX_STATES +}; + +// State transition table +int parser_state[MAX_STATES][MAX_INPUTS] = { +/* METHOD */ { ST_URL, ST_ERR, ST_ERR, ST_ERR, ST_METHOD }, +/* URL */ { ST_PROTO, ST_ERR, ST_ERR, ST_URL, ST_URL }, +/* PROTOCOL */ { ST_ERR, ST_HEADER, ST_NAME, ST_ERR, ST_PROTO }, +/* HEADER */ { ST_ERR, ST_ERR, ST_NAME, ST_ERR, ST_ERR }, +/* NAME */ { ST_SEPARATOR, ST_DONE, ST_ERR, ST_SEPARATOR, ST_NAME }, +/* SEPARATOR */ { ST_SEPARATOR, ST_ERR, ST_ERR, ST_SEPARATOR, ST_VALUE }, +/* VALUE */ { ST_VALUE, ST_HEADER, ST_NAME, ST_VALUE, ST_VALUE }, +/* DONE */ { ST_DONE, ST_DONE, ST_DONE, ST_DONE, ST_DONE }, +/* ERR */ { ST_ERR, ST_ERR, ST_ERR, ST_ERR, ST_ERR } +}; + +// Convert an input character to the parser's input token. +int charToInput(char ch) { + switch(ch) { + case ' ': + return INPUT_SPACE; + case '\r': + return INPUT_CR; + case '\n': + return INPUT_LF; + case ':': + return INPUT_COLON; + } + return INPUT_DEFAULT; +} + +HttpServerRequestInfo* HttpListenSocket::ParseHeaders() { + int pos = 0; + int data_len = recv_data_.length(); + int state = ST_METHOD; + HttpServerRequestInfo* info = new HttpServerRequestInfo(); + std::string buffer; + std::string header_name; + std::string header_value; + while (pos < data_len) { + char ch = recv_data_[pos++]; + int input = charToInput(ch); + int next_state = parser_state[state][input]; + + bool transition = (next_state != state); + if (transition) { + // Do any actions based on state transitions. + switch (state) { + case ST_METHOD: + info->method = buffer; + buffer.clear(); + break; + case ST_URL: + info->url = GURL(buffer); + buffer.clear(); + break; + case ST_PROTO: + // TODO(mbelshe): Deal better with parsing protocol. + DCHECK(buffer == "HTTP/1.1"); + buffer.clear(); + break; + case ST_NAME: + header_name = buffer; + buffer.clear(); + break; + case ST_VALUE: + header_value = buffer; + // TODO(mbelshe): Deal better with duplicate headers + DCHECK(info->headers.find(header_name) == info->headers.end()); + info->headers[header_name] = header_value; + buffer.clear(); + break; + } + state = next_state; + } else { + // Do any actions based on current state + switch (state) { + case ST_METHOD: + case ST_URL: + case ST_PROTO: + case ST_VALUE: + case ST_NAME: + buffer.append(&ch, 1); + break; + case ST_DONE: + recv_data_ = recv_data_.substr(pos); + return info; + case ST_ERR: + delete info; + return NULL; + } + } + } + // No more characters, but we haven't finished parsing yet. + delete info; + return NULL; +} + +void HttpListenSocket::DidAccept(ListenSocket* server, + ListenSocket* connection) { + connection->AddRef(); +} + +void HttpListenSocket::DidRead(ListenSocket* connection, + const std::string& data) { + recv_data_ += data; + while (recv_data_.length()) { + HttpServerRequestInfo* request = ParseHeaders(); + if (!request) + break; + delegate_->OnRequest(this, request); + delete request; + } +} + +void HttpListenSocket::DidClose(ListenSocket* sock) { + sock->Release(); +} + +// Convert the numeric status code to a string. +// e.g. 200 -> "200 OK" +std::string ServerStatus(int code) { + switch(code) { + case 200: + return std::string("200 OK"); + // TODO(mbelshe): handle other codes. + } + NOTREACHED(); + return std::string(); +} + +void HttpListenSocket::Respond(HttpServerResponseInfo* info, + std::string& data) { + std::string response; + + // status line + response = info->protocol + " "; + response += ServerStatus(info->status); + response += "\r\n"; + + // standard headers + if (info->content_type.length()) + response += "Content-type: " + info->content_type + "\r\n"; + + if (info->content_length > 0) + response += "Content-length: " + IntToString(info->content_length) + + "\r\n"; + + if (info->connection_close) + response += "Connection: close\r\n"; + + // TODO(mbelshe): support additional headers + + // End of headers + response += "\r\n"; + + // Add data + response += data; + + // Write it all out. + this->Send(response, false); +} diff --git a/net/tools/fetch/http_server.cc b/net/tools/fetch/http_server.cc index 7102805..3fa416c 100644 --- a/net/tools/fetch/http_server.cc +++ b/net/tools/fetch/http_server.cc @@ -1,13 +1,13 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "net/tools/fetch/http_server.h"
-#include "net/tools/fetch/http_listen_socket.h"
-
-HttpServer::HttpServer(std::string ip, int port)
- : ALLOW_THIS_IN_INITIALIZER_LIST(session_(new HttpSession(ip, port))) {
-}
-
-HttpServer::~HttpServer() {
-}
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/tools/fetch/http_server.h" +#include "net/tools/fetch/http_listen_socket.h" + +HttpServer::HttpServer(std::string ip, int port) + : ALLOW_THIS_IN_INITIALIZER_LIST(session_(new HttpSession(ip, port))) { +} + +HttpServer::~HttpServer() { +} diff --git a/net/tools/fetch/http_session.cc b/net/tools/fetch/http_session.cc index f740289..17e68b6 100644 --- a/net/tools/fetch/http_session.cc +++ b/net/tools/fetch/http_session.cc @@ -1,34 +1,34 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "net/tools/fetch/http_session.h"
-#include "net/tools/fetch/http_server_response_info.h"
-
-HttpSession::HttpSession(const std::string& ip, int port)
- : ALLOW_THIS_IN_INITIALIZER_LIST(
- socket_(HttpListenSocket::Listen(ip, port, this))) {
-}
-
-HttpSession::~HttpSession() {
-}
-
-void HttpSession::OnRequest(HttpListenSocket* connection,
- HttpServerRequestInfo* info) {
- // TODO(mbelshe): Make this function more interesting.
-
- // Generate a 10KB sequence of data.
- static std::string data;
- if (data.length() == 0) {
- while (data.length() < (10 * 1024))
- data += 'a' + (rand() % 26);
- }
-
- HttpServerResponseInfo response_info;
- response_info.protocol = "HTTP/1.1";
- response_info.status = 200;
- response_info.content_type = "text/plain";
- response_info.content_length = data.length();
-
- connection->Respond(&response_info, data);
-}
+// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/tools/fetch/http_session.h" +#include "net/tools/fetch/http_server_response_info.h" + +HttpSession::HttpSession(const std::string& ip, int port) + : ALLOW_THIS_IN_INITIALIZER_LIST( + socket_(HttpListenSocket::Listen(ip, port, this))) { +} + +HttpSession::~HttpSession() { +} + +void HttpSession::OnRequest(HttpListenSocket* connection, + HttpServerRequestInfo* info) { + // TODO(mbelshe): Make this function more interesting. + + // Generate a 10KB sequence of data. + static std::string data; + if (data.length() == 0) { + while (data.length() < (10 * 1024)) + data += 'a' + (rand() % 26); + } + + HttpServerResponseInfo response_info; + response_info.protocol = "HTTP/1.1"; + response_info.status = 200; + response_info.content_type = "text/plain"; + response_info.content_length = data.length(); + + connection->Respond(&response_info, data); +} diff --git a/o3d/command_buffer/service/cross/gl/effect_gl.cc b/o3d/command_buffer/service/cross/gl/effect_gl.cc index 4e9da56..c2c49e3 100644 --- a/o3d/command_buffer/service/cross/gl/effect_gl.cc +++ b/o3d/command_buffer/service/cross/gl/effect_gl.cc @@ -445,7 +445,7 @@ void EffectGL::AddLowLevelParams(CGprogram prog, CGenum name_space, bool vp) { int num_elements; if (cg_type == CG_ARRAY) { num_elements = cgGetArraySize(cg_param, 0); - // Substitute the first element's type for our type.
+ // Substitute the first element's type for our type. cg_type = cgGetParameterType(cgGetArrayParameter(cg_param, 0)); } else { num_elements = 0; diff --git a/o3d/command_buffer/service/cross/precompile.cc b/o3d/command_buffer/service/cross/precompile.cc index 97064b4..d95a72b 100644 --- a/o3d/command_buffer/service/cross/precompile.cc +++ b/o3d/command_buffer/service/cross/precompile.cc @@ -1,33 +1,33 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-#include "command_buffer/service/cross/precompile.h"
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "command_buffer/service/cross/precompile.h" diff --git a/o3d/command_buffer/service/win/d3d9/render_surface_d3d9.cc b/o3d/command_buffer/service/win/d3d9/render_surface_d3d9.cc index 987eceb..2883baf 100644 --- a/o3d/command_buffer/service/win/d3d9/render_surface_d3d9.cc +++ b/o3d/command_buffer/service/win/d3d9/render_surface_d3d9.cc @@ -1,230 +1,230 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-// This file implements the D3D9 versions of the render surface resources,
-// as well as the related GAPID3D9 function implementations.
-
-#include "command_buffer/service/win/d3d9/render_surface_d3d9.h"
-#include "command_buffer/service/win/d3d9/gapi_d3d9.h"
-#include "command_buffer/service/win/d3d9/texture_d3d9.h"
-
-
-namespace o3d {
-namespace command_buffer {
-
-RenderSurfaceD3D9::RenderSurfaceD3D9(int width,
- int height,
- int mip_level,
- int side,
- TextureD3D9 *texture,
- IDirect3DSurface9 *direct3d_surface)
- : width_(width),
- height_(height),
- mip_level_(mip_level),
- texture_(texture),
- direct3d_surface_(direct3d_surface) {
- DCHECK_GT(width, 0);
- DCHECK_GT(height, 0);
- DCHECK_GT(mip_level, -1);
- DCHECK(texture);
-}
-
-RenderSurfaceD3D9* RenderSurfaceD3D9::Create(GAPID3D9 *gapi,
- int width,
- int height,
- int mip_level,
- int side,
- TextureD3D9 *texture) {
- DCHECK(gapi);
- DCHECK_GT(width, 0);
- DCHECK_GT(height, 0);
- DCHECK_GT(mip_level, -1);
- DCHECK(texture);
- CComPtr<IDirect3DSurface9> direct3d_surface_handle;
- bool success =
- texture->CreateRenderSurface(width, height, mip_level, side,
- &direct3d_surface_handle);
- if (!success || direct3d_surface_handle == NULL) {
- // If the surface was not created properly, delete and return nothing.
- return NULL;
- }
- RenderSurfaceD3D9 *render_surface =
- new RenderSurfaceD3D9(width,
- height,
- mip_level,
- side,
- texture,
- direct3d_surface_handle);
- return render_surface;
-}
-
-RenderDepthStencilSurfaceD3D9::RenderDepthStencilSurfaceD3D9(
- int width,
- int height,
- IDirect3DSurface9 *direct3d_surface)
- : width_(width),
- height_(height),
- direct3d_surface_(direct3d_surface) {
- DCHECK_GT(width, 0);
- DCHECK_GT(height, 0);
-}
-
-RenderDepthStencilSurfaceD3D9* RenderDepthStencilSurfaceD3D9::Create(
- GAPID3D9 *gapi,
- int width,
- int height) {
- DCHECK(gapi);
- DCHECK_GT(width, 0);
- DCHECK_GT(height, 0);
-
- CComPtr<IDirect3DSurface9> direct3d_surface;
- gapi->d3d_device()->CreateDepthStencilSurface(
- width,
- height,
- D3DFMT_D24S8, // d3d format
- D3DMULTISAMPLE_NONE, // multisampling type
- 0, // multisample quality level
- FALSE, // z-buffer discarding disabled
- &direct3d_surface,
- NULL); // This parameter is required to be NULL.
- if (direct3d_surface == NULL) {
- return NULL;
- }
- RenderDepthStencilSurfaceD3D9 *depth_stencil =
- new RenderDepthStencilSurfaceD3D9(width, height, direct3d_surface);
- return depth_stencil;
-}
-
-// GAPI Interface functions ---------------------------------------------------
-
-// Copies the data from a texture resource.
-BufferSyncInterface::ParseError GAPID3D9::CreateRenderSurface(
- ResourceID id,
- unsigned int width,
- unsigned int height,
- unsigned int mip_level,
- unsigned int side,
- ResourceID texture_id) {
- if (id == current_surface_id_) {
- // This will delete the current surface which would be bad.
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- TextureD3D9 *texture = textures_.Get(texture_id);
- if (!texture->render_surfaces_enabled()) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- } else {
- RenderSurfaceD3D9 *render_surface = RenderSurfaceD3D9::Create(this,
- width,
- height,
- mip_level,
- side,
- texture);
- if (render_surface == NULL) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- render_surfaces_.Assign(id, render_surface);
- }
- return BufferSyncInterface::PARSE_NO_ERROR;
-}
-
-BufferSyncInterface::ParseError GAPID3D9::DestroyRenderSurface(ResourceID id) {
- if (id == current_surface_id_) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- return render_surfaces_.Destroy(id) ?
- BufferSyncInterface::PARSE_NO_ERROR :
- BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
-}
-
-BufferSyncInterface::ParseError GAPID3D9::CreateDepthSurface(
- ResourceID id,
- unsigned int width,
- unsigned int height) {
- if (id == current_depth_surface_id_) {
- // This will delete the current surface which would be bad.
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- RenderDepthStencilSurfaceD3D9 *depth_surface =
- RenderDepthStencilSurfaceD3D9::Create(this, width, height);
- if (depth_surface == NULL) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- depth_surfaces_.Assign(id, depth_surface);
- return BufferSyncInterface::PARSE_NO_ERROR;
-}
-
-BufferSyncInterface::ParseError GAPID3D9::DestroyDepthSurface(ResourceID id) {
- if (id == current_depth_surface_id_) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
- return depth_surfaces_.Destroy(id) ?
- BufferSyncInterface::PARSE_NO_ERROR :
- BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
-}
-
-BufferSyncInterface::ParseError GAPID3D9::SetRenderSurface(
- ResourceID render_surface_id,
- ResourceID depth_stencil_id) {
- RenderSurfaceD3D9 *d3d_render_surface =
- render_surfaces_.Get(render_surface_id);
- RenderDepthStencilSurfaceD3D9 *d3d_render_depth_surface =
- depth_surfaces_.Get(depth_stencil_id);
-
- if (d3d_render_surface == NULL && d3d_render_depth_surface == NULL) {
- return BufferSyncInterface::PARSE_INVALID_ARGUMENTS;
- }
-
- IDirect3DSurface9 *d3d_surface =
- d3d_render_surface ? d3d_render_surface->direct3d_surface() : NULL;
- IDirect3DSurface9 *d3d_depth_surface = d3d_render_depth_surface ?
- d3d_render_depth_surface->direct3d_surface() : NULL;
-
- // Get the device and set the render target and the depth stencil surface.
- IDirect3DDevice9 *device = this->d3d_device();
-
- HR(device->SetRenderTarget(0, d3d_surface));
- HR(device->SetDepthStencilSurface(d3d_depth_surface));
- current_surface_id_ = render_surface_id;
- current_depth_surface_id_ = depth_stencil_id;
- return BufferSyncInterface::PARSE_NO_ERROR;
-}
-
-void GAPID3D9::SetBackSurfaces() {
- // Get the device and set the render target and the depth stencil surface.
- IDirect3DDevice9 *device = this->d3d_device();
- HR(d3d_device()->SetRenderTarget(0, back_buffer_surface_));
- HR(d3d_device()->SetDepthStencilSurface(back_buffer_depth_surface_));
-}
-
-} // namespace command_buffer
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This file implements the D3D9 versions of the render surface resources, +// as well as the related GAPID3D9 function implementations. + +#include "command_buffer/service/win/d3d9/render_surface_d3d9.h" +#include "command_buffer/service/win/d3d9/gapi_d3d9.h" +#include "command_buffer/service/win/d3d9/texture_d3d9.h" + + +namespace o3d { +namespace command_buffer { + +RenderSurfaceD3D9::RenderSurfaceD3D9(int width, + int height, + int mip_level, + int side, + TextureD3D9 *texture, + IDirect3DSurface9 *direct3d_surface) + : width_(width), + height_(height), + mip_level_(mip_level), + texture_(texture), + direct3d_surface_(direct3d_surface) { + DCHECK_GT(width, 0); + DCHECK_GT(height, 0); + DCHECK_GT(mip_level, -1); + DCHECK(texture); +} + +RenderSurfaceD3D9* RenderSurfaceD3D9::Create(GAPID3D9 *gapi, + int width, + int height, + int mip_level, + int side, + TextureD3D9 *texture) { + DCHECK(gapi); + DCHECK_GT(width, 0); + DCHECK_GT(height, 0); + DCHECK_GT(mip_level, -1); + DCHECK(texture); + CComPtr<IDirect3DSurface9> direct3d_surface_handle; + bool success = + texture->CreateRenderSurface(width, height, mip_level, side, + &direct3d_surface_handle); + if (!success || direct3d_surface_handle == NULL) { + // If the surface was not created properly, delete and return nothing. + return NULL; + } + RenderSurfaceD3D9 *render_surface = + new RenderSurfaceD3D9(width, + height, + mip_level, + side, + texture, + direct3d_surface_handle); + return render_surface; +} + +RenderDepthStencilSurfaceD3D9::RenderDepthStencilSurfaceD3D9( + int width, + int height, + IDirect3DSurface9 *direct3d_surface) + : width_(width), + height_(height), + direct3d_surface_(direct3d_surface) { + DCHECK_GT(width, 0); + DCHECK_GT(height, 0); +} + +RenderDepthStencilSurfaceD3D9* RenderDepthStencilSurfaceD3D9::Create( + GAPID3D9 *gapi, + int width, + int height) { + DCHECK(gapi); + DCHECK_GT(width, 0); + DCHECK_GT(height, 0); + + CComPtr<IDirect3DSurface9> direct3d_surface; + gapi->d3d_device()->CreateDepthStencilSurface( + width, + height, + D3DFMT_D24S8, // d3d format + D3DMULTISAMPLE_NONE, // multisampling type + 0, // multisample quality level + FALSE, // z-buffer discarding disabled + &direct3d_surface, + NULL); // This parameter is required to be NULL. + if (direct3d_surface == NULL) { + return NULL; + } + RenderDepthStencilSurfaceD3D9 *depth_stencil = + new RenderDepthStencilSurfaceD3D9(width, height, direct3d_surface); + return depth_stencil; +} + +// GAPI Interface functions --------------------------------------------------- + +// Copies the data from a texture resource. +BufferSyncInterface::ParseError GAPID3D9::CreateRenderSurface( + ResourceID id, + unsigned int width, + unsigned int height, + unsigned int mip_level, + unsigned int side, + ResourceID texture_id) { + if (id == current_surface_id_) { + // This will delete the current surface which would be bad. + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + TextureD3D9 *texture = textures_.Get(texture_id); + if (!texture->render_surfaces_enabled()) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } else { + RenderSurfaceD3D9 *render_surface = RenderSurfaceD3D9::Create(this, + width, + height, + mip_level, + side, + texture); + if (render_surface == NULL) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + render_surfaces_.Assign(id, render_surface); + } + return BufferSyncInterface::PARSE_NO_ERROR; +} + +BufferSyncInterface::ParseError GAPID3D9::DestroyRenderSurface(ResourceID id) { + if (id == current_surface_id_) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + return render_surfaces_.Destroy(id) ? + BufferSyncInterface::PARSE_NO_ERROR : + BufferSyncInterface::PARSE_INVALID_ARGUMENTS; +} + +BufferSyncInterface::ParseError GAPID3D9::CreateDepthSurface( + ResourceID id, + unsigned int width, + unsigned int height) { + if (id == current_depth_surface_id_) { + // This will delete the current surface which would be bad. + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + RenderDepthStencilSurfaceD3D9 *depth_surface = + RenderDepthStencilSurfaceD3D9::Create(this, width, height); + if (depth_surface == NULL) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + depth_surfaces_.Assign(id, depth_surface); + return BufferSyncInterface::PARSE_NO_ERROR; +} + +BufferSyncInterface::ParseError GAPID3D9::DestroyDepthSurface(ResourceID id) { + if (id == current_depth_surface_id_) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + return depth_surfaces_.Destroy(id) ? + BufferSyncInterface::PARSE_NO_ERROR : + BufferSyncInterface::PARSE_INVALID_ARGUMENTS; +} + +BufferSyncInterface::ParseError GAPID3D9::SetRenderSurface( + ResourceID render_surface_id, + ResourceID depth_stencil_id) { + RenderSurfaceD3D9 *d3d_render_surface = + render_surfaces_.Get(render_surface_id); + RenderDepthStencilSurfaceD3D9 *d3d_render_depth_surface = + depth_surfaces_.Get(depth_stencil_id); + + if (d3d_render_surface == NULL && d3d_render_depth_surface == NULL) { + return BufferSyncInterface::PARSE_INVALID_ARGUMENTS; + } + + IDirect3DSurface9 *d3d_surface = + d3d_render_surface ? d3d_render_surface->direct3d_surface() : NULL; + IDirect3DSurface9 *d3d_depth_surface = d3d_render_depth_surface ? + d3d_render_depth_surface->direct3d_surface() : NULL; + + // Get the device and set the render target and the depth stencil surface. + IDirect3DDevice9 *device = this->d3d_device(); + + HR(device->SetRenderTarget(0, d3d_surface)); + HR(device->SetDepthStencilSurface(d3d_depth_surface)); + current_surface_id_ = render_surface_id; + current_depth_surface_id_ = depth_stencil_id; + return BufferSyncInterface::PARSE_NO_ERROR; +} + +void GAPID3D9::SetBackSurfaces() { + // Get the device and set the render target and the depth stencil surface. + IDirect3DDevice9 *device = this->d3d_device(); + HR(d3d_device()->SetRenderTarget(0, back_buffer_surface_)); + HR(d3d_device()->SetDepthStencilSurface(back_buffer_depth_surface_)); +} + +} // namespace command_buffer +} // namespace o3d + diff --git a/o3d/command_buffer/service/win/d3d9/texture_d3d9.cc b/o3d/command_buffer/service/win/d3d9/texture_d3d9.cc index d570fda..b11121fa 100644 --- a/o3d/command_buffer/service/win/d3d9/texture_d3d9.cc +++ b/o3d/command_buffer/service/win/d3d9/texture_d3d9.cc @@ -230,18 +230,18 @@ bool Texture2DD3D9::GetData(GAPID3D9 *gapi, return true; } -bool Texture2DD3D9::CreateRenderSurface(int width,
- int height,
- int mip_level,
- int side,
- IDirect3DSurface9** direct3d_surface) {
- IDirect3DTexture9* d3d_texture =
- static_cast<IDirect3DTexture9*>(d3d_base_texture());
- D3DSURFACE_DESC surface_desc;
- d3d_texture->GetLevelDesc(mip_level, &surface_desc);
- if (width != surface_desc.Width || height != surface_desc.Height) {
- return false;
- }
+bool Texture2DD3D9::CreateRenderSurface(int width, + int height, + int mip_level, + int side, + IDirect3DSurface9** direct3d_surface) { + IDirect3DTexture9* d3d_texture = + static_cast<IDirect3DTexture9*>(d3d_base_texture()); + D3DSURFACE_DESC surface_desc; + d3d_texture->GetLevelDesc(mip_level, &surface_desc); + if (width != surface_desc.Width || height != surface_desc.Height) { + return false; + } HR(d3d_texture->GetSurfaceLevel(mip_level, direct3d_surface)); return true; } @@ -591,12 +591,12 @@ bool TextureCubeD3D9::CreateRenderSurface( int side, IDirect3DSurface9** direct3d_surface) { IDirect3DCubeTexture9* d3d_cube_texture = - static_cast<IDirect3DCubeTexture9*>(d3d_base_texture());
- D3DSURFACE_DESC surface_desc;
- d3d_cube_texture->GetLevelDesc(mip_level, &surface_desc);
- if (width != surface_desc.Width || height != surface_desc.Height ||
- side < 0 || side > 5) {
- return false;
+ static_cast<IDirect3DCubeTexture9*>(d3d_base_texture()); + D3DSURFACE_DESC surface_desc; + d3d_cube_texture->GetLevelDesc(mip_level, &surface_desc); + if (width != surface_desc.Width || height != surface_desc.Height || + side < 0 || side > 5) { + return false; } HR(d3d_cube_texture->GetCubeMapSurface( D3DFace(static_cast<texture::Face>(side)), diff --git a/o3d/core/cross/command_buffer/render_surface_cb.cc b/o3d/core/cross/command_buffer/render_surface_cb.cc index f65ce3d..bb967d2 100644 --- a/o3d/core/cross/command_buffer/render_surface_cb.cc +++ b/o3d/core/cross/command_buffer/render_surface_cb.cc @@ -49,9 +49,9 @@ RenderSurfaceCB::RenderSurfaceCB(ServiceLocator *service_locator, RendererCB *renderer) : RenderSurface(service_locator, width, height, texture), resource_id_(command_buffer::kInvalidResource), - renderer_(renderer) {
- DCHECK_GT(width, 0);
- DCHECK_GT(height, 0);
+ renderer_(renderer) { + DCHECK_GT(width, 0); + DCHECK_GT(height, 0); DCHECK_GT(mip_level, -1); DCHECK(texture); DCHECK(renderer); @@ -95,8 +95,8 @@ RenderDepthStencilSurfaceCB::RenderDepthStencilSurfaceCB( RendererCB *renderer) : RenderDepthStencilSurface(service_locator, width, height), resource_id_(command_buffer::kInvalidResource), - renderer_(renderer) {
- DCHECK_GT(width, 0);
+ renderer_(renderer) { + DCHECK_GT(width, 0); DCHECK_GT(height, 0); DCHECK(renderer); ResourceID id = renderer_->depth_surface_ids().AllocateID(); diff --git a/o3d/core/cross/command_buffer/texture_cb.cc b/o3d/core/cross/command_buffer/texture_cb.cc index 73f53fc..12e3d98 100644 --- a/o3d/core/cross/command_buffer/texture_cb.cc +++ b/o3d/core/cross/command_buffer/texture_cb.cc @@ -359,7 +359,7 @@ void Texture2DCB::SetRect(int level, << "SetRect must be full rectangle for compressed textures"; return; } - unsigned int dst_pitch = image::ComputePitch(format(), src_width);
+ unsigned int dst_pitch = image::ComputePitch(format(), src_width); size_t size = dst_pitch * src_height; FencedAllocatorWrapper *allocator = renderer_->allocator(); @@ -588,7 +588,7 @@ void TextureCUBECB::SetRect(TextureCUBE::CubeFace face, return; } - unsigned int dst_pitch = image::ComputePitch(format(), src_width);
+ unsigned int dst_pitch = image::ComputePitch(format(), src_width); size_t size = dst_pitch * src_height; FencedAllocatorWrapper *allocator = renderer_->allocator(); diff --git a/o3d/core/cross/message_commands.cc b/o3d/core/cross/message_commands.cc index 9744612..f7c0f7e 100644 --- a/o3d/core/cross/message_commands.cc +++ b/o3d/core/cross/message_commands.cc @@ -1,49 +1,49 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This file contains string descriptions of various IMC messages for O3D.
-
-#include "core/cross/message_commands.h"
-
-namespace o3d {
-namespace imc {
-const char* GetMessageDescription(MessageId id) {
- static const char* const message_descriptions[] = {
- #define O3D_IMC_MESSAGE_OP(id, class_name) #id,
- O3D_IMC_MESSAGE_LIST(O3D_IMC_MESSAGE_OP)
- #undef O3D_IMC_MESSAGE_OP
- };
- return message_descriptions[id];
-}
-} // namespace imc
-} // namespace o3d
-
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This file contains string descriptions of various IMC messages for O3D. + +#include "core/cross/message_commands.h" + +namespace o3d { +namespace imc { +const char* GetMessageDescription(MessageId id) { + static const char* const message_descriptions[] = { + #define O3D_IMC_MESSAGE_OP(id, class_name) #id, + O3D_IMC_MESSAGE_LIST(O3D_IMC_MESSAGE_OP) + #undef O3D_IMC_MESSAGE_OP + }; + return message_descriptions[id]; +} +} // namespace imc +} // namespace o3d + + diff --git a/o3d/core/cross/render_surface_test.cc b/o3d/core/cross/render_surface_test.cc index 5aafbfe..5e51e62 100644 --- a/o3d/core/cross/render_surface_test.cc +++ b/o3d/core/cross/render_surface_test.cc @@ -1,273 +1,273 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-#include "core/cross/precompile.h"
-#include "tests/common/win/testing_common.h"
-#include "core/cross/client.h"
-#include "core/cross/pack.h"
-#include "core/cross/renderer.h"
-#include "core/cross/bitmap.h"
-#include "core/cross/features.h"
-#include "core/cross/texture.h"
-#include "core/cross/render_surface.h"
-#include "core/cross/render_surface_set.h"
-#include "core/cross/renderer_platform.h"
-#include "core/cross/error_status.h"
-
-// Defined in testing_common.cc, for each platform.
-extern o3d::DisplayWindow *g_display_window;
-
-namespace o3d {
-
-// A mock render which pushes commands to the renderer so that
-// actual rendering calls that maintain state can be handled by a
-// variable rather than actually rendering.
-class MockRenderer {
- public:
- // Creates a forwarding render class that pushes functionality to be tested
- // to the desired renderer and handles other functionality on its own.
- explicit MockRenderer(Renderer *renderer) : renderer_(renderer) {}
-
- virtual ~MockRenderer() {}
-
- // Rather than actually rendering, this just sets the state in the renderer.
- void StartRendering() {
- renderer_->set_rendering(true);
- }
-
- // This resets the state in the renderer.
- void FinishRendering() {
- renderer_->set_rendering(false);
- }
-
- // Pushes SetRenderSurfaces to the renderer.
- void SetRenderSurfaces(const RenderSurface *surface,
- const RenderDepthStencilSurface *depth_surface,
- bool is_back_buffer) {
- renderer_->SetRenderSurfaces(surface, depth_surface, is_back_buffer);
- }
-
- // Pushes GetRenderSurfaces to the renderer.
- void GetRenderSurfaces(const RenderSurface **surface,
- const RenderDepthStencilSurface **depth_surface,
- bool *is_back_buffer) {
- renderer_->GetRenderSurfaces(surface, depth_surface, is_back_buffer);
- }
- private:
- Renderer *renderer_;
-};
-
-// Class for testing render surfaces and associated functionality.
-class RenderSurfaceTest : public testing::Test {
- public:
- RenderSurfaceTest()
- : object_manager_(g_service_locator),
- error_status_(g_service_locator) {}
-
- protected:
- virtual void SetUp() {
- service_locator_ = new ServiceLocator;
- features_ = new Features(service_locator_);
- pack_ = object_manager_->CreatePack();
- renderer_ = new MockRenderer(g_renderer);
- renderer_->StartRendering();
- }
-
- virtual void TearDown() {
- renderer_->FinishRendering();
- pack_->Destroy();
- error_status_.ClearLastError();
- delete features_;
- delete service_locator_;
- delete renderer_;
- }
-
- ServiceLocator* service_locator() const {
- return service_locator_;
- }
-
- MockRenderer* renderer() const {
- return renderer_;
- }
-
- Pack* pack() const {
- return pack_;
- }
-
- ServiceDependency<ObjectManager> object_manager_;
- ErrorStatus error_status_;
- ServiceLocator *service_locator_;
- Features *features_;
- Pack *pack_;
- MockRenderer *renderer_;
-};
-
-// Tests that non PoT textures can't make render surfaces.
-TEST_F(RenderSurfaceTest, NonPowerOfTwoRenderSurfaceEnabled) {
- Texture2D *texture = pack()->CreateTexture2D(20, 32, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL == texture);
-}
-
-// Tests that a render surface can be created from a texture 2d.
-TEST_F(RenderSurfaceTest, CreateRenderSurfaceFromTexture2D) {
- Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL != texture);
-
- RenderSurface::Ref render_surface = texture->GetRenderSurface(0);
- ASSERT_TRUE(NULL != render_surface);
- ASSERT_TRUE(NULL != render_surface->texture());
- ASSERT_EQ(render_surface->width(), 16);
- ASSERT_EQ(render_surface->height(), 32);
-}
-
-// Tests that a render surface can be created from a cube texture.
-TEST_F(RenderSurfaceTest, CreateRenderSurfaceFromTextureCUBE) {
- TextureCUBE *texture = pack()->CreateTextureCUBE(16, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL != texture);
-
- RenderSurface::Ref render_surface = texture->GetRenderSurface(
- TextureCUBE::FACE_POSITIVE_X, 0);
- ASSERT_TRUE(NULL != render_surface);
- ASSERT_TRUE(NULL != render_surface->texture());
- ASSERT_EQ(render_surface->width(), 16);
- ASSERT_EQ(render_surface->height(), 16);
-}
-
-// Tests the renderer's functionality to swap render surfaces and that
-// the correct render surfaces are set.
-TEST_F(RenderSurfaceTest, SwapRenderSurfaces) {
- Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL != texture);
-
- RenderSurface::Ref render_surface = texture->GetRenderSurface(0);
- ASSERT_TRUE(NULL != render_surface);
- ASSERT_TRUE(texture == render_surface->texture());
-
- RenderDepthStencilSurface *depth_surface =
- pack()->CreateDepthStencilSurface(16, 32);
-
- // Now swap surfaces.
- renderer()->SetRenderSurfaces(render_surface, depth_surface, false);
- const RenderSurface *test_render_surface = NULL;
- const RenderDepthStencilSurface *test_depth_surface = NULL;
- bool test_is_back_buffer;
- renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface,
- &test_is_back_buffer);
- ASSERT_TRUE(test_render_surface == render_surface);
- ASSERT_TRUE(test_depth_surface == depth_surface);
- ASSERT_FALSE(test_is_back_buffer);
-}
-
-// Tests the renderer's functionality to swap render surfaces and return
-// the old one to the main rendering surface.
-TEST_F(RenderSurfaceTest, SetBackSurfaces) {
- Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL != texture);
-
- RenderSurface::Ref render_surface = texture->GetRenderSurface(0);
- ASSERT_TRUE(NULL != render_surface);
- ASSERT_TRUE(texture == render_surface->texture());
-
- RenderDepthStencilSurface *depth_surface =
- pack()->CreateDepthStencilSurface(16, 32);
-
- // Save the original surfaces for comparison.
- const RenderSurface *original_render_surface = NULL;
- const RenderDepthStencilSurface *original_depth_surface = NULL;
- bool original_is_back_buffer;
- renderer()->GetRenderSurfaces(&original_render_surface,
- &original_depth_surface,
- &original_is_back_buffer);
- // Now swap surfaces.
- renderer()->SetRenderSurfaces(render_surface, depth_surface, false);
- // Return the back buffers
- renderer()->SetRenderSurfaces(NULL, NULL, true);
- // Get the original surfaces again for comparison.
- const RenderSurface *restored_render_surface = NULL;
- const RenderDepthStencilSurface *restored_depth_surface = NULL;
- bool restored_is_back_buffer;
- renderer()->GetRenderSurfaces(&original_render_surface,
- &original_depth_surface,
- &restored_is_back_buffer);
- ASSERT_TRUE(original_render_surface == restored_render_surface);
- ASSERT_TRUE(original_depth_surface == restored_depth_surface);
- ASSERT_TRUE(restored_is_back_buffer);
-}
-
-// Tests the render surfaces interaction as part of a render surface set
-// which is how they are commonly used in practice.
-TEST_F(RenderSurfaceTest, RenderSurfaceSetTest) {
- Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true);
- ASSERT_TRUE(NULL != texture);
-
- RenderSurface::Ref render_surface = texture->GetRenderSurface(0);
- ASSERT_TRUE(NULL != render_surface);
- ASSERT_TRUE(texture == render_surface->texture());
-
- RenderDepthStencilSurface *depth_surface =
- pack()->CreateDepthStencilSurface(16, 32);
- ASSERT_TRUE(depth_surface != NULL);
-
- RenderSurfaceSet *render_surface_set = pack()->Create<RenderSurfaceSet>();
- ASSERT_TRUE(render_surface_set != NULL);
- render_surface_set->set_render_surface(render_surface);
- render_surface_set->set_render_depth_stencil_surface(depth_surface);
- ASSERT_TRUE(render_surface_set->ValidateBoundSurfaces());
-
- RenderContext render_context(g_renderer);
-
- const RenderSurface *old_render_surface = NULL;
- const RenderDepthStencilSurface *old_depth_surface = NULL;
- bool old_is_back_buffer = false;
- renderer()->GetRenderSurfaces(&old_render_surface, &old_depth_surface,
- &old_is_back_buffer);
-
- render_surface_set->Render(&render_context);
- const RenderSurface *test_render_surface = NULL;
- const RenderDepthStencilSurface *test_depth_surface = NULL;
- bool test_is_back_buffer = false;
- renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface,
- &test_is_back_buffer);
- ASSERT_TRUE(test_render_surface == render_surface);
- ASSERT_TRUE(test_depth_surface == depth_surface);
- ASSERT_FALSE(test_is_back_buffer);
-
- render_surface_set->PostRender(&render_context);
- renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface,
- &test_is_back_buffer);
- ASSERT_TRUE(test_render_surface == old_render_surface);
- ASSERT_TRUE(test_depth_surface == old_depth_surface);
- ASSERT_TRUE(test_is_back_buffer == old_is_back_buffer);
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include "core/cross/precompile.h" +#include "tests/common/win/testing_common.h" +#include "core/cross/client.h" +#include "core/cross/pack.h" +#include "core/cross/renderer.h" +#include "core/cross/bitmap.h" +#include "core/cross/features.h" +#include "core/cross/texture.h" +#include "core/cross/render_surface.h" +#include "core/cross/render_surface_set.h" +#include "core/cross/renderer_platform.h" +#include "core/cross/error_status.h" + +// Defined in testing_common.cc, for each platform. +extern o3d::DisplayWindow *g_display_window; + +namespace o3d { + +// A mock render which pushes commands to the renderer so that +// actual rendering calls that maintain state can be handled by a +// variable rather than actually rendering. +class MockRenderer { + public: + // Creates a forwarding render class that pushes functionality to be tested + // to the desired renderer and handles other functionality on its own. + explicit MockRenderer(Renderer *renderer) : renderer_(renderer) {} + + virtual ~MockRenderer() {} + + // Rather than actually rendering, this just sets the state in the renderer. + void StartRendering() { + renderer_->set_rendering(true); + } + + // This resets the state in the renderer. + void FinishRendering() { + renderer_->set_rendering(false); + } + + // Pushes SetRenderSurfaces to the renderer. + void SetRenderSurfaces(const RenderSurface *surface, + const RenderDepthStencilSurface *depth_surface, + bool is_back_buffer) { + renderer_->SetRenderSurfaces(surface, depth_surface, is_back_buffer); + } + + // Pushes GetRenderSurfaces to the renderer. + void GetRenderSurfaces(const RenderSurface **surface, + const RenderDepthStencilSurface **depth_surface, + bool *is_back_buffer) { + renderer_->GetRenderSurfaces(surface, depth_surface, is_back_buffer); + } + private: + Renderer *renderer_; +}; + +// Class for testing render surfaces and associated functionality. +class RenderSurfaceTest : public testing::Test { + public: + RenderSurfaceTest() + : object_manager_(g_service_locator), + error_status_(g_service_locator) {} + + protected: + virtual void SetUp() { + service_locator_ = new ServiceLocator; + features_ = new Features(service_locator_); + pack_ = object_manager_->CreatePack(); + renderer_ = new MockRenderer(g_renderer); + renderer_->StartRendering(); + } + + virtual void TearDown() { + renderer_->FinishRendering(); + pack_->Destroy(); + error_status_.ClearLastError(); + delete features_; + delete service_locator_; + delete renderer_; + } + + ServiceLocator* service_locator() const { + return service_locator_; + } + + MockRenderer* renderer() const { + return renderer_; + } + + Pack* pack() const { + return pack_; + } + + ServiceDependency<ObjectManager> object_manager_; + ErrorStatus error_status_; + ServiceLocator *service_locator_; + Features *features_; + Pack *pack_; + MockRenderer *renderer_; +}; + +// Tests that non PoT textures can't make render surfaces. +TEST_F(RenderSurfaceTest, NonPowerOfTwoRenderSurfaceEnabled) { + Texture2D *texture = pack()->CreateTexture2D(20, 32, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL == texture); +} + +// Tests that a render surface can be created from a texture 2d. +TEST_F(RenderSurfaceTest, CreateRenderSurfaceFromTexture2D) { + Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL != texture); + + RenderSurface::Ref render_surface = texture->GetRenderSurface(0); + ASSERT_TRUE(NULL != render_surface); + ASSERT_TRUE(NULL != render_surface->texture()); + ASSERT_EQ(render_surface->width(), 16); + ASSERT_EQ(render_surface->height(), 32); +} + +// Tests that a render surface can be created from a cube texture. +TEST_F(RenderSurfaceTest, CreateRenderSurfaceFromTextureCUBE) { + TextureCUBE *texture = pack()->CreateTextureCUBE(16, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL != texture); + + RenderSurface::Ref render_surface = texture->GetRenderSurface( + TextureCUBE::FACE_POSITIVE_X, 0); + ASSERT_TRUE(NULL != render_surface); + ASSERT_TRUE(NULL != render_surface->texture()); + ASSERT_EQ(render_surface->width(), 16); + ASSERT_EQ(render_surface->height(), 16); +} + +// Tests the renderer's functionality to swap render surfaces and that +// the correct render surfaces are set. +TEST_F(RenderSurfaceTest, SwapRenderSurfaces) { + Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL != texture); + + RenderSurface::Ref render_surface = texture->GetRenderSurface(0); + ASSERT_TRUE(NULL != render_surface); + ASSERT_TRUE(texture == render_surface->texture()); + + RenderDepthStencilSurface *depth_surface = + pack()->CreateDepthStencilSurface(16, 32); + + // Now swap surfaces. + renderer()->SetRenderSurfaces(render_surface, depth_surface, false); + const RenderSurface *test_render_surface = NULL; + const RenderDepthStencilSurface *test_depth_surface = NULL; + bool test_is_back_buffer; + renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface, + &test_is_back_buffer); + ASSERT_TRUE(test_render_surface == render_surface); + ASSERT_TRUE(test_depth_surface == depth_surface); + ASSERT_FALSE(test_is_back_buffer); +} + +// Tests the renderer's functionality to swap render surfaces and return +// the old one to the main rendering surface. +TEST_F(RenderSurfaceTest, SetBackSurfaces) { + Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL != texture); + + RenderSurface::Ref render_surface = texture->GetRenderSurface(0); + ASSERT_TRUE(NULL != render_surface); + ASSERT_TRUE(texture == render_surface->texture()); + + RenderDepthStencilSurface *depth_surface = + pack()->CreateDepthStencilSurface(16, 32); + + // Save the original surfaces for comparison. + const RenderSurface *original_render_surface = NULL; + const RenderDepthStencilSurface *original_depth_surface = NULL; + bool original_is_back_buffer; + renderer()->GetRenderSurfaces(&original_render_surface, + &original_depth_surface, + &original_is_back_buffer); + // Now swap surfaces. + renderer()->SetRenderSurfaces(render_surface, depth_surface, false); + // Return the back buffers + renderer()->SetRenderSurfaces(NULL, NULL, true); + // Get the original surfaces again for comparison. + const RenderSurface *restored_render_surface = NULL; + const RenderDepthStencilSurface *restored_depth_surface = NULL; + bool restored_is_back_buffer; + renderer()->GetRenderSurfaces(&original_render_surface, + &original_depth_surface, + &restored_is_back_buffer); + ASSERT_TRUE(original_render_surface == restored_render_surface); + ASSERT_TRUE(original_depth_surface == restored_depth_surface); + ASSERT_TRUE(restored_is_back_buffer); +} + +// Tests the render surfaces interaction as part of a render surface set +// which is how they are commonly used in practice. +TEST_F(RenderSurfaceTest, RenderSurfaceSetTest) { + Texture2D *texture = pack()->CreateTexture2D(16, 32, Texture::ARGB8, 2, true); + ASSERT_TRUE(NULL != texture); + + RenderSurface::Ref render_surface = texture->GetRenderSurface(0); + ASSERT_TRUE(NULL != render_surface); + ASSERT_TRUE(texture == render_surface->texture()); + + RenderDepthStencilSurface *depth_surface = + pack()->CreateDepthStencilSurface(16, 32); + ASSERT_TRUE(depth_surface != NULL); + + RenderSurfaceSet *render_surface_set = pack()->Create<RenderSurfaceSet>(); + ASSERT_TRUE(render_surface_set != NULL); + render_surface_set->set_render_surface(render_surface); + render_surface_set->set_render_depth_stencil_surface(depth_surface); + ASSERT_TRUE(render_surface_set->ValidateBoundSurfaces()); + + RenderContext render_context(g_renderer); + + const RenderSurface *old_render_surface = NULL; + const RenderDepthStencilSurface *old_depth_surface = NULL; + bool old_is_back_buffer = false; + renderer()->GetRenderSurfaces(&old_render_surface, &old_depth_surface, + &old_is_back_buffer); + + render_surface_set->Render(&render_context); + const RenderSurface *test_render_surface = NULL; + const RenderDepthStencilSurface *test_depth_surface = NULL; + bool test_is_back_buffer = false; + renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface, + &test_is_back_buffer); + ASSERT_TRUE(test_render_surface == render_surface); + ASSERT_TRUE(test_depth_surface == depth_surface); + ASSERT_FALSE(test_is_back_buffer); + + render_surface_set->PostRender(&render_context); + renderer()->GetRenderSurfaces(&test_render_surface, &test_depth_surface, + &test_is_back_buffer); + ASSERT_TRUE(test_render_surface == old_render_surface); + ASSERT_TRUE(test_depth_surface == old_depth_surface); + ASSERT_TRUE(test_is_back_buffer == old_is_back_buffer); +} + +} // namespace o3d + diff --git a/o3d/core/cross/texture_base_test.cc b/o3d/core/cross/texture_base_test.cc index 5f11c8f..c52bfbc 100644 --- a/o3d/core/cross/texture_base_test.cc +++ b/o3d/core/cross/texture_base_test.cc @@ -1,101 +1,101 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-// This file implements unit tests for class Texture.
-
-#include "tests/common/win/testing_common.h"
-#include "core/cross/texture_base.h"
-#include "core/cross/object_manager.h"
-
-namespace o3d {
-
-namespace {
-
-Texture::RGBASwizzleIndices swizzle;
-
-class MockTexture : public Texture {
- public:
- typedef SmartPointer<MockTexture> Ref;
-
- MockTexture(ServiceLocator* service_locator,
- Texture::Format format,
- int levels,
- bool enable_render_surfaces)
- : Texture(service_locator, format, levels, enable_render_surfaces) {
- }
-
- virtual const RGBASwizzleIndices& GetABGR32FSwizzleIndices() {
- return swizzle;
- }
-
- virtual void* GetTextureHandle() const {
- return NULL;
- }
-
- virtual void SetFromBitmap(const Bitmap& bitmap) {
- }
-
- virtual void GenerateMips(int source_level, int num_levels) {
- }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(MockTexture);
-};
-
-} // anonymous namespace
-
-class TextureTest : public testing::Test {
- protected:
-
- TextureTest()
- : object_manager_(g_service_locator) {
- }
-
- private:
- ServiceDependency<ObjectManager> object_manager_;
-};
-
-TEST_F(TextureTest, Basic) {
- MockTexture::Ref texture(new MockTexture(
- g_service_locator,
- Texture::XRGB8,
- 1,
- false));
- ASSERT_TRUE(texture != NULL);
- EXPECT_EQ(texture->format(), Texture::XRGB8);
- EXPECT_EQ(texture->levels(), 1);
- EXPECT_FALSE(texture->alpha_is_one());
- EXPECT_FALSE(texture->render_surfaces_enabled());
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This file implements unit tests for class Texture. + +#include "tests/common/win/testing_common.h" +#include "core/cross/texture_base.h" +#include "core/cross/object_manager.h" + +namespace o3d { + +namespace { + +Texture::RGBASwizzleIndices swizzle; + +class MockTexture : public Texture { + public: + typedef SmartPointer<MockTexture> Ref; + + MockTexture(ServiceLocator* service_locator, + Texture::Format format, + int levels, + bool enable_render_surfaces) + : Texture(service_locator, format, levels, enable_render_surfaces) { + } + + virtual const RGBASwizzleIndices& GetABGR32FSwizzleIndices() { + return swizzle; + } + + virtual void* GetTextureHandle() const { + return NULL; + } + + virtual void SetFromBitmap(const Bitmap& bitmap) { + } + + virtual void GenerateMips(int source_level, int num_levels) { + } + + private: + DISALLOW_COPY_AND_ASSIGN(MockTexture); +}; + +} // anonymous namespace + +class TextureTest : public testing::Test { + protected: + + TextureTest() + : object_manager_(g_service_locator) { + } + + private: + ServiceDependency<ObjectManager> object_manager_; +}; + +TEST_F(TextureTest, Basic) { + MockTexture::Ref texture(new MockTexture( + g_service_locator, + Texture::XRGB8, + 1, + false)); + ASSERT_TRUE(texture != NULL); + EXPECT_EQ(texture->format(), Texture::XRGB8); + EXPECT_EQ(texture->levels(), 1); + EXPECT_FALSE(texture->alpha_is_one()); + EXPECT_FALSE(texture->render_surfaces_enabled()); +} + +} // namespace o3d + diff --git a/o3d/core/cross/texture_test.cc b/o3d/core/cross/texture_test.cc index ba6e93c..d8de84f 100644 --- a/o3d/core/cross/texture_test.cc +++ b/o3d/core/cross/texture_test.cc @@ -1,201 +1,201 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-// This file implements unit tests for class Texture.
-
-#include "tests/common/win/testing_common.h"
-#include "core/cross/texture.h"
-#include "core/cross/object_manager.h"
-#include "core/cross/pack.h"
-
-namespace o3d {
-
-namespace {
-
-bool CompareTexture(Texture2D* texture, int level, const uint8* expected) {
- Texture2D::LockHelper helper(texture, level, Texture::kReadOnly);
- const uint8* data = helper.GetDataAs<const uint8>();
- unsigned mip_width = image::ComputeMipDimension(level, texture->width());
- unsigned mip_height = image::ComputeMipDimension(level, texture->height());
-
- int bytes_per_row = image::ComputePitch(texture->format(), mip_width);
- for (unsigned yy = 0; yy < mip_height; ++yy) {
- if (memcmp(data, expected, bytes_per_row) != 0) {
- return false;
- }
- expected += bytes_per_row;
- data += helper.pitch();
- }
- return true;
-}
-
-} // anonymous namespace.
-
-class Texture2DTest : public testing::Test {
- protected:
- Texture2DTest()
- : object_manager_(g_service_locator) {
- }
-
- virtual void SetUp() {
- pack_ = object_manager_->CreatePack();
- }
-
- virtual void TearDown() {
- pack_->Destroy();
- }
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ObjectManager> object_manager_;
- Pack* pack_;
-};
-
-TEST_F(Texture2DTest, Basic) {
- Texture2D* texture = pack()->CreateTexture2D(8, 8, Texture::ARGB8, 1, false);
- ASSERT_TRUE(texture != NULL);
- EXPECT_TRUE(texture->IsA(Texture2D::GetApparentClass()));
- EXPECT_TRUE(texture->IsA(Texture::GetApparentClass()));
- EXPECT_TRUE(texture->IsA(ParamObject::GetApparentClass()));
- EXPECT_EQ(texture->format(), Texture::ARGB8);
- EXPECT_EQ(texture->levels(), 1);
- EXPECT_FALSE(texture->render_surfaces_enabled());
- EXPECT_EQ(0, Texture::kMaxDimension >> Texture::kMaxLevels);
- EXPECT_EQ(1, Texture::kMaxDimension >> (Texture::kMaxLevels - 1));
-}
-
-TEST_F(Texture2DTest, SetRect) {
- const int kWidth = 8;
- const int kHeight = 8;
- const int kLevels = 2;
- const int kDestMip = 1;
- const unsigned kDestX = 1u;
- const unsigned kDestY = 1u;
- Texture2D* texture = pack()->CreateTexture2D(
- kWidth, kHeight, Texture::ARGB8, kLevels, false);
- ASSERT_TRUE(texture != NULL);
- static const uint8 kExpected1[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- EXPECT_TRUE(CompareTexture(texture, 1, kExpected1));
- const int kSrcWidth = 2;
- const int kSrcHeight = 2;
- static const uint8 kSourcePixels[] = {
- 0x01, 0x01, 0x01, 0x02, 0x03, 0x03, 0x03, 0x04,
- 0x05, 0x05, 0x05, 0x06, 0x07, 0x07, 0x07, 0x08,
- };
- const int kSourcePitch = sizeof(kSourcePixels[0]) * kSrcWidth * 4;
- // normal copy
- texture->SetRect(kDestMip, kDestX, kDestY,
- kSrcWidth, kSrcHeight, kSourcePixels, kSourcePitch);
- static const uint8 kExpected2[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02,
- 0x03, 0x03, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x06,
- 0x07, 0x07, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- EXPECT_TRUE(CompareTexture(texture, 1, kExpected2));
- // flipped copy
- texture->SetRect(
- kDestMip, kDestX, kDestY,
- kSrcWidth, kSrcHeight,
- kSourcePixels + kSourcePitch,
- -kSourcePitch);
- static const uint8 kExpected3[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x06,
- 0x07, 0x07, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02,
- 0x03, 0x03, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00,
-
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- EXPECT_TRUE(CompareTexture(texture, 1, kExpected3));
-}
-
-class TextureCUBETest : public testing::Test {
- protected:
- TextureCUBETest()
- : object_manager_(g_service_locator) {
- }
-
- virtual void SetUp() {
- pack_ = object_manager_->CreatePack();
- }
-
- virtual void TearDown() {
- pack_->Destroy();
- }
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ObjectManager> object_manager_;
- Pack* pack_;
-};
-
-TEST_F(TextureCUBETest, Basic) {
- TextureCUBE* texture =
- pack()->CreateTextureCUBE(8, Texture::ARGB8, 1, false);
- ASSERT_TRUE(texture != NULL);
- EXPECT_TRUE(texture->IsA(TextureCUBE::GetApparentClass()));
- EXPECT_TRUE(texture->IsA(Texture::GetApparentClass()));
- EXPECT_TRUE(texture->IsA(ParamObject::GetApparentClass()));
- EXPECT_EQ(texture->format(), Texture::ARGB8);
- EXPECT_EQ(texture->levels(), 1);
- EXPECT_FALSE(texture->render_surfaces_enabled());
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This file implements unit tests for class Texture. + +#include "tests/common/win/testing_common.h" +#include "core/cross/texture.h" +#include "core/cross/object_manager.h" +#include "core/cross/pack.h" + +namespace o3d { + +namespace { + +bool CompareTexture(Texture2D* texture, int level, const uint8* expected) { + Texture2D::LockHelper helper(texture, level, Texture::kReadOnly); + const uint8* data = helper.GetDataAs<const uint8>(); + unsigned mip_width = image::ComputeMipDimension(level, texture->width()); + unsigned mip_height = image::ComputeMipDimension(level, texture->height()); + + int bytes_per_row = image::ComputePitch(texture->format(), mip_width); + for (unsigned yy = 0; yy < mip_height; ++yy) { + if (memcmp(data, expected, bytes_per_row) != 0) { + return false; + } + expected += bytes_per_row; + data += helper.pitch(); + } + return true; +} + +} // anonymous namespace. + +class Texture2DTest : public testing::Test { + protected: + Texture2DTest() + : object_manager_(g_service_locator) { + } + + virtual void SetUp() { + pack_ = object_manager_->CreatePack(); + } + + virtual void TearDown() { + pack_->Destroy(); + } + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ObjectManager> object_manager_; + Pack* pack_; +}; + +TEST_F(Texture2DTest, Basic) { + Texture2D* texture = pack()->CreateTexture2D(8, 8, Texture::ARGB8, 1, false); + ASSERT_TRUE(texture != NULL); + EXPECT_TRUE(texture->IsA(Texture2D::GetApparentClass())); + EXPECT_TRUE(texture->IsA(Texture::GetApparentClass())); + EXPECT_TRUE(texture->IsA(ParamObject::GetApparentClass())); + EXPECT_EQ(texture->format(), Texture::ARGB8); + EXPECT_EQ(texture->levels(), 1); + EXPECT_FALSE(texture->render_surfaces_enabled()); + EXPECT_EQ(0, Texture::kMaxDimension >> Texture::kMaxLevels); + EXPECT_EQ(1, Texture::kMaxDimension >> (Texture::kMaxLevels - 1)); +} + +TEST_F(Texture2DTest, SetRect) { + const int kWidth = 8; + const int kHeight = 8; + const int kLevels = 2; + const int kDestMip = 1; + const unsigned kDestX = 1u; + const unsigned kDestY = 1u; + Texture2D* texture = pack()->CreateTexture2D( + kWidth, kHeight, Texture::ARGB8, kLevels, false); + ASSERT_TRUE(texture != NULL); + static const uint8 kExpected1[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + EXPECT_TRUE(CompareTexture(texture, 1, kExpected1)); + const int kSrcWidth = 2; + const int kSrcHeight = 2; + static const uint8 kSourcePixels[] = { + 0x01, 0x01, 0x01, 0x02, 0x03, 0x03, 0x03, 0x04, + 0x05, 0x05, 0x05, 0x06, 0x07, 0x07, 0x07, 0x08, + }; + const int kSourcePitch = sizeof(kSourcePixels[0]) * kSrcWidth * 4; + // normal copy + texture->SetRect(kDestMip, kDestX, kDestY, + kSrcWidth, kSrcHeight, kSourcePixels, kSourcePitch); + static const uint8 kExpected2[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, + 0x03, 0x03, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x06, + 0x07, 0x07, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + EXPECT_TRUE(CompareTexture(texture, 1, kExpected2)); + // flipped copy + texture->SetRect( + kDestMip, kDestX, kDestY, + kSrcWidth, kSrcHeight, + kSourcePixels + kSourcePitch, + -kSourcePitch); + static const uint8 kExpected3[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x06, + 0x07, 0x07, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x02, + 0x03, 0x03, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, + + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + }; + EXPECT_TRUE(CompareTexture(texture, 1, kExpected3)); +} + +class TextureCUBETest : public testing::Test { + protected: + TextureCUBETest() + : object_manager_(g_service_locator) { + } + + virtual void SetUp() { + pack_ = object_manager_->CreatePack(); + } + + virtual void TearDown() { + pack_->Destroy(); + } + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ObjectManager> object_manager_; + Pack* pack_; +}; + +TEST_F(TextureCUBETest, Basic) { + TextureCUBE* texture = + pack()->CreateTextureCUBE(8, Texture::ARGB8, 1, false); + ASSERT_TRUE(texture != NULL); + EXPECT_TRUE(texture->IsA(TextureCUBE::GetApparentClass())); + EXPECT_TRUE(texture->IsA(Texture::GetApparentClass())); + EXPECT_TRUE(texture->IsA(ParamObject::GetApparentClass())); + EXPECT_EQ(texture->format(), Texture::ARGB8); + EXPECT_EQ(texture->levels(), 1); + EXPECT_FALSE(texture->render_surfaces_enabled()); +} + +} // namespace o3d + diff --git a/o3d/import/cross/camera_info.cc b/o3d/import/cross/camera_info.cc index 905c337..3f46e4c 100644 --- a/o3d/import/cross/camera_info.cc +++ b/o3d/import/cross/camera_info.cc @@ -1,98 +1,98 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "import/cross/camera_info.h"
-
-namespace o3d {
-
-O3D_OBJECT_BASE_DEFN_CLASS(
- "o3djs.CameraInfo", CameraInfo, JSONObject);
-O3D_OBJECT_BASE_DEFN_CLASS(
- "o3djs.PerspectiveCameraInfo", PerspectiveCameraInfo, CameraInfo);
-O3D_OBJECT_BASE_DEFN_CLASS(
- "o3djs.OrthographicCameraInfo", OrthographicCameraInfo, CameraInfo);
-
-const char* CameraInfo::kAspectRatioParamName = "aspectRatio";
-const char* CameraInfo::kNearZParamName = "nearZ";
-const char* CameraInfo::kFarZParamName = "farZ";
-const char* CameraInfo::kTransformValueName = "transform";
-const char* CameraInfo::kEyeValueName = "eye";
-const char* CameraInfo::kTargetValueName = "target";
-const char* CameraInfo::kUpValueName = "up";
-
-CameraInfo::CameraInfo(ServiceLocator* service_locator)
- : JSONObject(service_locator) {
- RegisterParamRef(kAspectRatioParamName, &aspect_ratio_param_);
- RegisterParamRef(kNearZParamName, &near_z_param_);
- RegisterParamRef(kFarZParamName, &far_z_param_);
- RegisterJSONValue(kTransformValueName, &transform_value_);
- RegisterJSONValue(kEyeValueName, &eye_value_);
- RegisterJSONValue(kTargetValueName, &target_value_);
- RegisterJSONValue(kUpValueName, &up_value_);
-
- set_aspect_ratio(1.0f);
- set_near_z(0.01f);
- set_far_z(10000.0f);
-}
-
-const char* OrthographicCameraInfo::kMagXParamName = "magX";
-const char* OrthographicCameraInfo::kMagYParamName = "magY";
-
-OrthographicCameraInfo::OrthographicCameraInfo(ServiceLocator* service_locator)
- : CameraInfo(service_locator) {
- RegisterParamRef(kMagXParamName, &mag_x_param_);
- RegisterParamRef(kMagYParamName, &mag_y_param_);
-
- set_mag_x(1.0f);
- set_mag_y(1.0f);
-}
-
-ObjectBase::Ref OrthographicCameraInfo::Create(
- ServiceLocator* service_locator) {
- return ObjectBase::Ref(new OrthographicCameraInfo(service_locator));
-}
-
-const char* PerspectiveCameraInfo::kFieldOfViewYParamName = "fieldOfViewY";
-
-PerspectiveCameraInfo::PerspectiveCameraInfo(ServiceLocator* service_locator)
- : CameraInfo(service_locator) {
- RegisterParamRef(kFieldOfViewYParamName, &field_of_view_y_param_);
-
- set_field_of_view_y(30.0f * 3.14159f / 180.0f);
-}
-
-ObjectBase::Ref PerspectiveCameraInfo::Create(ServiceLocator* service_locator) {
- return ObjectBase::Ref(new PerspectiveCameraInfo(service_locator));
-}
-
-} // namespace o3d
-
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "import/cross/camera_info.h" + +namespace o3d { + +O3D_OBJECT_BASE_DEFN_CLASS( + "o3djs.CameraInfo", CameraInfo, JSONObject); +O3D_OBJECT_BASE_DEFN_CLASS( + "o3djs.PerspectiveCameraInfo", PerspectiveCameraInfo, CameraInfo); +O3D_OBJECT_BASE_DEFN_CLASS( + "o3djs.OrthographicCameraInfo", OrthographicCameraInfo, CameraInfo); + +const char* CameraInfo::kAspectRatioParamName = "aspectRatio"; +const char* CameraInfo::kNearZParamName = "nearZ"; +const char* CameraInfo::kFarZParamName = "farZ"; +const char* CameraInfo::kTransformValueName = "transform"; +const char* CameraInfo::kEyeValueName = "eye"; +const char* CameraInfo::kTargetValueName = "target"; +const char* CameraInfo::kUpValueName = "up"; + +CameraInfo::CameraInfo(ServiceLocator* service_locator) + : JSONObject(service_locator) { + RegisterParamRef(kAspectRatioParamName, &aspect_ratio_param_); + RegisterParamRef(kNearZParamName, &near_z_param_); + RegisterParamRef(kFarZParamName, &far_z_param_); + RegisterJSONValue(kTransformValueName, &transform_value_); + RegisterJSONValue(kEyeValueName, &eye_value_); + RegisterJSONValue(kTargetValueName, &target_value_); + RegisterJSONValue(kUpValueName, &up_value_); + + set_aspect_ratio(1.0f); + set_near_z(0.01f); + set_far_z(10000.0f); +} + +const char* OrthographicCameraInfo::kMagXParamName = "magX"; +const char* OrthographicCameraInfo::kMagYParamName = "magY"; + +OrthographicCameraInfo::OrthographicCameraInfo(ServiceLocator* service_locator) + : CameraInfo(service_locator) { + RegisterParamRef(kMagXParamName, &mag_x_param_); + RegisterParamRef(kMagYParamName, &mag_y_param_); + + set_mag_x(1.0f); + set_mag_y(1.0f); +} + +ObjectBase::Ref OrthographicCameraInfo::Create( + ServiceLocator* service_locator) { + return ObjectBase::Ref(new OrthographicCameraInfo(service_locator)); +} + +const char* PerspectiveCameraInfo::kFieldOfViewYParamName = "fieldOfViewY"; + +PerspectiveCameraInfo::PerspectiveCameraInfo(ServiceLocator* service_locator) + : CameraInfo(service_locator) { + RegisterParamRef(kFieldOfViewYParamName, &field_of_view_y_param_); + + set_field_of_view_y(30.0f * 3.14159f / 180.0f); +} + +ObjectBase::Ref PerspectiveCameraInfo::Create(ServiceLocator* service_locator) { + return ObjectBase::Ref(new PerspectiveCameraInfo(service_locator)); +} + +} // namespace o3d + + diff --git a/o3d/import/cross/camera_info_test.cc b/o3d/import/cross/camera_info_test.cc index ce1b105..662b161 100644 --- a/o3d/import/cross/camera_info_test.cc +++ b/o3d/import/cross/camera_info_test.cc @@ -1,222 +1,222 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "tests/common/win/testing_common.h"
-#include "import/cross/camera_info.h"
-#include "core/cross/class_manager.h"
-#include "core/cross/object_manager.h"
-#include "core/cross/pack.h"
-#include "core/cross/service_dependency.h"
-#include "utils/cross/math_gtest.h"
-
-namespace o3d {
-
-namespace {
-
-// A class to test CameraInfo.
-class TestCameraInfo : public CameraInfo {
- public:
- typedef SmartPointer<TestCameraInfo> Ref;
-
- static ObjectBase::Ref Create(ServiceLocator* service_locator);
-
- private:
- explicit TestCameraInfo(ServiceLocator* service_locator)
- : CameraInfo(service_locator) {
- }
-
- O3D_OBJECT_BASE_DECL_CLASS(TestCameraInfo, CameraInfo);
- DISALLOW_COPY_AND_ASSIGN(TestCameraInfo);
-};
-
-O3D_OBJECT_BASE_DEFN_CLASS(
- "o3djs.TestCameraInfo", TestCameraInfo, CameraInfo);
-
-ObjectBase::Ref TestCameraInfo::Create(
- ServiceLocator* service_locator) {
- return ObjectBase::Ref(new TestCameraInfo(service_locator));
-}
-
-} // anonymous namespace
-
-class TestCameraInfoTest : public testing::Test {
- protected:
- TestCameraInfoTest()
- : class_manager_(g_service_locator),
- object_manager_(g_service_locator),
- class_register_(g_service_locator) {
- }
-
- virtual void SetUp();
- virtual void TearDown();
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ClassManager> class_manager_;
- ServiceDependency<ObjectManager> object_manager_;
- ClassManager::Register<TestCameraInfo> class_register_;
- Pack* pack_;
-};
-
-void TestCameraInfoTest::SetUp() {
- pack_ = object_manager_->CreatePack();
-}
-
-void TestCameraInfoTest::TearDown() {
- object_manager_->DestroyPack(pack_);
-}
-
-// Creates a TestCameraInfo, tests basic properties.
-TEST_F(TestCameraInfoTest, TestTestCameraInfo) {
- TestCameraInfo *camera_info = pack()->Create<TestCameraInfo>();
- EXPECT_TRUE(camera_info->IsA(TestCameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
-
- EXPECT_EQ(camera_info->aspect_ratio(), 1.0f);
- EXPECT_EQ(camera_info->near_z(), 0.01f);
- EXPECT_EQ(camera_info->far_z(), 10000.0f);
- EXPECT_TRUE(camera_info->transform() == NULL);
-
- Transform* transform = pack()->Create<Transform>();
- ASSERT_TRUE(transform != NULL);
-
- camera_info->set_aspect_ratio(2.0f);
- camera_info->set_near_z(0.02f);
- camera_info->set_far_z(20000.0f);
- camera_info->set_transform(transform);
- camera_info->set_eye(Float3(10.0f, 20.0f, 30.0f));
- camera_info->set_target(Float3(11.0f, 22.0f, 33.0f));
- camera_info->set_up(Float3(12.0f, 23.0f, 34.0f));
-
- EXPECT_EQ(camera_info->aspect_ratio(), 2.0f);
- EXPECT_EQ(camera_info->near_z(), 0.02f);
- EXPECT_EQ(camera_info->far_z(), 20000.0f);
- EXPECT_TRUE(camera_info->transform() == transform);
- EXPECT_EQ(camera_info->eye(), Float3(10.0f, 20.0f, 30.0f));
- EXPECT_EQ(camera_info->target(), Float3(11.0f, 22.0f, 33.0f));
- EXPECT_EQ(camera_info->up(), Float3(12.0f, 23.0f, 34.0f));
-}
-
-class PerspectiveCameraInfoTest : public testing::Test {
- protected:
- PerspectiveCameraInfoTest()
- : class_manager_(g_service_locator),
- object_manager_(g_service_locator),
- class_register_(g_service_locator) {
- }
-
- virtual void SetUp();
- virtual void TearDown();
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ClassManager> class_manager_;
- ServiceDependency<ObjectManager> object_manager_;
- ClassManager::Register<PerspectiveCameraInfo> class_register_;
- Pack* pack_;
-};
-
-void PerspectiveCameraInfoTest::SetUp() {
- pack_ = object_manager_->CreatePack();
-}
-
-void PerspectiveCameraInfoTest::TearDown() {
- object_manager_->DestroyPack(pack_);
-}
-
-// Creates a PerspectiveCameraInfo, tests basic properties.
-TEST_F(PerspectiveCameraInfoTest, TestPerspectiveCameraInfo) {
- PerspectiveCameraInfo *camera_info = pack()->Create<PerspectiveCameraInfo>();
- EXPECT_TRUE(camera_info->IsA(PerspectiveCameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
-
- EXPECT_EQ(camera_info->field_of_view_y(), 30.0f * 3.14159f / 180.0f);
-
- camera_info->set_field_of_view_y(60.0f * 3.14159f / 180.0f);
-
- EXPECT_EQ(camera_info->field_of_view_y(), 60.0f * 3.14159f / 180.0f);
-}
-
-class OrthographicCameraInfoTest : public testing::Test {
- protected:
- OrthographicCameraInfoTest()
- : class_manager_(g_service_locator),
- object_manager_(g_service_locator),
- class_register_(g_service_locator) {
- }
-
- virtual void SetUp();
- virtual void TearDown();
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ClassManager> class_manager_;
- ServiceDependency<ObjectManager> object_manager_;
- ClassManager::Register<OrthographicCameraInfo> class_register_;
- Pack* pack_;
-};
-
-void OrthographicCameraInfoTest::SetUp() {
- pack_ = object_manager_->CreatePack();
-}
-
-void OrthographicCameraInfoTest::TearDown() {
- object_manager_->DestroyPack(pack_);
-}
-
-// Creates a OrthographicCameraInfo, tests basic properties.
-TEST_F(OrthographicCameraInfoTest, TestOrthographicCameraInfo) {
- OrthographicCameraInfo *camera_info =
- pack()->Create<OrthographicCameraInfo>();
- EXPECT_TRUE(camera_info->IsA(OrthographicCameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass()));
- EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass()));
-
- EXPECT_EQ(camera_info->mag_x(), 1.0f);
- EXPECT_EQ(camera_info->mag_y(), 1.0f);
-
- camera_info->set_mag_x(2.0f);
- camera_info->set_mag_y(3.0f);
-
- EXPECT_EQ(camera_info->mag_x(), 2.0f);
- EXPECT_EQ(camera_info->mag_y(), 3.0f);
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests/common/win/testing_common.h" +#include "import/cross/camera_info.h" +#include "core/cross/class_manager.h" +#include "core/cross/object_manager.h" +#include "core/cross/pack.h" +#include "core/cross/service_dependency.h" +#include "utils/cross/math_gtest.h" + +namespace o3d { + +namespace { + +// A class to test CameraInfo. +class TestCameraInfo : public CameraInfo { + public: + typedef SmartPointer<TestCameraInfo> Ref; + + static ObjectBase::Ref Create(ServiceLocator* service_locator); + + private: + explicit TestCameraInfo(ServiceLocator* service_locator) + : CameraInfo(service_locator) { + } + + O3D_OBJECT_BASE_DECL_CLASS(TestCameraInfo, CameraInfo); + DISALLOW_COPY_AND_ASSIGN(TestCameraInfo); +}; + +O3D_OBJECT_BASE_DEFN_CLASS( + "o3djs.TestCameraInfo", TestCameraInfo, CameraInfo); + +ObjectBase::Ref TestCameraInfo::Create( + ServiceLocator* service_locator) { + return ObjectBase::Ref(new TestCameraInfo(service_locator)); +} + +} // anonymous namespace + +class TestCameraInfoTest : public testing::Test { + protected: + TestCameraInfoTest() + : class_manager_(g_service_locator), + object_manager_(g_service_locator), + class_register_(g_service_locator) { + } + + virtual void SetUp(); + virtual void TearDown(); + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ClassManager> class_manager_; + ServiceDependency<ObjectManager> object_manager_; + ClassManager::Register<TestCameraInfo> class_register_; + Pack* pack_; +}; + +void TestCameraInfoTest::SetUp() { + pack_ = object_manager_->CreatePack(); +} + +void TestCameraInfoTest::TearDown() { + object_manager_->DestroyPack(pack_); +} + +// Creates a TestCameraInfo, tests basic properties. +TEST_F(TestCameraInfoTest, TestTestCameraInfo) { + TestCameraInfo *camera_info = pack()->Create<TestCameraInfo>(); + EXPECT_TRUE(camera_info->IsA(TestCameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass())); + + EXPECT_EQ(camera_info->aspect_ratio(), 1.0f); + EXPECT_EQ(camera_info->near_z(), 0.01f); + EXPECT_EQ(camera_info->far_z(), 10000.0f); + EXPECT_TRUE(camera_info->transform() == NULL); + + Transform* transform = pack()->Create<Transform>(); + ASSERT_TRUE(transform != NULL); + + camera_info->set_aspect_ratio(2.0f); + camera_info->set_near_z(0.02f); + camera_info->set_far_z(20000.0f); + camera_info->set_transform(transform); + camera_info->set_eye(Float3(10.0f, 20.0f, 30.0f)); + camera_info->set_target(Float3(11.0f, 22.0f, 33.0f)); + camera_info->set_up(Float3(12.0f, 23.0f, 34.0f)); + + EXPECT_EQ(camera_info->aspect_ratio(), 2.0f); + EXPECT_EQ(camera_info->near_z(), 0.02f); + EXPECT_EQ(camera_info->far_z(), 20000.0f); + EXPECT_TRUE(camera_info->transform() == transform); + EXPECT_EQ(camera_info->eye(), Float3(10.0f, 20.0f, 30.0f)); + EXPECT_EQ(camera_info->target(), Float3(11.0f, 22.0f, 33.0f)); + EXPECT_EQ(camera_info->up(), Float3(12.0f, 23.0f, 34.0f)); +} + +class PerspectiveCameraInfoTest : public testing::Test { + protected: + PerspectiveCameraInfoTest() + : class_manager_(g_service_locator), + object_manager_(g_service_locator), + class_register_(g_service_locator) { + } + + virtual void SetUp(); + virtual void TearDown(); + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ClassManager> class_manager_; + ServiceDependency<ObjectManager> object_manager_; + ClassManager::Register<PerspectiveCameraInfo> class_register_; + Pack* pack_; +}; + +void PerspectiveCameraInfoTest::SetUp() { + pack_ = object_manager_->CreatePack(); +} + +void PerspectiveCameraInfoTest::TearDown() { + object_manager_->DestroyPack(pack_); +} + +// Creates a PerspectiveCameraInfo, tests basic properties. +TEST_F(PerspectiveCameraInfoTest, TestPerspectiveCameraInfo) { + PerspectiveCameraInfo *camera_info = pack()->Create<PerspectiveCameraInfo>(); + EXPECT_TRUE(camera_info->IsA(PerspectiveCameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass())); + + EXPECT_EQ(camera_info->field_of_view_y(), 30.0f * 3.14159f / 180.0f); + + camera_info->set_field_of_view_y(60.0f * 3.14159f / 180.0f); + + EXPECT_EQ(camera_info->field_of_view_y(), 60.0f * 3.14159f / 180.0f); +} + +class OrthographicCameraInfoTest : public testing::Test { + protected: + OrthographicCameraInfoTest() + : class_manager_(g_service_locator), + object_manager_(g_service_locator), + class_register_(g_service_locator) { + } + + virtual void SetUp(); + virtual void TearDown(); + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ClassManager> class_manager_; + ServiceDependency<ObjectManager> object_manager_; + ClassManager::Register<OrthographicCameraInfo> class_register_; + Pack* pack_; +}; + +void OrthographicCameraInfoTest::SetUp() { + pack_ = object_manager_->CreatePack(); +} + +void OrthographicCameraInfoTest::TearDown() { + object_manager_->DestroyPack(pack_); +} + +// Creates a OrthographicCameraInfo, tests basic properties. +TEST_F(OrthographicCameraInfoTest, TestOrthographicCameraInfo) { + OrthographicCameraInfo *camera_info = + pack()->Create<OrthographicCameraInfo>(); + EXPECT_TRUE(camera_info->IsA(OrthographicCameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(CameraInfo::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(JSONObject::GetApparentClass())); + EXPECT_TRUE(camera_info->IsA(ParamObject::GetApparentClass())); + + EXPECT_EQ(camera_info->mag_x(), 1.0f); + EXPECT_EQ(camera_info->mag_y(), 1.0f); + + camera_info->set_mag_x(2.0f); + camera_info->set_mag_y(3.0f); + + EXPECT_EQ(camera_info->mag_x(), 2.0f); + EXPECT_EQ(camera_info->mag_y(), 3.0f); +} + +} // namespace o3d + diff --git a/o3d/import/cross/destination_buffer.cc b/o3d/import/cross/destination_buffer.cc index 5aa3124..d52bda8 100644 --- a/o3d/import/cross/destination_buffer.cc +++ b/o3d/import/cross/destination_buffer.cc @@ -1,79 +1,79 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "import/cross/destination_buffer.h"
-
-namespace o3d {
-
-O3D_OBJECT_BASE_DEFN_CLASS(
- "o3djs.DestinationBuffer", DestinationBuffer, VertexBuffer);
-
-DestinationBuffer::DestinationBuffer(ServiceLocator* service_locator)
- : VertexBuffer(service_locator),
- buffer_() {
-}
-
-DestinationBuffer::~DestinationBuffer() {
- ConcreteFree();
-}
-
-void DestinationBuffer::ConcreteFree() {
- buffer_.reset();
-}
-
-bool DestinationBuffer::ConcreteAllocate(size_t size_in_bytes) {
- ConcreteFree();
-
- buffer_.reset(new char[size_in_bytes]);
-
- return true;
-}
-
-bool DestinationBuffer::ConcreteLock(AccessMode access_mode,
- void **buffer_data) {
- if (!buffer_.get()) {
- return false;
- }
-
- *buffer_data = reinterpret_cast<void*>(buffer_.get());
- return true;
-}
-
-bool DestinationBuffer::ConcreteUnlock() {
- return buffer_.get() != NULL;
-}
-
-ObjectBase::Ref DestinationBuffer::Create(ServiceLocator* service_locator) {
- return ObjectBase::Ref(new DestinationBuffer(service_locator));
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "import/cross/destination_buffer.h" + +namespace o3d { + +O3D_OBJECT_BASE_DEFN_CLASS( + "o3djs.DestinationBuffer", DestinationBuffer, VertexBuffer); + +DestinationBuffer::DestinationBuffer(ServiceLocator* service_locator) + : VertexBuffer(service_locator), + buffer_() { +} + +DestinationBuffer::~DestinationBuffer() { + ConcreteFree(); +} + +void DestinationBuffer::ConcreteFree() { + buffer_.reset(); +} + +bool DestinationBuffer::ConcreteAllocate(size_t size_in_bytes) { + ConcreteFree(); + + buffer_.reset(new char[size_in_bytes]); + + return true; +} + +bool DestinationBuffer::ConcreteLock(AccessMode access_mode, + void **buffer_data) { + if (!buffer_.get()) { + return false; + } + + *buffer_data = reinterpret_cast<void*>(buffer_.get()); + return true; +} + +bool DestinationBuffer::ConcreteUnlock() { + return buffer_.get() != NULL; +} + +ObjectBase::Ref DestinationBuffer::Create(ServiceLocator* service_locator) { + return ObjectBase::Ref(new DestinationBuffer(service_locator)); +} + +} // namespace o3d + diff --git a/o3d/import/cross/destination_buffer_test.cc b/o3d/import/cross/destination_buffer_test.cc index c7f69d3..2ad2513 100644 --- a/o3d/import/cross/destination_buffer_test.cc +++ b/o3d/import/cross/destination_buffer_test.cc @@ -1,104 +1,104 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "tests/common/win/testing_common.h"
-#include "import/cross/destination_buffer.h"
-#include "core/cross/class_manager.h"
-#include "core/cross/object_manager.h"
-#include "core/cross/pack.h"
-#include "core/cross/service_dependency.h"
-
-namespace o3d {
-
-class DestinationBufferTest : public testing::Test {
- protected:
- DestinationBufferTest()
- : class_manager_(g_service_locator),
- object_manager_(g_service_locator) {
- class_manager_->AddTypedClass<DestinationBuffer>();
- }
-
- virtual void SetUp();
- virtual void TearDown();
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ClassManager> class_manager_;
- ServiceDependency<ObjectManager> object_manager_;
- Pack* pack_;
-};
-
-void DestinationBufferTest::SetUp() {
- pack_ = object_manager_->CreatePack();
-}
-
-void DestinationBufferTest::TearDown() {
- object_manager_->DestroyPack(pack_);
-}
-
-
-// Creates a Destination buffer, tests basic properties, and checks that writing
-// then reading data works.
-TEST_F(DestinationBufferTest, TestDestinationBuffer) {
- Buffer *buffer = pack()->Create<DestinationBuffer>();
- EXPECT_TRUE(buffer->IsA(DestinationBuffer::GetApparentClass()));
- EXPECT_TRUE(buffer->IsA(VertexBuffer::GetApparentClass()));
- EXPECT_TRUE(buffer->IsA(Buffer::GetApparentClass()));
-
- const size_t kSize = 100;
- Field* field = buffer->CreateField(UInt32Field::GetApparentClass(), 1);
- ASSERT_TRUE(field != NULL);
- ASSERT_TRUE(buffer->AllocateElements(kSize));
- EXPECT_EQ(kSize * sizeof(uint32), buffer->GetSizeInBytes()); // NOLINT
-
- // Put some data into the buffer.
- uint32 *data = NULL;
- ASSERT_TRUE(buffer->LockAs(Buffer::WRITE_ONLY, &data));
- ASSERT_TRUE(data != NULL);
- for (uint32 i = 0; i < kSize; ++i) {
- data[i] = i;
- }
- ASSERT_TRUE(buffer->Unlock());
-
- data = NULL;
- // Read the data from the buffer, checks that it's the expected values.
- ASSERT_TRUE(buffer->LockAs(Buffer::READ_ONLY, &data));
- ASSERT_TRUE(data != NULL);
- for (uint32 i = 0; i < kSize; ++i) {
- EXPECT_EQ(i, data[i]);
- }
- ASSERT_TRUE(buffer->Unlock());
-}
-
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests/common/win/testing_common.h" +#include "import/cross/destination_buffer.h" +#include "core/cross/class_manager.h" +#include "core/cross/object_manager.h" +#include "core/cross/pack.h" +#include "core/cross/service_dependency.h" + +namespace o3d { + +class DestinationBufferTest : public testing::Test { + protected: + DestinationBufferTest() + : class_manager_(g_service_locator), + object_manager_(g_service_locator) { + class_manager_->AddTypedClass<DestinationBuffer>(); + } + + virtual void SetUp(); + virtual void TearDown(); + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ClassManager> class_manager_; + ServiceDependency<ObjectManager> object_manager_; + Pack* pack_; +}; + +void DestinationBufferTest::SetUp() { + pack_ = object_manager_->CreatePack(); +} + +void DestinationBufferTest::TearDown() { + object_manager_->DestroyPack(pack_); +} + + +// Creates a Destination buffer, tests basic properties, and checks that writing +// then reading data works. +TEST_F(DestinationBufferTest, TestDestinationBuffer) { + Buffer *buffer = pack()->Create<DestinationBuffer>(); + EXPECT_TRUE(buffer->IsA(DestinationBuffer::GetApparentClass())); + EXPECT_TRUE(buffer->IsA(VertexBuffer::GetApparentClass())); + EXPECT_TRUE(buffer->IsA(Buffer::GetApparentClass())); + + const size_t kSize = 100; + Field* field = buffer->CreateField(UInt32Field::GetApparentClass(), 1); + ASSERT_TRUE(field != NULL); + ASSERT_TRUE(buffer->AllocateElements(kSize)); + EXPECT_EQ(kSize * sizeof(uint32), buffer->GetSizeInBytes()); // NOLINT + + // Put some data into the buffer. + uint32 *data = NULL; + ASSERT_TRUE(buffer->LockAs(Buffer::WRITE_ONLY, &data)); + ASSERT_TRUE(data != NULL); + for (uint32 i = 0; i < kSize; ++i) { + data[i] = i; + } + ASSERT_TRUE(buffer->Unlock()); + + data = NULL; + // Read the data from the buffer, checks that it's the expected values. + ASSERT_TRUE(buffer->LockAs(Buffer::READ_ONLY, &data)); + ASSERT_TRUE(data != NULL); + for (uint32 i = 0; i < kSize; ++i) { + EXPECT_EQ(i, data[i]); + } + ASSERT_TRUE(buffer->Unlock()); +} + + +} // namespace o3d + diff --git a/o3d/import/cross/json_object.cc b/o3d/import/cross/json_object.cc index a681a3a..906e730 100644 --- a/o3d/import/cross/json_object.cc +++ b/o3d/import/cross/json_object.cc @@ -1,66 +1,66 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This file defines the JSON Object class.
-
-#include "import/cross/json_object.h"
-
-namespace o3d {
-
-O3D_OBJECT_BASE_DEFN_CLASS("o3djs.JSONObject", JSONObject, ParamObject);
-
-JSONObject::JSONObject(ServiceLocator* service_locator)
- : ParamObject(service_locator) {
-}
-
-void JSONObject::AddProperty(const String& name, JSONValue* value) {
- std::pair<NameValueMap::iterator, bool> result = properties_.insert(
- std::pair<String, JSONValue::Ref>(name, JSONValue::Ref(value)));
- DCHECK(result.second);
-}
-
-void JSONObject::Serialize(StructuredWriter* writer) const {
- writer->WritePropertyName("object");
- writer->OpenObject();
- for (NameValueMap::const_iterator it(properties_.begin());
- it != properties_.end();
- ++it) {
- if (it->second->exists()) {
- writer->WritePropertyName(it->first);
- it->second->Serialize(writer);
- }
- }
- writer->CloseObject();
-}
-
-} // namespace o3d
-
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This file defines the JSON Object class. + +#include "import/cross/json_object.h" + +namespace o3d { + +O3D_OBJECT_BASE_DEFN_CLASS("o3djs.JSONObject", JSONObject, ParamObject); + +JSONObject::JSONObject(ServiceLocator* service_locator) + : ParamObject(service_locator) { +} + +void JSONObject::AddProperty(const String& name, JSONValue* value) { + std::pair<NameValueMap::iterator, bool> result = properties_.insert( + std::pair<String, JSONValue::Ref>(name, JSONValue::Ref(value))); + DCHECK(result.second); +} + +void JSONObject::Serialize(StructuredWriter* writer) const { + writer->WritePropertyName("object"); + writer->OpenObject(); + for (NameValueMap::const_iterator it(properties_.begin()); + it != properties_.end(); + ++it) { + if (it->second->exists()) { + writer->WritePropertyName(it->first); + it->second->Serialize(writer); + } + } + writer->CloseObject(); +} + +} // namespace o3d + + diff --git a/o3d/import/cross/json_object_test.cc b/o3d/import/cross/json_object_test.cc index 8935f8b..137de45 100644 --- a/o3d/import/cross/json_object_test.cc +++ b/o3d/import/cross/json_object_test.cc @@ -1,448 +1,448 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "tests/common/win/testing_common.h"
-#include "import/cross/json_object.h"
-#include "core/cross/class_manager.h"
-#include "core/cross/object_manager.h"
-#include "core/cross/pack.h"
-#include "core/cross/service_dependency.h"
-#include "utils/cross/string_writer.h"
-#include "utils/cross/json_writer.h"
-#include "utils/cross/math_gtest.h"
-
-namespace o3d {
-
-namespace {
-
-// A class to test JSONObject
-class TestJSONObject : public JSONObject {
- public:
- typedef SmartPointer<TestJSONObject> Ref;
-
- static const char* kFloatValueName;
- static const char* kFloat2ValueName;
- static const char* kFloat3ValueName;
- static const char* kFloat4ValueName;
- static const char* kMatrix4ValueName;
- static const char* kIntegerValueName;
- static const char* kBooleanValueName;
- static const char* kStringValueName;
- static const char* kTransformValueName;
- static const char* kMaterialValueName;
- static const char* kOptionalFloatValueName;
- static const char* kOptionalFloat2ValueName;
- static const char* kOptionalFloat3ValueName;
- static const char* kOptionalFloat4ValueName;
- static const char* kOptionalMatrix4ValueName;
- static const char* kOptionalIntegerValueName;
- static const char* kOptionalBooleanValueName;
- static const char* kOptionalStringValueName;
-
- float float_value() const {
- return float_value_->value();
- }
-
- void set_float_value(float value) {
- float_value_->set_value(value);
- }
-
- Float2 float2_value() const {
- return float2_value_->value();
- }
-
- void set_float2_value(const Float2& value) {
- float2_value_->set_value(value);
- }
-
- Float3 float3_value() const {
- return float3_value_->value();
- }
-
- void set_float3_value(const Float3& value) {
- float3_value_->set_value(value);
- }
-
- Float4 float4_value() const {
- return float4_value_->value();
- }
-
- void set_float4_value(const Float4& value) {
- float4_value_->set_value(value);
- }
-
- Matrix4 matrix4_value() const {
- return matrix4_value_->value();
- }
-
- void set_matrix4_value(const Matrix4& value) {
- matrix4_value_->set_value(value);
- }
-
- int integer_value() const {
- return integer_value_->value();
- }
-
- void set_integer_value(int value) {
- integer_value_->set_value(value);
- }
-
- bool boolean_value() const {
- return boolean_value_->value();
- }
-
- void set_boolean_value(bool value) {
- boolean_value_->set_value(value);
- }
-
- String string_value() const {
- return string_value_->value();
- }
-
- void set_string_value(const String& value) {
- string_value_->set_value(value);
- }
-
- Transform* transform_value() const {
- return transform_value_->value();
- }
-
- void set_transform_value(Transform* value) {
- transform_value_->set_value(value);
- }
-
- Material* material_value() const {
- return material_value_->value();
- }
-
- void set_material_value(Material* value) {
- material_value_->set_value(value);
- }
-
- float optional_float_value() const {
- return optional_float_value_->value();
- }
-
- void set_optional_float_value(float value) {
- optional_float_value_->set_value(value);
- }
-
- Float2 optional_float2_value() const {
- return optional_float2_value_->value();
- }
-
- void set_optional_float2_value(const Float2& value) {
- optional_float2_value_->set_value(value);
- }
-
- Float3 optional_float3_value() const {
- return optional_float3_value_->value();
- }
-
- void set_optional_float3_value(const Float3& value) {
- optional_float3_value_->set_value(value);
- }
-
- Float4 optional_float4_value() const {
- return optional_float4_value_->value();
- }
-
- void set_optional_float4_value(const Float4& value) {
- optional_float4_value_->set_value(value);
- }
-
- Matrix4 optional_matrix4_value() const {
- return optional_matrix4_value_->value();
- }
-
- void set_optional_matrix4_value(const Matrix4& value) {
- optional_matrix4_value_->set_value(value);
- }
-
- int optional_integer_value() const {
- return optional_integer_value_->value();
- }
-
- void set_optional_integer_value(int value) {
- optional_integer_value_->set_value(value);
- }
-
- bool optional_boolean_value() const {
- return optional_boolean_value_->value();
- }
-
- void set_optional_boolean_value(bool value) {
- optional_boolean_value_->set_value(value);
- }
-
- String optional_string_value() const {
- return optional_string_value_->value();
- }
-
- void set_optional_string_value(const String& value) {
- optional_string_value_->set_value(value);
- }
-
- static ObjectBase::Ref Create(ServiceLocator* service_locator) {
- return ObjectBase::Ref(new TestJSONObject(service_locator));
- }
-
- private:
- explicit TestJSONObject(ServiceLocator* service_locator)
- : JSONObject(service_locator) {
- RegisterJSONValue(kFloatValueName, &float_value_);
- RegisterJSONValue(kFloat2ValueName, &float2_value_);
- RegisterJSONValue(kFloat3ValueName, &float3_value_);
- RegisterJSONValue(kFloat4ValueName, &float4_value_);
- RegisterJSONValue(kMatrix4ValueName, &matrix4_value_);
- RegisterJSONValue(kIntegerValueName, &integer_value_);
- RegisterJSONValue(kBooleanValueName, &boolean_value_);
- RegisterJSONValue(kStringValueName, &string_value_);
- RegisterJSONValue(kTransformValueName, &transform_value_);
- RegisterJSONValue(kMaterialValueName, &material_value_);
- RegisterJSONValue(kOptionalFloatValueName, &optional_float_value_);
- RegisterJSONValue(kOptionalFloat2ValueName, &optional_float2_value_);
- RegisterJSONValue(kOptionalFloat3ValueName, &optional_float3_value_);
- RegisterJSONValue(kOptionalFloat4ValueName, &optional_float4_value_);
- RegisterJSONValue(kOptionalMatrix4ValueName, &optional_matrix4_value_);
- RegisterJSONValue(kOptionalIntegerValueName, &optional_integer_value_);
- RegisterJSONValue(kOptionalBooleanValueName, &optional_boolean_value_);
- RegisterJSONValue(kOptionalStringValueName, &optional_string_value_);
- }
-
- // One of each type of JSONValue
- JSONFloat::Ref float_value_;
- JSONFloat2::Ref float2_value_;
- JSONFloat3::Ref float3_value_;
- JSONFloat4::Ref float4_value_;
- JSONMatrix4::Ref matrix4_value_;
- JSONInteger::Ref integer_value_;
- JSONBoolean::Ref boolean_value_;
- JSONString::Ref string_value_;
- JSONTransform::Ref transform_value_;
- JSONMaterial::Ref material_value_;
- JSONOptionalFloat::Ref optional_float_value_;
- JSONOptionalFloat2::Ref optional_float2_value_;
- JSONOptionalFloat3::Ref optional_float3_value_;
- JSONOptionalFloat4::Ref optional_float4_value_;
- JSONOptionalMatrix4::Ref optional_matrix4_value_;
- JSONOptionalInteger::Ref optional_integer_value_;
- JSONOptionalBoolean::Ref optional_boolean_value_;
- JSONOptionalString::Ref optional_string_value_;
-
- O3D_OBJECT_BASE_DECL_CLASS(TestJSONObject, JSONObject);
- DISALLOW_COPY_AND_ASSIGN(TestJSONObject);
-};
-
-O3D_OBJECT_BASE_DEFN_CLASS("TestJSONObject", TestJSONObject, JSONObject);
-
-const char* TestJSONObject::kFloatValueName = "floatValue";
-const char* TestJSONObject::kFloat2ValueName = "float2Value";
-const char* TestJSONObject::kFloat3ValueName = "float3Value";
-const char* TestJSONObject::kFloat4ValueName = "float4Value";
-const char* TestJSONObject::kMatrix4ValueName = "matrix4Value";
-const char* TestJSONObject::kIntegerValueName = "integerValue";
-const char* TestJSONObject::kBooleanValueName = "booleanValue";
-const char* TestJSONObject::kStringValueName = "stringValue";
-const char* TestJSONObject::kTransformValueName = "transformValue";
-const char* TestJSONObject::kMaterialValueName = "materialValue";
-const char* TestJSONObject::kOptionalFloatValueName = "optionalFloatValue";
-const char* TestJSONObject::kOptionalFloat2ValueName = "optionalFloat2Value";
-const char* TestJSONObject::kOptionalFloat3ValueName = "optionalFloat3Value";
-const char* TestJSONObject::kOptionalFloat4ValueName = "optionalFloat4Value";
-const char* TestJSONObject::kOptionalMatrix4ValueName = "optionalMatrix4Value";
-const char* TestJSONObject::kOptionalIntegerValueName = "optionalIntegerValue";
-const char* TestJSONObject::kOptionalStringValueName = "optionalStringValue";
-const char* TestJSONObject::kOptionalBooleanValueName = "optionalBooleanValue";
-
-} // anonymous namespace
-
-class JSONObjectTest : public testing::Test {
- protected:
- JSONObjectTest()
- : class_manager_(g_service_locator),
- object_manager_(g_service_locator),
- class_register_(g_service_locator) {
- }
-
- virtual void SetUp();
- virtual void TearDown();
-
- Pack* pack() { return pack_; }
-
- private:
- ServiceDependency<ClassManager> class_manager_;
- ServiceDependency<ObjectManager> object_manager_;
- ClassManager::Register<TestJSONObject> class_register_;
- Pack* pack_;
-};
-
-void JSONObjectTest::SetUp() {
- pack_ = object_manager_->CreatePack();
-}
-
-void JSONObjectTest::TearDown() {
- object_manager_->DestroyPack(pack_);
-}
-
-// Creates a JSONObject, tests basic properties.
-TEST_F(JSONObjectTest, BasicTest) {
- TestJSONObject* object = pack()->Create<TestJSONObject>();
- ASSERT_TRUE(object != NULL);
- EXPECT_TRUE(object->IsA(TestJSONObject::GetApparentClass()));
- EXPECT_TRUE(object->IsA(JSONObject::GetApparentClass()));
- EXPECT_TRUE(object->IsA(ParamObject::GetApparentClass()));
-
- // Test everything has its default value.
- EXPECT_EQ(object->float_value(), 0.0f);
- EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f));
- EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f));
- EXPECT_EQ(object->float3_value(), Float3(0.0f, 0.0f, 0.0f));
- EXPECT_EQ(object->float4_value(), Float4(0.0f, 0.0f, 0.0f, 0.0f));
- EXPECT_EQ(object->matrix4_value(), Matrix4::identity());
- EXPECT_EQ(object->integer_value(), 0);
- EXPECT_FALSE(object->boolean_value());
- EXPECT_EQ(object->string_value(), "");
- EXPECT_TRUE(object->transform_value() == NULL);
- EXPECT_TRUE(object->material_value() == NULL);
-
- // Test that all the values got created by serializing it.
- {
- StringWriter output(StringWriter::LF);
- JsonWriter json_writer(&output, 2);
- object->Serialize(&json_writer);
- json_writer.Close();
- // There is an assumption here that objects will be serialized in this
- // order.
- static const char *kExpected =
- "\"object\": {\n"
- " \"booleanValue\": false,\n"
- " \"float2Value\": [0,0],\n"
- " \"float3Value\": [0,0,0],\n"
- " \"float4Value\": [0,0,0,0],\n"
- " \"floatValue\": 0,\n"
- " \"integerValue\": 0,\n"
- " \"materialValue\": null,\n"
- " \"matrix4Value\": [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],\n"
- " \"stringValue\": \"\",\n"
- " \"transformValue\": null\n"
- "}\n";
- EXPECT_EQ(kExpected, output.ToString());
- }
-}
-
-TEST_F(JSONObjectTest, SetTest) {
- TestJSONObject* object = pack()->Create<TestJSONObject>();
- ASSERT_TRUE(object != NULL);
- // Test setting the values.
- Matrix4 test_matrix(Vector4(1.0f, 2.0f, 3.0f, 4.0f),
- Vector4(2.0f, 2.0f, 8.0f, 9.0f),
- Vector4(3.0f, 5.0f, 3.0f, 10.0f),
- Vector4(4.0f, 6.0f, 7.0f, 4.0f));
- Matrix4 test_matrix2(Vector4(11.0f, 12.0f, 13.0f, 14.0f),
- Vector4(12.0f, 12.0f, 18.0f, 19.0f),
- Vector4(13.0f, 15.0f, 13.0f, 110.0f),
- Vector4(14.0f, 16.0f, 17.0f, 14.0f));
-
- object->set_float_value(1.2f);
- object->set_float2_value(Float2(3.4f, 5.6f));
- object->set_float3_value(Float3(7.8f, 9.0f, 10.0f));
- object->set_float4_value(Float4(11.0f, 12.0f, 13.0f, 14.0f));
- object->set_matrix4_value(test_matrix);
- object->set_integer_value(123);
- object->set_boolean_value(true);
- object->set_string_value("test");
- object->set_optional_float_value(15.0f);
- object->set_optional_float2_value(Float2(14.0f, 15.0f));
- object->set_optional_float3_value(Float3(13.0f, 14.0f, 15.0f));
- object->set_optional_float4_value(Float4(12.0f, 13.0f, 14.0f, 15.0f));
- object->set_optional_matrix4_value(test_matrix2);
- object->set_optional_integer_value(456);
- object->set_optional_string_value("hello");
- object->set_optional_boolean_value(false);
-
- Transform* transform = pack()->Create<Transform>();
- Material* material = pack()->Create<Material>();
- ASSERT_TRUE(transform != NULL);
- ASSERT_TRUE(material != NULL);
-
- object->set_transform_value(transform);
- object->set_material_value(material);
-
- // Check the new values
- EXPECT_EQ(object->float_value(), 1.2f);
- EXPECT_EQ(object->float2_value(), Float2(3.4f, 5.6f));
- EXPECT_EQ(object->float3_value(), Float3(7.8f, 9.0f, 10.0f));
- EXPECT_EQ(object->float4_value(), Float4(11.0f, 12.0f, 13.0f, 14.0f));
- EXPECT_EQ(object->matrix4_value(), test_matrix);
- EXPECT_EQ(object->integer_value(), 123);
- EXPECT_TRUE(object->boolean_value());
- EXPECT_EQ(object->string_value(), "test");
- EXPECT_TRUE(object->transform_value() == transform);
- EXPECT_TRUE(object->material_value() == material);
-
- // See that they got set by serializing them.
- {
- StringWriter output(StringWriter::LF);
- JsonWriter json_writer(&output, 2);
- object->Serialize(&json_writer);
- json_writer.Close();
-
- char buffer[1000];
- static const char* kExpected =
- "\"object\": {\n"
- " \"booleanValue\": true,\n"
- " \"float2Value\": [3.4,5.6],\n"
- " \"float3Value\": [7.8,9,10],\n"
- " \"float4Value\": [11,12,13,14],\n"
- " \"floatValue\": 1.2,\n"
- " \"integerValue\": 123,\n"
- " \"materialValue\": {\"ref\":%d},\n"
- " \"matrix4Value\": [[1,2,3,4],[2,2,8,9],[3,5,3,10],[4,6,7,4]],\n"
- " \"optionalBooleanValue\": false,\n"
- " \"optionalFloat2Value\": [14,15],\n"
- " \"optionalFloat3Value\": [13,14,15],\n"
- " \"optionalFloat4Value\": [12,13,14,15],\n"
- " \"optionalFloatValue\": 15,\n"
- " \"optionalIntegerValue\": 456,\n"
- " \"optionalMatrix4Value\": "
- "[[11,12,13,14],[12,12,18,19],[13,15,13,110],[14,16,17,14]],\n"
- " \"optionalStringValue\": \"hello\",\n"
- " \"stringValue\": \"test\",\n"
- " \"transformValue\": {\"ref\":%d}\n"
- "}\n";
- sprintf(buffer, kExpected, material->id(), transform->id()); // NOLINT
- EXPECT_EQ(static_cast<const char*>(buffer), output.ToString());
- }
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests/common/win/testing_common.h" +#include "import/cross/json_object.h" +#include "core/cross/class_manager.h" +#include "core/cross/object_manager.h" +#include "core/cross/pack.h" +#include "core/cross/service_dependency.h" +#include "utils/cross/string_writer.h" +#include "utils/cross/json_writer.h" +#include "utils/cross/math_gtest.h" + +namespace o3d { + +namespace { + +// A class to test JSONObject +class TestJSONObject : public JSONObject { + public: + typedef SmartPointer<TestJSONObject> Ref; + + static const char* kFloatValueName; + static const char* kFloat2ValueName; + static const char* kFloat3ValueName; + static const char* kFloat4ValueName; + static const char* kMatrix4ValueName; + static const char* kIntegerValueName; + static const char* kBooleanValueName; + static const char* kStringValueName; + static const char* kTransformValueName; + static const char* kMaterialValueName; + static const char* kOptionalFloatValueName; + static const char* kOptionalFloat2ValueName; + static const char* kOptionalFloat3ValueName; + static const char* kOptionalFloat4ValueName; + static const char* kOptionalMatrix4ValueName; + static const char* kOptionalIntegerValueName; + static const char* kOptionalBooleanValueName; + static const char* kOptionalStringValueName; + + float float_value() const { + return float_value_->value(); + } + + void set_float_value(float value) { + float_value_->set_value(value); + } + + Float2 float2_value() const { + return float2_value_->value(); + } + + void set_float2_value(const Float2& value) { + float2_value_->set_value(value); + } + + Float3 float3_value() const { + return float3_value_->value(); + } + + void set_float3_value(const Float3& value) { + float3_value_->set_value(value); + } + + Float4 float4_value() const { + return float4_value_->value(); + } + + void set_float4_value(const Float4& value) { + float4_value_->set_value(value); + } + + Matrix4 matrix4_value() const { + return matrix4_value_->value(); + } + + void set_matrix4_value(const Matrix4& value) { + matrix4_value_->set_value(value); + } + + int integer_value() const { + return integer_value_->value(); + } + + void set_integer_value(int value) { + integer_value_->set_value(value); + } + + bool boolean_value() const { + return boolean_value_->value(); + } + + void set_boolean_value(bool value) { + boolean_value_->set_value(value); + } + + String string_value() const { + return string_value_->value(); + } + + void set_string_value(const String& value) { + string_value_->set_value(value); + } + + Transform* transform_value() const { + return transform_value_->value(); + } + + void set_transform_value(Transform* value) { + transform_value_->set_value(value); + } + + Material* material_value() const { + return material_value_->value(); + } + + void set_material_value(Material* value) { + material_value_->set_value(value); + } + + float optional_float_value() const { + return optional_float_value_->value(); + } + + void set_optional_float_value(float value) { + optional_float_value_->set_value(value); + } + + Float2 optional_float2_value() const { + return optional_float2_value_->value(); + } + + void set_optional_float2_value(const Float2& value) { + optional_float2_value_->set_value(value); + } + + Float3 optional_float3_value() const { + return optional_float3_value_->value(); + } + + void set_optional_float3_value(const Float3& value) { + optional_float3_value_->set_value(value); + } + + Float4 optional_float4_value() const { + return optional_float4_value_->value(); + } + + void set_optional_float4_value(const Float4& value) { + optional_float4_value_->set_value(value); + } + + Matrix4 optional_matrix4_value() const { + return optional_matrix4_value_->value(); + } + + void set_optional_matrix4_value(const Matrix4& value) { + optional_matrix4_value_->set_value(value); + } + + int optional_integer_value() const { + return optional_integer_value_->value(); + } + + void set_optional_integer_value(int value) { + optional_integer_value_->set_value(value); + } + + bool optional_boolean_value() const { + return optional_boolean_value_->value(); + } + + void set_optional_boolean_value(bool value) { + optional_boolean_value_->set_value(value); + } + + String optional_string_value() const { + return optional_string_value_->value(); + } + + void set_optional_string_value(const String& value) { + optional_string_value_->set_value(value); + } + + static ObjectBase::Ref Create(ServiceLocator* service_locator) { + return ObjectBase::Ref(new TestJSONObject(service_locator)); + } + + private: + explicit TestJSONObject(ServiceLocator* service_locator) + : JSONObject(service_locator) { + RegisterJSONValue(kFloatValueName, &float_value_); + RegisterJSONValue(kFloat2ValueName, &float2_value_); + RegisterJSONValue(kFloat3ValueName, &float3_value_); + RegisterJSONValue(kFloat4ValueName, &float4_value_); + RegisterJSONValue(kMatrix4ValueName, &matrix4_value_); + RegisterJSONValue(kIntegerValueName, &integer_value_); + RegisterJSONValue(kBooleanValueName, &boolean_value_); + RegisterJSONValue(kStringValueName, &string_value_); + RegisterJSONValue(kTransformValueName, &transform_value_); + RegisterJSONValue(kMaterialValueName, &material_value_); + RegisterJSONValue(kOptionalFloatValueName, &optional_float_value_); + RegisterJSONValue(kOptionalFloat2ValueName, &optional_float2_value_); + RegisterJSONValue(kOptionalFloat3ValueName, &optional_float3_value_); + RegisterJSONValue(kOptionalFloat4ValueName, &optional_float4_value_); + RegisterJSONValue(kOptionalMatrix4ValueName, &optional_matrix4_value_); + RegisterJSONValue(kOptionalIntegerValueName, &optional_integer_value_); + RegisterJSONValue(kOptionalBooleanValueName, &optional_boolean_value_); + RegisterJSONValue(kOptionalStringValueName, &optional_string_value_); + } + + // One of each type of JSONValue + JSONFloat::Ref float_value_; + JSONFloat2::Ref float2_value_; + JSONFloat3::Ref float3_value_; + JSONFloat4::Ref float4_value_; + JSONMatrix4::Ref matrix4_value_; + JSONInteger::Ref integer_value_; + JSONBoolean::Ref boolean_value_; + JSONString::Ref string_value_; + JSONTransform::Ref transform_value_; + JSONMaterial::Ref material_value_; + JSONOptionalFloat::Ref optional_float_value_; + JSONOptionalFloat2::Ref optional_float2_value_; + JSONOptionalFloat3::Ref optional_float3_value_; + JSONOptionalFloat4::Ref optional_float4_value_; + JSONOptionalMatrix4::Ref optional_matrix4_value_; + JSONOptionalInteger::Ref optional_integer_value_; + JSONOptionalBoolean::Ref optional_boolean_value_; + JSONOptionalString::Ref optional_string_value_; + + O3D_OBJECT_BASE_DECL_CLASS(TestJSONObject, JSONObject); + DISALLOW_COPY_AND_ASSIGN(TestJSONObject); +}; + +O3D_OBJECT_BASE_DEFN_CLASS("TestJSONObject", TestJSONObject, JSONObject); + +const char* TestJSONObject::kFloatValueName = "floatValue"; +const char* TestJSONObject::kFloat2ValueName = "float2Value"; +const char* TestJSONObject::kFloat3ValueName = "float3Value"; +const char* TestJSONObject::kFloat4ValueName = "float4Value"; +const char* TestJSONObject::kMatrix4ValueName = "matrix4Value"; +const char* TestJSONObject::kIntegerValueName = "integerValue"; +const char* TestJSONObject::kBooleanValueName = "booleanValue"; +const char* TestJSONObject::kStringValueName = "stringValue"; +const char* TestJSONObject::kTransformValueName = "transformValue"; +const char* TestJSONObject::kMaterialValueName = "materialValue"; +const char* TestJSONObject::kOptionalFloatValueName = "optionalFloatValue"; +const char* TestJSONObject::kOptionalFloat2ValueName = "optionalFloat2Value"; +const char* TestJSONObject::kOptionalFloat3ValueName = "optionalFloat3Value"; +const char* TestJSONObject::kOptionalFloat4ValueName = "optionalFloat4Value"; +const char* TestJSONObject::kOptionalMatrix4ValueName = "optionalMatrix4Value"; +const char* TestJSONObject::kOptionalIntegerValueName = "optionalIntegerValue"; +const char* TestJSONObject::kOptionalStringValueName = "optionalStringValue"; +const char* TestJSONObject::kOptionalBooleanValueName = "optionalBooleanValue"; + +} // anonymous namespace + +class JSONObjectTest : public testing::Test { + protected: + JSONObjectTest() + : class_manager_(g_service_locator), + object_manager_(g_service_locator), + class_register_(g_service_locator) { + } + + virtual void SetUp(); + virtual void TearDown(); + + Pack* pack() { return pack_; } + + private: + ServiceDependency<ClassManager> class_manager_; + ServiceDependency<ObjectManager> object_manager_; + ClassManager::Register<TestJSONObject> class_register_; + Pack* pack_; +}; + +void JSONObjectTest::SetUp() { + pack_ = object_manager_->CreatePack(); +} + +void JSONObjectTest::TearDown() { + object_manager_->DestroyPack(pack_); +} + +// Creates a JSONObject, tests basic properties. +TEST_F(JSONObjectTest, BasicTest) { + TestJSONObject* object = pack()->Create<TestJSONObject>(); + ASSERT_TRUE(object != NULL); + EXPECT_TRUE(object->IsA(TestJSONObject::GetApparentClass())); + EXPECT_TRUE(object->IsA(JSONObject::GetApparentClass())); + EXPECT_TRUE(object->IsA(ParamObject::GetApparentClass())); + + // Test everything has its default value. + EXPECT_EQ(object->float_value(), 0.0f); + EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f)); + EXPECT_EQ(object->float2_value(), Float2(0.0f, 0.0f)); + EXPECT_EQ(object->float3_value(), Float3(0.0f, 0.0f, 0.0f)); + EXPECT_EQ(object->float4_value(), Float4(0.0f, 0.0f, 0.0f, 0.0f)); + EXPECT_EQ(object->matrix4_value(), Matrix4::identity()); + EXPECT_EQ(object->integer_value(), 0); + EXPECT_FALSE(object->boolean_value()); + EXPECT_EQ(object->string_value(), ""); + EXPECT_TRUE(object->transform_value() == NULL); + EXPECT_TRUE(object->material_value() == NULL); + + // Test that all the values got created by serializing it. + { + StringWriter output(StringWriter::LF); + JsonWriter json_writer(&output, 2); + object->Serialize(&json_writer); + json_writer.Close(); + // There is an assumption here that objects will be serialized in this + // order. + static const char *kExpected = + "\"object\": {\n" + " \"booleanValue\": false,\n" + " \"float2Value\": [0,0],\n" + " \"float3Value\": [0,0,0],\n" + " \"float4Value\": [0,0,0,0],\n" + " \"floatValue\": 0,\n" + " \"integerValue\": 0,\n" + " \"materialValue\": null,\n" + " \"matrix4Value\": [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],\n" + " \"stringValue\": \"\",\n" + " \"transformValue\": null\n" + "}\n"; + EXPECT_EQ(kExpected, output.ToString()); + } +} + +TEST_F(JSONObjectTest, SetTest) { + TestJSONObject* object = pack()->Create<TestJSONObject>(); + ASSERT_TRUE(object != NULL); + // Test setting the values. + Matrix4 test_matrix(Vector4(1.0f, 2.0f, 3.0f, 4.0f), + Vector4(2.0f, 2.0f, 8.0f, 9.0f), + Vector4(3.0f, 5.0f, 3.0f, 10.0f), + Vector4(4.0f, 6.0f, 7.0f, 4.0f)); + Matrix4 test_matrix2(Vector4(11.0f, 12.0f, 13.0f, 14.0f), + Vector4(12.0f, 12.0f, 18.0f, 19.0f), + Vector4(13.0f, 15.0f, 13.0f, 110.0f), + Vector4(14.0f, 16.0f, 17.0f, 14.0f)); + + object->set_float_value(1.2f); + object->set_float2_value(Float2(3.4f, 5.6f)); + object->set_float3_value(Float3(7.8f, 9.0f, 10.0f)); + object->set_float4_value(Float4(11.0f, 12.0f, 13.0f, 14.0f)); + object->set_matrix4_value(test_matrix); + object->set_integer_value(123); + object->set_boolean_value(true); + object->set_string_value("test"); + object->set_optional_float_value(15.0f); + object->set_optional_float2_value(Float2(14.0f, 15.0f)); + object->set_optional_float3_value(Float3(13.0f, 14.0f, 15.0f)); + object->set_optional_float4_value(Float4(12.0f, 13.0f, 14.0f, 15.0f)); + object->set_optional_matrix4_value(test_matrix2); + object->set_optional_integer_value(456); + object->set_optional_string_value("hello"); + object->set_optional_boolean_value(false); + + Transform* transform = pack()->Create<Transform>(); + Material* material = pack()->Create<Material>(); + ASSERT_TRUE(transform != NULL); + ASSERT_TRUE(material != NULL); + + object->set_transform_value(transform); + object->set_material_value(material); + + // Check the new values + EXPECT_EQ(object->float_value(), 1.2f); + EXPECT_EQ(object->float2_value(), Float2(3.4f, 5.6f)); + EXPECT_EQ(object->float3_value(), Float3(7.8f, 9.0f, 10.0f)); + EXPECT_EQ(object->float4_value(), Float4(11.0f, 12.0f, 13.0f, 14.0f)); + EXPECT_EQ(object->matrix4_value(), test_matrix); + EXPECT_EQ(object->integer_value(), 123); + EXPECT_TRUE(object->boolean_value()); + EXPECT_EQ(object->string_value(), "test"); + EXPECT_TRUE(object->transform_value() == transform); + EXPECT_TRUE(object->material_value() == material); + + // See that they got set by serializing them. + { + StringWriter output(StringWriter::LF); + JsonWriter json_writer(&output, 2); + object->Serialize(&json_writer); + json_writer.Close(); + + char buffer[1000]; + static const char* kExpected = + "\"object\": {\n" + " \"booleanValue\": true,\n" + " \"float2Value\": [3.4,5.6],\n" + " \"float3Value\": [7.8,9,10],\n" + " \"float4Value\": [11,12,13,14],\n" + " \"floatValue\": 1.2,\n" + " \"integerValue\": 123,\n" + " \"materialValue\": {\"ref\":%d},\n" + " \"matrix4Value\": [[1,2,3,4],[2,2,8,9],[3,5,3,10],[4,6,7,4]],\n" + " \"optionalBooleanValue\": false,\n" + " \"optionalFloat2Value\": [14,15],\n" + " \"optionalFloat3Value\": [13,14,15],\n" + " \"optionalFloat4Value\": [12,13,14,15],\n" + " \"optionalFloatValue\": 15,\n" + " \"optionalIntegerValue\": 456,\n" + " \"optionalMatrix4Value\": " + "[[11,12,13,14],[12,12,18,19],[13,15,13,110],[14,16,17,14]],\n" + " \"optionalStringValue\": \"hello\",\n" + " \"stringValue\": \"test\",\n" + " \"transformValue\": {\"ref\":%d}\n" + "}\n"; + sprintf(buffer, kExpected, material->id(), transform->id()); // NOLINT + EXPECT_EQ(static_cast<const char*>(buffer), output.ToString()); + } +} + +} // namespace o3d + diff --git a/o3d/plugin/cross/texture_static_glue.cc b/o3d/plugin/cross/texture_static_glue.cc index b5cf995..a3da6ef 100644 --- a/o3d/plugin/cross/texture_static_glue.cc +++ b/o3d/plugin/cross/texture_static_glue.cc @@ -1,606 +1,606 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <vector>
-#include "core/cross/pointer_utils.h"
-#include "core/cross/error.h"
-#include "core/cross/math_utilities.h"
-#include "core/cross/texture.h"
-#include "core/cross/image_utils.h"
-
-namespace {
-
-void SetRectCheck(o3d::Texture* self,
- void* data,
- int pitch,
- int destination_x,
- int destination_y,
- int texture_width,
- int texture_height,
- int source_width,
- int source_height,
- const std::vector<float>& values) {
- unsigned num_components;
- unsigned swizzle[4] = {2, 1, 0, 3};
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- swizzle[0] = 0;
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- const o3d::Texture::RGBASwizzleIndices& indices =
- self->GetABGR32FSwizzleIndices();
- for (int ii = 0; ii < 4; ++ii) {
- swizzle[ii] = indices[ii];
- }
- break;
- }
- default:
- DCHECK(false);
- return;
- }
-
- // clip
- int source_x = 0;
- int source_y = 0;
- int copy_width = source_width;
- int copy_height = source_height;
-
- if (destination_x < 0) {
- copy_width += destination_x;
- source_x -= destination_x;
- destination_x = 0;
- }
- if (destination_x + copy_width > static_cast<int>(texture_width)) {
- copy_width -= destination_x + copy_width - texture_width;
- }
-
- if (destination_y < 0) {
- copy_height += destination_y;
- source_y -= destination_y;
- destination_y = 0;
- }
- if (destination_y + copy_height > static_cast<int>(texture_height)) {
- copy_height -= destination_y + copy_height - texture_height;
- }
-
- if (copy_width <= 0 || copy_height <= 0) {
- return;
- }
-
- const float* source =
- &values[0] +
- (source_y * source_width + source_x) * num_components;
- unsigned source_stride = (source_width - copy_width) * num_components;
- switch (self->format()) {
- case o3d::Texture::ABGR16F: {
- uint16* dest_line =
- static_cast<uint16*>(data) +
- (destination_y * texture_width + destination_x) * num_components;
- for (; copy_height > 0; --copy_height) {
- uint16* destination = dest_line;
- for (int xx = 0; xx < copy_width; ++xx) {
- for (unsigned element = 0; element < num_components; ++element) {
- destination[element] = Vectormath::Aos::FloatToHalf(
- source[swizzle[element]]);
- }
- destination += num_components;
- source += num_components;
- }
- dest_line = o3d::AddPointerOffset<uint16*>(dest_line, pitch);
- source += source_stride;
- }
- break;
- }
- case o3d::Texture::R32F:
- case o3d::Texture::ABGR32F: {
- float* dest_line =
- static_cast<float*>(data) +
- (destination_y * texture_width + destination_x) * num_components;
- for (; copy_height > 0; --copy_height) {
- float* destination = dest_line;
- for (int xx = 0; xx < copy_width; ++xx) {
- for (unsigned element = 0; element < num_components; ++element) {
- destination[element] = source[swizzle[element]];
- }
- destination += num_components;
- source += num_components;
- }
- dest_line = o3d::AddPointerOffset<float*>(dest_line, pitch);
- source += source_stride;
- }
- break;
- }
- default: {
- uint8* dest_line =
- static_cast<uint8*>(data) +
- (destination_y * texture_width + destination_x) * 4;
- for (; copy_height > 0; --copy_height) {
- uint8* destination = dest_line;
- for (int xx = 0; xx < copy_width; ++xx) {
- destination[0] = static_cast<unsigned char>(
- source[swizzle[0]] * 255.0f);
- destination[1] = static_cast<unsigned char>(
- source[swizzle[1]] * 255.0f);
- destination[2] = static_cast<unsigned char>(
- source[swizzle[2]] * 255.0f);
- destination[3] = (num_components == 4) ?
- static_cast<unsigned char>(source[swizzle[3]] * 255.0f) : 255;
- destination += 4;
- source += num_components;
- }
- dest_line = o3d::AddPointerOffset<uint8*>(dest_line, pitch);
- source += source_stride;
- }
- break;
- }
- }
-}
-
-void SetRectCheck2D(o3d::Texture2D* self,
- int level,
- int destination_x,
- int destination_y,
- int source_width,
- const std::vector<float>& values,
- bool check_needed) {
- if (level < 0 || level >= self->levels()) {
- O3D_ERROR(self->service_locator())
- << "level (" << level << " out of range";
- return;
- }
- if (values.empty() || source_width <= 0) {
- return;
- }
- unsigned num_values = values.size();
- unsigned texture_width = std::max(self->width() >> level, 1);
- unsigned texture_height = std::max(self->height() >> level, 1);
- unsigned num_components;
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- break;
- }
- default:
- O3D_ERROR(self->service_locator())
- << "Texture::Set not supported for this type of texture";
- return;
- }
- if (num_values % num_components != 0) {
- O3D_ERROR(self->service_locator())
- << "The number of elements passed in must be a multiple of "
- << num_components;
- }
- unsigned num_elements = num_values / num_components;
- if (num_elements % source_width != 0) {
- O3D_ERROR(self->service_locator())
- << "The number of elements passed in must be a multiple of the "
- << "width";
- return;
- }
- unsigned source_height = num_elements / source_width;
- if (check_needed) {
- unsigned needed = num_components * texture_width * texture_height;
- if (num_values != needed) {
- O3D_ERROR(self->service_locator())
- << "needed " << needed << " values but " << num_values
- << " passed in.";
- return;
- }
- }
- o3d::Texture2D::LockHelper helper(self, level, o3d::Texture::kWriteOnly);
- void* data = helper.GetData();
- if (!data) {
- O3D_ERROR(self->service_locator()) << "could not lock texture";
- return;
- }
-
- SetRectCheck(self, data, helper.pitch(),
- destination_x, destination_y,
- texture_width, texture_height,
- source_width, source_height,
- values);
-}
-
-void SetRectCheckCUBE(o3d::TextureCUBE* self,
- o3d::TextureCUBE::CubeFace face,
- int level,
- int destination_x,
- int destination_y,
- int source_width,
- const std::vector<float>& values,
- bool check_needed) {
- if (level < 0 || level >= self->levels()) {
- O3D_ERROR(self->service_locator())
- << "level (" << level << " out of range";
- return;
- }
- if (values.empty() || source_width <= 0) {
- return;
- }
- unsigned num_values = values.size();
- unsigned texture_width = std::max(self->edge_length() >> level, 1);
- unsigned texture_height = texture_width;
- unsigned num_components;
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- break;
- }
- default:
- O3D_ERROR(self->service_locator())
- << "Texture::Set not supported for this type of texture";
- return;
- }
- if (num_values % num_components != 0) {
- O3D_ERROR(self->service_locator())
- << "The number of elements passed in must be a multiple of "
- << num_components;
- }
- unsigned num_elements = num_values / num_components;
- if (num_elements % source_width != 0) {
- O3D_ERROR(self->service_locator())
- << "The number of elements passed in must be a multiple of the "
- << "width";
- return;
- }
- unsigned source_height = num_elements / source_width;
- if (check_needed) {
- unsigned needed = num_components * texture_width * texture_height;
- if (num_values != needed) {
- O3D_ERROR(self->service_locator())
- << "needed " << needed << " values but " << num_values
- << " passed in.";
- return;
- }
- }
- o3d::TextureCUBE::LockHelper helper(
- self, face, level, o3d::Texture::kWriteOnly);
- void* data = helper.GetData();
- if (!data) {
- O3D_ERROR(self->service_locator()) << "could not lock texture";
- return;
- }
-
- SetRectCheck(self, data, helper.pitch(),
- destination_x, destination_y,
- texture_width, texture_height,
- source_width, source_height,
- values);
-}
-
-// Assumes dst points to width * height * num_components floats.
-void GetRect(o3d::Texture* self,
- const void* src_data,
- int src_pitch,
- int x,
- int y,
- int width,
- int height,
- float* dst) {
- unsigned num_components;
- unsigned swizzle[4] = {2, 1, 0, 3};
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- swizzle[0] = 0;
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- const o3d::Texture::RGBASwizzleIndices& indices =
- self->GetABGR32FSwizzleIndices();
- for (int ii = 0; ii < 4; ++ii) {
- swizzle[ii] = indices[ii];
- }
- break;
- }
- default:
- DCHECK(false);
- return;
- }
-
- switch (self->format()) {
- case o3d::Texture::ABGR16F: {
- uint16* src_line = o3d::PointerFromVoidPointer<uint16*>(
- src_data,
- (y * src_pitch)) + x * num_components;
- for (; height > 0; --height) {
- uint16* src = src_line;
- for (int xx = 0; xx < width; ++xx) {
- for (unsigned element = 0; element < num_components; ++element) {
- dst[swizzle[element]] = Vectormath::Aos::HalfToFloat(src[element]);
- }
- dst += num_components;
- src += num_components;
- }
- src_line = o3d::AddPointerOffset<uint16*>(src_line, src_pitch);
- }
- break;
- }
- case o3d::Texture::R32F:
- case o3d::Texture::ABGR32F: {
- float* src_line = o3d::PointerFromVoidPointer<float*>(
- src_data,
- (y * src_pitch)) + x * num_components;
- for (; height > 0; --height) {
- float* src = src_line;
- for (int xx = 0; xx < width; ++xx) {
- for (unsigned element = 0; element < num_components; ++element) {
- dst[swizzle[element]] = src[element];
- }
- dst += num_components;
- src += num_components;
- }
- src_line = o3d::AddPointerOffset<float*>(src_line, src_pitch);
- }
- break;
- }
- default: {
- uint8* src_line = o3d::PointerFromVoidPointer<uint8*>(
- src_data,
- (y * src_pitch)) + x * num_components;
- for (; height > 0; --height) {
- uint8* src = src_line;
- for (int xx = 0; xx < width; ++xx) {
- dst[swizzle[0]] = static_cast<float>(src[0]) / 255.0f;
- dst[swizzle[1]] = static_cast<float>(src[1]) / 255.0f;
- dst[swizzle[2]] = static_cast<float>(src[2]) / 255.0f;
- if (num_components == 4) {
- dst[swizzle[3]] = static_cast<float>(src[3]) / 255.0f;
- }
- dst += num_components;
- src += 4;
- }
- src_line = o3d::AddPointerOffset<uint8*>(src_line, src_pitch);
- }
- break;
- }
- }
-}
-
-} // anonymous namespace
-
-namespace glue {
-namespace namespace_o3d {
-namespace class_Texture2D {
-
-void userglue_method_SetRect(o3d::Texture2D* self,
- int level,
- int destination_x,
- int destination_y,
- int source_width,
- const std::vector<float>& values) {
- SetRectCheck2D(self,
- level,
- destination_x,
- destination_y,
- source_width,
- values,
- false);
-}
-void userglue_method_Set(o3d::Texture2D* self,
- int level,
- const std::vector<float>& values) {
- SetRectCheck2D(
- self, level, 0, 0,
- o3d::image::ComputeMipDimension(level, self->width()),
- values, true);
-}
-std::vector<float> userglue_method_GetRect(o3d::Texture2D* self,
- int level,
- int x,
- int y,
- int width,
- int height) {
- std::vector<float> empty;
- if (level < 0 || level >= self->levels()) {
- O3D_ERROR(self->service_locator())
- << "level (" << level << " out of range";
- return empty;
- }
- if (width <= 0 || height <= 0) {
- O3D_ERROR(self->service_locator())
- << "width and height must be positive";
- return empty;
- }
-
- int mip_width =
- static_cast<int>(o3d::image::ComputeMipDimension(level, self->width()));
- int mip_height =
- static_cast<int>(o3d::image::ComputeMipDimension(level, self->height()));
-
- if (x < 0 || x + width > mip_width || y < 0 || y + height > mip_height) {
- O3D_ERROR(self->service_locator()) << "area out of range";
- return empty;
- }
-
- unsigned num_components;
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- break;
- }
- default:
- O3D_ERROR(self->service_locator())
- << "Texture::Set not supported for this type of texture";
- return empty;
- }
- o3d::Texture2D::LockHelper helper(self, level, o3d::Texture::kReadOnly);
- void* data = helper.GetData();
- if (!data) {
- O3D_ERROR(self->service_locator()) << "could not lock texture";
- return empty;
- }
-
- std::vector<float> values(width * height * num_components, 0);
- GetRect(self, data, helper.pitch(), x, y, width, height, &values[0]);
- return values;
-}
-
-} // namespace class_Texture2D
-
-namespace class_TextureCUBE {
-
-void userglue_method_SetRect(o3d::TextureCUBE* self,
- o3d::TextureCUBE::CubeFace face,
- int level,
- int destination_x,
- int destination_y,
- int source_width,
- const std::vector<float>& values) {
- SetRectCheckCUBE(self,
- face,
- level,
- destination_x,
- destination_y,
- source_width,
- values,
- false);
-}
-void userglue_method_Set(o3d::TextureCUBE* self,
- o3d::TextureCUBE::CubeFace face,
- int level,
- const std::vector<float>& values) {
- SetRectCheckCUBE(self, face, level, 0, 0,
- o3d::image::ComputeMipDimension(level, self->edge_length()),
- values, true);
-}
-std::vector<float> userglue_method_GetRect(o3d::TextureCUBE* self,
- o3d::TextureCUBE::CubeFace face,
- int level,
- int x,
- int y,
- int width,
- int height) {
- std::vector<float> empty;
- if (level < 0 || level >= self->levels()) {
- O3D_ERROR(self->service_locator())
- << "level (" << level << " out of range";
- return empty;
- }
- if (width <= 0 || height <= 0) {
- O3D_ERROR(self->service_locator())
- << "width and height must be positive";
- return empty;
- }
-
- int mip_length = static_cast<int>(o3d::image::ComputeMipDimension(
- level, self->edge_length()));
-
- if (x < 0 || x + width > mip_length || y < 0 || y + height > mip_length) {
- O3D_ERROR(self->service_locator()) << "area out of range";
- return empty;
- }
-
- unsigned num_components;
- switch (self->format()) {
- case o3d::Texture::XRGB8:
- num_components = 3;
- break;
- case o3d::Texture::R32F:
- num_components = 1;
- break;
- case o3d::Texture::ARGB8:
- case o3d::Texture::ABGR16F:
- num_components = 4;
- break;
- case o3d::Texture::ABGR32F: {
- num_components = 4;
- break;
- }
- default:
- O3D_ERROR(self->service_locator())
- << "Texture::Set not supported for this type of texture";
- return empty;
- }
- o3d::TextureCUBE::LockHelper helper(
- self, face, level, o3d::Texture::kReadOnly);
- void* data = helper.GetData();
- if (!data) {
- O3D_ERROR(self->service_locator()) << "could not lock texture";
- return empty;
- }
-
- std::vector<float> values(width * height * num_components, 0);
- GetRect(self, data, helper.pitch(), x, y, width, height, &values[0]);
- return values;
-}
-
-} // namespace class_TextureCUBE
-
-} // namespace namespace_o3d
-} // namespace glue
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <vector> +#include "core/cross/pointer_utils.h" +#include "core/cross/error.h" +#include "core/cross/math_utilities.h" +#include "core/cross/texture.h" +#include "core/cross/image_utils.h" + +namespace { + +void SetRectCheck(o3d::Texture* self, + void* data, + int pitch, + int destination_x, + int destination_y, + int texture_width, + int texture_height, + int source_width, + int source_height, + const std::vector<float>& values) { + unsigned num_components; + unsigned swizzle[4] = {2, 1, 0, 3}; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + swizzle[0] = 0; + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + const o3d::Texture::RGBASwizzleIndices& indices = + self->GetABGR32FSwizzleIndices(); + for (int ii = 0; ii < 4; ++ii) { + swizzle[ii] = indices[ii]; + } + break; + } + default: + DCHECK(false); + return; + } + + // clip + int source_x = 0; + int source_y = 0; + int copy_width = source_width; + int copy_height = source_height; + + if (destination_x < 0) { + copy_width += destination_x; + source_x -= destination_x; + destination_x = 0; + } + if (destination_x + copy_width > static_cast<int>(texture_width)) { + copy_width -= destination_x + copy_width - texture_width; + } + + if (destination_y < 0) { + copy_height += destination_y; + source_y -= destination_y; + destination_y = 0; + } + if (destination_y + copy_height > static_cast<int>(texture_height)) { + copy_height -= destination_y + copy_height - texture_height; + } + + if (copy_width <= 0 || copy_height <= 0) { + return; + } + + const float* source = + &values[0] + + (source_y * source_width + source_x) * num_components; + unsigned source_stride = (source_width - copy_width) * num_components; + switch (self->format()) { + case o3d::Texture::ABGR16F: { + uint16* dest_line = + static_cast<uint16*>(data) + + (destination_y * texture_width + destination_x) * num_components; + for (; copy_height > 0; --copy_height) { + uint16* destination = dest_line; + for (int xx = 0; xx < copy_width; ++xx) { + for (unsigned element = 0; element < num_components; ++element) { + destination[element] = Vectormath::Aos::FloatToHalf( + source[swizzle[element]]); + } + destination += num_components; + source += num_components; + } + dest_line = o3d::AddPointerOffset<uint16*>(dest_line, pitch); + source += source_stride; + } + break; + } + case o3d::Texture::R32F: + case o3d::Texture::ABGR32F: { + float* dest_line = + static_cast<float*>(data) + + (destination_y * texture_width + destination_x) * num_components; + for (; copy_height > 0; --copy_height) { + float* destination = dest_line; + for (int xx = 0; xx < copy_width; ++xx) { + for (unsigned element = 0; element < num_components; ++element) { + destination[element] = source[swizzle[element]]; + } + destination += num_components; + source += num_components; + } + dest_line = o3d::AddPointerOffset<float*>(dest_line, pitch); + source += source_stride; + } + break; + } + default: { + uint8* dest_line = + static_cast<uint8*>(data) + + (destination_y * texture_width + destination_x) * 4; + for (; copy_height > 0; --copy_height) { + uint8* destination = dest_line; + for (int xx = 0; xx < copy_width; ++xx) { + destination[0] = static_cast<unsigned char>( + source[swizzle[0]] * 255.0f); + destination[1] = static_cast<unsigned char>( + source[swizzle[1]] * 255.0f); + destination[2] = static_cast<unsigned char>( + source[swizzle[2]] * 255.0f); + destination[3] = (num_components == 4) ? + static_cast<unsigned char>(source[swizzle[3]] * 255.0f) : 255; + destination += 4; + source += num_components; + } + dest_line = o3d::AddPointerOffset<uint8*>(dest_line, pitch); + source += source_stride; + } + break; + } + } +} + +void SetRectCheck2D(o3d::Texture2D* self, + int level, + int destination_x, + int destination_y, + int source_width, + const std::vector<float>& values, + bool check_needed) { + if (level < 0 || level >= self->levels()) { + O3D_ERROR(self->service_locator()) + << "level (" << level << " out of range"; + return; + } + if (values.empty() || source_width <= 0) { + return; + } + unsigned num_values = values.size(); + unsigned texture_width = std::max(self->width() >> level, 1); + unsigned texture_height = std::max(self->height() >> level, 1); + unsigned num_components; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + break; + } + default: + O3D_ERROR(self->service_locator()) + << "Texture::Set not supported for this type of texture"; + return; + } + if (num_values % num_components != 0) { + O3D_ERROR(self->service_locator()) + << "The number of elements passed in must be a multiple of " + << num_components; + } + unsigned num_elements = num_values / num_components; + if (num_elements % source_width != 0) { + O3D_ERROR(self->service_locator()) + << "The number of elements passed in must be a multiple of the " + << "width"; + return; + } + unsigned source_height = num_elements / source_width; + if (check_needed) { + unsigned needed = num_components * texture_width * texture_height; + if (num_values != needed) { + O3D_ERROR(self->service_locator()) + << "needed " << needed << " values but " << num_values + << " passed in."; + return; + } + } + o3d::Texture2D::LockHelper helper(self, level, o3d::Texture::kWriteOnly); + void* data = helper.GetData(); + if (!data) { + O3D_ERROR(self->service_locator()) << "could not lock texture"; + return; + } + + SetRectCheck(self, data, helper.pitch(), + destination_x, destination_y, + texture_width, texture_height, + source_width, source_height, + values); +} + +void SetRectCheckCUBE(o3d::TextureCUBE* self, + o3d::TextureCUBE::CubeFace face, + int level, + int destination_x, + int destination_y, + int source_width, + const std::vector<float>& values, + bool check_needed) { + if (level < 0 || level >= self->levels()) { + O3D_ERROR(self->service_locator()) + << "level (" << level << " out of range"; + return; + } + if (values.empty() || source_width <= 0) { + return; + } + unsigned num_values = values.size(); + unsigned texture_width = std::max(self->edge_length() >> level, 1); + unsigned texture_height = texture_width; + unsigned num_components; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + break; + } + default: + O3D_ERROR(self->service_locator()) + << "Texture::Set not supported for this type of texture"; + return; + } + if (num_values % num_components != 0) { + O3D_ERROR(self->service_locator()) + << "The number of elements passed in must be a multiple of " + << num_components; + } + unsigned num_elements = num_values / num_components; + if (num_elements % source_width != 0) { + O3D_ERROR(self->service_locator()) + << "The number of elements passed in must be a multiple of the " + << "width"; + return; + } + unsigned source_height = num_elements / source_width; + if (check_needed) { + unsigned needed = num_components * texture_width * texture_height; + if (num_values != needed) { + O3D_ERROR(self->service_locator()) + << "needed " << needed << " values but " << num_values + << " passed in."; + return; + } + } + o3d::TextureCUBE::LockHelper helper( + self, face, level, o3d::Texture::kWriteOnly); + void* data = helper.GetData(); + if (!data) { + O3D_ERROR(self->service_locator()) << "could not lock texture"; + return; + } + + SetRectCheck(self, data, helper.pitch(), + destination_x, destination_y, + texture_width, texture_height, + source_width, source_height, + values); +} + +// Assumes dst points to width * height * num_components floats. +void GetRect(o3d::Texture* self, + const void* src_data, + int src_pitch, + int x, + int y, + int width, + int height, + float* dst) { + unsigned num_components; + unsigned swizzle[4] = {2, 1, 0, 3}; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + swizzle[0] = 0; + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + const o3d::Texture::RGBASwizzleIndices& indices = + self->GetABGR32FSwizzleIndices(); + for (int ii = 0; ii < 4; ++ii) { + swizzle[ii] = indices[ii]; + } + break; + } + default: + DCHECK(false); + return; + } + + switch (self->format()) { + case o3d::Texture::ABGR16F: { + uint16* src_line = o3d::PointerFromVoidPointer<uint16*>( + src_data, + (y * src_pitch)) + x * num_components; + for (; height > 0; --height) { + uint16* src = src_line; + for (int xx = 0; xx < width; ++xx) { + for (unsigned element = 0; element < num_components; ++element) { + dst[swizzle[element]] = Vectormath::Aos::HalfToFloat(src[element]); + } + dst += num_components; + src += num_components; + } + src_line = o3d::AddPointerOffset<uint16*>(src_line, src_pitch); + } + break; + } + case o3d::Texture::R32F: + case o3d::Texture::ABGR32F: { + float* src_line = o3d::PointerFromVoidPointer<float*>( + src_data, + (y * src_pitch)) + x * num_components; + for (; height > 0; --height) { + float* src = src_line; + for (int xx = 0; xx < width; ++xx) { + for (unsigned element = 0; element < num_components; ++element) { + dst[swizzle[element]] = src[element]; + } + dst += num_components; + src += num_components; + } + src_line = o3d::AddPointerOffset<float*>(src_line, src_pitch); + } + break; + } + default: { + uint8* src_line = o3d::PointerFromVoidPointer<uint8*>( + src_data, + (y * src_pitch)) + x * num_components; + for (; height > 0; --height) { + uint8* src = src_line; + for (int xx = 0; xx < width; ++xx) { + dst[swizzle[0]] = static_cast<float>(src[0]) / 255.0f; + dst[swizzle[1]] = static_cast<float>(src[1]) / 255.0f; + dst[swizzle[2]] = static_cast<float>(src[2]) / 255.0f; + if (num_components == 4) { + dst[swizzle[3]] = static_cast<float>(src[3]) / 255.0f; + } + dst += num_components; + src += 4; + } + src_line = o3d::AddPointerOffset<uint8*>(src_line, src_pitch); + } + break; + } + } +} + +} // anonymous namespace + +namespace glue { +namespace namespace_o3d { +namespace class_Texture2D { + +void userglue_method_SetRect(o3d::Texture2D* self, + int level, + int destination_x, + int destination_y, + int source_width, + const std::vector<float>& values) { + SetRectCheck2D(self, + level, + destination_x, + destination_y, + source_width, + values, + false); +} +void userglue_method_Set(o3d::Texture2D* self, + int level, + const std::vector<float>& values) { + SetRectCheck2D( + self, level, 0, 0, + o3d::image::ComputeMipDimension(level, self->width()), + values, true); +} +std::vector<float> userglue_method_GetRect(o3d::Texture2D* self, + int level, + int x, + int y, + int width, + int height) { + std::vector<float> empty; + if (level < 0 || level >= self->levels()) { + O3D_ERROR(self->service_locator()) + << "level (" << level << " out of range"; + return empty; + } + if (width <= 0 || height <= 0) { + O3D_ERROR(self->service_locator()) + << "width and height must be positive"; + return empty; + } + + int mip_width = + static_cast<int>(o3d::image::ComputeMipDimension(level, self->width())); + int mip_height = + static_cast<int>(o3d::image::ComputeMipDimension(level, self->height())); + + if (x < 0 || x + width > mip_width || y < 0 || y + height > mip_height) { + O3D_ERROR(self->service_locator()) << "area out of range"; + return empty; + } + + unsigned num_components; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + break; + } + default: + O3D_ERROR(self->service_locator()) + << "Texture::Set not supported for this type of texture"; + return empty; + } + o3d::Texture2D::LockHelper helper(self, level, o3d::Texture::kReadOnly); + void* data = helper.GetData(); + if (!data) { + O3D_ERROR(self->service_locator()) << "could not lock texture"; + return empty; + } + + std::vector<float> values(width * height * num_components, 0); + GetRect(self, data, helper.pitch(), x, y, width, height, &values[0]); + return values; +} + +} // namespace class_Texture2D + +namespace class_TextureCUBE { + +void userglue_method_SetRect(o3d::TextureCUBE* self, + o3d::TextureCUBE::CubeFace face, + int level, + int destination_x, + int destination_y, + int source_width, + const std::vector<float>& values) { + SetRectCheckCUBE(self, + face, + level, + destination_x, + destination_y, + source_width, + values, + false); +} +void userglue_method_Set(o3d::TextureCUBE* self, + o3d::TextureCUBE::CubeFace face, + int level, + const std::vector<float>& values) { + SetRectCheckCUBE(self, face, level, 0, 0, + o3d::image::ComputeMipDimension(level, self->edge_length()), + values, true); +} +std::vector<float> userglue_method_GetRect(o3d::TextureCUBE* self, + o3d::TextureCUBE::CubeFace face, + int level, + int x, + int y, + int width, + int height) { + std::vector<float> empty; + if (level < 0 || level >= self->levels()) { + O3D_ERROR(self->service_locator()) + << "level (" << level << " out of range"; + return empty; + } + if (width <= 0 || height <= 0) { + O3D_ERROR(self->service_locator()) + << "width and height must be positive"; + return empty; + } + + int mip_length = static_cast<int>(o3d::image::ComputeMipDimension( + level, self->edge_length())); + + if (x < 0 || x + width > mip_length || y < 0 || y + height > mip_length) { + O3D_ERROR(self->service_locator()) << "area out of range"; + return empty; + } + + unsigned num_components; + switch (self->format()) { + case o3d::Texture::XRGB8: + num_components = 3; + break; + case o3d::Texture::R32F: + num_components = 1; + break; + case o3d::Texture::ARGB8: + case o3d::Texture::ABGR16F: + num_components = 4; + break; + case o3d::Texture::ABGR32F: { + num_components = 4; + break; + } + default: + O3D_ERROR(self->service_locator()) + << "Texture::Set not supported for this type of texture"; + return empty; + } + o3d::TextureCUBE::LockHelper helper( + self, face, level, o3d::Texture::kReadOnly); + void* data = helper.GetData(); + if (!data) { + O3D_ERROR(self->service_locator()) << "could not lock texture"; + return empty; + } + + std::vector<float> values(width * height * num_components, 0); + GetRect(self, data, helper.pitch(), x, y, width, height, &values[0]); + return values; +} + +} // namespace class_TextureCUBE + +} // namespace namespace_o3d +} // namespace glue + diff --git a/o3d/utils/cross/base64_test.cc b/o3d/utils/cross/base64_test.cc index 9eacd69..d291f14 100644 --- a/o3d/utils/cross/base64_test.cc +++ b/o3d/utils/cross/base64_test.cc @@ -1,170 +1,170 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-
-// This file contains the tests of base64 functions
-
-#include "tests/common/win/testing_common.h"
-#include "utils/cross/base64.h"
-
-namespace o3d {
-
-class Base64Test : public testing::Test {
-};
-
-TEST_F(Base64Test, GetEncodeLength) {
- EXPECT_EQ(0u, base64::GetEncodeLength(0));
- EXPECT_EQ(4u, base64::GetEncodeLength(1));
- EXPECT_EQ(4u, base64::GetEncodeLength(2));
- EXPECT_EQ(4u, base64::GetEncodeLength(3));
- EXPECT_EQ(8u, base64::GetEncodeLength(4));
-}
-
-TEST_F(Base64Test, Encode) {
- unsigned char buffer[100];
- memset(buffer, 0xFF, sizeof(buffer));
- base64::Encode("abc", 3, buffer);
- EXPECT_EQ(0, memcmp(buffer, "YWJj", 4));
- EXPECT_EQ(0xFF, buffer[4]);
- memset(buffer, 0xFF, sizeof(buffer));
- base64::Encode("ab\0c", 4, buffer);
- EXPECT_EQ(0, memcmp(buffer, "YWIAYw==", 8));
- EXPECT_EQ(0xFF, buffer[8]);
-}
-
-TEST_F(Base64Test, GetDecodeLength) {
- size_t length = 256u;
- base64::GetDecodeLength("", 0, &length);
- EXPECT_EQ(0u, length);
-
- length = 256u;
- base64::GetDecodeLength("YQ==", 4, &length);
- EXPECT_EQ(1u, length);
-
- length = 256u;
- base64::GetDecodeLength("YWI=", 4, &length);
- EXPECT_EQ(2u, length);
-
- length = 256u;
- base64::GetDecodeLength("YWJj", 4, &length);
- EXPECT_EQ(3u, length);
-
- length = 256u;
- base64::GetDecodeLength("YWJjZA==", 8, &length);
- EXPECT_EQ(4u, length);
-
- length = 256u;
- base64::GetDecodeLength("YWJjZGU=", 8, &length);
- EXPECT_EQ(5u, length);
-}
-
-TEST_F(Base64Test, GetDecodeLengthInputError) {
- base64::DecodeStatus status = base64::kSuccess;
- size_t length = 256u;
- status = base64::GetDecodeLength("Y@==", 4, &length);
- EXPECT_EQ(base64::kBadCharError, status);
-
- status = base64::GetDecodeLength("Y Q==", 4, &length);
- EXPECT_EQ(base64::kSuccess, status);
-
- status = base64::GetDecodeLength("Y", 1, &length);
- EXPECT_EQ(base64::kPadError, status);
-}
-
-TEST_F(Base64Test, Decode) {
- unsigned char buffer[10];
- memset(buffer, 0xFF, sizeof(buffer));
- unsigned char result_buffer[10];
- memset(result_buffer, 0xFF, sizeof(result_buffer));
- base64::DecodeStatus status = base64::kSuccess;
-
- status = base64::Decode("", 0, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-
- result_buffer[0] = 'a';
- status = base64::Decode("YQ==", 4, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-
- result_buffer[1] = 'b';
- status = base64::Decode("YWI=", 4, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-
- result_buffer[2] = 'c';
- status = base64::Decode("YWJj", 4, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-
- result_buffer[3] = 'd';
- status = base64::Decode("YWJjZA==", 8, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-
- result_buffer[4] = 'e';
- status = base64::Decode("YWJjZGU=", 8, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
- EXPECT_EQ(0, memcmp(buffer, result_buffer, 10));
-}
-
-TEST_F(Base64Test, DecodeInputError) {
- unsigned char buffer[10];
- base64::DecodeStatus status = base64::kSuccess;
- status = base64::Decode("Y@==", 4, buffer, 10);
- EXPECT_EQ(base64::kBadCharError, status);
-
- status = base64::Decode("Y Q==", 4, buffer, 10);
- EXPECT_EQ(base64::kSuccess, status);
-
- status = base64::Decode("Y", 1, buffer, 10);
- EXPECT_EQ(base64::kPadError, status);
-}
-
-TEST_F(Base64Test, DecodeOverflowError) {
- unsigned char buffer[10];
- base64::DecodeStatus status = base64::kSuccess;
- status = base64::Decode("YWJjZA==", 8, buffer, 3);
- EXPECT_EQ(base64::kOutputOverflowError, status);
-
- status = base64::Decode("YWJjZA==", 8, buffer, 4);
- EXPECT_EQ(base64::kSuccess, status);
-
- status = base64::Decode("YQ==", 8, buffer, 0);
- EXPECT_EQ(base64::kOutputOverflowError, status);
-
- status = base64::Decode("YQ==", 8, buffer, 1);
- EXPECT_EQ(base64::kSuccess, status);
-}
-
-} // namespace o3d
-
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +// This file contains the tests of base64 functions + +#include "tests/common/win/testing_common.h" +#include "utils/cross/base64.h" + +namespace o3d { + +class Base64Test : public testing::Test { +}; + +TEST_F(Base64Test, GetEncodeLength) { + EXPECT_EQ(0u, base64::GetEncodeLength(0)); + EXPECT_EQ(4u, base64::GetEncodeLength(1)); + EXPECT_EQ(4u, base64::GetEncodeLength(2)); + EXPECT_EQ(4u, base64::GetEncodeLength(3)); + EXPECT_EQ(8u, base64::GetEncodeLength(4)); +} + +TEST_F(Base64Test, Encode) { + unsigned char buffer[100]; + memset(buffer, 0xFF, sizeof(buffer)); + base64::Encode("abc", 3, buffer); + EXPECT_EQ(0, memcmp(buffer, "YWJj", 4)); + EXPECT_EQ(0xFF, buffer[4]); + memset(buffer, 0xFF, sizeof(buffer)); + base64::Encode("ab\0c", 4, buffer); + EXPECT_EQ(0, memcmp(buffer, "YWIAYw==", 8)); + EXPECT_EQ(0xFF, buffer[8]); +} + +TEST_F(Base64Test, GetDecodeLength) { + size_t length = 256u; + base64::GetDecodeLength("", 0, &length); + EXPECT_EQ(0u, length); + + length = 256u; + base64::GetDecodeLength("YQ==", 4, &length); + EXPECT_EQ(1u, length); + + length = 256u; + base64::GetDecodeLength("YWI=", 4, &length); + EXPECT_EQ(2u, length); + + length = 256u; + base64::GetDecodeLength("YWJj", 4, &length); + EXPECT_EQ(3u, length); + + length = 256u; + base64::GetDecodeLength("YWJjZA==", 8, &length); + EXPECT_EQ(4u, length); + + length = 256u; + base64::GetDecodeLength("YWJjZGU=", 8, &length); + EXPECT_EQ(5u, length); +} + +TEST_F(Base64Test, GetDecodeLengthInputError) { + base64::DecodeStatus status = base64::kSuccess; + size_t length = 256u; + status = base64::GetDecodeLength("Y@==", 4, &length); + EXPECT_EQ(base64::kBadCharError, status); + + status = base64::GetDecodeLength("Y Q==", 4, &length); + EXPECT_EQ(base64::kSuccess, status); + + status = base64::GetDecodeLength("Y", 1, &length); + EXPECT_EQ(base64::kPadError, status); +} + +TEST_F(Base64Test, Decode) { + unsigned char buffer[10]; + memset(buffer, 0xFF, sizeof(buffer)); + unsigned char result_buffer[10]; + memset(result_buffer, 0xFF, sizeof(result_buffer)); + base64::DecodeStatus status = base64::kSuccess; + + status = base64::Decode("", 0, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); + + result_buffer[0] = 'a'; + status = base64::Decode("YQ==", 4, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); + + result_buffer[1] = 'b'; + status = base64::Decode("YWI=", 4, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); + + result_buffer[2] = 'c'; + status = base64::Decode("YWJj", 4, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); + + result_buffer[3] = 'd'; + status = base64::Decode("YWJjZA==", 8, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); + + result_buffer[4] = 'e'; + status = base64::Decode("YWJjZGU=", 8, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + EXPECT_EQ(0, memcmp(buffer, result_buffer, 10)); +} + +TEST_F(Base64Test, DecodeInputError) { + unsigned char buffer[10]; + base64::DecodeStatus status = base64::kSuccess; + status = base64::Decode("Y@==", 4, buffer, 10); + EXPECT_EQ(base64::kBadCharError, status); + + status = base64::Decode("Y Q==", 4, buffer, 10); + EXPECT_EQ(base64::kSuccess, status); + + status = base64::Decode("Y", 1, buffer, 10); + EXPECT_EQ(base64::kPadError, status); +} + +TEST_F(Base64Test, DecodeOverflowError) { + unsigned char buffer[10]; + base64::DecodeStatus status = base64::kSuccess; + status = base64::Decode("YWJjZA==", 8, buffer, 3); + EXPECT_EQ(base64::kOutputOverflowError, status); + + status = base64::Decode("YWJjZA==", 8, buffer, 4); + EXPECT_EQ(base64::kSuccess, status); + + status = base64::Decode("YQ==", 8, buffer, 0); + EXPECT_EQ(base64::kOutputOverflowError, status); + + status = base64::Decode("YQ==", 8, buffer, 1); + EXPECT_EQ(base64::kSuccess, status); +} + +} // namespace o3d + + diff --git a/o3d/utils/cross/dataurl.cc b/o3d/utils/cross/dataurl.cc index f796719..328d174 100644 --- a/o3d/utils/cross/dataurl.cc +++ b/o3d/utils/cross/dataurl.cc @@ -1,113 +1,113 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This file contains the definition of functions for dealing with data urls.
-
-#include "utils/cross/dataurl.h"
-#include "core/cross/types.h"
-#include "utils/cross/base64.h"
-
-namespace o3d {
-namespace dataurl {
-
-const char* const kEmptyDataURL = "data:,";
-
-String ToDataURL(const String& mime_type, const void* data, size_t length) {
- String header(String("data:") + mime_type + ";base64,");
- String result(header.size() + base64::GetEncodeLength(length), ' ');
- result.replace(0, header.size(), header);
- base64::Encode(data, length, &result[header.size()]);
- return result;
-}
-
-// Decodes the data URL and stores a pointer to the data in dst_buffer
-bool FromDataURL(const String& data_url,
- scoped_array<uint8>* dst_buffer,
- size_t* output_length,
- String* error_string) {
- // First parse the data_url
- const String kDataHeader("data:");
- const String kBase64Header(";base64,");
- // The string has to be long enough.
- if (data_url.size() <= kDataHeader.size() + kBase64Header.size()) {
- *error_string = "Invalid formatting: The data URL is not long enough.";
- return false;
- }
- // it must start with "data:"
- if (data_url.compare(0, kDataHeader.size(), kDataHeader) != 0) {
- *error_string
- = "Invalid formatting: The data URL must start with 'data:'";
- return false;
- }
- // we only support base64 data URL's
- String::size_type data_index = data_url.find(kBase64Header);
- if (data_index == String::npos) {
- *error_string
- = "Invalid formatting: The data URL have ';base64,' in the header.";
- return false;
- }
- // The start of the data.
- data_index += kBase64Header.size();
- if (data_index >= data_url.size()) {
- *error_string
- = "Invalid formatting: There must be data in the body of the data URL.";
- return false;
- }
-
- // Get the length of the decoded data
- size_t input_length = data_url.size() - data_index;
- base64::DecodeStatus return_code = base64::GetDecodeLength(
- &data_url[data_index],
- input_length,
- output_length);
- if (return_code != base64::kSuccess) {
- if (return_code == base64::kPadError) {
- *error_string
- = "Invalid formatting: Padding error in the data URL data.";
- } else {
- *error_string
- = "Invalid formatting: Bad character error in the data URL data.";
- }
- return false;
- }
-
- dst_buffer->reset(new uint8[*output_length]);
- base64::Decode(&data_url[data_index],
- input_length,
- dst_buffer->get(),
- (*output_length));
-
- return true;
-}
-
-} // namespace dataurl
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This file contains the definition of functions for dealing with data urls. + +#include "utils/cross/dataurl.h" +#include "core/cross/types.h" +#include "utils/cross/base64.h" + +namespace o3d { +namespace dataurl { + +const char* const kEmptyDataURL = "data:,"; + +String ToDataURL(const String& mime_type, const void* data, size_t length) { + String header(String("data:") + mime_type + ";base64,"); + String result(header.size() + base64::GetEncodeLength(length), ' '); + result.replace(0, header.size(), header); + base64::Encode(data, length, &result[header.size()]); + return result; +} + +// Decodes the data URL and stores a pointer to the data in dst_buffer +bool FromDataURL(const String& data_url, + scoped_array<uint8>* dst_buffer, + size_t* output_length, + String* error_string) { + // First parse the data_url + const String kDataHeader("data:"); + const String kBase64Header(";base64,"); + // The string has to be long enough. + if (data_url.size() <= kDataHeader.size() + kBase64Header.size()) { + *error_string = "Invalid formatting: The data URL is not long enough."; + return false; + } + // it must start with "data:" + if (data_url.compare(0, kDataHeader.size(), kDataHeader) != 0) { + *error_string + = "Invalid formatting: The data URL must start with 'data:'"; + return false; + } + // we only support base64 data URL's + String::size_type data_index = data_url.find(kBase64Header); + if (data_index == String::npos) { + *error_string + = "Invalid formatting: The data URL have ';base64,' in the header."; + return false; + } + // The start of the data. + data_index += kBase64Header.size(); + if (data_index >= data_url.size()) { + *error_string + = "Invalid formatting: There must be data in the body of the data URL."; + return false; + } + + // Get the length of the decoded data + size_t input_length = data_url.size() - data_index; + base64::DecodeStatus return_code = base64::GetDecodeLength( + &data_url[data_index], + input_length, + output_length); + if (return_code != base64::kSuccess) { + if (return_code == base64::kPadError) { + *error_string + = "Invalid formatting: Padding error in the data URL data."; + } else { + *error_string + = "Invalid formatting: Bad character error in the data URL data."; + } + return false; + } + + dst_buffer->reset(new uint8[*output_length]); + base64::Decode(&data_url[data_index], + input_length, + dst_buffer->get(), + (*output_length)); + + return true; +} + +} // namespace dataurl +} // namespace o3d + diff --git a/o3d/utils/cross/math_gtest.cc b/o3d/utils/cross/math_gtest.cc index 0f7cc90d..1296817 100644 --- a/o3d/utils/cross/math_gtest.cc +++ b/o3d/utils/cross/math_gtest.cc @@ -1,119 +1,119 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-// This file defines the some helper for gtest for the math library used by O3D.
-
-#include "utils/cross/math_gtest.h"
-#include "core/cross/types.h"
-
-
-namespace Vectormath {
-namespace Aos {
-
-bool operator==(const Vector4& left, const Vector4& right) {
- return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] &&
- left[3] == right[3];
-}
-
-bool operator!=(const Vector4& left, const Vector4& right) {
- return !(left == right);
-}
-
-bool operator==(const Matrix4& left, const Matrix4& right) {
- return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] &&
- left[3] == right[3];
-}
-
-bool operator!=(const Matrix4& left, const Matrix4& right) {
- return !(left == right);
-}
-
-std::ostream& operator<<(std::ostream& stream, const Vector4& value) {
- stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", "
- << value[3] << "}";
- return stream;
-}
-
-std::ostream& operator<<(std::ostream& stream, const Matrix4& value) {
- stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", "
- << value[3] << "}";
- return stream;
-}
-
-} // namespace Aos
-} // namespace Vectormath
-
-namespace o3d {
-
-bool operator==(const Float2& left, const Float2& right) {
- return left[0] == right[0] && left[1] == right[1];
-}
-
-bool operator!=(const Float2& left, const Float2& right) {
- return !(left == right);
-}
-
-bool operator==(const Float3& left, const Float3& right) {
- return left[0] == right[0] && left[1] == right[1] && left[2] == right[2];
-}
-
-bool operator!=(const Float3& left, const Float3& right) {
- return !(left == right);
-}
-
-bool operator==(const Float4& left, const Float4& right) {
- return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] &&
- left[3] == right[3];
-}
-
-bool operator!=(const Float4& left, const Float4& right) {
- return !(left == right);
-}
-
-std::ostream& operator<<(std::ostream& stream, const Float2& value) {
- stream << "{" << value[0] << ", " << value[1] << "}";
- return stream;
-}
-
-std::ostream& operator<<(std::ostream& stream, const Float3& value) {
- stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << "}";
- return stream;
-}
-
-std::ostream& operator<<(std::ostream& stream, const Float4& value) {
- stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", "
- << value[3] << "}";
- return stream;
-}
-
-} // namespace o3d
-
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +// This file defines the some helper for gtest for the math library used by O3D. + +#include "utils/cross/math_gtest.h" +#include "core/cross/types.h" + + +namespace Vectormath { +namespace Aos { + +bool operator==(const Vector4& left, const Vector4& right) { + return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] && + left[3] == right[3]; +} + +bool operator!=(const Vector4& left, const Vector4& right) { + return !(left == right); +} + +bool operator==(const Matrix4& left, const Matrix4& right) { + return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] && + left[3] == right[3]; +} + +bool operator!=(const Matrix4& left, const Matrix4& right) { + return !(left == right); +} + +std::ostream& operator<<(std::ostream& stream, const Vector4& value) { + stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", " + << value[3] << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const Matrix4& value) { + stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", " + << value[3] << "}"; + return stream; +} + +} // namespace Aos +} // namespace Vectormath + +namespace o3d { + +bool operator==(const Float2& left, const Float2& right) { + return left[0] == right[0] && left[1] == right[1]; +} + +bool operator!=(const Float2& left, const Float2& right) { + return !(left == right); +} + +bool operator==(const Float3& left, const Float3& right) { + return left[0] == right[0] && left[1] == right[1] && left[2] == right[2]; +} + +bool operator!=(const Float3& left, const Float3& right) { + return !(left == right); +} + +bool operator==(const Float4& left, const Float4& right) { + return left[0] == right[0] && left[1] == right[1] && left[2] == right[2] && + left[3] == right[3]; +} + +bool operator!=(const Float4& left, const Float4& right) { + return !(left == right); +} + +std::ostream& operator<<(std::ostream& stream, const Float2& value) { + stream << "{" << value[0] << ", " << value[1] << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const Float3& value) { + stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << "}"; + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const Float4& value) { + stream << "{" << value[0] << ", " << value[1] << ", " << value[2] << ", " + << value[3] << "}"; + return stream; +} + +} // namespace o3d + + diff --git a/o3d/utils/cross/math_gtest_test.cc b/o3d/utils/cross/math_gtest_test.cc index a0e4f00..ae6bc62 100644 --- a/o3d/utils/cross/math_gtest_test.cc +++ b/o3d/utils/cross/math_gtest_test.cc @@ -1,70 +1,70 @@ -/*
- * Copyright 2009, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "tests/common/win/testing_common.h"
-#include "utils/cross/math_gtest.h"
-
-namespace o3d {
-
-class MathGTestTest : public testing::Test {
-};
-
-// Test the math gtest helper functions.
-TEST_F(MathGTestTest, TestMathGTest) {
- EXPECT_EQ(Float2(1.2f, 2.3f), Float2(1.2f, 2.3f));
- EXPECT_NE(Float2(1.2f, 2.3f), Float2(1.3f, 2.3f));
- EXPECT_NE(Float2(1.2f, 2.3f), Float2(1.2f, 2.4f));
- EXPECT_EQ(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.3f, 4.5f));
- EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.3f, 2.3f, 4.5f));
- EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.4f, 4.5f));
- EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.3f, 4.6f));
- EXPECT_EQ(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.5f, 6.7f));
- EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.3f, 2.3f, 4.5f, 6.7f));
- EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.4f, 4.5f, 6.7f));
- EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.6f, 6.7f));
- EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.5f, 6.8f));
- Matrix4 a(Vector4(1.1f, 2.2f, 3.3f, 4.4f),
- Vector4(1.2f, 2.3f, 3.4f, 4.5f),
- Vector4(1.3f, 2.4f, 3.5f, 4.6f),
- Vector4(1.4f, 2.5f, 3.6f, 4.7f));
- Matrix4 b(a);
- EXPECT_EQ(a, b);
- for (int ii = 0; ii < 4; ++ii) {
- for (int jj = 0; jj < 4; ++jj) {
- b = a;
- b.setElem(ii, jj, b.getElem(ii, jj) * 2);
- EXPECT_NE(a, b);
- }
- }
-}
-
-} // namespace o3d
-
+/* + * Copyright 2009, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests/common/win/testing_common.h" +#include "utils/cross/math_gtest.h" + +namespace o3d { + +class MathGTestTest : public testing::Test { +}; + +// Test the math gtest helper functions. +TEST_F(MathGTestTest, TestMathGTest) { + EXPECT_EQ(Float2(1.2f, 2.3f), Float2(1.2f, 2.3f)); + EXPECT_NE(Float2(1.2f, 2.3f), Float2(1.3f, 2.3f)); + EXPECT_NE(Float2(1.2f, 2.3f), Float2(1.2f, 2.4f)); + EXPECT_EQ(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.3f, 4.5f)); + EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.3f, 2.3f, 4.5f)); + EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.4f, 4.5f)); + EXPECT_NE(Float3(1.2f, 2.3f, 4.5f), Float3(1.2f, 2.3f, 4.6f)); + EXPECT_EQ(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.5f, 6.7f)); + EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.3f, 2.3f, 4.5f, 6.7f)); + EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.4f, 4.5f, 6.7f)); + EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.6f, 6.7f)); + EXPECT_NE(Float4(1.2f, 2.3f, 4.5f, 6.7f), Float4(1.2f, 2.3f, 4.5f, 6.8f)); + Matrix4 a(Vector4(1.1f, 2.2f, 3.3f, 4.4f), + Vector4(1.2f, 2.3f, 3.4f, 4.5f), + Vector4(1.3f, 2.4f, 3.5f, 4.6f), + Vector4(1.4f, 2.5f, 3.6f, 4.7f)); + Matrix4 b(a); + EXPECT_EQ(a, b); + for (int ii = 0; ii < 4; ++ii) { + for (int jj = 0; jj < 4; ++jj) { + b = a; + b.setElem(ii, jj, b.getElem(ii, jj) * 2); + EXPECT_NE(a, b); + } + } +} + +} // namespace o3d + diff --git a/third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.cc b/third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.cc index 05d4a45..146f862 100644 --- a/third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.cc +++ b/third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.cc @@ -1,570 +1,570 @@ -// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.h"
-#include <stdio.h>
-#include <string.h>
-
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/lang_enc.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8propjustletter.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8propletterscriptnum.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8scannotjustletterspecial.h"
-
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_basictypes.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_commandlineflags.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_google.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_htmlutils.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_unilib.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_utf8statetable.h"
-#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_utf8utils.h"
-
-static const Language GRAY_LANG = (Language)254;
-
-static const int kMaxUpToWordBoundary = 50; // span < this make longer,
- // else make shorter
-static const int kMaxAdvanceToWordBoundary = 10; // +/- this many bytes
- // to round to word boundary,
- // direction above
-
-static const char kSpecialSymbol[256] = { // true for < > &
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,1,0,1,0,
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
-
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
-};
-
-
-
-#define LT 0 // <
-#define GT 1 // >
-#define EX 2 // !
-#define HY 3 // -
-#define QU 4 // "
-#define AP 5 // '
-#define SL 6 // /
-#define S_ 7
-#define C_ 8
-#define R_ 9
-#define I_ 10
-#define P_ 11
-#define T_ 12
-#define Y_ 13
-#define L_ 14
-#define E_ 15
-#define CR 16 // <cr> or <lf>
-#define NL 17 // non-letter: ASCII whitespace, digit, punctuation
-#define PL 18 // possible letter, incl. &
-#define xx 19 // <unused>
-
-// Map byte to one of ~20 interesting categories for cheap tag parsing
-static const uint8 kCharToSub[256] = {
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,CR,NL, NL,CR,NL,NL,
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL,
- NL,EX,QU,NL, NL,NL,PL,AP, NL,NL,NL,NL, NL,HY,NL,SL,
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, LT,NL,GT,NL,
-
- PL,PL,PL,C_, PL,E_,PL,PL, PL,I_,PL,PL, L_,PL,PL,PL,
- P_,PL,R_,S_, T_,PL,PL,PL, PL,Y_,PL,NL, NL,NL,NL,NL,
- PL,PL,PL,C_, PL,E_,PL,PL, PL,I_,PL,PL, L_,PL,PL,PL,
- P_,PL,R_,S_, T_,PL,PL,PL, PL,Y_,PL,NL, NL,NL,NL,NL,
-
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL,
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL,
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL,
- NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL,
-
- PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL,
- PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL,
- PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL,
- PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL,
-};
-
-#undef LT
-#undef GT
-#undef EX
-#undef HY
-#undef QU
-#undef AP
-#undef SL
-#undef S_
-#undef C_
-#undef R_
-#undef I_
-#undef P_
-#undef T_
-#undef Y_
-#undef L_
-#undef E_
-#undef CR
-#undef NL
-#undef PL
-#undef xx
-
-
-#define OK 0
-#define X_ 1
-
-// State machine to do cheap parse of non-letter strings incl. tags
-// advances <tag>
-// | |
-// advances <tag> ... </tag> for <script> <style>
-// | |
-// advances <!-- ... <tag> ... -->
-// | |
-// advances <tag
-// || (0)
-// advances <tag <tag2>
-// || (0)
-static const uint8 kTagParseTbl_0[] = {
-// < > ! - " ' / S C R I P T Y L E CR NL PL xx
- 3, 2, 2, 2, 2, 2, 2,OK, OK,OK,OK,OK, OK,OK,OK,OK, 2, 2,OK,X_, // [0] OK
- X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, // [1] error
- 3, 2, 2, 2, 2, 2, 2,OK, OK,OK,OK,OK, OK,OK,OK,OK, 2, 2,OK,X_, // [2] NL*
- X_, 2, 4, 9, 10,11, 9,13, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [3] <
- X_, 2, 9, 5, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [4] <!
- X_, 2, 9, 6, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [5] <!-
- 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [6] <!--.*
- 6, 6, 6, 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [7] <!--.*-
- 6, 2, 6, 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [8] <!--.*--
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [9] <.*
- 10,10,10,10, 9,10,10,10, 10,10,10,10, 10,10,10,10, 12,10,10,X_, // [10] <.*"
- 11,11,11,11, 11, 9,11,11, 11,11,11,11, 11,11,11,11, 12,11,11,X_, // [11] <.*'
- X_, 2,12,12, 12,12,12,12, 12,12,12,12, 12,12,12,12, 12,12,12,X_, // [12] <.* no " '
-
-// < > ! - " ' / S C R I P T Y L E CR NL PL xx
- X_, 2, 9, 9, 10,11, 9, 9, 14, 9, 9, 9, 28, 9, 9, 9, 9, 9, 9,X_, // [13] <S
- X_, 2, 9, 9, 10,11, 9, 9, 9,15, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [14] <SC
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9,16, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [15] <SCR
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9,17, 9, 9, 9, 9, 9, 9, 9,X_, // [16] <SCRI
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 18, 9, 9, 9, 9, 9, 9,X_, // [17] <SCRIP
- X_,19, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 19,19, 9,X_, // [18] <SCRIPT
- 20,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [19] <SCRIPT .*
- 19,19,19,19, 19,19,21,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [20] <SCRIPT .*<
- 19,19,19,19, 19,19,19,22, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [21] <SCRIPT .*</
- 19,19,19,19, 19,19,19,19, 23,19,19,19, 19,19,19,19, 19,19,19,X_, // [22] <SCRIPT .*</S
- 19,19,19,19, 19,19,19,19, 19,24,19,19, 19,19,19,19, 19,19,19,X_, // [23] <SCRIPT .*</SC
- 19,19,19,19, 19,19,19,19, 19,19,25,19, 19,19,19,19, 19,19,19,X_, // [24] <SCRIPT .*</SCR
- 19,19,19,19, 19,19,19,19, 19,19,19,26, 19,19,19,19, 19,19,19,X_, // [25] <SCRIPT .*</SCRI
- 19,19,19,19, 19,19,19,19, 19,19,19,19, 27,19,19,19, 19,19,19,X_, // [26] <SCRIPT .*</SCRIP
- 19, 2,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [27] <SCRIPT .*</SCRIPT
-
-// < > ! - " ' / S C R I P T Y L E CR NL PL xx
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9,29, 9, 9, 9, 9, 9,X_, // [28] <ST
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9,30, 9, 9, 9, 9,X_, // [29] <STY
- X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9,31, 9, 9, 9,X_, // [30] <STYL
- X_,32, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 32,32, 9,X_, // [31] <STYLE
- 33,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [32] <STYLE .*
- 32,32,32,32, 32,32,34,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [33] <STYLE .*<
- 32,32,32,32, 32,32,32,35, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [34] <STYLE .*</
- 32,32,32,32, 32,32,32,32, 32,32,32,32, 36,32,32,32, 32,32,32,X_, // [35] <STYLE .*</S
- 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,37,32,32, 32,32,32,X_, // [36] <STYLE .*</ST
- 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,38,32, 32,32,32,X_, // [37] <STYLE .*</STY
- 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,39, 32,32,32,X_, // [38] <STYLE .*</STYL
- 32, 2,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [39] <STYLE .*</STYLE
-};
-
-#undef OK
-#undef X_
-
-
-/*
-// Convert GetTimeOfDay output to 64-bit usec
-static inline uint64 Microseconds(const struct timeval& t) {
- // The SumReducer uses uint64, so convert to (uint64) microseconds,
- // not (double) seconds.
- return t.tv_sec * 1000000ULL + t.tv_usec;
-}
-*/
-
-
-// Returns true if character is < > or &
-bool inline IsSpecial(char c) {
- if ((c & 0xe0) == 0x20) {
- return kSpecialSymbol[static_cast<uint8>(c)];
- }
- return false;
-}
-
-// Quick Skip to next letter or < > & or to end of string (eos)
-// Always return is_letter for eos
-int ScanToLetterOrSpecial(const char* src, int len) {
- int bytes_consumed;
- cld::UTF8GenericScan(&utf8scannotjustletterspecial_obj, src, len,
- &bytes_consumed);
- return bytes_consumed;
-}
-
-
-
-// src points to non-letter, such as tag-opening '<'
-// Return length from here to next possible letter
-// On eos or another < before >, return 1
-// advances <tag>
-// | |
-// advances <tag> ... </tag> for <script> <style>
-// | |
-// advances <!-- ... <tag> ... -->
-// | |
-// advances <tag
-// || (1)
-// advances <tag <tag2>
-// || (1)
-int ScanToPossibleLetter(const char* isrc, int len) {
- const uint8* src = reinterpret_cast<const uint8*>(isrc);
- const uint8* srclimit = src + len;
- const uint8* tagParseTbl = kTagParseTbl_0;
- int e = 0;
- while (src < srclimit) {
- e = tagParseTbl[kCharToSub[*src++]];
- if ((e & ~1) == 0) {
- // We overshot by one byte
- --src;
- break;
- }
- tagParseTbl = &kTagParseTbl_0[e * 20];
- }
-
- if (src >= srclimit) {
- // We fell off the end of the text.
- // It looks like the most common case for this is a truncated file, not
- // mismatched angle brackets. So we pretend that the last char was '>'
- return len;
- }
-
- // OK to be in state 0 or state 2 at exit
- if ((e != 0) && (e != 2)) {
- // Error, '<' followed by '<'
- // We want to back up to first <, then advance by one byte past it
- int offset = src - reinterpret_cast<const uint8*>(isrc);
- // printf("ScanToPossibleLetter error at %d[%d] in '%s'\n",offset,e,isrc);
-
- // Backscan to first '<' and return enough length to just get past it
- --offset; // back up over the second '<', which caused us to stop
- while ((0 < offset) && (isrc[offset] != '<')) {
- // Find the first '<', which is unmatched
- --offset;
- }
- // skip to just beyond first '<'
- // printf(" returning %d\n", offset + 1);
- return offset + 1;
- }
-
- return src - reinterpret_cast<const uint8*>(isrc);
-}
-
-
-
-ScriptScanner::ScriptScanner(const char* buffer,
- int buffer_length,
- bool is_plain_text)
- : start_byte_(buffer),
- next_byte_(buffer),
- next_byte_limit_(buffer + buffer_length),
- byte_length_(buffer_length),
- is_plain_text_(is_plain_text) {
- script_buffer_ = new char[getone::kMaxScriptBuffer];
- script_buffer_lower_ = new char[getone::kMaxScriptLowerBuffer];
-}
-
-ScriptScanner::~ScriptScanner() {
- delete[] script_buffer_;
- delete[] script_buffer_lower_;
-}
-
-
-
-
-// Get to the first real non-tag letter or entity that is a letter
-// Sets script of that letter
-// Return len if no more letters
-int ScriptScanner::SkipToFrontOfSpan(const char* src, int len, int* script) {
- int sc = UNKNOWN_LSCRIPT;
- int skip = 0;
- int tlen, plen;
-
- // Do run of non-letters (tag | &NL | NL)*
- while (skip < len) {
- // Do fast scan to next interesting byte
- // int oldskip = skip;
- skip += ScanToLetterOrSpecial(src + skip, len - skip);
- // TEMP
- // printf("ScanToLetterOrSpecial[%d] 0x%02x => [%d] 0x%02x\n",
- // oldskip, src[oldskip], skip, src[skip]);
-
- // Check for no more letters/specials
- if (skip >= len) {
- // All done
- return len;
- }
-
- // We are at a letter, nonletter, tag, or entity
- if (IsSpecial(src[skip]) && !is_plain_text_) {
- if (src[skip] == '<') {
- // Begining of tag; skip to end and go around again
- tlen = ScanToPossibleLetter(src + skip, len - skip);
- sc = 0;
- // printf("<...> ");
- } else if (src[skip] == '>') {
- // Unexpected end of tag; skip it and go around again
- tlen = 1; // Over the >
- sc = 0;
- // printf("..> ");
- } else if (src[skip] == '&') {
- // Expand entity, no advance
- char temp[4];
- EntityToBuffer(src + skip, len - skip,
- temp, &tlen, &plen);
- sc = getone::GetUTF8LetterScriptNum(temp);
- // printf("#(%02x%02x)=%d ", temp[0], temp[1], sc);
- }
- } else {
- // Update 1..4 bytes
- tlen = cld_UniLib::OneCharLen(src + skip);
- sc = getone::GetUTF8LetterScriptNum(src + skip);
- // printf("#(%02x%02x)=%d ", src[skip], src[skip+1], sc);
- }
- // TEMP
- // printf("sc=%d ", sc);
- if (sc != 0) {break;} // Letter found
- skip += tlen; // Advance
- }
-
- *script = sc;
- return skip;
-}
-
-
-
-// Copy next run of same-script non-tag letters to buffer [NUL terminated]
-// Buffer has leading space and all text is lowercased
-bool ScriptScanner::GetOneScriptSpan(getone::LangSpan* span) {
- span->text = script_buffer_;
- span->text_bytes = 0;
- span->offset = next_byte_ - start_byte_;
- span->script = UNKNOWN_LSCRIPT;
- span->lang = UNKNOWN_LANGUAGE;
- span->truncated = false;
-
- // printf("GetOneScriptSpan[[ ");
- // struct timeval script_start, script_mid, script_end;
-
- int spanscript; // The script of this span
- int sc = UNKNOWN_LSCRIPT; // The script of next character
- int tlen, plen;
-
-
- script_buffer_[0] = ' '; // Always a space at front of output
- script_buffer_[1] = '\0';
- int take = 0;
- int put = 1; // Start after the initial space
-
- // gettimeofday(&script_start, NULL);
- // Get to the first real non-tag letter or entity that is a letter
- int skip = SkipToFrontOfSpan(next_byte_, byte_length_, &spanscript);
- next_byte_ += skip;
- byte_length_ -= skip;
- if (byte_length_ <= 0) {
- // printf("]]\n");
- return false; // No more letters to be found
- }
-
- // gettimeofday(&script_mid, NULL);
-
- // There is at least one letter, so we know the script for this span
- // printf("{%d} ", spanscript);
- span->script = (UnicodeLScript)spanscript;
-
-
- // Go over alternating spans of same-script letters and non-letters,
- // copying letters to buffer with single spaces for each run of non-letters
- while (take < byte_length_) {
- // Copy run of letters in same script (&LS | LS)*
- int letter_count = 0; // Keep track of word length
- bool need_break = false;
- while (take < byte_length_) {
- // We are at a letter, nonletter, tag, or entity
- if (IsSpecial(next_byte_[take]) && !is_plain_text_) {
- // printf("\"%c\" ", next_byte_[take]);
- if (next_byte_[take] == '<') {
- // Begining of tag
- sc = 0;
- break;
- } else if (next_byte_[take] == '>') {
- // Unexpected end of tag
- sc = 0;
- break;
- } else if (next_byte_[take] == '&') {
- // Copy entity, no advance
- EntityToBuffer(next_byte_ + take, byte_length_ - take,
- script_buffer_ + put, &tlen, &plen);
- sc = getone::GetUTF8LetterScriptNum(script_buffer_ + put);
- }
- } else {
- // Real letter, safely copy up to 4 bytes, increment by 1..4
- // Will update by 1..4 bytes at Advance, below
- tlen = plen = cld_UniLib::OneCharLen(next_byte_ + take);
- if (take < (byte_length_ - 3)) {
- // Fast case
- *reinterpret_cast<uint32*>(script_buffer_ + put) =
- *reinterpret_cast<const uint32*>(next_byte_ + take);
- } else {
- // Slow case, happens 1-3 times per input document
- memcpy(script_buffer_ + put, next_byte_ + take, plen);
- }
- sc = getone::GetUTF8LetterScriptNum(next_byte_ + take);
- }
- // printf("sc(%c)=%d ", next_byte_[take], sc);
- // char xtmp[8]; memcpy(xtmp,script_buffer_ + put, plen);
- // xtmp[plen] = '\0'; printf("'%s'{%d} ", xtmp, sc);
-
- // Allow continue across a single letter in a different script:
- // A B D = three scripts, c = common script, i = inherited script,
- // - = don't care, ( = take position before the += below
- // AAA(A- continue
- //
- // AAA(BA continue
- // AAA(BB break
- // AAA(Bc continue (breaks after B)
- // AAA(BD break
- // AAA(Bi break
- //
- // AAA(c- break
- //
- // AAA(i- continue
- //
-
- if ((sc != spanscript) && (sc != ULScript_Inherited)) {
- // Might need to break this script span
- if (sc == ULScript_Common) {
- need_break = true;
- } else {
- // Look at next following character, ignoring entity as Common
- int sc2 = getone::GetUTF8LetterScriptNum(next_byte_ + take + tlen);
- if ((sc2 != ULScript_Common) && (sc2 != spanscript)) {
- need_break = true;
- }
- }
- }
- if (need_break) {break;} // Non-letter or letter in wrong script
-
- take += tlen; // Advance
- put += plen; // Advance
- ++letter_count;
- if (put >= getone::kMaxScriptBytes) {
- // Buffer is full
- span->truncated = true;
- break;
- }
- } // End while letters
-
- // Do run of non-letters (tag | &NL | NL)*
- while (take < byte_length_) {
- // Do fast scan to next interesting byte
- take += ScanToLetterOrSpecial(next_byte_ + take, byte_length_ - take);
-
- // Check for no more letters/specials
- if (take >= byte_length_) {
- take = byte_length_;
- break;
- }
-
- // We are at a letter, nonletter, tag, or entity
- if (IsSpecial(next_byte_[take]) && !is_plain_text_) {
- // printf("\"%c\" ", next_byte_[take]);
- if (next_byte_[take] == '<') {
- // Begining of tag; skip to end and go around again
- tlen = ScanToPossibleLetter(next_byte_ + take, byte_length_ - take);
- sc = 0;
- // printf("<...> ");
- } else if (next_byte_[take] == '>') {
- // Unexpected end of tag; skip it and go around again
- tlen = 1; // Over the >
- sc = 0;
- // printf("..> ");
- } else if (next_byte_[take] == '&') {
- // Expand entity, no advance
- EntityToBuffer(next_byte_ + take, byte_length_ - take,
- script_buffer_ + put, &tlen, &plen);
- sc = getone::GetUTF8LetterScriptNum(script_buffer_ + put);
- }
- } else {
- // Update 1..4
- tlen = cld_UniLib::OneCharLen(next_byte_ + take);
- sc = getone::GetUTF8LetterScriptNum(next_byte_ + take);
- }
- // printf("sc[%c]=%d ", next_byte_[take], sc);
- if (sc != 0) {break;} // Letter found
- take += tlen; // Advance
- } // End while not-letters
-
- script_buffer_[put++] = ' ';
-
- // We are at a letter again (or eos), after letter* not-letter*
- if (sc != spanscript) {break;} // Letter in wrong script
- if (put >= getone::kMaxScriptBytes - 8) {
- // Buffer is almost full
- span->truncated = true;
- break;
- }
- }
-
- // Update input position
- next_byte_ += take;
- byte_length_ -= take;
-
- // Put four more spaces/NUL. Worst case is abcd _ _ _ \0
- // kMaxScriptBytes | | put
- script_buffer_[put + 0] = ' ';
- script_buffer_[put + 1] = ' ';
- script_buffer_[put + 2] = ' ';
- script_buffer_[put + 3] = '\0';
-
- span->text_bytes = put; // Does not include the last four chars above
-
- // printf(" %d]]\n\n", put);
- return true;
-}
-
-// Force Latin, Cyrillic, Greek scripts to be lowercase
-void ScriptScanner::LowerScriptSpan(getone::LangSpan* span) {
- // On Windows, text is lowercased beforehand, so no need to do anything here.
-#if !defined(CLD_WINDOWS)
- // If needed, lowercase all the text. If we do it sooner, might miss
- // lowercasing an entity such as Á
- // We only need to do this for Latn and Cyrl scripts
- if ((span->script == ULScript_Latin) ||
- (span->script == ULScript_Cyrillic) ||
- (span->script == ULScript_Greek)) {
- // Full Unicode lowercase of the entire buffer, including
- // four pad bytes off the end
- int consumed, filled;
- UniLib::ToLower(span->text, span->text_bytes + 4,
- script_buffer_lower_, getone::kMaxScriptLowerBuffer,
- &consumed, &filled);
- span->text = script_buffer_lower_;
- span->text_bytes = filled - 4;
- }
-#endif
-}
-
-// Copy next run of same-script non-tag letters to buffer [NUL terminated]
-// Force Latin and Cyrillic scripts to be lowercase
-bool ScriptScanner::GetOneScriptSpanLower(getone::LangSpan* span) {
- bool ok = GetOneScriptSpan(span);
- LowerScriptSpan(span);
- return ok;
-}
-
-// Gets lscript number for letters; always returns
-// 0 (common script) for non-letters
-int getone::GetUTF8LetterScriptNum(const char* src) {
- int srclen = cld_UniLib::OneCharLen(src);
- const uint8* usrc = reinterpret_cast<const uint8*>(src);
- return UTF8GenericProperty(&utf8propletterscriptnum_obj, &usrc, &srclen);
-}
+// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/getonescriptspan.h" +#include <stdio.h> +#include <string.h> + +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/lang_enc.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8propjustletter.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8propletterscriptnum.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/utf8scannotjustletterspecial.h" + +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_basictypes.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_commandlineflags.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_google.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_htmlutils.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_unilib.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_utf8statetable.h" +#include "third_party/cld/bar/toolbar/cld/i18n/encodings/compact_lang_det/win/cld_utf8utils.h" + +static const Language GRAY_LANG = (Language)254; + +static const int kMaxUpToWordBoundary = 50; // span < this make longer, + // else make shorter +static const int kMaxAdvanceToWordBoundary = 10; // +/- this many bytes + // to round to word boundary, + // direction above + +static const char kSpecialSymbol[256] = { // true for < > & + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,1,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,1,0,1,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, +}; + + + +#define LT 0 // < +#define GT 1 // > +#define EX 2 // ! +#define HY 3 // - +#define QU 4 // " +#define AP 5 // ' +#define SL 6 // / +#define S_ 7 +#define C_ 8 +#define R_ 9 +#define I_ 10 +#define P_ 11 +#define T_ 12 +#define Y_ 13 +#define L_ 14 +#define E_ 15 +#define CR 16 // <cr> or <lf> +#define NL 17 // non-letter: ASCII whitespace, digit, punctuation +#define PL 18 // possible letter, incl. & +#define xx 19 // <unused> + +// Map byte to one of ~20 interesting categories for cheap tag parsing +static const uint8 kCharToSub[256] = { + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,CR,NL, NL,CR,NL,NL, + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, + NL,EX,QU,NL, NL,NL,PL,AP, NL,NL,NL,NL, NL,HY,NL,SL, + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, LT,NL,GT,NL, + + PL,PL,PL,C_, PL,E_,PL,PL, PL,I_,PL,PL, L_,PL,PL,PL, + P_,PL,R_,S_, T_,PL,PL,PL, PL,Y_,PL,NL, NL,NL,NL,NL, + PL,PL,PL,C_, PL,E_,PL,PL, PL,I_,PL,PL, L_,PL,PL,PL, + P_,PL,R_,S_, T_,PL,PL,PL, PL,Y_,PL,NL, NL,NL,NL,NL, + + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, + NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, NL,NL,NL,NL, + + PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, + PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, + PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, + PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, PL,PL,PL,PL, +}; + +#undef LT +#undef GT +#undef EX +#undef HY +#undef QU +#undef AP +#undef SL +#undef S_ +#undef C_ +#undef R_ +#undef I_ +#undef P_ +#undef T_ +#undef Y_ +#undef L_ +#undef E_ +#undef CR +#undef NL +#undef PL +#undef xx + + +#define OK 0 +#define X_ 1 + +// State machine to do cheap parse of non-letter strings incl. tags +// advances <tag> +// | | +// advances <tag> ... </tag> for <script> <style> +// | | +// advances <!-- ... <tag> ... --> +// | | +// advances <tag +// || (0) +// advances <tag <tag2> +// || (0) +static const uint8 kTagParseTbl_0[] = { +// < > ! - " ' / S C R I P T Y L E CR NL PL xx + 3, 2, 2, 2, 2, 2, 2,OK, OK,OK,OK,OK, OK,OK,OK,OK, 2, 2,OK,X_, // [0] OK + X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, X_,X_,X_,X_, // [1] error + 3, 2, 2, 2, 2, 2, 2,OK, OK,OK,OK,OK, OK,OK,OK,OK, 2, 2,OK,X_, // [2] NL* + X_, 2, 4, 9, 10,11, 9,13, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [3] < + X_, 2, 9, 5, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [4] <! + X_, 2, 9, 6, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [5] <!- + 6, 6, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [6] <!--.* + 6, 6, 6, 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [7] <!--.*- + 6, 2, 6, 8, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,X_, // [8] <!--.*-- + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [9] <.* + 10,10,10,10, 9,10,10,10, 10,10,10,10, 10,10,10,10, 12,10,10,X_, // [10] <.*" + 11,11,11,11, 11, 9,11,11, 11,11,11,11, 11,11,11,11, 12,11,11,X_, // [11] <.*' + X_, 2,12,12, 12,12,12,12, 12,12,12,12, 12,12,12,12, 12,12,12,X_, // [12] <.* no " ' + +// < > ! - " ' / S C R I P T Y L E CR NL PL xx + X_, 2, 9, 9, 10,11, 9, 9, 14, 9, 9, 9, 28, 9, 9, 9, 9, 9, 9,X_, // [13] <S + X_, 2, 9, 9, 10,11, 9, 9, 9,15, 9, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [14] <SC + X_, 2, 9, 9, 10,11, 9, 9, 9, 9,16, 9, 9, 9, 9, 9, 9, 9, 9,X_, // [15] <SCR + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9,17, 9, 9, 9, 9, 9, 9, 9,X_, // [16] <SCRI + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 18, 9, 9, 9, 9, 9, 9,X_, // [17] <SCRIP + X_,19, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 19,19, 9,X_, // [18] <SCRIPT + 20,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [19] <SCRIPT .* + 19,19,19,19, 19,19,21,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [20] <SCRIPT .*< + 19,19,19,19, 19,19,19,22, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [21] <SCRIPT .*</ + 19,19,19,19, 19,19,19,19, 23,19,19,19, 19,19,19,19, 19,19,19,X_, // [22] <SCRIPT .*</S + 19,19,19,19, 19,19,19,19, 19,24,19,19, 19,19,19,19, 19,19,19,X_, // [23] <SCRIPT .*</SC + 19,19,19,19, 19,19,19,19, 19,19,25,19, 19,19,19,19, 19,19,19,X_, // [24] <SCRIPT .*</SCR + 19,19,19,19, 19,19,19,19, 19,19,19,26, 19,19,19,19, 19,19,19,X_, // [25] <SCRIPT .*</SCRI + 19,19,19,19, 19,19,19,19, 19,19,19,19, 27,19,19,19, 19,19,19,X_, // [26] <SCRIPT .*</SCRIP + 19, 2,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,19, 19,19,19,X_, // [27] <SCRIPT .*</SCRIPT + +// < > ! - " ' / S C R I P T Y L E CR NL PL xx + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9,29, 9, 9, 9, 9, 9,X_, // [28] <ST + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9,30, 9, 9, 9, 9,X_, // [29] <STY + X_, 2, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9,31, 9, 9, 9,X_, // [30] <STYL + X_,32, 9, 9, 10,11, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 32,32, 9,X_, // [31] <STYLE + 33,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [32] <STYLE .* + 32,32,32,32, 32,32,34,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [33] <STYLE .*< + 32,32,32,32, 32,32,32,35, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [34] <STYLE .*</ + 32,32,32,32, 32,32,32,32, 32,32,32,32, 36,32,32,32, 32,32,32,X_, // [35] <STYLE .*</S + 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,37,32,32, 32,32,32,X_, // [36] <STYLE .*</ST + 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,38,32, 32,32,32,X_, // [37] <STYLE .*</STY + 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,39, 32,32,32,X_, // [38] <STYLE .*</STYL + 32, 2,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,32, 32,32,32,X_, // [39] <STYLE .*</STYLE +}; + +#undef OK +#undef X_ + + +/* +// Convert GetTimeOfDay output to 64-bit usec +static inline uint64 Microseconds(const struct timeval& t) { + // The SumReducer uses uint64, so convert to (uint64) microseconds, + // not (double) seconds. + return t.tv_sec * 1000000ULL + t.tv_usec; +} +*/ + + +// Returns true if character is < > or & +bool inline IsSpecial(char c) { + if ((c & 0xe0) == 0x20) { + return kSpecialSymbol[static_cast<uint8>(c)]; + } + return false; +} + +// Quick Skip to next letter or < > & or to end of string (eos) +// Always return is_letter for eos +int ScanToLetterOrSpecial(const char* src, int len) { + int bytes_consumed; + cld::UTF8GenericScan(&utf8scannotjustletterspecial_obj, src, len, + &bytes_consumed); + return bytes_consumed; +} + + + +// src points to non-letter, such as tag-opening '<' +// Return length from here to next possible letter +// On eos or another < before >, return 1 +// advances <tag> +// | | +// advances <tag> ... </tag> for <script> <style> +// | | +// advances <!-- ... <tag> ... --> +// | | +// advances <tag +// || (1) +// advances <tag <tag2> +// || (1) +int ScanToPossibleLetter(const char* isrc, int len) { + const uint8* src = reinterpret_cast<const uint8*>(isrc); + const uint8* srclimit = src + len; + const uint8* tagParseTbl = kTagParseTbl_0; + int e = 0; + while (src < srclimit) { + e = tagParseTbl[kCharToSub[*src++]]; + if ((e & ~1) == 0) { + // We overshot by one byte + --src; + break; + } + tagParseTbl = &kTagParseTbl_0[e * 20]; + } + + if (src >= srclimit) { + // We fell off the end of the text. + // It looks like the most common case for this is a truncated file, not + // mismatched angle brackets. So we pretend that the last char was '>' + return len; + } + + // OK to be in state 0 or state 2 at exit + if ((e != 0) && (e != 2)) { + // Error, '<' followed by '<' + // We want to back up to first <, then advance by one byte past it + int offset = src - reinterpret_cast<const uint8*>(isrc); + // printf("ScanToPossibleLetter error at %d[%d] in '%s'\n",offset,e,isrc); + + // Backscan to first '<' and return enough length to just get past it + --offset; // back up over the second '<', which caused us to stop + while ((0 < offset) && (isrc[offset] != '<')) { + // Find the first '<', which is unmatched + --offset; + } + // skip to just beyond first '<' + // printf(" returning %d\n", offset + 1); + return offset + 1; + } + + return src - reinterpret_cast<const uint8*>(isrc); +} + + + +ScriptScanner::ScriptScanner(const char* buffer, + int buffer_length, + bool is_plain_text) + : start_byte_(buffer), + next_byte_(buffer), + next_byte_limit_(buffer + buffer_length), + byte_length_(buffer_length), + is_plain_text_(is_plain_text) { + script_buffer_ = new char[getone::kMaxScriptBuffer]; + script_buffer_lower_ = new char[getone::kMaxScriptLowerBuffer]; +} + +ScriptScanner::~ScriptScanner() { + delete[] script_buffer_; + delete[] script_buffer_lower_; +} + + + + +// Get to the first real non-tag letter or entity that is a letter +// Sets script of that letter +// Return len if no more letters +int ScriptScanner::SkipToFrontOfSpan(const char* src, int len, int* script) { + int sc = UNKNOWN_LSCRIPT; + int skip = 0; + int tlen, plen; + + // Do run of non-letters (tag | &NL | NL)* + while (skip < len) { + // Do fast scan to next interesting byte + // int oldskip = skip; + skip += ScanToLetterOrSpecial(src + skip, len - skip); + // TEMP + // printf("ScanToLetterOrSpecial[%d] 0x%02x => [%d] 0x%02x\n", + // oldskip, src[oldskip], skip, src[skip]); + + // Check for no more letters/specials + if (skip >= len) { + // All done + return len; + } + + // We are at a letter, nonletter, tag, or entity + if (IsSpecial(src[skip]) && !is_plain_text_) { + if (src[skip] == '<') { + // Begining of tag; skip to end and go around again + tlen = ScanToPossibleLetter(src + skip, len - skip); + sc = 0; + // printf("<...> "); + } else if (src[skip] == '>') { + // Unexpected end of tag; skip it and go around again + tlen = 1; // Over the > + sc = 0; + // printf("..> "); + } else if (src[skip] == '&') { + // Expand entity, no advance + char temp[4]; + EntityToBuffer(src + skip, len - skip, + temp, &tlen, &plen); + sc = getone::GetUTF8LetterScriptNum(temp); + // printf("#(%02x%02x)=%d ", temp[0], temp[1], sc); + } + } else { + // Update 1..4 bytes + tlen = cld_UniLib::OneCharLen(src + skip); + sc = getone::GetUTF8LetterScriptNum(src + skip); + // printf("#(%02x%02x)=%d ", src[skip], src[skip+1], sc); + } + // TEMP + // printf("sc=%d ", sc); + if (sc != 0) {break;} // Letter found + skip += tlen; // Advance + } + + *script = sc; + return skip; +} + + + +// Copy next run of same-script non-tag letters to buffer [NUL terminated] +// Buffer has leading space and all text is lowercased +bool ScriptScanner::GetOneScriptSpan(getone::LangSpan* span) { + span->text = script_buffer_; + span->text_bytes = 0; + span->offset = next_byte_ - start_byte_; + span->script = UNKNOWN_LSCRIPT; + span->lang = UNKNOWN_LANGUAGE; + span->truncated = false; + + // printf("GetOneScriptSpan[[ "); + // struct timeval script_start, script_mid, script_end; + + int spanscript; // The script of this span + int sc = UNKNOWN_LSCRIPT; // The script of next character + int tlen, plen; + + + script_buffer_[0] = ' '; // Always a space at front of output + script_buffer_[1] = '\0'; + int take = 0; + int put = 1; // Start after the initial space + + // gettimeofday(&script_start, NULL); + // Get to the first real non-tag letter or entity that is a letter + int skip = SkipToFrontOfSpan(next_byte_, byte_length_, &spanscript); + next_byte_ += skip; + byte_length_ -= skip; + if (byte_length_ <= 0) { + // printf("]]\n"); + return false; // No more letters to be found + } + + // gettimeofday(&script_mid, NULL); + + // There is at least one letter, so we know the script for this span + // printf("{%d} ", spanscript); + span->script = (UnicodeLScript)spanscript; + + + // Go over alternating spans of same-script letters and non-letters, + // copying letters to buffer with single spaces for each run of non-letters + while (take < byte_length_) { + // Copy run of letters in same script (&LS | LS)* + int letter_count = 0; // Keep track of word length + bool need_break = false; + while (take < byte_length_) { + // We are at a letter, nonletter, tag, or entity + if (IsSpecial(next_byte_[take]) && !is_plain_text_) { + // printf("\"%c\" ", next_byte_[take]); + if (next_byte_[take] == '<') { + // Begining of tag + sc = 0; + break; + } else if (next_byte_[take] == '>') { + // Unexpected end of tag + sc = 0; + break; + } else if (next_byte_[take] == '&') { + // Copy entity, no advance + EntityToBuffer(next_byte_ + take, byte_length_ - take, + script_buffer_ + put, &tlen, &plen); + sc = getone::GetUTF8LetterScriptNum(script_buffer_ + put); + } + } else { + // Real letter, safely copy up to 4 bytes, increment by 1..4 + // Will update by 1..4 bytes at Advance, below + tlen = plen = cld_UniLib::OneCharLen(next_byte_ + take); + if (take < (byte_length_ - 3)) { + // Fast case + *reinterpret_cast<uint32*>(script_buffer_ + put) = + *reinterpret_cast<const uint32*>(next_byte_ + take); + } else { + // Slow case, happens 1-3 times per input document + memcpy(script_buffer_ + put, next_byte_ + take, plen); + } + sc = getone::GetUTF8LetterScriptNum(next_byte_ + take); + } + // printf("sc(%c)=%d ", next_byte_[take], sc); + // char xtmp[8]; memcpy(xtmp,script_buffer_ + put, plen); + // xtmp[plen] = '\0'; printf("'%s'{%d} ", xtmp, sc); + + // Allow continue across a single letter in a different script: + // A B D = three scripts, c = common script, i = inherited script, + // - = don't care, ( = take position before the += below + // AAA(A- continue + // + // AAA(BA continue + // AAA(BB break + // AAA(Bc continue (breaks after B) + // AAA(BD break + // AAA(Bi break + // + // AAA(c- break + // + // AAA(i- continue + // + + if ((sc != spanscript) && (sc != ULScript_Inherited)) { + // Might need to break this script span + if (sc == ULScript_Common) { + need_break = true; + } else { + // Look at next following character, ignoring entity as Common + int sc2 = getone::GetUTF8LetterScriptNum(next_byte_ + take + tlen); + if ((sc2 != ULScript_Common) && (sc2 != spanscript)) { + need_break = true; + } + } + } + if (need_break) {break;} // Non-letter or letter in wrong script + + take += tlen; // Advance + put += plen; // Advance + ++letter_count; + if (put >= getone::kMaxScriptBytes) { + // Buffer is full + span->truncated = true; + break; + } + } // End while letters + + // Do run of non-letters (tag | &NL | NL)* + while (take < byte_length_) { + // Do fast scan to next interesting byte + take += ScanToLetterOrSpecial(next_byte_ + take, byte_length_ - take); + + // Check for no more letters/specials + if (take >= byte_length_) { + take = byte_length_; + break; + } + + // We are at a letter, nonletter, tag, or entity + if (IsSpecial(next_byte_[take]) && !is_plain_text_) { + // printf("\"%c\" ", next_byte_[take]); + if (next_byte_[take] == '<') { + // Begining of tag; skip to end and go around again + tlen = ScanToPossibleLetter(next_byte_ + take, byte_length_ - take); + sc = 0; + // printf("<...> "); + } else if (next_byte_[take] == '>') { + // Unexpected end of tag; skip it and go around again + tlen = 1; // Over the > + sc = 0; + // printf("..> "); + } else if (next_byte_[take] == '&') { + // Expand entity, no advance + EntityToBuffer(next_byte_ + take, byte_length_ - take, + script_buffer_ + put, &tlen, &plen); + sc = getone::GetUTF8LetterScriptNum(script_buffer_ + put); + } + } else { + // Update 1..4 + tlen = cld_UniLib::OneCharLen(next_byte_ + take); + sc = getone::GetUTF8LetterScriptNum(next_byte_ + take); + } + // printf("sc[%c]=%d ", next_byte_[take], sc); + if (sc != 0) {break;} // Letter found + take += tlen; // Advance + } // End while not-letters + + script_buffer_[put++] = ' '; + + // We are at a letter again (or eos), after letter* not-letter* + if (sc != spanscript) {break;} // Letter in wrong script + if (put >= getone::kMaxScriptBytes - 8) { + // Buffer is almost full + span->truncated = true; + break; + } + } + + // Update input position + next_byte_ += take; + byte_length_ -= take; + + // Put four more spaces/NUL. Worst case is abcd _ _ _ \0 + // kMaxScriptBytes | | put + script_buffer_[put + 0] = ' '; + script_buffer_[put + 1] = ' '; + script_buffer_[put + 2] = ' '; + script_buffer_[put + 3] = '\0'; + + span->text_bytes = put; // Does not include the last four chars above + + // printf(" %d]]\n\n", put); + return true; +} + +// Force Latin, Cyrillic, Greek scripts to be lowercase +void ScriptScanner::LowerScriptSpan(getone::LangSpan* span) { + // On Windows, text is lowercased beforehand, so no need to do anything here. +#if !defined(CLD_WINDOWS) + // If needed, lowercase all the text. If we do it sooner, might miss + // lowercasing an entity such as Á + // We only need to do this for Latn and Cyrl scripts + if ((span->script == ULScript_Latin) || + (span->script == ULScript_Cyrillic) || + (span->script == ULScript_Greek)) { + // Full Unicode lowercase of the entire buffer, including + // four pad bytes off the end + int consumed, filled; + UniLib::ToLower(span->text, span->text_bytes + 4, + script_buffer_lower_, getone::kMaxScriptLowerBuffer, + &consumed, &filled); + span->text = script_buffer_lower_; + span->text_bytes = filled - 4; + } +#endif +} + +// Copy next run of same-script non-tag letters to buffer [NUL terminated] +// Force Latin and Cyrillic scripts to be lowercase +bool ScriptScanner::GetOneScriptSpanLower(getone::LangSpan* span) { + bool ok = GetOneScriptSpan(span); + LowerScriptSpan(span); + return ok; +} + +// Gets lscript number for letters; always returns +// 0 (common script) for non-letters +int getone::GetUTF8LetterScriptNum(const char* src) { + int srclen = cld_UniLib::OneCharLen(src); + const uint8* usrc = reinterpret_cast<const uint8*>(src); + return UTF8GenericProperty(&utf8propletterscriptnum_obj, &usrc, &srclen); +} |